nsalloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsalloc - Namespace allocation and deletion utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include <acpi/acnamesp.h>
  44. #define _COMPONENT ACPI_NAMESPACE
  45. ACPI_MODULE_NAME("nsalloc")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ns_create_node
  49. *
  50. * PARAMETERS: Name - Name of the new node (4 char ACPI name)
  51. *
  52. * RETURN: New namespace node (Null on failure)
  53. *
  54. * DESCRIPTION: Create a namespace node
  55. *
  56. ******************************************************************************/
  57. struct acpi_namespace_node *acpi_ns_create_node(u32 name)
  58. {
  59. struct acpi_namespace_node *node;
  60. ACPI_FUNCTION_TRACE(ns_create_node);
  61. node = acpi_os_acquire_object(acpi_gbl_namespace_cache);
  62. if (!node) {
  63. return_PTR(NULL);
  64. }
  65. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
  66. node->name.integer = name;
  67. ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
  68. return_PTR(node);
  69. }
  70. /*******************************************************************************
  71. *
  72. * FUNCTION: acpi_ns_delete_node
  73. *
  74. * PARAMETERS: Node - Node to be deleted
  75. *
  76. * RETURN: None
  77. *
  78. * DESCRIPTION: Delete a namespace node
  79. *
  80. ******************************************************************************/
  81. void acpi_ns_delete_node(struct acpi_namespace_node *node)
  82. {
  83. struct acpi_namespace_node *parent_node;
  84. struct acpi_namespace_node *prev_node;
  85. struct acpi_namespace_node *next_node;
  86. ACPI_FUNCTION_TRACE_PTR(ns_delete_node, node);
  87. parent_node = acpi_ns_get_parent_node(node);
  88. prev_node = NULL;
  89. next_node = parent_node->child;
  90. /* Find the node that is the previous peer in the parent's child list */
  91. while (next_node != node) {
  92. prev_node = next_node;
  93. next_node = prev_node->peer;
  94. }
  95. if (prev_node) {
  96. /* Node is not first child, unlink it */
  97. prev_node->peer = next_node->peer;
  98. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  99. prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
  100. }
  101. } else {
  102. /* Node is first child (has no previous peer) */
  103. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  104. /* No peers at all */
  105. parent_node->child = NULL;
  106. } else { /* Link peer list to parent */
  107. parent_node->child = next_node->peer;
  108. }
  109. }
  110. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  111. /*
  112. * Detach an object if there is one, then delete the node
  113. */
  114. acpi_ns_detach_object(node);
  115. (void)acpi_os_release_object(acpi_gbl_namespace_cache, node);
  116. return_VOID;
  117. }
  118. /*******************************************************************************
  119. *
  120. * FUNCTION: acpi_ns_install_node
  121. *
  122. * PARAMETERS: walk_state - Current state of the walk
  123. * parent_node - The parent of the new Node
  124. * Node - The new Node to install
  125. * Type - ACPI object type of the new Node
  126. *
  127. * RETURN: None
  128. *
  129. * DESCRIPTION: Initialize a new namespace node and install it amongst
  130. * its peers.
  131. *
  132. * Note: Current namespace lookup is linear search. This appears
  133. * to be sufficient as namespace searches consume only a small
  134. * fraction of the execution time of the ACPI subsystem.
  135. *
  136. ******************************************************************************/
  137. void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
  138. struct acpi_namespace_node *node, /* New Child */
  139. acpi_object_type type)
  140. {
  141. acpi_owner_id owner_id = 0;
  142. struct acpi_namespace_node *child_node;
  143. ACPI_FUNCTION_TRACE(ns_install_node);
  144. /*
  145. * Get the owner ID from the Walk state
  146. * The owner ID is used to track table deletion and
  147. * deletion of objects created by methods
  148. */
  149. if (walk_state) {
  150. owner_id = walk_state->owner_id;
  151. }
  152. /* Link the new entry into the parent and existing children */
  153. child_node = parent_node->child;
  154. if (!child_node) {
  155. parent_node->child = node;
  156. node->flags |= ANOBJ_END_OF_PEER_LIST;
  157. node->peer = parent_node;
  158. } else {
  159. while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
  160. child_node = child_node->peer;
  161. }
  162. child_node->peer = node;
  163. /* Clear end-of-list flag */
  164. child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
  165. node->flags |= ANOBJ_END_OF_PEER_LIST;
  166. node->peer = parent_node;
  167. }
  168. /* Init the new entry */
  169. node->owner_id = owner_id;
  170. node->type = (u8) type;
  171. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  172. "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
  173. acpi_ut_get_node_name(node),
  174. acpi_ut_get_type_name(node->type), node, owner_id,
  175. acpi_ut_get_node_name(parent_node),
  176. acpi_ut_get_type_name(parent_node->type),
  177. parent_node));
  178. return_VOID;
  179. }
  180. /*******************************************************************************
  181. *
  182. * FUNCTION: acpi_ns_delete_children
  183. *
  184. * PARAMETERS: parent_node - Delete this objects children
  185. *
  186. * RETURN: None.
  187. *
  188. * DESCRIPTION: Delete all children of the parent object. In other words,
  189. * deletes a "scope".
  190. *
  191. ******************************************************************************/
  192. void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
  193. {
  194. struct acpi_namespace_node *child_node;
  195. struct acpi_namespace_node *next_node;
  196. u8 flags;
  197. ACPI_FUNCTION_TRACE_PTR(ns_delete_children, parent_node);
  198. if (!parent_node) {
  199. return_VOID;
  200. }
  201. /* If no children, all done! */
  202. child_node = parent_node->child;
  203. if (!child_node) {
  204. return_VOID;
  205. }
  206. /*
  207. * Deallocate all children at this level
  208. */
  209. do {
  210. /* Get the things we need */
  211. next_node = child_node->peer;
  212. flags = child_node->flags;
  213. /* Grandchildren should have all been deleted already */
  214. if (child_node->child) {
  215. ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p",
  216. parent_node, child_node));
  217. }
  218. /* Now we can free this child object */
  219. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  220. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  221. "Object %p, Remaining %X\n", child_node,
  222. acpi_gbl_current_node_count));
  223. /*
  224. * Detach an object if there is one, then free the child node
  225. */
  226. acpi_ns_detach_object(child_node);
  227. /* Now we can delete the node */
  228. (void)acpi_os_release_object(acpi_gbl_namespace_cache,
  229. child_node);
  230. /* And move on to the next child in the list */
  231. child_node = next_node;
  232. } while (!(flags & ANOBJ_END_OF_PEER_LIST));
  233. /* Clear the parent's child pointer */
  234. parent_node->child = NULL;
  235. return_VOID;
  236. }
  237. /*******************************************************************************
  238. *
  239. * FUNCTION: acpi_ns_delete_namespace_subtree
  240. *
  241. * PARAMETERS: parent_node - Root of the subtree to be deleted
  242. *
  243. * RETURN: None.
  244. *
  245. * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
  246. * stored within the subtree.
  247. *
  248. ******************************************************************************/
  249. void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
  250. {
  251. struct acpi_namespace_node *child_node = NULL;
  252. u32 level = 1;
  253. ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree);
  254. if (!parent_node) {
  255. return_VOID;
  256. }
  257. /*
  258. * Traverse the tree of objects until we bubble back up
  259. * to where we started.
  260. */
  261. while (level > 0) {
  262. /* Get the next node in this scope (NULL if none) */
  263. child_node =
  264. acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
  265. child_node);
  266. if (child_node) {
  267. /* Found a child node - detach any attached object */
  268. acpi_ns_detach_object(child_node);
  269. /* Check if this node has any children */
  270. if (acpi_ns_get_next_node
  271. (ACPI_TYPE_ANY, child_node, NULL)) {
  272. /*
  273. * There is at least one child of this node,
  274. * visit the node
  275. */
  276. level++;
  277. parent_node = child_node;
  278. child_node = NULL;
  279. }
  280. } else {
  281. /*
  282. * No more children of this parent node.
  283. * Move up to the grandparent.
  284. */
  285. level--;
  286. /*
  287. * Now delete all of the children of this parent
  288. * all at the same time.
  289. */
  290. acpi_ns_delete_children(parent_node);
  291. /* New "last child" is this parent node */
  292. child_node = parent_node;
  293. /* Move up the tree to the grandparent */
  294. parent_node = acpi_ns_get_parent_node(parent_node);
  295. }
  296. }
  297. return_VOID;
  298. }
  299. /*******************************************************************************
  300. *
  301. * FUNCTION: acpi_ns_delete_namespace_by_owner
  302. *
  303. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  304. *
  305. * RETURN: Status
  306. *
  307. * DESCRIPTION: Delete entries within the namespace that are owned by a
  308. * specific ID. Used to delete entire ACPI tables. All
  309. * reference counts are updated.
  310. *
  311. * MUTEX: Locks namespace during deletion walk.
  312. *
  313. ******************************************************************************/
  314. void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
  315. {
  316. struct acpi_namespace_node *child_node;
  317. struct acpi_namespace_node *deletion_node;
  318. struct acpi_namespace_node *parent_node;
  319. u32 level;
  320. acpi_status status;
  321. ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id);
  322. if (owner_id == 0) {
  323. return_VOID;
  324. }
  325. /* Lock namespace for possible update */
  326. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  327. if (ACPI_FAILURE(status)) {
  328. return_VOID;
  329. }
  330. deletion_node = NULL;
  331. parent_node = acpi_gbl_root_node;
  332. child_node = NULL;
  333. level = 1;
  334. /*
  335. * Traverse the tree of nodes until we bubble back up
  336. * to where we started.
  337. */
  338. while (level > 0) {
  339. /*
  340. * Get the next child of this parent node. When child_node is NULL,
  341. * the first child of the parent is returned
  342. */
  343. child_node =
  344. acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
  345. child_node);
  346. if (deletion_node) {
  347. acpi_ns_delete_children(deletion_node);
  348. acpi_ns_delete_node(deletion_node);
  349. deletion_node = NULL;
  350. }
  351. if (child_node) {
  352. if (child_node->owner_id == owner_id) {
  353. /* Found a matching child node - detach any attached object */
  354. acpi_ns_detach_object(child_node);
  355. }
  356. /* Check if this node has any children */
  357. if (acpi_ns_get_next_node
  358. (ACPI_TYPE_ANY, child_node, NULL)) {
  359. /*
  360. * There is at least one child of this node,
  361. * visit the node
  362. */
  363. level++;
  364. parent_node = child_node;
  365. child_node = NULL;
  366. } else if (child_node->owner_id == owner_id) {
  367. deletion_node = child_node;
  368. }
  369. } else {
  370. /*
  371. * No more children of this parent node.
  372. * Move up to the grandparent.
  373. */
  374. level--;
  375. if (level != 0) {
  376. if (parent_node->owner_id == owner_id) {
  377. deletion_node = parent_node;
  378. }
  379. }
  380. /* New "last child" is this parent node */
  381. child_node = parent_node;
  382. /* Move up the tree to the grandparent */
  383. parent_node = acpi_ns_get_parent_node(parent_node);
  384. }
  385. }
  386. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  387. return_VOID;
  388. }