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