1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2004, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/acnamesp.h>
48 #include <acpi/acevents.h>
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME ("utdelete")
54 /*******************************************************************************
56 * FUNCTION: acpi_ut_delete_internal_obj
58 * PARAMETERS: *Object - Pointer to the list to be deleted
62 * DESCRIPTION: Low level object deletion, after reference counts have been
63 * updated (All reference counts, including sub-objects!)
65 ******************************************************************************/
68 acpi_ut_delete_internal_obj (
69 union acpi_operand_object *object)
71 void *obj_pointer = NULL;
72 union acpi_operand_object *handler_desc;
73 union acpi_operand_object *second_desc;
74 union acpi_operand_object *next_desc;
77 ACPI_FUNCTION_TRACE_PTR ("ut_delete_internal_obj", object);
85 * Must delete or free any pointers within the object that are not
86 * actual ACPI objects (for example, a raw buffer pointer).
88 switch (ACPI_GET_OBJECT_TYPE (object)) {
89 case ACPI_TYPE_STRING:
91 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
92 object, object->string.pointer));
94 /* Free the actual string buffer */
96 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
97 /* But only if it is NOT a pointer into an ACPI table */
99 obj_pointer = object->string.pointer;
104 case ACPI_TYPE_BUFFER:
106 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
107 object, object->buffer.pointer));
109 /* Free the actual buffer */
111 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
112 /* But only if it is NOT a pointer into an ACPI table */
114 obj_pointer = object->buffer.pointer;
119 case ACPI_TYPE_PACKAGE:
121 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
122 object->package.count));
125 * Elements of the package are not handled here, they are deleted
129 /* Free the (variable length) element pointer array */
131 obj_pointer = object->package.elements;
135 case ACPI_TYPE_DEVICE:
137 if (object->device.gpe_block) {
138 (void) acpi_ev_delete_gpe_block (object->device.gpe_block);
141 /* Walk the handler list for this device */
143 handler_desc = object->device.handler;
144 while (handler_desc) {
145 next_desc = handler_desc->address_space.next;
146 acpi_ut_remove_reference (handler_desc);
147 handler_desc = next_desc;
152 case ACPI_TYPE_MUTEX:
154 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Mutex %p, Semaphore %p\n",
155 object, object->mutex.semaphore));
157 acpi_ex_unlink_mutex (object);
158 (void) acpi_os_delete_semaphore (object->mutex.semaphore);
162 case ACPI_TYPE_EVENT:
164 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Event %p, Semaphore %p\n",
165 object, object->event.semaphore));
167 (void) acpi_os_delete_semaphore (object->event.semaphore);
168 object->event.semaphore = NULL;
172 case ACPI_TYPE_METHOD:
174 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Method %p\n", object));
176 /* Delete the method semaphore if it exists */
178 if (object->method.semaphore) {
179 (void) acpi_os_delete_semaphore (object->method.semaphore);
180 object->method.semaphore = NULL;
185 case ACPI_TYPE_REGION:
187 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Region %p\n", object));
189 second_desc = acpi_ns_get_secondary_object (object);
192 * Free the region_context if and only if the handler is one of the
193 * default handlers -- and therefore, we created the context object
194 * locally, it was not created by an external caller.
196 handler_desc = object->region.handler;
198 if (handler_desc->address_space.hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
199 obj_pointer = second_desc->extra.region_context;
202 acpi_ut_remove_reference (handler_desc);
205 /* Now we can free the Extra object */
207 acpi_ut_delete_object_desc (second_desc);
212 case ACPI_TYPE_BUFFER_FIELD:
214 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Buffer Field %p\n", object));
216 second_desc = acpi_ns_get_secondary_object (object);
218 acpi_ut_delete_object_desc (second_desc);
227 /* Free any allocated memory (pointer within the object) found above */
230 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
232 ACPI_MEM_FREE (obj_pointer);
235 /* Now the object can be safely deleted */
237 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
238 object, acpi_ut_get_object_type_name (object)));
240 acpi_ut_delete_object_desc (object);
245 /*******************************************************************************
247 * FUNCTION: acpi_ut_delete_internal_object_list
249 * PARAMETERS: *obj_list - Pointer to the list to be deleted
253 * DESCRIPTION: This function deletes an internal object list, including both
254 * simple objects and package objects
256 ******************************************************************************/
259 acpi_ut_delete_internal_object_list (
260 union acpi_operand_object **obj_list)
262 union acpi_operand_object **internal_obj;
265 ACPI_FUNCTION_TRACE ("ut_delete_internal_object_list");
268 /* Walk the null-terminated internal list */
270 for (internal_obj = obj_list; *internal_obj; internal_obj++) {
271 acpi_ut_remove_reference (*internal_obj);
274 /* Free the combined parameter pointer list and object array */
276 ACPI_MEM_FREE (obj_list);
281 /*******************************************************************************
283 * FUNCTION: acpi_ut_update_ref_count
285 * PARAMETERS: *Object - Object whose ref count is to be updated
286 * Action - What to do
288 * RETURN: New ref count
290 * DESCRIPTION: Modify the ref count and return it.
292 ******************************************************************************/
295 acpi_ut_update_ref_count (
296 union acpi_operand_object *object,
303 ACPI_FUNCTION_NAME ("ut_update_ref_count");
310 count = object->common.reference_count;
314 * Perform the reference count action (increment, decrement, or force delete)
321 object->common.reference_count = new_count;
323 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, [Incremented]\n",
331 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
339 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, [Decremented]\n",
343 if (ACPI_GET_OBJECT_TYPE (object) == ACPI_TYPE_METHOD) {
344 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Method Obj %p Refs=%X, [Decremented]\n",
348 object->common.reference_count = new_count;
349 if (new_count == 0) {
350 acpi_ut_delete_internal_obj (object);
356 case REF_FORCE_DELETE:
358 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, Force delete! (Set to 0)\n",
362 object->common.reference_count = new_count;
363 acpi_ut_delete_internal_obj (object);
369 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown action (%X)\n", action));
374 * Sanity check the reference count, for debug purposes only.
375 * (A deleted object will have a huge reference count)
377 if (count > ACPI_MAX_REFERENCE_COUNT) {
379 ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
380 "**** Warning **** Large Reference Count (%X) in object %p\n\n",
388 /*******************************************************************************
390 * FUNCTION: acpi_ut_update_object_reference
392 * PARAMETERS: *Object - Increment ref count for this object
393 * and all sub-objects
394 * Action - Either REF_INCREMENT or REF_DECREMENT or
399 * DESCRIPTION: Increment the object reference count
401 * Object references are incremented when:
402 * 1) An object is attached to a Node (namespace object)
403 * 2) An object is copied (all subobjects must be incremented)
405 * Object references are decremented when:
406 * 1) An object is detached from an Node
408 ******************************************************************************/
411 acpi_ut_update_object_reference (
412 union acpi_operand_object *object,
417 union acpi_generic_state *state_list = NULL;
418 union acpi_generic_state *state;
419 union acpi_operand_object *tmp;
421 ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
424 /* Ignore a null object ptr */
427 return_ACPI_STATUS (AE_OK);
430 /* Make sure that this isn't a namespace handle */
432 if (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) {
433 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Object %p is NS handle\n", object));
434 return_ACPI_STATUS (AE_OK);
437 state = acpi_ut_create_update_state (object, action);
440 object = state->update.object;
441 action = state->update.value;
442 acpi_ut_delete_generic_state (state);
445 * All sub-objects must have their reference count incremented also.
446 * Different object types have different subobjects.
448 switch (ACPI_GET_OBJECT_TYPE (object)) {
449 case ACPI_TYPE_DEVICE:
451 tmp = object->device.system_notify;
452 if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
453 object->device.system_notify = NULL;
454 acpi_ut_update_ref_count (tmp, action);
456 tmp = object->device.device_notify;
457 if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
458 object->device.device_notify = NULL;
459 acpi_ut_update_ref_count (tmp, action);
464 case ACPI_TYPE_PACKAGE:
467 * We must update all the sub-objects of the package
468 * (Each of whom may have their own sub-objects, etc.
470 for (i = 0; i < object->package.count; i++) {
472 * Push each element onto the stack for later processing.
473 * Note: There can be null elements within the package,
474 * these are simply ignored
476 status = acpi_ut_create_update_state_and_push (
477 object->package.elements[i], action, &state_list);
478 if (ACPI_FAILURE (status)) {
482 tmp = object->package.elements[i];
483 if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
484 object->package.elements[i] = NULL;
489 case ACPI_TYPE_BUFFER_FIELD:
491 status = acpi_ut_create_update_state_and_push (
492 object->buffer_field.buffer_obj, action, &state_list);
493 if (ACPI_FAILURE (status)) {
497 tmp = object->buffer_field.buffer_obj;
498 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
499 object->buffer_field.buffer_obj = NULL;
503 case ACPI_TYPE_LOCAL_REGION_FIELD:
505 status = acpi_ut_create_update_state_and_push (
506 object->field.region_obj, action, &state_list);
507 if (ACPI_FAILURE (status)) {
511 tmp = object->field.region_obj;
512 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
513 object->field.region_obj = NULL;
517 case ACPI_TYPE_LOCAL_BANK_FIELD:
519 status = acpi_ut_create_update_state_and_push (
520 object->bank_field.bank_obj, action, &state_list);
521 if (ACPI_FAILURE (status)) {
525 tmp = object->bank_field.bank_obj;
526 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
527 object->bank_field.bank_obj = NULL;
529 status = acpi_ut_create_update_state_and_push (
530 object->bank_field.region_obj, action, &state_list);
531 if (ACPI_FAILURE (status)) {
535 tmp = object->bank_field.region_obj;
536 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
537 object->bank_field.region_obj = NULL;
541 case ACPI_TYPE_LOCAL_INDEX_FIELD:
543 status = acpi_ut_create_update_state_and_push (
544 object->index_field.index_obj, action, &state_list);
545 if (ACPI_FAILURE (status)) {
549 tmp = object->index_field.index_obj;
550 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
551 object->index_field.index_obj = NULL;
553 status = acpi_ut_create_update_state_and_push (
554 object->index_field.data_obj, action, &state_list);
555 if (ACPI_FAILURE (status)) {
559 tmp = object->index_field.data_obj;
560 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
561 object->index_field.data_obj = NULL;
565 case ACPI_TYPE_REGION:
566 case ACPI_TYPE_LOCAL_REFERENCE:
574 * Now we can update the count in the main object. This can only
575 * happen after we update the sub-objects in case this causes the
576 * main object to be deleted.
578 acpi_ut_update_ref_count (object, action);
580 /* Move on to the next object to be updated */
582 state = acpi_ut_pop_generic_state (&state_list);
585 return_ACPI_STATUS (AE_OK);
590 ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
591 acpi_format_exception (status)));
593 return_ACPI_STATUS (status);
597 /*******************************************************************************
599 * FUNCTION: acpi_ut_add_reference
601 * PARAMETERS: *Object - Object whose reference count is to be
606 * DESCRIPTION: Add one reference to an ACPI object
608 ******************************************************************************/
611 acpi_ut_add_reference (
612 union acpi_operand_object *object)
615 ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
618 /* Ensure that we have a valid object */
620 if (!acpi_ut_valid_internal_object (object)) {
624 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
625 "Obj %p Current Refs=%X [To Be Incremented]\n",
626 object, object->common.reference_count));
628 /* Increment the reference count */
630 (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
635 /*******************************************************************************
637 * FUNCTION: acpi_ut_remove_reference
639 * PARAMETERS: *Object - Object whose ref count will be decremented
643 * DESCRIPTION: Decrement the reference count of an ACPI internal object
645 ******************************************************************************/
648 acpi_ut_remove_reference (
649 union acpi_operand_object *object)
652 ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
656 * Allow a NULL pointer to be passed in, just ignore it. This saves
657 * each caller from having to check. Also, ignore NS nodes.
661 (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
665 /* Ensure that we have a valid object */
667 if (!acpi_ut_valid_internal_object (object)) {
671 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
672 "Obj %p Current Refs=%X [To Be Decremented]\n",
673 object, object->common.reference_count));
676 * Decrement the reference count, and only actually delete the object
677 * if the reference count becomes 0. (Must also decrement the ref count
678 * of all subobjects!)
680 (void) acpi_ut_update_object_reference (object, REF_DECREMENT);