nsalloc.c 15 KB

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