ACPICA 20050708 from Bob Moore <robert.moore@intel.com>
[linux-2.6/verdex.git] / drivers / acpi / namespace / nsalloc.c
blobedbf1db36b6876099f26d71a0b3e25c5abb58865
1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
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 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nsalloc")
52 /* Local prototypes */
54 static void
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 *
72 acpi_ns_create_node (
73 u32 name)
75 struct acpi_namespace_node *node;
78 ACPI_FUNCTION_TRACE ("ns_create_node");
81 node = ACPI_MEM_CALLOCATE (sizeof (struct acpi_namespace_node));
82 if (!node) {
83 return_PTR (NULL);
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);
92 return_PTR (node);
96 /*******************************************************************************
98 * FUNCTION: acpi_ns_delete_node
100 * PARAMETERS: Node - Node to be deleted
102 * RETURN: None
104 * DESCRIPTION: Delete a namespace node
106 ******************************************************************************/
108 void
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);
122 prev_node = NULL;
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;
132 if (prev_node) {
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;
140 else {
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);
161 return_VOID;
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
174 * RETURN: None
176 * DESCRIPTION: Initialize a new namespace node and install it amongst
177 * its peers.
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
182 * namespace dump.
184 ******************************************************************************/
186 void
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;
198 #endif
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
209 if (walk_state) {
210 owner_id = walk_state->owner_id;
213 /* Link the new entry into the parent and existing children */
215 child_node = parent_node->child;
216 if (!child_node) {
217 parent_node->child = node;
218 node->flags |= ANOBJ_END_OF_PEER_LIST;
219 node->peer = parent_node;
221 else {
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;
243 break;
246 /* Get next peer */
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;
265 else {
266 /* Insert node alphabetically at start of list */
268 node->peer = child_node;
269 parent_node->child = node;
272 #else
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;
284 #endif
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),
296 parent_node));
299 * Increment the reference count(s) of all parents up to
300 * the root!
302 while ((node = acpi_ns_get_parent_node (node)) != NULL) {
303 node->reference_count++;
306 return_VOID;
310 /*******************************************************************************
312 * FUNCTION: acpi_ns_delete_children
314 * PARAMETERS: parent_node - Delete this objects children
316 * RETURN: None.
318 * DESCRIPTION: Delete all children of the parent object. In other words,
319 * deletes a "scope".
321 ******************************************************************************/
323 void
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;
330 u8 flags;
333 ACPI_FUNCTION_TRACE_PTR ("ns_delete_children", parent_node);
336 if (!parent_node) {
337 return_VOID;
340 /* If no children, all done! */
342 child_node = parent_node->child;
343 if (!child_node) {
344 return_VOID;
348 * Deallocate all children at this level
350 do {
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)
379 node = child_node;
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;
407 return_VOID;
411 /*******************************************************************************
413 * FUNCTION: acpi_ns_delete_namespace_subtree
415 * PARAMETERS: parent_node - Root of the subtree to be deleted
417 * RETURN: None.
419 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
420 * stored within the subtree.
422 ******************************************************************************/
424 void
425 acpi_ns_delete_namespace_subtree (
426 struct acpi_namespace_node *parent_node)
428 struct acpi_namespace_node *child_node = NULL;
429 u32 level = 1;
432 ACPI_FUNCTION_TRACE ("ns_delete_namespace_subtree");
435 if (!parent_node) {
436 return_VOID;
440 * Traverse the tree of objects until we bubble back up
441 * to where we started.
443 while (level > 0) {
444 /* Get the next node in this scope (NULL if none) */
446 child_node = acpi_ns_get_next_node (ACPI_TYPE_ANY, parent_node,
447 child_node);
448 if (child_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,
458 * visit the node
460 level++;
461 parent_node = child_node;
462 child_node = NULL;
465 else {
467 * No more children of this parent node.
468 * Move up to the grandparent.
470 level--;
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);
488 return_VOID;
492 /*******************************************************************************
494 * FUNCTION: acpi_ns_remove_reference
496 * PARAMETERS: Node - Named node whose reference count is to be
497 * decremented
499 * RETURN: None.
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 ******************************************************************************/
507 static void
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.
522 this_node = node;
523 while (this_node) {
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
552 * RETURN: Status
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 ******************************************************************************/
560 void
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;
566 u32 level;
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;
574 child_node = NULL;
575 deletion_node = NULL;
576 level = 1;
579 * Traverse the tree of nodes until we bubble back up
580 * to where we started.
582 while (level > 0) {
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);
589 if (deletion_node) {
590 acpi_ns_remove_reference (deletion_node);
591 deletion_node = NULL;
594 if (child_node) {
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,
606 * visit the node
608 level++;
609 parent_node = child_node;
610 child_node = NULL;
612 else if (child_node->owner_id == owner_id) {
613 deletion_node = child_node;
616 else {
618 * No more children of this parent node.
619 * Move up to the grandparent.
621 level--;
622 if (level != 0) {
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);
639 return_VOID;
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 (
660 char *name1,
661 char *name2)
663 char reversed_name1[ACPI_NAME_SIZE];
664 char reversed_name2[ACPI_NAME_SIZE];
665 u32 i;
666 u32 j;
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
672 * first.
674 * Reverse the name bytewise so we can just do a 32-bit compare instead
675 * of a strncmp.
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);
691 #endif