Home | History | Annotate | Line # | Download | only in utilities
uttrack.c revision 1.5.2.2
      1      1.1    jruoho /******************************************************************************
      2      1.1    jruoho  *
      3      1.1    jruoho  * Module Name: uttrack - Memory allocation tracking routines (debug only)
      4      1.1    jruoho  *
      5      1.1    jruoho  *****************************************************************************/
      6      1.1    jruoho 
      7      1.3    jruoho /*
      8  1.5.2.1     skrll  * Copyright (C) 2000 - 2015, Intel Corp.
      9      1.1    jruoho  * All rights reserved.
     10      1.1    jruoho  *
     11      1.3    jruoho  * Redistribution and use in source and binary forms, with or without
     12      1.3    jruoho  * modification, are permitted provided that the following conditions
     13      1.3    jruoho  * are met:
     14      1.3    jruoho  * 1. Redistributions of source code must retain the above copyright
     15      1.3    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16      1.3    jruoho  *    without modification.
     17      1.3    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.3    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.3    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.3    jruoho  *    including a substantially similar Disclaimer requirement for further
     21      1.3    jruoho  *    binary redistribution.
     22      1.3    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.3    jruoho  *    of any contributors may be used to endorse or promote products derived
     24      1.3    jruoho  *    from this software without specific prior written permission.
     25      1.3    jruoho  *
     26      1.3    jruoho  * Alternatively, this software may be distributed under the terms of the
     27      1.3    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.3    jruoho  * Software Foundation.
     29      1.3    jruoho  *
     30      1.3    jruoho  * NO WARRANTY
     31      1.3    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.3    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.3    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.3    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.3    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.3    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.3    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.3    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.3    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.3    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.3    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42      1.3    jruoho  */
     43      1.1    jruoho 
     44      1.1    jruoho /*
     45      1.1    jruoho  * These procedures are used for tracking memory leaks in the subsystem, and
     46      1.1    jruoho  * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
     47      1.1    jruoho  *
     48      1.4  christos  * Each memory allocation is tracked via a doubly linked list. Each
     49      1.1    jruoho  * element contains the caller's component, module name, function name, and
     50      1.4  christos  * line number. AcpiUtAllocate and AcpiUtAllocateZeroed call
     51      1.1    jruoho  * AcpiUtTrackAllocation to add an element to the list; deletion
     52      1.1    jruoho  * occurs in the body of AcpiUtFree.
     53      1.1    jruoho  */
     54      1.1    jruoho 
     55      1.1    jruoho #include "acpi.h"
     56      1.1    jruoho #include "accommon.h"
     57      1.1    jruoho 
     58      1.1    jruoho #ifdef ACPI_DBG_TRACK_ALLOCATIONS
     59      1.1    jruoho 
     60      1.1    jruoho #define _COMPONENT          ACPI_UTILITIES
     61      1.1    jruoho         ACPI_MODULE_NAME    ("uttrack")
     62      1.1    jruoho 
     63      1.4  christos 
     64      1.1    jruoho /* Local prototypes */
     65      1.1    jruoho 
     66      1.1    jruoho static ACPI_DEBUG_MEM_BLOCK *
     67      1.1    jruoho AcpiUtFindAllocation (
     68      1.4  christos     ACPI_DEBUG_MEM_BLOCK    *Allocation);
     69      1.1    jruoho 
     70      1.1    jruoho static ACPI_STATUS
     71      1.1    jruoho AcpiUtTrackAllocation (
     72      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Address,
     73      1.1    jruoho     ACPI_SIZE               Size,
     74      1.1    jruoho     UINT8                   AllocType,
     75      1.1    jruoho     UINT32                  Component,
     76      1.1    jruoho     const char              *Module,
     77      1.1    jruoho     UINT32                  Line);
     78      1.1    jruoho 
     79      1.1    jruoho static ACPI_STATUS
     80      1.1    jruoho AcpiUtRemoveAllocation (
     81      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Address,
     82      1.1    jruoho     UINT32                  Component,
     83      1.1    jruoho     const char              *Module,
     84      1.1    jruoho     UINT32                  Line);
     85      1.1    jruoho 
     86      1.1    jruoho 
     87      1.1    jruoho /*******************************************************************************
     88      1.1    jruoho  *
     89      1.1    jruoho  * FUNCTION:    AcpiUtCreateList
     90      1.1    jruoho  *
     91      1.1    jruoho  * PARAMETERS:  CacheName       - Ascii name for the cache
     92      1.1    jruoho  *              ObjectSize      - Size of each cached object
     93      1.1    jruoho  *              ReturnCache     - Where the new cache object is returned
     94      1.1    jruoho  *
     95      1.1    jruoho  * RETURN:      Status
     96      1.1    jruoho  *
     97      1.1    jruoho  * DESCRIPTION: Create a local memory list for tracking purposed
     98      1.1    jruoho  *
     99      1.1    jruoho  ******************************************************************************/
    100      1.1    jruoho 
    101      1.1    jruoho ACPI_STATUS
    102      1.1    jruoho AcpiUtCreateList (
    103      1.2    jruoho     const char              *ListName,
    104      1.1    jruoho     UINT16                  ObjectSize,
    105      1.1    jruoho     ACPI_MEMORY_LIST        **ReturnCache)
    106      1.1    jruoho {
    107      1.1    jruoho     ACPI_MEMORY_LIST        *Cache;
    108      1.1    jruoho 
    109      1.1    jruoho 
    110      1.1    jruoho     Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
    111      1.1    jruoho     if (!Cache)
    112      1.1    jruoho     {
    113      1.1    jruoho         return (AE_NO_MEMORY);
    114      1.1    jruoho     }
    115      1.1    jruoho 
    116  1.5.2.2     skrll     memset (Cache, 0, sizeof (ACPI_MEMORY_LIST));
    117      1.1    jruoho 
    118      1.2    jruoho     Cache->ListName   = __UNCONST(ListName);
    119      1.1    jruoho     Cache->ObjectSize = ObjectSize;
    120      1.1    jruoho 
    121      1.1    jruoho     *ReturnCache = Cache;
    122      1.1    jruoho     return (AE_OK);
    123      1.1    jruoho }
    124      1.1    jruoho 
    125      1.1    jruoho 
    126      1.1    jruoho /*******************************************************************************
    127      1.1    jruoho  *
    128      1.1    jruoho  * FUNCTION:    AcpiUtAllocateAndTrack
    129      1.1    jruoho  *
    130      1.1    jruoho  * PARAMETERS:  Size                - Size of the allocation
    131      1.1    jruoho  *              Component           - Component type of caller
    132      1.1    jruoho  *              Module              - Source file name of caller
    133      1.1    jruoho  *              Line                - Line number of caller
    134      1.1    jruoho  *
    135      1.1    jruoho  * RETURN:      Address of the allocated memory on success, NULL on failure.
    136      1.1    jruoho  *
    137      1.1    jruoho  * DESCRIPTION: The subsystem's equivalent of malloc.
    138      1.1    jruoho  *
    139      1.1    jruoho  ******************************************************************************/
    140      1.1    jruoho 
    141      1.1    jruoho void *
    142      1.1    jruoho AcpiUtAllocateAndTrack (
    143      1.1    jruoho     ACPI_SIZE               Size,
    144      1.1    jruoho     UINT32                  Component,
    145      1.1    jruoho     const char              *Module,
    146      1.1    jruoho     UINT32                  Line)
    147      1.1    jruoho {
    148      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Allocation;
    149      1.1    jruoho     ACPI_STATUS             Status;
    150      1.1    jruoho 
    151      1.1    jruoho 
    152      1.4  christos     /* Check for an inadvertent size of zero bytes */
    153      1.4  christos 
    154      1.4  christos     if (!Size)
    155      1.4  christos     {
    156      1.4  christos         ACPI_WARNING ((Module, Line,
    157      1.4  christos             "Attempt to allocate zero bytes, allocating 1 byte"));
    158      1.4  christos         Size = 1;
    159      1.4  christos     }
    160      1.4  christos 
    161      1.4  christos     Allocation = AcpiOsAllocate (Size + sizeof (ACPI_DEBUG_MEM_HEADER));
    162      1.1    jruoho     if (!Allocation)
    163      1.1    jruoho     {
    164      1.4  christos         /* Report allocation error */
    165      1.4  christos 
    166      1.4  christos         ACPI_WARNING ((Module, Line,
    167      1.4  christos             "Could not allocate size %u", (UINT32) Size));
    168      1.4  christos 
    169      1.1    jruoho         return (NULL);
    170      1.1    jruoho     }
    171      1.1    jruoho 
    172      1.1    jruoho     Status = AcpiUtTrackAllocation (Allocation, Size,
    173      1.1    jruoho                     ACPI_MEM_MALLOC, Component, Module, Line);
    174      1.1    jruoho     if (ACPI_FAILURE (Status))
    175      1.1    jruoho     {
    176      1.1    jruoho         AcpiOsFree (Allocation);
    177      1.1    jruoho         return (NULL);
    178      1.1    jruoho     }
    179      1.1    jruoho 
    180      1.1    jruoho     AcpiGbl_GlobalList->TotalAllocated++;
    181      1.1    jruoho     AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
    182      1.1    jruoho     AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
    183      1.1    jruoho     if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
    184      1.1    jruoho     {
    185      1.1    jruoho         AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
    186      1.1    jruoho     }
    187      1.1    jruoho 
    188      1.1    jruoho     return ((void *) &Allocation->UserSpace);
    189      1.1    jruoho }
    190      1.1    jruoho 
    191      1.1    jruoho 
    192      1.1    jruoho /*******************************************************************************
    193      1.1    jruoho  *
    194      1.1    jruoho  * FUNCTION:    AcpiUtAllocateZeroedAndTrack
    195      1.1    jruoho  *
    196      1.1    jruoho  * PARAMETERS:  Size                - Size of the allocation
    197      1.1    jruoho  *              Component           - Component type of caller
    198      1.1    jruoho  *              Module              - Source file name of caller
    199      1.1    jruoho  *              Line                - Line number of caller
    200      1.1    jruoho  *
    201      1.1    jruoho  * RETURN:      Address of the allocated memory on success, NULL on failure.
    202      1.1    jruoho  *
    203      1.1    jruoho  * DESCRIPTION: Subsystem equivalent of calloc.
    204      1.1    jruoho  *
    205      1.1    jruoho  ******************************************************************************/
    206      1.1    jruoho 
    207      1.1    jruoho void *
    208      1.1    jruoho AcpiUtAllocateZeroedAndTrack (
    209      1.1    jruoho     ACPI_SIZE               Size,
    210      1.1    jruoho     UINT32                  Component,
    211      1.1    jruoho     const char              *Module,
    212      1.1    jruoho     UINT32                  Line)
    213      1.1    jruoho {
    214      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Allocation;
    215      1.1    jruoho     ACPI_STATUS             Status;
    216      1.1    jruoho 
    217      1.1    jruoho 
    218      1.4  christos     /* Check for an inadvertent size of zero bytes */
    219      1.4  christos 
    220      1.4  christos     if (!Size)
    221      1.4  christos     {
    222      1.4  christos         ACPI_WARNING ((Module, Line,
    223      1.4  christos             "Attempt to allocate zero bytes, allocating 1 byte"));
    224      1.4  christos         Size = 1;
    225      1.4  christos     }
    226      1.4  christos 
    227      1.4  christos     Allocation = AcpiOsAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER));
    228      1.1    jruoho     if (!Allocation)
    229      1.1    jruoho     {
    230      1.1    jruoho         /* Report allocation error */
    231      1.1    jruoho 
    232      1.1    jruoho         ACPI_ERROR ((Module, Line,
    233      1.1    jruoho             "Could not allocate size %u", (UINT32) Size));
    234      1.1    jruoho         return (NULL);
    235      1.1    jruoho     }
    236      1.1    jruoho 
    237      1.1    jruoho     Status = AcpiUtTrackAllocation (Allocation, Size,
    238      1.1    jruoho                 ACPI_MEM_CALLOC, Component, Module, Line);
    239      1.1    jruoho     if (ACPI_FAILURE (Status))
    240      1.1    jruoho     {
    241      1.1    jruoho         AcpiOsFree (Allocation);
    242      1.1    jruoho         return (NULL);
    243      1.1    jruoho     }
    244      1.1    jruoho 
    245      1.1    jruoho     AcpiGbl_GlobalList->TotalAllocated++;
    246      1.1    jruoho     AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
    247      1.1    jruoho     AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
    248      1.1    jruoho     if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
    249      1.1    jruoho     {
    250      1.1    jruoho         AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
    251      1.1    jruoho     }
    252      1.1    jruoho 
    253      1.1    jruoho     return ((void *) &Allocation->UserSpace);
    254      1.1    jruoho }
    255      1.1    jruoho 
    256      1.1    jruoho 
    257      1.1    jruoho /*******************************************************************************
    258      1.1    jruoho  *
    259      1.1    jruoho  * FUNCTION:    AcpiUtFreeAndTrack
    260      1.1    jruoho  *
    261      1.1    jruoho  * PARAMETERS:  Allocation          - Address of the memory to deallocate
    262      1.1    jruoho  *              Component           - Component type of caller
    263      1.1    jruoho  *              Module              - Source file name of caller
    264      1.1    jruoho  *              Line                - Line number of caller
    265      1.1    jruoho  *
    266      1.1    jruoho  * RETURN:      None
    267      1.1    jruoho  *
    268      1.1    jruoho  * DESCRIPTION: Frees the memory at Allocation
    269      1.1    jruoho  *
    270      1.1    jruoho  ******************************************************************************/
    271      1.1    jruoho 
    272      1.1    jruoho void
    273      1.1    jruoho AcpiUtFreeAndTrack (
    274      1.1    jruoho     void                    *Allocation,
    275      1.1    jruoho     UINT32                  Component,
    276      1.1    jruoho     const char              *Module,
    277      1.1    jruoho     UINT32                  Line)
    278      1.1    jruoho {
    279      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *DebugBlock;
    280      1.1    jruoho     ACPI_STATUS             Status;
    281      1.1    jruoho 
    282      1.1    jruoho 
    283      1.1    jruoho     ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
    284      1.1    jruoho 
    285      1.1    jruoho 
    286      1.1    jruoho     if (NULL == Allocation)
    287      1.1    jruoho     {
    288      1.1    jruoho         ACPI_ERROR ((Module, Line,
    289      1.1    jruoho             "Attempt to delete a NULL address"));
    290      1.1    jruoho 
    291      1.1    jruoho         return_VOID;
    292      1.1    jruoho     }
    293      1.1    jruoho 
    294      1.1    jruoho     DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
    295      1.1    jruoho                     (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
    296      1.1    jruoho 
    297      1.1    jruoho     AcpiGbl_GlobalList->TotalFreed++;
    298      1.1    jruoho     AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
    299      1.1    jruoho 
    300      1.1    jruoho     Status = AcpiUtRemoveAllocation (DebugBlock,
    301      1.1    jruoho                     Component, Module, Line);
    302      1.1    jruoho     if (ACPI_FAILURE (Status))
    303      1.1    jruoho     {
    304      1.1    jruoho         ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
    305      1.1    jruoho     }
    306      1.1    jruoho 
    307      1.1    jruoho     AcpiOsFree (DebugBlock);
    308      1.5  christos     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed (block %p)\n",
    309      1.5  christos         Allocation, DebugBlock));
    310      1.1    jruoho     return_VOID;
    311      1.1    jruoho }
    312      1.1    jruoho 
    313      1.1    jruoho 
    314      1.1    jruoho /*******************************************************************************
    315      1.1    jruoho  *
    316      1.1    jruoho  * FUNCTION:    AcpiUtFindAllocation
    317      1.1    jruoho  *
    318      1.1    jruoho  * PARAMETERS:  Allocation              - Address of allocated memory
    319      1.1    jruoho  *
    320      1.4  christos  * RETURN:      Three cases:
    321      1.4  christos  *              1) List is empty, NULL is returned.
    322      1.4  christos  *              2) Element was found. Returns Allocation parameter.
    323      1.4  christos  *              3) Element was not found. Returns position where it should be
    324      1.4  christos  *                  inserted into the list.
    325      1.1    jruoho  *
    326      1.1    jruoho  * DESCRIPTION: Searches for an element in the global allocation tracking list.
    327      1.4  christos  *              If the element is not found, returns the location within the
    328      1.4  christos  *              list where the element should be inserted.
    329      1.4  christos  *
    330      1.4  christos  *              Note: The list is ordered by larger-to-smaller addresses.
    331      1.4  christos  *
    332      1.4  christos  *              This global list is used to detect memory leaks in ACPICA as
    333      1.4  christos  *              well as other issues such as an attempt to release the same
    334      1.4  christos  *              internal object more than once. Although expensive as far
    335      1.4  christos  *              as cpu time, this list is much more helpful for finding these
    336      1.4  christos  *              types of issues than using memory leak detectors outside of
    337      1.4  christos  *              the ACPICA code.
    338      1.1    jruoho  *
    339      1.1    jruoho  ******************************************************************************/
    340      1.1    jruoho 
    341      1.1    jruoho static ACPI_DEBUG_MEM_BLOCK *
    342      1.1    jruoho AcpiUtFindAllocation (
    343      1.4  christos     ACPI_DEBUG_MEM_BLOCK    *Allocation)
    344      1.1    jruoho {
    345      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Element;
    346      1.1    jruoho 
    347      1.1    jruoho 
    348      1.1    jruoho     Element = AcpiGbl_GlobalList->ListHead;
    349      1.4  christos     if (!Element)
    350      1.4  christos     {
    351      1.4  christos         return (NULL);
    352      1.4  christos     }
    353      1.1    jruoho 
    354      1.4  christos     /*
    355      1.4  christos      * Search for the address.
    356      1.4  christos      *
    357      1.4  christos      * Note: List is ordered by larger-to-smaller addresses, on the
    358      1.4  christos      * assumption that a new allocation usually has a larger address
    359      1.4  christos      * than previous allocations.
    360      1.4  christos      */
    361      1.4  christos     while (Element > Allocation)
    362      1.4  christos     {
    363      1.4  christos         /* Check for end-of-list */
    364      1.1    jruoho 
    365      1.4  christos         if (!Element->Next)
    366      1.1    jruoho         {
    367      1.1    jruoho             return (Element);
    368      1.1    jruoho         }
    369      1.1    jruoho 
    370      1.1    jruoho         Element = Element->Next;
    371      1.1    jruoho     }
    372      1.1    jruoho 
    373      1.4  christos     if (Element == Allocation)
    374      1.4  christos     {
    375      1.4  christos         return (Element);
    376      1.4  christos     }
    377      1.4  christos 
    378      1.4  christos     return (Element->Previous);
    379      1.1    jruoho }
    380      1.1    jruoho 
    381      1.1    jruoho 
    382      1.1    jruoho /*******************************************************************************
    383      1.1    jruoho  *
    384      1.1    jruoho  * FUNCTION:    AcpiUtTrackAllocation
    385      1.1    jruoho  *
    386      1.1    jruoho  * PARAMETERS:  Allocation          - Address of allocated memory
    387      1.1    jruoho  *              Size                - Size of the allocation
    388      1.1    jruoho  *              AllocType           - MEM_MALLOC or MEM_CALLOC
    389      1.1    jruoho  *              Component           - Component type of caller
    390      1.1    jruoho  *              Module              - Source file name of caller
    391      1.1    jruoho  *              Line                - Line number of caller
    392      1.1    jruoho  *
    393      1.4  christos  * RETURN:      Status
    394      1.1    jruoho  *
    395      1.1    jruoho  * DESCRIPTION: Inserts an element into the global allocation tracking list.
    396      1.1    jruoho  *
    397      1.1    jruoho  ******************************************************************************/
    398      1.1    jruoho 
    399      1.1    jruoho static ACPI_STATUS
    400      1.1    jruoho AcpiUtTrackAllocation (
    401      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Allocation,
    402      1.1    jruoho     ACPI_SIZE               Size,
    403      1.1    jruoho     UINT8                   AllocType,
    404      1.1    jruoho     UINT32                  Component,
    405      1.1    jruoho     const char              *Module,
    406      1.1    jruoho     UINT32                  Line)
    407      1.1    jruoho {
    408      1.1    jruoho     ACPI_MEMORY_LIST        *MemList;
    409      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Element;
    410      1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    411      1.1    jruoho 
    412      1.1    jruoho 
    413      1.1    jruoho     ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
    414      1.1    jruoho 
    415      1.1    jruoho 
    416      1.1    jruoho     if (AcpiGbl_DisableMemTracking)
    417      1.1    jruoho     {
    418      1.1    jruoho         return_ACPI_STATUS (AE_OK);
    419      1.1    jruoho     }
    420      1.1    jruoho 
    421      1.1    jruoho     MemList = AcpiGbl_GlobalList;
    422      1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
    423      1.1    jruoho     if (ACPI_FAILURE (Status))
    424      1.1    jruoho     {
    425      1.1    jruoho         return_ACPI_STATUS (Status);
    426      1.1    jruoho     }
    427      1.1    jruoho 
    428      1.1    jruoho     /*
    429      1.4  christos      * Search the global list for this address to make sure it is not
    430      1.4  christos      * already present. This will catch several kinds of problems.
    431      1.1    jruoho      */
    432      1.1    jruoho     Element = AcpiUtFindAllocation (Allocation);
    433      1.4  christos     if (Element == Allocation)
    434      1.1    jruoho     {
    435      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    436      1.4  christos             "UtTrackAllocation: Allocation (%p) already present in global list!",
    437      1.1    jruoho             Allocation));
    438      1.1    jruoho         goto UnlockAndExit;
    439      1.1    jruoho     }
    440      1.1    jruoho 
    441      1.4  christos     /* Fill in the instance data */
    442      1.1    jruoho 
    443      1.1    jruoho     Allocation->Size      = (UINT32) Size;
    444      1.1    jruoho     Allocation->AllocType = AllocType;
    445      1.1    jruoho     Allocation->Component = Component;
    446      1.1    jruoho     Allocation->Line      = Line;
    447      1.1    jruoho 
    448  1.5.2.2     skrll     strncpy (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
    449      1.1    jruoho     Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
    450      1.1    jruoho 
    451      1.4  christos     if (!Element)
    452      1.4  christos     {
    453      1.4  christos         /* Insert at list head */
    454      1.4  christos 
    455      1.4  christos         if (MemList->ListHead)
    456      1.4  christos         {
    457      1.4  christos             ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
    458      1.4  christos         }
    459      1.4  christos 
    460      1.4  christos         Allocation->Next = MemList->ListHead;
    461      1.4  christos         Allocation->Previous = NULL;
    462      1.1    jruoho 
    463      1.4  christos         MemList->ListHead = Allocation;
    464      1.4  christos     }
    465      1.4  christos     else
    466      1.1    jruoho     {
    467      1.4  christos         /* Insert after element */
    468      1.4  christos 
    469      1.4  christos         Allocation->Next = Element->Next;
    470      1.4  christos         Allocation->Previous = Element;
    471      1.1    jruoho 
    472      1.4  christos         if (Element->Next)
    473      1.4  christos         {
    474      1.4  christos             (Element->Next)->Previous = Allocation;
    475      1.4  christos         }
    476      1.1    jruoho 
    477      1.4  christos         Element->Next = Allocation;
    478      1.4  christos     }
    479      1.1    jruoho 
    480      1.1    jruoho 
    481      1.1    jruoho UnlockAndExit:
    482      1.1    jruoho     Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
    483      1.1    jruoho     return_ACPI_STATUS (Status);
    484      1.1    jruoho }
    485      1.1    jruoho 
    486      1.1    jruoho 
    487      1.1    jruoho /*******************************************************************************
    488      1.1    jruoho  *
    489      1.1    jruoho  * FUNCTION:    AcpiUtRemoveAllocation
    490      1.1    jruoho  *
    491      1.1    jruoho  * PARAMETERS:  Allocation          - Address of allocated memory
    492      1.1    jruoho  *              Component           - Component type of caller
    493      1.1    jruoho  *              Module              - Source file name of caller
    494      1.1    jruoho  *              Line                - Line number of caller
    495      1.1    jruoho  *
    496      1.4  christos  * RETURN:      Status
    497      1.1    jruoho  *
    498      1.1    jruoho  * DESCRIPTION: Deletes an element from the global allocation tracking list.
    499      1.1    jruoho  *
    500      1.1    jruoho  ******************************************************************************/
    501      1.1    jruoho 
    502      1.1    jruoho static ACPI_STATUS
    503      1.1    jruoho AcpiUtRemoveAllocation (
    504      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Allocation,
    505      1.1    jruoho     UINT32                  Component,
    506      1.1    jruoho     const char              *Module,
    507      1.1    jruoho     UINT32                  Line)
    508      1.1    jruoho {
    509      1.1    jruoho     ACPI_MEMORY_LIST        *MemList;
    510      1.1    jruoho     ACPI_STATUS             Status;
    511      1.1    jruoho 
    512      1.1    jruoho 
    513      1.4  christos     ACPI_FUNCTION_NAME (UtRemoveAllocation);
    514      1.1    jruoho 
    515      1.1    jruoho 
    516      1.1    jruoho     if (AcpiGbl_DisableMemTracking)
    517      1.1    jruoho     {
    518      1.4  christos         return (AE_OK);
    519      1.1    jruoho     }
    520      1.1    jruoho 
    521      1.1    jruoho     MemList = AcpiGbl_GlobalList;
    522      1.1    jruoho     if (NULL == MemList->ListHead)
    523      1.1    jruoho     {
    524      1.1    jruoho         /* No allocations! */
    525      1.1    jruoho 
    526      1.1    jruoho         ACPI_ERROR ((Module, Line,
    527      1.1    jruoho             "Empty allocation list, nothing to free!"));
    528      1.1    jruoho 
    529      1.4  christos         return (AE_OK);
    530      1.1    jruoho     }
    531      1.1    jruoho 
    532      1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
    533      1.1    jruoho     if (ACPI_FAILURE (Status))
    534      1.1    jruoho     {
    535      1.4  christos         return (Status);
    536      1.1    jruoho     }
    537      1.1    jruoho 
    538      1.1    jruoho     /* Unlink */
    539      1.1    jruoho 
    540      1.1    jruoho     if (Allocation->Previous)
    541      1.1    jruoho     {
    542      1.1    jruoho         (Allocation->Previous)->Next = Allocation->Next;
    543      1.1    jruoho     }
    544      1.1    jruoho     else
    545      1.1    jruoho     {
    546      1.1    jruoho         MemList->ListHead = Allocation->Next;
    547      1.1    jruoho     }
    548      1.1    jruoho 
    549      1.1    jruoho     if (Allocation->Next)
    550      1.1    jruoho     {
    551      1.1    jruoho         (Allocation->Next)->Previous = Allocation->Previous;
    552      1.1    jruoho     }
    553      1.1    jruoho 
    554      1.4  christos     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing %p, size 0%X\n",
    555      1.4  christos         &Allocation->UserSpace, Allocation->Size));
    556      1.4  christos 
    557      1.1    jruoho     /* Mark the segment as deleted */
    558      1.1    jruoho 
    559  1.5.2.2     skrll     memset (&Allocation->UserSpace, 0xEA, Allocation->Size);
    560      1.1    jruoho 
    561      1.1    jruoho     Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
    562      1.4  christos     return (Status);
    563      1.1    jruoho }
    564      1.1    jruoho 
    565      1.1    jruoho 
    566      1.1    jruoho /*******************************************************************************
    567      1.1    jruoho  *
    568      1.1    jruoho  * FUNCTION:    AcpiUtDumpAllocationInfo
    569      1.1    jruoho  *
    570      1.4  christos  * PARAMETERS:  None
    571      1.1    jruoho  *
    572      1.1    jruoho  * RETURN:      None
    573      1.1    jruoho  *
    574      1.1    jruoho  * DESCRIPTION: Print some info about the outstanding allocations.
    575      1.1    jruoho  *
    576      1.1    jruoho  ******************************************************************************/
    577      1.1    jruoho 
    578      1.1    jruoho void
    579      1.1    jruoho AcpiUtDumpAllocationInfo (
    580      1.1    jruoho     void)
    581      1.1    jruoho {
    582      1.1    jruoho /*
    583      1.1    jruoho     ACPI_MEMORY_LIST        *MemList;
    584      1.1    jruoho */
    585      1.1    jruoho 
    586      1.1    jruoho     ACPI_FUNCTION_TRACE (UtDumpAllocationInfo);
    587      1.1    jruoho 
    588      1.1    jruoho /*
    589      1.1    jruoho     ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
    590      1.1    jruoho                     ("%30s: %4d (%3d Kb)\n", "Current allocations",
    591      1.1    jruoho                     MemList->CurrentCount,
    592      1.1    jruoho                     ROUND_UP_TO_1K (MemList->CurrentSize)));
    593      1.1    jruoho 
    594      1.1    jruoho     ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
    595      1.1    jruoho                     ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
    596      1.1    jruoho                     MemList->MaxConcurrentCount,
    597      1.1    jruoho                     ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
    598      1.1    jruoho 
    599      1.1    jruoho 
    600      1.1    jruoho     ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
    601      1.1    jruoho                     ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
    602      1.1    jruoho                     RunningObjectCount,
    603      1.1    jruoho                     ROUND_UP_TO_1K (RunningObjectSize)));
    604      1.1    jruoho 
    605      1.1    jruoho     ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
    606      1.1    jruoho                     ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
    607      1.1    jruoho                     RunningAllocCount,
    608      1.1    jruoho                     ROUND_UP_TO_1K (RunningAllocSize)));
    609      1.1    jruoho 
    610      1.1    jruoho 
    611      1.1    jruoho     ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
    612      1.1    jruoho                     ("%30s: %4d (%3d Kb)\n", "Current Nodes",
    613      1.1    jruoho                     AcpiGbl_CurrentNodeCount,
    614      1.1    jruoho                     ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
    615      1.1    jruoho 
    616      1.1    jruoho     ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
    617      1.1    jruoho                     ("%30s: %4d (%3d Kb)\n", "Max Nodes",
    618      1.1    jruoho                     AcpiGbl_MaxConcurrentNodeCount,
    619      1.1    jruoho                     ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
    620      1.1    jruoho                         sizeof (ACPI_NAMESPACE_NODE)))));
    621      1.1    jruoho */
    622      1.1    jruoho     return_VOID;
    623      1.1    jruoho }
    624      1.1    jruoho 
    625      1.1    jruoho 
    626      1.1    jruoho /*******************************************************************************
    627      1.1    jruoho  *
    628      1.1    jruoho  * FUNCTION:    AcpiUtDumpAllocations
    629      1.1    jruoho  *
    630      1.1    jruoho  * PARAMETERS:  Component           - Component(s) to dump info for.
    631      1.4  christos  *              Module              - Module to dump info for. NULL means all.
    632      1.1    jruoho  *
    633      1.1    jruoho  * RETURN:      None
    634      1.1    jruoho  *
    635      1.1    jruoho  * DESCRIPTION: Print a list of all outstanding allocations.
    636      1.1    jruoho  *
    637      1.1    jruoho  ******************************************************************************/
    638      1.1    jruoho 
    639      1.1    jruoho void
    640      1.1    jruoho AcpiUtDumpAllocations (
    641      1.1    jruoho     UINT32                  Component,
    642      1.1    jruoho     const char              *Module)
    643      1.1    jruoho {
    644      1.1    jruoho     ACPI_DEBUG_MEM_BLOCK    *Element;
    645      1.1    jruoho     ACPI_DESCRIPTOR         *Descriptor;
    646      1.1    jruoho     UINT32                  NumOutstanding = 0;
    647      1.1    jruoho     UINT8                   DescriptorType;
    648      1.1    jruoho 
    649      1.1    jruoho 
    650      1.1    jruoho     ACPI_FUNCTION_TRACE (UtDumpAllocations);
    651      1.1    jruoho 
    652      1.1    jruoho 
    653      1.1    jruoho     if (AcpiGbl_DisableMemTracking)
    654      1.1    jruoho     {
    655      1.4  christos         return_VOID;
    656      1.1    jruoho     }
    657      1.1    jruoho 
    658      1.1    jruoho     /*
    659      1.1    jruoho      * Walk the allocation list.
    660      1.1    jruoho      */
    661      1.1    jruoho     if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
    662      1.1    jruoho     {
    663      1.4  christos         return_VOID;
    664      1.1    jruoho     }
    665      1.1    jruoho 
    666      1.1    jruoho     Element = AcpiGbl_GlobalList->ListHead;
    667      1.1    jruoho     while (Element)
    668      1.1    jruoho     {
    669      1.1    jruoho         if ((Element->Component & Component) &&
    670  1.5.2.2     skrll             ((Module == NULL) || (0 == strcmp (Module, Element->Module))))
    671      1.1    jruoho         {
    672      1.1    jruoho             Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
    673      1.1    jruoho 
    674      1.1    jruoho             if (Element->Size < sizeof (ACPI_COMMON_DESCRIPTOR))
    675      1.1    jruoho             {
    676      1.1    jruoho                 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
    677      1.1    jruoho                     "[Not a Descriptor - too small]\n",
    678      1.1    jruoho                     Descriptor, Element->Size, Element->Module,
    679      1.1    jruoho                     Element->Line);
    680      1.1    jruoho             }
    681      1.1    jruoho             else
    682      1.1    jruoho             {
    683      1.1    jruoho                 /* Ignore allocated objects that are in a cache */
    684      1.1    jruoho 
    685      1.1    jruoho                 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
    686      1.1    jruoho                 {
    687      1.1    jruoho                     AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
    688      1.1    jruoho                         Descriptor, Element->Size, Element->Module,
    689      1.1    jruoho                         Element->Line, AcpiUtGetDescriptorName (Descriptor));
    690      1.1    jruoho 
    691      1.1    jruoho                     /* Validate the descriptor type using Type field and length */
    692      1.1    jruoho 
    693      1.1    jruoho                     DescriptorType = 0; /* Not a valid descriptor type */
    694      1.1    jruoho 
    695      1.1    jruoho                     switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
    696      1.1    jruoho                     {
    697      1.1    jruoho                     case ACPI_DESC_TYPE_OPERAND:
    698      1.4  christos 
    699      1.4  christos                         if (Element->Size == sizeof (ACPI_OPERAND_OBJECT))
    700      1.1    jruoho                         {
    701      1.1    jruoho                             DescriptorType = ACPI_DESC_TYPE_OPERAND;
    702      1.1    jruoho                         }
    703      1.1    jruoho                         break;
    704      1.1    jruoho 
    705      1.1    jruoho                     case ACPI_DESC_TYPE_PARSER:
    706      1.4  christos 
    707      1.4  christos                         if (Element->Size == sizeof (ACPI_PARSE_OBJECT))
    708      1.1    jruoho                         {
    709      1.1    jruoho                             DescriptorType = ACPI_DESC_TYPE_PARSER;
    710      1.1    jruoho                         }
    711      1.1    jruoho                         break;
    712      1.1    jruoho 
    713      1.1    jruoho                     case ACPI_DESC_TYPE_NAMED:
    714      1.4  christos 
    715      1.4  christos                         if (Element->Size == sizeof (ACPI_NAMESPACE_NODE))
    716      1.1    jruoho                         {
    717      1.1    jruoho                             DescriptorType = ACPI_DESC_TYPE_NAMED;
    718      1.1    jruoho                         }
    719      1.1    jruoho                         break;
    720      1.1    jruoho 
    721      1.1    jruoho                     default:
    722      1.4  christos 
    723      1.1    jruoho                         break;
    724      1.1    jruoho                     }
    725      1.1    jruoho 
    726      1.1    jruoho                     /* Display additional info for the major descriptor types */
    727      1.1    jruoho 
    728      1.1    jruoho                     switch (DescriptorType)
    729      1.1    jruoho                     {
    730      1.1    jruoho                     case ACPI_DESC_TYPE_OPERAND:
    731      1.4  christos 
    732      1.1    jruoho                         AcpiOsPrintf ("%12.12s  RefCount 0x%04X\n",
    733      1.1    jruoho                             AcpiUtGetTypeName (Descriptor->Object.Common.Type),
    734      1.1    jruoho                             Descriptor->Object.Common.ReferenceCount);
    735      1.1    jruoho                         break;
    736      1.1    jruoho 
    737      1.1    jruoho                     case ACPI_DESC_TYPE_PARSER:
    738      1.4  christos 
    739      1.1    jruoho                         AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
    740      1.1    jruoho                             Descriptor->Op.Asl.AmlOpcode);
    741      1.1    jruoho                         break;
    742      1.1    jruoho 
    743      1.1    jruoho                     case ACPI_DESC_TYPE_NAMED:
    744      1.4  christos 
    745      1.1    jruoho                         AcpiOsPrintf ("%4.4s\n",
    746      1.1    jruoho                             AcpiUtGetNodeName (&Descriptor->Node));
    747      1.1    jruoho                         break;
    748      1.1    jruoho 
    749      1.1    jruoho                     default:
    750      1.4  christos 
    751      1.1    jruoho                         AcpiOsPrintf ( "\n");
    752      1.1    jruoho                         break;
    753      1.1    jruoho                     }
    754      1.1    jruoho                 }
    755      1.1    jruoho             }
    756      1.1    jruoho 
    757      1.1    jruoho             NumOutstanding++;
    758      1.1    jruoho         }
    759      1.1    jruoho 
    760      1.1    jruoho         Element = Element->Next;
    761      1.1    jruoho     }
    762      1.1    jruoho 
    763      1.1    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
    764      1.1    jruoho 
    765      1.1    jruoho     /* Print summary */
    766      1.1    jruoho 
    767      1.1    jruoho     if (!NumOutstanding)
    768      1.1    jruoho     {
    769      1.1    jruoho         ACPI_INFO ((AE_INFO, "No outstanding allocations"));
    770      1.1    jruoho     }
    771      1.1    jruoho     else
    772      1.1    jruoho     {
    773      1.1    jruoho         ACPI_ERROR ((AE_INFO, "%u(0x%X) Outstanding allocations",
    774      1.1    jruoho             NumOutstanding, NumOutstanding));
    775      1.1    jruoho     }
    776      1.1    jruoho 
    777      1.1    jruoho     return_VOID;
    778      1.1    jruoho }
    779      1.1    jruoho 
    780      1.1    jruoho #endif  /* ACPI_DBG_TRACK_ALLOCATIONS */
    781