1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2008, Intel Corp.
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.
44 #include <acpi/acpi.h>
45 #include <acpi/acinterp.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/acevents.h>
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME("utdelete")
52 /* Local prototypes */
53 static void acpi_ut_delete_internal_obj(union acpi_operand_object
*object
);
56 acpi_ut_update_ref_count(union acpi_operand_object
*object
, u32 action
);
58 /*******************************************************************************
60 * FUNCTION: acpi_ut_delete_internal_obj
62 * PARAMETERS: Object - Object to be deleted
66 * DESCRIPTION: Low level object deletion, after reference counts have been
67 * updated (All reference counts, including sub-objects!)
69 ******************************************************************************/
71 static void acpi_ut_delete_internal_obj(union acpi_operand_object
*object
)
73 void *obj_pointer
= NULL
;
74 union acpi_operand_object
*handler_desc
;
75 union acpi_operand_object
*second_desc
;
76 union acpi_operand_object
*next_desc
;
78 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
,
92 "**** String %p, ptr %p\n", object
,
93 object
->string
.pointer
));
95 /* Free the actual string buffer */
97 if (!(object
->common
.flags
& AOPOBJ_STATIC_POINTER
)) {
99 /* But only if it is NOT a pointer into an ACPI table */
101 obj_pointer
= object
->string
.pointer
;
105 case ACPI_TYPE_BUFFER
:
107 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
108 "**** Buffer %p, ptr %p\n", object
,
109 object
->buffer
.pointer
));
111 /* Free the actual buffer */
113 if (!(object
->common
.flags
& AOPOBJ_STATIC_POINTER
)) {
115 /* But only if it is NOT a pointer into an ACPI table */
117 obj_pointer
= object
->buffer
.pointer
;
121 case ACPI_TYPE_PACKAGE
:
123 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
124 " **** Package of count %X\n",
125 object
->package
.count
));
128 * Elements of the package are not handled here, they are deleted
132 /* Free the (variable length) element pointer array */
134 obj_pointer
= object
->package
.elements
;
138 * These objects have a possible list of notify handlers.
139 * Device object also may have a GPE block.
141 case ACPI_TYPE_DEVICE
:
143 if (object
->device
.gpe_block
) {
144 (void)acpi_ev_delete_gpe_block(object
->device
.
148 /*lint -fallthrough */
150 case ACPI_TYPE_PROCESSOR
:
151 case ACPI_TYPE_THERMAL
:
153 /* Walk the notify handler list for this object */
155 handler_desc
= object
->common_notify
.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
;
163 case ACPI_TYPE_MUTEX
:
165 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
166 "***** Mutex %p, OS Mutex %p\n",
167 object
, object
->mutex
.os_mutex
));
169 if (object
== acpi_gbl_global_lock_mutex
) {
171 /* Global Lock has extra semaphore */
174 acpi_os_delete_semaphore
175 (acpi_gbl_global_lock_semaphore
);
176 acpi_gbl_global_lock_semaphore
= NULL
;
178 acpi_os_delete_mutex(object
->mutex
.os_mutex
);
179 acpi_gbl_global_lock_mutex
= NULL
;
181 acpi_ex_unlink_mutex(object
);
182 acpi_os_delete_mutex(object
->mutex
.os_mutex
);
186 case ACPI_TYPE_EVENT
:
188 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
189 "***** Event %p, OS Semaphore %p\n",
190 object
, object
->event
.os_semaphore
));
192 (void)acpi_os_delete_semaphore(object
->event
.os_semaphore
);
193 object
->event
.os_semaphore
= NULL
;
196 case ACPI_TYPE_METHOD
:
198 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
199 "***** Method %p\n", object
));
201 /* Delete the method mutex if it exists */
203 if (object
->method
.mutex
) {
204 acpi_os_delete_mutex(object
->method
.mutex
->mutex
.
206 acpi_ut_delete_object_desc(object
->method
.mutex
);
207 object
->method
.mutex
= NULL
;
211 case ACPI_TYPE_REGION
:
213 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
214 "***** Region %p\n", object
));
216 second_desc
= acpi_ns_get_secondary_object(object
);
219 * Free the region_context if and only if the handler is one of the
220 * default handlers -- and therefore, we created the context object
221 * locally, it was not created by an external caller.
223 handler_desc
= object
->region
.handler
;
225 if (handler_desc
->address_space
.handler_flags
&
226 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
) {
228 /* Deactivate region and free region context */
230 if (handler_desc
->address_space
.setup
) {
232 address_space
.setup(object
,
233 ACPI_REGION_DEACTIVATE
,
243 acpi_ut_remove_reference(handler_desc
);
246 /* Now we can free the Extra object */
248 acpi_ut_delete_object_desc(second_desc
);
252 case ACPI_TYPE_BUFFER_FIELD
:
254 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
255 "***** Buffer Field %p\n", object
));
257 second_desc
= acpi_ns_get_secondary_object(object
);
259 acpi_ut_delete_object_desc(second_desc
);
263 case ACPI_TYPE_LOCAL_BANK_FIELD
:
265 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
266 "***** Bank Field %p\n", object
));
268 second_desc
= acpi_ns_get_secondary_object(object
);
270 acpi_ut_delete_object_desc(second_desc
);
278 /* Free any allocated memory (pointer within the object) found above */
281 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
282 "Deleting Object Subptr %p\n", obj_pointer
));
283 ACPI_FREE(obj_pointer
);
286 /* Now the object can be safely deleted */
288 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "Deleting Object %p [%s]\n",
289 object
, acpi_ut_get_object_type_name(object
)));
291 acpi_ut_delete_object_desc(object
);
295 /*******************************************************************************
297 * FUNCTION: acpi_ut_delete_internal_object_list
299 * PARAMETERS: obj_list - Pointer to the list to be deleted
303 * DESCRIPTION: This function deletes an internal object list, including both
304 * simple objects and package objects
306 ******************************************************************************/
308 void acpi_ut_delete_internal_object_list(union acpi_operand_object
**obj_list
)
310 union acpi_operand_object
**internal_obj
;
312 ACPI_FUNCTION_TRACE(ut_delete_internal_object_list
);
314 /* Walk the null-terminated internal list */
316 for (internal_obj
= obj_list
; *internal_obj
; internal_obj
++) {
317 acpi_ut_remove_reference(*internal_obj
);
320 /* Free the combined parameter pointer list and object array */
326 /*******************************************************************************
328 * FUNCTION: acpi_ut_update_ref_count
330 * PARAMETERS: Object - Object whose ref count is to be updated
331 * Action - What to do
333 * RETURN: New ref count
335 * DESCRIPTION: Modify the ref count and return it.
337 ******************************************************************************/
340 acpi_ut_update_ref_count(union acpi_operand_object
*object
, u32 action
)
345 ACPI_FUNCTION_NAME(ut_update_ref_count
);
351 count
= object
->common
.reference_count
;
355 * Perform the reference count action (increment, decrement, force delete)
361 object
->common
.reference_count
= new_count
;
363 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
364 "Obj %p Refs=%X, [Incremented]\n",
371 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
372 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
379 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
380 "Obj %p Refs=%X, [Decremented]\n",
384 if (ACPI_GET_OBJECT_TYPE(object
) == ACPI_TYPE_METHOD
) {
385 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
386 "Method Obj %p Refs=%X, [Decremented]\n",
390 object
->common
.reference_count
= new_count
;
391 if (new_count
== 0) {
392 acpi_ut_delete_internal_obj(object
);
396 case REF_FORCE_DELETE
:
398 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
399 "Obj %p Refs=%X, Force delete! (Set to 0)\n",
403 object
->common
.reference_count
= new_count
;
404 acpi_ut_delete_internal_obj(object
);
409 ACPI_ERROR((AE_INFO
, "Unknown action (%X)", action
));
414 * Sanity check the reference count, for debug purposes only.
415 * (A deleted object will have a huge reference count)
417 if (count
> ACPI_MAX_REFERENCE_COUNT
) {
418 ACPI_WARNING((AE_INFO
,
419 "Large Reference Count (%X) in object %p", count
,
424 /*******************************************************************************
426 * FUNCTION: acpi_ut_update_object_reference
428 * PARAMETERS: Object - Increment ref count for this object
429 * and all sub-objects
430 * Action - Either REF_INCREMENT or REF_DECREMENT or
435 * DESCRIPTION: Increment the object reference count
437 * Object references are incremented when:
438 * 1) An object is attached to a Node (namespace object)
439 * 2) An object is copied (all subobjects must be incremented)
441 * Object references are decremented when:
442 * 1) An object is detached from an Node
444 ******************************************************************************/
447 acpi_ut_update_object_reference(union acpi_operand_object
*object
, u16 action
)
449 acpi_status status
= AE_OK
;
450 union acpi_generic_state
*state_list
= NULL
;
451 union acpi_operand_object
*next_object
= NULL
;
452 union acpi_generic_state
*state
;
455 ACPI_FUNCTION_TRACE_PTR(ut_update_object_reference
, object
);
459 /* Make sure that this isn't a namespace handle */
461 if (ACPI_GET_DESCRIPTOR_TYPE(object
) == ACPI_DESC_TYPE_NAMED
) {
462 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
463 "Object %p is NS handle\n", object
));
464 return_ACPI_STATUS(AE_OK
);
468 * All sub-objects must have their reference count incremented also.
469 * Different object types have different subobjects.
471 switch (ACPI_GET_OBJECT_TYPE(object
)) {
472 case ACPI_TYPE_DEVICE
:
473 case ACPI_TYPE_PROCESSOR
:
474 case ACPI_TYPE_POWER
:
475 case ACPI_TYPE_THERMAL
:
477 /* Update the notify objects for these types (if present) */
479 acpi_ut_update_ref_count(object
->common_notify
.
480 system_notify
, action
);
481 acpi_ut_update_ref_count(object
->common_notify
.
482 device_notify
, action
);
485 case ACPI_TYPE_PACKAGE
:
487 * We must update all the sub-objects of the package,
488 * each of whom may have their own sub-objects.
490 for (i
= 0; i
< object
->package
.count
; i
++) {
492 * Push each element onto the stack for later processing.
493 * Note: There can be null elements within the package,
494 * these are simply ignored
497 acpi_ut_create_update_state_and_push
498 (object
->package
.elements
[i
], action
,
500 if (ACPI_FAILURE(status
)) {
506 case ACPI_TYPE_BUFFER_FIELD
:
508 next_object
= object
->buffer_field
.buffer_obj
;
511 case ACPI_TYPE_LOCAL_REGION_FIELD
:
513 next_object
= object
->field
.region_obj
;
516 case ACPI_TYPE_LOCAL_BANK_FIELD
:
518 next_object
= object
->bank_field
.bank_obj
;
520 acpi_ut_create_update_state_and_push(object
->
525 if (ACPI_FAILURE(status
)) {
530 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
532 next_object
= object
->index_field
.index_obj
;
534 acpi_ut_create_update_state_and_push(object
->
539 if (ACPI_FAILURE(status
)) {
544 case ACPI_TYPE_LOCAL_REFERENCE
:
546 * The target of an Index (a package, string, or buffer) or a named
547 * reference must track changes to the ref count of the index or
550 if ((object
->reference
.class == ACPI_REFCLASS_INDEX
) ||
551 (object
->reference
.class == ACPI_REFCLASS_NAME
)) {
552 next_object
= object
->reference
.object
;
556 case ACPI_TYPE_REGION
:
558 break; /* No subobjects for all other types */
562 * Now we can update the count in the main object. This can only
563 * happen after we update the sub-objects in case this causes the
564 * main object to be deleted.
566 acpi_ut_update_ref_count(object
, action
);
569 /* Move on to the next object to be updated */
572 object
= next_object
;
574 } else if (state_list
) {
575 state
= acpi_ut_pop_generic_state(&state_list
);
576 object
= state
->update
.object
;
577 acpi_ut_delete_generic_state(state
);
581 return_ACPI_STATUS(AE_OK
);
585 ACPI_EXCEPTION((AE_INFO
, status
,
586 "Could not update object reference count"));
588 /* Free any stacked Update State objects */
591 state
= acpi_ut_pop_generic_state(&state_list
);
592 acpi_ut_delete_generic_state(state
);
595 return_ACPI_STATUS(status
);
598 /*******************************************************************************
600 * FUNCTION: acpi_ut_add_reference
602 * PARAMETERS: Object - Object whose reference count is to be
607 * DESCRIPTION: Add one reference to an ACPI object
609 ******************************************************************************/
611 void acpi_ut_add_reference(union acpi_operand_object
*object
)
614 ACPI_FUNCTION_TRACE_PTR(ut_add_reference
, object
);
616 /* Ensure that we have a valid object */
618 if (!acpi_ut_valid_internal_object(object
)) {
622 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
623 "Obj %p Current Refs=%X [To Be Incremented]\n",
624 object
, object
->common
.reference_count
));
626 /* Increment the reference count */
628 (void)acpi_ut_update_object_reference(object
, REF_INCREMENT
);
632 /*******************************************************************************
634 * FUNCTION: acpi_ut_remove_reference
636 * PARAMETERS: Object - Object whose ref count will be decremented
640 * DESCRIPTION: Decrement the reference count of an ACPI internal object
642 ******************************************************************************/
644 void acpi_ut_remove_reference(union acpi_operand_object
*object
)
647 ACPI_FUNCTION_TRACE_PTR(ut_remove_reference
, object
);
650 * Allow a NULL pointer to be passed in, just ignore it. This saves
651 * each caller from having to check. Also, ignore NS nodes.
655 (ACPI_GET_DESCRIPTOR_TYPE(object
) == ACPI_DESC_TYPE_NAMED
)) {
659 /* Ensure that we have a valid object */
661 if (!acpi_ut_valid_internal_object(object
)) {
665 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
,
666 "Obj %p Current Refs=%X [To Be Decremented]\n",
667 object
, object
->common
.reference_count
));
670 * Decrement the reference count, and only actually delete the object
671 * if the reference count becomes 0. (Must also decrement the ref count
672 * of all subobjects!)
674 (void)acpi_ut_update_object_reference(object
, REF_DECREMENT
);