1*3446Smrj /*******************************************************************************
2*3446Smrj  *
3*3446Smrj  * Module Name: utdelete - object deletion and reference count utilities
4*3446Smrj  *              $Revision: 1.121 $
5*3446Smrj  *
6*3446Smrj  ******************************************************************************/
7*3446Smrj 
8*3446Smrj /******************************************************************************
9*3446Smrj  *
10*3446Smrj  * 1. Copyright Notice
11*3446Smrj  *
12*3446Smrj  * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13*3446Smrj  * All rights reserved.
14*3446Smrj  *
15*3446Smrj  * 2. License
16*3446Smrj  *
17*3446Smrj  * 2.1. This is your license from Intel Corp. under its intellectual property
18*3446Smrj  * rights.  You may have additional license terms from the party that provided
19*3446Smrj  * you this software, covering your right to use that party's intellectual
20*3446Smrj  * property rights.
21*3446Smrj  *
22*3446Smrj  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23*3446Smrj  * copy of the source code appearing in this file ("Covered Code") an
24*3446Smrj  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25*3446Smrj  * base code distributed originally by Intel ("Original Intel Code") to copy,
26*3446Smrj  * make derivatives, distribute, use and display any portion of the Covered
27*3446Smrj  * Code in any form, with the right to sublicense such rights; and
28*3446Smrj  *
29*3446Smrj  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30*3446Smrj  * license (with the right to sublicense), under only those claims of Intel
31*3446Smrj  * patents that are infringed by the Original Intel Code, to make, use, sell,
32*3446Smrj  * offer to sell, and import the Covered Code and derivative works thereof
33*3446Smrj  * solely to the minimum extent necessary to exercise the above copyright
34*3446Smrj  * license, and in no event shall the patent license extend to any additions
35*3446Smrj  * to or modifications of the Original Intel Code.  No other license or right
36*3446Smrj  * is granted directly or by implication, estoppel or otherwise;
37*3446Smrj  *
38*3446Smrj  * The above copyright and patent license is granted only if the following
39*3446Smrj  * conditions are met:
40*3446Smrj  *
41*3446Smrj  * 3. Conditions
42*3446Smrj  *
43*3446Smrj  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44*3446Smrj  * Redistribution of source code of any substantial portion of the Covered
45*3446Smrj  * Code or modification with rights to further distribute source must include
46*3446Smrj  * the above Copyright Notice, the above License, this list of Conditions,
47*3446Smrj  * and the following Disclaimer and Export Compliance provision.  In addition,
48*3446Smrj  * Licensee must cause all Covered Code to which Licensee contributes to
49*3446Smrj  * contain a file documenting the changes Licensee made to create that Covered
50*3446Smrj  * Code and the date of any change.  Licensee must include in that file the
51*3446Smrj  * documentation of any changes made by any predecessor Licensee.  Licensee
52*3446Smrj  * must include a prominent statement that the modification is derived,
53*3446Smrj  * directly or indirectly, from Original Intel Code.
54*3446Smrj  *
55*3446Smrj  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56*3446Smrj  * Redistribution of source code of any substantial portion of the Covered
57*3446Smrj  * Code or modification without rights to further distribute source must
58*3446Smrj  * include the following Disclaimer and Export Compliance provision in the
59*3446Smrj  * documentation and/or other materials provided with distribution.  In
60*3446Smrj  * addition, Licensee may not authorize further sublicense of source of any
61*3446Smrj  * portion of the Covered Code, and must include terms to the effect that the
62*3446Smrj  * license from Licensee to its licensee is limited to the intellectual
63*3446Smrj  * property embodied in the software Licensee provides to its licensee, and
64*3446Smrj  * not to intellectual property embodied in modifications its licensee may
65*3446Smrj  * make.
66*3446Smrj  *
67*3446Smrj  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68*3446Smrj  * substantial portion of the Covered Code or modification must reproduce the
69*3446Smrj  * above Copyright Notice, and the following Disclaimer and Export Compliance
70*3446Smrj  * provision in the documentation and/or other materials provided with the
71*3446Smrj  * distribution.
72*3446Smrj  *
73*3446Smrj  * 3.4. Intel retains all right, title, and interest in and to the Original
74*3446Smrj  * Intel Code.
75*3446Smrj  *
76*3446Smrj  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77*3446Smrj  * Intel shall be used in advertising or otherwise to promote the sale, use or
78*3446Smrj  * other dealings in products derived from or relating to the Covered Code
79*3446Smrj  * without prior written authorization from Intel.
80*3446Smrj  *
81*3446Smrj  * 4. Disclaimer and Export Compliance
82*3446Smrj  *
83*3446Smrj  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84*3446Smrj  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85*3446Smrj  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86*3446Smrj  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87*3446Smrj  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88*3446Smrj  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89*3446Smrj  * PARTICULAR PURPOSE.
90*3446Smrj  *
91*3446Smrj  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92*3446Smrj  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93*3446Smrj  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94*3446Smrj  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95*3446Smrj  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96*3446Smrj  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97*3446Smrj  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98*3446Smrj  * LIMITED REMEDY.
99*3446Smrj  *
100*3446Smrj  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101*3446Smrj  * software or system incorporating such software without first obtaining any
102*3446Smrj  * required license or other approval from the U. S. Department of Commerce or
103*3446Smrj  * any other agency or department of the United States Government.  In the
104*3446Smrj  * event Licensee exports any such software from the United States or
105*3446Smrj  * re-exports any such software from a foreign destination, Licensee shall
106*3446Smrj  * ensure that the distribution and export/re-export of the software is in
107*3446Smrj  * compliance with all laws, regulations, orders, or other restrictions of the
108*3446Smrj  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109*3446Smrj  * any of its subsidiaries will export/re-export any technical data, process,
110*3446Smrj  * software, or service, directly or indirectly, to any country for which the
111*3446Smrj  * United States government or any agency thereof requires an export license,
112*3446Smrj  * other governmental approval, or letter of assurance, without first obtaining
113*3446Smrj  * such license, approval or letter.
114*3446Smrj  *
115*3446Smrj  *****************************************************************************/
116*3446Smrj 
117*3446Smrj #define __UTDELETE_C__
118*3446Smrj 
119*3446Smrj #include "acpi.h"
120*3446Smrj #include "acinterp.h"
121*3446Smrj #include "acnamesp.h"
122*3446Smrj #include "acevents.h"
123*3446Smrj #include "amlcode.h"
124*3446Smrj 
125*3446Smrj #define _COMPONENT          ACPI_UTILITIES
126*3446Smrj         ACPI_MODULE_NAME    ("utdelete")
127*3446Smrj 
128*3446Smrj /* Local prototypes */
129*3446Smrj 
130*3446Smrj static void
131*3446Smrj AcpiUtDeleteInternalObj (
132*3446Smrj     ACPI_OPERAND_OBJECT     *Object);
133*3446Smrj 
134*3446Smrj static void
135*3446Smrj AcpiUtUpdateRefCount (
136*3446Smrj     ACPI_OPERAND_OBJECT     *Object,
137*3446Smrj     UINT32                  Action);
138*3446Smrj 
139*3446Smrj 
140*3446Smrj /*******************************************************************************
141*3446Smrj  *
142*3446Smrj  * FUNCTION:    AcpiUtDeleteInternalObj
143*3446Smrj  *
144*3446Smrj  * PARAMETERS:  Object         - Object to be deleted
145*3446Smrj  *
146*3446Smrj  * RETURN:      None
147*3446Smrj  *
148*3446Smrj  * DESCRIPTION: Low level object deletion, after reference counts have been
149*3446Smrj  *              updated (All reference counts, including sub-objects!)
150*3446Smrj  *
151*3446Smrj  ******************************************************************************/
152*3446Smrj 
153*3446Smrj static void
154*3446Smrj AcpiUtDeleteInternalObj (
155*3446Smrj     ACPI_OPERAND_OBJECT     *Object)
156*3446Smrj {
157*3446Smrj     void                    *ObjPointer = NULL;
158*3446Smrj     ACPI_OPERAND_OBJECT     *HandlerDesc;
159*3446Smrj     ACPI_OPERAND_OBJECT     *SecondDesc;
160*3446Smrj     ACPI_OPERAND_OBJECT     *NextDesc;
161*3446Smrj 
162*3446Smrj 
163*3446Smrj     ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
164*3446Smrj 
165*3446Smrj 
166*3446Smrj     if (!Object)
167*3446Smrj     {
168*3446Smrj         return_VOID;
169*3446Smrj     }
170*3446Smrj 
171*3446Smrj     /*
172*3446Smrj      * Must delete or free any pointers within the object that are not
173*3446Smrj      * actual ACPI objects (for example, a raw buffer pointer).
174*3446Smrj      */
175*3446Smrj     switch (ACPI_GET_OBJECT_TYPE (Object))
176*3446Smrj     {
177*3446Smrj     case ACPI_TYPE_STRING:
178*3446Smrj 
179*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
180*3446Smrj             Object, Object->String.Pointer));
181*3446Smrj 
182*3446Smrj         /* Free the actual string buffer */
183*3446Smrj 
184*3446Smrj         if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
185*3446Smrj         {
186*3446Smrj             /* But only if it is NOT a pointer into an ACPI table */
187*3446Smrj 
188*3446Smrj             ObjPointer = Object->String.Pointer;
189*3446Smrj         }
190*3446Smrj         break;
191*3446Smrj 
192*3446Smrj 
193*3446Smrj     case ACPI_TYPE_BUFFER:
194*3446Smrj 
195*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
196*3446Smrj             Object, Object->Buffer.Pointer));
197*3446Smrj 
198*3446Smrj         /* Free the actual buffer */
199*3446Smrj 
200*3446Smrj         if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
201*3446Smrj         {
202*3446Smrj             /* But only if it is NOT a pointer into an ACPI table */
203*3446Smrj 
204*3446Smrj             ObjPointer = Object->Buffer.Pointer;
205*3446Smrj         }
206*3446Smrj         break;
207*3446Smrj 
208*3446Smrj 
209*3446Smrj     case ACPI_TYPE_PACKAGE:
210*3446Smrj 
211*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
212*3446Smrj             Object->Package.Count));
213*3446Smrj 
214*3446Smrj         /*
215*3446Smrj          * Elements of the package are not handled here, they are deleted
216*3446Smrj          * separately
217*3446Smrj          */
218*3446Smrj 
219*3446Smrj         /* Free the (variable length) element pointer array */
220*3446Smrj 
221*3446Smrj         ObjPointer = Object->Package.Elements;
222*3446Smrj         break;
223*3446Smrj 
224*3446Smrj 
225*3446Smrj     case ACPI_TYPE_DEVICE:
226*3446Smrj 
227*3446Smrj         if (Object->Device.GpeBlock)
228*3446Smrj         {
229*3446Smrj             (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
230*3446Smrj         }
231*3446Smrj 
232*3446Smrj         /* Walk the handler list for this device */
233*3446Smrj 
234*3446Smrj         HandlerDesc = Object->Device.Handler;
235*3446Smrj         while (HandlerDesc)
236*3446Smrj         {
237*3446Smrj             NextDesc = HandlerDesc->AddressSpace.Next;
238*3446Smrj             AcpiUtRemoveReference (HandlerDesc);
239*3446Smrj             HandlerDesc = NextDesc;
240*3446Smrj         }
241*3446Smrj         break;
242*3446Smrj 
243*3446Smrj 
244*3446Smrj     case ACPI_TYPE_MUTEX:
245*3446Smrj 
246*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
247*3446Smrj             "***** Mutex %p, OS Mutex %p\n",
248*3446Smrj             Object, Object->Mutex.OsMutex));
249*3446Smrj 
250*3446Smrj         if (Object->Mutex.OsMutex == AcpiGbl_GlobalLockMutex)
251*3446Smrj         {
252*3446Smrj             /* Global Lock has extra semaphore */
253*3446Smrj 
254*3446Smrj             (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
255*3446Smrj             AcpiGbl_GlobalLockSemaphore = NULL;
256*3446Smrj 
257*3446Smrj             AcpiOsDeleteMutex (Object->Mutex.OsMutex);
258*3446Smrj             AcpiGbl_GlobalLockMutex = NULL;
259*3446Smrj         }
260*3446Smrj         else
261*3446Smrj         {
262*3446Smrj             AcpiExUnlinkMutex (Object);
263*3446Smrj             AcpiOsDeleteMutex (Object->Mutex.OsMutex);
264*3446Smrj         }
265*3446Smrj         break;
266*3446Smrj 
267*3446Smrj 
268*3446Smrj     case ACPI_TYPE_EVENT:
269*3446Smrj 
270*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
271*3446Smrj             "***** Event %p, OS Semaphore %p\n",
272*3446Smrj             Object, Object->Event.OsSemaphore));
273*3446Smrj 
274*3446Smrj         (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
275*3446Smrj         Object->Event.OsSemaphore = NULL;
276*3446Smrj         break;
277*3446Smrj 
278*3446Smrj 
279*3446Smrj     case ACPI_TYPE_METHOD:
280*3446Smrj 
281*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
282*3446Smrj             "***** Method %p\n", Object));
283*3446Smrj 
284*3446Smrj         /* Delete the method mutex if it exists */
285*3446Smrj 
286*3446Smrj         if (Object->Method.Mutex)
287*3446Smrj         {
288*3446Smrj             AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
289*3446Smrj             AcpiUtDeleteObjectDesc (Object->Method.Mutex);
290*3446Smrj             Object->Method.Mutex = NULL;
291*3446Smrj         }
292*3446Smrj         break;
293*3446Smrj 
294*3446Smrj 
295*3446Smrj     case ACPI_TYPE_REGION:
296*3446Smrj 
297*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
298*3446Smrj             "***** Region %p\n", Object));
299*3446Smrj 
300*3446Smrj         SecondDesc = AcpiNsGetSecondaryObject (Object);
301*3446Smrj         if (SecondDesc)
302*3446Smrj         {
303*3446Smrj             /*
304*3446Smrj              * Free the RegionContext if and only if the handler is one of the
305*3446Smrj              * default handlers -- and therefore, we created the context object
306*3446Smrj              * locally, it was not created by an external caller.
307*3446Smrj              */
308*3446Smrj             HandlerDesc = Object->Region.Handler;
309*3446Smrj             if (HandlerDesc)
310*3446Smrj             {
311*3446Smrj                 if (HandlerDesc->AddressSpace.HandlerFlags &
312*3446Smrj                     ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
313*3446Smrj                 {
314*3446Smrj                     /* Deactivate region and free region context */
315*3446Smrj 
316*3446Smrj                     if (HandlerDesc->AddressSpace.Setup)
317*3446Smrj                     {
318*3446Smrj                         (void) HandlerDesc->AddressSpace.Setup (Object,
319*3446Smrj                             ACPI_REGION_DEACTIVATE,
320*3446Smrj                             HandlerDesc->AddressSpace.Context,
321*3446Smrj                             &SecondDesc->Extra.RegionContext);
322*3446Smrj                     }
323*3446Smrj                 }
324*3446Smrj 
325*3446Smrj                 AcpiUtRemoveReference (HandlerDesc);
326*3446Smrj             }
327*3446Smrj 
328*3446Smrj             /* Now we can free the Extra object */
329*3446Smrj 
330*3446Smrj             AcpiUtDeleteObjectDesc (SecondDesc);
331*3446Smrj         }
332*3446Smrj         break;
333*3446Smrj 
334*3446Smrj 
335*3446Smrj     case ACPI_TYPE_BUFFER_FIELD:
336*3446Smrj 
337*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
338*3446Smrj             "***** Buffer Field %p\n", Object));
339*3446Smrj 
340*3446Smrj         SecondDesc = AcpiNsGetSecondaryObject (Object);
341*3446Smrj         if (SecondDesc)
342*3446Smrj         {
343*3446Smrj             AcpiUtDeleteObjectDesc (SecondDesc);
344*3446Smrj         }
345*3446Smrj         break;
346*3446Smrj 
347*3446Smrj 
348*3446Smrj     default:
349*3446Smrj         break;
350*3446Smrj     }
351*3446Smrj 
352*3446Smrj     /* Free any allocated memory (pointer within the object) found above */
353*3446Smrj 
354*3446Smrj     if (ObjPointer)
355*3446Smrj     {
356*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
357*3446Smrj             ObjPointer));
358*3446Smrj         ACPI_FREE (ObjPointer);
359*3446Smrj     }
360*3446Smrj 
361*3446Smrj     /* Now the object can be safely deleted */
362*3446Smrj 
363*3446Smrj     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
364*3446Smrj         Object, AcpiUtGetObjectTypeName (Object)));
365*3446Smrj 
366*3446Smrj     AcpiUtDeleteObjectDesc (Object);
367*3446Smrj     return_VOID;
368*3446Smrj }
369*3446Smrj 
370*3446Smrj 
371*3446Smrj /*******************************************************************************
372*3446Smrj  *
373*3446Smrj  * FUNCTION:    AcpiUtDeleteInternalObjectList
374*3446Smrj  *
375*3446Smrj  * PARAMETERS:  ObjList         - Pointer to the list to be deleted
376*3446Smrj  *
377*3446Smrj  * RETURN:      None
378*3446Smrj  *
379*3446Smrj  * DESCRIPTION: This function deletes an internal object list, including both
380*3446Smrj  *              simple objects and package objects
381*3446Smrj  *
382*3446Smrj  ******************************************************************************/
383*3446Smrj 
384*3446Smrj void
385*3446Smrj AcpiUtDeleteInternalObjectList (
386*3446Smrj     ACPI_OPERAND_OBJECT     **ObjList)
387*3446Smrj {
388*3446Smrj     ACPI_OPERAND_OBJECT     **InternalObj;
389*3446Smrj 
390*3446Smrj 
391*3446Smrj     ACPI_FUNCTION_TRACE (UtDeleteInternalObjectList);
392*3446Smrj 
393*3446Smrj 
394*3446Smrj     /* Walk the null-terminated internal list */
395*3446Smrj 
396*3446Smrj     for (InternalObj = ObjList; *InternalObj; InternalObj++)
397*3446Smrj     {
398*3446Smrj         AcpiUtRemoveReference (*InternalObj);
399*3446Smrj     }
400*3446Smrj 
401*3446Smrj     /* Free the combined parameter pointer list and object array */
402*3446Smrj 
403*3446Smrj     ACPI_FREE (ObjList);
404*3446Smrj     return_VOID;
405*3446Smrj }
406*3446Smrj 
407*3446Smrj 
408*3446Smrj /*******************************************************************************
409*3446Smrj  *
410*3446Smrj  * FUNCTION:    AcpiUtUpdateRefCount
411*3446Smrj  *
412*3446Smrj  * PARAMETERS:  Object          - Object whose ref count is to be updated
413*3446Smrj  *              Action          - What to do
414*3446Smrj  *
415*3446Smrj  * RETURN:      New ref count
416*3446Smrj  *
417*3446Smrj  * DESCRIPTION: Modify the ref count and return it.
418*3446Smrj  *
419*3446Smrj  ******************************************************************************/
420*3446Smrj 
421*3446Smrj static void
422*3446Smrj AcpiUtUpdateRefCount (
423*3446Smrj     ACPI_OPERAND_OBJECT     *Object,
424*3446Smrj     UINT32                  Action)
425*3446Smrj {
426*3446Smrj     UINT16                  Count;
427*3446Smrj     UINT16                  NewCount;
428*3446Smrj 
429*3446Smrj 
430*3446Smrj     ACPI_FUNCTION_NAME (UtUpdateRefCount);
431*3446Smrj 
432*3446Smrj 
433*3446Smrj     if (!Object)
434*3446Smrj     {
435*3446Smrj         return;
436*3446Smrj     }
437*3446Smrj 
438*3446Smrj     Count = Object->Common.ReferenceCount;
439*3446Smrj     NewCount = Count;
440*3446Smrj 
441*3446Smrj     /*
442*3446Smrj      * Perform the reference count action (increment, decrement, force delete)
443*3446Smrj      */
444*3446Smrj     switch (Action)
445*3446Smrj     {
446*3446Smrj     case REF_INCREMENT:
447*3446Smrj 
448*3446Smrj         NewCount++;
449*3446Smrj         Object->Common.ReferenceCount = NewCount;
450*3446Smrj 
451*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
452*3446Smrj             "Obj %p Refs=%X, [Incremented]\n",
453*3446Smrj             Object, NewCount));
454*3446Smrj         break;
455*3446Smrj 
456*3446Smrj     case REF_DECREMENT:
457*3446Smrj 
458*3446Smrj         if (Count < 1)
459*3446Smrj         {
460*3446Smrj             ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
461*3446Smrj                 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
462*3446Smrj                 Object, NewCount));
463*3446Smrj 
464*3446Smrj             NewCount = 0;
465*3446Smrj         }
466*3446Smrj         else
467*3446Smrj         {
468*3446Smrj             NewCount--;
469*3446Smrj 
470*3446Smrj             ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
471*3446Smrj                 "Obj %p Refs=%X, [Decremented]\n",
472*3446Smrj                 Object, NewCount));
473*3446Smrj         }
474*3446Smrj 
475*3446Smrj         if (ACPI_GET_OBJECT_TYPE (Object) == ACPI_TYPE_METHOD)
476*3446Smrj         {
477*3446Smrj             ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
478*3446Smrj                 "Method Obj %p Refs=%X, [Decremented]\n", Object, NewCount));
479*3446Smrj         }
480*3446Smrj 
481*3446Smrj         Object->Common.ReferenceCount = NewCount;
482*3446Smrj         if (NewCount == 0)
483*3446Smrj         {
484*3446Smrj             AcpiUtDeleteInternalObj (Object);
485*3446Smrj         }
486*3446Smrj         break;
487*3446Smrj 
488*3446Smrj     case REF_FORCE_DELETE:
489*3446Smrj 
490*3446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
491*3446Smrj             "Obj %p Refs=%X, Force delete! (Set to 0)\n", Object, Count));
492*3446Smrj 
493*3446Smrj         NewCount = 0;
494*3446Smrj         Object->Common.ReferenceCount = NewCount;
495*3446Smrj         AcpiUtDeleteInternalObj (Object);
496*3446Smrj         break;
497*3446Smrj 
498*3446Smrj     default:
499*3446Smrj 
500*3446Smrj         ACPI_ERROR ((AE_INFO, "Unknown action (%X)", Action));
501*3446Smrj         break;
502*3446Smrj     }
503*3446Smrj 
504*3446Smrj     /*
505*3446Smrj      * Sanity check the reference count, for debug purposes only.
506*3446Smrj      * (A deleted object will have a huge reference count)
507*3446Smrj      */
508*3446Smrj     if (Count > ACPI_MAX_REFERENCE_COUNT)
509*3446Smrj     {
510*3446Smrj         ACPI_WARNING ((AE_INFO,
511*3446Smrj             "Large Reference Count (%X) in object %p", Count, Object));
512*3446Smrj     }
513*3446Smrj }
514*3446Smrj 
515*3446Smrj 
516*3446Smrj /*******************************************************************************
517*3446Smrj  *
518*3446Smrj  * FUNCTION:    AcpiUtUpdateObjectReference
519*3446Smrj  *
520*3446Smrj  * PARAMETERS:  Object              - Increment ref count for this object
521*3446Smrj  *                                    and all sub-objects
522*3446Smrj  *              Action              - Either REF_INCREMENT or REF_DECREMENT or
523*3446Smrj  *                                    REF_FORCE_DELETE
524*3446Smrj  *
525*3446Smrj  * RETURN:      Status
526*3446Smrj  *
527*3446Smrj  * DESCRIPTION: Increment the object reference count
528*3446Smrj  *
529*3446Smrj  * Object references are incremented when:
530*3446Smrj  * 1) An object is attached to a Node (namespace object)
531*3446Smrj  * 2) An object is copied (all subobjects must be incremented)
532*3446Smrj  *
533*3446Smrj  * Object references are decremented when:
534*3446Smrj  * 1) An object is detached from an Node
535*3446Smrj  *
536*3446Smrj  ******************************************************************************/
537*3446Smrj 
538*3446Smrj ACPI_STATUS
539*3446Smrj AcpiUtUpdateObjectReference (
540*3446Smrj     ACPI_OPERAND_OBJECT     *Object,
541*3446Smrj     UINT16                  Action)
542*3446Smrj {
543*3446Smrj     ACPI_STATUS             Status = AE_OK;
544*3446Smrj     ACPI_GENERIC_STATE      *StateList = NULL;
545*3446Smrj     ACPI_OPERAND_OBJECT     *NextObject = NULL;
546*3446Smrj     ACPI_GENERIC_STATE      *State;
547*3446Smrj     ACPI_NATIVE_UINT        i;
548*3446Smrj 
549*3446Smrj 
550*3446Smrj     ACPI_FUNCTION_TRACE_PTR (UtUpdateObjectReference, Object);
551*3446Smrj 
552*3446Smrj 
553*3446Smrj     while (Object)
554*3446Smrj     {
555*3446Smrj         /* Make sure that this isn't a namespace handle */
556*3446Smrj 
557*3446Smrj         if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
558*3446Smrj         {
559*3446Smrj             ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
560*3446Smrj                 "Object %p is NS handle\n", Object));
561*3446Smrj             return_ACPI_STATUS (AE_OK);
562*3446Smrj         }
563*3446Smrj 
564*3446Smrj         /*
565*3446Smrj          * All sub-objects must have their reference count incremented also.
566*3446Smrj          * Different object types have different subobjects.
567*3446Smrj          */
568*3446Smrj         switch (ACPI_GET_OBJECT_TYPE (Object))
569*3446Smrj         {
570*3446Smrj         case ACPI_TYPE_DEVICE:
571*3446Smrj         case ACPI_TYPE_PROCESSOR:
572*3446Smrj         case ACPI_TYPE_POWER:
573*3446Smrj         case ACPI_TYPE_THERMAL:
574*3446Smrj 
575*3446Smrj             /* Update the notify objects for these types (if present) */
576*3446Smrj 
577*3446Smrj             AcpiUtUpdateRefCount (Object->CommonNotify.SystemNotify, Action);
578*3446Smrj             AcpiUtUpdateRefCount (Object->CommonNotify.DeviceNotify, Action);
579*3446Smrj             break;
580*3446Smrj 
581*3446Smrj         case ACPI_TYPE_PACKAGE:
582*3446Smrj             /*
583*3446Smrj              * We must update all the sub-objects of the package,
584*3446Smrj              * each of whom may have their own sub-objects.
585*3446Smrj              */
586*3446Smrj             for (i = 0; i < Object->Package.Count; i++)
587*3446Smrj             {
588*3446Smrj                 /*
589*3446Smrj                  * Push each element onto the stack for later processing.
590*3446Smrj                  * Note: There can be null elements within the package,
591*3446Smrj                  * these are simply ignored
592*3446Smrj                  */
593*3446Smrj                 Status = AcpiUtCreateUpdateStateAndPush (
594*3446Smrj                             Object->Package.Elements[i], Action, &StateList);
595*3446Smrj                 if (ACPI_FAILURE (Status))
596*3446Smrj                 {
597*3446Smrj                     goto ErrorExit;
598*3446Smrj                 }
599*3446Smrj             }
600*3446Smrj             break;
601*3446Smrj 
602*3446Smrj         case ACPI_TYPE_BUFFER_FIELD:
603*3446Smrj 
604*3446Smrj             NextObject = Object->BufferField.BufferObj;
605*3446Smrj             break;
606*3446Smrj 
607*3446Smrj         case ACPI_TYPE_LOCAL_REGION_FIELD:
608*3446Smrj 
609*3446Smrj             NextObject = Object->Field.RegionObj;
610*3446Smrj             break;
611*3446Smrj 
612*3446Smrj         case ACPI_TYPE_LOCAL_BANK_FIELD:
613*3446Smrj 
614*3446Smrj             NextObject = Object->BankField.BankObj;
615*3446Smrj             Status = AcpiUtCreateUpdateStateAndPush (
616*3446Smrj                         Object->BankField.RegionObj, Action, &StateList);
617*3446Smrj             if (ACPI_FAILURE (Status))
618*3446Smrj             {
619*3446Smrj                 goto ErrorExit;
620*3446Smrj             }
621*3446Smrj             break;
622*3446Smrj 
623*3446Smrj         case ACPI_TYPE_LOCAL_INDEX_FIELD:
624*3446Smrj 
625*3446Smrj             NextObject = Object->IndexField.IndexObj;
626*3446Smrj             Status = AcpiUtCreateUpdateStateAndPush (
627*3446Smrj                         Object->IndexField.DataObj, Action, &StateList);
628*3446Smrj             if (ACPI_FAILURE (Status))
629*3446Smrj             {
630*3446Smrj                 goto ErrorExit;
631*3446Smrj             }
632*3446Smrj             break;
633*3446Smrj 
634*3446Smrj         case ACPI_TYPE_LOCAL_REFERENCE:
635*3446Smrj             /*
636*3446Smrj              * The target of an Index (a package, string, or buffer) must track
637*3446Smrj              * changes to the ref count of the index.
638*3446Smrj              */
639*3446Smrj             if (Object->Reference.Opcode == AML_INDEX_OP)
640*3446Smrj             {
641*3446Smrj                 NextObject = Object->Reference.Object;
642*3446Smrj             }
643*3446Smrj             break;
644*3446Smrj 
645*3446Smrj         case ACPI_TYPE_REGION:
646*3446Smrj         default:
647*3446Smrj             break; /* No subobjects for all other types */
648*3446Smrj         }
649*3446Smrj 
650*3446Smrj         /*
651*3446Smrj          * Now we can update the count in the main object. This can only
652*3446Smrj          * happen after we update the sub-objects in case this causes the
653*3446Smrj          * main object to be deleted.
654*3446Smrj          */
655*3446Smrj         AcpiUtUpdateRefCount (Object, Action);
656*3446Smrj         Object = NULL;
657*3446Smrj 
658*3446Smrj         /* Move on to the next object to be updated */
659*3446Smrj 
660*3446Smrj         if (NextObject)
661*3446Smrj         {
662*3446Smrj             Object = NextObject;
663*3446Smrj             NextObject = NULL;
664*3446Smrj         }
665*3446Smrj         else if (StateList)
666*3446Smrj         {
667*3446Smrj             State = AcpiUtPopGenericState (&StateList);
668*3446Smrj             Object = State->Update.Object;
669*3446Smrj             AcpiUtDeleteGenericState (State);
670*3446Smrj         }
671*3446Smrj     }
672*3446Smrj 
673*3446Smrj     return_ACPI_STATUS (AE_OK);
674*3446Smrj 
675*3446Smrj ErrorExit:
676*3446Smrj 
677*3446Smrj     ACPI_EXCEPTION ((AE_INFO, Status,
678*3446Smrj         "Could not update object reference count"));
679*3446Smrj 
680*3446Smrj     return_ACPI_STATUS (Status);
681*3446Smrj }
682*3446Smrj 
683*3446Smrj 
684*3446Smrj /*******************************************************************************
685*3446Smrj  *
686*3446Smrj  * FUNCTION:    AcpiUtAddReference
687*3446Smrj  *
688*3446Smrj  * PARAMETERS:  Object          - Object whose reference count is to be
689*3446Smrj  *                                incremented
690*3446Smrj  *
691*3446Smrj  * RETURN:      None
692*3446Smrj  *
693*3446Smrj  * DESCRIPTION: Add one reference to an ACPI object
694*3446Smrj  *
695*3446Smrj  ******************************************************************************/
696*3446Smrj 
697*3446Smrj void
698*3446Smrj AcpiUtAddReference (
699*3446Smrj     ACPI_OPERAND_OBJECT     *Object)
700*3446Smrj {
701*3446Smrj 
702*3446Smrj     ACPI_FUNCTION_TRACE_PTR (UtAddReference, Object);
703*3446Smrj 
704*3446Smrj 
705*3446Smrj     /* Ensure that we have a valid object */
706*3446Smrj 
707*3446Smrj     if (!AcpiUtValidInternalObject (Object))
708*3446Smrj     {
709*3446Smrj         return_VOID;
710*3446Smrj     }
711*3446Smrj 
712*3446Smrj     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
713*3446Smrj         "Obj %p Current Refs=%X [To Be Incremented]\n",
714*3446Smrj         Object, Object->Common.ReferenceCount));
715*3446Smrj 
716*3446Smrj     /* Increment the reference count */
717*3446Smrj 
718*3446Smrj     (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
719*3446Smrj     return_VOID;
720*3446Smrj }
721*3446Smrj 
722*3446Smrj 
723*3446Smrj /*******************************************************************************
724*3446Smrj  *
725*3446Smrj  * FUNCTION:    AcpiUtRemoveReference
726*3446Smrj  *
727*3446Smrj  * PARAMETERS:  Object         - Object whose ref count will be decremented
728*3446Smrj  *
729*3446Smrj  * RETURN:      None
730*3446Smrj  *
731*3446Smrj  * DESCRIPTION: Decrement the reference count of an ACPI internal object
732*3446Smrj  *
733*3446Smrj  ******************************************************************************/
734*3446Smrj 
735*3446Smrj void
736*3446Smrj AcpiUtRemoveReference (
737*3446Smrj     ACPI_OPERAND_OBJECT     *Object)
738*3446Smrj {
739*3446Smrj 
740*3446Smrj     ACPI_FUNCTION_TRACE_PTR (UtRemoveReference, Object);
741*3446Smrj 
742*3446Smrj 
743*3446Smrj     /*
744*3446Smrj      * Allow a NULL pointer to be passed in, just ignore it. This saves
745*3446Smrj      * each caller from having to check. Also, ignore NS nodes.
746*3446Smrj      *
747*3446Smrj      */
748*3446Smrj     if (!Object ||
749*3446Smrj         (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
750*3446Smrj 
751*3446Smrj     {
752*3446Smrj         return_VOID;
753*3446Smrj     }
754*3446Smrj 
755*3446Smrj     /* Ensure that we have a valid object */
756*3446Smrj 
757*3446Smrj     if (!AcpiUtValidInternalObject (Object))
758*3446Smrj     {
759*3446Smrj         return_VOID;
760*3446Smrj     }
761*3446Smrj 
762*3446Smrj     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
763*3446Smrj         "Obj %p Current Refs=%X [To Be Decremented]\n",
764*3446Smrj         Object, Object->Common.ReferenceCount));
765*3446Smrj 
766*3446Smrj     /*
767*3446Smrj      * Decrement the reference count, and only actually delete the object
768*3446Smrj      * if the reference count becomes 0. (Must also decrement the ref count
769*3446Smrj      * of all subobjects!)
770*3446Smrj      */
771*3446Smrj     (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);
772*3446Smrj     return_VOID;
773*3446Smrj }
774*3446Smrj 
775*3446Smrj 
776