1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
51 #define _COMPONENT ACPI_NAMESPACE
52 ACPI_MODULE_NAME ("nsalloc")
55 /*******************************************************************************
57 * FUNCTION: AcpiNsCreateNode
59 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
61 * RETURN: New namespace node (Null on failure)
63 * DESCRIPTION: Create a namespace node
65 ******************************************************************************/
71 ACPI_NAMESPACE_NODE *Node;
72 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
77 ACPI_FUNCTION_TRACE (NsCreateNode);
80 Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);
86 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalAllocated++);
88 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
89 Temp = AcpiGbl_NsNodeList->TotalAllocated -
90 AcpiGbl_NsNodeList->TotalFreed;
91 if (Temp > AcpiGbl_NsNodeList->MaxOccupied)
93 AcpiGbl_NsNodeList->MaxOccupied = Temp;
97 Node->Name.Integer = Name;
98 ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
103 /*******************************************************************************
105 * FUNCTION: AcpiNsDeleteNode
107 * PARAMETERS: Node - Node to be deleted
111 * DESCRIPTION: Delete a namespace node. All node deletions must come through
112 * here. Detaches any attached objects, including any attached
113 * data. If a handler is associated with attached data, it is
114 * invoked before the node is deleted.
116 ******************************************************************************/
120 ACPI_NAMESPACE_NODE *Node)
122 ACPI_OPERAND_OBJECT *ObjDesc;
123 ACPI_OPERAND_OBJECT *NextDesc;
126 ACPI_FUNCTION_NAME (NsDeleteNode);
129 /* Detach an object if there is one */
131 AcpiNsDetachObject (Node);
134 * Delete an attached data object list if present (objects that were
135 * attached via AcpiAttachData). Note: After any normal object is
136 * detached above, the only possible remaining object(s) are data
137 * objects, in a linked list.
139 ObjDesc = Node->Object;
141 (ObjDesc->Common.Type == ACPI_TYPE_LOCAL_DATA))
143 /* Invoke the attached data deletion handler if present */
145 if (ObjDesc->Data.Handler)
147 ObjDesc->Data.Handler (Node, ObjDesc->Data.Pointer);
150 NextDesc = ObjDesc->Common.NextObject;
151 AcpiUtRemoveReference (ObjDesc);
155 /* Special case for the statically allocated root node */
157 if (Node == AcpiGbl_RootNode)
162 /* Now we can delete the node */
164 (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, Node);
166 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
167 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Node %p, Remaining %X\n",
168 Node, AcpiGbl_CurrentNodeCount));
172 /*******************************************************************************
174 * FUNCTION: AcpiNsRemoveNode
176 * PARAMETERS: Node - Node to be removed/deleted
180 * DESCRIPTION: Remove (unlink) and delete a namespace node
182 ******************************************************************************/
186 ACPI_NAMESPACE_NODE *Node)
188 ACPI_NAMESPACE_NODE *ParentNode;
189 ACPI_NAMESPACE_NODE *PrevNode;
190 ACPI_NAMESPACE_NODE *NextNode;
193 ACPI_FUNCTION_TRACE_PTR (NsRemoveNode, Node);
196 ParentNode = Node->Parent;
199 NextNode = ParentNode->Child;
201 /* Find the node that is the previous peer in the parent's child list */
203 while (NextNode != Node)
206 NextNode = NextNode->Peer;
211 /* Node is not first child, unlink it */
213 PrevNode->Peer = Node->Peer;
218 * Node is first child (has no previous peer).
219 * Link peer list to parent
221 ParentNode->Child = Node->Peer;
224 /* Delete the node and any attached objects */
226 AcpiNsDeleteNode (Node);
231 /*******************************************************************************
233 * FUNCTION: AcpiNsInstallNode
235 * PARAMETERS: WalkState - Current state of the walk
236 * ParentNode - The parent of the new Node
237 * Node - The new Node to install
238 * Type - ACPI object type of the new Node
242 * DESCRIPTION: Initialize a new namespace node and install it amongst
245 * Note: Current namespace lookup is linear search. This appears
246 * to be sufficient as namespace searches consume only a small
247 * fraction of the execution time of the ACPI subsystem.
249 ******************************************************************************/
253 ACPI_WALK_STATE *WalkState,
254 ACPI_NAMESPACE_NODE *ParentNode, /* Parent */
255 ACPI_NAMESPACE_NODE *Node, /* New Child*/
256 ACPI_OBJECT_TYPE Type)
258 ACPI_OWNER_ID OwnerId = 0;
259 ACPI_NAMESPACE_NODE *ChildNode;
262 ACPI_FUNCTION_TRACE (NsInstallNode);
268 * Get the owner ID from the Walk state. The owner ID is used to
269 * track table deletion and deletion of objects created by methods.
271 OwnerId = WalkState->OwnerId;
273 if ((WalkState->MethodDesc) &&
274 (ParentNode != WalkState->MethodNode))
277 * A method is creating a new node that is not a child of the
278 * method (it is non-local). Mark the executing method as having
279 * modified the namespace. This is used for cleanup when the
282 WalkState->MethodDesc->Method.InfoFlags |= ACPI_METHOD_MODIFIED_NAMESPACE;
286 /* Link the new entry into the parent and existing children */
289 Node->Parent = ParentNode;
290 ChildNode = ParentNode->Child;
294 ParentNode->Child = Node;
298 /* Add node to the end of the peer list */
300 while (ChildNode->Peer)
302 ChildNode = ChildNode->Peer;
305 ChildNode->Peer = Node;
308 /* Init the new entry */
310 Node->OwnerId = OwnerId;
311 Node->Type = (UINT8) Type;
313 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
314 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
315 AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type), Node, OwnerId,
316 AcpiUtGetNodeName (ParentNode), AcpiUtGetTypeName (ParentNode->Type),
323 /*******************************************************************************
325 * FUNCTION: AcpiNsDeleteChildren
327 * PARAMETERS: ParentNode - Delete this objects children
331 * DESCRIPTION: Delete all children of the parent object. In other words,
334 ******************************************************************************/
337 AcpiNsDeleteChildren (
338 ACPI_NAMESPACE_NODE *ParentNode)
340 ACPI_NAMESPACE_NODE *NextNode;
341 ACPI_NAMESPACE_NODE *NodeToDelete;
344 ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);
352 /* Deallocate all children at this level */
354 NextNode = ParentNode->Child;
357 /* Grandchildren should have all been deleted already */
361 ACPI_ERROR ((AE_INFO, "Found a grandchild! P=%p C=%p",
362 ParentNode, NextNode));
366 * Delete this child node and move on to the next child in the list.
367 * No need to unlink the node since we are deleting the entire branch.
369 NodeToDelete = NextNode;
370 NextNode = NextNode->Peer;
371 AcpiNsDeleteNode (NodeToDelete);
374 /* Clear the parent's child pointer */
376 ParentNode->Child = NULL;
381 /*******************************************************************************
383 * FUNCTION: AcpiNsDeleteNamespaceSubtree
385 * PARAMETERS: ParentNode - Root of the subtree to be deleted
389 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
390 * stored within the subtree.
392 ******************************************************************************/
395 AcpiNsDeleteNamespaceSubtree (
396 ACPI_NAMESPACE_NODE *ParentNode)
398 ACPI_NAMESPACE_NODE *ChildNode = NULL;
403 ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);
411 /* Lock namespace for possible update */
413 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
414 if (ACPI_FAILURE (Status))
420 * Traverse the tree of objects until we bubble back up
421 * to where we started.
425 /* Get the next node in this scope (NULL if none) */
427 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
430 /* Found a child node - detach any attached object */
432 AcpiNsDetachObject (ChildNode);
434 /* Check if this node has any children */
436 if (ChildNode->Child)
439 * There is at least one child of this node,
443 ParentNode = ChildNode;
450 * No more children of this parent node.
451 * Move up to the grandparent.
456 * Now delete all of the children of this parent
457 * all at the same time.
459 AcpiNsDeleteChildren (ParentNode);
461 /* New "last child" is this parent node */
463 ChildNode = ParentNode;
465 /* Move up the tree to the grandparent */
467 ParentNode = ParentNode->Parent;
471 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
476 /*******************************************************************************
478 * FUNCTION: AcpiNsDeleteNamespaceByOwner
480 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
484 * DESCRIPTION: Delete entries within the namespace that are owned by a
485 * specific ID. Used to delete entire ACPI tables. All
486 * reference counts are updated.
488 * MUTEX: Locks namespace during deletion walk.
490 ******************************************************************************/
493 AcpiNsDeleteNamespaceByOwner (
494 ACPI_OWNER_ID OwnerId)
496 ACPI_NAMESPACE_NODE *ChildNode;
497 ACPI_NAMESPACE_NODE *DeletionNode;
498 ACPI_NAMESPACE_NODE *ParentNode;
503 ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);
511 /* Lock namespace for possible update */
513 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
514 if (ACPI_FAILURE (Status))
520 ParentNode = AcpiGbl_RootNode;
525 * Traverse the tree of nodes until we bubble back up
526 * to where we started.
531 * Get the next child of this parent node. When ChildNode is NULL,
532 * the first child of the parent is returned
534 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
538 AcpiNsDeleteChildren (DeletionNode);
539 AcpiNsRemoveNode (DeletionNode);
545 if (ChildNode->OwnerId == OwnerId)
547 /* Found a matching child node - detach any attached object */
549 AcpiNsDetachObject (ChildNode);
552 /* Check if this node has any children */
554 if (ChildNode->Child)
557 * There is at least one child of this node,
561 ParentNode = ChildNode;
564 else if (ChildNode->OwnerId == OwnerId)
566 DeletionNode = ChildNode;
572 * No more children of this parent node.
573 * Move up to the grandparent.
578 if (ParentNode->OwnerId == OwnerId)
580 DeletionNode = ParentNode;
584 /* New "last child" is this parent node */
586 ChildNode = ParentNode;
588 /* Move up the tree to the grandparent */
590 ParentNode = ParentNode->Parent;
594 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);