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