close_port, kpacket_gen kmalloc oom, formard.c wake_sender/skb receive oom handling...
[cor_2_6_31.git] / drivers / acpi / acpica / nsalloc.c
blobefc971ab7d6512dbc47607bd25d6d976f28ddff6
1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2008, 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.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
48 #define _COMPONENT ACPI_NAMESPACE
49 ACPI_MODULE_NAME("nsalloc")
51 /*******************************************************************************
53 * FUNCTION: acpi_ns_create_node
55 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
57 * RETURN: New namespace node (Null on failure)
59 * DESCRIPTION: Create a namespace node
61 ******************************************************************************/
62 struct acpi_namespace_node *acpi_ns_create_node(u32 name)
64 struct acpi_namespace_node *node;
65 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
66 u32 temp;
67 #endif
69 ACPI_FUNCTION_TRACE(ns_create_node);
71 node = acpi_os_acquire_object(acpi_gbl_namespace_cache);
72 if (!node) {
73 return_PTR(NULL);
76 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
78 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
79 temp = acpi_gbl_ns_node_list->total_allocated -
80 acpi_gbl_ns_node_list->total_freed;
81 if (temp > acpi_gbl_ns_node_list->max_occupied) {
82 acpi_gbl_ns_node_list->max_occupied = temp;
84 #endif
86 node->name.integer = name;
87 ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
88 return_PTR(node);
91 /*******************************************************************************
93 * FUNCTION: acpi_ns_delete_node
95 * PARAMETERS: Node - Node to be deleted
97 * RETURN: None
99 * DESCRIPTION: Delete a namespace node
101 ******************************************************************************/
103 void acpi_ns_delete_node(struct acpi_namespace_node *node)
105 struct acpi_namespace_node *parent_node;
106 struct acpi_namespace_node *prev_node;
107 struct acpi_namespace_node *next_node;
109 ACPI_FUNCTION_TRACE_PTR(ns_delete_node, node);
111 parent_node = acpi_ns_get_parent_node(node);
113 prev_node = NULL;
114 next_node = parent_node->child;
116 /* Find the node that is the previous peer in the parent's child list */
118 while (next_node != node) {
119 prev_node = next_node;
120 next_node = prev_node->peer;
123 if (prev_node) {
125 /* Node is not first child, unlink it */
127 prev_node->peer = next_node->peer;
128 if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
129 prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
131 } else {
132 /* Node is first child (has no previous peer) */
134 if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
136 /* No peers at all */
138 parent_node->child = NULL;
139 } else { /* Link peer list to parent */
141 parent_node->child = next_node->peer;
145 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
147 /* Detach an object if there is one, then delete the node */
149 acpi_ns_detach_object(node);
150 (void)acpi_os_release_object(acpi_gbl_namespace_cache, node);
151 return_VOID;
154 /*******************************************************************************
156 * FUNCTION: acpi_ns_install_node
158 * PARAMETERS: walk_state - Current state of the walk
159 * parent_node - The parent of the new Node
160 * Node - The new Node to install
161 * Type - ACPI object type of the new Node
163 * RETURN: None
165 * DESCRIPTION: Initialize a new namespace node and install it amongst
166 * its peers.
168 * Note: Current namespace lookup is linear search. This appears
169 * to be sufficient as namespace searches consume only a small
170 * fraction of the execution time of the ACPI subsystem.
172 ******************************************************************************/
174 void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
175 struct acpi_namespace_node *node, /* New Child */
176 acpi_object_type type)
178 acpi_owner_id owner_id = 0;
179 struct acpi_namespace_node *child_node;
181 ACPI_FUNCTION_TRACE(ns_install_node);
184 * Get the owner ID from the Walk state. The owner ID is used to track
185 * table deletion and deletion of objects created by methods.
187 if (walk_state) {
188 owner_id = walk_state->owner_id;
191 /* Link the new entry into the parent and existing children */
193 child_node = parent_node->child;
194 if (!child_node) {
195 parent_node->child = node;
196 node->flags |= ANOBJ_END_OF_PEER_LIST;
197 node->peer = parent_node;
198 } else {
199 while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
200 child_node = child_node->peer;
203 child_node->peer = node;
205 /* Clear end-of-list flag */
207 child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
208 node->flags |= ANOBJ_END_OF_PEER_LIST;
209 node->peer = parent_node;
212 /* Init the new entry */
214 node->owner_id = owner_id;
215 node->type = (u8) type;
217 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
218 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
219 acpi_ut_get_node_name(node),
220 acpi_ut_get_type_name(node->type), node, owner_id,
221 acpi_ut_get_node_name(parent_node),
222 acpi_ut_get_type_name(parent_node->type),
223 parent_node));
225 return_VOID;
228 /*******************************************************************************
230 * FUNCTION: acpi_ns_delete_children
232 * PARAMETERS: parent_node - Delete this objects children
234 * RETURN: None.
236 * DESCRIPTION: Delete all children of the parent object. In other words,
237 * deletes a "scope".
239 ******************************************************************************/
241 void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
243 struct acpi_namespace_node *child_node;
244 struct acpi_namespace_node *next_node;
245 u8 flags;
247 ACPI_FUNCTION_TRACE_PTR(ns_delete_children, parent_node);
249 if (!parent_node) {
250 return_VOID;
253 /* If no children, all done! */
255 child_node = parent_node->child;
256 if (!child_node) {
257 return_VOID;
260 /* Deallocate all children at this level */
262 do {
264 /* Get the things we need */
266 next_node = child_node->peer;
267 flags = child_node->flags;
269 /* Grandchildren should have all been deleted already */
271 if (child_node->child) {
272 ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p",
273 parent_node, child_node));
276 /* Now we can free this child object */
278 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
280 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
281 "Object %p, Remaining %X\n", child_node,
282 acpi_gbl_current_node_count));
284 /* Detach an object if there is one, then free the child node */
286 acpi_ns_detach_object(child_node);
288 /* Now we can delete the node */
290 (void)acpi_os_release_object(acpi_gbl_namespace_cache,
291 child_node);
293 /* And move on to the next child in the list */
295 child_node = next_node;
297 } while (!(flags & ANOBJ_END_OF_PEER_LIST));
299 /* Clear the parent's child pointer */
301 parent_node->child = NULL;
302 return_VOID;
305 /*******************************************************************************
307 * FUNCTION: acpi_ns_delete_namespace_subtree
309 * PARAMETERS: parent_node - Root of the subtree to be deleted
311 * RETURN: None.
313 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
314 * stored within the subtree.
316 ******************************************************************************/
318 void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
320 struct acpi_namespace_node *child_node = NULL;
321 u32 level = 1;
323 ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree);
325 if (!parent_node) {
326 return_VOID;
330 * Traverse the tree of objects until we bubble back up
331 * to where we started.
333 while (level > 0) {
335 /* Get the next node in this scope (NULL if none) */
337 child_node = acpi_ns_get_next_node(parent_node, child_node);
338 if (child_node) {
340 /* Found a child node - detach any attached object */
342 acpi_ns_detach_object(child_node);
344 /* Check if this node has any children */
346 if (child_node->child) {
348 * There is at least one child of this node,
349 * visit the node
351 level++;
352 parent_node = child_node;
353 child_node = NULL;
355 } else {
357 * No more children of this parent node.
358 * Move up to the grandparent.
360 level--;
363 * Now delete all of the children of this parent
364 * all at the same time.
366 acpi_ns_delete_children(parent_node);
368 /* New "last child" is this parent node */
370 child_node = parent_node;
372 /* Move up the tree to the grandparent */
374 parent_node = acpi_ns_get_parent_node(parent_node);
378 return_VOID;
381 /*******************************************************************************
383 * FUNCTION: acpi_ns_delete_namespace_by_owner
385 * PARAMETERS: owner_id - All nodes with this owner will be deleted
387 * RETURN: Status
389 * DESCRIPTION: Delete entries within the namespace that are owned by a
390 * specific ID. Used to delete entire ACPI tables. All
391 * reference counts are updated.
393 * MUTEX: Locks namespace during deletion walk.
395 ******************************************************************************/
397 void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
399 struct acpi_namespace_node *child_node;
400 struct acpi_namespace_node *deletion_node;
401 struct acpi_namespace_node *parent_node;
402 u32 level;
403 acpi_status status;
405 ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id);
407 if (owner_id == 0) {
408 return_VOID;
411 /* Lock namespace for possible update */
413 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
414 if (ACPI_FAILURE(status)) {
415 return_VOID;
418 deletion_node = NULL;
419 parent_node = acpi_gbl_root_node;
420 child_node = NULL;
421 level = 1;
424 * Traverse the tree of nodes until we bubble back up
425 * to where we started.
427 while (level > 0) {
429 * Get the next child of this parent node. When child_node is NULL,
430 * the first child of the parent is returned
432 child_node = acpi_ns_get_next_node(parent_node, child_node);
434 if (deletion_node) {
435 acpi_ns_delete_children(deletion_node);
436 acpi_ns_delete_node(deletion_node);
437 deletion_node = NULL;
440 if (child_node) {
441 if (child_node->owner_id == owner_id) {
443 /* Found a matching child node - detach any attached object */
445 acpi_ns_detach_object(child_node);
448 /* Check if this node has any children */
450 if (child_node->child) {
452 * There is at least one child of this node,
453 * visit the node
455 level++;
456 parent_node = child_node;
457 child_node = NULL;
458 } else if (child_node->owner_id == owner_id) {
459 deletion_node = child_node;
461 } else {
463 * No more children of this parent node.
464 * Move up to the grandparent.
466 level--;
467 if (level != 0) {
468 if (parent_node->owner_id == owner_id) {
469 deletion_node = parent_node;
473 /* New "last child" is this parent node */
475 child_node = parent_node;
477 /* Move up the tree to the grandparent */
479 parent_node = acpi_ns_get_parent_node(parent_node);
483 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
484 return_VOID;