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