1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2012, 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 #define __UTDELETE_C__
53 #define _COMPONENT ACPI_UTILITIES
54 ACPI_MODULE_NAME ("utdelete")
56 /* Local prototypes */
59 AcpiUtDeleteInternalObj (
60 ACPI_OPERAND_OBJECT *Object);
63 AcpiUtUpdateRefCount (
64 ACPI_OPERAND_OBJECT *Object,
68 /*******************************************************************************
70 * FUNCTION: AcpiUtDeleteInternalObj
72 * PARAMETERS: Object - Object to be deleted
76 * DESCRIPTION: Low level object deletion, after reference counts have been
77 * updated (All reference counts, including sub-objects!)
79 ******************************************************************************/
82 AcpiUtDeleteInternalObj (
83 ACPI_OPERAND_OBJECT *Object)
85 void *ObjPointer = NULL;
86 ACPI_OPERAND_OBJECT *HandlerDesc;
87 ACPI_OPERAND_OBJECT *SecondDesc;
88 ACPI_OPERAND_OBJECT *NextDesc;
89 ACPI_OPERAND_OBJECT **LastObjPtr;
92 ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
101 * Must delete or free any pointers within the object that are not
102 * actual ACPI objects (for example, a raw buffer pointer).
104 switch (Object->Common.Type)
106 case ACPI_TYPE_STRING:
108 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
109 Object, Object->String.Pointer));
111 /* Free the actual string buffer */
113 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
115 /* But only if it is NOT a pointer into an ACPI table */
117 ObjPointer = Object->String.Pointer;
122 case ACPI_TYPE_BUFFER:
124 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
125 Object, Object->Buffer.Pointer));
127 /* Free the actual buffer */
129 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
131 /* But only if it is NOT a pointer into an ACPI table */
133 ObjPointer = Object->Buffer.Pointer;
138 case ACPI_TYPE_PACKAGE:
140 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
141 Object->Package.Count));
144 * Elements of the package are not handled here, they are deleted
148 /* Free the (variable length) element pointer array */
150 ObjPointer = Object->Package.Elements;
155 * These objects have a possible list of notify handlers.
156 * Device object also may have a GPE block.
158 case ACPI_TYPE_DEVICE:
160 if (Object->Device.GpeBlock)
162 (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
165 /*lint -fallthrough */
167 case ACPI_TYPE_PROCESSOR:
168 case ACPI_TYPE_THERMAL:
170 /* Walk the address handler list for this object */
172 HandlerDesc = Object->CommonNotify.Handler;
175 NextDesc = HandlerDesc->AddressSpace.Next;
176 AcpiUtRemoveReference (HandlerDesc);
177 HandlerDesc = NextDesc;
182 case ACPI_TYPE_MUTEX:
184 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
185 "***** Mutex %p, OS Mutex %p\n",
186 Object, Object->Mutex.OsMutex));
188 if (Object == AcpiGbl_GlobalLockMutex)
190 /* Global Lock has extra semaphore */
192 (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
193 AcpiGbl_GlobalLockSemaphore = NULL;
195 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
196 AcpiGbl_GlobalLockMutex = NULL;
200 AcpiExUnlinkMutex (Object);
201 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
206 case ACPI_TYPE_EVENT:
208 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
209 "***** Event %p, OS Semaphore %p\n",
210 Object, Object->Event.OsSemaphore));
212 (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
213 Object->Event.OsSemaphore = NULL;
217 case ACPI_TYPE_METHOD:
219 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
220 "***** Method %p\n", Object));
222 /* Delete the method mutex if it exists */
224 if (Object->Method.Mutex)
226 AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
227 AcpiUtDeleteObjectDesc (Object->Method.Mutex);
228 Object->Method.Mutex = NULL;
233 case ACPI_TYPE_REGION:
235 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
236 "***** Region %p\n", Object));
239 * Update AddressRange list. However, only permanent regions
240 * are installed in this list. (Not created within a method)
242 if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
244 AcpiUtRemoveAddressRange (Object->Region.SpaceId,
245 Object->Region.Node);
248 SecondDesc = AcpiNsGetSecondaryObject (Object);
252 * Free the RegionContext if and only if the handler is one of the
253 * default handlers -- and therefore, we created the context object
254 * locally, it was not created by an external caller.
256 HandlerDesc = Object->Region.Handler;
259 NextDesc = HandlerDesc->AddressSpace.RegionList;
260 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
262 /* Remove the region object from the handler's list */
266 if (NextDesc == Object)
268 *LastObjPtr = NextDesc->Region.Next;
272 /* Walk the linked list of handler */
274 LastObjPtr = &NextDesc->Region.Next;
275 NextDesc = NextDesc->Region.Next;
278 if (HandlerDesc->AddressSpace.HandlerFlags &
279 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
281 /* Deactivate region and free region context */
283 if (HandlerDesc->AddressSpace.Setup)
285 (void) HandlerDesc->AddressSpace.Setup (Object,
286 ACPI_REGION_DEACTIVATE,
287 HandlerDesc->AddressSpace.Context,
288 &SecondDesc->Extra.RegionContext);
292 AcpiUtRemoveReference (HandlerDesc);
295 /* Now we can free the Extra object */
297 AcpiUtDeleteObjectDesc (SecondDesc);
302 case ACPI_TYPE_BUFFER_FIELD:
304 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
305 "***** Buffer Field %p\n", Object));
307 SecondDesc = AcpiNsGetSecondaryObject (Object);
310 AcpiUtDeleteObjectDesc (SecondDesc);
315 case ACPI_TYPE_LOCAL_BANK_FIELD:
317 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
318 "***** Bank Field %p\n", Object));
320 SecondDesc = AcpiNsGetSecondaryObject (Object);
323 AcpiUtDeleteObjectDesc (SecondDesc);
332 /* Free any allocated memory (pointer within the object) found above */
336 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
338 ACPI_FREE (ObjPointer);
341 /* Now the object can be safely deleted */
343 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
344 Object, AcpiUtGetObjectTypeName (Object)));
346 AcpiUtDeleteObjectDesc (Object);
351 /*******************************************************************************
353 * FUNCTION: AcpiUtDeleteInternalObjectList
355 * PARAMETERS: ObjList - Pointer to the list to be deleted
359 * DESCRIPTION: This function deletes an internal object list, including both
360 * simple objects and package objects
362 ******************************************************************************/
365 AcpiUtDeleteInternalObjectList (
366 ACPI_OPERAND_OBJECT **ObjList)
368 ACPI_OPERAND_OBJECT **InternalObj;
371 ACPI_FUNCTION_NAME (UtDeleteInternalObjectList);
374 /* Walk the null-terminated internal list */
376 for (InternalObj = ObjList; *InternalObj; InternalObj++)
378 AcpiUtRemoveReference (*InternalObj);
381 /* Free the combined parameter pointer list and object array */
388 /*******************************************************************************
390 * FUNCTION: AcpiUtUpdateRefCount
392 * PARAMETERS: Object - Object whose ref count is to be updated
393 * Action - What to do
395 * RETURN: New ref count
397 * DESCRIPTION: Modify the ref count and return it.
399 ******************************************************************************/
402 AcpiUtUpdateRefCount (
403 ACPI_OPERAND_OBJECT *Object,
410 ACPI_FUNCTION_NAME (UtUpdateRefCount);
418 Count = Object->Common.ReferenceCount;
422 * Perform the reference count action (increment, decrement, force delete)
429 Object->Common.ReferenceCount = NewCount;
431 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
432 "Obj %p Refs=%X, [Incremented]\n",
440 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
441 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
450 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
451 "Obj %p Refs=%X, [Decremented]\n",
455 if (Object->Common.Type == ACPI_TYPE_METHOD)
457 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
458 "Method Obj %p Refs=%X, [Decremented]\n", Object, NewCount));
461 Object->Common.ReferenceCount = NewCount;
464 AcpiUtDeleteInternalObj (Object);
468 case REF_FORCE_DELETE:
470 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
471 "Obj %p Refs=%X, Force delete! (Set to 0)\n", Object, Count));
474 Object->Common.ReferenceCount = NewCount;
475 AcpiUtDeleteInternalObj (Object);
480 ACPI_ERROR ((AE_INFO, "Unknown action (0x%X)", Action));
485 * Sanity check the reference count, for debug purposes only.
486 * (A deleted object will have a huge reference count)
488 if (Count > ACPI_MAX_REFERENCE_COUNT)
490 ACPI_WARNING ((AE_INFO,
491 "Large Reference Count (0x%X) in object %p", Count, Object));
496 /*******************************************************************************
498 * FUNCTION: AcpiUtUpdateObjectReference
500 * PARAMETERS: Object - Increment ref count for this object
501 * and all sub-objects
502 * Action - Either REF_INCREMENT or REF_DECREMENT or
507 * DESCRIPTION: Increment the object reference count
509 * Object references are incremented when:
510 * 1) An object is attached to a Node (namespace object)
511 * 2) An object is copied (all subobjects must be incremented)
513 * Object references are decremented when:
514 * 1) An object is detached from an Node
516 ******************************************************************************/
519 AcpiUtUpdateObjectReference (
520 ACPI_OPERAND_OBJECT *Object,
523 ACPI_STATUS Status = AE_OK;
524 ACPI_GENERIC_STATE *StateList = NULL;
525 ACPI_OPERAND_OBJECT *NextObject = NULL;
526 ACPI_OPERAND_OBJECT *PrevObject;
527 ACPI_GENERIC_STATE *State;
531 ACPI_FUNCTION_NAME (UtUpdateObjectReference);
536 /* Make sure that this isn't a namespace handle */
538 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
540 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
541 "Object %p is NS handle\n", Object));
546 * All sub-objects must have their reference count incremented also.
547 * Different object types have different subobjects.
549 switch (Object->Common.Type)
551 case ACPI_TYPE_DEVICE:
552 case ACPI_TYPE_PROCESSOR:
553 case ACPI_TYPE_POWER:
554 case ACPI_TYPE_THERMAL:
557 * Update the notify objects for these types (if present)
558 * Two lists, system and device notify handlers.
560 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
562 PrevObject = Object->CommonNotify.NotifyList[i];
565 NextObject = PrevObject->Notify.Next[i];
566 AcpiUtUpdateRefCount (PrevObject, Action);
567 PrevObject = NextObject;
572 case ACPI_TYPE_PACKAGE:
574 * We must update all the sub-objects of the package,
575 * each of whom may have their own sub-objects.
577 for (i = 0; i < Object->Package.Count; i++)
580 * Null package elements are legal and can be simply
583 NextObject = Object->Package.Elements[i];
589 switch (NextObject->Common.Type)
591 case ACPI_TYPE_INTEGER:
592 case ACPI_TYPE_STRING:
593 case ACPI_TYPE_BUFFER:
595 * For these very simple sub-objects, we can just
596 * update the reference count here and continue.
597 * Greatly increases performance of this operation.
599 AcpiUtUpdateRefCount (NextObject, Action);
604 * For complex sub-objects, push them onto the stack
605 * for later processing (this eliminates recursion.)
607 Status = AcpiUtCreateUpdateStateAndPush (
608 NextObject, Action, &StateList);
609 if (ACPI_FAILURE (Status))
619 case ACPI_TYPE_BUFFER_FIELD:
621 NextObject = Object->BufferField.BufferObj;
624 case ACPI_TYPE_LOCAL_REGION_FIELD:
626 NextObject = Object->Field.RegionObj;
629 case ACPI_TYPE_LOCAL_BANK_FIELD:
631 NextObject = Object->BankField.BankObj;
632 Status = AcpiUtCreateUpdateStateAndPush (
633 Object->BankField.RegionObj, Action, &StateList);
634 if (ACPI_FAILURE (Status))
640 case ACPI_TYPE_LOCAL_INDEX_FIELD:
642 NextObject = Object->IndexField.IndexObj;
643 Status = AcpiUtCreateUpdateStateAndPush (
644 Object->IndexField.DataObj, Action, &StateList);
645 if (ACPI_FAILURE (Status))
651 case ACPI_TYPE_LOCAL_REFERENCE:
653 * The target of an Index (a package, string, or buffer) or a named
654 * reference must track changes to the ref count of the index or
657 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
658 (Object->Reference.Class== ACPI_REFCLASS_NAME))
660 NextObject = Object->Reference.Object;
664 case ACPI_TYPE_REGION:
666 break; /* No subobjects for all other types */
670 * Now we can update the count in the main object. This can only
671 * happen after we update the sub-objects in case this causes the
672 * main object to be deleted.
674 AcpiUtUpdateRefCount (Object, Action);
677 /* Move on to the next object to be updated */
686 State = AcpiUtPopGenericState (&StateList);
687 Object = State->Update.Object;
688 AcpiUtDeleteGenericState (State);
697 ACPI_EXCEPTION ((AE_INFO, Status,
698 "Could not update object reference count"));
700 /* Free any stacked Update State objects */
704 State = AcpiUtPopGenericState (&StateList);
705 AcpiUtDeleteGenericState (State);
712 /*******************************************************************************
714 * FUNCTION: AcpiUtAddReference
716 * PARAMETERS: Object - Object whose reference count is to be
721 * DESCRIPTION: Add one reference to an ACPI object
723 ******************************************************************************/
727 ACPI_OPERAND_OBJECT *Object)
730 ACPI_FUNCTION_NAME (UtAddReference);
733 /* Ensure that we have a valid object */
735 if (!AcpiUtValidInternalObject (Object))
740 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
741 "Obj %p Current Refs=%X [To Be Incremented]\n",
742 Object, Object->Common.ReferenceCount));
744 /* Increment the reference count */
746 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
751 /*******************************************************************************
753 * FUNCTION: AcpiUtRemoveReference
755 * PARAMETERS: Object - Object whose ref count will be decremented
759 * DESCRIPTION: Decrement the reference count of an ACPI internal object
761 ******************************************************************************/
764 AcpiUtRemoveReference (
765 ACPI_OPERAND_OBJECT *Object)
768 ACPI_FUNCTION_NAME (UtRemoveReference);
772 * Allow a NULL pointer to be passed in, just ignore it. This saves
773 * each caller from having to check. Also, ignore NS nodes.
777 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
783 /* Ensure that we have a valid object */
785 if (!AcpiUtValidInternalObject (Object))
790 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
791 "Obj %p Current Refs=%X [To Be Decremented]\n",
792 Object, Object->Common.ReferenceCount));
795 * Decrement the reference count, and only actually delete the object
796 * if the reference count becomes 0. (Must also decrement the ref count
797 * of all subobjects!)
799 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);