utalloc.c revision 82367
1/******************************************************************************
2 *
3 * Module Name: utalloc - local cache and memory allocation routines
4 *              $Revision: 95 $
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 *
132 * FUNCTION:    AcpiUtReleaseToCache
133 *
134 * PARAMETERS:  ListId              - Memory list/cache ID
135 *              Object              - The object to be released
136 *
137 * RETURN:      None
138 *
139 * DESCRIPTION: Release an object to the specified cache.  If cache is full,
140 *              the object is deleted.
141 *
142 ******************************************************************************/
143
144void
145AcpiUtReleaseToCache (
146    UINT32                  ListId,
147    void                    *Object)
148{
149    ACPI_MEMORY_LIST        *CacheInfo;
150
151
152    /* If walk cache is full, just free this wallkstate object */
153
154    CacheInfo = &AcpiGbl_MemoryLists[ListId];
155    if (CacheInfo->CacheDepth >= CacheInfo->MaxCacheDepth)
156    {
157        ACPI_MEM_FREE (Object);
158        ACPI_MEM_TRACKING (CacheInfo->TotalFreed++);
159    }
160
161    /* Otherwise put this object back into the cache */
162
163    else
164    {
165        AcpiUtAcquireMutex (ACPI_MTX_CACHES);
166
167        /* Mark the object as cached */
168
169        MEMSET (Object, 0xCA, CacheInfo->ObjectSize);
170
171        /* Put the object at the head of the cache list */
172
173        * (char **) (((char *) Object) + CacheInfo->LinkOffset) = CacheInfo->ListHead;
174        CacheInfo->ListHead = Object;
175        CacheInfo->CacheDepth++;
176
177        AcpiUtReleaseMutex (ACPI_MTX_CACHES);
178    }
179}
180
181
182/******************************************************************************
183 *
184 * FUNCTION:    AcpiUtAcquireFromCache
185 *
186 * PARAMETERS:  ListId              - Memory list ID
187 *
188 * RETURN:      A requested object.  NULL if the object could not be
189 *              allocated.
190 *
191 * DESCRIPTION: Get an object from the specified cache.  If cache is empty,
192 *              the object is allocated.
193 *
194 ******************************************************************************/
195
196void *
197AcpiUtAcquireFromCache (
198    UINT32                  ListId)
199{
200    ACPI_MEMORY_LIST        *CacheInfo;
201    void                    *Object;
202
203
204    PROC_NAME ("AcpiUtAcquireFromCache");
205
206    CacheInfo = &AcpiGbl_MemoryLists[ListId];
207    AcpiUtAcquireMutex (ACPI_MTX_CACHES);
208    ACPI_MEM_TRACKING (CacheInfo->CacheRequests++);
209
210    /* Check the cache first */
211
212    if (CacheInfo->ListHead)
213    {
214        /* There is an object available, use it */
215
216        Object = CacheInfo->ListHead;
217        CacheInfo->ListHead = * (char **) (((char *) Object) + CacheInfo->LinkOffset);
218
219        ACPI_MEM_TRACKING (CacheInfo->CacheHits++);
220        CacheInfo->CacheDepth--;
221
222        ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p from cache %d\n", Object, ListId));
223
224        AcpiUtReleaseMutex (ACPI_MTX_CACHES);
225
226        /* Clear (zero) the previously used Object */
227
228        MEMSET (Object, 0, CacheInfo->ObjectSize);
229    }
230
231    else
232    {
233        /* The cache is empty, create a new object */
234
235        /* Avoid deadlock with ACPI_MEM_CALLOCATE */
236
237        AcpiUtReleaseMutex (ACPI_MTX_CACHES);
238
239        Object = ACPI_MEM_CALLOCATE (CacheInfo->ObjectSize);
240        ACPI_MEM_TRACKING (CacheInfo->TotalAllocated++);
241    }
242
243    return (Object);
244}
245
246
247/******************************************************************************
248 *
249 * FUNCTION:    AcpiUtDeleteGenericCache
250 *
251 * PARAMETERS:  ListId          - Memory list ID
252 *
253 * RETURN:      None
254 *
255 * DESCRIPTION: Free all objects within the requested cache.
256 *
257 ******************************************************************************/
258
259void
260AcpiUtDeleteGenericCache (
261    UINT32                  ListId)
262{
263    ACPI_MEMORY_LIST        *CacheInfo;
264    char                    *Next;
265
266
267    CacheInfo = &AcpiGbl_MemoryLists[ListId];
268    while (CacheInfo->ListHead)
269    {
270        /* Delete one cached state object */
271
272        Next = * (char **) (((char *) CacheInfo->ListHead) + CacheInfo->LinkOffset);
273        ACPI_MEM_FREE (CacheInfo->ListHead);
274
275        CacheInfo->ListHead = Next;
276        CacheInfo->CacheDepth--;
277    }
278}
279
280
281
282#ifdef ACPI_DBG_TRACK_ALLOCATIONS
283
284
285/*
286 * These procedures are used for tracking memory leaks in the subsystem, and
287 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
288 *
289 * Each memory allocation is tracked via a doubly linked list.  Each
290 * element contains the caller's component, module name, function name, and
291 * line number.  AcpiUtAllocate and AcpiUtCallocate call
292 * AcpiUtAddElementToAllocList to add an element to the list; deletion
293 * occurs in the body of AcpiUtFree.
294 */
295
296
297/*******************************************************************************
298 *
299 * FUNCTION:    AcpiUtSearchAllocList
300 *
301 * PARAMETERS:  Address             - Address of allocated memory
302 *
303 * RETURN:      A list element if found; NULL otherwise.
304 *
305 * DESCRIPTION: Searches for an element in the global allocation tracking list.
306 *
307 ******************************************************************************/
308
309ACPI_DEBUG_MEM_BLOCK *
310AcpiUtSearchAllocList (
311    UINT32                  ListId,
312    void                    *Address)
313{
314    ACPI_DEBUG_MEM_BLOCK    *Element;
315
316
317    if (ListId > ACPI_MEM_LIST_MAX)
318    {
319        return (NULL);
320    }
321
322    Element = AcpiGbl_MemoryLists[ListId].ListHead;
323
324    /* Search for the address. */
325
326    while (Element)
327    {
328        if (Element == Address)
329        {
330            return (Element);
331        }
332
333        Element = Element->Next;
334    }
335
336    return (NULL);
337}
338
339
340/*******************************************************************************
341 *
342 * FUNCTION:    AcpiUtAddElementToAllocList
343 *
344 * PARAMETERS:  Address             - Address of allocated memory
345 *              Size                - Size of the allocation
346 *              AllocType           - MEM_MALLOC or MEM_CALLOC
347 *              Component           - Component type of caller
348 *              Module              - Source file name of caller
349 *              Line                - Line number of caller
350 *
351 * RETURN:      None.
352 *
353 * DESCRIPTION: Inserts an element into the global allocation tracking list.
354 *
355 ******************************************************************************/
356
357ACPI_STATUS
358AcpiUtAddElementToAllocList (
359    UINT32                  ListId,
360    ACPI_DEBUG_MEM_BLOCK    *Address,
361    UINT32                  Size,
362    UINT8                   AllocType,
363    UINT32                  Component,
364    NATIVE_CHAR             *Module,
365    UINT32                  Line)
366{
367    ACPI_MEMORY_LIST        *MemList;
368    ACPI_DEBUG_MEM_BLOCK    *Element;
369    ACPI_STATUS             Status = AE_OK;
370
371
372    FUNCTION_TRACE_PTR ("UtAddElementToAllocList", Address);
373
374
375    if (ListId > ACPI_MEM_LIST_MAX)
376    {
377        return_ACPI_STATUS (AE_BAD_PARAMETER);
378    }
379
380    MemList = &AcpiGbl_MemoryLists[ListId];
381    AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
382
383    /*
384     * Search list for this address to make sure it is not already on the list.
385     * This will catch several kinds of problems.
386     */
387
388    Element = AcpiUtSearchAllocList (ListId, Address);
389    if (Element)
390    {
391        REPORT_ERROR (("UtAddElementToAllocList: Address already present in list! (%p)\n",
392            Address));
393
394        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Element %p Address %p\n", Element, Address));
395
396        goto UnlockAndExit;
397    }
398
399    /* Fill in the instance data. */
400
401    Address->Size      = Size;
402    Address->AllocType = AllocType;
403    Address->Component = Component;
404    Address->Line      = Line;
405
406    STRNCPY (Address->Module, Module, MAX_MODULE_NAME);
407
408    /* Insert at list head */
409
410    if (MemList->ListHead)
411    {
412        ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Address;
413    }
414
415    Address->Next = MemList->ListHead;
416    Address->Previous = NULL;
417
418    MemList->ListHead = Address;
419
420
421UnlockAndExit:
422    AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
423    return_ACPI_STATUS (Status);
424}
425
426
427/*******************************************************************************
428 *
429 * FUNCTION:    AcpiUtDeleteElementFromAllocList
430 *
431 * PARAMETERS:  Address             - Address of allocated memory
432 *              Component           - Component type of caller
433 *              Module              - Source file name of caller
434 *              Line                - Line number of caller
435 *
436 * RETURN:
437 *
438 * DESCRIPTION: Deletes an element from the global allocation tracking list.
439 *
440 ******************************************************************************/
441
442ACPI_STATUS
443AcpiUtDeleteElementFromAllocList (
444    UINT32                  ListId,
445    ACPI_DEBUG_MEM_BLOCK    *Address,
446    UINT32                  Component,
447    NATIVE_CHAR             *Module,
448    UINT32                  Line)
449{
450    ACPI_MEMORY_LIST        *MemList;
451
452
453    FUNCTION_TRACE ("UtDeleteElementFromAllocList");
454
455
456    if (ListId > ACPI_MEM_LIST_MAX)
457    {
458        return_ACPI_STATUS (AE_BAD_PARAMETER);
459    }
460
461    MemList = &AcpiGbl_MemoryLists[ListId];
462    if (NULL == MemList->ListHead)
463    {
464        /* No allocations! */
465
466        _REPORT_ERROR (Module, Line, Component,
467                ("UtDeleteElementFromAllocList: Empty allocation list, nothing to free!\n"));
468
469        return_ACPI_STATUS (AE_OK);
470    }
471
472
473    AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
474
475    /* Unlink */
476
477    if (Address->Previous)
478    {
479        (Address->Previous)->Next = Address->Next;
480    }
481    else
482    {
483        MemList->ListHead = Address->Next;
484    }
485
486    if (Address->Next)
487    {
488        (Address->Next)->Previous = Address->Previous;
489    }
490
491
492    /* Mark the segment as deleted */
493
494    MEMSET (&Address->UserSpace, 0xEA, Address->Size);
495
496    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size %X\n", Address->Size));
497
498    AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
499    return_ACPI_STATUS (AE_OK);
500}
501
502
503/*******************************************************************************
504 *
505 * FUNCTION:    AcpiUtDumpAllocationInfo
506 *
507 * PARAMETERS:
508 *
509 * RETURN:      None
510 *
511 * DESCRIPTION: Print some info about the outstanding allocations.
512 *
513 ******************************************************************************/
514
515void
516AcpiUtDumpAllocationInfo (
517    void)
518{
519/*
520    ACPI_MEMORY_LIST        *MemList;
521*/
522
523    FUNCTION_TRACE ("UtDumpAllocationInfo");
524
525/*
526    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
527                    ("%30s: %4d (%3d Kb)\n", "Current allocations",
528                    MemList->CurrentCount,
529                    ROUND_UP_TO_1K (MemList->CurrentSize)));
530
531    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
532                    ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
533                    MemList->MaxConcurrentCount,
534                    ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
535
536
537    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
538                    ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
539                    RunningObjectCount,
540                    ROUND_UP_TO_1K (RunningObjectSize)));
541
542    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
543                    ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
544                    RunningAllocCount,
545                    ROUND_UP_TO_1K (RunningAllocSize)));
546
547
548    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
549                    ("%30s: %4d (%3d Kb)\n", "Current Nodes",
550                    AcpiGbl_CurrentNodeCount,
551                    ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
552
553    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
554                    ("%30s: %4d (%3d Kb)\n", "Max Nodes",
555                    AcpiGbl_MaxConcurrentNodeCount,
556                    ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount * sizeof (ACPI_NAMESPACE_NODE)))));
557*/
558    return_VOID;
559}
560
561
562/*******************************************************************************
563 *
564 * FUNCTION:    AcpiUtDumpCurrentAllocations
565 *
566 * PARAMETERS:  Component           - Component(s) to dump info for.
567 *              Module              - Module to dump info for.  NULL means all.
568 *
569 * RETURN:      None
570 *
571 * DESCRIPTION: Print a list of all outstanding allocations.
572 *
573 ******************************************************************************/
574
575void
576AcpiUtDumpCurrentAllocations (
577    UINT32                  Component,
578    NATIVE_CHAR             *Module)
579{
580    ACPI_DEBUG_MEM_BLOCK    *Element;
581    UINT32                  i;
582
583
584    FUNCTION_TRACE ("UtDumpCurrentAllocations");
585
586    Element = AcpiGbl_MemoryLists[0].ListHead;
587
588    if (Element == NULL)
589    {
590        ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
591                "No outstanding allocations.\n"));
592        return_VOID;
593    }
594
595
596    /*
597     * Walk the allocation list.
598     */
599    AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
600
601    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
602        "Outstanding allocations:\n"));
603
604    for (i = 1; ; i++)  /* Just a counter */
605    {
606        if ((Element->Component & Component) &&
607            ((Module == NULL) || (0 == STRCMP (Module, Element->Module))))
608        {
609            ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
610                        "%p Len %04lX %9.9s-%ld",
611                        &Element->UserSpace, Element->Size, Element->Module,
612                        Element->Line));
613
614            /* Most of the elements will be internal objects. */
615
616            switch (((ACPI_OPERAND_OBJECT  *)
617                (&Element->UserSpace))->Common.DataType)
618            {
619            case ACPI_DESC_TYPE_INTERNAL:
620                ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
621                        " ObjType %s",
622                        AcpiUtGetTypeName (((ACPI_OPERAND_OBJECT  *)(&Element->UserSpace))->Common.Type)));
623                break;
624
625            case ACPI_DESC_TYPE_PARSER:
626                ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
627                        " ParseObj Opcode %04X",
628                        ((ACPI_PARSE_OBJECT *)(&Element->UserSpace))->Opcode));
629                break;
630
631            case ACPI_DESC_TYPE_NAMED:
632                ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
633                        " Node %4.4s",
634                        &((ACPI_NAMESPACE_NODE *)(&Element->UserSpace))->Name));
635                break;
636
637            case ACPI_DESC_TYPE_STATE:
638                ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
639                        " StateObj"));
640                break;
641            }
642
643            ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS, "\n"));
644        }
645
646        if (Element->Next == NULL)
647        {
648            break;
649        }
650
651        Element = Element->Next;
652    }
653
654    AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
655
656    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
657        "Total number of unfreed allocations = %d(%X)\n", i,i));
658
659
660    return_VOID;
661
662}
663
664
665/*******************************************************************************
666 *
667 * FUNCTION:    AcpiUtAllocate
668 *
669 * PARAMETERS:  Size                - Size of the allocation
670 *              Component           - Component type of caller
671 *              Module              - Source file name of caller
672 *              Line                - Line number of caller
673 *
674 * RETURN:      Address of the allocated memory on success, NULL on failure.
675 *
676 * DESCRIPTION: The subsystem's equivalent of malloc.
677 *
678 ******************************************************************************/
679
680void *
681AcpiUtAllocate (
682    UINT32                  Size,
683    UINT32                  Component,
684    NATIVE_CHAR             *Module,
685    UINT32                  Line)
686{
687    ACPI_DEBUG_MEM_BLOCK    *Address;
688    ACPI_STATUS             Status;
689
690
691    FUNCTION_TRACE_U32 ("AcpiUtAllocate", Size);
692
693
694    /* Check for an inadvertent size of zero bytes */
695
696    if (!Size)
697    {
698        _REPORT_ERROR (Module, Line, Component,
699                ("UtAllocate: Attempt to allocate zero bytes\n"));
700        Size = 1;
701    }
702
703    Address = AcpiOsAllocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK));
704    if (!Address)
705    {
706        /* Report allocation error */
707
708        _REPORT_ERROR (Module, Line, Component,
709                ("UtAllocate: Could not allocate size %X\n", Size));
710
711        return_PTR (NULL);
712    }
713
714    Status = AcpiUtAddElementToAllocList (ACPI_MEM_LIST_GLOBAL, Address, Size,
715                    MEM_MALLOC, Component, Module, Line);
716    if (ACPI_FAILURE (Status))
717    {
718        AcpiOsFree (Address);
719        return_PTR (NULL);
720    }
721
722    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
723    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += Size;
724
725    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n", Address, Size));
726
727    return_PTR ((void *) &Address->UserSpace);
728}
729
730
731/*******************************************************************************
732 *
733 * FUNCTION:    AcpiUtCallocate
734 *
735 * PARAMETERS:  Size                - Size of the allocation
736 *              Component           - Component type of caller
737 *              Module              - Source file name of caller
738 *              Line                - Line number of caller
739 *
740 * RETURN:      Address of the allocated memory on success, NULL on failure.
741 *
742 * DESCRIPTION: Subsystem equivalent of calloc.
743 *
744 ******************************************************************************/
745
746void *
747AcpiUtCallocate (
748    UINT32                  Size,
749    UINT32                  Component,
750    NATIVE_CHAR             *Module,
751    UINT32                  Line)
752{
753    ACPI_DEBUG_MEM_BLOCK    *Address;
754    ACPI_STATUS             Status;
755
756
757    FUNCTION_TRACE_U32 ("AcpiUtCallocate", Size);
758
759
760    /* Check for an inadvertent size of zero bytes */
761
762    if (!Size)
763    {
764        _REPORT_ERROR (Module, Line, Component,
765                ("UtCallocate: Attempt to allocate zero bytes\n"));
766        return_PTR (NULL);
767    }
768
769
770    Address = AcpiOsCallocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK));
771    if (!Address)
772    {
773        /* Report allocation error */
774
775        _REPORT_ERROR (Module, Line, Component,
776                ("UtCallocate: Could not allocate size %X\n", Size));
777        return_PTR (NULL);
778    }
779
780    Status = AcpiUtAddElementToAllocList (ACPI_MEM_LIST_GLOBAL, Address, Size,
781                        MEM_CALLOC, Component, Module, Line);
782    if (ACPI_FAILURE (Status))
783    {
784        AcpiOsFree (Address);
785        return_PTR (NULL);
786    }
787
788    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
789    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += Size;
790
791    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n", Address, Size));
792    return_PTR ((void *) &Address->UserSpace);
793}
794
795
796/*******************************************************************************
797 *
798 * FUNCTION:    AcpiUtFree
799 *
800 * PARAMETERS:  Address             - Address of the memory to deallocate
801 *              Component           - Component type of caller
802 *              Module              - Source file name of caller
803 *              Line                - Line number of caller
804 *
805 * RETURN:      None
806 *
807 * DESCRIPTION: Frees the memory at Address
808 *
809 ******************************************************************************/
810
811void
812AcpiUtFree (
813    void                    *Address,
814    UINT32                  Component,
815    NATIVE_CHAR             *Module,
816    UINT32                  Line)
817{
818    ACPI_DEBUG_MEM_BLOCK    *DebugBlock;
819
820
821    FUNCTION_TRACE_PTR ("AcpiUtFree", Address);
822
823
824    if (NULL == Address)
825    {
826        _REPORT_ERROR (Module, Line, Component,
827            ("AcpiUtFree: Trying to delete a NULL address\n"));
828
829        return_VOID;
830    }
831
832    DebugBlock = (ACPI_DEBUG_MEM_BLOCK *)
833                    (((char *) Address) - sizeof (ACPI_DEBUG_MEM_HEADER));
834
835    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalFreed++;
836    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize -= DebugBlock->Size;
837
838    AcpiUtDeleteElementFromAllocList (ACPI_MEM_LIST_GLOBAL, DebugBlock,
839            Component, Module, Line);
840    AcpiOsFree (DebugBlock);
841
842    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Address));
843
844    return_VOID;
845}
846
847#endif  /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */
848
849