1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2005, 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>
49 #include <acpi/amlcode.h>
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("utdelete")
54 /* Local prototypes */
57 acpi_ut_delete_internal_obj (
58 union acpi_operand_object
*object
);
61 acpi_ut_update_ref_count (
62 union acpi_operand_object
*object
,
66 /*******************************************************************************
68 * FUNCTION: acpi_ut_delete_internal_obj
70 * PARAMETERS: Object - Object to be deleted
74 * DESCRIPTION: Low level object deletion, after reference counts have been
75 * updated (All reference counts, including sub-objects!)
77 ******************************************************************************/
80 acpi_ut_delete_internal_obj (
81 union acpi_operand_object
*object
)
83 void *obj_pointer
= NULL
;
84 union acpi_operand_object
*handler_desc
;
85 union acpi_operand_object
*second_desc
;
86 union acpi_operand_object
*next_desc
;
89 ACPI_FUNCTION_TRACE_PTR ("ut_delete_internal_obj", object
);
97 * Must delete or free any pointers within the object that are not
98 * actual ACPI objects (for example, a raw buffer pointer).
100 switch (ACPI_GET_OBJECT_TYPE (object
)) {
101 case ACPI_TYPE_STRING
:
103 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "**** String %p, ptr %p\n",
104 object
, object
->string
.pointer
));
106 /* Free the actual string buffer */
108 if (!(object
->common
.flags
& AOPOBJ_STATIC_POINTER
)) {
109 /* But only if it is NOT a pointer into an ACPI table */
111 obj_pointer
= object
->string
.pointer
;
116 case ACPI_TYPE_BUFFER
:
118 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "**** Buffer %p, ptr %p\n",
119 object
, object
->buffer
.pointer
));
121 /* Free the actual buffer */
123 if (!(object
->common
.flags
& AOPOBJ_STATIC_POINTER
)) {
124 /* But only if it is NOT a pointer into an ACPI table */
126 obj_pointer
= object
->buffer
.pointer
;
131 case ACPI_TYPE_PACKAGE
:
133 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, " **** Package of count %X\n",
134 object
->package
.count
));
137 * Elements of the package are not handled here, they are deleted
141 /* Free the (variable length) element pointer array */
143 obj_pointer
= object
->package
.elements
;
147 case ACPI_TYPE_DEVICE
:
149 if (object
->device
.gpe_block
) {
150 (void) acpi_ev_delete_gpe_block (object
->device
.gpe_block
);
153 /* Walk the handler list for this device */
155 handler_desc
= object
->device
.handler
;
156 while (handler_desc
) {
157 next_desc
= handler_desc
->address_space
.next
;
158 acpi_ut_remove_reference (handler_desc
);
159 handler_desc
= next_desc
;
164 case ACPI_TYPE_MUTEX
:
166 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
167 "***** Mutex %p, Semaphore %p\n",
168 object
, object
->mutex
.semaphore
));
170 acpi_ex_unlink_mutex (object
);
171 (void) acpi_os_delete_semaphore (object
->mutex
.semaphore
);
175 case ACPI_TYPE_EVENT
:
177 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
178 "***** Event %p, Semaphore %p\n",
179 object
, object
->event
.semaphore
));
181 (void) acpi_os_delete_semaphore (object
->event
.semaphore
);
182 object
->event
.semaphore
= NULL
;
186 case ACPI_TYPE_METHOD
:
188 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
189 "***** Method %p\n", object
));
191 /* Delete the method semaphore if it exists */
193 if (object
->method
.semaphore
) {
194 (void) acpi_os_delete_semaphore (object
->method
.semaphore
);
195 object
->method
.semaphore
= NULL
;
200 case ACPI_TYPE_REGION
:
202 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
203 "***** Region %p\n", object
));
205 second_desc
= acpi_ns_get_secondary_object (object
);
208 * Free the region_context if and only if the handler is one of the
209 * default handlers -- and therefore, we created the context object
210 * locally, it was not created by an external caller.
212 handler_desc
= object
->region
.handler
;
214 if (handler_desc
->address_space
.hflags
& ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
) {
215 obj_pointer
= second_desc
->extra
.region_context
;
218 acpi_ut_remove_reference (handler_desc
);
221 /* Now we can free the Extra object */
223 acpi_ut_delete_object_desc (second_desc
);
228 case ACPI_TYPE_BUFFER_FIELD
:
230 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
231 "***** Buffer Field %p\n", object
));
233 second_desc
= acpi_ns_get_secondary_object (object
);
235 acpi_ut_delete_object_desc (second_desc
);
244 /* Free any allocated memory (pointer within the object) found above */
247 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Deleting Object Subptr %p\n",
249 ACPI_MEM_FREE (obj_pointer
);
252 /* Now the object can be safely deleted */
254 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Deleting Object %p [%s]\n",
255 object
, acpi_ut_get_object_type_name (object
)));
257 acpi_ut_delete_object_desc (object
);
262 /*******************************************************************************
264 * FUNCTION: acpi_ut_delete_internal_object_list
266 * PARAMETERS: obj_list - Pointer to the list to be deleted
270 * DESCRIPTION: This function deletes an internal object list, including both
271 * simple objects and package objects
273 ******************************************************************************/
276 acpi_ut_delete_internal_object_list (
277 union acpi_operand_object
**obj_list
)
279 union acpi_operand_object
**internal_obj
;
282 ACPI_FUNCTION_TRACE ("ut_delete_internal_object_list");
285 /* Walk the null-terminated internal list */
287 for (internal_obj
= obj_list
; *internal_obj
; internal_obj
++) {
288 acpi_ut_remove_reference (*internal_obj
);
291 /* Free the combined parameter pointer list and object array */
293 ACPI_MEM_FREE (obj_list
);
298 /*******************************************************************************
300 * FUNCTION: acpi_ut_update_ref_count
302 * PARAMETERS: Object - Object whose ref count is to be updated
303 * Action - What to do
305 * RETURN: New ref count
307 * DESCRIPTION: Modify the ref count and return it.
309 ******************************************************************************/
312 acpi_ut_update_ref_count (
313 union acpi_operand_object
*object
,
320 ACPI_FUNCTION_NAME ("ut_update_ref_count");
327 count
= object
->common
.reference_count
;
331 * Perform the reference count action
332 * (increment, decrement, or force delete)
339 object
->common
.reference_count
= new_count
;
341 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
342 "Obj %p Refs=%X, [Incremented]\n",
350 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
351 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
359 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
360 "Obj %p Refs=%X, [Decremented]\n",
364 if (ACPI_GET_OBJECT_TYPE (object
) == ACPI_TYPE_METHOD
) {
365 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
366 "Method Obj %p Refs=%X, [Decremented]\n",
370 object
->common
.reference_count
= new_count
;
371 if (new_count
== 0) {
372 acpi_ut_delete_internal_obj (object
);
378 case REF_FORCE_DELETE
:
380 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
381 "Obj %p Refs=%X, Force delete! (Set to 0)\n",
385 object
->common
.reference_count
= new_count
;
386 acpi_ut_delete_internal_obj (object
);
392 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
, "Unknown action (%X)\n", action
));
397 * Sanity check the reference count, for debug purposes only.
398 * (A deleted object will have a huge reference count)
400 if (count
> ACPI_MAX_REFERENCE_COUNT
) {
402 ACPI_DEBUG_PRINT ((ACPI_DB_WARN
,
403 "**** Warning **** Large Reference Count (%X) in object %p\n\n",
411 /*******************************************************************************
413 * FUNCTION: acpi_ut_update_object_reference
415 * PARAMETERS: Object - Increment ref count for this object
416 * and all sub-objects
417 * Action - Either REF_INCREMENT or REF_DECREMENT or
422 * DESCRIPTION: Increment the object reference count
424 * Object references are incremented when:
425 * 1) An object is attached to a Node (namespace object)
426 * 2) An object is copied (all subobjects must be incremented)
428 * Object references are decremented when:
429 * 1) An object is detached from an Node
431 ******************************************************************************/
434 acpi_ut_update_object_reference (
435 union acpi_operand_object
*object
,
438 acpi_status status
= AE_OK
;
439 union acpi_generic_state
*state_list
= NULL
;
440 union acpi_operand_object
*next_object
= NULL
;
441 union acpi_generic_state
*state
;
445 ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object
);
449 /* Make sure that this isn't a namespace handle */
451 if (ACPI_GET_DESCRIPTOR_TYPE (object
) == ACPI_DESC_TYPE_NAMED
) {
452 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
453 "Object %p is NS handle\n", object
));
454 return_ACPI_STATUS (AE_OK
);
458 * All sub-objects must have their reference count incremented also.
459 * Different object types have different subobjects.
461 switch (ACPI_GET_OBJECT_TYPE (object
)) {
462 case ACPI_TYPE_DEVICE
:
464 acpi_ut_update_ref_count (object
->device
.system_notify
, action
);
465 acpi_ut_update_ref_count (object
->device
.device_notify
, action
);
468 case ACPI_TYPE_PACKAGE
:
470 * We must update all the sub-objects of the package,
471 * each of whom may have their own sub-objects.
473 for (i
= 0; i
< object
->package
.count
; i
++) {
475 * Push each element onto the stack for later processing.
476 * Note: There can be null elements within the package,
477 * these are simply ignored
479 status
= acpi_ut_create_update_state_and_push (
480 object
->package
.elements
[i
], action
, &state_list
);
481 if (ACPI_FAILURE (status
)) {
487 case ACPI_TYPE_BUFFER_FIELD
:
489 next_object
= object
->buffer_field
.buffer_obj
;
492 case ACPI_TYPE_LOCAL_REGION_FIELD
:
494 next_object
= object
->field
.region_obj
;
497 case ACPI_TYPE_LOCAL_BANK_FIELD
:
499 next_object
= object
->bank_field
.bank_obj
;
500 status
= acpi_ut_create_update_state_and_push (
501 object
->bank_field
.region_obj
, action
, &state_list
);
502 if (ACPI_FAILURE (status
)) {
507 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
509 next_object
= object
->index_field
.index_obj
;
510 status
= acpi_ut_create_update_state_and_push (
511 object
->index_field
.data_obj
, action
, &state_list
);
512 if (ACPI_FAILURE (status
)) {
517 case ACPI_TYPE_LOCAL_REFERENCE
:
519 * The target of an Index (a package, string, or buffer) must track
520 * changes to the ref count of the index.
522 if (object
->reference
.opcode
== AML_INDEX_OP
) {
523 next_object
= object
->reference
.object
;
527 case ACPI_TYPE_REGION
:
529 break;/* No subobjects */
533 * Now we can update the count in the main object. This can only
534 * happen after we update the sub-objects in case this causes the
535 * main object to be deleted.
537 acpi_ut_update_ref_count (object
, action
);
540 /* Move on to the next object to be updated */
543 object
= next_object
;
546 else if (state_list
) {
547 state
= acpi_ut_pop_generic_state (&state_list
);
548 object
= state
->update
.object
;
549 acpi_ut_delete_generic_state (state
);
553 return_ACPI_STATUS (AE_OK
);
557 ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
558 acpi_format_exception (status
)));
560 return_ACPI_STATUS (status
);
564 /*******************************************************************************
566 * FUNCTION: acpi_ut_add_reference
568 * PARAMETERS: Object - Object whose reference count is to be
573 * DESCRIPTION: Add one reference to an ACPI object
575 ******************************************************************************/
578 acpi_ut_add_reference (
579 union acpi_operand_object
*object
)
582 ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object
);
585 /* Ensure that we have a valid object */
587 if (!acpi_ut_valid_internal_object (object
)) {
591 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
592 "Obj %p Current Refs=%X [To Be Incremented]\n",
593 object
, object
->common
.reference_count
));
595 /* Increment the reference count */
597 (void) acpi_ut_update_object_reference (object
, REF_INCREMENT
);
602 /*******************************************************************************
604 * FUNCTION: acpi_ut_remove_reference
606 * PARAMETERS: Object - Object whose ref count will be decremented
610 * DESCRIPTION: Decrement the reference count of an ACPI internal object
612 ******************************************************************************/
615 acpi_ut_remove_reference (
616 union acpi_operand_object
*object
)
619 ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object
);
623 * Allow a NULL pointer to be passed in, just ignore it. This saves
624 * each caller from having to check. Also, ignore NS nodes.
628 (ACPI_GET_DESCRIPTOR_TYPE (object
) == ACPI_DESC_TYPE_NAMED
)) {
632 /* Ensure that we have a valid object */
634 if (!acpi_ut_valid_internal_object (object
)) {
638 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
639 "Obj %p Current Refs=%X [To Be Decremented]\n",
640 object
, object
->common
.reference_count
));
643 * Decrement the reference count, and only actually delete the object
644 * if the reference count becomes 0. (Must also decrement the ref count
645 * of all subobjects!)
647 (void) acpi_ut_update_object_reference (object
, REF_DECREMENT
);