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. However, the
180 * nodes are linked in alphabetical order to 1) put all reserved
181 * names (start with underscore) first, and to 2) make a readable
184 ******************************************************************************/
187 acpi_ns_install_node (
188 struct acpi_walk_state
*walk_state
,
189 struct acpi_namespace_node
*parent_node
, /* Parent */
190 struct acpi_namespace_node
*node
, /* New Child*/
191 acpi_object_type type
)
193 acpi_owner_id owner_id
= 0;
194 struct acpi_namespace_node
*child_node
;
195 #ifdef ACPI_ALPHABETIC_NAMESPACE
197 struct acpi_namespace_node
*previous_child_node
;
201 ACPI_FUNCTION_TRACE ("ns_install_node");
205 * Get the owner ID from the Walk state
206 * The owner ID is used to track table deletion and
207 * deletion of objects created by methods
210 owner_id
= walk_state
->owner_id
;
213 /* Link the new entry into the parent and existing children */
215 child_node
= parent_node
->child
;
217 parent_node
->child
= node
;
218 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
219 node
->peer
= parent_node
;
222 #ifdef ACPI_ALPHABETIC_NAMESPACE
224 * Walk the list whilst searching for the correct
225 * alphabetic placement.
227 previous_child_node
= NULL
;
228 while (acpi_ns_compare_names (acpi_ut_get_node_name (child_node
),
229 acpi_ut_get_node_name (node
)) < 0) {
230 if (child_node
->flags
& ANOBJ_END_OF_PEER_LIST
) {
231 /* Last peer; Clear end-of-list flag */
233 child_node
->flags
&= ~ANOBJ_END_OF_PEER_LIST
;
235 /* This node is the new peer to the child node */
237 child_node
->peer
= node
;
239 /* This node is the new end-of-list */
241 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
242 node
->peer
= parent_node
;
248 previous_child_node
= child_node
;
249 child_node
= child_node
->peer
;
252 /* Did the node get inserted at the end-of-list? */
254 if (!(node
->flags
& ANOBJ_END_OF_PEER_LIST
)) {
256 * Loop above terminated without reaching the end-of-list.
257 * Insert the new node at the current location
259 if (previous_child_node
) {
260 /* Insert node alphabetically */
262 node
->peer
= child_node
;
263 previous_child_node
->peer
= node
;
266 /* Insert node alphabetically at start of list */
268 node
->peer
= child_node
;
269 parent_node
->child
= node
;
273 while (!(child_node
->flags
& ANOBJ_END_OF_PEER_LIST
)) {
274 child_node
= child_node
->peer
;
277 child_node
->peer
= node
;
279 /* Clear end-of-list flag */
281 child_node
->flags
&= ~ANOBJ_END_OF_PEER_LIST
;
282 node
->flags
|= ANOBJ_END_OF_PEER_LIST
;
283 node
->peer
= parent_node
;
287 /* Init the new entry */
289 node
->owner_id
= owner_id
;
290 node
->type
= (u8
) type
;
292 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES
,
293 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
294 acpi_ut_get_node_name (node
), acpi_ut_get_type_name (node
->type
), node
, owner_id
,
295 acpi_ut_get_node_name (parent_node
), acpi_ut_get_type_name (parent_node
->type
),
299 * Increment the reference count(s) of all parents up to
302 while ((node
= acpi_ns_get_parent_node (node
)) != NULL
) {
303 node
->reference_count
++;
310 /*******************************************************************************
312 * FUNCTION: acpi_ns_delete_children
314 * PARAMETERS: parent_node - Delete this objects children
318 * DESCRIPTION: Delete all children of the parent object. In other words,
321 ******************************************************************************/
324 acpi_ns_delete_children (
325 struct acpi_namespace_node
*parent_node
)
327 struct acpi_namespace_node
*child_node
;
328 struct acpi_namespace_node
*next_node
;
329 struct acpi_namespace_node
*node
;
333 ACPI_FUNCTION_TRACE_PTR ("ns_delete_children", parent_node
);
340 /* If no children, all done! */
342 child_node
= parent_node
->child
;
348 * Deallocate all children at this level
351 /* Get the things we need */
353 next_node
= child_node
->peer
;
354 flags
= child_node
->flags
;
356 /* Grandchildren should have all been deleted already */
358 if (child_node
->child
) {
359 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Found a grandchild! P=%p C=%p\n",
360 parent_node
, child_node
));
363 /* Now we can free this child object */
365 ACPI_MEM_TRACKING (acpi_gbl_ns_node_list
->total_freed
++);
367 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Object %p, Remaining %X\n",
368 child_node
, acpi_gbl_current_node_count
));
371 * Detach an object if there is one, then free the child node
373 acpi_ns_detach_object (child_node
);
376 * Decrement the reference count(s) of all parents up to
377 * the root! (counts were incremented when the node was created)
380 while ((node
= acpi_ns_get_parent_node (node
)) != NULL
) {
381 node
->reference_count
--;
384 /* There should be only one reference remaining on this node */
386 if (child_node
->reference_count
!= 1) {
387 ACPI_REPORT_WARNING ((
388 "Existing references (%d) on node being deleted (%p)\n",
389 child_node
->reference_count
, child_node
));
392 /* Now we can delete the node */
394 ACPI_MEM_FREE (child_node
);
396 /* And move on to the next child in the list */
398 child_node
= next_node
;
400 } while (!(flags
& ANOBJ_END_OF_PEER_LIST
));
403 /* Clear the parent's child pointer */
405 parent_node
->child
= NULL
;
411 /*******************************************************************************
413 * FUNCTION: acpi_ns_delete_namespace_subtree
415 * PARAMETERS: parent_node - Root of the subtree to be deleted
419 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
420 * stored within the subtree.
422 ******************************************************************************/
425 acpi_ns_delete_namespace_subtree (
426 struct acpi_namespace_node
*parent_node
)
428 struct acpi_namespace_node
*child_node
= NULL
;
432 ACPI_FUNCTION_TRACE ("ns_delete_namespace_subtree");
440 * Traverse the tree of objects until we bubble back up
441 * to where we started.
444 /* Get the next node in this scope (NULL if none) */
446 child_node
= acpi_ns_get_next_node (ACPI_TYPE_ANY
, parent_node
,
449 /* Found a child node - detach any attached object */
451 acpi_ns_detach_object (child_node
);
453 /* Check if this node has any children */
455 if (acpi_ns_get_next_node (ACPI_TYPE_ANY
, child_node
, NULL
)) {
457 * There is at least one child of this node,
461 parent_node
= child_node
;
467 * No more children of this parent node.
468 * Move up to the grandparent.
473 * Now delete all of the children of this parent
474 * all at the same time.
476 acpi_ns_delete_children (parent_node
);
478 /* New "last child" is this parent node */
480 child_node
= parent_node
;
482 /* Move up the tree to the grandparent */
484 parent_node
= acpi_ns_get_parent_node (parent_node
);
492 /*******************************************************************************
494 * FUNCTION: acpi_ns_remove_reference
496 * PARAMETERS: Node - Named node whose reference count is to be
501 * DESCRIPTION: Remove a Node reference. Decrements the reference count
502 * of all parent Nodes up to the root. Any node along
503 * the way that reaches zero references is freed.
505 ******************************************************************************/
508 acpi_ns_remove_reference (
509 struct acpi_namespace_node
*node
)
511 struct acpi_namespace_node
*parent_node
;
512 struct acpi_namespace_node
*this_node
;
515 ACPI_FUNCTION_ENTRY ();
519 * Decrement the reference count(s) of this node and all
520 * nodes up to the root, Delete anything with zero remaining references.
524 /* Prepare to move up to parent */
526 parent_node
= acpi_ns_get_parent_node (this_node
);
528 /* Decrement the reference count on this node */
530 this_node
->reference_count
--;
532 /* Delete the node if no more references */
534 if (!this_node
->reference_count
) {
535 /* Delete all children and delete the node */
537 acpi_ns_delete_children (this_node
);
538 acpi_ns_delete_node (this_node
);
541 this_node
= parent_node
;
546 /*******************************************************************************
548 * FUNCTION: acpi_ns_delete_namespace_by_owner
550 * PARAMETERS: owner_id - All nodes with this owner will be deleted
554 * DESCRIPTION: Delete entries within the namespace that are owned by a
555 * specific ID. Used to delete entire ACPI tables. All
556 * reference counts are updated.
558 ******************************************************************************/
561 acpi_ns_delete_namespace_by_owner (
562 acpi_owner_id owner_id
)
564 struct acpi_namespace_node
*child_node
;
565 struct acpi_namespace_node
*deletion_node
;
567 struct acpi_namespace_node
*parent_node
;
570 ACPI_FUNCTION_TRACE_U32 ("ns_delete_namespace_by_owner", owner_id
);
573 parent_node
= acpi_gbl_root_node
;
575 deletion_node
= NULL
;
579 * Traverse the tree of nodes until we bubble back up
580 * to where we started.
584 * Get the next child of this parent node. When child_node is NULL,
585 * the first child of the parent is returned
587 child_node
= acpi_ns_get_next_node (ACPI_TYPE_ANY
, parent_node
, child_node
);
590 acpi_ns_remove_reference (deletion_node
);
591 deletion_node
= NULL
;
595 if (child_node
->owner_id
== owner_id
) {
596 /* Found a matching child node - detach any attached object */
598 acpi_ns_detach_object (child_node
);
601 /* Check if this node has any children */
603 if (acpi_ns_get_next_node (ACPI_TYPE_ANY
, child_node
, NULL
)) {
605 * There is at least one child of this node,
609 parent_node
= child_node
;
612 else if (child_node
->owner_id
== owner_id
) {
613 deletion_node
= child_node
;
618 * No more children of this parent node.
619 * Move up to the grandparent.
623 if (parent_node
->owner_id
== owner_id
) {
624 deletion_node
= parent_node
;
628 /* New "last child" is this parent node */
630 child_node
= parent_node
;
632 /* Move up the tree to the grandparent */
634 parent_node
= acpi_ns_get_parent_node (parent_node
);
638 (void) acpi_ut_release_owner_id (owner_id
);
643 #ifdef ACPI_ALPHABETIC_NAMESPACE
644 /*******************************************************************************
646 * FUNCTION: acpi_ns_compare_names
648 * PARAMETERS: Name1 - First name to compare
649 * Name2 - Second name to compare
651 * RETURN: value from strncmp
653 * DESCRIPTION: Compare two ACPI names. Names that are prefixed with an
654 * underscore are forced to be alphabetically first.
656 ******************************************************************************/
659 acpi_ns_compare_names (
663 char reversed_name1
[ACPI_NAME_SIZE
];
664 char reversed_name2
[ACPI_NAME_SIZE
];
670 * Replace all instances of "underscore" with a value that is smaller so
671 * that all names that are prefixed with underscore(s) are alphabetically
674 * Reverse the name bytewise so we can just do a 32-bit compare instead
677 for (i
= 0, j
= (ACPI_NAME_SIZE
- 1); i
< ACPI_NAME_SIZE
; i
++, j
--) {
678 reversed_name1
[j
] = name1
[i
];
679 if (name1
[i
] == '_') {
680 reversed_name1
[j
] = '*';
683 reversed_name2
[j
] = name2
[i
];
684 if (name2
[i
] == '_') {
685 reversed_name2
[j
] = '*';
689 return (*(int *) reversed_name1
- *(int *) reversed_name2
);