utdelete.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*******************************************************************************
  2.  *
  3.  * Module Name: utdelete - object deletion and reference count utilities
  4.  *              $Revision: 81 $
  5.  *
  6.  ******************************************************************************/
  7. /*
  8.  *  Copyright (C) 2000, 2001 R. Byron Moore
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23.  */
  24. #include "acpi.h"
  25. #include "acinterp.h"
  26. #include "acnamesp.h"
  27. #include "actables.h"
  28. #include "acparser.h"
  29. #define _COMPONENT          ACPI_UTILITIES
  30.  MODULE_NAME         ("utdelete")
  31. /*******************************************************************************
  32.  *
  33.  * FUNCTION:    Acpi_ut_delete_internal_obj
  34.  *
  35.  * PARAMETERS:  *Object        - Pointer to the list to be deleted
  36.  *
  37.  * RETURN:      None
  38.  *
  39.  * DESCRIPTION: Low level object deletion, after reference counts have been
  40.  *              updated (All reference counts, including sub-objects!)
  41.  *
  42.  ******************************************************************************/
  43. void
  44. acpi_ut_delete_internal_obj (
  45. acpi_operand_object     *object)
  46. {
  47. void                    *obj_pointer = NULL;
  48. acpi_operand_object     *handler_desc;
  49. FUNCTION_TRACE_PTR ("Ut_delete_internal_obj", object);
  50. if (!object) {
  51. return_VOID;
  52. }
  53. /*
  54.  * Must delete or free any pointers within the object that are not
  55.  * actual ACPI objects (for example, a raw buffer pointer).
  56.  */
  57. switch (object->common.type) {
  58. case ACPI_TYPE_STRING:
  59. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** String %p, ptr %pn",
  60. object, object->string.pointer));
  61. /* Free the actual string buffer */
  62. if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
  63. obj_pointer = object->string.pointer;
  64. }
  65. break;
  66. case ACPI_TYPE_BUFFER:
  67. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Buffer %p, ptr %pn",
  68. object, object->buffer.pointer));
  69. /* Free the actual buffer */
  70. obj_pointer = object->buffer.pointer;
  71. break;
  72. case ACPI_TYPE_PACKAGE:
  73. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, " **** Package of count %Xn",
  74. object->package.count));
  75. /*
  76.  * Elements of the package are not handled here, they are deleted
  77.  * separately
  78.  */
  79. /* Free the (variable length) element pointer array */
  80. obj_pointer = object->package.elements;
  81. break;
  82. case ACPI_TYPE_MUTEX:
  83. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "***** Mutex %p, Semaphore %pn",
  84. object, object->mutex.semaphore));
  85. acpi_ex_unlink_mutex (object);
  86. acpi_os_delete_semaphore (object->mutex.semaphore);
  87. break;
  88. case ACPI_TYPE_EVENT:
  89. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "***** Event %p, Semaphore %pn",
  90. object, object->event.semaphore));
  91. acpi_os_delete_semaphore (object->event.semaphore);
  92. object->event.semaphore = NULL;
  93. break;
  94. case ACPI_TYPE_METHOD:
  95. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "***** Method %pn", object));
  96. /* Delete the method semaphore if it exists */
  97. if (object->method.semaphore) {
  98. acpi_os_delete_semaphore (object->method.semaphore);
  99. object->method.semaphore = NULL;
  100. }
  101. break;
  102. case ACPI_TYPE_REGION:
  103. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "***** Region %pn", object));
  104. if (object->region.extra) {
  105. /*
  106.  * Free the Region_context if and only if the handler is one of the
  107.  * default handlers -- and therefore, we created the context object
  108.  * locally, it was not created by an external caller.
  109.  */
  110. handler_desc = object->region.addr_handler;
  111. if ((handler_desc) &&
  112. (handler_desc->addr_handler.hflags == ADDR_HANDLER_DEFAULT_INSTALLED)) {
  113. obj_pointer = object->region.extra->extra.region_context;
  114. }
  115. /* Now we can free the Extra object */
  116. acpi_ut_delete_object_desc (object->region.extra);
  117. }
  118. break;
  119. case ACPI_TYPE_BUFFER_FIELD:
  120. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "***** Buffer Field %pn", object));
  121. if (object->buffer_field.extra) {
  122. acpi_ut_delete_object_desc (object->buffer_field.extra);
  123. }
  124. break;
  125. default:
  126. break;
  127. }
  128. /*
  129.  * Delete any allocated memory found above
  130.  */
  131. if (obj_pointer) {
  132. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Deleting Obj Ptr %p n", obj_pointer));
  133. ACPI_MEM_FREE (obj_pointer);
  134. }
  135. /* Only delete the object if it was dynamically allocated */
  136. if (object->common.flags & AOPOBJ_STATIC_ALLOCATION) {
  137. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Object %p [%s] static allocation, no deleten",
  138. object, acpi_ut_get_type_name (object->common.type)));
  139. }
  140. if (!(object->common.flags & AOPOBJ_STATIC_ALLOCATION)) {
  141. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Deleting object %p [%s]n",
  142. object, acpi_ut_get_type_name (object->common.type)));
  143. acpi_ut_delete_object_desc (object);
  144. }
  145. return_VOID;
  146. }
  147. /*******************************************************************************
  148.  *
  149.  * FUNCTION:    Acpi_ut_delete_internal_object_list
  150.  *
  151.  * PARAMETERS:  *Obj_list       - Pointer to the list to be deleted
  152.  *
  153.  * RETURN:      Status          - the status of the call
  154.  *
  155.  * DESCRIPTION: This function deletes an internal object list, including both
  156.  *              simple objects and package objects
  157.  *
  158.  ******************************************************************************/
  159. acpi_status
  160. acpi_ut_delete_internal_object_list (
  161. acpi_operand_object     **obj_list)
  162. {
  163. acpi_operand_object     **internal_obj;
  164. FUNCTION_TRACE ("Ut_delete_internal_object_list");
  165. /* Walk the null-terminated internal list */
  166. for (internal_obj = obj_list; *internal_obj; internal_obj++) {
  167. acpi_ut_remove_reference (*internal_obj);
  168. }
  169. /* Free the combined parameter pointer list and object array */
  170. ACPI_MEM_FREE (obj_list);
  171. return_ACPI_STATUS (AE_OK);
  172. }
  173. /*******************************************************************************
  174.  *
  175.  * FUNCTION:    Acpi_ut_update_ref_count
  176.  *
  177.  * PARAMETERS:  *Object         - Object whose ref count is to be updated
  178.  *              Action          - What to do
  179.  *
  180.  * RETURN:      New ref count
  181.  *
  182.  * DESCRIPTION: Modify the ref count and return it.
  183.  *
  184.  ******************************************************************************/
  185. static void
  186. acpi_ut_update_ref_count (
  187. acpi_operand_object     *object,
  188. u32                     action)
  189. {
  190. u16                     count;
  191. u16                     new_count;
  192. PROC_NAME ("Ut_update_ref_count");
  193. if (!object) {
  194. return;
  195. }
  196. count = object->common.reference_count;
  197. new_count = count;
  198. /*
  199.  * Reference count action (increment, decrement, or force delete)
  200.  */
  201. switch (action) {
  202. case REF_INCREMENT:
  203. new_count++;
  204. object->common.reference_count = new_count;
  205. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Obj %p Refs=%X, [Incremented]n",
  206. object, new_count));
  207. break;
  208. case REF_DECREMENT:
  209. if (count < 1) {
  210. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Obj %p Refs=%X, can't decrement! (Set to 0)n",
  211. object, new_count));
  212. new_count = 0;
  213. }
  214. else {
  215. new_count--;
  216. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Obj %p Refs=%X, [Decremented]n",
  217. object, new_count));
  218. }
  219. if (object->common.type == ACPI_TYPE_METHOD) {
  220. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Method Obj %p Refs=%X, [Decremented]n",
  221. object, new_count));
  222. }
  223. object->common.reference_count = new_count;
  224. if (new_count == 0) {
  225. acpi_ut_delete_internal_obj (object);
  226. }
  227. break;
  228. case REF_FORCE_DELETE:
  229. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Obj %p Refs=%X, Force delete! (Set to 0)n",
  230. object, count));
  231. new_count = 0;
  232. object->common.reference_count = new_count;
  233. acpi_ut_delete_internal_obj (object);
  234. break;
  235. default:
  236. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown action (%X)n", action));
  237. break;
  238. }
  239. /*
  240.  * Sanity check the reference count, for debug purposes only.
  241.  * (A deleted object will have a huge reference count)
  242.  */
  243. if (count > MAX_REFERENCE_COUNT) {
  244. ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
  245. "**** Warning **** Large Reference Count (%X) in object %pnn",
  246. count, object));
  247. }
  248. return;
  249. }
  250. /*******************************************************************************
  251.  *
  252.  * FUNCTION:    Acpi_ut_update_object_reference
  253.  *
  254.  * PARAMETERS:  *Object             - Increment ref count for this object
  255.  *                                    and all sub-objects
  256.  *              Action              - Either REF_INCREMENT or REF_DECREMENT or
  257.  *                                    REF_FORCE_DELETE
  258.  *
  259.  * RETURN:      Status
  260.  *
  261.  * DESCRIPTION: Increment the object reference count
  262.  *
  263.  * Object references are incremented when:
  264.  * 1) An object is attached to a Node (namespace object)
  265.  * 2) An object is copied (all subobjects must be incremented)
  266.  *
  267.  * Object references are decremented when:
  268.  * 1) An object is detached from an Node
  269.  *
  270.  ******************************************************************************/
  271. acpi_status
  272. acpi_ut_update_object_reference (
  273. acpi_operand_object     *object,
  274. u16                     action)
  275. {
  276. acpi_status             status;
  277. u32                     i;
  278. acpi_operand_object     *next;
  279. acpi_operand_object     *new;
  280. acpi_generic_state       *state_list = NULL;
  281. acpi_generic_state       *state;
  282. FUNCTION_TRACE_PTR ("Ut_update_object_reference", object);
  283. /* Ignore a null object ptr */
  284. if (!object) {
  285. return_ACPI_STATUS (AE_OK);
  286. }
  287. /*
  288.  * Make sure that this isn't a namespace handle or an AML pointer
  289.  */
  290. if (VALID_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_NAMED)) {
  291. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Object %p is NS handlen", object));
  292. return_ACPI_STATUS (AE_OK);
  293. }
  294. state = acpi_ut_create_update_state (object, action);
  295. while (state) {
  296. object = state->update.object;
  297. action = state->update.value;
  298. acpi_ut_delete_generic_state (state);
  299. /*
  300.  * All sub-objects must have their reference count incremented also.
  301.  * Different object types have different subobjects.
  302.  */
  303. switch (object->common.type) {
  304. case ACPI_TYPE_DEVICE:
  305. status = acpi_ut_create_update_state_and_push (object->device.addr_handler,
  306.    action, &state_list);
  307. if (ACPI_FAILURE (status)) {
  308. return_ACPI_STATUS (status);
  309. }
  310. acpi_ut_update_ref_count (object->device.sys_handler, action);
  311. acpi_ut_update_ref_count (object->device.drv_handler, action);
  312. break;
  313. case INTERNAL_TYPE_ADDRESS_HANDLER:
  314. /* Must walk list of address handlers */
  315. next = object->addr_handler.next;
  316. while (next) {
  317. new = next->addr_handler.next;
  318. acpi_ut_update_ref_count (next, action);
  319. next = new;
  320. }
  321. break;
  322. case ACPI_TYPE_PACKAGE:
  323. /*
  324.  * We must update all the sub-objects of the package
  325.  * (Each of whom may have their own sub-objects, etc.
  326.  */
  327. for (i = 0; i < object->package.count; i++) {
  328. /*
  329.  * Push each element onto the stack for later processing.
  330.  * Note: There can be null elements within the package,
  331.  * these are simply ignored
  332.  */
  333. status = acpi_ut_create_update_state_and_push (
  334.  object->package.elements[i], action, &state_list);
  335. if (ACPI_FAILURE (status)) {
  336. return_ACPI_STATUS (status);
  337. }
  338. }
  339. break;
  340. case ACPI_TYPE_BUFFER_FIELD:
  341. status = acpi_ut_create_update_state_and_push (
  342.  object->buffer_field.buffer_obj, action, &state_list);
  343. if (ACPI_FAILURE (status)) {
  344. return_ACPI_STATUS (status);
  345. }
  346. break;
  347. case INTERNAL_TYPE_REGION_FIELD:
  348. status = acpi_ut_create_update_state_and_push (
  349.  object->field.region_obj, action, &state_list);
  350. if (ACPI_FAILURE (status)) {
  351. return_ACPI_STATUS (status);
  352. }
  353.    break;
  354. case INTERNAL_TYPE_BANK_FIELD:
  355. status = acpi_ut_create_update_state_and_push (
  356.  object->bank_field.bank_register_obj, action, &state_list);
  357. if (ACPI_FAILURE (status)) {
  358. return_ACPI_STATUS (status);
  359. }
  360. status = acpi_ut_create_update_state_and_push (
  361.  object->bank_field.region_obj, action, &state_list);
  362. if (ACPI_FAILURE (status)) {
  363. return_ACPI_STATUS (status);
  364. }
  365. break;
  366. case INTERNAL_TYPE_INDEX_FIELD:
  367. status = acpi_ut_create_update_state_and_push (
  368.  object->index_field.index_obj, action, &state_list);
  369. if (ACPI_FAILURE (status)) {
  370. return_ACPI_STATUS (status);
  371. }
  372. status = acpi_ut_create_update_state_and_push (
  373.  object->index_field.data_obj, action, &state_list);
  374. if (ACPI_FAILURE (status)) {
  375. return_ACPI_STATUS (status);
  376. }
  377. break;
  378. case ACPI_TYPE_REGION:
  379. case INTERNAL_TYPE_REFERENCE:
  380. /* No subobjects */
  381. break;
  382. }
  383. /*
  384.  * Now we can update the count in the main object.  This can only
  385.  * happen after we update the sub-objects in case this causes the
  386.  * main object to be deleted.
  387.  */
  388. acpi_ut_update_ref_count (object, action);
  389. /* Move on to the next object to be updated */
  390. state = acpi_ut_pop_generic_state (&state_list);
  391. }
  392. return_ACPI_STATUS (AE_OK);
  393. }
  394. /*******************************************************************************
  395.  *
  396.  * FUNCTION:    Acpi_ut_add_reference
  397.  *
  398.  * PARAMETERS:  *Object        - Object whose reference count is to be
  399.  *                                  incremented
  400.  *
  401.  * RETURN:      None
  402.  *
  403.  * DESCRIPTION: Add one reference to an ACPI object
  404.  *
  405.  ******************************************************************************/
  406. void
  407. acpi_ut_add_reference (
  408. acpi_operand_object     *object)
  409. {
  410. FUNCTION_TRACE_PTR ("Ut_add_reference", object);
  411. /*
  412.  * Ensure that we have a valid object
  413.  */
  414. if (!acpi_ut_valid_internal_object (object)) {
  415. return_VOID;
  416. }
  417. /*
  418.  * We have a valid ACPI internal object, now increment the reference count
  419.  */
  420. acpi_ut_update_object_reference (object, REF_INCREMENT);
  421. return_VOID;
  422. }
  423. /*******************************************************************************
  424.  *
  425.  * FUNCTION:    Acpi_ut_remove_reference
  426.  *
  427.  * PARAMETERS:  *Object        - Object whose ref count will be decremented
  428.  *
  429.  * RETURN:      None
  430.  *
  431.  * DESCRIPTION: Decrement the reference count of an ACPI internal object
  432.  *
  433.  ******************************************************************************/
  434. void
  435. acpi_ut_remove_reference (
  436. acpi_operand_object     *object)
  437. {
  438. FUNCTION_TRACE_PTR ("Ut_remove_reference", object);
  439. /*
  440.  * Allow a NULL pointer to be passed in, just ignore it.  This saves
  441.  * each caller from having to check.  Also, ignore NS nodes.
  442.  *
  443.  */
  444. if (!object ||
  445. (VALID_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_NAMED))) {
  446. return_VOID;
  447. }
  448. /*
  449.  * Ensure that we have a valid object
  450.  */
  451. if (!acpi_ut_valid_internal_object (object)) {
  452. return_VOID;
  453. }
  454. ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Obj %p Refs=%Xn",
  455. object, object->common.reference_count));
  456. /*
  457.  * Decrement the reference count, and only actually delete the object
  458.  * if the reference count becomes 0.  (Must also decrement the ref count
  459.  * of all subobjects!)
  460.  */
  461. acpi_ut_update_object_reference (object, REF_DECREMENT);
  462. return_VOID;
  463. }