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")
53 /*******************************************************************************
55 * FUNCTION: acpi_ns_create_node
57 * PARAMETERS: acpi_name - Name of the new node
61 * DESCRIPTION: Create a namespace node
63 ******************************************************************************/
65 struct acpi_namespace_node
*
69 struct acpi_namespace_node
*node
;
72 ACPI_FUNCTION_TRACE ("ns_create_node");
75 node
= ACPI_MEM_CALLOCATE (sizeof (struct acpi_namespace_node
));
80 ACPI_MEM_TRACKING (acpi_gbl_memory_lists
[ACPI_MEM_LIST_NSNODE
].total_allocated
++);
82 node
->name
.integer
= name
;
83 node
->reference_count
= 1;
84 ACPI_SET_DESCRIPTOR_TYPE (node
, ACPI_DESC_TYPE_NAMED
);
90 /*******************************************************************************
92 * FUNCTION: acpi_ns_delete_node
94 * PARAMETERS: Node - Node to be deleted
98 * DESCRIPTION: Delete a namespace node
100 ******************************************************************************/
103 acpi_ns_delete_node (
104 struct acpi_namespace_node
*node
)
106 struct acpi_namespace_node
*parent_node
;
107 struct acpi_namespace_node
*prev_node
;
108 struct acpi_namespace_node
*next_node
;
111 ACPI_FUNCTION_TRACE_PTR ("ns_delete_node", node
);
114 parent_node
= acpi_ns_get_parent_node (node
);
117 next_node
= parent_node
->child
;
119 /* Find the node that is the previous peer in the parent's child list */
121 while (next_node
!= node
) {
122 prev_node
= next_node
;
123 next_node
= prev_node
->peer
;
127 /* Node is not first child, unlink it */
129 prev_node
->peer
= next_node
->peer
;
130 if (next_node
->flags
& ANOBJ_END_OF_PEER_LIST
) {
131 prev_node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
135 /* Node is first child (has no previous peer) */
137 if (next_node
->flags
& ANOBJ_END_OF_PEER_LIST
) {
138 /* No peers at all */
140 parent_node
->child
= NULL
;
142 else { /* Link peer list to parent */
144 parent_node
->child
= next_node
->peer
;
149 ACPI_MEM_TRACKING (acpi_gbl_memory_lists
[ACPI_MEM_LIST_NSNODE
].total_freed
++);
152 * Detach an object if there is one then delete the node
154 acpi_ns_detach_object (node
);
155 ACPI_MEM_FREE (node
);
160 #ifdef ACPI_ALPHABETIC_NAMESPACE
161 /*******************************************************************************
163 * FUNCTION: acpi_ns_compare_names
165 * PARAMETERS: Name1 - First name to compare
166 * Name2 - Second name to compare
168 * RETURN: value from strncmp
170 * DESCRIPTION: Compare two ACPI names. Names that are prefixed with an
171 * underscore are forced to be alphabetically first.
173 ******************************************************************************/
176 acpi_ns_compare_names (
180 char reversed_name1
[ACPI_NAME_SIZE
];
181 char reversed_name2
[ACPI_NAME_SIZE
];
187 * Replace all instances of "underscore" with a value that is smaller so
188 * that all names that are prefixed with underscore(s) are alphabetically
191 * Reverse the name bytewise so we can just do a 32-bit compare instead
194 for (i
= 0, j
= (ACPI_NAME_SIZE
- 1); i
< ACPI_NAME_SIZE
; i
++, j
--) {
195 reversed_name1
[j
] = name1
[i
];
196 if (name1
[i
] == '_') {
197 reversed_name1
[j
] = '*';
200 reversed_name2
[j
] = name2
[i
];
201 if (name2
[i
] == '_') {
202 reversed_name2
[j
] = '*';
206 return (*(int *) reversed_name1
- *(int *) reversed_name2
);
211 /*******************************************************************************
213 * FUNCTION: acpi_ns_install_node
215 * PARAMETERS: walk_state - Current state of the walk
216 * parent_node - The parent of the new Node
217 * Node - The new Node to install
218 * Type - ACPI object type of the new Node
222 * DESCRIPTION: Initialize a new namespace node and install it amongst
225 * Note: Current namespace lookup is linear search. However, the
226 * nodes are linked in alphabetical order to 1) put all reserved
227 * names (start with underscore) first, and to 2) make a readable
230 ******************************************************************************/
233 acpi_ns_install_node (
234 struct acpi_walk_state
*walk_state
,
235 struct acpi_namespace_node
*parent_node
, /* Parent */
236 struct acpi_namespace_node
*node
, /* New Child*/
237 acpi_object_type type
)
240 struct acpi_namespace_node
*child_node
;
241 #ifdef ACPI_ALPHABETIC_NAMESPACE
243 struct acpi_namespace_node
*previous_child_node
;
247 ACPI_FUNCTION_TRACE ("ns_install_node");
251 * Get the owner ID from the Walk state
252 * The owner ID is used to track table deletion and
253 * deletion of objects created by methods
256 owner_id
= walk_state
->owner_id
;
259 /* Link the new entry into the parent and existing children */
261 child_node
= parent_node
->child
;
263 parent_node
->child
= node
;
264 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
265 node
->peer
= parent_node
;
268 #ifdef ACPI_ALPHABETIC_NAMESPACE
270 * Walk the list whilst searching for the correct
271 * alphabetic placement.
273 previous_child_node
= NULL
;
274 while (acpi_ns_compare_names (acpi_ut_get_node_name (child_node
), acpi_ut_get_node_name (node
)) < 0) {
275 if (child_node
->flags
& ANOBJ_END_OF_PEER_LIST
) {
276 /* Last peer; Clear end-of-list flag */
278 child_node
->flags
&= ~ANOBJ_END_OF_PEER_LIST
;
280 /* This node is the new peer to the child node */
282 child_node
->peer
= node
;
284 /* This node is the new end-of-list */
286 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
287 node
->peer
= parent_node
;
293 previous_child_node
= child_node
;
294 child_node
= child_node
->peer
;
297 /* Did the node get inserted at the end-of-list? */
299 if (!(node
->flags
& ANOBJ_END_OF_PEER_LIST
)) {
301 * Loop above terminated without reaching the end-of-list.
302 * Insert the new node at the current location
304 if (previous_child_node
) {
305 /* Insert node alphabetically */
307 node
->peer
= child_node
;
308 previous_child_node
->peer
= node
;
311 /* Insert node alphabetically at start of list */
313 node
->peer
= child_node
;
314 parent_node
->child
= node
;
318 while (!(child_node
->flags
& ANOBJ_END_OF_PEER_LIST
)) {
319 child_node
= child_node
->peer
;
322 child_node
->peer
= node
;
324 /* Clear end-of-list flag */
326 child_node
->flags
&= ~ANOBJ_END_OF_PEER_LIST
;
327 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
328 node
->peer
= parent_node
;
332 /* Init the new entry */
334 node
->owner_id
= owner_id
;
335 node
->type
= (u8
) type
;
337 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES
,
338 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
339 acpi_ut_get_node_name (node
), acpi_ut_get_type_name (node
->type
), node
, owner_id
,
340 acpi_ut_get_node_name (parent_node
), acpi_ut_get_type_name (parent_node
->type
),
344 * Increment the reference count(s) of all parents up to
347 while ((node
= acpi_ns_get_parent_node (node
)) != NULL
) {
348 node
->reference_count
++;
355 /*******************************************************************************
357 * FUNCTION: acpi_ns_delete_children
359 * PARAMETERS: parent_node - Delete this objects children
363 * DESCRIPTION: Delete all children of the parent object. In other words,
366 ******************************************************************************/
369 acpi_ns_delete_children (
370 struct acpi_namespace_node
*parent_node
)
372 struct acpi_namespace_node
*child_node
;
373 struct acpi_namespace_node
*next_node
;
374 struct acpi_namespace_node
*node
;
378 ACPI_FUNCTION_TRACE_PTR ("ns_delete_children", parent_node
);
385 /* If no children, all done! */
387 child_node
= parent_node
->child
;
393 * Deallocate all children at this level
396 /* Get the things we need */
398 next_node
= child_node
->peer
;
399 flags
= child_node
->flags
;
401 /* Grandchildren should have all been deleted already */
403 if (child_node
->child
) {
404 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Found a grandchild! P=%p C=%p\n",
405 parent_node
, child_node
));
408 /* Now we can free this child object */
410 ACPI_MEM_TRACKING (acpi_gbl_memory_lists
[ACPI_MEM_LIST_NSNODE
].total_freed
++);
412 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Object %p, Remaining %X\n",
413 child_node
, acpi_gbl_current_node_count
));
416 * Detach an object if there is one, then free the child node
418 acpi_ns_detach_object (child_node
);
421 * Decrement the reference count(s) of all parents up to
422 * the root! (counts were incremented when the node was created)
425 while ((node
= acpi_ns_get_parent_node (node
)) != NULL
) {
426 node
->reference_count
--;
429 /* There should be only one reference remaining on this node */
431 if (child_node
->reference_count
!= 1) {
432 ACPI_REPORT_WARNING (("Existing references (%d) on node being deleted (%p)\n",
433 child_node
->reference_count
, child_node
));
436 /* Now we can delete the node */
438 ACPI_MEM_FREE (child_node
);
440 /* And move on to the next child in the list */
442 child_node
= next_node
;
444 } while (!(flags
& ANOBJ_END_OF_PEER_LIST
));
447 /* Clear the parent's child pointer */
449 parent_node
->child
= NULL
;
455 /*******************************************************************************
457 * FUNCTION: acpi_ns_delete_namespace_subtree
459 * PARAMETERS: parent_node - Root of the subtree to be deleted
463 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
464 * stored within the subtree.
466 ******************************************************************************/
469 acpi_ns_delete_namespace_subtree (
470 struct acpi_namespace_node
*parent_node
)
472 struct acpi_namespace_node
*child_node
= NULL
;
476 ACPI_FUNCTION_TRACE ("ns_delete_namespace_subtree");
484 * Traverse the tree of objects until we bubble back up
485 * to where we started.
488 /* Get the next node in this scope (NULL if none) */
490 child_node
= acpi_ns_get_next_node (ACPI_TYPE_ANY
, parent_node
,
493 /* Found a child node - detach any attached object */
495 acpi_ns_detach_object (child_node
);
497 /* Check if this node has any children */
499 if (acpi_ns_get_next_node (ACPI_TYPE_ANY
, child_node
, NULL
)) {
501 * There is at least one child of this node,
505 parent_node
= child_node
;
511 * No more children of this parent node.
512 * Move up to the grandparent.
517 * Now delete all of the children of this parent
518 * all at the same time.
520 acpi_ns_delete_children (parent_node
);
522 /* New "last child" is this parent node */
524 child_node
= parent_node
;
526 /* Move up the tree to the grandparent */
528 parent_node
= acpi_ns_get_parent_node (parent_node
);
536 /*******************************************************************************
538 * FUNCTION: acpi_ns_remove_reference
540 * PARAMETERS: Node - Named node whose reference count is to be
545 * DESCRIPTION: Remove a Node reference. Decrements the reference count
546 * of all parent Nodes up to the root. Any node along
547 * the way that reaches zero references is freed.
549 ******************************************************************************/
552 acpi_ns_remove_reference (
553 struct acpi_namespace_node
*node
)
555 struct acpi_namespace_node
*parent_node
;
556 struct acpi_namespace_node
*this_node
;
559 ACPI_FUNCTION_ENTRY ();
563 * Decrement the reference count(s) of this node and all
564 * nodes up to the root, Delete anything with zero remaining references.
568 /* Prepare to move up to parent */
570 parent_node
= acpi_ns_get_parent_node (this_node
);
572 /* Decrement the reference count on this node */
574 this_node
->reference_count
--;
576 /* Delete the node if no more references */
578 if (!this_node
->reference_count
) {
579 /* Delete all children and delete the node */
581 acpi_ns_delete_children (this_node
);
582 acpi_ns_delete_node (this_node
);
585 this_node
= parent_node
;
590 /*******************************************************************************
592 * FUNCTION: acpi_ns_delete_namespace_by_owner
594 * PARAMETERS: owner_id - All nodes with this owner will be deleted
598 * DESCRIPTION: Delete entries within the namespace that are owned by a
599 * specific ID. Used to delete entire ACPI tables. All
600 * reference counts are updated.
602 ******************************************************************************/
605 acpi_ns_delete_namespace_by_owner (
608 struct acpi_namespace_node
*child_node
;
609 struct acpi_namespace_node
*deletion_node
;
611 struct acpi_namespace_node
*parent_node
;
614 ACPI_FUNCTION_TRACE_U32 ("ns_delete_namespace_by_owner", owner_id
);
617 parent_node
= acpi_gbl_root_node
;
619 deletion_node
= NULL
;
623 * Traverse the tree of nodes until we bubble back up
624 * to where we started.
628 * Get the next child of this parent node. When child_node is NULL,
629 * the first child of the parent is returned
631 child_node
= acpi_ns_get_next_node (ACPI_TYPE_ANY
, parent_node
, child_node
);
634 acpi_ns_remove_reference (deletion_node
);
635 deletion_node
= NULL
;
639 if (child_node
->owner_id
== owner_id
) {
640 /* Found a matching child node - detach any attached object */
642 acpi_ns_detach_object (child_node
);
645 /* Check if this node has any children */
647 if (acpi_ns_get_next_node (ACPI_TYPE_ANY
, child_node
, NULL
)) {
649 * There is at least one child of this node,
653 parent_node
= child_node
;
656 else if (child_node
->owner_id
== owner_id
) {
657 deletion_node
= child_node
;
662 * No more children of this parent node.
663 * Move up to the grandparent.
667 if (parent_node
->owner_id
== owner_id
) {
668 deletion_node
= parent_node
;
672 /* New "last child" is this parent node */
674 child_node
= parent_node
;
676 /* Move up the tree to the grandparent */
678 parent_node
= acpi_ns_get_parent_node (parent_node
);