Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / components / namespace / nsalloc.c
blob38ce8fce60954c20c3cdcfaa2cd242f8551ce202
1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.
45 #define __NSALLOC_C__
47 #include "acpi.h"
48 #include "accommon.h"
49 #include "acnamesp.h"
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 ******************************************************************************/
68 ACPI_NAMESPACE_NODE *
69 AcpiNsCreateNode (
70 UINT32 Name)
72 ACPI_NAMESPACE_NODE *Node;
73 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
74 UINT32 Temp;
75 #endif
78 ACPI_FUNCTION_TRACE (NsCreateNode);
81 Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);
82 if (!Node)
84 return_PTR (NULL);
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;
96 #endif
98 Node->Name.Integer = Name;
99 ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
100 return_PTR (Node);
104 /*******************************************************************************
106 * FUNCTION: AcpiNsDeleteNode
108 * PARAMETERS: Node - Node to be deleted
110 * RETURN: None
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 ******************************************************************************/
119 void
120 AcpiNsDeleteNode (
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;
141 while (ObjDesc &&
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);
153 ObjDesc = NextDesc;
156 /* Special case for the statically allocated root node */
158 if (Node == AcpiGbl_RootNode)
160 return;
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
179 * RETURN: None
181 * DESCRIPTION: Remove (unlink) and delete a namespace node
183 ******************************************************************************/
185 void
186 AcpiNsRemoveNode (
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;
199 PrevNode = NULL;
200 NextNode = ParentNode->Child;
202 /* Find the node that is the previous peer in the parent's child list */
204 while (NextNode != Node)
206 PrevNode = NextNode;
207 NextNode = NextNode->Peer;
210 if (PrevNode)
212 /* Node is not first child, unlink it */
214 PrevNode->Peer = Node->Peer;
216 else
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);
228 return_VOID;
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
241 * RETURN: None
243 * DESCRIPTION: Initialize a new namespace node and install it amongst
244 * its peers.
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 ******************************************************************************/
252 void
253 AcpiNsInstallNode (
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);
266 if (WalkState)
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
281 * method exits.
283 WalkState->MethodDesc->Method.InfoFlags |= ACPI_METHOD_MODIFIED_NAMESPACE;
287 /* Link the new entry into the parent and existing children */
289 Node->Peer = NULL;
290 Node->Parent = ParentNode;
291 ChildNode = ParentNode->Child;
293 if (!ChildNode)
295 ParentNode->Child = Node;
297 else
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),
318 ParentNode));
320 return_VOID;
324 /*******************************************************************************
326 * FUNCTION: AcpiNsDeleteChildren
328 * PARAMETERS: ParentNode - Delete this objects children
330 * RETURN: None.
332 * DESCRIPTION: Delete all children of the parent object. In other words,
333 * deletes a "scope".
335 ******************************************************************************/
337 void
338 AcpiNsDeleteChildren (
339 ACPI_NAMESPACE_NODE *ParentNode)
341 ACPI_NAMESPACE_NODE *NextNode;
342 ACPI_NAMESPACE_NODE *NodeToDelete;
345 ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);
348 if (!ParentNode)
350 return_VOID;
353 /* Deallocate all children at this level */
355 NextNode = ParentNode->Child;
356 while (NextNode)
358 /* Grandchildren should have all been deleted already */
360 if (NextNode->Child)
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;
378 return_VOID;
382 /*******************************************************************************
384 * FUNCTION: AcpiNsDeleteNamespaceSubtree
386 * PARAMETERS: ParentNode - Root of the subtree to be deleted
388 * RETURN: None.
390 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
391 * stored within the subtree.
393 ******************************************************************************/
395 void
396 AcpiNsDeleteNamespaceSubtree (
397 ACPI_NAMESPACE_NODE *ParentNode)
399 ACPI_NAMESPACE_NODE *ChildNode = NULL;
400 UINT32 Level = 1;
401 ACPI_STATUS Status;
404 ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);
407 if (!ParentNode)
409 return_VOID;
412 /* Lock namespace for possible update */
414 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
415 if (ACPI_FAILURE (Status))
417 return_VOID;
421 * Traverse the tree of objects until we bubble back up
422 * to where we started.
424 while (Level > 0)
426 /* Get the next node in this scope (NULL if none) */
428 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
429 if (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,
441 * visit the node
443 Level++;
444 ParentNode = ChildNode;
445 ChildNode = NULL;
448 else
451 * No more children of this parent node.
452 * Move up to the grandparent.
454 Level--;
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);
473 return_VOID;
477 /*******************************************************************************
479 * FUNCTION: AcpiNsDeleteNamespaceByOwner
481 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
483 * RETURN: Status
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 ******************************************************************************/
493 void
494 AcpiNsDeleteNamespaceByOwner (
495 ACPI_OWNER_ID OwnerId)
497 ACPI_NAMESPACE_NODE *ChildNode;
498 ACPI_NAMESPACE_NODE *DeletionNode;
499 ACPI_NAMESPACE_NODE *ParentNode;
500 UINT32 Level;
501 ACPI_STATUS Status;
504 ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);
507 if (OwnerId == 0)
509 return_VOID;
512 /* Lock namespace for possible update */
514 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
515 if (ACPI_FAILURE (Status))
517 return_VOID;
520 DeletionNode = NULL;
521 ParentNode = AcpiGbl_RootNode;
522 ChildNode = NULL;
523 Level = 1;
526 * Traverse the tree of nodes until we bubble back up
527 * to where we started.
529 while (Level > 0)
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);
537 if (DeletionNode)
539 AcpiNsDeleteChildren (DeletionNode);
540 AcpiNsRemoveNode (DeletionNode);
541 DeletionNode = NULL;
544 if (ChildNode)
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,
559 * visit the node
561 Level++;
562 ParentNode = ChildNode;
563 ChildNode = NULL;
565 else if (ChildNode->OwnerId == OwnerId)
567 DeletionNode = ChildNode;
570 else
573 * No more children of this parent node.
574 * Move up to the grandparent.
576 Level--;
577 if (Level != 0)
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);
596 return_VOID;