utalloc.c revision 99679
1/******************************************************************************
2 *
3 * Module Name: utalloc - local cache and memory allocation routines
4 *              $Revision: 127 $
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2002, 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
417        /* Clear the buffer */
418
419        ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
420        break;
421    }
422
423    Buffer->Length = RequiredLength;
424    return (Status);
425}
426
427
428/*******************************************************************************
429 *
430 * FUNCTION:    AcpiUtAllocate
431 *
432 * PARAMETERS:  Size                - Size of the allocation
433 *              Component           - Component type of caller
434 *              Module              - Source file name of caller
435 *              Line                - Line number of caller
436 *
437 * RETURN:      Address of the allocated memory on success, NULL on failure.
438 *
439 * DESCRIPTION: The subsystem's equivalent of malloc.
440 *
441 ******************************************************************************/
442
443void *
444AcpiUtAllocate (
445    ACPI_SIZE               Size,
446    UINT32                  Component,
447    NATIVE_CHAR             *Module,
448    UINT32                  Line)
449{
450    void                    *Allocation;
451
452
453    ACPI_FUNCTION_TRACE_U32 ("UtAllocate", Size);
454
455
456    /* Check for an inadvertent size of zero bytes */
457
458    if (!Size)
459    {
460        _ACPI_REPORT_ERROR (Module, Line, Component,
461                ("UtAllocate: Attempt to allocate zero bytes\n"));
462        Size = 1;
463    }
464
465    Allocation = AcpiOsAllocate (Size);
466    if (!Allocation)
467    {
468        /* Report allocation error */
469
470        _ACPI_REPORT_ERROR (Module, Line, Component,
471                ("UtAllocate: Could not allocate size %X\n", (UINT32) Size));
472
473        return_PTR (NULL);
474    }
475
476    return_PTR (Allocation);
477}
478
479
480/*******************************************************************************
481 *
482 * FUNCTION:    AcpiUtCallocate
483 *
484 * PARAMETERS:  Size                - Size of the allocation
485 *              Component           - Component type of caller
486 *              Module              - Source file name of caller
487 *              Line                - Line number of caller
488 *
489 * RETURN:      Address of the allocated memory on success, NULL on failure.
490 *
491 * DESCRIPTION: Subsystem equivalent of calloc.
492 *
493 ******************************************************************************/
494
495void *
496AcpiUtCallocate (
497    ACPI_SIZE               Size,
498    UINT32                  Component,
499    NATIVE_CHAR             *Module,
500    UINT32                  Line)
501{
502    void                    *Allocation;
503
504
505    ACPI_FUNCTION_TRACE_U32 ("UtCallocate", Size);
506
507
508    /* Check for an inadvertent size of zero bytes */
509
510    if (!Size)
511    {
512        _ACPI_REPORT_ERROR (Module, Line, Component,
513                ("UtCallocate: Attempt to allocate zero bytes\n"));
514        return_PTR (NULL);
515    }
516
517    Allocation = AcpiOsAllocate (Size);
518    if (!Allocation)
519    {
520        /* Report allocation error */
521
522        _ACPI_REPORT_ERROR (Module, Line, Component,
523                ("UtCallocate: Could not allocate size %X\n", (UINT32) Size));
524        return_PTR (NULL);
525    }
526
527    /* Clear the memory block */
528
529    ACPI_MEMSET (Allocation, 0, Size);
530    return_PTR (Allocation);
531}
532
533
534#ifdef ACPI_DBG_TRACK_ALLOCATIONS
535/*
536 * These procedures are used for tracking memory leaks in the subsystem, and
537 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
538 *
539 * Each memory allocation is tracked via a doubly linked list.  Each
540 * element contains the caller's component, module name, function name, and
541 * line number.  AcpiUtAllocate and AcpiUtCallocate call
542 * AcpiUtTrackAllocation to add an element to the list; deletion
543 * occurs in the body of AcpiUtFree.
544 */
545
546
547/*******************************************************************************
548 *
549 * FUNCTION:    AcpiUtAllocateAndTrack
550 *
551 * PARAMETERS:  Size                - Size of the allocation
552 *              Component           - Component type of caller
553 *              Module              - Source file name of caller
554 *              Line                - Line number of caller
555 *
556 * RETURN:      Address of the allocated memory on success, NULL on failure.
557 *
558 * DESCRIPTION: The subsystem's equivalent of malloc.
559 *
560 ******************************************************************************/
561
562void *
563AcpiUtAllocateAndTrack (
564    ACPI_SIZE               Size,
565    UINT32                  Component,
566    NATIVE_CHAR             *Module,
567    UINT32                  Line)
568{
569    ACPI_DEBUG_MEM_BLOCK    *Allocation;
570    ACPI_STATUS             Status;
571
572
573    Allocation = AcpiUtAllocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK), Component,
574                                Module, Line);
575    if (!Allocation)
576    {
577        return (NULL);
578    }
579
580    Status = AcpiUtTrackAllocation (ACPI_MEM_LIST_GLOBAL, Allocation, Size,
581                    ACPI_MEM_MALLOC, Component, Module, Line);
582    if (ACPI_FAILURE (Status))
583    {
584        AcpiOsFree (Allocation);
585        return (NULL);
586    }
587
588    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
589    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += (UINT32) Size;
590
591    return ((void *) &Allocation->UserSpace);
592}
593
594
595/*******************************************************************************
596 *
597 * FUNCTION:    AcpiUtCallocateAndTrack
598 *
599 * PARAMETERS:  Size                - Size of the allocation
600 *              Component           - Component type of caller
601 *              Module              - Source file name of caller
602 *              Line                - Line number of caller
603 *
604 * RETURN:      Address of the allocated memory on success, NULL on failure.
605 *
606 * DESCRIPTION: Subsystem equivalent of calloc.
607 *
608 ******************************************************************************/
609
610void *
611AcpiUtCallocateAndTrack (
612    ACPI_SIZE               Size,
613    UINT32                  Component,
614    NATIVE_CHAR             *Module,
615    UINT32                  Line)
616{
617    ACPI_DEBUG_MEM_BLOCK    *Allocation;
618    ACPI_STATUS             Status;
619
620
621    Allocation = AcpiUtCallocate (Size + sizeof (ACPI_DEBUG_MEM_BLOCK), Component,
622                                Module, Line);
623    if (!Allocation)
624    {
625        /* Report allocation error */
626
627        _ACPI_REPORT_ERROR (Module, Line, Component,
628                ("UtCallocate: Could not allocate size %X\n", (UINT32) Size));
629        return (NULL);
630    }
631
632    Status = AcpiUtTrackAllocation (ACPI_MEM_LIST_GLOBAL, Allocation, Size,
633                        ACPI_MEM_CALLOC, Component, Module, Line);
634    if (ACPI_FAILURE (Status))
635    {
636        AcpiOsFree (Allocation);
637        return (NULL);
638    }
639
640    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalAllocated++;
641    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize += (UINT32) Size;
642
643    return ((void *) &Allocation->UserSpace);
644}
645
646
647/*******************************************************************************
648 *
649 * FUNCTION:    AcpiUtFreeAndTrack
650 *
651 * PARAMETERS:  Allocation          - Address of the memory to deallocate
652 *              Component           - Component type of caller
653 *              Module              - Source file name of caller
654 *              Line                - Line number of caller
655 *
656 * RETURN:      None
657 *
658 * DESCRIPTION: Frees the memory at Allocation
659 *
660 ******************************************************************************/
661
662void
663AcpiUtFreeAndTrack (
664    void                    *Allocation,
665    UINT32                  Component,
666    NATIVE_CHAR             *Module,
667    UINT32                  Line)
668{
669    ACPI_DEBUG_MEM_BLOCK    *DebugBlock;
670    ACPI_STATUS             Status;
671
672
673    ACPI_FUNCTION_TRACE_PTR ("UtFree", Allocation);
674
675
676    if (NULL == Allocation)
677    {
678        _ACPI_REPORT_ERROR (Module, Line, Component,
679            ("AcpiUtFree: Attempt to delete a NULL address\n"));
680
681        return_VOID;
682    }
683
684    DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
685                    (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
686
687    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].TotalFreed++;
688    AcpiGbl_MemoryLists[ACPI_MEM_LIST_GLOBAL].CurrentTotalSize -= DebugBlock->Size;
689
690    Status = AcpiUtRemoveAllocation (ACPI_MEM_LIST_GLOBAL, DebugBlock,
691                    Component, Module, Line);
692    if (ACPI_FAILURE (Status))
693    {
694        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not free memory, %s\n",
695            AcpiFormatException (Status)));
696    }
697
698    AcpiOsFree (DebugBlock);
699
700    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Allocation));
701
702    return_VOID;
703}
704
705
706/*******************************************************************************
707 *
708 * FUNCTION:    AcpiUtFindAllocation
709 *
710 * PARAMETERS:  Allocation             - Address of allocated memory
711 *
712 * RETURN:      A list element if found; NULL otherwise.
713 *
714 * DESCRIPTION: Searches for an element in the global allocation tracking list.
715 *
716 ******************************************************************************/
717
718ACPI_DEBUG_MEM_BLOCK *
719AcpiUtFindAllocation (
720    UINT32                  ListId,
721    void                    *Allocation)
722{
723    ACPI_DEBUG_MEM_BLOCK    *Element;
724
725
726    ACPI_FUNCTION_ENTRY ();
727
728
729    if (ListId > ACPI_MEM_LIST_MAX)
730    {
731        return (NULL);
732    }
733
734    Element = AcpiGbl_MemoryLists[ListId].ListHead;
735
736    /* Search for the address. */
737
738    while (Element)
739    {
740        if (Element == Allocation)
741        {
742            return (Element);
743        }
744
745        Element = Element->Next;
746    }
747
748    return (NULL);
749}
750
751
752/*******************************************************************************
753 *
754 * FUNCTION:    AcpiUtTrackAllocation
755 *
756 * PARAMETERS:  Allocation          - Address of allocated memory
757 *              Size                - Size of the allocation
758 *              AllocType           - MEM_MALLOC or MEM_CALLOC
759 *              Component           - Component type of caller
760 *              Module              - Source file name of caller
761 *              Line                - Line number of caller
762 *
763 * RETURN:      None.
764 *
765 * DESCRIPTION: Inserts an element into the global allocation tracking list.
766 *
767 ******************************************************************************/
768
769ACPI_STATUS
770AcpiUtTrackAllocation (
771    UINT32                  ListId,
772    ACPI_DEBUG_MEM_BLOCK    *Allocation,
773    ACPI_SIZE               Size,
774    UINT8                   AllocType,
775    UINT32                  Component,
776    NATIVE_CHAR             *Module,
777    UINT32                  Line)
778{
779    ACPI_MEMORY_LIST        *MemList;
780    ACPI_DEBUG_MEM_BLOCK    *Element;
781    ACPI_STATUS             Status = AE_OK;
782
783
784    ACPI_FUNCTION_TRACE_PTR ("UtTrackAllocation", Allocation);
785
786
787    if (ListId > ACPI_MEM_LIST_MAX)
788    {
789        return_ACPI_STATUS (AE_BAD_PARAMETER);
790    }
791
792    MemList = &AcpiGbl_MemoryLists[ListId];
793    Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
794    if (ACPI_FAILURE (Status))
795    {
796        return_ACPI_STATUS (Status);
797    }
798
799    /*
800     * Search list for this address to make sure it is not already on the list.
801     * This will catch several kinds of problems.
802     */
803
804    Element = AcpiUtFindAllocation (ListId, Allocation);
805    if (Element)
806    {
807        ACPI_REPORT_ERROR (("UtTrackAllocation: Allocation already present in list! (%p)\n",
808            Allocation));
809
810        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Element %p Address %p\n", Element, Allocation));
811
812        goto UnlockAndExit;
813    }
814
815    /* Fill in the instance data. */
816
817    Allocation->Size      = (UINT32) Size;
818    Allocation->AllocType = AllocType;
819    Allocation->Component = Component;
820    Allocation->Line      = Line;
821
822    ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
823
824    /* Insert at list head */
825
826    if (MemList->ListHead)
827    {
828        ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
829    }
830
831    Allocation->Next = MemList->ListHead;
832    Allocation->Previous = NULL;
833
834    MemList->ListHead = Allocation;
835
836
837UnlockAndExit:
838    Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
839    return_ACPI_STATUS (Status);
840}
841
842
843/*******************************************************************************
844 *
845 * FUNCTION:    AcpiUtRemoveAllocation
846 *
847 * PARAMETERS:  Allocation          - Address of allocated memory
848 *              Component           - Component type of caller
849 *              Module              - Source file name of caller
850 *              Line                - Line number of caller
851 *
852 * RETURN:
853 *
854 * DESCRIPTION: Deletes an element from the global allocation tracking list.
855 *
856 ******************************************************************************/
857
858ACPI_STATUS
859AcpiUtRemoveAllocation (
860    UINT32                  ListId,
861    ACPI_DEBUG_MEM_BLOCK    *Allocation,
862    UINT32                  Component,
863    NATIVE_CHAR             *Module,
864    UINT32                  Line)
865{
866    ACPI_MEMORY_LIST        *MemList;
867    ACPI_STATUS             Status;
868
869
870    ACPI_FUNCTION_TRACE ("UtRemoveAllocation");
871
872
873    if (ListId > ACPI_MEM_LIST_MAX)
874    {
875        return_ACPI_STATUS (AE_BAD_PARAMETER);
876    }
877
878    MemList = &AcpiGbl_MemoryLists[ListId];
879    if (NULL == MemList->ListHead)
880    {
881        /* No allocations! */
882
883        _ACPI_REPORT_ERROR (Module, Line, Component,
884                ("UtRemoveAllocation: Empty allocation list, nothing to free!\n"));
885
886        return_ACPI_STATUS (AE_OK);
887    }
888
889    Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
890    if (ACPI_FAILURE (Status))
891    {
892        return_ACPI_STATUS (Status);
893    }
894
895    /* Unlink */
896
897    if (Allocation->Previous)
898    {
899        (Allocation->Previous)->Next = Allocation->Next;
900    }
901    else
902    {
903        MemList->ListHead = Allocation->Next;
904    }
905
906    if (Allocation->Next)
907    {
908        (Allocation->Next)->Previous = Allocation->Previous;
909    }
910
911    /* Mark the segment as deleted */
912
913    ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
914
915    ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size %X\n", Allocation->Size));
916
917    Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
918    return_ACPI_STATUS (Status);
919}
920
921
922/*******************************************************************************
923 *
924 * FUNCTION:    AcpiUtDumpAllocationInfo
925 *
926 * PARAMETERS:
927 *
928 * RETURN:      None
929 *
930 * DESCRIPTION: Print some info about the outstanding allocations.
931 *
932 ******************************************************************************/
933
934void
935AcpiUtDumpAllocationInfo (
936    void)
937{
938/*
939    ACPI_MEMORY_LIST        *MemList;
940*/
941
942    ACPI_FUNCTION_TRACE ("UtDumpAllocationInfo");
943
944/*
945    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
946                    ("%30s: %4d (%3d Kb)\n", "Current allocations",
947                    MemList->CurrentCount,
948                    ROUND_UP_TO_1K (MemList->CurrentSize)));
949
950    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
951                    ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
952                    MemList->MaxConcurrentCount,
953                    ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
954
955
956    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
957                    ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
958                    RunningObjectCount,
959                    ROUND_UP_TO_1K (RunningObjectSize)));
960
961    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
962                    ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
963                    RunningAllocCount,
964                    ROUND_UP_TO_1K (RunningAllocSize)));
965
966
967    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
968                    ("%30s: %4d (%3d Kb)\n", "Current Nodes",
969                    AcpiGbl_CurrentNodeCount,
970                    ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
971
972    ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
973                    ("%30s: %4d (%3d Kb)\n", "Max Nodes",
974                    AcpiGbl_MaxConcurrentNodeCount,
975                    ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount * sizeof (ACPI_NAMESPACE_NODE)))));
976*/
977    return_VOID;
978}
979
980
981/*******************************************************************************
982 *
983 * FUNCTION:    AcpiUtDumpAllocations
984 *
985 * PARAMETERS:  Component           - Component(s) to dump info for.
986 *              Module              - Module to dump info for.  NULL means all.
987 *
988 * RETURN:      None
989 *
990 * DESCRIPTION: Print a list of all outstanding allocations.
991 *
992 ******************************************************************************/
993
994void
995AcpiUtDumpAllocations (
996    UINT32                  Component,
997    NATIVE_CHAR             *Module)
998{
999    ACPI_DEBUG_MEM_BLOCK    *Element;
1000    ACPI_DESCRIPTOR         *Descriptor;
1001    UINT32                  NumOutstanding = 0;
1002
1003
1004    ACPI_FUNCTION_TRACE ("UtDumpAllocations");
1005
1006
1007    /*
1008     * Walk the allocation list.
1009     */
1010    if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
1011    {
1012        return;
1013    }
1014
1015    Element = AcpiGbl_MemoryLists[0].ListHead;
1016    while (Element)
1017    {
1018        if ((Element->Component & Component) &&
1019            ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
1020        {
1021            /* Ignore allocated objects that are in a cache */
1022
1023            Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
1024            if (Descriptor->DescriptorId != ACPI_DESC_TYPE_CACHED)
1025            {
1026                AcpiOsPrintf ("%p Len %04X %9.9s-%d ",
1027                            Descriptor, Element->Size, Element->Module,
1028                            Element->Line);
1029
1030                /* Most of the elements will be internal objects. */
1031
1032                switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
1033                {
1034                case ACPI_DESC_TYPE_OPERAND:
1035                    AcpiOsPrintf ("ObjType %12.12s R%hd",
1036                            AcpiUtGetTypeName (Descriptor->Object.Common.Type),
1037                            Descriptor->Object.Common.ReferenceCount);
1038                    break;
1039
1040                case ACPI_DESC_TYPE_PARSER:
1041                    AcpiOsPrintf ("ParseObj AmlOpcode %04hX",
1042                            Descriptor->Op.Asl.AmlOpcode);
1043                    break;
1044
1045                case ACPI_DESC_TYPE_NAMED:
1046                    AcpiOsPrintf ("Node %4.4s",
1047                            Descriptor->Node.Name.Ascii);
1048                    break;
1049
1050                case ACPI_DESC_TYPE_STATE:
1051                    AcpiOsPrintf ("Untyped StateObj");
1052                    break;
1053
1054                case ACPI_DESC_TYPE_STATE_UPDATE:
1055                    AcpiOsPrintf ("UPDATE StateObj");
1056                    break;
1057
1058                case ACPI_DESC_TYPE_STATE_PACKAGE:
1059                    AcpiOsPrintf ("PACKAGE StateObj");
1060                    break;
1061
1062                case ACPI_DESC_TYPE_STATE_CONTROL:
1063                    AcpiOsPrintf ("CONTROL StateObj");
1064                    break;
1065
1066                case ACPI_DESC_TYPE_STATE_RPSCOPE:
1067                    AcpiOsPrintf ("ROOT-PARSE-SCOPE StateObj");
1068                    break;
1069
1070                case ACPI_DESC_TYPE_STATE_PSCOPE:
1071                    AcpiOsPrintf ("PARSE-SCOPE StateObj");
1072                    break;
1073
1074                case ACPI_DESC_TYPE_STATE_WSCOPE:
1075                    AcpiOsPrintf ("WALK-SCOPE StateObj");
1076                    break;
1077
1078                case ACPI_DESC_TYPE_STATE_RESULT:
1079                    AcpiOsPrintf ("RESULT StateObj");
1080                    break;
1081
1082                case ACPI_DESC_TYPE_STATE_NOTIFY:
1083                    AcpiOsPrintf ("NOTIFY StateObj");
1084                    break;
1085
1086                case ACPI_DESC_TYPE_STATE_THREAD:
1087                    AcpiOsPrintf ("THREAD StateObj");
1088                    break;
1089
1090                default:
1091                    /* All types should appear above */
1092                    break;
1093                }
1094
1095                AcpiOsPrintf ( "\n");
1096                NumOutstanding++;
1097            }
1098        }
1099        Element = Element->Next;
1100    }
1101
1102    (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
1103
1104    /* Print summary */
1105
1106    if (!NumOutstanding)
1107    {
1108        ACPI_DEBUG_PRINT ((ACPI_DB_OK,
1109            "No outstanding allocations.\n"));
1110    }
1111    else
1112    {
1113        ACPI_DEBUG_PRINT ((ACPI_DB_OK,
1114            "%d(%X) Outstanding allocations\n",
1115            NumOutstanding, NumOutstanding));
1116    }
1117
1118    return_VOID;
1119}
1120
1121
1122#endif  /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */
1123
1124