1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, 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")
65 /* Local prototypes */
67 static ACPI_DEBUG_MEM_BLOCK *
68 AcpiUtFindAllocation (
72 AcpiUtTrackAllocation (
73 ACPI_DEBUG_MEM_BLOCK *Address,
81 AcpiUtRemoveAllocation (
82 ACPI_DEBUG_MEM_BLOCK *Address,
88 /*******************************************************************************
90 * FUNCTION: AcpiUtCreateList
92 * PARAMETERS: CacheName - Ascii name for the cache
93 * ObjectSize - Size of each cached object
94 * ReturnCache - Where the new cache object is returned
98 * DESCRIPTION: Create a local memory list for tracking purposed
100 ******************************************************************************/
106 ACPI_MEMORY_LIST **ReturnCache)
108 ACPI_MEMORY_LIST *Cache;
111 Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
114 return (AE_NO_MEMORY);
117 ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
119 Cache->ListName = ListName;
120 Cache->ObjectSize = ObjectSize;
122 *ReturnCache = Cache;
127 /*******************************************************************************
129 * FUNCTION: AcpiUtAllocateAndTrack
131 * PARAMETERS: Size - Size of the allocation
132 * Component - Component type of caller
133 * Module - Source file name of caller
134 * Line - Line number of caller
136 * RETURN: Address of the allocated memory on success, NULL on failure.
138 * DESCRIPTION: The subsystem's equivalent of malloc.
140 ******************************************************************************/
143 AcpiUtAllocateAndTrack (
149 ACPI_DEBUG_MEM_BLOCK *Allocation;
153 Allocation = AcpiUtAllocate (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
154 Component, Module, Line);
160 Status = AcpiUtTrackAllocation (Allocation, Size,
161 ACPI_MEM_MALLOC, Component, Module, Line);
162 if (ACPI_FAILURE (Status))
164 AcpiOsFree (Allocation);
168 AcpiGbl_GlobalList->TotalAllocated++;
169 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
170 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
171 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
173 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
176 return ((void *) &Allocation->UserSpace);
180 /*******************************************************************************
182 * FUNCTION: AcpiUtAllocateZeroedAndTrack
184 * PARAMETERS: Size - Size of the allocation
185 * Component - Component type of caller
186 * Module - Source file name of caller
187 * Line - Line number of caller
189 * RETURN: Address of the allocated memory on success, NULL on failure.
191 * DESCRIPTION: Subsystem equivalent of calloc.
193 ******************************************************************************/
196 AcpiUtAllocateZeroedAndTrack (
202 ACPI_DEBUG_MEM_BLOCK *Allocation;
206 Allocation = AcpiUtAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER),
207 Component, Module, Line);
210 /* Report allocation error */
212 ACPI_ERROR ((Module, Line,
213 "Could not allocate size %u", (UINT32) Size));
217 Status = AcpiUtTrackAllocation (Allocation, Size,
218 ACPI_MEM_CALLOC, Component, Module, Line);
219 if (ACPI_FAILURE (Status))
221 AcpiOsFree (Allocation);
225 AcpiGbl_GlobalList->TotalAllocated++;
226 AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
227 AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
228 if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
230 AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
233 return ((void *) &Allocation->UserSpace);
237 /*******************************************************************************
239 * FUNCTION: AcpiUtFreeAndTrack
241 * PARAMETERS: Allocation - Address of the memory to deallocate
242 * Component - Component type of caller
243 * Module - Source file name of caller
244 * Line - Line number of caller
248 * DESCRIPTION: Frees the memory at Allocation
250 ******************************************************************************/
259 ACPI_DEBUG_MEM_BLOCK *DebugBlock;
263 ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
266 if (NULL == Allocation)
268 ACPI_ERROR ((Module, Line,
269 "Attempt to delete a NULL address"));
274 DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
275 (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
277 AcpiGbl_GlobalList->TotalFreed++;
278 AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
280 Status = AcpiUtRemoveAllocation (DebugBlock,
281 Component, Module, Line);
282 if (ACPI_FAILURE (Status))
284 ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
287 AcpiOsFree (DebugBlock);
288 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed\n", Allocation));
293 /*******************************************************************************
295 * FUNCTION: AcpiUtFindAllocation
297 * PARAMETERS: Allocation - Address of allocated memory
299 * RETURN: A list element if found; NULL otherwise.
301 * DESCRIPTION: Searches for an element in the global allocation tracking list.
303 ******************************************************************************/
305 static ACPI_DEBUG_MEM_BLOCK *
306 AcpiUtFindAllocation (
309 ACPI_DEBUG_MEM_BLOCK *Element;
312 ACPI_FUNCTION_ENTRY ();
315 Element = AcpiGbl_GlobalList->ListHead;
317 /* Search for the address. */
321 if (Element == Allocation)
326 Element = Element->Next;
333 /*******************************************************************************
335 * FUNCTION: AcpiUtTrackAllocation
337 * PARAMETERS: Allocation - Address of allocated memory
338 * Size - Size of the allocation
339 * AllocType - MEM_MALLOC or MEM_CALLOC
340 * Component - Component type of caller
341 * Module - Source file name of caller
342 * Line - Line number of caller
346 * DESCRIPTION: Inserts an element into the global allocation tracking list.
348 ******************************************************************************/
351 AcpiUtTrackAllocation (
352 ACPI_DEBUG_MEM_BLOCK *Allocation,
359 ACPI_MEMORY_LIST *MemList;
360 ACPI_DEBUG_MEM_BLOCK *Element;
361 ACPI_STATUS Status = AE_OK;
364 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
367 if (AcpiGbl_DisableMemTracking)
369 return_ACPI_STATUS (AE_OK);
372 MemList = AcpiGbl_GlobalList;
373 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
374 if (ACPI_FAILURE (Status))
376 return_ACPI_STATUS (Status);
380 * Search list for this address to make sure it is not already on the list.
381 * This will catch several kinds of problems.
383 Element = AcpiUtFindAllocation (Allocation);
386 ACPI_ERROR ((AE_INFO,
387 "UtTrackAllocation: Allocation already present in list! (%p)",
390 ACPI_ERROR ((AE_INFO, "Element %p Address %p",
391 Element, Allocation));
396 /* Fill in the instance data. */
398 Allocation->Size = (UINT32) Size;
399 Allocation->AllocType = AllocType;
400 Allocation->Component = Component;
401 Allocation->Line = Line;
403 ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
404 Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
406 /* Insert at list head */
408 if (MemList->ListHead)
410 ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
413 Allocation->Next = MemList->ListHead;
414 Allocation->Previous = NULL;
416 MemList->ListHead = Allocation;
420 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
421 return_ACPI_STATUS (Status);
425 /*******************************************************************************
427 * FUNCTION: AcpiUtRemoveAllocation
429 * PARAMETERS: Allocation - Address of allocated memory
430 * Component - Component type of caller
431 * Module - Source file name of caller
432 * Line - Line number of caller
436 * DESCRIPTION: Deletes an element from the global allocation tracking list.
438 ******************************************************************************/
441 AcpiUtRemoveAllocation (
442 ACPI_DEBUG_MEM_BLOCK *Allocation,
447 ACPI_MEMORY_LIST *MemList;
451 ACPI_FUNCTION_TRACE (UtRemoveAllocation);
454 if (AcpiGbl_DisableMemTracking)
456 return_ACPI_STATUS (AE_OK);
459 MemList = AcpiGbl_GlobalList;
460 if (NULL == MemList->ListHead)
462 /* No allocations! */
464 ACPI_ERROR ((Module, Line,
465 "Empty allocation list, nothing to free!"));
467 return_ACPI_STATUS (AE_OK);
470 Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
471 if (ACPI_FAILURE (Status))
473 return_ACPI_STATUS (Status);
478 if (Allocation->Previous)
480 (Allocation->Previous)->Next = Allocation->Next;
484 MemList->ListHead = Allocation->Next;
487 if (Allocation->Next)
489 (Allocation->Next)->Previous = Allocation->Previous;
492 /* Mark the segment as deleted */
494 ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
496 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
499 Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
500 return_ACPI_STATUS (Status);
504 /*******************************************************************************
506 * FUNCTION: AcpiUtDumpAllocationInfo
512 * DESCRIPTION: Print some info about the outstanding allocations.
514 ******************************************************************************/
517 AcpiUtDumpAllocationInfo (
521 ACPI_MEMORY_LIST *MemList;
524 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo);
527 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
528 ("%30s: %4d (%3d Kb)\n", "Current allocations",
529 MemList->CurrentCount,
530 ROUND_UP_TO_1K (MemList->CurrentSize)));
532 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
533 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
534 MemList->MaxConcurrentCount,
535 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
538 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
539 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
541 ROUND_UP_TO_1K (RunningObjectSize)));
543 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
544 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
546 ROUND_UP_TO_1K (RunningAllocSize)));
549 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
550 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
551 AcpiGbl_CurrentNodeCount,
552 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
554 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
555 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
556 AcpiGbl_MaxConcurrentNodeCount,
557 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
558 sizeof (ACPI_NAMESPACE_NODE)))));
564 /*******************************************************************************
566 * FUNCTION: AcpiUtDumpAllocations
568 * PARAMETERS: Component - Component(s) to dump info for.
569 * Module - Module to dump info for. NULL means all.
573 * DESCRIPTION: Print a list of all outstanding allocations.
575 ******************************************************************************/
578 AcpiUtDumpAllocations (
582 ACPI_DEBUG_MEM_BLOCK *Element;
583 ACPI_DESCRIPTOR *Descriptor;
584 UINT32 NumOutstanding = 0;
585 UINT8 DescriptorType;
588 ACPI_FUNCTION_TRACE (UtDumpAllocations);
591 if (AcpiGbl_DisableMemTracking)
597 * Walk the allocation list.
599 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
604 Element = AcpiGbl_GlobalList->ListHead;
607 if ((Element->Component & Component) &&
608 ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
610 Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
612 if (Element->Size < sizeof (ACPI_COMMON_DESCRIPTOR))
614 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
615 "[Not a Descriptor - too small]\n",
616 Descriptor, Element->Size, Element->Module,
621 /* Ignore allocated objects that are in a cache */
623 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
625 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
626 Descriptor, Element->Size, Element->Module,
627 Element->Line, AcpiUtGetDescriptorName (Descriptor));
629 /* Validate the descriptor type using Type field and length */
631 DescriptorType = 0; /* Not a valid descriptor type */
633 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
635 case ACPI_DESC_TYPE_OPERAND:
636 if (Element->Size == sizeof (ACPI_DESC_TYPE_OPERAND))
638 DescriptorType = ACPI_DESC_TYPE_OPERAND;
642 case ACPI_DESC_TYPE_PARSER:
643 if (Element->Size == sizeof (ACPI_DESC_TYPE_PARSER))
645 DescriptorType = ACPI_DESC_TYPE_PARSER;
649 case ACPI_DESC_TYPE_NAMED:
650 if (Element->Size == sizeof (ACPI_DESC_TYPE_NAMED))
652 DescriptorType = ACPI_DESC_TYPE_NAMED;
660 /* Display additional info for the major descriptor types */
662 switch (DescriptorType)
664 case ACPI_DESC_TYPE_OPERAND:
665 AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
666 AcpiUtGetTypeName (Descriptor->Object.Common.Type),
667 Descriptor->Object.Common.ReferenceCount);
670 case ACPI_DESC_TYPE_PARSER:
671 AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
672 Descriptor->Op.Asl.AmlOpcode);
675 case ACPI_DESC_TYPE_NAMED:
676 AcpiOsPrintf ("%4.4s\n",
677 AcpiUtGetNodeName (&Descriptor->Node));
681 AcpiOsPrintf ( "\n");
690 Element = Element->Next;
693 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
699 ACPI_INFO ((AE_INFO, "No outstanding allocations"));
703 ACPI_ERROR ((AE_INFO, "%u(0x%X) Outstanding allocations",
704 NumOutstanding, NumOutstanding));
710 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */