nsalloc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ns_create_node
  49. *
  50. * PARAMETERS: acpi_name - Name of the new node
  51. *
  52. * RETURN: None
  53. *
  54. * DESCRIPTION: Create a namespace node
  55. *
  56. ******************************************************************************/
  57. struct acpi_namespace_node *
  58. acpi_ns_create_node (
  59. 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_memory_lists[ACPI_MEM_LIST_NSNODE].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
  85. acpi_ns_delete_node (
  86. struct acpi_namespace_node *node)
  87. {
  88. struct acpi_namespace_node *parent_node;
  89. struct acpi_namespace_node *prev_node;
  90. struct acpi_namespace_node *next_node;
  91. ACPI_FUNCTION_TRACE_PTR ("ns_delete_node", node);
  92. parent_node = acpi_ns_get_parent_node (node);
  93. prev_node = NULL;
  94. next_node = parent_node->child;
  95. /* Find the node that is the previous peer in the parent's child list */
  96. while (next_node != node) {
  97. prev_node = next_node;
  98. next_node = prev_node->peer;
  99. }
  100. if (prev_node) {
  101. /* Node is not first child, unlink it */
  102. prev_node->peer = next_node->peer;
  103. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  104. prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
  105. }
  106. }
  107. else {
  108. /* Node is first child (has no previous peer) */
  109. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  110. /* No peers at all */
  111. parent_node->child = NULL;
  112. }
  113. else { /* Link peer list to parent */
  114. parent_node->child = next_node->peer;
  115. }
  116. }
  117. ACPI_MEM_TRACKING (acpi_gbl_memory_lists[ACPI_MEM_LIST_NSNODE].total_freed++);
  118. /*
  119. * Detach an object if there is one then delete the node
  120. */
  121. acpi_ns_detach_object (node);
  122. ACPI_MEM_FREE (node);
  123. return_VOID;
  124. }
  125. #ifdef ACPI_ALPHABETIC_NAMESPACE
  126. /*******************************************************************************
  127. *
  128. * FUNCTION: acpi_ns_compare_names
  129. *
  130. * PARAMETERS: Name1 - First name to compare
  131. * Name2 - Second name to compare
  132. *
  133. * RETURN: value from strncmp
  134. *
  135. * DESCRIPTION: Compare two ACPI names. Names that are prefixed with an
  136. * underscore are forced to be alphabetically first.
  137. *
  138. ******************************************************************************/
  139. int
  140. acpi_ns_compare_names (
  141. char *name1,
  142. char *name2)
  143. {
  144. char reversed_name1[ACPI_NAME_SIZE];
  145. char reversed_name2[ACPI_NAME_SIZE];
  146. u32 i;
  147. u32 j;
  148. /*
  149. * Replace all instances of "underscore" with a value that is smaller so
  150. * that all names that are prefixed with underscore(s) are alphabetically
  151. * first.
  152. *
  153. * Reverse the name bytewise so we can just do a 32-bit compare instead
  154. * of a strncmp.
  155. */
  156. for (i = 0, j= (ACPI_NAME_SIZE - 1); i < ACPI_NAME_SIZE; i++, j--) {
  157. reversed_name1[j] = name1[i];
  158. if (name1[i] == '_') {
  159. reversed_name1[j] = '*';
  160. }
  161. reversed_name2[j] = name2[i];
  162. if (name2[i] == '_') {
  163. reversed_name2[j] = '*';
  164. }
  165. }
  166. return (*(int *) reversed_name1 - *(int *) reversed_name2);
  167. }
  168. #endif
  169. /*******************************************************************************
  170. *
  171. * FUNCTION: acpi_ns_install_node
  172. *
  173. * PARAMETERS: walk_state - Current state of the walk
  174. * parent_node - The parent of the new Node
  175. * Node - The new Node to install
  176. * Type - ACPI object type of the new Node
  177. *
  178. * RETURN: None
  179. *
  180. * DESCRIPTION: Initialize a new namespace node and install it amongst
  181. * its peers.
  182. *
  183. * Note: Current namespace lookup is linear search. However, the
  184. * nodes are linked in alphabetical order to 1) put all reserved
  185. * names (start with underscore) first, and to 2) make a readable
  186. * namespace dump.
  187. *
  188. ******************************************************************************/
  189. void
  190. acpi_ns_install_node (
  191. struct acpi_walk_state *walk_state,
  192. struct acpi_namespace_node *parent_node, /* Parent */
  193. struct acpi_namespace_node *node, /* New Child*/
  194. acpi_object_type type)
  195. {
  196. u16 owner_id = 0;
  197. struct acpi_namespace_node *child_node;
  198. #ifdef ACPI_ALPHABETIC_NAMESPACE
  199. struct acpi_namespace_node *previous_child_node;
  200. #endif
  201. ACPI_FUNCTION_TRACE ("ns_install_node");
  202. /*
  203. * Get the owner ID from the Walk state
  204. * The owner ID is used to track table deletion and
  205. * deletion of objects created by methods
  206. */
  207. if (walk_state) {
  208. owner_id = walk_state->owner_id;
  209. }
  210. /* Link the new entry into the parent and existing children */
  211. child_node = parent_node->child;
  212. if (!child_node) {
  213. parent_node->child = node;
  214. node->flags |= ANOBJ_END_OF_PEER_LIST;
  215. node->peer = parent_node;
  216. }
  217. else {
  218. #ifdef ACPI_ALPHABETIC_NAMESPACE
  219. /*
  220. * Walk the list whilst searching for the correct
  221. * alphabetic placement.
  222. */
  223. previous_child_node = NULL;
  224. while (acpi_ns_compare_names (acpi_ut_get_node_name (child_node), acpi_ut_get_node_name (node)) < 0) {
  225. if (child_node->flags & ANOBJ_END_OF_PEER_LIST) {
  226. /* Last peer; Clear end-of-list flag */
  227. child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
  228. /* This node is the new peer to the child node */
  229. child_node->peer = node;
  230. /* This node is the new end-of-list */
  231. node->flags |= ANOBJ_END_OF_PEER_LIST;
  232. node->peer = parent_node;
  233. break;
  234. }
  235. /* Get next peer */
  236. previous_child_node = child_node;
  237. child_node = child_node->peer;
  238. }
  239. /* Did the node get inserted at the end-of-list? */
  240. if (!(node->flags & ANOBJ_END_OF_PEER_LIST)) {
  241. /*
  242. * Loop above terminated without reaching the end-of-list.
  243. * Insert the new node at the current location
  244. */
  245. if (previous_child_node) {
  246. /* Insert node alphabetically */
  247. node->peer = child_node;
  248. previous_child_node->peer = node;
  249. }
  250. else {
  251. /* Insert node alphabetically at start of list */
  252. node->peer = child_node;
  253. parent_node->child = node;
  254. }
  255. }
  256. #else
  257. while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
  258. child_node = child_node->peer;
  259. }
  260. child_node->peer = node;
  261. /* Clear end-of-list flag */
  262. child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
  263. node->flags |= ANOBJ_END_OF_PEER_LIST;
  264. node->peer = parent_node;
  265. #endif
  266. }
  267. /* Init the new entry */
  268. node->owner_id = owner_id;
  269. node->type = (u8) type;
  270. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  271. "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
  272. acpi_ut_get_node_name (node), acpi_ut_get_type_name (node->type), node, owner_id,
  273. acpi_ut_get_node_name (parent_node), acpi_ut_get_type_name (parent_node->type),
  274. parent_node));
  275. /*
  276. * Increment the reference count(s) of all parents up to
  277. * the root!
  278. */
  279. while ((node = acpi_ns_get_parent_node (node)) != NULL) {
  280. node->reference_count++;
  281. }
  282. return_VOID;
  283. }
  284. /*******************************************************************************
  285. *
  286. * FUNCTION: acpi_ns_delete_children
  287. *
  288. * PARAMETERS: parent_node - Delete this objects children
  289. *
  290. * RETURN: None.
  291. *
  292. * DESCRIPTION: Delete all children of the parent object. In other words,
  293. * deletes a "scope".
  294. *
  295. ******************************************************************************/
  296. void
  297. acpi_ns_delete_children (
  298. struct acpi_namespace_node *parent_node)
  299. {
  300. struct acpi_namespace_node *child_node;
  301. struct acpi_namespace_node *next_node;
  302. struct acpi_namespace_node *node;
  303. u8 flags;
  304. ACPI_FUNCTION_TRACE_PTR ("ns_delete_children", parent_node);
  305. if (!parent_node) {
  306. return_VOID;
  307. }
  308. /* If no children, all done! */
  309. child_node = parent_node->child;
  310. if (!child_node) {
  311. return_VOID;
  312. }
  313. /*
  314. * Deallocate all children at this level
  315. */
  316. do {
  317. /* Get the things we need */
  318. next_node = child_node->peer;
  319. flags = child_node->flags;
  320. /* Grandchildren should have all been deleted already */
  321. if (child_node->child) {
  322. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Found a grandchild! P=%p C=%p\n",
  323. parent_node, child_node));
  324. }
  325. /* Now we can free this child object */
  326. ACPI_MEM_TRACKING (acpi_gbl_memory_lists[ACPI_MEM_LIST_NSNODE].total_freed++);
  327. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Object %p, Remaining %X\n",
  328. child_node, acpi_gbl_current_node_count));
  329. /*
  330. * Detach an object if there is one, then free the child node
  331. */
  332. acpi_ns_detach_object (child_node);
  333. /*
  334. * Decrement the reference count(s) of all parents up to
  335. * the root! (counts were incremented when the node was created)
  336. */
  337. node = child_node;
  338. while ((node = acpi_ns_get_parent_node (node)) != NULL) {
  339. node->reference_count--;
  340. }
  341. /* There should be only one reference remaining on this node */
  342. if (child_node->reference_count != 1) {
  343. ACPI_REPORT_WARNING (("Existing references (%d) on node being deleted (%p)\n",
  344. child_node->reference_count, child_node));
  345. }
  346. /* Now we can delete the node */
  347. ACPI_MEM_FREE (child_node);
  348. /* And move on to the next child in the list */
  349. child_node = next_node;
  350. } while (!(flags & ANOBJ_END_OF_PEER_LIST));
  351. /* Clear the parent's child pointer */
  352. parent_node->child = NULL;
  353. return_VOID;
  354. }
  355. /*******************************************************************************
  356. *
  357. * FUNCTION: acpi_ns_delete_namespace_subtree
  358. *
  359. * PARAMETERS: parent_node - Root of the subtree to be deleted
  360. *
  361. * RETURN: None.
  362. *
  363. * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
  364. * stored within the subtree.
  365. *
  366. ******************************************************************************/
  367. void
  368. acpi_ns_delete_namespace_subtree (
  369. struct acpi_namespace_node *parent_node)
  370. {
  371. struct acpi_namespace_node *child_node = NULL;
  372. u32 level = 1;
  373. ACPI_FUNCTION_TRACE ("ns_delete_namespace_subtree");
  374. if (!parent_node) {
  375. return_VOID;
  376. }
  377. /*
  378. * Traverse the tree of objects until we bubble back up
  379. * to where we started.
  380. */
  381. while (level > 0) {
  382. /* Get the next node in this scope (NULL if none) */
  383. child_node = acpi_ns_get_next_node (ACPI_TYPE_ANY, parent_node,
  384. child_node);
  385. if (child_node) {
  386. /* Found a child node - detach any attached object */
  387. acpi_ns_detach_object (child_node);
  388. /* Check if this node has any children */
  389. if (acpi_ns_get_next_node (ACPI_TYPE_ANY, child_node, NULL)) {
  390. /*
  391. * There is at least one child of this node,
  392. * visit the node
  393. */
  394. level++;
  395. parent_node = child_node;
  396. child_node = NULL;
  397. }
  398. }
  399. else {
  400. /*
  401. * No more children of this parent node.
  402. * Move up to the grandparent.
  403. */
  404. level--;
  405. /*
  406. * Now delete all of the children of this parent
  407. * all at the same time.
  408. */
  409. acpi_ns_delete_children (parent_node);
  410. /* New "last child" is this parent node */
  411. child_node = parent_node;
  412. /* Move up the tree to the grandparent */
  413. parent_node = acpi_ns_get_parent_node (parent_node);
  414. }
  415. }
  416. return_VOID;
  417. }
  418. /*******************************************************************************
  419. *
  420. * FUNCTION: acpi_ns_remove_reference
  421. *
  422. * PARAMETERS: Node - Named node whose reference count is to be
  423. * decremented
  424. *
  425. * RETURN: None.
  426. *
  427. * DESCRIPTION: Remove a Node reference. Decrements the reference count
  428. * of all parent Nodes up to the root. Any node along
  429. * the way that reaches zero references is freed.
  430. *
  431. ******************************************************************************/
  432. void
  433. acpi_ns_remove_reference (
  434. struct acpi_namespace_node *node)
  435. {
  436. struct acpi_namespace_node *parent_node;
  437. struct acpi_namespace_node *this_node;
  438. ACPI_FUNCTION_ENTRY ();
  439. /*
  440. * Decrement the reference count(s) of this node and all
  441. * nodes up to the root, Delete anything with zero remaining references.
  442. */
  443. this_node = node;
  444. while (this_node) {
  445. /* Prepare to move up to parent */
  446. parent_node = acpi_ns_get_parent_node (this_node);
  447. /* Decrement the reference count on this node */
  448. this_node->reference_count--;
  449. /* Delete the node if no more references */
  450. if (!this_node->reference_count) {
  451. /* Delete all children and delete the node */
  452. acpi_ns_delete_children (this_node);
  453. acpi_ns_delete_node (this_node);
  454. }
  455. this_node = parent_node;
  456. }
  457. }
  458. /*******************************************************************************
  459. *
  460. * FUNCTION: acpi_ns_delete_namespace_by_owner
  461. *
  462. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  463. *
  464. * RETURN: Status
  465. *
  466. * DESCRIPTION: Delete entries within the namespace that are owned by a
  467. * specific ID. Used to delete entire ACPI tables. All
  468. * reference counts are updated.
  469. *
  470. ******************************************************************************/
  471. void
  472. acpi_ns_delete_namespace_by_owner (
  473. u16 owner_id)
  474. {
  475. struct acpi_namespace_node *child_node;
  476. struct acpi_namespace_node *deletion_node;
  477. u32 level;
  478. struct acpi_namespace_node *parent_node;
  479. ACPI_FUNCTION_TRACE_U32 ("ns_delete_namespace_by_owner", owner_id);
  480. parent_node = acpi_gbl_root_node;
  481. child_node = NULL;
  482. deletion_node = NULL;
  483. level = 1;
  484. /*
  485. * Traverse the tree of nodes until we bubble back up
  486. * to where we started.
  487. */
  488. while (level > 0) {
  489. /*
  490. * Get the next child of this parent node. When child_node is NULL,
  491. * the first child of the parent is returned
  492. */
  493. child_node = acpi_ns_get_next_node (ACPI_TYPE_ANY, parent_node, child_node);
  494. if (deletion_node) {
  495. acpi_ns_remove_reference (deletion_node);
  496. deletion_node = NULL;
  497. }
  498. if (child_node) {
  499. if (child_node->owner_id == owner_id) {
  500. /* Found a matching child node - detach any attached object */
  501. acpi_ns_detach_object (child_node);
  502. }
  503. /* Check if this node has any children */
  504. if (acpi_ns_get_next_node (ACPI_TYPE_ANY, child_node, NULL)) {
  505. /*
  506. * There is at least one child of this node,
  507. * visit the node
  508. */
  509. level++;
  510. parent_node = child_node;
  511. child_node = NULL;
  512. }
  513. else if (child_node->owner_id == owner_id) {
  514. deletion_node = child_node;
  515. }
  516. }
  517. else {
  518. /*
  519. * No more children of this parent node.
  520. * Move up to the grandparent.
  521. */
  522. level--;
  523. if (level != 0) {
  524. if (parent_node->owner_id == owner_id) {
  525. deletion_node = parent_node;
  526. }
  527. }
  528. /* New "last child" is this parent node */
  529. child_node = parent_node;
  530. /* Move up the tree to the grandparent */
  531. parent_node = acpi_ns_get_parent_node (parent_node);
  532. }
  533. }
  534. return_VOID;
  535. }