utalloc.c revision 126372
1/******************************************************************************
2 *
3 * Module Name: utalloc - local cache and memory allocation routines
4 *              $Revision: 135 $
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2004, 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
121#define _COMPONENT          ACPI_UTILITIES
122        ACPI_MODULE_NAME    ("utalloc")
123
124
125/******************************************************************************
126 *
127 * FUNCTION:    AcpiUtReleaseToCache
128 *
129 * PARAMETERS:  ListId              - Memory list/cache ID
130 *              Object              - The object to be released
131 *
132 * RETURN:      None
133 *
134 * DESCRIPTION: Release an object to the specified cache.  If cache is full,
135 *              the object is deleted.
136 *
137 ******************************************************************************/
138
139void
140AcpiUtReleaseToCache (
141    UINT32                  ListId,
142    void                    *Object)
143{
144    ACPI_MEMORY_LIST        *CacheInfo;
145
146
147    ACPI_FUNCTION_ENTRY ();
148
149
150    /* If walk cache is full, just free this wallkstate object */
151
152    CacheInfo = &AcpiGbl_MemoryLists[ListId];
153    if (CacheInfo->CacheDepth >= CacheInfo->MaxCacheDepth)
154    {
155        ACPI_MEM_FREE (Object);
156        ACPI_MEM_TRACKING (CacheInfo->TotalFreed++);
157    }
158
159    /* Otherwise put this object back into the cache */
160
161    else
162    {
163        if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_CACHES)))
164        {
165            return;
166        }
167
168        /* Mark the object as cached */
169
170        ACPI_MEMSET (Object, 0xCA, CacheInfo->ObjectSize);
171        ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
172
173        /* Put the object at the head of the cache list */
174
175        * (ACPI_CAST_INDIRECT_PTR (char, &(((char *) Object)[CacheInfo->LinkOffset]))) = CacheInfo->ListHead;
176        CacheInfo->ListHead = Object;
177        CacheInfo->CacheDepth++;
178
179        (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
180    }
181}
182
183
184/******************************************************************************
185 *
186 * FUNCTION:    AcpiUtAcquireFromCache
187 *
188 * PARAMETERS:  ListId              - Memory list ID
189 *
190 * RETURN:      A requested object.  NULL if the object could not be
191 *              allocated.
192 *
193 * DESCRIPTION: Get an object from the specified cache.  If cache is empty,
194 *              the object is allocated.
195 *
196 ******************************************************************************/
197
198void *
199AcpiUtAcquireFromCache (
200    UINT32                  ListId)
201{
202    ACPI_MEMORY_LIST        *CacheInfo;
203    void                    *Object;
204
205
206    ACPI_FUNCTION_NAME ("UtAcquireFromCache");
207
208
209    CacheInfo = &AcpiGbl_MemoryLists[ListId];
210    if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_CACHES)))
211    {
212        return (NULL);
213    }
214
215    ACPI_MEM_TRACKING (CacheInfo->CacheRequests++);
216
217    /* Check the cache first */
218
219    if (CacheInfo->ListHead)
220    {
221        /* There is an object available, use it */
222
223        Object = CacheInfo->ListHead;
224        CacheInfo->ListHead = *(ACPI_CAST_INDIRECT_PTR (char, &(((char *) Object)[CacheInfo->LinkOffset])));
225
226        ACPI_MEM_TRACKING (CacheInfo->CacheHits++);
227        CacheInfo->CacheDepth--;
228
229#ifdef ACPI_DBG_TRACK_ALLOCATIONS
230        ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p from %s\n",
231            Object, AcpiGbl_MemoryLists[ListId].ListName));
232#endif
233
234        if (ACPI_FAILURE (AcpiUtReleaseMutex (ACPI_MTX_CACHES)))
235        {
236            return (NULL);
237        }
238
239        /* Clear (zero) the previously used Object */
240
241        ACPI_MEMSET (Object, 0, CacheInfo->ObjectSize);
242    }
243
244    else
245    {
246        /* The cache is empty, create a new object */
247
248        /* Avoid deadlock with ACPI_MEM_CALLOCATE */
249
250        if (ACPI_FAILURE (AcpiUtReleaseMutex (ACPI_MTX_CACHES)))
251        {
252            return (NULL);
253        }
254
255        Object = ACPI_MEM_CALLOCATE (CacheInfo->ObjectSize);
256        ACPI_MEM_TRACKING (CacheInfo->TotalAllocated++);
257    }
258
259    return (Object);
260}
261
262
263/******************************************************************************
264 *
265 * FUNCTION:    AcpiUtDeleteGenericCache
266 *
267 * PARAMETERS:  ListId          - Memory list ID
268 *
269 * RETURN:      None
270 *
271 * DESCRIPTION: Free all objects within the requested cache.
272 *
273 ******************************************************************************/
274
275void
276AcpiUtDeleteGenericCache (
277    UINT32                  ListId)
278{
279    ACPI_MEMORY_LIST        *CacheInfo;
280    char                    *Next;
281
282
283    ACPI_FUNCTION_ENTRY ();
284
285
286    CacheInfo = &AcpiGbl_MemoryLists[ListId];
287    while (CacheInfo->ListHead)
288    {
289        /* Delete one cached state object */
290
291        Next = *(ACPI_CAST_INDIRECT_PTR (char, &(((char *) CacheInfo->ListHead)[CacheInfo->LinkOffset])));
292        ACPI_MEM_FREE (CacheInfo->ListHead);
293
294        CacheInfo->ListHead = Next;
295        CacheInfo->CacheDepth--;
296    }
297}
298
299
300/*******************************************************************************
301 *
302 * FUNCTION:    AcpiUtValidateBuffer
303 *
304 * PARAMETERS:  Buffer              - Buffer descriptor to be validated
305 *
306 * RETURN:      Status
307 *
308 * DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER
309 *
310 ******************************************************************************/
311
312ACPI_STATUS
313AcpiUtValidateBuffer (
314    ACPI_BUFFER             *Buffer)
315{
316
317    /* Obviously, the structure pointer must be valid */
318
319    if (!Buffer)
320    {
321        return (AE_BAD_PARAMETER);
322    }
323
324    /* Special semantics for the length */
325
326    if ((Buffer->Length == ACPI_NO_BUFFER)              ||
327        (Buffer->Length == ACPI_ALLOCATE_BUFFER)        ||
328        (Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))
329    {
330        return (AE_OK);
331    }
332
333    /* Length is valid, the buffer pointer must be also */
334
335    if (!Buffer->Pointer)
336    {
337        return (AE_BAD_PARAMETER);
338    }
339
340    return (AE_OK);
341}
342
343
344/*******************************************************************************
345 *
346 * FUNCTION:    AcpiUtInitializeBuffer
347 *
348 * PARAMETERS:  RequiredLength      - Length needed
349 *              Buffer              - Buffer to be validated
350 *
351 * RETURN:      Status
352 *
353 * DESCRIPTION: Validate that the buffer is of the required length or
354 *              allocate a new buffer.
355 *
356 ******************************************************************************/
357
358ACPI_STATUS
359AcpiUtInitializeBuffer (
360    ACPI_BUFFER             *Buffer,
361    ACPI_SIZE               RequiredLength)
362{
363    ACPI_STATUS             Status = AE_OK;
364
365
366    switch (Buffer->Length)
367    {
368    case ACPI_NO_BUFFER:
369
370        /* Set the exception and returned the required length */
371
372        Status = AE_BUFFER_OVERFLOW;
373        break;
374
375
376    case ACPI_ALLOCATE_BUFFER:
377
378        /* Allocate a new buffer */
379
380        Buffer->Pointer = AcpiOsAllocate (RequiredLength);
381        if (!Buffer->Pointer)
382        {
383            return (AE_NO_MEMORY);
384        }
385
386        /* Clear the buffer */
387
388        ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
389        break;
390
391
392    case ACPI_ALLOCATE_LOCAL_BUFFER:
393
394        /* Allocate a new buffer with local interface to allow tracking */
395
396        Buffer->Pointer = ACPI_MEM_ALLOCATE (RequiredLength);
397        if (!Buffer->Pointer)
398        {
399            return (AE_NO_MEMORY);
400        }
401
402        /* Clear the buffer */
403
404        ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
405        break;
406
407
408    default:
409
410        /* Validate the size of the buffer */
411
412        if (Buffer->Length < RequiredLength)
413        {
414            Status = AE_BUFFER_OVERFLOW;
415        }
416        break;
417    }
418
419    Buffer->Length = RequiredLength;
420    return (Status);
421}
422
423
424/*******************************************************************************
425 *
426 * FUNCTION:    AcpiUtAllocate
427 *
428 * PARAMETERS:  Size                - Size of the allocation
429 *              Component           - Component type of caller
430 *              Module              - Source file name of caller
431 *              Line                - Line number of caller
432 *
433 * RETURN:      Address of the allocated memory on success, NULL on failure.
434 *
435 * DESCRIPTION: The subsystem's equivalent of malloc.
436 *
437 ******************************************************************************/
438
439void *
440AcpiUtAllocate (
441    ACPI_SIZE               Size,
442    UINT32                  Component,
443    char                    *Module,
444    UINT32                  Line)
445{
446    void                    *Allocation;
447
448
449    ACPI_FUNCTION_TRACE_U32 ("UtAllocate", Size);
450
451
452    /* Check for an inadvertent size of zero bytes */
453
454    if (!Size)
455    {
456        _ACPI_REPORT_ERROR (Module, Line, Component,
457                ("UtAllocate: Attempt to allocate zero bytes\n"));
458        Size = 1;
459    }
460
461    Allocation = AcpiOsAllocate (Size);
462    if (!Allocation)
463    {
464        /* Report allocation error */
465
466        _ACPI_REPORT_ERROR (Module, Line, Component,
467                ("UtAllocate: Could not allocate size %X\n", (UINT32) Size));
468
469        return_PTR (NULL);
470    }
471
472    return_PTR (Allocation);
473}
474
475
476/*******************************************************************************
477 *
478 * FUNCTION:    AcpiUtCallocate
479 *
480 * PARAMETERS:  Size                - Size of the allocation
481 *              Component           - Component type of caller
482 *              Module              - Source file name of caller
483 *              Line                - Line number of caller
484 *
485 * RETURN:      Address of the allocated memory on success, NULL on failure.
486 *
487 * DESCRIPTION: Subsystem equivalent of calloc.
488 *
489 ******************************************************************************/
490
491void *
492AcpiUtCallocate (
493    ACPI_SIZE               Size,
494    UINT32                  Component,
495    char                    *Module,
496    UINT32                  Line)
497{
498    void                    *Allocation;
499
500
501    ACPI_FUNCTION_TRACE_U32 ("UtCallocate", Size);
502
503
504    /* Check for an inadvertent size of zero bytes */
505
506    if (!Size)
507    {
508        _ACPI_REPORT_ERROR (Module, Line, Component,
509                ("UtCallocate: Attempt to allocate zero bytes\n"));
510        return_PTR (NULL);
511    }
512
513    Allocation = AcpiOsAllocate (Size);
514    if (!Allocation)
515    {
516        /* Report allocation error */
517
518        _ACPI_REPORT_ERROR (Module, Line, Component,
519                ("UtCallocate: Could not allocate size %X\n", (UINT32) Size));
520        return_PTR (NULL);
521    }
522
523    /* Clear the memory block */
524
525    ACPI_MEMSET (Allocation, 0, Size);
526    return_PTR (Allocation);
527}
528
529
530#ifdef ACPI_DBG_TRACK_ALLOCATIONS
531/*
532 * These procedures are used for tracking memory leaks in the subsystem, and
533 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
534 *
535 * Each memory allocation is tracked via a doubly linked list.  Each
536 * element contains the caller's component, module name, function name, and
537 * line number.  AcpiUtAllocate and AcpiUtCallocate call
538 * AcpiUtTrackAllocation to add an element to the list; deletion
539 * occurs in the body of AcpiUtFree.
540 */
541
542
543/*******************************************************************************
544 *
545 * FUNCTION:    AcpiUtAllocateAndTrack
546 *
547 * PARAMETERS:  Size                - Size of the allocation
548 *              Component           - Component type of caller
549 *              Module              - Source file name of caller
550 *              Line                - Line number of caller
551 *
552 * RETURN:      Address of the allocated memory on success, NULL on failure.
553 *
554 * DESCRIPTION: The subsystem's equivalent of malloc.
555 *
556 ******************************************************************************/
557
558void *
559AcpiUtAllocateAndTrack (
560    ACPI_SIZE               Size,
561    UINT32                  Component,
562    char                    *Module,
563    UINT32                  Line)
564{
565    ACPI_DEBUG_MEM_BLOCK    *Allocation;
566    ACPI_STATUS             Status;
567
568
569    Allocation = AcpiUtAllocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK), Component,
570                                Module, Line);
571    if (!Allocation)
572    {
573        return (NULL);
574    }
575
576    Status = AcpiUtTrackAllocation (ACPI_MEM_LIST_GLOBAL, Allocation, Size,
577                    ACPI_MEM_MALLOC, Component, Module, Line);
578    if (ACPI_FAILURE (Status))
579    {
580        AcpiOsFree (Allocation);
581        return (NULL);
582    }
583
584    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
585    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += (UINT32) Size;
586
587    return ((void *) &Allocation->UserSpace);
588}
589
590
591/*******************************************************************************
592 *
593 * FUNCTION:    AcpiUtCallocateAndTrack
594 *
595 * PARAMETERS:  Size                - Size of the allocation
596 *              Component           - Component type of caller
597 *              Module              - Source file name of caller
598 *              Line                - Line number of caller
599 *
600 * RETURN:      Address of the allocated memory on success, NULL on failure.
601 *
602 * DESCRIPTION: Subsystem equivalent of calloc.
603 *
604 ******************************************************************************/
605
606void *
607AcpiUtCallocateAndTrack (
608    ACPI_SIZE               Size,
609    UINT32                  Component,
610    char                    *Module,
611    UINT32                  Line)
612{
613    ACPI_DEBUG_MEM_BLOCK    *Allocation;
614    ACPI_STATUS             Status;
615
616
617    Allocation = AcpiUtCallocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK), Component,
618                                Module, Line);
619    if (!Allocation)
620    {
621        /* Report allocation error */
622
623        _ACPI_REPORT_ERROR (Module, Line, Component,
624                ("UtCallocate: Could not allocate size %X\n", (UINT32) Size));
625        return (NULL);
626    }
627
628    Status = AcpiUtTrackAllocation (ACPI_MEM_LIST_GLOBAL, Allocation, Size,
629                        ACPI_MEM_CALLOC, Component, Module, Line);
630    if (ACPI_FAILURE (Status))
631    {
632        AcpiOsFree (Allocation);
633        return (NULL);
634    }
635
636    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
637    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += (UINT32) Size;
638
639    return ((void *) &Allocation->UserSpace);
640}
641
642
643/*******************************************************************************
644 *
645 * FUNCTION:    AcpiUtFreeAndTrack
646 *
647 * PARAMETERS:  Allocation          - Address of the memory to deallocate
648 *              Component           - Component type of caller
649 *              Module              - Source file name of caller
650 *              Line                - Line number of caller
651 *
652 * RETURN:      None
653 *
654 * DESCRIPTION: Frees the memory at Allocation
655 *
656 ******************************************************************************/
657
658void
659AcpiUtFreeAndTrack (
660    void                    *Allocation,
661    UINT32                  Component,
662    char                    *Module,
663    UINT32                  Line)
664{
665    ACPI_DEBUG_MEM_BLOCK    *DebugBlock;
666    ACPI_STATUS             Status;
667
668
669    ACPI_FUNCTION_TRACE_PTR ("UtFree", Allocation);
670
671
672    if (NULL == Allocation)
673    {
674        _ACPI_REPORT_ERROR (Module, Line, Component,
675            ("AcpiUtFree: Attempt to delete a NULL address\n"));
676
677        return_VOID;
678    }
679
680    DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
681                    (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
682
683    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalFreed++;
684    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize -= DebugBlock->Size;
685
686    Status = AcpiUtRemoveAllocation (ACPI_MEM_LIST_GLOBAL, DebugBlock,
687                    Component, Module, Line);
688    if (ACPI_FAILURE (Status))
689    {
690        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not free memory, %s\n",
691            AcpiFormatException (Status)));
692    }
693
694    AcpiOsFree (DebugBlock);
695
696    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Allocation));
697
698    return_VOID;
699}
700
701
702/*******************************************************************************
703 *
704 * FUNCTION:    AcpiUtFindAllocation
705 *
706 * PARAMETERS:  Allocation             - Address of allocated memory
707 *
708 * RETURN:      A list element if found; NULL otherwise.
709 *
710 * DESCRIPTION: Searches for an element in the global allocation tracking list.
711 *
712 ******************************************************************************/
713
714ACPI_DEBUG_MEM_BLOCK *
715AcpiUtFindAllocation (
716    UINT32                  ListId,
717    void                    *Allocation)
718{
719    ACPI_DEBUG_MEM_BLOCK    *Element;
720
721
722    ACPI_FUNCTION_ENTRY ();
723
724
725    if (ListId > ACPI_MEM_LIST_MAX)
726    {
727        return (NULL);
728    }
729
730    Element = AcpiGbl_MemoryLists[ListId].ListHead;
731
732    /* Search for the address. */
733
734    while (Element)
735    {
736        if (Element == Allocation)
737        {
738            return (Element);
739        }
740
741        Element = Element->Next;
742    }
743
744    return (NULL);
745}
746
747
748/*******************************************************************************
749 *
750 * FUNCTION:    AcpiUtTrackAllocation
751 *
752 * PARAMETERS:  Allocation          - Address of allocated memory
753 *              Size                - Size of the allocation
754 *              AllocType           - MEM_MALLOC or MEM_CALLOC
755 *              Component           - Component type of caller
756 *              Module              - Source file name of caller
757 *              Line                - Line number of caller
758 *
759 * RETURN:      None.
760 *
761 * DESCRIPTION: Inserts an element into the global allocation tracking list.
762 *
763 ******************************************************************************/
764
765ACPI_STATUS
766AcpiUtTrackAllocation (
767    UINT32                  ListId,
768    ACPI_DEBUG_MEM_BLOCK    *Allocation,
769    ACPI_SIZE               Size,
770    UINT8                   AllocType,
771    UINT32                  Component,
772    char                    *Module,
773    UINT32                  Line)
774{
775    ACPI_MEMORY_LIST        *MemList;
776    ACPI_DEBUG_MEM_BLOCK    *Element;
777    ACPI_STATUS             Status = AE_OK;
778
779
780    ACPI_FUNCTION_TRACE_PTR ("UtTrackAllocation", Allocation);
781
782
783    if (ListId > ACPI_MEM_LIST_MAX)
784    {
785        return_ACPI_STATUS (AE_BAD_PARAMETER);
786    }
787
788    MemList = &AcpiGbl_MemoryLists[ListId];
789    Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
790    if (ACPI_FAILURE (Status))
791    {
792        return_ACPI_STATUS (Status);
793    }
794
795    /*
796     * Search list for this address to make sure it is not already on the list.
797     * This will catch several kinds of problems.
798     */
799
800    Element = AcpiUtFindAllocation (ListId, Allocation);
801    if (Element)
802    {
803        ACPI_REPORT_ERROR (("UtTrackAllocation: Allocation already present in list! (%p)\n",
804            Allocation));
805
806        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Element %p Address %p\n", Element, Allocation));
807
808        goto UnlockAndExit;
809    }
810
811    /* Fill in the instance data. */
812
813    Allocation->Size      = (UINT32) Size;
814    Allocation->AllocType = AllocType;
815    Allocation->Component = Component;
816    Allocation->Line      = Line;
817
818    ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
819
820    /* Insert at list head */
821
822    if (MemList->ListHead)
823    {
824        ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
825    }
826
827    Allocation->Next = MemList->ListHead;
828    Allocation->Previous = NULL;
829
830    MemList->ListHead = Allocation;
831
832
833UnlockAndExit:
834    Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
835    return_ACPI_STATUS (Status);
836}
837
838
839/*******************************************************************************
840 *
841 * FUNCTION:    AcpiUtRemoveAllocation
842 *
843 * PARAMETERS:  Allocation          - Address of allocated memory
844 *              Component           - Component type of caller
845 *              Module              - Source file name of caller
846 *              Line                - Line number of caller
847 *
848 * RETURN:
849 *
850 * DESCRIPTION: Deletes an element from the global allocation tracking list.
851 *
852 ******************************************************************************/
853
854ACPI_STATUS
855AcpiUtRemoveAllocation (
856    UINT32                  ListId,
857    ACPI_DEBUG_MEM_BLOCK    *Allocation,
858    UINT32                  Component,
859    char                    *Module,
860    UINT32                  Line)
861{
862    ACPI_MEMORY_LIST        *MemList;
863    ACPI_STATUS             Status;
864
865
866    ACPI_FUNCTION_TRACE ("UtRemoveAllocation");
867
868
869    if (ListId > ACPI_MEM_LIST_MAX)
870    {
871        return_ACPI_STATUS (AE_BAD_PARAMETER);
872    }
873
874    MemList = &AcpiGbl_MemoryLists[ListId];
875    if (NULL == MemList->ListHead)
876    {
877        /* No allocations! */
878
879        _ACPI_REPORT_ERROR (Module, Line, Component,
880                ("UtRemoveAllocation: Empty allocation list, nothing to free!\n"));
881
882        return_ACPI_STATUS (AE_OK);
883    }
884
885    Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
886    if (ACPI_FAILURE (Status))
887    {
888        return_ACPI_STATUS (Status);
889    }
890
891    /* Unlink */
892
893    if (Allocation->Previous)
894    {
895        (Allocation->Previous)->Next = Allocation->Next;
896    }
897    else
898    {
899        MemList->ListHead = Allocation->Next;
900    }
901
902    if (Allocation->Next)
903    {
904        (Allocation->Next)->Previous = Allocation->Previous;
905    }
906
907    /* Mark the segment as deleted */
908
909    ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
910
911    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n", Allocation->Size));
912
913    Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
914    return_ACPI_STATUS (Status);
915}
916
917
918/*******************************************************************************
919 *
920 * FUNCTION:    AcpiUtDumpAllocationInfo
921 *
922 * PARAMETERS:
923 *
924 * RETURN:      None
925 *
926 * DESCRIPTION: Print some info about the outstanding allocations.
927 *
928 ******************************************************************************/
929
930void
931AcpiUtDumpAllocationInfo (
932    void)
933{
934/*
935    ACPI_MEMORY_LIST        *MemList;
936*/
937
938    ACPI_FUNCTION_TRACE ("UtDumpAllocationInfo");
939
940/*
941    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
942                    ("%30s: %4d (%3d Kb)\n", "Current allocations",
943                    MemList->CurrentCount,
944                    ROUND_UP_TO_1K (MemList->CurrentSize)));
945
946    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
947                    ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
948                    MemList->MaxConcurrentCount,
949                    ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
950
951
952    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
953                    ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
954                    RunningObjectCount,
955                    ROUND_UP_TO_1K (RunningObjectSize)));
956
957    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
958                    ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
959                    RunningAllocCount,
960                    ROUND_UP_TO_1K (RunningAllocSize)));
961
962
963    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
964                    ("%30s: %4d (%3d Kb)\n", "Current Nodes",
965                    AcpiGbl_CurrentNodeCount,
966                    ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
967
968    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
969                    ("%30s: %4d (%3d Kb)\n", "Max Nodes",
970                    AcpiGbl_MaxConcurrentNodeCount,
971                    ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount * sizeof (ACPI_NAMESPACE_NODE)))));
972*/
973    return_VOID;
974}
975
976
977/*******************************************************************************
978 *
979 * FUNCTION:    AcpiUtDumpAllocations
980 *
981 * PARAMETERS:  Component           - Component(s) to dump info for.
982 *              Module              - Module to dump info for.  NULL means all.
983 *
984 * RETURN:      None
985 *
986 * DESCRIPTION: Print a list of all outstanding allocations.
987 *
988 ******************************************************************************/
989
990void
991AcpiUtDumpAllocations (
992    UINT32                  Component,
993    char                    *Module)
994{
995    ACPI_DEBUG_MEM_BLOCK    *Element;
996    ACPI_DESCRIPTOR         *Descriptor;
997    UINT32                  NumOutstanding = 0;
998
999
1000    ACPI_FUNCTION_TRACE ("UtDumpAllocations");
1001
1002
1003    /*
1004     * Walk the allocation list.
1005     */
1006    if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
1007    {
1008        return;
1009    }
1010
1011    Element = AcpiGbl_MemoryLists[0].ListHead;
1012    while (Element)
1013    {
1014        if ((Element->Component & Component) &&
1015            ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
1016        {
1017            /* Ignore allocated objects that are in a cache */
1018
1019            Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
1020            if (Descriptor->DescriptorId != ACPI_DESC_TYPE_CACHED)
1021            {
1022                AcpiOsPrintf ("%p Len %04X %9.9s-%d [%s] ",
1023                            Descriptor, Element->Size, Element->Module,
1024                            Element->Line, AcpiUtGetDescriptorName (Descriptor));
1025
1026                /* Most of the elements will be Operand objects. */
1027
1028                switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
1029                {
1030                case ACPI_DESC_TYPE_OPERAND:
1031                    AcpiOsPrintf ("%12.12s R%hd",
1032                            AcpiUtGetTypeName (Descriptor->Object.Common.Type),
1033                            Descriptor->Object.Common.ReferenceCount);
1034                    break;
1035
1036                case ACPI_DESC_TYPE_PARSER:
1037                    AcpiOsPrintf ("AmlOpcode %04hX",
1038                            Descriptor->Op.Asl.AmlOpcode);
1039                    break;
1040
1041                case ACPI_DESC_TYPE_NAMED:
1042                    AcpiOsPrintf ("%4.4s",
1043                            AcpiUtGetNodeName (&Descriptor->Node));
1044                    break;
1045
1046                default:
1047                    break;
1048                }
1049
1050                AcpiOsPrintf ( "\n");
1051                NumOutstanding++;
1052            }
1053        }
1054        Element = Element->Next;
1055    }
1056
1057    (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
1058
1059    /* Print summary */
1060
1061    if (!NumOutstanding)
1062    {
1063        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
1064            "No outstanding allocations.\n"));
1065    }
1066    else
1067    {
1068        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
1069            "%d(%X) Outstanding allocations\n",
1070            NumOutstanding, NumOutstanding));
1071    }
1072
1073    return_VOID;
1074}
1075
1076
1077#endif  /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */
1078
1079