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