nsalloc.c 18 KB

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