1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2015, 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.
128 #include "accommon.h"
130 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
132 #define _COMPONENT ACPI_UTILITIES
133 ACPI_MODULE_NAME ("uttrack")
136 /* Local prototypes */
138 static ACPI_DEBUG_MEM_BLOCK
*
139 AcpiUtFindAllocation (
140 ACPI_DEBUG_MEM_BLOCK
*Allocation
);
143 AcpiUtTrackAllocation (
144 ACPI_DEBUG_MEM_BLOCK
*Address
,
152 AcpiUtRemoveAllocation (
153 ACPI_DEBUG_MEM_BLOCK
*Address
,
159 /*******************************************************************************
161 * FUNCTION: AcpiUtCreateList
163 * PARAMETERS: CacheName - Ascii name for the cache
164 * ObjectSize - Size of each cached object
165 * ReturnCache - Where the new cache object is returned
169 * DESCRIPTION: Create a local memory list for tracking purposed
171 ******************************************************************************/
177 ACPI_MEMORY_LIST
**ReturnCache
)
179 ACPI_MEMORY_LIST
*Cache
;
182 Cache
= AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST
));
185 return (AE_NO_MEMORY
);
188 memset (Cache
, 0, sizeof (ACPI_MEMORY_LIST
));
190 Cache
->ListName
= ListName
;
191 Cache
->ObjectSize
= ObjectSize
;
193 *ReturnCache
= Cache
;
198 /*******************************************************************************
200 * FUNCTION: AcpiUtAllocateAndTrack
202 * PARAMETERS: Size - Size of the allocation
203 * Component - Component type of caller
204 * Module - Source file name of caller
205 * Line - Line number of caller
207 * RETURN: Address of the allocated memory on success, NULL on failure.
209 * DESCRIPTION: The subsystem's equivalent of malloc.
211 ******************************************************************************/
214 AcpiUtAllocateAndTrack (
220 ACPI_DEBUG_MEM_BLOCK
*Allocation
;
224 /* Check for an inadvertent size of zero bytes */
228 ACPI_WARNING ((Module
, Line
,
229 "Attempt to allocate zero bytes, allocating 1 byte"));
233 Allocation
= AcpiOsAllocate (Size
+ sizeof (ACPI_DEBUG_MEM_HEADER
));
236 /* Report allocation error */
238 ACPI_WARNING ((Module
, Line
,
239 "Could not allocate size %u", (UINT32
) Size
));
244 Status
= AcpiUtTrackAllocation (Allocation
, Size
,
245 ACPI_MEM_MALLOC
, Component
, Module
, Line
);
246 if (ACPI_FAILURE (Status
))
248 AcpiOsFree (Allocation
);
252 AcpiGbl_GlobalList
->TotalAllocated
++;
253 AcpiGbl_GlobalList
->TotalSize
+= (UINT32
) Size
;
254 AcpiGbl_GlobalList
->CurrentTotalSize
+= (UINT32
) Size
;
255 if (AcpiGbl_GlobalList
->CurrentTotalSize
> AcpiGbl_GlobalList
->MaxOccupied
)
257 AcpiGbl_GlobalList
->MaxOccupied
= AcpiGbl_GlobalList
->CurrentTotalSize
;
260 return ((void *) &Allocation
->UserSpace
);
264 /*******************************************************************************
266 * FUNCTION: AcpiUtAllocateZeroedAndTrack
268 * PARAMETERS: Size - Size of the allocation
269 * Component - Component type of caller
270 * Module - Source file name of caller
271 * Line - Line number of caller
273 * RETURN: Address of the allocated memory on success, NULL on failure.
275 * DESCRIPTION: Subsystem equivalent of calloc.
277 ******************************************************************************/
280 AcpiUtAllocateZeroedAndTrack (
286 ACPI_DEBUG_MEM_BLOCK
*Allocation
;
290 /* Check for an inadvertent size of zero bytes */
294 ACPI_WARNING ((Module
, Line
,
295 "Attempt to allocate zero bytes, allocating 1 byte"));
299 Allocation
= AcpiOsAllocateZeroed (Size
+ sizeof (ACPI_DEBUG_MEM_HEADER
));
302 /* Report allocation error */
304 ACPI_ERROR ((Module
, Line
,
305 "Could not allocate size %u", (UINT32
) Size
));
309 Status
= AcpiUtTrackAllocation (Allocation
, Size
,
310 ACPI_MEM_CALLOC
, Component
, Module
, Line
);
311 if (ACPI_FAILURE (Status
))
313 AcpiOsFree (Allocation
);
317 AcpiGbl_GlobalList
->TotalAllocated
++;
318 AcpiGbl_GlobalList
->TotalSize
+= (UINT32
) Size
;
319 AcpiGbl_GlobalList
->CurrentTotalSize
+= (UINT32
) Size
;
320 if (AcpiGbl_GlobalList
->CurrentTotalSize
> AcpiGbl_GlobalList
->MaxOccupied
)
322 AcpiGbl_GlobalList
->MaxOccupied
= AcpiGbl_GlobalList
->CurrentTotalSize
;
325 return ((void *) &Allocation
->UserSpace
);
329 /*******************************************************************************
331 * FUNCTION: AcpiUtFreeAndTrack
333 * PARAMETERS: Allocation - Address of the memory to deallocate
334 * Component - Component type of caller
335 * Module - Source file name of caller
336 * Line - Line number of caller
340 * DESCRIPTION: Frees the memory at Allocation
342 ******************************************************************************/
351 ACPI_DEBUG_MEM_BLOCK
*DebugBlock
;
355 ACPI_FUNCTION_TRACE_PTR (UtFree
, Allocation
);
358 if (NULL
== Allocation
)
360 ACPI_ERROR ((Module
, Line
,
361 "Attempt to delete a NULL address"));
366 DebugBlock
= ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK
,
367 (((char *) Allocation
) - sizeof (ACPI_DEBUG_MEM_HEADER
)));
369 AcpiGbl_GlobalList
->TotalFreed
++;
370 AcpiGbl_GlobalList
->CurrentTotalSize
-= DebugBlock
->Size
;
372 Status
= AcpiUtRemoveAllocation (DebugBlock
,
373 Component
, Module
, Line
);
374 if (ACPI_FAILURE (Status
))
376 ACPI_EXCEPTION ((AE_INFO
, Status
, "Could not free memory"));
379 AcpiOsFree (DebugBlock
);
380 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "%p freed (block %p)\n",
381 Allocation
, DebugBlock
));
386 /*******************************************************************************
388 * FUNCTION: AcpiUtFindAllocation
390 * PARAMETERS: Allocation - Address of allocated memory
392 * RETURN: Three cases:
393 * 1) List is empty, NULL is returned.
394 * 2) Element was found. Returns Allocation parameter.
395 * 3) Element was not found. Returns position where it should be
396 * inserted into the list.
398 * DESCRIPTION: Searches for an element in the global allocation tracking list.
399 * If the element is not found, returns the location within the
400 * list where the element should be inserted.
402 * Note: The list is ordered by larger-to-smaller addresses.
404 * This global list is used to detect memory leaks in ACPICA as
405 * well as other issues such as an attempt to release the same
406 * internal object more than once. Although expensive as far
407 * as cpu time, this list is much more helpful for finding these
408 * types of issues than using memory leak detectors outside of
411 ******************************************************************************/
413 static ACPI_DEBUG_MEM_BLOCK
*
414 AcpiUtFindAllocation (
415 ACPI_DEBUG_MEM_BLOCK
*Allocation
)
417 ACPI_DEBUG_MEM_BLOCK
*Element
;
420 Element
= AcpiGbl_GlobalList
->ListHead
;
427 * Search for the address.
429 * Note: List is ordered by larger-to-smaller addresses, on the
430 * assumption that a new allocation usually has a larger address
431 * than previous allocations.
433 while (Element
> Allocation
)
435 /* Check for end-of-list */
442 Element
= Element
->Next
;
445 if (Element
== Allocation
)
450 return (Element
->Previous
);
454 /*******************************************************************************
456 * FUNCTION: AcpiUtTrackAllocation
458 * PARAMETERS: Allocation - Address of allocated memory
459 * Size - Size of the allocation
460 * AllocType - MEM_MALLOC or MEM_CALLOC
461 * Component - Component type of caller
462 * Module - Source file name of caller
463 * Line - Line number of caller
467 * DESCRIPTION: Inserts an element into the global allocation tracking list.
469 ******************************************************************************/
472 AcpiUtTrackAllocation (
473 ACPI_DEBUG_MEM_BLOCK
*Allocation
,
480 ACPI_MEMORY_LIST
*MemList
;
481 ACPI_DEBUG_MEM_BLOCK
*Element
;
482 ACPI_STATUS Status
= AE_OK
;
485 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation
, Allocation
);
488 if (AcpiGbl_DisableMemTracking
)
490 return_ACPI_STATUS (AE_OK
);
493 MemList
= AcpiGbl_GlobalList
;
494 Status
= AcpiUtAcquireMutex (ACPI_MTX_MEMORY
);
495 if (ACPI_FAILURE (Status
))
497 return_ACPI_STATUS (Status
);
501 * Search the global list for this address to make sure it is not
502 * already present. This will catch several kinds of problems.
504 Element
= AcpiUtFindAllocation (Allocation
);
505 if (Element
== Allocation
)
507 ACPI_ERROR ((AE_INFO
,
508 "UtTrackAllocation: Allocation (%p) already present in global list!",
513 /* Fill in the instance data */
515 Allocation
->Size
= (UINT32
) Size
;
516 Allocation
->AllocType
= AllocType
;
517 Allocation
->Component
= Component
;
518 Allocation
->Line
= Line
;
520 strncpy (Allocation
->Module
, Module
, ACPI_MAX_MODULE_NAME
);
521 Allocation
->Module
[ACPI_MAX_MODULE_NAME
-1] = 0;
525 /* Insert at list head */
527 if (MemList
->ListHead
)
529 ((ACPI_DEBUG_MEM_BLOCK
*)(MemList
->ListHead
))->Previous
= Allocation
;
532 Allocation
->Next
= MemList
->ListHead
;
533 Allocation
->Previous
= NULL
;
535 MemList
->ListHead
= Allocation
;
539 /* Insert after element */
541 Allocation
->Next
= Element
->Next
;
542 Allocation
->Previous
= Element
;
546 (Element
->Next
)->Previous
= Allocation
;
549 Element
->Next
= Allocation
;
554 Status
= AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
555 return_ACPI_STATUS (Status
);
559 /*******************************************************************************
561 * FUNCTION: AcpiUtRemoveAllocation
563 * PARAMETERS: Allocation - Address of allocated memory
564 * Component - Component type of caller
565 * Module - Source file name of caller
566 * Line - Line number of caller
570 * DESCRIPTION: Deletes an element from the global allocation tracking list.
572 ******************************************************************************/
575 AcpiUtRemoveAllocation (
576 ACPI_DEBUG_MEM_BLOCK
*Allocation
,
581 ACPI_MEMORY_LIST
*MemList
;
585 ACPI_FUNCTION_NAME (UtRemoveAllocation
);
588 if (AcpiGbl_DisableMemTracking
)
593 MemList
= AcpiGbl_GlobalList
;
594 if (NULL
== MemList
->ListHead
)
596 /* No allocations! */
598 ACPI_ERROR ((Module
, Line
,
599 "Empty allocation list, nothing to free!"));
604 Status
= AcpiUtAcquireMutex (ACPI_MTX_MEMORY
);
605 if (ACPI_FAILURE (Status
))
612 if (Allocation
->Previous
)
614 (Allocation
->Previous
)->Next
= Allocation
->Next
;
618 MemList
->ListHead
= Allocation
->Next
;
621 if (Allocation
->Next
)
623 (Allocation
->Next
)->Previous
= Allocation
->Previous
;
626 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Freeing %p, size 0%X\n",
627 &Allocation
->UserSpace
, Allocation
->Size
));
629 /* Mark the segment as deleted */
631 memset (&Allocation
->UserSpace
, 0xEA, Allocation
->Size
);
633 Status
= AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
638 /*******************************************************************************
640 * FUNCTION: AcpiUtDumpAllocationInfo
646 * DESCRIPTION: Print some info about the outstanding allocations.
648 ******************************************************************************/
651 AcpiUtDumpAllocationInfo (
655 ACPI_MEMORY_LIST *MemList;
658 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo
);
661 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
662 ("%30s: %4d (%3d Kb)\n", "Current allocations",
663 MemList->CurrentCount,
664 ROUND_UP_TO_1K (MemList->CurrentSize)));
666 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
667 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
668 MemList->MaxConcurrentCount,
669 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
672 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
673 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
675 ROUND_UP_TO_1K (RunningObjectSize)));
677 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
678 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
680 ROUND_UP_TO_1K (RunningAllocSize)));
683 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
684 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
685 AcpiGbl_CurrentNodeCount,
686 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
688 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
689 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
690 AcpiGbl_MaxConcurrentNodeCount,
691 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
692 sizeof (ACPI_NAMESPACE_NODE)))));
698 /*******************************************************************************
700 * FUNCTION: AcpiUtDumpAllocations
702 * PARAMETERS: Component - Component(s) to dump info for.
703 * Module - Module to dump info for. NULL means all.
707 * DESCRIPTION: Print a list of all outstanding allocations.
709 ******************************************************************************/
712 AcpiUtDumpAllocations (
716 ACPI_DEBUG_MEM_BLOCK
*Element
;
717 ACPI_DESCRIPTOR
*Descriptor
;
718 UINT32 NumOutstanding
= 0;
719 UINT8 DescriptorType
;
722 ACPI_FUNCTION_TRACE (UtDumpAllocations
);
725 if (AcpiGbl_DisableMemTracking
)
731 * Walk the allocation list.
733 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY
)))
738 Element
= AcpiGbl_GlobalList
->ListHead
;
741 if ((Element
->Component
& Component
) &&
742 ((Module
== NULL
) || (0 == strcmp (Module
, Element
->Module
))))
744 Descriptor
= ACPI_CAST_PTR (ACPI_DESCRIPTOR
, &Element
->UserSpace
);
746 if (Element
->Size
< sizeof (ACPI_COMMON_DESCRIPTOR
))
748 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
749 "[Not a Descriptor - too small]\n",
750 Descriptor
, Element
->Size
, Element
->Module
,
755 /* Ignore allocated objects that are in a cache */
757 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor
) != ACPI_DESC_TYPE_CACHED
)
759 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
760 Descriptor
, Element
->Size
, Element
->Module
,
761 Element
->Line
, AcpiUtGetDescriptorName (Descriptor
));
763 /* Validate the descriptor type using Type field and length */
765 DescriptorType
= 0; /* Not a valid descriptor type */
767 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor
))
769 case ACPI_DESC_TYPE_OPERAND
:
771 if (Element
->Size
== sizeof (ACPI_OPERAND_OBJECT
))
773 DescriptorType
= ACPI_DESC_TYPE_OPERAND
;
777 case ACPI_DESC_TYPE_PARSER
:
779 if (Element
->Size
== sizeof (ACPI_PARSE_OBJECT
))
781 DescriptorType
= ACPI_DESC_TYPE_PARSER
;
785 case ACPI_DESC_TYPE_NAMED
:
787 if (Element
->Size
== sizeof (ACPI_NAMESPACE_NODE
))
789 DescriptorType
= ACPI_DESC_TYPE_NAMED
;
798 /* Display additional info for the major descriptor types */
800 switch (DescriptorType
)
802 case ACPI_DESC_TYPE_OPERAND
:
804 AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
805 AcpiUtGetTypeName (Descriptor
->Object
.Common
.Type
),
806 Descriptor
->Object
.Common
.ReferenceCount
);
809 case ACPI_DESC_TYPE_PARSER
:
811 AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
812 Descriptor
->Op
.Asl
.AmlOpcode
);
815 case ACPI_DESC_TYPE_NAMED
:
817 AcpiOsPrintf ("%4.4s\n",
818 AcpiUtGetNodeName (&Descriptor
->Node
));
823 AcpiOsPrintf ( "\n");
832 Element
= Element
->Next
;
835 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
841 ACPI_INFO ((AE_INFO
, "No outstanding allocations"));
845 ACPI_ERROR ((AE_INFO
, "%u(0x%X) Outstanding allocations",
846 NumOutstanding
, NumOutstanding
));
852 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */