nsalloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsalloc - Namespace allocation and deletion utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, 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. /* Local prototypes */
  47. static void acpi_ns_remove_reference(struct acpi_namespace_node *node);
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ns_create_node
  51. *
  52. * PARAMETERS: Name - Name of the new node (4 char ACPI name)
  53. *
  54. * RETURN: New namespace node (Null on failure)
  55. *
  56. * DESCRIPTION: Create a namespace node
  57. *
  58. ******************************************************************************/
  59. struct acpi_namespace_node *acpi_ns_create_node(u32 name)
  60. {
  61. struct acpi_namespace_node *node;
  62. ACPI_FUNCTION_TRACE("ns_create_node");
  63. node = ACPI_MEM_CALLOCATE(sizeof(struct acpi_namespace_node));
  64. if (!node) {
  65. return_PTR(NULL);
  66. }
  67. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
  68. node->name.integer = name;
  69. node->reference_count = 1;
  70. ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
  71. return_PTR(node);
  72. }
  73. /*******************************************************************************
  74. *
  75. * FUNCTION: acpi_ns_delete_node
  76. *
  77. * PARAMETERS: Node - Node to be deleted
  78. *
  79. * RETURN: None
  80. *
  81. * DESCRIPTION: Delete a namespace node
  82. *
  83. ******************************************************************************/
  84. void acpi_ns_delete_node(struct acpi_namespace_node *node)
  85. {
  86. struct acpi_namespace_node *parent_node;
  87. struct acpi_namespace_node *prev_node;
  88. struct acpi_namespace_node *next_node;
  89. ACPI_FUNCTION_TRACE_PTR("ns_delete_node", node);
  90. parent_node = acpi_ns_get_parent_node(node);
  91. prev_node = NULL;
  92. next_node = parent_node->child;
  93. /* Find the node that is the previous peer in the parent's child list */
  94. while (next_node != node) {
  95. prev_node = next_node;
  96. next_node = prev_node->peer;
  97. }
  98. if (prev_node) {
  99. /* Node is not first child, unlink it */
  100. prev_node->peer = next_node->peer;
  101. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  102. prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
  103. }
  104. } else {
  105. /* Node is first child (has no previous peer) */
  106. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  107. /* No peers at all */
  108. parent_node->child = NULL;
  109. } else { /* Link peer list to parent */
  110. parent_node->child = next_node->peer;
  111. }
  112. }
  113. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  114. /*
  115. * Detach an object if there is one then delete the node
  116. */
  117. acpi_ns_detach_object(node);
  118. ACPI_MEM_FREE(node);
  119. return_VOID;
  120. }
  121. /*******************************************************************************
  122. *
  123. * FUNCTION: acpi_ns_install_node
  124. *
  125. * PARAMETERS: walk_state - Current state of the walk
  126. * parent_node - The parent of the new Node
  127. * Node - The new Node to install
  128. * Type - ACPI object type of the new Node
  129. *
  130. * RETURN: None
  131. *
  132. * DESCRIPTION: Initialize a new namespace node and install it amongst
  133. * its peers.
  134. *
  135. * Note: Current namespace lookup is linear search. This appears
  136. * to be sufficient as namespace searches consume only a small
  137. * fraction of the execution time of the ACPI subsystem.
  138. *
  139. ******************************************************************************/
  140. void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
  141. struct acpi_namespace_node *node, /* New Child */
  142. acpi_object_type type)
  143. {
  144. acpi_owner_id owner_id = 0;
  145. struct acpi_namespace_node *child_node;
  146. ACPI_FUNCTION_TRACE("ns_install_node");
  147. /*
  148. * Get the owner ID from the Walk state
  149. * The owner ID is used to track table deletion and
  150. * deletion of objects created by methods
  151. */
  152. if (walk_state) {
  153. owner_id = walk_state->owner_id;
  154. }
  155. /* Link the new entry into the parent and existing children */
  156. child_node = parent_node->child;
  157. if (!child_node) {
  158. parent_node->child = node;
  159. node->flags |= ANOBJ_END_OF_PEER_LIST;
  160. node->peer = parent_node;
  161. } else {
  162. while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
  163. child_node = child_node->peer;
  164. }
  165. child_node->peer = node;
  166. /* Clear end-of-list flag */
  167. child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
  168. node->flags |= ANOBJ_END_OF_PEER_LIST;
  169. node->peer = parent_node;
  170. }
  171. /* Init the new entry */
  172. node->owner_id = owner_id;
  173. node->type = (u8) type;
  174. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  175. "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
  176. acpi_ut_get_node_name(node),
  177. acpi_ut_get_type_name(node->type), node, owner_id,
  178. acpi_ut_get_node_name(parent_node),
  179. acpi_ut_get_type_name(parent_node->type),
  180. parent_node));
  181. /*
  182. * Increment the reference count(s) of all parents up to
  183. * the root!
  184. */
  185. while ((node = acpi_ns_get_parent_node(node)) != NULL) {
  186. node->reference_count++;
  187. }
  188. return_VOID;
  189. }
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: acpi_ns_delete_children
  193. *
  194. * PARAMETERS: parent_node - Delete this objects children
  195. *
  196. * RETURN: None.
  197. *
  198. * DESCRIPTION: Delete all children of the parent object. In other words,
  199. * deletes a "scope".
  200. *
  201. ******************************************************************************/
  202. void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
  203. {
  204. struct acpi_namespace_node *child_node;
  205. struct acpi_namespace_node *next_node;
  206. struct acpi_namespace_node *node;
  207. u8 flags;
  208. ACPI_FUNCTION_TRACE_PTR("ns_delete_children", parent_node);
  209. if (!parent_node) {
  210. return_VOID;
  211. }
  212. /* If no children, all done! */
  213. child_node = parent_node->child;
  214. if (!child_node) {
  215. return_VOID;
  216. }
  217. /*
  218. * Deallocate all children at this level
  219. */
  220. do {
  221. /* Get the things we need */
  222. next_node = child_node->peer;
  223. flags = child_node->flags;
  224. /* Grandchildren should have all been deleted already */
  225. if (child_node->child) {
  226. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  227. "Found a grandchild! P=%p C=%p\n",
  228. parent_node, child_node));
  229. }
  230. /* Now we can free this child object */
  231. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  232. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  233. "Object %p, Remaining %X\n", child_node,
  234. acpi_gbl_current_node_count));
  235. /*
  236. * Detach an object if there is one, then free the child node
  237. */
  238. acpi_ns_detach_object(child_node);
  239. /*
  240. * Decrement the reference count(s) of all parents up to
  241. * the root! (counts were incremented when the node was created)
  242. */
  243. node = child_node;
  244. while ((node = acpi_ns_get_parent_node(node)) != NULL) {
  245. node->reference_count--;
  246. }
  247. /* There should be only one reference remaining on this node */
  248. if (child_node->reference_count != 1) {
  249. ACPI_REPORT_WARNING(("Existing references (%d) on node being deleted (%p)\n", child_node->reference_count, child_node));
  250. }
  251. /* Now we can delete the node */
  252. ACPI_MEM_FREE(child_node);
  253. /* And move on to the next child in the list */
  254. child_node = next_node;
  255. } while (!(flags & ANOBJ_END_OF_PEER_LIST));
  256. /* Clear the parent's child pointer */
  257. parent_node->child = NULL;
  258. return_VOID;
  259. }
  260. /*******************************************************************************
  261. *
  262. * FUNCTION: acpi_ns_delete_namespace_subtree
  263. *
  264. * PARAMETERS: parent_node - Root of the subtree to be deleted
  265. *
  266. * RETURN: None.
  267. *
  268. * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
  269. * stored within the subtree.
  270. *
  271. ******************************************************************************/
  272. void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
  273. {
  274. struct acpi_namespace_node *child_node = NULL;
  275. u32 level = 1;
  276. ACPI_FUNCTION_TRACE("ns_delete_namespace_subtree");
  277. if (!parent_node) {
  278. return_VOID;
  279. }
  280. /*
  281. * Traverse the tree of objects until we bubble back up
  282. * to where we started.
  283. */
  284. while (level > 0) {
  285. /* Get the next node in this scope (NULL if none) */
  286. child_node = acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
  287. child_node);
  288. if (child_node) {
  289. /* Found a child node - detach any attached object */
  290. acpi_ns_detach_object(child_node);
  291. /* Check if this node has any children */
  292. if (acpi_ns_get_next_node
  293. (ACPI_TYPE_ANY, child_node, NULL)) {
  294. /*
  295. * There is at least one child of this node,
  296. * visit the node
  297. */
  298. level++;
  299. parent_node = child_node;
  300. child_node = NULL;
  301. }
  302. } else {
  303. /*
  304. * No more children of this parent node.
  305. * Move up to the grandparent.
  306. */
  307. level--;
  308. /*
  309. * Now delete all of the children of this parent
  310. * all at the same time.
  311. */
  312. acpi_ns_delete_children(parent_node);
  313. /* New "last child" is this parent node */
  314. child_node = parent_node;
  315. /* Move up the tree to the grandparent */
  316. parent_node = acpi_ns_get_parent_node(parent_node);
  317. }
  318. }
  319. return_VOID;
  320. }
  321. /*******************************************************************************
  322. *
  323. * FUNCTION: acpi_ns_remove_reference
  324. *
  325. * PARAMETERS: Node - Named node whose reference count is to be
  326. * decremented
  327. *
  328. * RETURN: None.
  329. *
  330. * DESCRIPTION: Remove a Node reference. Decrements the reference count
  331. * of all parent Nodes up to the root. Any node along
  332. * the way that reaches zero references is freed.
  333. *
  334. ******************************************************************************/
  335. static void acpi_ns_remove_reference(struct acpi_namespace_node *node)
  336. {
  337. struct acpi_namespace_node *parent_node;
  338. struct acpi_namespace_node *this_node;
  339. ACPI_FUNCTION_ENTRY();
  340. /*
  341. * Decrement the reference count(s) of this node and all
  342. * nodes up to the root, Delete anything with zero remaining references.
  343. */
  344. this_node = node;
  345. while (this_node) {
  346. /* Prepare to move up to parent */
  347. parent_node = acpi_ns_get_parent_node(this_node);
  348. /* Decrement the reference count on this node */
  349. this_node->reference_count--;
  350. /* Delete the node if no more references */
  351. if (!this_node->reference_count) {
  352. /* Delete all children and delete the node */
  353. acpi_ns_delete_children(this_node);
  354. acpi_ns_delete_node(this_node);
  355. }
  356. this_node = parent_node;
  357. }
  358. }
  359. /*******************************************************************************
  360. *
  361. * FUNCTION: acpi_ns_delete_namespace_by_owner
  362. *
  363. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  364. *
  365. * RETURN: Status
  366. *
  367. * DESCRIPTION: Delete entries within the namespace that are owned by a
  368. * specific ID. Used to delete entire ACPI tables. All
  369. * reference counts are updated.
  370. *
  371. ******************************************************************************/
  372. void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
  373. {
  374. struct acpi_namespace_node *child_node;
  375. struct acpi_namespace_node *deletion_node;
  376. u32 level;
  377. struct acpi_namespace_node *parent_node;
  378. ACPI_FUNCTION_TRACE_U32("ns_delete_namespace_by_owner", owner_id);
  379. if (owner_id == 0) {
  380. return_VOID;
  381. }
  382. parent_node = acpi_gbl_root_node;
  383. child_node = NULL;
  384. deletion_node = NULL;
  385. level = 1;
  386. /*
  387. * Traverse the tree of nodes until we bubble back up
  388. * to where we started.
  389. */
  390. while (level > 0) {
  391. /*
  392. * Get the next child of this parent node. When child_node is NULL,
  393. * the first child of the parent is returned
  394. */
  395. child_node =
  396. acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
  397. child_node);
  398. if (deletion_node) {
  399. acpi_ns_remove_reference(deletion_node);
  400. deletion_node = NULL;
  401. }
  402. if (child_node) {
  403. if (child_node->owner_id == owner_id) {
  404. /* Found a matching child node - detach any attached object */
  405. acpi_ns_detach_object(child_node);
  406. }
  407. /* Check if this node has any children */
  408. if (acpi_ns_get_next_node
  409. (ACPI_TYPE_ANY, child_node, NULL)) {
  410. /*
  411. * There is at least one child of this node,
  412. * visit the node
  413. */
  414. level++;
  415. parent_node = child_node;
  416. child_node = NULL;
  417. } else if (child_node->owner_id == owner_id) {
  418. deletion_node = child_node;
  419. }
  420. } else {
  421. /*
  422. * No more children of this parent node.
  423. * Move up to the grandparent.
  424. */
  425. level--;
  426. if (level != 0) {
  427. if (parent_node->owner_id == owner_id) {
  428. deletion_node = parent_node;
  429. }
  430. }
  431. /* New "last child" is this parent node */
  432. child_node = parent_node;
  433. /* Move up the tree to the grandparent */
  434. parent_node = acpi_ns_get_parent_node(parent_node);
  435. }
  436. }
  437. return_VOID;
  438. }