1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2012, Intel Corp.
12 * All rights reserved.
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
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
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
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
72 * 3.4. Intel retains all right, title, and interest in and to the Original
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
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
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__
130 #include "accommon.h"
132 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
134 #define _COMPONENT ACPI_UTILITIES
135 ACPI_MODULE_NAME ("uttrack")
138 /* Local prototypes */
140 static ACPI_DEBUG_MEM_BLOCK
*
141 AcpiUtFindAllocation (
142 ACPI_DEBUG_MEM_BLOCK
*Allocation
);
145 AcpiUtTrackAllocation (
146 ACPI_DEBUG_MEM_BLOCK
*Address
,
154 AcpiUtRemoveAllocation (
155 ACPI_DEBUG_MEM_BLOCK
*Address
,
161 /*******************************************************************************
163 * FUNCTION: AcpiUtCreateList
165 * PARAMETERS: CacheName - Ascii name for the cache
166 * ObjectSize - Size of each cached object
167 * ReturnCache - Where the new cache object is returned
171 * DESCRIPTION: Create a local memory list for tracking purposed
173 ******************************************************************************/
179 ACPI_MEMORY_LIST
**ReturnCache
)
181 ACPI_MEMORY_LIST
*Cache
;
184 Cache
= AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST
));
187 return (AE_NO_MEMORY
);
190 ACPI_MEMSET (Cache
, 0, sizeof (ACPI_MEMORY_LIST
));
192 Cache
->ListName
= ListName
;
193 Cache
->ObjectSize
= ObjectSize
;
195 *ReturnCache
= Cache
;
200 /*******************************************************************************
202 * FUNCTION: AcpiUtAllocateAndTrack
204 * PARAMETERS: Size - Size of the allocation
205 * Component - Component type of caller
206 * Module - Source file name of caller
207 * Line - Line number of caller
209 * RETURN: Address of the allocated memory on success, NULL on failure.
211 * DESCRIPTION: The subsystem's equivalent of malloc.
213 ******************************************************************************/
216 AcpiUtAllocateAndTrack (
222 ACPI_DEBUG_MEM_BLOCK
*Allocation
;
226 Allocation
= AcpiUtAllocate (Size
+ sizeof (ACPI_DEBUG_MEM_HEADER
),
227 Component
, Module
, Line
);
233 Status
= AcpiUtTrackAllocation (Allocation
, Size
,
234 ACPI_MEM_MALLOC
, Component
, Module
, Line
);
235 if (ACPI_FAILURE (Status
))
237 AcpiOsFree (Allocation
);
241 AcpiGbl_GlobalList
->TotalAllocated
++;
242 AcpiGbl_GlobalList
->TotalSize
+= (UINT32
) Size
;
243 AcpiGbl_GlobalList
->CurrentTotalSize
+= (UINT32
) Size
;
244 if (AcpiGbl_GlobalList
->CurrentTotalSize
> AcpiGbl_GlobalList
->MaxOccupied
)
246 AcpiGbl_GlobalList
->MaxOccupied
= AcpiGbl_GlobalList
->CurrentTotalSize
;
249 return ((void *) &Allocation
->UserSpace
);
253 /*******************************************************************************
255 * FUNCTION: AcpiUtAllocateZeroedAndTrack
257 * PARAMETERS: Size - Size of the allocation
258 * Component - Component type of caller
259 * Module - Source file name of caller
260 * Line - Line number of caller
262 * RETURN: Address of the allocated memory on success, NULL on failure.
264 * DESCRIPTION: Subsystem equivalent of calloc.
266 ******************************************************************************/
269 AcpiUtAllocateZeroedAndTrack (
275 ACPI_DEBUG_MEM_BLOCK
*Allocation
;
279 Allocation
= AcpiUtAllocateZeroed (Size
+ sizeof (ACPI_DEBUG_MEM_HEADER
),
280 Component
, Module
, Line
);
283 /* Report allocation error */
285 ACPI_ERROR ((Module
, Line
,
286 "Could not allocate size %u", (UINT32
) Size
));
290 Status
= AcpiUtTrackAllocation (Allocation
, Size
,
291 ACPI_MEM_CALLOC
, Component
, Module
, Line
);
292 if (ACPI_FAILURE (Status
))
294 AcpiOsFree (Allocation
);
298 AcpiGbl_GlobalList
->TotalAllocated
++;
299 AcpiGbl_GlobalList
->TotalSize
+= (UINT32
) Size
;
300 AcpiGbl_GlobalList
->CurrentTotalSize
+= (UINT32
) Size
;
301 if (AcpiGbl_GlobalList
->CurrentTotalSize
> AcpiGbl_GlobalList
->MaxOccupied
)
303 AcpiGbl_GlobalList
->MaxOccupied
= AcpiGbl_GlobalList
->CurrentTotalSize
;
306 return ((void *) &Allocation
->UserSpace
);
310 /*******************************************************************************
312 * FUNCTION: AcpiUtFreeAndTrack
314 * PARAMETERS: Allocation - Address of the memory to deallocate
315 * Component - Component type of caller
316 * Module - Source file name of caller
317 * Line - Line number of caller
321 * DESCRIPTION: Frees the memory at Allocation
323 ******************************************************************************/
332 ACPI_DEBUG_MEM_BLOCK
*DebugBlock
;
336 ACPI_FUNCTION_TRACE_PTR (UtFree
, Allocation
);
339 if (NULL
== Allocation
)
341 ACPI_ERROR ((Module
, Line
,
342 "Attempt to delete a NULL address"));
347 DebugBlock
= ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK
,
348 (((char *) Allocation
) - sizeof (ACPI_DEBUG_MEM_HEADER
)));
350 AcpiGbl_GlobalList
->TotalFreed
++;
351 AcpiGbl_GlobalList
->CurrentTotalSize
-= DebugBlock
->Size
;
353 Status
= AcpiUtRemoveAllocation (DebugBlock
,
354 Component
, Module
, Line
);
355 if (ACPI_FAILURE (Status
))
357 ACPI_EXCEPTION ((AE_INFO
, Status
, "Could not free memory"));
360 AcpiOsFree (DebugBlock
);
361 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "%p freed\n", Allocation
));
366 /*******************************************************************************
368 * FUNCTION: AcpiUtFindAllocation
370 * PARAMETERS: Allocation - Address of allocated memory
372 * RETURN: Three cases:
373 * 1) List is empty, NULL is returned.
374 * 2) Element was found. Returns Allocation parameter.
375 * 3) Element was not found. Returns position where it should be
376 * inserted into the list.
378 * DESCRIPTION: Searches for an element in the global allocation tracking list.
379 * If the element is not found, returns the location within the
380 * list where the element should be inserted.
382 * Note: The list is ordered by larger-to-smaller addresses.
384 * This global list is used to detect memory leaks in ACPICA as
385 * well as other issues such as an attempt to release the same
386 * internal object more than once. Although expensive as far
387 * as cpu time, this list is much more helpful for finding these
388 * types of issues than using memory leak detectors outside of
391 ******************************************************************************/
393 static ACPI_DEBUG_MEM_BLOCK
*
394 AcpiUtFindAllocation (
395 ACPI_DEBUG_MEM_BLOCK
*Allocation
)
397 ACPI_DEBUG_MEM_BLOCK
*Element
;
400 Element
= AcpiGbl_GlobalList
->ListHead
;
407 * Search for the address.
409 * Note: List is ordered by larger-to-smaller addresses, on the
410 * assumption that a new allocation usually has a larger address
411 * than previous allocations.
413 while (Element
> Allocation
)
415 /* Check for end-of-list */
422 Element
= Element
->Next
;
425 if (Element
== Allocation
)
430 return (Element
->Previous
);
434 /*******************************************************************************
436 * FUNCTION: AcpiUtTrackAllocation
438 * PARAMETERS: Allocation - Address of allocated memory
439 * Size - Size of the allocation
440 * AllocType - MEM_MALLOC or MEM_CALLOC
441 * Component - Component type of caller
442 * Module - Source file name of caller
443 * Line - Line number of caller
447 * DESCRIPTION: Inserts an element into the global allocation tracking list.
449 ******************************************************************************/
452 AcpiUtTrackAllocation (
453 ACPI_DEBUG_MEM_BLOCK
*Allocation
,
460 ACPI_MEMORY_LIST
*MemList
;
461 ACPI_DEBUG_MEM_BLOCK
*Element
;
462 ACPI_STATUS Status
= AE_OK
;
465 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation
, Allocation
);
468 if (AcpiGbl_DisableMemTracking
)
470 return_ACPI_STATUS (AE_OK
);
473 MemList
= AcpiGbl_GlobalList
;
474 Status
= AcpiUtAcquireMutex (ACPI_MTX_MEMORY
);
475 if (ACPI_FAILURE (Status
))
477 return_ACPI_STATUS (Status
);
481 * Search the global list for this address to make sure it is not
482 * already present. This will catch several kinds of problems.
484 Element
= AcpiUtFindAllocation (Allocation
);
485 if (Element
== Allocation
)
487 ACPI_ERROR ((AE_INFO
,
488 "UtTrackAllocation: Allocation (%p) already present in global list!",
493 /* Fill in the instance data */
495 Allocation
->Size
= (UINT32
) Size
;
496 Allocation
->AllocType
= AllocType
;
497 Allocation
->Component
= Component
;
498 Allocation
->Line
= Line
;
500 ACPI_STRNCPY (Allocation
->Module
, Module
, ACPI_MAX_MODULE_NAME
);
501 Allocation
->Module
[ACPI_MAX_MODULE_NAME
-1] = 0;
505 /* Insert at list head */
507 if (MemList
->ListHead
)
509 ((ACPI_DEBUG_MEM_BLOCK
*)(MemList
->ListHead
))->Previous
= Allocation
;
512 Allocation
->Next
= MemList
->ListHead
;
513 Allocation
->Previous
= NULL
;
515 MemList
->ListHead
= Allocation
;
519 /* Insert after element */
521 Allocation
->Next
= Element
->Next
;
522 Allocation
->Previous
= Element
;
526 (Element
->Next
)->Previous
= Allocation
;
529 Element
->Next
= Allocation
;
534 Status
= AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
535 return_ACPI_STATUS (Status
);
539 /*******************************************************************************
541 * FUNCTION: AcpiUtRemoveAllocation
543 * PARAMETERS: Allocation - Address of allocated memory
544 * Component - Component type of caller
545 * Module - Source file name of caller
546 * Line - Line number of caller
550 * DESCRIPTION: Deletes an element from the global allocation tracking list.
552 ******************************************************************************/
555 AcpiUtRemoveAllocation (
556 ACPI_DEBUG_MEM_BLOCK
*Allocation
,
561 ACPI_MEMORY_LIST
*MemList
;
565 ACPI_FUNCTION_TRACE (UtRemoveAllocation
);
568 if (AcpiGbl_DisableMemTracking
)
570 return_ACPI_STATUS (AE_OK
);
573 MemList
= AcpiGbl_GlobalList
;
574 if (NULL
== MemList
->ListHead
)
576 /* No allocations! */
578 ACPI_ERROR ((Module
, Line
,
579 "Empty allocation list, nothing to free!"));
581 return_ACPI_STATUS (AE_OK
);
584 Status
= AcpiUtAcquireMutex (ACPI_MTX_MEMORY
);
585 if (ACPI_FAILURE (Status
))
587 return_ACPI_STATUS (Status
);
592 if (Allocation
->Previous
)
594 (Allocation
->Previous
)->Next
= Allocation
->Next
;
598 MemList
->ListHead
= Allocation
->Next
;
601 if (Allocation
->Next
)
603 (Allocation
->Next
)->Previous
= Allocation
->Previous
;
606 /* Mark the segment as deleted */
608 ACPI_MEMSET (&Allocation
->UserSpace
, 0xEA, Allocation
->Size
);
610 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Freeing size 0%X\n",
613 Status
= AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
614 return_ACPI_STATUS (Status
);
618 /*******************************************************************************
620 * FUNCTION: AcpiUtDumpAllocationInfo
626 * DESCRIPTION: Print some info about the outstanding allocations.
628 ******************************************************************************/
631 AcpiUtDumpAllocationInfo (
635 ACPI_MEMORY_LIST *MemList;
638 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo
);
641 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
642 ("%30s: %4d (%3d Kb)\n", "Current allocations",
643 MemList->CurrentCount,
644 ROUND_UP_TO_1K (MemList->CurrentSize)));
646 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
647 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
648 MemList->MaxConcurrentCount,
649 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
652 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
653 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
655 ROUND_UP_TO_1K (RunningObjectSize)));
657 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
658 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
660 ROUND_UP_TO_1K (RunningAllocSize)));
663 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
664 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
665 AcpiGbl_CurrentNodeCount,
666 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
668 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
669 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
670 AcpiGbl_MaxConcurrentNodeCount,
671 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
672 sizeof (ACPI_NAMESPACE_NODE)))));
678 /*******************************************************************************
680 * FUNCTION: AcpiUtDumpAllocations
682 * PARAMETERS: Component - Component(s) to dump info for.
683 * Module - Module to dump info for. NULL means all.
687 * DESCRIPTION: Print a list of all outstanding allocations.
689 ******************************************************************************/
692 AcpiUtDumpAllocations (
696 ACPI_DEBUG_MEM_BLOCK
*Element
;
697 ACPI_DESCRIPTOR
*Descriptor
;
698 UINT32 NumOutstanding
= 0;
699 UINT8 DescriptorType
;
702 ACPI_FUNCTION_TRACE (UtDumpAllocations
);
705 if (AcpiGbl_DisableMemTracking
)
711 * Walk the allocation list.
713 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY
)))
718 Element
= AcpiGbl_GlobalList
->ListHead
;
721 if ((Element
->Component
& Component
) &&
722 ((Module
== NULL
) || (0 == ACPI_STRCMP (Module
, Element
->Module
))))
724 Descriptor
= ACPI_CAST_PTR (ACPI_DESCRIPTOR
, &Element
->UserSpace
);
726 if (Element
->Size
< sizeof (ACPI_COMMON_DESCRIPTOR
))
728 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
729 "[Not a Descriptor - too small]\n",
730 Descriptor
, Element
->Size
, Element
->Module
,
735 /* Ignore allocated objects that are in a cache */
737 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor
) != ACPI_DESC_TYPE_CACHED
)
739 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
740 Descriptor
, Element
->Size
, Element
->Module
,
741 Element
->Line
, AcpiUtGetDescriptorName (Descriptor
));
743 /* Validate the descriptor type using Type field and length */
745 DescriptorType
= 0; /* Not a valid descriptor type */
747 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor
))
749 case ACPI_DESC_TYPE_OPERAND
:
750 if (Element
->Size
== sizeof (ACPI_OPERAND_OBJECT
))
752 DescriptorType
= ACPI_DESC_TYPE_OPERAND
;
756 case ACPI_DESC_TYPE_PARSER
:
757 if (Element
->Size
== sizeof (ACPI_PARSE_OBJECT
))
759 DescriptorType
= ACPI_DESC_TYPE_PARSER
;
763 case ACPI_DESC_TYPE_NAMED
:
764 if (Element
->Size
== sizeof (ACPI_NAMESPACE_NODE
))
766 DescriptorType
= ACPI_DESC_TYPE_NAMED
;
774 /* Display additional info for the major descriptor types */
776 switch (DescriptorType
)
778 case ACPI_DESC_TYPE_OPERAND
:
779 AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
780 AcpiUtGetTypeName (Descriptor
->Object
.Common
.Type
),
781 Descriptor
->Object
.Common
.ReferenceCount
);
784 case ACPI_DESC_TYPE_PARSER
:
785 AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
786 Descriptor
->Op
.Asl
.AmlOpcode
);
789 case ACPI_DESC_TYPE_NAMED
:
790 AcpiOsPrintf ("%4.4s\n",
791 AcpiUtGetNodeName (&Descriptor
->Node
));
795 AcpiOsPrintf ( "\n");
804 Element
= Element
->Next
;
807 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
813 ACPI_INFO ((AE_INFO
, "No outstanding allocations"));
817 ACPI_ERROR ((AE_INFO
, "%u(0x%X) Outstanding allocations",
818 NumOutstanding
, NumOutstanding
));
824 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */