1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nsalloc")
52 /* Local prototypes */
55 acpi_ns_remove_reference (
56 struct acpi_namespace_node
*node
);
59 /*******************************************************************************
61 * FUNCTION: acpi_ns_create_node
63 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
65 * RETURN: New namespace node (Null on failure)
67 * DESCRIPTION: Create a namespace node
69 ******************************************************************************/
71 struct acpi_namespace_node
*
75 struct acpi_namespace_node
*node
;
78 ACPI_FUNCTION_TRACE ("ns_create_node");
81 node
= ACPI_MEM_CALLOCATE (sizeof (struct acpi_namespace_node
));
86 ACPI_MEM_TRACKING (acpi_gbl_ns_node_list
->total_allocated
++);
88 node
->name
.integer
= name
;
89 node
->reference_count
= 1;
90 ACPI_SET_DESCRIPTOR_TYPE (node
, ACPI_DESC_TYPE_NAMED
);
96 /*******************************************************************************
98 * FUNCTION: acpi_ns_delete_node
100 * PARAMETERS: Node - Node to be deleted
104 * DESCRIPTION: Delete a namespace node
106 ******************************************************************************/
109 acpi_ns_delete_node (
110 struct acpi_namespace_node
*node
)
112 struct acpi_namespace_node
*parent_node
;
113 struct acpi_namespace_node
*prev_node
;
114 struct acpi_namespace_node
*next_node
;
117 ACPI_FUNCTION_TRACE_PTR ("ns_delete_node", node
);
120 parent_node
= acpi_ns_get_parent_node (node
);
123 next_node
= parent_node
->child
;
125 /* Find the node that is the previous peer in the parent's child list */
127 while (next_node
!= node
) {
128 prev_node
= next_node
;
129 next_node
= prev_node
->peer
;
133 /* Node is not first child, unlink it */
135 prev_node
->peer
= next_node
->peer
;
136 if (next_node
->flags
& ANOBJ_END_OF_PEER_LIST
) {
137 prev_node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
141 /* Node is first child (has no previous peer) */
143 if (next_node
->flags
& ANOBJ_END_OF_PEER_LIST
) {
144 /* No peers at all */
146 parent_node
->child
= NULL
;
148 else { /* Link peer list to parent */
150 parent_node
->child
= next_node
->peer
;
154 ACPI_MEM_TRACKING (acpi_gbl_ns_node_list
->total_freed
++);
157 * Detach an object if there is one then delete the node
159 acpi_ns_detach_object (node
);
160 ACPI_MEM_FREE (node
);
165 /*******************************************************************************
167 * FUNCTION: acpi_ns_install_node
169 * PARAMETERS: walk_state - Current state of the walk
170 * parent_node - The parent of the new Node
171 * Node - The new Node to install
172 * Type - ACPI object type of the new Node
176 * DESCRIPTION: Initialize a new namespace node and install it amongst
179 * Note: Current namespace lookup is linear search. This appears
180 * to be sufficient as namespace searches consume only a small
181 * fraction of the execution time of the ACPI subsystem.
183 ******************************************************************************/
186 acpi_ns_install_node (
187 struct acpi_walk_state
*walk_state
,
188 struct acpi_namespace_node
*parent_node
, /* Parent */
189 struct acpi_namespace_node
*node
, /* New Child*/
190 acpi_object_type type
)
192 acpi_owner_id owner_id
= 0;
193 struct acpi_namespace_node
*child_node
;
196 ACPI_FUNCTION_TRACE ("ns_install_node");
200 * Get the owner ID from the Walk state
201 * The owner ID is used to track table deletion and
202 * deletion of objects created by methods
205 owner_id
= walk_state
->owner_id
;
208 /* Link the new entry into the parent and existing children */
210 child_node
= parent_node
->child
;
212 parent_node
->child
= node
;
213 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
214 node
->peer
= parent_node
;
217 while (!(child_node
->flags
& ANOBJ_END_OF_PEER_LIST
)) {
218 child_node
= child_node
->peer
;
221 child_node
->peer
= node
;
223 /* Clear end-of-list flag */
225 child_node
->flags
&= ~ANOBJ_END_OF_PEER_LIST
;
226 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
227 node
->peer
= parent_node
;
230 /* Init the new entry */
232 node
->owner_id
= owner_id
;
233 node
->type
= (u8
) type
;
235 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES
,
236 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
237 acpi_ut_get_node_name (node
), acpi_ut_get_type_name (node
->type
), node
, owner_id
,
238 acpi_ut_get_node_name (parent_node
), acpi_ut_get_type_name (parent_node
->type
),
242 * Increment the reference count(s) of all parents up to
245 while ((node
= acpi_ns_get_parent_node (node
)) != NULL
) {
246 node
->reference_count
++;
253 /*******************************************************************************
255 * FUNCTION: acpi_ns_delete_children
257 * PARAMETERS: parent_node - Delete this objects children
261 * DESCRIPTION: Delete all children of the parent object. In other words,
264 ******************************************************************************/
267 acpi_ns_delete_children (
268 struct acpi_namespace_node
*parent_node
)
270 struct acpi_namespace_node
*child_node
;
271 struct acpi_namespace_node
*next_node
;
272 struct acpi_namespace_node
*node
;
276 ACPI_FUNCTION_TRACE_PTR ("ns_delete_children", parent_node
);
283 /* If no children, all done! */
285 child_node
= parent_node
->child
;
291 * Deallocate all children at this level
294 /* Get the things we need */
296 next_node
= child_node
->peer
;
297 flags
= child_node
->flags
;
299 /* Grandchildren should have all been deleted already */
301 if (child_node
->child
) {
302 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Found a grandchild! P=%p C=%p\n",
303 parent_node
, child_node
));
306 /* Now we can free this child object */
308 ACPI_MEM_TRACKING (acpi_gbl_ns_node_list
->total_freed
++);
310 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Object %p, Remaining %X\n",
311 child_node
, acpi_gbl_current_node_count
));
314 * Detach an object if there is one, then free the child node
316 acpi_ns_detach_object (child_node
);
319 * Decrement the reference count(s) of all parents up to
320 * the root! (counts were incremented when the node was created)
323 while ((node
= acpi_ns_get_parent_node (node
)) != NULL
) {
324 node
->reference_count
--;
327 /* There should be only one reference remaining on this node */
329 if (child_node
->reference_count
!= 1) {
330 ACPI_REPORT_WARNING ((
331 "Existing references (%d) on node being deleted (%p)\n",
332 child_node
->reference_count
, child_node
));
335 /* Now we can delete the node */
337 ACPI_MEM_FREE (child_node
);
339 /* And move on to the next child in the list */
341 child_node
= next_node
;
343 } while (!(flags
& ANOBJ_END_OF_PEER_LIST
));
346 /* Clear the parent's child pointer */
348 parent_node
->child
= NULL
;
354 /*******************************************************************************
356 * FUNCTION: acpi_ns_delete_namespace_subtree
358 * PARAMETERS: parent_node - Root of the subtree to be deleted
362 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
363 * stored within the subtree.
365 ******************************************************************************/
368 acpi_ns_delete_namespace_subtree (
369 struct acpi_namespace_node
*parent_node
)
371 struct acpi_namespace_node
*child_node
= NULL
;
375 ACPI_FUNCTION_TRACE ("ns_delete_namespace_subtree");
383 * Traverse the tree of objects until we bubble back up
384 * to where we started.
387 /* Get the next node in this scope (NULL if none) */
389 child_node
= acpi_ns_get_next_node (ACPI_TYPE_ANY
, parent_node
,
392 /* Found a child node - detach any attached object */
394 acpi_ns_detach_object (child_node
);
396 /* Check if this node has any children */
398 if (acpi_ns_get_next_node (ACPI_TYPE_ANY
, child_node
, NULL
)) {
400 * There is at least one child of this node,
404 parent_node
= child_node
;
410 * No more children of this parent node.
411 * Move up to the grandparent.
416 * Now delete all of the children of this parent
417 * all at the same time.
419 acpi_ns_delete_children (parent_node
);
421 /* New "last child" is this parent node */
423 child_node
= parent_node
;
425 /* Move up the tree to the grandparent */
427 parent_node
= acpi_ns_get_parent_node (parent_node
);
435 /*******************************************************************************
437 * FUNCTION: acpi_ns_remove_reference
439 * PARAMETERS: Node - Named node whose reference count is to be
444 * DESCRIPTION: Remove a Node reference. Decrements the reference count
445 * of all parent Nodes up to the root. Any node along
446 * the way that reaches zero references is freed.
448 ******************************************************************************/
451 acpi_ns_remove_reference (
452 struct acpi_namespace_node
*node
)
454 struct acpi_namespace_node
*parent_node
;
455 struct acpi_namespace_node
*this_node
;
458 ACPI_FUNCTION_ENTRY ();
462 * Decrement the reference count(s) of this node and all
463 * nodes up to the root, Delete anything with zero remaining references.
467 /* Prepare to move up to parent */
469 parent_node
= acpi_ns_get_parent_node (this_node
);
471 /* Decrement the reference count on this node */
473 this_node
->reference_count
--;
475 /* Delete the node if no more references */
477 if (!this_node
->reference_count
) {
478 /* Delete all children and delete the node */
480 acpi_ns_delete_children (this_node
);
481 acpi_ns_delete_node (this_node
);
484 this_node
= parent_node
;
489 /*******************************************************************************
491 * FUNCTION: acpi_ns_delete_namespace_by_owner
493 * PARAMETERS: owner_id - All nodes with this owner will be deleted
497 * DESCRIPTION: Delete entries within the namespace that are owned by a
498 * specific ID. Used to delete entire ACPI tables. All
499 * reference counts are updated.
501 ******************************************************************************/
504 acpi_ns_delete_namespace_by_owner (
505 acpi_owner_id owner_id
)
507 struct acpi_namespace_node
*child_node
;
508 struct acpi_namespace_node
*deletion_node
;
510 struct acpi_namespace_node
*parent_node
;
513 ACPI_FUNCTION_TRACE_U32 ("ns_delete_namespace_by_owner", owner_id
);
520 parent_node
= acpi_gbl_root_node
;
522 deletion_node
= NULL
;
526 * Traverse the tree of nodes until we bubble back up
527 * to where we started.
531 * Get the next child of this parent node. When child_node is NULL,
532 * the first child of the parent is returned
534 child_node
= acpi_ns_get_next_node (ACPI_TYPE_ANY
, parent_node
, child_node
);
537 acpi_ns_remove_reference (deletion_node
);
538 deletion_node
= NULL
;
542 if (child_node
->owner_id
== owner_id
) {
543 /* Found a matching child node - detach any attached object */
545 acpi_ns_detach_object (child_node
);
548 /* Check if this node has any children */
550 if (acpi_ns_get_next_node (ACPI_TYPE_ANY
, child_node
, NULL
)) {
552 * There is at least one child of this node,
556 parent_node
= child_node
;
559 else if (child_node
->owner_id
== owner_id
) {
560 deletion_node
= child_node
;
565 * No more children of this parent node.
566 * Move up to the grandparent.
570 if (parent_node
->owner_id
== owner_id
) {
571 deletion_node
= parent_node
;
575 /* New "last child" is this parent node */
577 child_node
= parent_node
;
579 /* Move up the tree to the grandparent */
581 parent_node
= acpi_ns_get_parent_node (parent_node
);