Merge from vendor branch LIBPCAP:
[dragonfly.git] / sys / contrib / dev / acpica / utalloc.c
1 /******************************************************************************
2  *
3  * Module Name: utalloc - local cache and memory allocation routines
4  *              $Revision: 131 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, 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 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/utalloc.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
117
118 #define __UTALLOC_C__
119
120 #include "acpi.h"
121
122 #define _COMPONENT          ACPI_UTILITIES
123         ACPI_MODULE_NAME    ("utalloc")
124
125
126 /******************************************************************************
127  *
128  * FUNCTION:    AcpiUtReleaseToCache
129  *
130  * PARAMETERS:  ListId              - Memory list/cache ID
131  *              Object              - The object to be released
132  *
133  * RETURN:      None
134  *
135  * DESCRIPTION: Release an object to the specified cache.  If cache is full,
136  *              the object is deleted.
137  *
138  ******************************************************************************/
139
140 void
141 AcpiUtReleaseToCache (
142     UINT32                  ListId,
143     void                    *Object)
144 {
145     ACPI_MEMORY_LIST        *CacheInfo;
146
147
148     ACPI_FUNCTION_ENTRY ();
149
150
151     /* If walk cache is full, just free this wallkstate object */
152
153     CacheInfo = &AcpiGbl_MemoryLists[ListId];
154     if (CacheInfo->CacheDepth >= CacheInfo->MaxCacheDepth)
155     {
156         ACPI_MEM_FREE (Object);
157         ACPI_MEM_TRACKING (CacheInfo->TotalFreed++);
158     }
159
160     /* Otherwise put this object back into the cache */
161
162     else
163     {
164         if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_CACHES)))
165         {
166             return;
167         }
168
169         /* Mark the object as cached */
170
171         ACPI_MEMSET (Object, 0xCA, CacheInfo->ObjectSize);
172         ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
173
174         /* Put the object at the head of the cache list */
175
176         * (ACPI_CAST_INDIRECT_PTR (char, &(((char *) Object)[CacheInfo->LinkOffset]))) = CacheInfo->ListHead;
177         CacheInfo->ListHead = Object;
178         CacheInfo->CacheDepth++;
179
180         (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
181     }
182 }
183
184
185 /******************************************************************************
186  *
187  * FUNCTION:    AcpiUtAcquireFromCache
188  *
189  * PARAMETERS:  ListId              - Memory list ID
190  *
191  * RETURN:      A requested object.  NULL if the object could not be
192  *              allocated.
193  *
194  * DESCRIPTION: Get an object from the specified cache.  If cache is empty,
195  *              the object is allocated.
196  *
197  ******************************************************************************/
198
199 void *
200 AcpiUtAcquireFromCache (
201     UINT32                  ListId)
202 {
203     ACPI_MEMORY_LIST        *CacheInfo;
204     void                    *Object;
205
206
207     ACPI_FUNCTION_NAME ("UtAcquireFromCache");
208
209
210     CacheInfo = &AcpiGbl_MemoryLists[ListId];
211     if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_CACHES)))
212     {
213         return (NULL);
214     }
215
216     ACPI_MEM_TRACKING (CacheInfo->CacheRequests++);
217
218     /* Check the cache first */
219
220     if (CacheInfo->ListHead)
221     {
222         /* There is an object available, use it */
223
224         Object = CacheInfo->ListHead;
225         CacheInfo->ListHead = *(ACPI_CAST_INDIRECT_PTR (char, &(((char *) Object)[CacheInfo->LinkOffset])));
226
227         ACPI_MEM_TRACKING (CacheInfo->CacheHits++);
228         CacheInfo->CacheDepth--;
229
230 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
231         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p from %s\n",
232             Object, AcpiGbl_MemoryLists[ListId].ListName));
233 #endif
234
235         if (ACPI_FAILURE (AcpiUtReleaseMutex (ACPI_MTX_CACHES)))
236         {
237             return (NULL);
238         }
239
240         /* Clear (zero) the previously used Object */
241
242         ACPI_MEMSET (Object, 0, CacheInfo->ObjectSize);
243     }
244
245     else
246     {
247         /* The cache is empty, create a new object */
248
249         /* Avoid deadlock with ACPI_MEM_CALLOCATE */
250
251         if (ACPI_FAILURE (AcpiUtReleaseMutex (ACPI_MTX_CACHES)))
252         {
253             return (NULL);
254         }
255
256         Object = ACPI_MEM_CALLOCATE (CacheInfo->ObjectSize);
257         ACPI_MEM_TRACKING (CacheInfo->TotalAllocated++);
258     }
259
260     return (Object);
261 }
262
263
264 /******************************************************************************
265  *
266  * FUNCTION:    AcpiUtDeleteGenericCache
267  *
268  * PARAMETERS:  ListId          - Memory list ID
269  *
270  * RETURN:      None
271  *
272  * DESCRIPTION: Free all objects within the requested cache.
273  *
274  ******************************************************************************/
275
276 void
277 AcpiUtDeleteGenericCache (
278     UINT32                  ListId)
279 {
280     ACPI_MEMORY_LIST        *CacheInfo;
281     char                    *Next;
282
283
284     ACPI_FUNCTION_ENTRY ();
285
286
287     CacheInfo = &AcpiGbl_MemoryLists[ListId];
288     while (CacheInfo->ListHead)
289     {
290         /* Delete one cached state object */
291
292         Next = *(ACPI_CAST_INDIRECT_PTR (char, &(((char *) CacheInfo->ListHead)[CacheInfo->LinkOffset])));
293         ACPI_MEM_FREE (CacheInfo->ListHead);
294
295         CacheInfo->ListHead = Next;
296         CacheInfo->CacheDepth--;
297     }
298 }
299
300
301 /*******************************************************************************
302  *
303  * FUNCTION:    AcpiUtValidateBuffer
304  *
305  * PARAMETERS:  Buffer              - Buffer descriptor to be validated
306  *
307  * RETURN:      Status
308  *
309  * DESCRIPTION: Perform parameter validation checks on an ACPI_BUFFER
310  *
311  ******************************************************************************/
312
313 ACPI_STATUS
314 AcpiUtValidateBuffer (
315     ACPI_BUFFER             *Buffer)
316 {
317
318     /* Obviously, the structure pointer must be valid */
319
320     if (!Buffer)
321     {
322         return (AE_BAD_PARAMETER);
323     }
324
325     /* Special semantics for the length */
326
327     if ((Buffer->Length == ACPI_NO_BUFFER)              ||
328         (Buffer->Length == ACPI_ALLOCATE_BUFFER)        ||
329         (Buffer->Length == ACPI_ALLOCATE_LOCAL_BUFFER))
330     {
331         return (AE_OK);
332     }
333
334     /* Length is valid, the buffer pointer must be also */
335
336     if (!Buffer->Pointer)
337     {
338         return (AE_BAD_PARAMETER);
339     }
340
341     return (AE_OK);
342 }
343
344
345 /*******************************************************************************
346  *
347  * FUNCTION:    AcpiUtInitializeBuffer
348  *
349  * PARAMETERS:  RequiredLength      - Length needed
350  *              Buffer              - Buffer to be validated
351  *
352  * RETURN:      Status
353  *
354  * DESCRIPTION: Validate that the buffer is of the required length or
355  *              allocate a new buffer.
356  *
357  ******************************************************************************/
358
359 ACPI_STATUS
360 AcpiUtInitializeBuffer (
361     ACPI_BUFFER             *Buffer,
362     ACPI_SIZE               RequiredLength)
363 {
364     ACPI_STATUS             Status = AE_OK;
365
366
367     switch (Buffer->Length)
368     {
369     case ACPI_NO_BUFFER:
370
371         /* Set the exception and returned the required length */
372
373         Status = AE_BUFFER_OVERFLOW;
374         break;
375
376
377     case ACPI_ALLOCATE_BUFFER:
378
379         /* Allocate a new buffer */
380
381         Buffer->Pointer = AcpiOsAllocate (RequiredLength);
382         if (!Buffer->Pointer)
383         {
384             return (AE_NO_MEMORY);
385         }
386
387         /* Clear the buffer */
388
389         ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
390         break;
391
392
393     case ACPI_ALLOCATE_LOCAL_BUFFER:
394
395         /* Allocate a new buffer with local interface to allow tracking */
396
397         Buffer->Pointer = ACPI_MEM_ALLOCATE (RequiredLength);
398         if (!Buffer->Pointer)
399         {
400             return (AE_NO_MEMORY);
401         }
402
403         /* Clear the buffer */
404
405         ACPI_MEMSET (Buffer->Pointer, 0, RequiredLength);
406         break;
407
408
409     default:
410
411         /* Validate the size of the buffer */
412
413         if (Buffer->Length < RequiredLength)
414         {
415             Status = AE_BUFFER_OVERFLOW;
416         }
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_BLOCK), 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_BLOCK), 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:  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
715 ACPI_DEBUG_MEM_BLOCK *
716 AcpiUtFindAllocation (
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
766 ACPI_STATUS
767 AcpiUtTrackAllocation (
768     UINT32                  ListId,
769     ACPI_DEBUG_MEM_BLOCK    *Allocation,
770     ACPI_SIZE               Size,
771     UINT8                   AllocType,
772     UINT32                  Component,
773     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
834 UnlockAndExit:
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
855 ACPI_STATUS
856 AcpiUtRemoveAllocation (
857     UINT32                  ListId,
858     ACPI_DEBUG_MEM_BLOCK    *Allocation,
859     UINT32                  Component,
860     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
931 void
932 AcpiUtDumpAllocationInfo (
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
991 void
992 AcpiUtDumpAllocations (
993     UINT32                  Component,
994     char                    *Module)
995 {
996     ACPI_DEBUG_MEM_BLOCK    *Element;
997     ACPI_DESCRIPTOR         *Descriptor;
998     UINT32                  NumOutstanding = 0;
999
1000
1001     ACPI_FUNCTION_TRACE ("UtDumpAllocations");
1002
1003
1004     /*
1005      * Walk the allocation list.
1006      */
1007     if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
1008     {
1009         return;
1010     }
1011
1012     Element = AcpiGbl_MemoryLists[0].ListHead;
1013     while (Element)
1014     {
1015         if ((Element->Component & Component) &&
1016             ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
1017         {
1018             /* Ignore allocated objects that are in a cache */
1019
1020             Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
1021             if (Descriptor->DescriptorId != ACPI_DESC_TYPE_CACHED)
1022             {
1023                 AcpiOsPrintf ("%p Len %04X %9.9s-%d ",
1024                             Descriptor, Element->Size, Element->Module,
1025                             Element->Line);
1026
1027                 /* Most of the elements will be internal objects. */
1028
1029                 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
1030                 {
1031                 case ACPI_DESC_TYPE_OPERAND:
1032                     AcpiOsPrintf ("ObjType %12.12s R%hd",
1033                             AcpiUtGetTypeName (Descriptor->Object.Common.Type),
1034                             Descriptor->Object.Common.ReferenceCount);
1035                     break;
1036
1037                 case ACPI_DESC_TYPE_PARSER:
1038                     AcpiOsPrintf ("ParseObj AmlOpcode %04hX",
1039                             Descriptor->Op.Asl.AmlOpcode);
1040                     break;
1041
1042                 case ACPI_DESC_TYPE_NAMED:
1043                     AcpiOsPrintf ("Node %4.4s",
1044                             Descriptor->Node.Name.Ascii);
1045                     break;
1046
1047                 case ACPI_DESC_TYPE_STATE:
1048                     AcpiOsPrintf ("Untyped StateObj");
1049                     break;
1050
1051                 case ACPI_DESC_TYPE_STATE_UPDATE:
1052                     AcpiOsPrintf ("UPDATE StateObj");
1053                     break;
1054
1055                 case ACPI_DESC_TYPE_STATE_PACKAGE:
1056                     AcpiOsPrintf ("PACKAGE StateObj");
1057                     break;
1058
1059                 case ACPI_DESC_TYPE_STATE_CONTROL:
1060                     AcpiOsPrintf ("CONTROL StateObj");
1061                     break;
1062
1063                 case ACPI_DESC_TYPE_STATE_RPSCOPE:
1064                     AcpiOsPrintf ("ROOT-PARSE-SCOPE StateObj");
1065                     break;
1066
1067                 case ACPI_DESC_TYPE_STATE_PSCOPE:
1068                     AcpiOsPrintf ("PARSE-SCOPE StateObj");
1069                     break;
1070
1071                 case ACPI_DESC_TYPE_STATE_WSCOPE:
1072                     AcpiOsPrintf ("WALK-SCOPE StateObj");
1073                     break;
1074
1075                 case ACPI_DESC_TYPE_STATE_RESULT:
1076                     AcpiOsPrintf ("RESULT StateObj");
1077                     break;
1078
1079                 case ACPI_DESC_TYPE_STATE_NOTIFY:
1080                     AcpiOsPrintf ("NOTIFY StateObj");
1081                     break;
1082
1083                 case ACPI_DESC_TYPE_STATE_THREAD:
1084                     AcpiOsPrintf ("THREAD StateObj");
1085                     break;
1086
1087                 default:
1088                     /* All types should appear above */
1089                     break;
1090                 }
1091
1092                 AcpiOsPrintf ( "\n");
1093                 NumOutstanding++;
1094             }
1095         }
1096         Element = Element->Next;
1097     }
1098
1099     (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
1100
1101     /* Print summary */
1102
1103     if (!NumOutstanding)
1104     {
1105         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
1106             "No outstanding allocations.\n"));
1107     }
1108     else
1109     {
1110         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
1111             "%d(%X) Outstanding allocations\n",
1112             NumOutstanding, NumOutstanding));
1113     }
1114
1115     return_VOID;
1116 }
1117
1118
1119 #endif  /* #ifdef ACPI_DBG_TRACK_ALLOCATIONS */
1120