1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, 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.
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nsalloc")
53 /*******************************************************************************
55 * FUNCTION: AcpiNsCreateNode
57 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
59 * RETURN: New namespace node (Null on failure)
61 * DESCRIPTION: Create a namespace node
63 ******************************************************************************/
69 ACPI_NAMESPACE_NODE
*Node
;
70 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
75 ACPI_FUNCTION_TRACE (NsCreateNode
);
78 Node
= AcpiOsAcquireObject (AcpiGbl_NamespaceCache
);
84 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList
->TotalAllocated
++);
86 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
87 Temp
= AcpiGbl_NsNodeList
->TotalAllocated
-
88 AcpiGbl_NsNodeList
->TotalFreed
;
89 if (Temp
> AcpiGbl_NsNodeList
->MaxOccupied
)
91 AcpiGbl_NsNodeList
->MaxOccupied
= Temp
;
95 Node
->Name
.Integer
= Name
;
96 ACPI_SET_DESCRIPTOR_TYPE (Node
, ACPI_DESC_TYPE_NAMED
);
101 /*******************************************************************************
103 * FUNCTION: AcpiNsDeleteNode
105 * PARAMETERS: Node - Node to be deleted
109 * DESCRIPTION: Delete a namespace node. All node deletions must come through
110 * here. Detaches any attached objects, including any attached
111 * data. If a handler is associated with attached data, it is
112 * invoked before the node is deleted.
114 ******************************************************************************/
118 ACPI_NAMESPACE_NODE
*Node
)
120 ACPI_OPERAND_OBJECT
*ObjDesc
;
121 ACPI_OPERAND_OBJECT
*NextDesc
;
124 ACPI_FUNCTION_NAME (NsDeleteNode
);
127 /* Detach an object if there is one */
129 AcpiNsDetachObject (Node
);
132 * Delete an attached data object list if present (objects that were
133 * attached via AcpiAttachData). Note: After any normal object is
134 * detached above, the only possible remaining object(s) are data
135 * objects, in a linked list.
137 ObjDesc
= Node
->Object
;
139 (ObjDesc
->Common
.Type
== ACPI_TYPE_LOCAL_DATA
))
141 /* Invoke the attached data deletion handler if present */
143 if (ObjDesc
->Data
.Handler
)
145 ObjDesc
->Data
.Handler (Node
, ObjDesc
->Data
.Pointer
);
148 NextDesc
= ObjDesc
->Common
.NextObject
;
149 AcpiUtRemoveReference (ObjDesc
);
153 /* Special case for the statically allocated root node */
155 if (Node
== AcpiGbl_RootNode
)
160 /* Now we can delete the node */
162 (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache
, Node
);
164 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList
->TotalFreed
++);
165 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Node %p, Remaining %X\n",
166 Node
, AcpiGbl_CurrentNodeCount
));
170 /*******************************************************************************
172 * FUNCTION: AcpiNsRemoveNode
174 * PARAMETERS: Node - Node to be removed/deleted
178 * DESCRIPTION: Remove (unlink) and delete a namespace node
180 ******************************************************************************/
184 ACPI_NAMESPACE_NODE
*Node
)
186 ACPI_NAMESPACE_NODE
*ParentNode
;
187 ACPI_NAMESPACE_NODE
*PrevNode
;
188 ACPI_NAMESPACE_NODE
*NextNode
;
191 ACPI_FUNCTION_TRACE_PTR (NsRemoveNode
, Node
);
194 ParentNode
= Node
->Parent
;
197 NextNode
= ParentNode
->Child
;
199 /* Find the node that is the previous peer in the parent's child list */
201 while (NextNode
!= Node
)
204 NextNode
= NextNode
->Peer
;
209 /* Node is not first child, unlink it */
211 PrevNode
->Peer
= Node
->Peer
;
216 * Node is first child (has no previous peer).
217 * Link peer list to parent
219 ParentNode
->Child
= Node
->Peer
;
222 /* Delete the node and any attached objects */
224 AcpiNsDeleteNode (Node
);
229 /*******************************************************************************
231 * FUNCTION: AcpiNsInstallNode
233 * PARAMETERS: WalkState - Current state of the walk
234 * ParentNode - The parent of the new Node
235 * Node - The new Node to install
236 * Type - ACPI object type of the new Node
240 * DESCRIPTION: Initialize a new namespace node and install it amongst
243 * Note: Current namespace lookup is linear search. This appears
244 * to be sufficient as namespace searches consume only a small
245 * fraction of the execution time of the ACPI subsystem.
247 ******************************************************************************/
251 ACPI_WALK_STATE
*WalkState
,
252 ACPI_NAMESPACE_NODE
*ParentNode
, /* Parent */
253 ACPI_NAMESPACE_NODE
*Node
, /* New Child*/
254 ACPI_OBJECT_TYPE Type
)
256 ACPI_OWNER_ID OwnerId
= 0;
257 ACPI_NAMESPACE_NODE
*ChildNode
;
260 ACPI_FUNCTION_TRACE (NsInstallNode
);
266 * Get the owner ID from the Walk state. The owner ID is used to
267 * track table deletion and deletion of objects created by methods.
269 OwnerId
= WalkState
->OwnerId
;
271 if ((WalkState
->MethodDesc
) &&
272 (ParentNode
!= WalkState
->MethodNode
))
275 * A method is creating a new node that is not a child of the
276 * method (it is non-local). Mark the executing method as having
277 * modified the namespace. This is used for cleanup when the
280 WalkState
->MethodDesc
->Method
.InfoFlags
|=
281 ACPI_METHOD_MODIFIED_NAMESPACE
;
285 /* Link the new entry into the parent and existing children */
288 Node
->Parent
= ParentNode
;
289 ChildNode
= ParentNode
->Child
;
293 ParentNode
->Child
= Node
;
297 /* Add node to the end of the peer list */
299 while (ChildNode
->Peer
)
301 ChildNode
= ChildNode
->Peer
;
304 ChildNode
->Peer
= Node
;
307 /* Init the new entry */
309 Node
->OwnerId
= OwnerId
;
310 Node
->Type
= (UINT8
) Type
;
312 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES
,
313 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
314 AcpiUtGetNodeName (Node
), AcpiUtGetTypeName (Node
->Type
), Node
, OwnerId
,
315 AcpiUtGetNodeName (ParentNode
), AcpiUtGetTypeName (ParentNode
->Type
),
322 /*******************************************************************************
324 * FUNCTION: AcpiNsDeleteChildren
326 * PARAMETERS: ParentNode - Delete this objects children
330 * DESCRIPTION: Delete all children of the parent object. In other words,
333 ******************************************************************************/
336 AcpiNsDeleteChildren (
337 ACPI_NAMESPACE_NODE
*ParentNode
)
339 ACPI_NAMESPACE_NODE
*NextNode
;
340 ACPI_NAMESPACE_NODE
*NodeToDelete
;
343 ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren
, ParentNode
);
351 /* Deallocate all children at this level */
353 NextNode
= ParentNode
->Child
;
356 /* Grandchildren should have all been deleted already */
360 ACPI_ERROR ((AE_INFO
, "Found a grandchild! P=%p C=%p",
361 ParentNode
, NextNode
));
365 * Delete this child node and move on to the next child in the list.
366 * No need to unlink the node since we are deleting the entire branch.
368 NodeToDelete
= NextNode
;
369 NextNode
= NextNode
->Peer
;
370 AcpiNsDeleteNode (NodeToDelete
);
373 /* Clear the parent's child pointer */
375 ParentNode
->Child
= NULL
;
380 /*******************************************************************************
382 * FUNCTION: AcpiNsDeleteNamespaceSubtree
384 * PARAMETERS: ParentNode - Root of the subtree to be deleted
388 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
389 * stored within the subtree.
391 ******************************************************************************/
394 AcpiNsDeleteNamespaceSubtree (
395 ACPI_NAMESPACE_NODE
*ParentNode
)
397 ACPI_NAMESPACE_NODE
*ChildNode
= NULL
;
402 ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree
);
410 /* Lock namespace for possible update */
412 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
413 if (ACPI_FAILURE (Status
))
419 * Traverse the tree of objects until we bubble back up
420 * to where we started.
424 /* Get the next node in this scope (NULL if none) */
426 ChildNode
= AcpiNsGetNextNode (ParentNode
, ChildNode
);
429 /* Found a child node - detach any attached object */
431 AcpiNsDetachObject (ChildNode
);
433 /* Check if this node has any children */
435 if (ChildNode
->Child
)
438 * There is at least one child of this node,
442 ParentNode
= ChildNode
;
449 * No more children of this parent node.
450 * Move up to the grandparent.
455 * Now delete all of the children of this parent
456 * all at the same time.
458 AcpiNsDeleteChildren (ParentNode
);
460 /* New "last child" is this parent node */
462 ChildNode
= ParentNode
;
464 /* Move up the tree to the grandparent */
466 ParentNode
= ParentNode
->Parent
;
470 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
475 /*******************************************************************************
477 * FUNCTION: AcpiNsDeleteNamespaceByOwner
479 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
483 * DESCRIPTION: Delete entries within the namespace that are owned by a
484 * specific ID. Used to delete entire ACPI tables. All
485 * reference counts are updated.
487 * MUTEX: Locks namespace during deletion walk.
489 ******************************************************************************/
492 AcpiNsDeleteNamespaceByOwner (
493 ACPI_OWNER_ID OwnerId
)
495 ACPI_NAMESPACE_NODE
*ChildNode
;
496 ACPI_NAMESPACE_NODE
*DeletionNode
;
497 ACPI_NAMESPACE_NODE
*ParentNode
;
502 ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner
, OwnerId
);
510 /* Lock namespace for possible update */
512 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
513 if (ACPI_FAILURE (Status
))
519 ParentNode
= AcpiGbl_RootNode
;
524 * Traverse the tree of nodes until we bubble back up
525 * to where we started.
530 * Get the next child of this parent node. When ChildNode is NULL,
531 * the first child of the parent is returned
533 ChildNode
= AcpiNsGetNextNode (ParentNode
, ChildNode
);
537 AcpiNsDeleteChildren (DeletionNode
);
538 AcpiNsRemoveNode (DeletionNode
);
544 if (ChildNode
->OwnerId
== OwnerId
)
546 /* Found a matching child node - detach any attached object */
548 AcpiNsDetachObject (ChildNode
);
551 /* Check if this node has any children */
553 if (ChildNode
->Child
)
556 * There is at least one child of this node,
560 ParentNode
= ChildNode
;
563 else if (ChildNode
->OwnerId
== OwnerId
)
565 DeletionNode
= ChildNode
;
571 * No more children of this parent node.
572 * Move up to the grandparent.
577 if (ParentNode
->OwnerId
== OwnerId
)
579 DeletionNode
= ParentNode
;
583 /* New "last child" is this parent node */
585 ChildNode
= ParentNode
;
587 /* Move up the tree to the grandparent */
589 ParentNode
= ParentNode
->Parent
;
593 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);