utalloc.c revision 87031
1/******************************************************************************
2 *
3 * Module Name: utalloc - local cache and memory allocation routines
4 *              $Revision: 109 $
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999, 2000, 2001, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights.  You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
21 *
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
28 *
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code.  No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision.  In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change.  Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee.  Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
54 *
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution.  In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
66 *
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government.  In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117#define __UTALLOC_C__
118
119#include "acpi.h"
120#include "acparser.h"
121#include "acinterp.h"
122#include "acnamesp.h"
123#include "acglobal.h"
124
125#define _COMPONENT          ACPI_UTILITIES
126        MODULE_NAME         ("utalloc")
127
128
129/******************************************************************************
130 *
131 * FUNCTION:    AcpiUtReleaseToCache
132 *
133 * PARAMETERS:  ListId              - Memory list/cache ID
134 *              Object              - The object to be released
135 *
136 * RETURN:      None
137 *
138 * DESCRIPTION: Release an object to the specified cache.  If cache is full,
139 *              the object is deleted.
140 *
141 ******************************************************************************/
142
143void
144AcpiUtReleaseToCache (
145    UINT32                  ListId,
146    void                    *Object)
147{
148    ACPI_MEMORY_LIST        *CacheInfo;
149
150
151    FUNCTION_ENTRY ();
152
153
154    /* If walk cache is full, just free this wallkstate object */
155
156    CacheInfo = &AcpiGbl_MemoryLists[ListId];
157    if (CacheInfo->CacheDepth >= CacheInfo->MaxCacheDepth)
158    {
159        ACPI_MEM_FREE (Object);
160        ACPI_MEM_TRACKING (CacheInfo->TotalFreed++);
161    }
162
163    /* Otherwise put this object back into the cache */
164
165    else
166    {
167        AcpiUtAcquireMutex (ACPI_MTX_CACHES);
168
169        /* Mark the object as cached */
170
171        MEMSET (Object, 0xCA, CacheInfo->ObjectSize);
172        ((ACPI_OPERAND_OBJECT *) Object)->Common.DataType = ACPI_CACHED_OBJECT;
173
174        /* Put the object at the head of the cache list */
175
176        * (char **) (((char *) Object) + CacheInfo->LinkOffset) = CacheInfo->ListHead;
177        CacheInfo->ListHead = Object;
178        CacheInfo->CacheDepth++;
179
180        AcpiUtReleaseMutex (ACPI_MTX_CACHES);
181    }
182}
183
184
185/******************************************************************************
186 *
187 * FUNCTION:    AcpiUtAcquireFromCache
188 *
189 * PARAMETERS:  ListId              - Memory list ID
190 *
191 * RETURN:      A requested object.  NULL if the object could not be
192 *              allocated.
193 *
194 * DESCRIPTION: Get an object from the specified cache.  If cache is empty,
195 *              the object is allocated.
196 *
197 ******************************************************************************/
198
199void *
200AcpiUtAcquireFromCache (
201    UINT32                  ListId)
202{
203    ACPI_MEMORY_LIST        *CacheInfo;
204    void                    *Object;
205
206
207    PROC_NAME ("UtAcquireFromCache");
208
209
210    CacheInfo = &AcpiGbl_MemoryLists[ListId];
211    AcpiUtAcquireMutex (ACPI_MTX_CACHES);
212    ACPI_MEM_TRACKING (CacheInfo->CacheRequests++);
213
214    /* Check the cache first */
215
216    if (CacheInfo->ListHead)
217    {
218        /* There is an object available, use it */
219
220        Object = CacheInfo->ListHead;
221        CacheInfo->ListHead = * (char **) (((char *) Object) + CacheInfo->LinkOffset);
222
223        ACPI_MEM_TRACKING (CacheInfo->CacheHits++);
224        CacheInfo->CacheDepth--;
225
226#ifdef ACPI_DBG_TRACK_ALLOCATIONS
227        ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p from %s\n",
228            Object, AcpiGbl_MemoryLists[ListId].ListName));
229#endif
230
231        AcpiUtReleaseMutex (ACPI_MTX_CACHES);
232
233        /* Clear (zero) the previously used Object */
234
235        MEMSET (Object, 0, CacheInfo->ObjectSize);
236    }
237
238    else
239    {
240        /* The cache is empty, create a new object */
241
242        /* Avoid deadlock with ACPI_MEM_CALLOCATE */
243
244        AcpiUtReleaseMutex (ACPI_MTX_CACHES);
245
246        Object = ACPI_MEM_CALLOCATE (CacheInfo->ObjectSize);
247        ACPI_MEM_TRACKING (CacheInfo->TotalAllocated++);
248    }
249
250    return (Object);
251}
252
253
254/******************************************************************************
255 *
256 * FUNCTION:    AcpiUtDeleteGenericCache
257 *
258 * PARAMETERS:  ListId          - Memory list ID
259 *
260 * RETURN:      None
261 *
262 * DESCRIPTION: Free all objects within the requested cache.
263 *
264 ******************************************************************************/
265
266void
267AcpiUtDeleteGenericCache (
268    UINT32                  ListId)
269{
270    ACPI_MEMORY_LIST        *CacheInfo;
271    char                    *Next;
272
273
274    FUNCTION_ENTRY ();
275
276
277    CacheInfo = &AcpiGbl_MemoryLists[ListId];
278    while (CacheInfo->ListHead)
279    {
280        /* Delete one cached state object */
281
282        Next = * (char **) (((char *) CacheInfo->ListHead) + CacheInfo->LinkOffset);
283        ACPI_MEM_FREE (CacheInfo->ListHead);
284
285        CacheInfo->ListHead = Next;
286        CacheInfo->CacheDepth--;
287    }
288}
289
290
291#ifdef ACPI_DBG_TRACK_ALLOCATIONS
292
293
294/*
295 * These procedures are used for tracking memory leaks in the subsystem, and
296 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
297 *
298 * Each memory allocation is tracked via a doubly linked list.  Each
299 * element contains the caller's component, module name, function name, and
300 * line number.  AcpiUtAllocate and AcpiUtCallocate call
301 * AcpiUtTrackAllocation to add an element to the list; deletion
302 * occurs in the body of AcpiUtFree.
303 */
304
305
306/*******************************************************************************
307 *
308 * FUNCTION:    AcpiUtFindAllocation
309 *
310 * PARAMETERS:  Address             - Address of allocated memory
311 *
312 * RETURN:      A list element if found; NULL otherwise.
313 *
314 * DESCRIPTION: Searches for an element in the global allocation tracking list.
315 *
316 ******************************************************************************/
317
318ACPI_DEBUG_MEM_BLOCK *
319AcpiUtFindAllocation (
320    UINT32                  ListId,
321    void                    *Address)
322{
323    ACPI_DEBUG_MEM_BLOCK    *Element;
324
325
326    FUNCTION_ENTRY ();
327
328
329    if (ListId > ACPI_MEM_LIST_MAX)
330    {
331        return (NULL);
332    }
333
334    Element = AcpiGbl_MemoryLists[ListId].ListHead;
335
336    /* Search for the address. */
337
338    while (Element)
339    {
340        if (Element == Address)
341        {
342            return (Element);
343        }
344
345        Element = Element->Next;
346    }
347
348    return (NULL);
349}
350
351
352/*******************************************************************************
353 *
354 * FUNCTION:    AcpiUtTrackAllocation
355 *
356 * PARAMETERS:  Address             - Address of allocated memory
357 *              Size                - Size of the allocation
358 *              AllocType           - MEM_MALLOC or MEM_CALLOC
359 *              Component           - Component type of caller
360 *              Module              - Source file name of caller
361 *              Line                - Line number of caller
362 *
363 * RETURN:      None.
364 *
365 * DESCRIPTION: Inserts an element into the global allocation tracking list.
366 *
367 ******************************************************************************/
368
369ACPI_STATUS
370AcpiUtTrackAllocation (
371    UINT32                  ListId,
372    ACPI_DEBUG_MEM_BLOCK    *Address,
373    UINT32                  Size,
374    UINT8                   AllocType,
375    UINT32                  Component,
376    NATIVE_CHAR             *Module,
377    UINT32                  Line)
378{
379    ACPI_MEMORY_LIST        *MemList;
380    ACPI_DEBUG_MEM_BLOCK    *Element;
381    ACPI_STATUS             Status = AE_OK;
382
383
384    FUNCTION_TRACE_PTR ("UtTrackAllocation", Address);
385
386
387    if (ListId > ACPI_MEM_LIST_MAX)
388    {
389        return_ACPI_STATUS (AE_BAD_PARAMETER);
390    }
391
392    MemList = &AcpiGbl_MemoryLists[ListId];
393    AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
394
395    /*
396     * Search list for this address to make sure it is not already on the list.
397     * This will catch several kinds of problems.
398     */
399
400    Element = AcpiUtFindAllocation (ListId, Address);
401    if (Element)
402    {
403        REPORT_ERROR (("UtTrackAllocation: Address already present in list! (%p)\n",
404            Address));
405
406        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Element %p Address %p\n", Element, Address));
407
408        goto UnlockAndExit;
409    }
410
411    /* Fill in the instance data. */
412
413    Address->Size      = Size;
414    Address->AllocType = AllocType;
415    Address->Component = Component;
416    Address->Line      = Line;
417
418    STRNCPY (Address->Module, Module, MAX_MODULE_NAME);
419
420    /* Insert at list head */
421
422    if (MemList->ListHead)
423    {
424        ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Address;
425    }
426
427    Address->Next = MemList->ListHead;
428    Address->Previous = NULL;
429
430    MemList->ListHead = Address;
431
432
433UnlockAndExit:
434    AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
435    return_ACPI_STATUS (Status);
436}
437
438
439/*******************************************************************************
440 *
441 * FUNCTION:    AcpiUtRemoveAllocation
442 *
443 * PARAMETERS:  Address             - Address of allocated memory
444 *              Component           - Component type of caller
445 *              Module              - Source file name of caller
446 *              Line                - Line number of caller
447 *
448 * RETURN:
449 *
450 * DESCRIPTION: Deletes an element from the global allocation tracking list.
451 *
452 ******************************************************************************/
453
454ACPI_STATUS
455AcpiUtRemoveAllocation (
456    UINT32                  ListId,
457    ACPI_DEBUG_MEM_BLOCK    *Address,
458    UINT32                  Component,
459    NATIVE_CHAR             *Module,
460    UINT32                  Line)
461{
462    ACPI_MEMORY_LIST        *MemList;
463
464
465    FUNCTION_TRACE ("UtRemoveAllocation");
466
467
468    if (ListId > ACPI_MEM_LIST_MAX)
469    {
470        return_ACPI_STATUS (AE_BAD_PARAMETER);
471    }
472
473    MemList = &AcpiGbl_MemoryLists[ListId];
474    if (NULL == MemList->ListHead)
475    {
476        /* No allocations! */
477
478        _REPORT_ERROR (Module, Line, Component,
479                ("UtRemoveAllocation: Empty allocation list, nothing to free!\n"));
480
481        return_ACPI_STATUS (AE_OK);
482    }
483
484
485    AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
486
487    /* Unlink */
488
489    if (Address->Previous)
490    {
491        (Address->Previous)->Next = Address->Next;
492    }
493    else
494    {
495        MemList->ListHead = Address->Next;
496    }
497
498    if (Address->Next)
499    {
500        (Address->Next)->Previous = Address->Previous;
501    }
502
503
504    /* Mark the segment as deleted */
505
506    MEMSET (&Address->UserSpace, 0xEA, Address->Size);
507
508    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size %X\n", Address->Size));
509
510    AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
511    return_ACPI_STATUS (AE_OK);
512}
513
514
515/*******************************************************************************
516 *
517 * FUNCTION:    AcpiUtDumpAllocationInfo
518 *
519 * PARAMETERS:
520 *
521 * RETURN:      None
522 *
523 * DESCRIPTION: Print some info about the outstanding allocations.
524 *
525 ******************************************************************************/
526
527void
528AcpiUtDumpAllocationInfo (
529    void)
530{
531/*
532    ACPI_MEMORY_LIST        *MemList;
533*/
534
535    FUNCTION_TRACE ("UtDumpAllocationInfo");
536
537/*
538    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
539                    ("%30s: %4d (%3d Kb)\n", "Current allocations",
540                    MemList->CurrentCount,
541                    ROUND_UP_TO_1K (MemList->CurrentSize)));
542
543    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
544                    ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
545                    MemList->MaxConcurrentCount,
546                    ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
547
548
549    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
550                    ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
551                    RunningObjectCount,
552                    ROUND_UP_TO_1K (RunningObjectSize)));
553
554    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
555                    ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
556                    RunningAllocCount,
557                    ROUND_UP_TO_1K (RunningAllocSize)));
558
559
560    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
561                    ("%30s: %4d (%3d Kb)\n", "Current Nodes",
562                    AcpiGbl_CurrentNodeCount,
563                    ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
564
565    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
566                    ("%30s: %4d (%3d Kb)\n", "Max Nodes",
567                    AcpiGbl_MaxConcurrentNodeCount,
568                    ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount * sizeof (ACPI_NAMESPACE_NODE)))));
569*/
570    return_VOID;
571}
572
573
574/*******************************************************************************
575 *
576 * FUNCTION:    AcpiUtDumpAllocations
577 *
578 * PARAMETERS:  Component           - Component(s) to dump info for.
579 *              Module              - Module to dump info for.  NULL means all.
580 *
581 * RETURN:      None
582 *
583 * DESCRIPTION: Print a list of all outstanding allocations.
584 *
585 ******************************************************************************/
586
587void
588AcpiUtDumpAllocations (
589    UINT32                  Component,
590    NATIVE_CHAR             *Module)
591{
592    ACPI_DEBUG_MEM_BLOCK    *Element;
593    UINT32                  NumOutstanding = 0;
594
595
596    FUNCTION_TRACE ("UtDumpAllocations");
597
598
599    /*
600     * Walk the allocation list.
601     */
602    AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
603
604    Element = AcpiGbl_MemoryLists[0].ListHead;
605    while (Element)
606    {
607        if ((Element->Component & Component) &&
608            ((Module == NULL) || (0 == STRCMP (Module, Element->Module))))
609        {
610            /* Ignore allocated objects that are in a cache */
611
612            if (((ACPI_OPERAND_OBJECT  *)(&Element->UserSpace))->Common.Type != ACPI_CACHED_OBJECT)
613            {
614                AcpiOsPrintf ("%p Len %04X %9.9s-%d ",
615                            &Element->UserSpace, Element->Size, Element->Module,
616                            Element->Line);
617
618                /* Most of the elements will be internal objects. */
619
620                switch (((ACPI_OPERAND_OBJECT  *)
621                    (&Element->UserSpace))->Common.DataType)
622                {
623                case ACPI_DESC_TYPE_INTERNAL:
624                    AcpiOsPrintf ("ObjType %12.12s R%d",
625                            AcpiUtGetTypeName (((ACPI_OPERAND_OBJECT *)(&Element->UserSpace))->Common.Type),
626                            ((ACPI_OPERAND_OBJECT *)(&Element->UserSpace))->Common.ReferenceCount);
627                    break;
628
629                case ACPI_DESC_TYPE_PARSER:
630                    AcpiOsPrintf ("ParseObj Opcode %04X",
631                            ((ACPI_PARSE_OBJECT *)(&Element->UserSpace))->Opcode);
632                    break;
633
634                case ACPI_DESC_TYPE_NAMED:
635                    AcpiOsPrintf ("Node %4.4s",
636                            (char *) &((ACPI_NAMESPACE_NODE *)(&Element->UserSpace))->Name);
637                    break;
638
639                case ACPI_DESC_TYPE_STATE:
640                    AcpiOsPrintf ("Untyped StateObj");
641                    break;
642
643                case ACPI_DESC_TYPE_STATE_UPDATE:
644                    AcpiOsPrintf ("UPDATE StateObj");
645                    break;
646
647                case ACPI_DESC_TYPE_STATE_PACKAGE:
648                    AcpiOsPrintf ("PACKAGE StateObj");
649                    break;
650
651                case ACPI_DESC_TYPE_STATE_CONTROL:
652                    AcpiOsPrintf ("CONTROL StateObj");
653                    break;
654
655                case ACPI_DESC_TYPE_STATE_RPSCOPE:
656                    AcpiOsPrintf ("ROOT-PARSE-SCOPE StateObj");
657                    break;
658
659                case ACPI_DESC_TYPE_STATE_PSCOPE:
660                    AcpiOsPrintf ("PARSE-SCOPE StateObj");
661                    break;
662
663                case ACPI_DESC_TYPE_STATE_WSCOPE:
664                    AcpiOsPrintf ("WALK-SCOPE StateObj");
665                    break;
666
667                case ACPI_DESC_TYPE_STATE_RESULT:
668                    AcpiOsPrintf ("RESULT StateObj");
669                    break;
670
671                case ACPI_DESC_TYPE_STATE_NOTIFY:
672                    AcpiOsPrintf ("NOTIFY StateObj");
673                    break;
674
675                case ACPI_DESC_TYPE_STATE_THREAD:
676                    AcpiOsPrintf ("THREAD StateObj");
677                    break;
678                }
679
680                AcpiOsPrintf ( "\n");
681                NumOutstanding++;
682            }
683        }
684        Element = Element->Next;
685    }
686
687    AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
688
689    /* Print summary */
690
691    if (!NumOutstanding)
692    {
693        ACPI_DEBUG_PRINT ((ACPI_DB_OK,
694                "No outstanding allocations.\n"));
695    }
696    else
697    {
698        ACPI_DEBUG_PRINT ((ACPI_DB_OK,
699            "%d(%X) Outstanding allocations\n",
700            NumOutstanding, NumOutstanding));
701    }
702
703    return_VOID;
704}
705
706
707/*******************************************************************************
708 *
709 * FUNCTION:    AcpiUtAllocate
710 *
711 * PARAMETERS:  Size                - Size of the allocation
712 *              Component           - Component type of caller
713 *              Module              - Source file name of caller
714 *              Line                - Line number of caller
715 *
716 * RETURN:      Address of the allocated memory on success, NULL on failure.
717 *
718 * DESCRIPTION: The subsystem's equivalent of malloc.
719 *
720 ******************************************************************************/
721
722void *
723AcpiUtAllocate (
724    UINT32                  Size,
725    UINT32                  Component,
726    NATIVE_CHAR             *Module,
727    UINT32                  Line)
728{
729    ACPI_DEBUG_MEM_BLOCK    *Address;
730    ACPI_STATUS             Status;
731
732
733    FUNCTION_TRACE_U32 ("UtAllocate", Size);
734
735
736    /* Check for an inadvertent size of zero bytes */
737
738    if (!Size)
739    {
740        _REPORT_ERROR (Module, Line, Component,
741                ("UtAllocate: Attempt to allocate zero bytes\n"));
742        Size = 1;
743    }
744
745    Address = AcpiOsAllocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK));
746    if (!Address)
747    {
748        /* Report allocation error */
749
750        _REPORT_ERROR (Module, Line, Component,
751                ("UtAllocate: Could not allocate size %X\n", Size));
752
753        return_PTR (NULL);
754    }
755
756    Status = AcpiUtTrackAllocation (ACPI_MEM_LIST_GLOBAL, Address, Size,
757                    MEM_MALLOC, Component, Module, Line);
758    if (ACPI_FAILURE (Status))
759    {
760        AcpiOsFree (Address);
761        return_PTR (NULL);
762    }
763
764    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
765    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += Size;
766
767    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n", Address, Size));
768
769    return_PTR ((void *) &Address->UserSpace);
770}
771
772
773/*******************************************************************************
774 *
775 * FUNCTION:    AcpiUtCallocate
776 *
777 * PARAMETERS:  Size                - Size of the allocation
778 *              Component           - Component type of caller
779 *              Module              - Source file name of caller
780 *              Line                - Line number of caller
781 *
782 * RETURN:      Address of the allocated memory on success, NULL on failure.
783 *
784 * DESCRIPTION: Subsystem equivalent of calloc.
785 *
786 ******************************************************************************/
787
788void *
789AcpiUtCallocate (
790    UINT32                  Size,
791    UINT32                  Component,
792    NATIVE_CHAR             *Module,
793    UINT32                  Line)
794{
795    ACPI_DEBUG_MEM_BLOCK    *Address;
796    ACPI_STATUS             Status;
797
798
799    FUNCTION_TRACE_U32 ("UtCallocate", Size);
800
801
802    /* Check for an inadvertent size of zero bytes */
803
804    if (!Size)
805    {
806        _REPORT_ERROR (Module, Line, Component,
807                ("UtCallocate: Attempt to allocate zero bytes\n"));
808        return_PTR (NULL);
809    }
810
811
812    Address = AcpiOsCallocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK));
813    if (!Address)
814    {
815        /* Report allocation error */
816
817        _REPORT_ERROR (Module, Line, Component,
818                ("UtCallocate: Could not allocate size %X\n", Size));
819        return_PTR (NULL);
820    }
821
822    Status = AcpiUtTrackAllocation (ACPI_MEM_LIST_GLOBAL, Address, Size,
823                        MEM_CALLOC, Component, Module, Line);
824    if (ACPI_FAILURE (Status))
825    {
826        AcpiOsFree (Address);
827        return_PTR (NULL);
828    }
829
830    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
831    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += Size;
832
833    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n", Address, Size));
834    return_PTR ((void *) &Address->UserSpace);
835}
836
837
838/*******************************************************************************
839 *
840 * FUNCTION:    AcpiUtFree
841 *
842 * PARAMETERS:  Address             - Address of the memory to deallocate
843 *              Component           - Component type of caller
844 *              Module              - Source file name of caller
845 *              Line                - Line number of caller
846 *
847 * RETURN:      None
848 *
849 * DESCRIPTION: Frees the memory at Address
850 *
851 ******************************************************************************/
852
853void
854AcpiUtFree (
855    void                    *Address,
856    UINT32                  Component,
857    NATIVE_CHAR             *Module,
858    UINT32                  Line)
859{
860    ACPI_DEBUG_MEM_BLOCK    *DebugBlock;
861
862
863    FUNCTION_TRACE_PTR ("UtFree", Address);
864
865
866    if (NULL == Address)
867    {
868        _REPORT_ERROR (Module, Line, Component,
869            ("AcpiUtFree: Trying to delete a NULL address\n"));
870
871        return_VOID;
872    }
873
874    DebugBlock = (ACPI_DEBUG_MEM_BLOCK *)
875                    (((char *) Address) - sizeof (ACPI_DEBUG_MEM_HEADER));
876
877    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalFreed++;
878    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize -= DebugBlock->Size;
879
880    AcpiUtRemoveAllocation (ACPI_MEM_LIST_GLOBAL, DebugBlock,
881            Component, Module, Line);
882    AcpiOsFree (DebugBlock);
883
884    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Address));
885
886    return_VOID;
887}
888
889#endif  /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */
890
891