No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / intel-public / acpica / dist / utilities / uttrack.c
blob85ae826db36c1511d8fc70a27511ca7260776273
1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
12 * All rights reserved.
14 * 2. License
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
117 * These procedures are used for tracking memory leaks in the subsystem, and
118 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
120 * Each memory allocation is tracked via a doubly linked list. Each
121 * element contains the caller's component, module name, function name, and
122 * line number. AcpiUtAllocate and AcpiUtAllocateZeroed call
123 * AcpiUtTrackAllocation to add an element to the list; deletion
124 * occurs in the body of AcpiUtFree.
127 #define __UTTRACK_C__
129 #include "acpi.h"
130 #include "accommon.h"
132 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
134 #define _COMPONENT ACPI_UTILITIES
135 ACPI_MODULE_NAME ("uttrack")
137 /* Local prototypes */
139 static ACPI_DEBUG_MEM_BLOCK *
140 AcpiUtFindAllocation (
141 void *Allocation);
143 static ACPI_STATUS
144 AcpiUtTrackAllocation (
145 ACPI_DEBUG_MEM_BLOCK *Address,
146 ACPI_SIZE Size,
147 UINT8 AllocType,
148 UINT32 Component,
149 const char *Module,
150 UINT32 Line);
152 static ACPI_STATUS
153 AcpiUtRemoveAllocation (
154 ACPI_DEBUG_MEM_BLOCK *Address,
155 UINT32 Component,
156 const char *Module,
157 UINT32 Line);
160 /*******************************************************************************
162 * FUNCTION: AcpiUtCreateList
164 * PARAMETERS: CacheName - Ascii name for the cache
165 * ObjectSize - Size of each cached object
166 * ReturnCache - Where the new cache object is returned
168 * RETURN: Status
170 * DESCRIPTION: Create a local memory list for tracking purposed
172 ******************************************************************************/
174 ACPI_STATUS
175 AcpiUtCreateList (
176 const char *ListName,
177 UINT16 ObjectSize,
178 ACPI_MEMORY_LIST **ReturnCache)
180 ACPI_MEMORY_LIST *Cache;
183 Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
184 if (!Cache)
186 return (AE_NO_MEMORY);
189 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
191 Cache->ListName = __UNCONST(ListName);
192 Cache->ObjectSize = ObjectSize;
194 *ReturnCache = Cache;
195 return (AE_OK);
199 /*******************************************************************************
201 * FUNCTION: AcpiUtAllocateAndTrack
203 * PARAMETERS: Size - Size of the allocation
204 * Component - Component type of caller
205 * Module - Source file name of caller
206 * Line - Line number of caller
208 * RETURN: Address of the allocated memory on success, NULL on failure.
210 * DESCRIPTION: The subsystem's equivalent of malloc.
212 ******************************************************************************/
214 void *
215 AcpiUtAllocateAndTrack (
216 ACPI_SIZE Size,
217 UINT32 Component,
218 const char *Module,
219 UINT32 Line)
221 ACPI_DEBUG_MEM_BLOCK *Allocation;
222 ACPI_STATUS Status;
225 Allocation = AcpiUtAllocate (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
226 Component, Module, Line);
227 if (!Allocation)
229 return (NULL);
232 Status = AcpiUtTrackAllocation (Allocation, Size,
233 ACPI_MEM_MALLOC, Component, Module, Line);
234 if (ACPI_FAILURE (Status))
236 AcpiOsFree (Allocation);
237 return (NULL);
240 AcpiGbl_GlobalList->TotalAllocated++;
241 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
242 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
243 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
245 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
248 return ((void *) &Allocation->UserSpace);
252 /*******************************************************************************
254 * FUNCTION: AcpiUtAllocateZeroedAndTrack
256 * PARAMETERS: Size - Size of the allocation
257 * Component - Component type of caller
258 * Module - Source file name of caller
259 * Line - Line number of caller
261 * RETURN: Address of the allocated memory on success, NULL on failure.
263 * DESCRIPTION: Subsystem equivalent of calloc.
265 ******************************************************************************/
267 void *
268 AcpiUtAllocateZeroedAndTrack (
269 ACPI_SIZE Size,
270 UINT32 Component,
271 const char *Module,
272 UINT32 Line)
274 ACPI_DEBUG_MEM_BLOCK *Allocation;
275 ACPI_STATUS Status;
278 Allocation = AcpiUtAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
279 Component, Module, Line);
280 if (!Allocation)
282 /* Report allocation error */
284 ACPI_ERROR ((Module, Line,
285 "Could not allocate size %X", (UINT32) Size));
286 return (NULL);
289 Status = AcpiUtTrackAllocation (Allocation, Size,
290 ACPI_MEM_CALLOC, Component, Module, Line);
291 if (ACPI_FAILURE (Status))
293 AcpiOsFree (Allocation);
294 return (NULL);
297 AcpiGbl_GlobalList->TotalAllocated++;
298 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
299 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
300 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
302 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
305 return ((void *) &Allocation->UserSpace);
309 /*******************************************************************************
311 * FUNCTION: AcpiUtFreeAndTrack
313 * PARAMETERS: Allocation - Address of the memory to deallocate
314 * Component - Component type of caller
315 * Module - Source file name of caller
316 * Line - Line number of caller
318 * RETURN: None
320 * DESCRIPTION: Frees the memory at Allocation
322 ******************************************************************************/
324 void
325 AcpiUtFreeAndTrack (
326 void *Allocation,
327 UINT32 Component,
328 const char *Module,
329 UINT32 Line)
331 ACPI_DEBUG_MEM_BLOCK *DebugBlock;
332 ACPI_STATUS Status;
335 ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
338 if (NULL == Allocation)
340 ACPI_ERROR ((Module, Line,
341 "Attempt to delete a NULL address"));
343 return_VOID;
346 DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
347 (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
349 AcpiGbl_GlobalList->TotalFreed++;
350 AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
352 Status = AcpiUtRemoveAllocation (DebugBlock,
353 Component, Module, Line);
354 if (ACPI_FAILURE (Status))
356 ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
359 AcpiOsFree (DebugBlock);
360 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Allocation));
361 return_VOID;
365 /*******************************************************************************
367 * FUNCTION: AcpiUtFindAllocation
369 * PARAMETERS: Allocation - Address of allocated memory
371 * RETURN: A list element if found; NULL otherwise.
373 * DESCRIPTION: Searches for an element in the global allocation tracking list.
375 ******************************************************************************/
377 static ACPI_DEBUG_MEM_BLOCK *
378 AcpiUtFindAllocation (
379 void *Allocation)
381 ACPI_DEBUG_MEM_BLOCK *Element;
384 ACPI_FUNCTION_ENTRY ();
387 Element = AcpiGbl_GlobalList->ListHead;
389 /* Search for the address. */
391 while (Element)
393 if (Element == Allocation)
395 return (Element);
398 Element = Element->Next;
401 return (NULL);
405 /*******************************************************************************
407 * FUNCTION: AcpiUtTrackAllocation
409 * PARAMETERS: Allocation - Address of allocated memory
410 * Size - Size of the allocation
411 * AllocType - MEM_MALLOC or MEM_CALLOC
412 * Component - Component type of caller
413 * Module - Source file name of caller
414 * Line - Line number of caller
416 * RETURN: None.
418 * DESCRIPTION: Inserts an element into the global allocation tracking list.
420 ******************************************************************************/
422 static ACPI_STATUS
423 AcpiUtTrackAllocation (
424 ACPI_DEBUG_MEM_BLOCK *Allocation,
425 ACPI_SIZE Size,
426 UINT8 AllocType,
427 UINT32 Component,
428 const char *Module,
429 UINT32 Line)
431 ACPI_MEMORY_LIST *MemList;
432 ACPI_DEBUG_MEM_BLOCK *Element;
433 ACPI_STATUS Status = AE_OK;
436 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
439 MemList = AcpiGbl_GlobalList;
440 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
441 if (ACPI_FAILURE (Status))
443 return_ACPI_STATUS (Status);
447 * Search list for this address to make sure it is not already on the list.
448 * This will catch several kinds of problems.
450 Element = AcpiUtFindAllocation (Allocation);
451 if (Element)
453 ACPI_ERROR ((AE_INFO,
454 "UtTrackAllocation: Allocation already present in list! (%p)",
455 Allocation));
457 ACPI_ERROR ((AE_INFO, "Element %p Address %p",
458 Element, Allocation));
460 goto UnlockAndExit;
463 /* Fill in the instance data. */
465 Allocation->Size = (UINT32) Size;
466 Allocation->AllocType = AllocType;
467 Allocation->Component = Component;
468 Allocation->Line = Line;
470 ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
471 Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
473 /* Insert at list head */
475 if (MemList->ListHead)
477 ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
480 Allocation->Next = MemList->ListHead;
481 Allocation->Previous = NULL;
483 MemList->ListHead = Allocation;
486 UnlockAndExit:
487 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
488 return_ACPI_STATUS (Status);
492 /*******************************************************************************
494 * FUNCTION: AcpiUtRemoveAllocation
496 * PARAMETERS: Allocation - Address of allocated memory
497 * Component - Component type of caller
498 * Module - Source file name of caller
499 * Line - Line number of caller
501 * RETURN:
503 * DESCRIPTION: Deletes an element from the global allocation tracking list.
505 ******************************************************************************/
507 static ACPI_STATUS
508 AcpiUtRemoveAllocation (
509 ACPI_DEBUG_MEM_BLOCK *Allocation,
510 UINT32 Component,
511 const char *Module,
512 UINT32 Line)
514 ACPI_MEMORY_LIST *MemList;
515 ACPI_STATUS Status;
518 ACPI_FUNCTION_TRACE (UtRemoveAllocation);
521 MemList = AcpiGbl_GlobalList;
522 if (NULL == MemList->ListHead)
524 /* No allocations! */
526 ACPI_ERROR ((Module, Line,
527 "Empty allocation list, nothing to free!"));
529 return_ACPI_STATUS (AE_OK);
532 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
533 if (ACPI_FAILURE (Status))
535 return_ACPI_STATUS (Status);
538 /* Unlink */
540 if (Allocation->Previous)
542 (Allocation->Previous)->Next = Allocation->Next;
544 else
546 MemList->ListHead = Allocation->Next;
549 if (Allocation->Next)
551 (Allocation->Next)->Previous = Allocation->Previous;
554 /* Mark the segment as deleted */
556 ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
558 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
559 Allocation->Size));
561 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
562 return_ACPI_STATUS (Status);
566 /*******************************************************************************
568 * FUNCTION: AcpiUtDumpAllocationInfo
570 * PARAMETERS:
572 * RETURN: None
574 * DESCRIPTION: Print some info about the outstanding allocations.
576 ******************************************************************************/
578 void
579 AcpiUtDumpAllocationInfo (
580 void)
583 ACPI_MEMORY_LIST *MemList;
586 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo);
589 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
590 ("%30s: %4d (%3d Kb)\n", "Current allocations",
591 MemList->CurrentCount,
592 ROUND_UP_TO_1K (MemList->CurrentSize)));
594 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
595 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
596 MemList->MaxConcurrentCount,
597 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
600 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
601 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
602 RunningObjectCount,
603 ROUND_UP_TO_1K (RunningObjectSize)));
605 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
606 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
607 RunningAllocCount,
608 ROUND_UP_TO_1K (RunningAllocSize)));
611 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
612 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
613 AcpiGbl_CurrentNodeCount,
614 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
616 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
617 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
618 AcpiGbl_MaxConcurrentNodeCount,
619 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
620 sizeof (ACPI_NAMESPACE_NODE)))));
622 return_VOID;
626 /*******************************************************************************
628 * FUNCTION: AcpiUtDumpAllocations
630 * PARAMETERS: Component - Component(s) to dump info for.
631 * Module - Module to dump info for. NULL means all.
633 * RETURN: None
635 * DESCRIPTION: Print a list of all outstanding allocations.
637 ******************************************************************************/
639 void
640 AcpiUtDumpAllocations (
641 UINT32 Component,
642 const char *Module)
644 ACPI_DEBUG_MEM_BLOCK *Element;
645 ACPI_DESCRIPTOR *Descriptor;
646 UINT32 NumOutstanding = 0;
649 ACPI_FUNCTION_TRACE (UtDumpAllocations);
653 * Walk the allocation list.
655 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
657 return;
660 Element = AcpiGbl_GlobalList->ListHead;
661 while (Element)
663 if ((Element->Component & Component) &&
664 ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
666 /* Ignore allocated objects that are in a cache */
668 Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
669 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
671 AcpiOsPrintf ("%p Len %04X %9.9s-%d [%s] ",
672 Descriptor, Element->Size, Element->Module,
673 Element->Line, AcpiUtGetDescriptorName (Descriptor));
675 /* Most of the elements will be Operand objects. */
677 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
679 case ACPI_DESC_TYPE_OPERAND:
680 AcpiOsPrintf ("%12.12s R%hd",
681 AcpiUtGetTypeName (Descriptor->Object.Common.Type),
682 Descriptor->Object.Common.ReferenceCount);
683 break;
685 case ACPI_DESC_TYPE_PARSER:
686 AcpiOsPrintf ("AmlOpcode %04hX",
687 Descriptor->Op.Asl.AmlOpcode);
688 break;
690 case ACPI_DESC_TYPE_NAMED:
691 AcpiOsPrintf ("%4.4s",
692 AcpiUtGetNodeName (&Descriptor->Node));
693 break;
695 default:
696 break;
699 AcpiOsPrintf ( "\n");
700 NumOutstanding++;
703 Element = Element->Next;
706 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
708 /* Print summary */
710 if (!NumOutstanding)
712 ACPI_INFO ((AE_INFO,
713 "No outstanding allocations"));
715 else
717 ACPI_ERROR ((AE_INFO,
718 "%d(%X) Outstanding allocations",
719 NumOutstanding, NumOutstanding));
722 return_VOID;
725 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */