1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsalloc")
56 /*******************************************************************************
58 * FUNCTION: AcpiNsCreateNode
60 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
62 * RETURN: New namespace node (Null on failure)
64 * DESCRIPTION: Create a namespace node
66 ******************************************************************************/
72 ACPI_NAMESPACE_NODE
*Node
;
73 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
78 ACPI_FUNCTION_TRACE (NsCreateNode
);
81 Node
= AcpiOsAcquireObject (AcpiGbl_NamespaceCache
);
87 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList
->TotalAllocated
++);
89 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
90 Temp
= AcpiGbl_NsNodeList
->TotalAllocated
-
91 AcpiGbl_NsNodeList
->TotalFreed
;
92 if (Temp
> AcpiGbl_NsNodeList
->MaxOccupied
)
94 AcpiGbl_NsNodeList
->MaxOccupied
= Temp
;
98 Node
->Name
.Integer
= Name
;
99 ACPI_SET_DESCRIPTOR_TYPE (Node
, ACPI_DESC_TYPE_NAMED
);
104 /*******************************************************************************
106 * FUNCTION: AcpiNsDeleteNode
108 * PARAMETERS: Node - Node to be deleted
112 * DESCRIPTION: Delete a namespace node. All node deletions must come through
113 * here. Detaches any attached objects, including any attached
114 * data. If a handler is associated with attached data, it is
115 * invoked before the node is deleted.
117 ******************************************************************************/
121 ACPI_NAMESPACE_NODE
*Node
)
123 ACPI_OPERAND_OBJECT
*ObjDesc
;
124 ACPI_OPERAND_OBJECT
*NextDesc
;
127 ACPI_FUNCTION_NAME (NsDeleteNode
);
130 /* Detach an object if there is one */
132 AcpiNsDetachObject (Node
);
135 * Delete an attached data object list if present (objects that were
136 * attached via AcpiAttachData). Note: After any normal object is
137 * detached above, the only possible remaining object(s) are data
138 * objects, in a linked list.
140 ObjDesc
= Node
->Object
;
142 (ObjDesc
->Common
.Type
== ACPI_TYPE_LOCAL_DATA
))
144 /* Invoke the attached data deletion handler if present */
146 if (ObjDesc
->Data
.Handler
)
148 ObjDesc
->Data
.Handler (Node
, ObjDesc
->Data
.Pointer
);
151 NextDesc
= ObjDesc
->Common
.NextObject
;
152 AcpiUtRemoveReference (ObjDesc
);
156 /* Special case for the statically allocated root node */
158 if (Node
== AcpiGbl_RootNode
)
163 /* Now we can delete the node */
165 (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache
, Node
);
167 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList
->TotalFreed
++);
168 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Node %p, Remaining %X\n",
169 Node
, AcpiGbl_CurrentNodeCount
));
173 /*******************************************************************************
175 * FUNCTION: AcpiNsRemoveNode
177 * PARAMETERS: Node - Node to be removed/deleted
181 * DESCRIPTION: Remove (unlink) and delete a namespace node
183 ******************************************************************************/
187 ACPI_NAMESPACE_NODE
*Node
)
189 ACPI_NAMESPACE_NODE
*ParentNode
;
190 ACPI_NAMESPACE_NODE
*PrevNode
;
191 ACPI_NAMESPACE_NODE
*NextNode
;
194 ACPI_FUNCTION_TRACE_PTR (NsRemoveNode
, Node
);
197 ParentNode
= Node
->Parent
;
200 NextNode
= ParentNode
->Child
;
202 /* Find the node that is the previous peer in the parent's child list */
204 while (NextNode
!= Node
)
207 NextNode
= NextNode
->Peer
;
212 /* Node is not first child, unlink it */
214 PrevNode
->Peer
= Node
->Peer
;
219 * Node is first child (has no previous peer).
220 * Link peer list to parent
222 ParentNode
->Child
= Node
->Peer
;
225 /* Delete the node and any attached objects */
227 AcpiNsDeleteNode (Node
);
232 /*******************************************************************************
234 * FUNCTION: AcpiNsInstallNode
236 * PARAMETERS: WalkState - Current state of the walk
237 * ParentNode - The parent of the new Node
238 * Node - The new Node to install
239 * Type - ACPI object type of the new Node
243 * DESCRIPTION: Initialize a new namespace node and install it amongst
246 * Note: Current namespace lookup is linear search. This appears
247 * to be sufficient as namespace searches consume only a small
248 * fraction of the execution time of the ACPI subsystem.
250 ******************************************************************************/
254 ACPI_WALK_STATE
*WalkState
,
255 ACPI_NAMESPACE_NODE
*ParentNode
, /* Parent */
256 ACPI_NAMESPACE_NODE
*Node
, /* New Child*/
257 ACPI_OBJECT_TYPE Type
)
259 ACPI_OWNER_ID OwnerId
= 0;
260 ACPI_NAMESPACE_NODE
*ChildNode
;
263 ACPI_FUNCTION_TRACE (NsInstallNode
);
269 * Get the owner ID from the Walk state. The owner ID is used to
270 * track table deletion and deletion of objects created by methods.
272 OwnerId
= WalkState
->OwnerId
;
274 if ((WalkState
->MethodDesc
) &&
275 (ParentNode
!= WalkState
->MethodNode
))
278 * A method is creating a new node that is not a child of the
279 * method (it is non-local). Mark the executing method as having
280 * modified the namespace. This is used for cleanup when the
283 WalkState
->MethodDesc
->Method
.InfoFlags
|= ACPI_METHOD_MODIFIED_NAMESPACE
;
287 /* Link the new entry into the parent and existing children */
290 Node
->Parent
= ParentNode
;
291 ChildNode
= ParentNode
->Child
;
295 ParentNode
->Child
= Node
;
299 /* Add node to the end of the peer list */
301 while (ChildNode
->Peer
)
303 ChildNode
= ChildNode
->Peer
;
306 ChildNode
->Peer
= Node
;
309 /* Init the new entry */
311 Node
->OwnerId
= OwnerId
;
312 Node
->Type
= (UINT8
) Type
;
314 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES
,
315 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
316 AcpiUtGetNodeName (Node
), AcpiUtGetTypeName (Node
->Type
), Node
, OwnerId
,
317 AcpiUtGetNodeName (ParentNode
), AcpiUtGetTypeName (ParentNode
->Type
),
324 /*******************************************************************************
326 * FUNCTION: AcpiNsDeleteChildren
328 * PARAMETERS: ParentNode - Delete this objects children
332 * DESCRIPTION: Delete all children of the parent object. In other words,
335 ******************************************************************************/
338 AcpiNsDeleteChildren (
339 ACPI_NAMESPACE_NODE
*ParentNode
)
341 ACPI_NAMESPACE_NODE
*NextNode
;
342 ACPI_NAMESPACE_NODE
*NodeToDelete
;
345 ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren
, ParentNode
);
353 /* Deallocate all children at this level */
355 NextNode
= ParentNode
->Child
;
358 /* Grandchildren should have all been deleted already */
362 ACPI_ERROR ((AE_INFO
, "Found a grandchild! P=%p C=%p",
363 ParentNode
, NextNode
));
367 * Delete this child node and move on to the next child in the list.
368 * No need to unlink the node since we are deleting the entire branch.
370 NodeToDelete
= NextNode
;
371 NextNode
= NextNode
->Peer
;
372 AcpiNsDeleteNode (NodeToDelete
);
375 /* Clear the parent's child pointer */
377 ParentNode
->Child
= NULL
;
382 /*******************************************************************************
384 * FUNCTION: AcpiNsDeleteNamespaceSubtree
386 * PARAMETERS: ParentNode - Root of the subtree to be deleted
390 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
391 * stored within the subtree.
393 ******************************************************************************/
396 AcpiNsDeleteNamespaceSubtree (
397 ACPI_NAMESPACE_NODE
*ParentNode
)
399 ACPI_NAMESPACE_NODE
*ChildNode
= NULL
;
404 ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree
);
412 /* Lock namespace for possible update */
414 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
415 if (ACPI_FAILURE (Status
))
421 * Traverse the tree of objects until we bubble back up
422 * to where we started.
426 /* Get the next node in this scope (NULL if none) */
428 ChildNode
= AcpiNsGetNextNode (ParentNode
, ChildNode
);
431 /* Found a child node - detach any attached object */
433 AcpiNsDetachObject (ChildNode
);
435 /* Check if this node has any children */
437 if (ChildNode
->Child
)
440 * There is at least one child of this node,
444 ParentNode
= ChildNode
;
451 * No more children of this parent node.
452 * Move up to the grandparent.
457 * Now delete all of the children of this parent
458 * all at the same time.
460 AcpiNsDeleteChildren (ParentNode
);
462 /* New "last child" is this parent node */
464 ChildNode
= ParentNode
;
466 /* Move up the tree to the grandparent */
468 ParentNode
= ParentNode
->Parent
;
472 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
477 /*******************************************************************************
479 * FUNCTION: AcpiNsDeleteNamespaceByOwner
481 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
485 * DESCRIPTION: Delete entries within the namespace that are owned by a
486 * specific ID. Used to delete entire ACPI tables. All
487 * reference counts are updated.
489 * MUTEX: Locks namespace during deletion walk.
491 ******************************************************************************/
494 AcpiNsDeleteNamespaceByOwner (
495 ACPI_OWNER_ID OwnerId
)
497 ACPI_NAMESPACE_NODE
*ChildNode
;
498 ACPI_NAMESPACE_NODE
*DeletionNode
;
499 ACPI_NAMESPACE_NODE
*ParentNode
;
504 ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner
, OwnerId
);
512 /* Lock namespace for possible update */
514 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
515 if (ACPI_FAILURE (Status
))
521 ParentNode
= AcpiGbl_RootNode
;
526 * Traverse the tree of nodes until we bubble back up
527 * to where we started.
532 * Get the next child of this parent node. When ChildNode is NULL,
533 * the first child of the parent is returned
535 ChildNode
= AcpiNsGetNextNode (ParentNode
, ChildNode
);
539 AcpiNsDeleteChildren (DeletionNode
);
540 AcpiNsRemoveNode (DeletionNode
);
546 if (ChildNode
->OwnerId
== OwnerId
)
548 /* Found a matching child node - detach any attached object */
550 AcpiNsDetachObject (ChildNode
);
553 /* Check if this node has any children */
555 if (ChildNode
->Child
)
558 * There is at least one child of this node,
562 ParentNode
= ChildNode
;
565 else if (ChildNode
->OwnerId
== OwnerId
)
567 DeletionNode
= ChildNode
;
573 * No more children of this parent node.
574 * Move up to the grandparent.
579 if (ParentNode
->OwnerId
== OwnerId
)
581 DeletionNode
= ParentNode
;
585 /* New "last child" is this parent node */
587 ChildNode
= ParentNode
;
589 /* Move up the tree to the grandparent */
591 ParentNode
= ParentNode
->Parent
;
595 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);