1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
6 ******************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #define __NSALLOC_C__
121 #include "acnamesp.h"
124 #define _COMPONENT ACPI_NAMESPACE
125 ACPI_MODULE_NAME ("nsalloc")
128 /*******************************************************************************
130 * FUNCTION: AcpiNsCreateNode
132 * PARAMETERS: AcpiName - Name of the new node
136 * DESCRIPTION: Create a namespace node
138 ******************************************************************************/
140 ACPI_NAMESPACE_NODE *
144 ACPI_NAMESPACE_NODE *Node;
147 ACPI_FUNCTION_TRACE ("NsCreateNode");
150 Node = ACPI_MEM_CALLOCATE (sizeof (ACPI_NAMESPACE_NODE));
156 ACPI_MEM_TRACKING (AcpiGbl_MemoryLists[ACPI_MEM_LIST_NSNODE].TotalAllocated++);
158 Node->Name.Integer = Name;
159 Node->ReferenceCount = 1;
160 ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
166 /*******************************************************************************
168 * FUNCTION: AcpiNsDeleteNode
170 * PARAMETERS: Node - Node to be deleted
174 * DESCRIPTION: Delete a namespace node
176 ******************************************************************************/
180 ACPI_NAMESPACE_NODE *Node)
182 ACPI_NAMESPACE_NODE *ParentNode;
183 ACPI_NAMESPACE_NODE *PrevNode;
184 ACPI_NAMESPACE_NODE *NextNode;
187 ACPI_FUNCTION_TRACE_PTR ("NsDeleteNode", Node);
190 ParentNode = AcpiNsGetParentNode (Node);
193 NextNode = ParentNode->Child;
195 while (NextNode != Node)
198 NextNode = PrevNode->Peer;
203 PrevNode->Peer = NextNode->Peer;
204 if (NextNode->Flags & ANOBJ_END_OF_PEER_LIST)
206 PrevNode->Flags |= ANOBJ_END_OF_PEER_LIST;
211 ParentNode->Child = NextNode->Peer;
215 ACPI_MEM_TRACKING (AcpiGbl_MemoryLists[ACPI_MEM_LIST_NSNODE].TotalFreed++);
218 * Detach an object if there is one then delete the node
220 AcpiNsDetachObject (Node);
221 ACPI_MEM_FREE (Node);
226 #ifdef ACPI_ALPHABETIC_NAMESPACE
227 /*******************************************************************************
229 * FUNCTION: AcpiNsCompareNames
231 * PARAMETERS: Name1 - First name to compare
232 * Name2 - Second name to compare
234 * RETURN: value from strncmp
236 * DESCRIPTION: Compare two ACPI names. Names that are prefixed with an
237 * underscore are forced to be alphabetically first.
239 ******************************************************************************/
246 char ReversedName1[ACPI_NAME_SIZE];
247 char ReversedName2[ACPI_NAME_SIZE];
253 * Replace all instances of "underscore" with a value that is smaller so
254 * that all names that are prefixed with underscore(s) are alphabetically
257 * Reverse the name bytewise so we can just do a 32-bit compare instead
260 for (i = 0, j= (ACPI_NAME_SIZE - 1); i < ACPI_NAME_SIZE; i++, j--)
262 ReversedName1[j] = Name1[i];
265 ReversedName1[j] = '*';
268 ReversedName2[j] = Name2[i];
271 ReversedName2[j] = '*';
275 return (*(int *) ReversedName1 - *(int *) ReversedName2);
280 /*******************************************************************************
282 * FUNCTION: AcpiNsInstallNode
284 * PARAMETERS: WalkState - Current state of the walk
285 * ParentNode - The parent of the new Node
286 * Node - The new Node to install
287 * Type - ACPI object type of the new Node
291 * DESCRIPTION: Initialize a new namespace node and install it amongst
294 * Note: Current namespace lookup is linear search. However, the
295 * nodes are linked in alphabetical order to 1) put all reserved
296 * names (start with underscore) first, and to 2) make a readable
299 ******************************************************************************/
303 ACPI_WALK_STATE *WalkState,
304 ACPI_NAMESPACE_NODE *ParentNode, /* Parent */
305 ACPI_NAMESPACE_NODE *Node, /* New Child*/
306 ACPI_OBJECT_TYPE Type)
308 UINT16 OwnerId = TABLE_ID_DSDT;
309 ACPI_NAMESPACE_NODE *ChildNode;
310 #ifdef ACPI_ALPHABETIC_NAMESPACE
312 ACPI_NAMESPACE_NODE *PreviousChildNode;
316 ACPI_FUNCTION_TRACE ("NsInstallNode");
320 * Get the owner ID from the Walk state
321 * The owner ID is used to track table deletion and
322 * deletion of objects created by methods
326 OwnerId = WalkState->OwnerId;
329 /* Link the new entry into the parent and existing children */
331 ChildNode = ParentNode->Child;
334 ParentNode->Child = Node;
335 Node->Flags |= ANOBJ_END_OF_PEER_LIST;
336 Node->Peer = ParentNode;
340 #ifdef ACPI_ALPHABETIC_NAMESPACE
342 * Walk the list whilst searching for the the correct
343 * alphabetic placement.
345 PreviousChildNode = NULL;
346 while (AcpiNsCompareNames (ChildNode->Name.Ascii, Node->Name.Ascii) < 0)
348 if (ChildNode->Flags & ANOBJ_END_OF_PEER_LIST)
350 /* Last peer; Clear end-of-list flag */
352 ChildNode->Flags &= ~ANOBJ_END_OF_PEER_LIST;
354 /* This node is the new peer to the child node */
356 ChildNode->Peer = Node;
358 /* This node is the new end-of-list */
360 Node->Flags |= ANOBJ_END_OF_PEER_LIST;
361 Node->Peer = ParentNode;
367 PreviousChildNode = ChildNode;
368 ChildNode = ChildNode->Peer;
371 /* Did the node get inserted at the end-of-list? */
373 if (!(Node->Flags & ANOBJ_END_OF_PEER_LIST))
376 * Loop above terminated without reaching the end-of-list.
377 * Insert the new node at the current location
379 if (PreviousChildNode)
381 /* Insert node alphabetically */
383 Node->Peer = ChildNode;
384 PreviousChildNode->Peer = Node;
388 /* Insert node alphabetically at start of list */
390 Node->Peer = ChildNode;
391 ParentNode->Child = Node;
395 while (!(ChildNode->Flags & ANOBJ_END_OF_PEER_LIST))
397 ChildNode = ChildNode->Peer;
400 ChildNode->Peer = Node;
402 /* Clear end-of-list flag */
404 ChildNode->Flags &= ~ANOBJ_END_OF_PEER_LIST;
405 Node->Flags |= ANOBJ_END_OF_PEER_LIST;
406 Node->Peer = ParentNode;
410 /* Init the new entry */
412 Node->OwnerId = OwnerId;
413 Node->Type = (UINT8) Type;
415 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%4.4s (%s) added to %4.4s (%s) %p at %p\n",
416 Node->Name.Ascii, AcpiUtGetTypeName (Node->Type),
417 ParentNode->Name.Ascii, AcpiUtGetTypeName (ParentNode->Type), ParentNode, Node));
420 * Increment the reference count(s) of all parents up to
423 while ((Node = AcpiNsGetParentNode (Node)) != NULL)
425 Node->ReferenceCount++;
432 /*******************************************************************************
434 * FUNCTION: AcpiNsDeleteChildren
436 * PARAMETERS: ParentNode - Delete this objects children
440 * DESCRIPTION: Delete all children of the parent object. In other words,
443 ******************************************************************************/
446 AcpiNsDeleteChildren (
447 ACPI_NAMESPACE_NODE *ParentNode)
449 ACPI_NAMESPACE_NODE *ChildNode;
450 ACPI_NAMESPACE_NODE *NextNode;
454 ACPI_FUNCTION_TRACE_PTR ("NsDeleteChildren", ParentNode);
462 /* If no children, all done! */
464 ChildNode = ParentNode->Child;
471 * Deallocate all children at this level
475 /* Get the things we need */
477 NextNode = ChildNode->Peer;
478 Flags = ChildNode->Flags;
480 /* Grandchildren should have all been deleted already */
482 if (ChildNode->Child)
484 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Found a grandchild! P=%p C=%p\n",
485 ParentNode, ChildNode));
488 /* Now we can free this child object */
490 ACPI_MEM_TRACKING (AcpiGbl_MemoryLists[ACPI_MEM_LIST_NSNODE].TotalFreed++);
492 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Object %p, Remaining %X\n",
493 ChildNode, AcpiGbl_CurrentNodeCount));
496 * Detach an object if there is one, then free the child node
498 AcpiNsDetachObject (ChildNode);
499 ACPI_MEM_FREE (ChildNode);
501 /* And move on to the next child in the list */
503 ChildNode = NextNode;
505 } while (!(Flags & ANOBJ_END_OF_PEER_LIST));
508 /* Clear the parent's child pointer */
510 ParentNode->Child = NULL;
516 /*******************************************************************************
518 * FUNCTION: AcpiNsDeleteNamespaceSubtree
520 * PARAMETERS: ParentNode - Root of the subtree to be deleted
524 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
525 * stored within the subtree.
527 ******************************************************************************/
530 AcpiNsDeleteNamespaceSubtree (
531 ACPI_NAMESPACE_NODE *ParentNode)
533 ACPI_NAMESPACE_NODE *ChildNode = NULL;
537 ACPI_FUNCTION_TRACE ("NsDeleteNamespaceSubtree");
546 * Traverse the tree of objects until we bubble back up
547 * to where we started.
551 /* Get the next node in this scope (NULL if none) */
553 ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode,
557 /* Found a child node - detach any attached object */
559 AcpiNsDetachObject (ChildNode);
561 /* Check if this node has any children */
563 if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, 0))
566 * There is at least one child of this node,
570 ParentNode = ChildNode;
577 * No more children of this parent node.
578 * Move up to the grandparent.
583 * Now delete all of the children of this parent
584 * all at the same time.
586 AcpiNsDeleteChildren (ParentNode);
588 /* New "last child" is this parent node */
590 ChildNode = ParentNode;
592 /* Move up the tree to the grandparent */
594 ParentNode = AcpiNsGetParentNode (ParentNode);
602 /*******************************************************************************
604 * FUNCTION: AcpiNsRemoveReference
606 * PARAMETERS: Node - Named node whose reference count is to be
611 * DESCRIPTION: Remove a Node reference. Decrements the reference count
612 * of all parent Nodes up to the root. Any node along
613 * the way that reaches zero references is freed.
615 ******************************************************************************/
618 AcpiNsRemoveReference (
619 ACPI_NAMESPACE_NODE *Node)
621 ACPI_NAMESPACE_NODE *ParentNode;
622 ACPI_NAMESPACE_NODE *ThisNode;
625 ACPI_FUNCTION_ENTRY ();
629 * Decrement the reference count(s) of this node and all
630 * nodes up to the root, Delete anything with zero remaining references.
635 /* Prepare to move up to parent */
637 ParentNode = AcpiNsGetParentNode (ThisNode);
639 /* Decrement the reference count on this node */
641 ThisNode->ReferenceCount--;
643 /* Delete the node if no more references */
645 if (!ThisNode->ReferenceCount)
647 /* Delete all children and delete the node */
649 AcpiNsDeleteChildren (ThisNode);
650 AcpiNsDeleteNode (ThisNode);
653 ThisNode = ParentNode;
658 /*******************************************************************************
660 * FUNCTION: AcpiNsDeleteNamespaceByOwner
662 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
666 * DESCRIPTION: Delete entries within the namespace that are owned by a
667 * specific ID. Used to delete entire ACPI tables. All
668 * reference counts are updated.
670 ******************************************************************************/
673 AcpiNsDeleteNamespaceByOwner (
676 ACPI_NAMESPACE_NODE *ChildNode;
677 ACPI_NAMESPACE_NODE *DeletionNode;
679 ACPI_NAMESPACE_NODE *ParentNode;
682 ACPI_FUNCTION_TRACE_U32 ("NsDeleteNamespaceByOwner", OwnerId);
685 ParentNode = AcpiGbl_RootNode;
691 * Traverse the tree of nodes until we bubble back up
692 * to where we started.
697 * Get the next child of this parent node. When ChildNode is NULL,
698 * the first child of the parent is returned
700 ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode, ChildNode);
704 AcpiNsRemoveReference (DeletionNode);
710 if (ChildNode->OwnerId == OwnerId)
712 /* Found a matching child node - detach any attached object */
714 AcpiNsDetachObject (ChildNode);
717 /* Check if this node has any children */
719 if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, NULL))
722 * There is at least one child of this node,
726 ParentNode = ChildNode;
729 else if (ChildNode->OwnerId == OwnerId)
731 DeletionNode = ChildNode;
737 * No more children of this parent node.
738 * Move up to the grandparent.
743 if (ParentNode->OwnerId == OwnerId)
745 DeletionNode = ParentNode;
749 /* New "last child" is this parent node */
751 ChildNode = ParentNode;
753 /* Move up the tree to the grandparent */
755 ParentNode = AcpiNsGetParentNode (ParentNode);