1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 * These procedures are used for tracking memory leaks in the subsystem, and
46 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
48 * Each memory allocation is tracked via a doubly linked list. Each
49 * element contains the caller's component, module name, function name, and
50 * line number. AcpiUtAllocate and AcpiUtAllocateZeroed call
51 * AcpiUtTrackAllocation to add an element to the list; deletion
52 * occurs in the body of AcpiUtFree.
55 #include <contrib/dev/acpica/include/acpi.h>
56 #include <contrib/dev/acpica/include/accommon.h>
58 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
60 #define _COMPONENT ACPI_UTILITIES
61 ACPI_MODULE_NAME ("uttrack")
64 /* Local prototypes */
66 static ACPI_DEBUG_MEM_BLOCK *
67 AcpiUtFindAllocation (
68 ACPI_DEBUG_MEM_BLOCK *Allocation);
71 AcpiUtTrackAllocation (
72 ACPI_DEBUG_MEM_BLOCK *Address,
80 AcpiUtRemoveAllocation (
81 ACPI_DEBUG_MEM_BLOCK *Address,
87 /*******************************************************************************
89 * FUNCTION: AcpiUtCreateList
91 * PARAMETERS: CacheName - Ascii name for the cache
92 * ObjectSize - Size of each cached object
93 * ReturnCache - Where the new cache object is returned
97 * DESCRIPTION: Create a local memory list for tracking purposed
99 ******************************************************************************/
105 ACPI_MEMORY_LIST **ReturnCache)
107 ACPI_MEMORY_LIST *Cache;
110 Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
113 return (AE_NO_MEMORY);
116 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
118 Cache->ListName = ListName;
119 Cache->ObjectSize = ObjectSize;
121 *ReturnCache = Cache;
126 /*******************************************************************************
128 * FUNCTION: AcpiUtAllocateAndTrack
130 * PARAMETERS: Size - Size of the allocation
131 * Component - Component type of caller
132 * Module - Source file name of caller
133 * Line - Line number of caller
135 * RETURN: Address of the allocated memory on success, NULL on failure.
137 * DESCRIPTION: The subsystem's equivalent of malloc.
139 ******************************************************************************/
142 AcpiUtAllocateAndTrack (
148 ACPI_DEBUG_MEM_BLOCK *Allocation;
152 /* Check for an inadvertent size of zero bytes */
156 ACPI_WARNING ((Module, Line,
157 "Attempt to allocate zero bytes, allocating 1 byte"));
161 Allocation = AcpiOsAllocate (Size + sizeof (ACPI_DEBUG_MEM_HEADER));
164 /* Report allocation error */
166 ACPI_WARNING ((Module, Line,
167 "Could not allocate size %u", (UINT32) Size));
172 Status = AcpiUtTrackAllocation (Allocation, Size,
173 ACPI_MEM_MALLOC, Component, Module, Line);
174 if (ACPI_FAILURE (Status))
176 AcpiOsFree (Allocation);
180 AcpiGbl_GlobalList->TotalAllocated++;
181 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
182 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
183 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
185 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
188 return ((void *) &Allocation->UserSpace);
192 /*******************************************************************************
194 * FUNCTION: AcpiUtAllocateZeroedAndTrack
196 * PARAMETERS: Size - Size of the allocation
197 * Component - Component type of caller
198 * Module - Source file name of caller
199 * Line - Line number of caller
201 * RETURN: Address of the allocated memory on success, NULL on failure.
203 * DESCRIPTION: Subsystem equivalent of calloc.
205 ******************************************************************************/
208 AcpiUtAllocateZeroedAndTrack (
214 ACPI_DEBUG_MEM_BLOCK *Allocation;
218 /* Check for an inadvertent size of zero bytes */
222 ACPI_WARNING ((Module, Line,
223 "Attempt to allocate zero bytes, allocating 1 byte"));
227 Allocation = AcpiOsAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER));
230 /* Report allocation error */
232 ACPI_ERROR ((Module, Line,
233 "Could not allocate size %u", (UINT32) Size));
237 Status = AcpiUtTrackAllocation (Allocation, Size,
238 ACPI_MEM_CALLOC, Component, Module, Line);
239 if (ACPI_FAILURE (Status))
241 AcpiOsFree (Allocation);
245 AcpiGbl_GlobalList->TotalAllocated++;
246 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
247 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
248 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
250 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
253 return ((void *) &Allocation->UserSpace);
257 /*******************************************************************************
259 * FUNCTION: AcpiUtFreeAndTrack
261 * PARAMETERS: Allocation - Address of the memory to deallocate
262 * Component - Component type of caller
263 * Module - Source file name of caller
264 * Line - Line number of caller
268 * DESCRIPTION: Frees the memory at Allocation
270 ******************************************************************************/
279 ACPI_DEBUG_MEM_BLOCK *DebugBlock;
283 ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
286 if (NULL == Allocation)
288 ACPI_ERROR ((Module, Line,
289 "Attempt to delete a NULL address"));
294 DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
295 (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
297 AcpiGbl_GlobalList->TotalFreed++;
298 AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
300 Status = AcpiUtRemoveAllocation (DebugBlock,
301 Component, Module, Line);
302 if (ACPI_FAILURE (Status))
304 ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
307 AcpiOsFree (DebugBlock);
308 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed (block %p)\n",
309 Allocation, DebugBlock));
314 /*******************************************************************************
316 * FUNCTION: AcpiUtFindAllocation
318 * PARAMETERS: Allocation - Address of allocated memory
320 * RETURN: Three cases:
321 * 1) List is empty, NULL is returned.
322 * 2) Element was found. Returns Allocation parameter.
323 * 3) Element was not found. Returns position where it should be
324 * inserted into the list.
326 * DESCRIPTION: Searches for an element in the global allocation tracking list.
327 * If the element is not found, returns the location within the
328 * list where the element should be inserted.
330 * Note: The list is ordered by larger-to-smaller addresses.
332 * This global list is used to detect memory leaks in ACPICA as
333 * well as other issues such as an attempt to release the same
334 * internal object more than once. Although expensive as far
335 * as cpu time, this list is much more helpful for finding these
336 * types of issues than using memory leak detectors outside of
339 ******************************************************************************/
341 static ACPI_DEBUG_MEM_BLOCK *
342 AcpiUtFindAllocation (
343 ACPI_DEBUG_MEM_BLOCK *Allocation)
345 ACPI_DEBUG_MEM_BLOCK *Element;
348 Element = AcpiGbl_GlobalList->ListHead;
355 * Search for the address.
357 * Note: List is ordered by larger-to-smaller addresses, on the
358 * assumption that a new allocation usually has a larger address
359 * than previous allocations.
361 while (Element > Allocation)
363 /* Check for end-of-list */
370 Element = Element->Next;
373 if (Element == Allocation)
378 return (Element->Previous);
382 /*******************************************************************************
384 * FUNCTION: AcpiUtTrackAllocation
386 * PARAMETERS: Allocation - Address of allocated memory
387 * Size - Size of the allocation
388 * AllocType - MEM_MALLOC or MEM_CALLOC
389 * Component - Component type of caller
390 * Module - Source file name of caller
391 * Line - Line number of caller
395 * DESCRIPTION: Inserts an element into the global allocation tracking list.
397 ******************************************************************************/
400 AcpiUtTrackAllocation (
401 ACPI_DEBUG_MEM_BLOCK *Allocation,
408 ACPI_MEMORY_LIST *MemList;
409 ACPI_DEBUG_MEM_BLOCK *Element;
410 ACPI_STATUS Status = AE_OK;
413 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
416 if (AcpiGbl_DisableMemTracking)
418 return_ACPI_STATUS (AE_OK);
421 MemList = AcpiGbl_GlobalList;
422 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
423 if (ACPI_FAILURE (Status))
425 return_ACPI_STATUS (Status);
429 * Search the global list for this address to make sure it is not
430 * already present. This will catch several kinds of problems.
432 Element = AcpiUtFindAllocation (Allocation);
433 if (Element == Allocation)
435 ACPI_ERROR ((AE_INFO,
436 "UtTrackAllocation: Allocation (%p) already present in global list!",
441 /* Fill in the instance data */
443 Allocation->Size = (UINT32) Size;
444 Allocation->AllocType = AllocType;
445 Allocation->Component = Component;
446 Allocation->Line = Line;
448 ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
449 Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
453 /* Insert at list head */
455 if (MemList->ListHead)
457 ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
460 Allocation->Next = MemList->ListHead;
461 Allocation->Previous = NULL;
463 MemList->ListHead = Allocation;
467 /* Insert after element */
469 Allocation->Next = Element->Next;
470 Allocation->Previous = Element;
474 (Element->Next)->Previous = Allocation;
477 Element->Next = Allocation;
482 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
483 return_ACPI_STATUS (Status);
487 /*******************************************************************************
489 * FUNCTION: AcpiUtRemoveAllocation
491 * PARAMETERS: Allocation - Address of allocated memory
492 * Component - Component type of caller
493 * Module - Source file name of caller
494 * Line - Line number of caller
498 * DESCRIPTION: Deletes an element from the global allocation tracking list.
500 ******************************************************************************/
503 AcpiUtRemoveAllocation (
504 ACPI_DEBUG_MEM_BLOCK *Allocation,
509 ACPI_MEMORY_LIST *MemList;
513 ACPI_FUNCTION_NAME (UtRemoveAllocation);
516 if (AcpiGbl_DisableMemTracking)
521 MemList = AcpiGbl_GlobalList;
522 if (NULL == MemList->ListHead)
524 /* No allocations! */
526 ACPI_ERROR ((Module, Line,
527 "Empty allocation list, nothing to free!"));
532 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
533 if (ACPI_FAILURE (Status))
540 if (Allocation->Previous)
542 (Allocation->Previous)->Next = Allocation->Next;
546 MemList->ListHead = Allocation->Next;
549 if (Allocation->Next)
551 (Allocation->Next)->Previous = Allocation->Previous;
554 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing %p, size 0%X\n",
555 &Allocation->UserSpace, Allocation->Size));
557 /* Mark the segment as deleted */
559 ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
561 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
566 /*******************************************************************************
568 * FUNCTION: AcpiUtDumpAllocationInfo
574 * DESCRIPTION: Print some info about the outstanding allocations.
576 ******************************************************************************/
579 AcpiUtDumpAllocationInfo (
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",
603 ROUND_UP_TO_1K (RunningObjectSize)));
605 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
606 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
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)))));
626 /*******************************************************************************
628 * FUNCTION: AcpiUtDumpAllocations
630 * PARAMETERS: Component - Component(s) to dump info for.
631 * Module - Module to dump info for. NULL means all.
635 * DESCRIPTION: Print a list of all outstanding allocations.
637 ******************************************************************************/
640 AcpiUtDumpAllocations (
644 ACPI_DEBUG_MEM_BLOCK *Element;
645 ACPI_DESCRIPTOR *Descriptor;
646 UINT32 NumOutstanding = 0;
647 UINT8 DescriptorType;
650 ACPI_FUNCTION_TRACE (UtDumpAllocations);
653 if (AcpiGbl_DisableMemTracking)
659 * Walk the allocation list.
661 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
666 Element = AcpiGbl_GlobalList->ListHead;
669 if ((Element->Component & Component) &&
670 ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
672 Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
674 if (Element->Size < sizeof (ACPI_COMMON_DESCRIPTOR))
676 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
677 "[Not a Descriptor - too small]\n",
678 Descriptor, Element->Size, Element->Module,
683 /* Ignore allocated objects that are in a cache */
685 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
687 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
688 Descriptor, Element->Size, Element->Module,
689 Element->Line, AcpiUtGetDescriptorName (Descriptor));
691 /* Validate the descriptor type using Type field and length */
693 DescriptorType = 0; /* Not a valid descriptor type */
695 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
697 case ACPI_DESC_TYPE_OPERAND:
699 if (Element->Size == sizeof (ACPI_OPERAND_OBJECT))
701 DescriptorType = ACPI_DESC_TYPE_OPERAND;
705 case ACPI_DESC_TYPE_PARSER:
707 if (Element->Size == sizeof (ACPI_PARSE_OBJECT))
709 DescriptorType = ACPI_DESC_TYPE_PARSER;
713 case ACPI_DESC_TYPE_NAMED:
715 if (Element->Size == sizeof (ACPI_NAMESPACE_NODE))
717 DescriptorType = ACPI_DESC_TYPE_NAMED;
726 /* Display additional info for the major descriptor types */
728 switch (DescriptorType)
730 case ACPI_DESC_TYPE_OPERAND:
732 AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
733 AcpiUtGetTypeName (Descriptor->Object.Common.Type),
734 Descriptor->Object.Common.ReferenceCount);
737 case ACPI_DESC_TYPE_PARSER:
739 AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
740 Descriptor->Op.Asl.AmlOpcode);
743 case ACPI_DESC_TYPE_NAMED:
745 AcpiOsPrintf ("%4.4s\n",
746 AcpiUtGetNodeName (&Descriptor->Node));
751 AcpiOsPrintf ( "\n");
760 Element = Element->Next;
763 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
769 ACPI_INFO ((AE_INFO, "No outstanding allocations"));
773 ACPI_ERROR ((AE_INFO, "%u(0x%X) Outstanding allocations",
774 NumOutstanding, NumOutstanding));
780 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */