1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
57 #include <contrib/dev/acpica/include/acpi.h>
58 #include <contrib/dev/acpica/include/accommon.h>
60 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
62 #define _COMPONENT ACPI_UTILITIES
63 ACPI_MODULE_NAME ("uttrack")
66 /* Local prototypes */
68 static ACPI_DEBUG_MEM_BLOCK *
69 AcpiUtFindAllocation (
70 ACPI_DEBUG_MEM_BLOCK *Allocation);
73 AcpiUtTrackAllocation (
74 ACPI_DEBUG_MEM_BLOCK *Address,
82 AcpiUtRemoveAllocation (
83 ACPI_DEBUG_MEM_BLOCK *Address,
89 /*******************************************************************************
91 * FUNCTION: AcpiUtCreateList
93 * PARAMETERS: CacheName - Ascii name for the cache
94 * ObjectSize - Size of each cached object
95 * ReturnCache - Where the new cache object is returned
99 * DESCRIPTION: Create a local memory list for tracking purposed
101 ******************************************************************************/
107 ACPI_MEMORY_LIST **ReturnCache)
109 ACPI_MEMORY_LIST *Cache;
112 Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
115 return (AE_NO_MEMORY);
118 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
120 Cache->ListName = ListName;
121 Cache->ObjectSize = ObjectSize;
123 *ReturnCache = Cache;
128 /*******************************************************************************
130 * FUNCTION: AcpiUtAllocateAndTrack
132 * PARAMETERS: Size - Size of the allocation
133 * Component - Component type of caller
134 * Module - Source file name of caller
135 * Line - Line number of caller
137 * RETURN: Address of the allocated memory on success, NULL on failure.
139 * DESCRIPTION: The subsystem's equivalent of malloc.
141 ******************************************************************************/
144 AcpiUtAllocateAndTrack (
150 ACPI_DEBUG_MEM_BLOCK *Allocation;
154 Allocation = AcpiUtAllocate (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
155 Component, Module, Line);
161 Status = AcpiUtTrackAllocation (Allocation, Size,
162 ACPI_MEM_MALLOC, Component, Module, Line);
163 if (ACPI_FAILURE (Status))
165 AcpiOsFree (Allocation);
169 AcpiGbl_GlobalList->TotalAllocated++;
170 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
171 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
172 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
174 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
177 return ((void *) &Allocation->UserSpace);
181 /*******************************************************************************
183 * FUNCTION: AcpiUtAllocateZeroedAndTrack
185 * PARAMETERS: Size - Size of the allocation
186 * Component - Component type of caller
187 * Module - Source file name of caller
188 * Line - Line number of caller
190 * RETURN: Address of the allocated memory on success, NULL on failure.
192 * DESCRIPTION: Subsystem equivalent of calloc.
194 ******************************************************************************/
197 AcpiUtAllocateZeroedAndTrack (
203 ACPI_DEBUG_MEM_BLOCK *Allocation;
207 Allocation = AcpiUtAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
208 Component, Module, Line);
211 /* Report allocation error */
213 ACPI_ERROR ((Module, Line,
214 "Could not allocate size %u", (UINT32) Size));
218 Status = AcpiUtTrackAllocation (Allocation, Size,
219 ACPI_MEM_CALLOC, Component, Module, Line);
220 if (ACPI_FAILURE (Status))
222 AcpiOsFree (Allocation);
226 AcpiGbl_GlobalList->TotalAllocated++;
227 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
228 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
229 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
231 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
234 return ((void *) &Allocation->UserSpace);
238 /*******************************************************************************
240 * FUNCTION: AcpiUtFreeAndTrack
242 * PARAMETERS: Allocation - Address of the memory to deallocate
243 * Component - Component type of caller
244 * Module - Source file name of caller
245 * Line - Line number of caller
249 * DESCRIPTION: Frees the memory at Allocation
251 ******************************************************************************/
260 ACPI_DEBUG_MEM_BLOCK *DebugBlock;
264 ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
267 if (NULL == Allocation)
269 ACPI_ERROR ((Module, Line,
270 "Attempt to delete a NULL address"));
275 DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
276 (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
278 AcpiGbl_GlobalList->TotalFreed++;
279 AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
281 Status = AcpiUtRemoveAllocation (DebugBlock,
282 Component, Module, Line);
283 if (ACPI_FAILURE (Status))
285 ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
288 AcpiOsFree (DebugBlock);
289 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Allocation));
294 /*******************************************************************************
296 * FUNCTION: AcpiUtFindAllocation
298 * PARAMETERS: Allocation - Address of allocated memory
300 * RETURN: Three cases:
301 * 1) List is empty, NULL is returned.
302 * 2) Element was found. Returns Allocation parameter.
303 * 3) Element was not found. Returns position where it should be
304 * inserted into the list.
306 * DESCRIPTION: Searches for an element in the global allocation tracking list.
307 * If the element is not found, returns the location within the
308 * list where the element should be inserted.
310 * Note: The list is ordered by larger-to-smaller addresses.
312 * This global list is used to detect memory leaks in ACPICA as
313 * well as other issues such as an attempt to release the same
314 * internal object more than once. Although expensive as far
315 * as cpu time, this list is much more helpful for finding these
316 * types of issues than using memory leak detectors outside of
319 ******************************************************************************/
321 static ACPI_DEBUG_MEM_BLOCK *
322 AcpiUtFindAllocation (
323 ACPI_DEBUG_MEM_BLOCK *Allocation)
325 ACPI_DEBUG_MEM_BLOCK *Element;
328 Element = AcpiGbl_GlobalList->ListHead;
335 * Search for the address.
337 * Note: List is ordered by larger-to-smaller addresses, on the
338 * assumption that a new allocation usually has a larger address
339 * than previous allocations.
341 while (Element > Allocation)
343 /* Check for end-of-list */
350 Element = Element->Next;
353 if (Element == Allocation)
358 return (Element->Previous);
362 /*******************************************************************************
364 * FUNCTION: AcpiUtTrackAllocation
366 * PARAMETERS: Allocation - Address of allocated memory
367 * Size - Size of the allocation
368 * AllocType - MEM_MALLOC or MEM_CALLOC
369 * Component - Component type of caller
370 * Module - Source file name of caller
371 * Line - Line number of caller
375 * DESCRIPTION: Inserts an element into the global allocation tracking list.
377 ******************************************************************************/
380 AcpiUtTrackAllocation (
381 ACPI_DEBUG_MEM_BLOCK *Allocation,
388 ACPI_MEMORY_LIST *MemList;
389 ACPI_DEBUG_MEM_BLOCK *Element;
390 ACPI_STATUS Status = AE_OK;
393 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
396 if (AcpiGbl_DisableMemTracking)
398 return_ACPI_STATUS (AE_OK);
401 MemList = AcpiGbl_GlobalList;
402 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
403 if (ACPI_FAILURE (Status))
405 return_ACPI_STATUS (Status);
409 * Search the global list for this address to make sure it is not
410 * already present. This will catch several kinds of problems.
412 Element = AcpiUtFindAllocation (Allocation);
413 if (Element == Allocation)
415 ACPI_ERROR ((AE_INFO,
416 "UtTrackAllocation: Allocation (%p) already present in global list!",
421 /* Fill in the instance data */
423 Allocation->Size = (UINT32) Size;
424 Allocation->AllocType = AllocType;
425 Allocation->Component = Component;
426 Allocation->Line = Line;
428 ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
429 Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
433 /* Insert at list head */
435 if (MemList->ListHead)
437 ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
440 Allocation->Next = MemList->ListHead;
441 Allocation->Previous = NULL;
443 MemList->ListHead = Allocation;
447 /* Insert after element */
449 Allocation->Next = Element->Next;
450 Allocation->Previous = Element;
454 (Element->Next)->Previous = Allocation;
457 Element->Next = Allocation;
462 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
463 return_ACPI_STATUS (Status);
467 /*******************************************************************************
469 * FUNCTION: AcpiUtRemoveAllocation
471 * PARAMETERS: Allocation - Address of allocated memory
472 * Component - Component type of caller
473 * Module - Source file name of caller
474 * Line - Line number of caller
478 * DESCRIPTION: Deletes an element from the global allocation tracking list.
480 ******************************************************************************/
483 AcpiUtRemoveAllocation (
484 ACPI_DEBUG_MEM_BLOCK *Allocation,
489 ACPI_MEMORY_LIST *MemList;
493 ACPI_FUNCTION_NAME (UtRemoveAllocation);
496 if (AcpiGbl_DisableMemTracking)
501 MemList = AcpiGbl_GlobalList;
502 if (NULL == MemList->ListHead)
504 /* No allocations! */
506 ACPI_ERROR ((Module, Line,
507 "Empty allocation list, nothing to free!"));
512 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
513 if (ACPI_FAILURE (Status))
520 if (Allocation->Previous)
522 (Allocation->Previous)->Next = Allocation->Next;
526 MemList->ListHead = Allocation->Next;
529 if (Allocation->Next)
531 (Allocation->Next)->Previous = Allocation->Previous;
534 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing %p, size 0%X\n",
535 &Allocation->UserSpace, Allocation->Size));
537 /* Mark the segment as deleted */
539 ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
541 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
546 /*******************************************************************************
548 * FUNCTION: AcpiUtDumpAllocationInfo
554 * DESCRIPTION: Print some info about the outstanding allocations.
556 ******************************************************************************/
559 AcpiUtDumpAllocationInfo (
563 ACPI_MEMORY_LIST *MemList;
566 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo);
569 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
570 ("%30s: %4d (%3d Kb)\n", "Current allocations",
571 MemList->CurrentCount,
572 ROUND_UP_TO_1K (MemList->CurrentSize)));
574 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
575 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
576 MemList->MaxConcurrentCount,
577 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
580 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
581 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
583 ROUND_UP_TO_1K (RunningObjectSize)));
585 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
586 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
588 ROUND_UP_TO_1K (RunningAllocSize)));
591 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
592 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
593 AcpiGbl_CurrentNodeCount,
594 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
596 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
597 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
598 AcpiGbl_MaxConcurrentNodeCount,
599 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
600 sizeof (ACPI_NAMESPACE_NODE)))));
606 /*******************************************************************************
608 * FUNCTION: AcpiUtDumpAllocations
610 * PARAMETERS: Component - Component(s) to dump info for.
611 * Module - Module to dump info for. NULL means all.
615 * DESCRIPTION: Print a list of all outstanding allocations.
617 ******************************************************************************/
620 AcpiUtDumpAllocations (
624 ACPI_DEBUG_MEM_BLOCK *Element;
625 ACPI_DESCRIPTOR *Descriptor;
626 UINT32 NumOutstanding = 0;
627 UINT8 DescriptorType;
630 ACPI_FUNCTION_TRACE (UtDumpAllocations);
633 if (AcpiGbl_DisableMemTracking)
639 * Walk the allocation list.
641 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
646 Element = AcpiGbl_GlobalList->ListHead;
649 if ((Element->Component & Component) &&
650 ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
652 Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
654 if (Element->Size < sizeof (ACPI_COMMON_DESCRIPTOR))
656 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
657 "[Not a Descriptor - too small]\n",
658 Descriptor, Element->Size, Element->Module,
663 /* Ignore allocated objects that are in a cache */
665 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
667 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
668 Descriptor, Element->Size, Element->Module,
669 Element->Line, AcpiUtGetDescriptorName (Descriptor));
671 /* Validate the descriptor type using Type field and length */
673 DescriptorType = 0; /* Not a valid descriptor type */
675 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
677 case ACPI_DESC_TYPE_OPERAND:
679 if (Element->Size == sizeof (ACPI_OPERAND_OBJECT))
681 DescriptorType = ACPI_DESC_TYPE_OPERAND;
685 case ACPI_DESC_TYPE_PARSER:
687 if (Element->Size == sizeof (ACPI_PARSE_OBJECT))
689 DescriptorType = ACPI_DESC_TYPE_PARSER;
693 case ACPI_DESC_TYPE_NAMED:
695 if (Element->Size == sizeof (ACPI_NAMESPACE_NODE))
697 DescriptorType = ACPI_DESC_TYPE_NAMED;
706 /* Display additional info for the major descriptor types */
708 switch (DescriptorType)
710 case ACPI_DESC_TYPE_OPERAND:
712 AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
713 AcpiUtGetTypeName (Descriptor->Object.Common.Type),
714 Descriptor->Object.Common.ReferenceCount);
717 case ACPI_DESC_TYPE_PARSER:
719 AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
720 Descriptor->Op.Asl.AmlOpcode);
723 case ACPI_DESC_TYPE_NAMED:
725 AcpiOsPrintf ("%4.4s\n",
726 AcpiUtGetNodeName (&Descriptor->Node));
731 AcpiOsPrintf ( "\n");
740 Element = Element->Next;
743 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
749 ACPI_INFO ((AE_INFO, "No outstanding allocations"));
753 ACPI_ERROR ((AE_INFO, "%u(0x%X) Outstanding allocations",
754 NumOutstanding, NumOutstanding));
760 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */