i386/identcpu.c: Add VIA Nano support
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / utilities / uttrack.c
blob4dbf0bf755b6969bda0b4f327e0a45d0a65e51ad
1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
4 * $Revision: 1.3 $
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13 * All rights reserved.
15 * 2. License
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.
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
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;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
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.
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.
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.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
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.
81 * 4. Disclaimer and Export Compliance
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.
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.
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.
115 *****************************************************************************/
118 * These procedures are used for tracking memory leaks in the subsystem, and
119 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
121 * Each memory allocation is tracked via a doubly linked list. Each
122 * element contains the caller's component, module name, function name, and
123 * line number. AcpiUtAllocate and AcpiUtAllocateZeroed call
124 * AcpiUtTrackAllocation to add an element to the list; deletion
125 * occurs in the body of AcpiUtFree.
128 #define __UTTRACK_C__
130 #include "acpi.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 char *Module,
150 UINT32 Line);
152 static ACPI_STATUS
153 AcpiUtRemoveAllocation (
154 ACPI_DEBUG_MEM_BLOCK *Address,
155 UINT32 Component,
156 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 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 = 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 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->CurrentTotalSize += (UINT32) Size;
243 return ((void *) &Allocation->UserSpace);
247 /*******************************************************************************
249 * FUNCTION: AcpiUtAllocateZeroedAndTrack
251 * PARAMETERS: Size - Size of the allocation
252 * Component - Component type of caller
253 * Module - Source file name of caller
254 * Line - Line number of caller
256 * RETURN: Address of the allocated memory on success, NULL on failure.
258 * DESCRIPTION: Subsystem equivalent of calloc.
260 ******************************************************************************/
262 void *
263 AcpiUtAllocateZeroedAndTrack (
264 ACPI_SIZE Size,
265 UINT32 Component,
266 char *Module,
267 UINT32 Line)
269 ACPI_DEBUG_MEM_BLOCK *Allocation;
270 ACPI_STATUS Status;
273 Allocation = AcpiUtAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
274 Component, Module, Line);
275 if (!Allocation)
277 /* Report allocation error */
279 ACPI_ERROR ((Module, Line,
280 "Could not allocate size %X", (UINT32) Size));
281 return (NULL);
284 Status = AcpiUtTrackAllocation (Allocation, Size,
285 ACPI_MEM_CALLOC, Component, Module, Line);
286 if (ACPI_FAILURE (Status))
288 AcpiOsFree (Allocation);
289 return (NULL);
292 AcpiGbl_GlobalList->TotalAllocated++;
293 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
295 return ((void *) &Allocation->UserSpace);
299 /*******************************************************************************
301 * FUNCTION: AcpiUtFreeAndTrack
303 * PARAMETERS: Allocation - Address of the memory to deallocate
304 * Component - Component type of caller
305 * Module - Source file name of caller
306 * Line - Line number of caller
308 * RETURN: None
310 * DESCRIPTION: Frees the memory at Allocation
312 ******************************************************************************/
314 void
315 AcpiUtFreeAndTrack (
316 void *Allocation,
317 UINT32 Component,
318 char *Module,
319 UINT32 Line)
321 ACPI_DEBUG_MEM_BLOCK *DebugBlock;
322 ACPI_STATUS Status;
325 ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
328 if (NULL == Allocation)
330 ACPI_ERROR ((Module, Line,
331 "Attempt to delete a NULL address"));
333 return_VOID;
336 DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
337 (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
339 AcpiGbl_GlobalList->TotalFreed++;
340 AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
342 Status = AcpiUtRemoveAllocation (DebugBlock,
343 Component, Module, Line);
344 if (ACPI_FAILURE (Status))
346 ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
349 AcpiOsFree (DebugBlock);
350 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Allocation));
351 return_VOID;
355 /*******************************************************************************
357 * FUNCTION: AcpiUtFindAllocation
359 * PARAMETERS: Allocation - Address of allocated memory
361 * RETURN: A list element if found; NULL otherwise.
363 * DESCRIPTION: Searches for an element in the global allocation tracking list.
365 ******************************************************************************/
367 static ACPI_DEBUG_MEM_BLOCK *
368 AcpiUtFindAllocation (
369 void *Allocation)
371 ACPI_DEBUG_MEM_BLOCK *Element;
374 ACPI_FUNCTION_ENTRY ();
377 Element = AcpiGbl_GlobalList->ListHead;
379 /* Search for the address. */
381 while (Element)
383 if (Element == Allocation)
385 return (Element);
388 Element = Element->Next;
391 return (NULL);
395 /*******************************************************************************
397 * FUNCTION: AcpiUtTrackAllocation
399 * PARAMETERS: Allocation - Address of allocated memory
400 * Size - Size of the allocation
401 * AllocType - MEM_MALLOC or MEM_CALLOC
402 * Component - Component type of caller
403 * Module - Source file name of caller
404 * Line - Line number of caller
406 * RETURN: None.
408 * DESCRIPTION: Inserts an element into the global allocation tracking list.
410 ******************************************************************************/
412 static ACPI_STATUS
413 AcpiUtTrackAllocation (
414 ACPI_DEBUG_MEM_BLOCK *Allocation,
415 ACPI_SIZE Size,
416 UINT8 AllocType,
417 UINT32 Component,
418 char *Module,
419 UINT32 Line)
421 ACPI_MEMORY_LIST *MemList;
422 ACPI_DEBUG_MEM_BLOCK *Element;
423 ACPI_STATUS Status = AE_OK;
426 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
429 MemList = AcpiGbl_GlobalList;
430 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
431 if (ACPI_FAILURE (Status))
433 return_ACPI_STATUS (Status);
437 * Search list for this address to make sure it is not already on the list.
438 * This will catch several kinds of problems.
440 Element = AcpiUtFindAllocation (Allocation);
441 if (Element)
443 ACPI_ERROR ((AE_INFO,
444 "UtTrackAllocation: Allocation already present in list! (%p)",
445 Allocation));
447 ACPI_ERROR ((AE_INFO, "Element %p Address %p",
448 Element, Allocation));
450 goto UnlockAndExit;
453 /* Fill in the instance data. */
455 Allocation->Size = (UINT32) Size;
456 Allocation->AllocType = AllocType;
457 Allocation->Component = Component;
458 Allocation->Line = Line;
460 ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
461 Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
463 /* Insert at list head */
465 if (MemList->ListHead)
467 ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
470 Allocation->Next = MemList->ListHead;
471 Allocation->Previous = NULL;
473 MemList->ListHead = Allocation;
476 UnlockAndExit:
477 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
478 return_ACPI_STATUS (Status);
482 /*******************************************************************************
484 * FUNCTION: AcpiUtRemoveAllocation
486 * PARAMETERS: Allocation - Address of allocated memory
487 * Component - Component type of caller
488 * Module - Source file name of caller
489 * Line - Line number of caller
491 * RETURN:
493 * DESCRIPTION: Deletes an element from the global allocation tracking list.
495 ******************************************************************************/
497 static ACPI_STATUS
498 AcpiUtRemoveAllocation (
499 ACPI_DEBUG_MEM_BLOCK *Allocation,
500 UINT32 Component,
501 char *Module,
502 UINT32 Line)
504 ACPI_MEMORY_LIST *MemList;
505 ACPI_STATUS Status;
508 ACPI_FUNCTION_TRACE (UtRemoveAllocation);
511 MemList = AcpiGbl_GlobalList;
512 if (NULL == MemList->ListHead)
514 /* No allocations! */
516 ACPI_ERROR ((Module, Line,
517 "Empty allocation list, nothing to free!"));
519 return_ACPI_STATUS (AE_OK);
522 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
523 if (ACPI_FAILURE (Status))
525 return_ACPI_STATUS (Status);
528 /* Unlink */
530 if (Allocation->Previous)
532 (Allocation->Previous)->Next = Allocation->Next;
534 else
536 MemList->ListHead = Allocation->Next;
539 if (Allocation->Next)
541 (Allocation->Next)->Previous = Allocation->Previous;
544 /* Mark the segment as deleted */
546 ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
548 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
549 Allocation->Size));
551 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
552 return_ACPI_STATUS (Status);
556 /*******************************************************************************
558 * FUNCTION: AcpiUtDumpAllocationInfo
560 * PARAMETERS:
562 * RETURN: None
564 * DESCRIPTION: Print some info about the outstanding allocations.
566 ******************************************************************************/
568 void
569 AcpiUtDumpAllocationInfo (
570 void)
573 ACPI_MEMORY_LIST *MemList;
576 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo);
579 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
580 ("%30s: %4d (%3d Kb)\n", "Current allocations",
581 MemList->CurrentCount,
582 ROUND_UP_TO_1K (MemList->CurrentSize)));
584 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
585 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
586 MemList->MaxConcurrentCount,
587 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
590 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
591 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
592 RunningObjectCount,
593 ROUND_UP_TO_1K (RunningObjectSize)));
595 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
596 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
597 RunningAllocCount,
598 ROUND_UP_TO_1K (RunningAllocSize)));
601 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
602 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
603 AcpiGbl_CurrentNodeCount,
604 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
606 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
607 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
608 AcpiGbl_MaxConcurrentNodeCount,
609 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
610 sizeof (ACPI_NAMESPACE_NODE)))));
612 return_VOID;
616 /*******************************************************************************
618 * FUNCTION: AcpiUtDumpAllocations
620 * PARAMETERS: Component - Component(s) to dump info for.
621 * Module - Module to dump info for. NULL means all.
623 * RETURN: None
625 * DESCRIPTION: Print a list of all outstanding allocations.
627 ******************************************************************************/
629 void
630 AcpiUtDumpAllocations (
631 UINT32 Component,
632 char *Module)
634 ACPI_DEBUG_MEM_BLOCK *Element;
635 ACPI_DESCRIPTOR *Descriptor;
636 UINT32 NumOutstanding = 0;
639 ACPI_FUNCTION_TRACE (UtDumpAllocations);
643 * Walk the allocation list.
645 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
647 return;
650 Element = AcpiGbl_GlobalList->ListHead;
651 while (Element)
653 if ((Element->Component & Component) &&
654 ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
656 /* Ignore allocated objects that are in a cache */
658 Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
659 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
661 AcpiOsPrintf ("%p Len %04X %9.9s-%d [%s] ",
662 Descriptor, Element->Size, Element->Module,
663 Element->Line, AcpiUtGetDescriptorName (Descriptor));
665 /* Most of the elements will be Operand objects. */
667 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
669 case ACPI_DESC_TYPE_OPERAND:
670 AcpiOsPrintf ("%12.12s R%hd",
671 AcpiUtGetTypeName (Descriptor->Object.Common.Type),
672 Descriptor->Object.Common.ReferenceCount);
673 break;
675 case ACPI_DESC_TYPE_PARSER:
676 AcpiOsPrintf ("AmlOpcode %04hX",
677 Descriptor->Op.Asl.AmlOpcode);
678 break;
680 case ACPI_DESC_TYPE_NAMED:
681 AcpiOsPrintf ("%4.4s",
682 AcpiUtGetNodeName (&Descriptor->Node));
683 break;
685 default:
686 break;
689 AcpiOsPrintf ( "\n");
690 NumOutstanding++;
693 Element = Element->Next;
696 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
698 /* Print summary */
700 if (!NumOutstanding)
702 ACPI_INFO ((AE_INFO,
703 "No outstanding allocations"));
705 else
707 ACPI_ERROR ((AE_INFO,
708 "%d(%X) Outstanding allocations",
709 NumOutstanding, NumOutstanding));
712 return_VOID;
715 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */