Home | History | Annotate | Download | only in Common
      1 /*++
      2 
      3 Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>
      4 This program and the accompanying materials
      5 are licensed and made available under the terms and conditions of the BSD License
      6 which accompanies this distribution.  The full text of the license may be found at
      7 http://opensource.org/licenses/bsd-license.php
      8 
      9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     11 
     12 Module Name:
     13 
     14   MyAlloc.h
     15 
     16 Abstract:
     17 
     18   Header file for memory allocation tracking functions.
     19 
     20 --*/
     21 
     22 #ifndef _MYALLOC_H_
     23 #define _MYALLOC_H_
     24 
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <string.h>
     28 
     29 #include "Tiano.h"
     30 
     31 //
     32 // Default operation is to use the memory allocation tracking functions.
     33 // To over-ride add "#define USE_MYALLOC 0" to your program header and/or
     34 // source files as needed.  Or, just do not include this header file in
     35 // your project.
     36 //
     37 #ifndef USE_MYALLOC
     38 #define USE_MYALLOC 1
     39 #endif
     40 
     41 #if USE_MYALLOC
     42 //
     43 // Replace C library allocation routines with MyAlloc routines.
     44 //
     45 #define malloc(size)        MyAlloc ((size), __FILE__, __LINE__)
     46 #define calloc(count, size) MyAlloc ((count) * (size), __FILE__, __LINE__)
     47 #define realloc(ptr, size)  MyRealloc ((ptr), (size), __FILE__, __LINE__)
     48 #define free(ptr)           MyFree ((ptr), __FILE__, __LINE__)
     49 #define alloc_check(final)  MyCheck ((final), __FILE__, __LINE__)
     50 
     51 //
     52 // Structure for checking/tracking memory allocations.
     53 //
     54 typedef struct MyAllocStruct {
     55   UINTN                 Cksum;
     56   struct MyAllocStruct  *Next;
     57   UINTN                 Line;
     58   UINTN                 Size;
     59   UINT8                 *File;
     60   UINT8                 *Buffer;
     61 } MY_ALLOC_STRUCT;
     62 //
     63 // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
     64 //          (UINTN)Buffer;
     65 //
     66 // Next := Pointer to next allocation structure in the list.
     67 //
     68 // Line := __LINE__
     69 //
     70 // Size := Size of allocation request.
     71 //
     72 // File := Pointer to __FILE__ string stored immediately following
     73 //         MY_ALLOC_STRUCT in memory.
     74 //
     75 // Buffer := Pointer to UINT32 aligned storage immediately following
     76 //           the NULL terminated __FILE__ string.  This is UINT32
     77 //           aligned because the underflow signature is 32-bits and
     78 //           this will place the first caller address on a 64-bit
     79 //           boundary.
     80 //
     81 //
     82 // Signatures used to check for buffer overflow/underflow conditions.
     83 //
     84 #define MYALLOC_HEAD_MAGIK  0xBADFACED
     85 #define MYALLOC_TAIL_MAGIK  0xDEADBEEF
     86 
     87 VOID
     88 MyCheck (
     89   BOOLEAN      Final,
     90   UINT8        File[],
     91   UINTN        Line
     92   );
     93 //
     94 // *++
     95 // Description:
     96 //
     97 //  Check for corruptions in the allocated memory chain.  If a corruption
     98 //  is detection program operation stops w/ an exit(1) call.
     99 //
    100 // Parameters:
    101 //
    102 //  Final := When FALSE, MyCheck() returns if the allocated memory chain
    103 //           has not been corrupted.  When TRUE, MyCheck() returns if there
    104 //           are no un-freed allocations.  If there are un-freed allocations,
    105 //           they are displayed and exit(1) is called.
    106 //
    107 //
    108 //  File := Set to __FILE__ by macro expansion.
    109 //
    110 //  Line := Set to __LINE__ by macro expansion.
    111 //
    112 // Returns:
    113 //
    114 //  n/a
    115 //
    116 // --*/
    117 //
    118 VOID  *
    119 MyAlloc (
    120   UINTN      Size,
    121   UINT8      File[],
    122   UINTN      Line
    123   );
    124 //
    125 // *++
    126 // Description:
    127 //
    128 //  Allocate a new link in the allocation chain along with enough storage
    129 //  for the File[] string, requested Size and alignment overhead.  If
    130 //  memory cannot be allocated or the allocation chain has been corrupted,
    131 //  exit(1) will be called.
    132 //
    133 // Parameters:
    134 //
    135 //  Size := Number of bytes (UINT8) requested by the called.
    136 //          Size cannot be zero.
    137 //
    138 //  File := Set to __FILE__ by macro expansion.
    139 //
    140 //  Line := Set to __LINE__ by macro expansion.
    141 //
    142 // Returns:
    143 //
    144 //  Pointer to the caller's buffer.
    145 //
    146 // --*/
    147 //
    148 VOID  *
    149 MyRealloc (
    150   VOID       *Ptr,
    151   UINTN      Size,
    152   UINT8      File[],
    153   UINTN      Line
    154   );
    155 //
    156 // *++
    157 // Description:
    158 //
    159 //  This does a MyAlloc(), memcpy() and MyFree().  There is no optimization
    160 //  for shrinking or expanding buffers.  An invalid parameter will cause
    161 //  MyRealloc() to fail with a call to exit(1).
    162 //
    163 // Parameters:
    164 //
    165 //  Ptr := Pointer to the caller's buffer to be re-allocated.
    166 //         Ptr cannot be NULL.
    167 //
    168 //  Size := Size of new buffer.  Size cannot be zero.
    169 //
    170 //  File := Set to __FILE__ by macro expansion.
    171 //
    172 //  Line := Set to __LINE__ by macro expansion.
    173 //
    174 // Returns:
    175 //
    176 //  Pointer to new caller's buffer.
    177 //
    178 // --*/
    179 //
    180 VOID
    181 MyFree (
    182   VOID       *Ptr,
    183   UINT8      File[],
    184   UINTN      Line
    185   );
    186 //
    187 // *++
    188 // Description:
    189 //
    190 //  Release a previously allocated buffer.  Invalid parameters will cause
    191 //  MyFree() to fail with an exit(1) call.
    192 //
    193 // Parameters:
    194 //
    195 //  Ptr := Pointer to the caller's buffer to be freed.
    196 //         A NULL pointer will be ignored.
    197 //
    198 //  File := Set to __FILE__ by macro expansion.
    199 //
    200 //  Line := Set to __LINE__ by macro expansion.
    201 //
    202 // Returns:
    203 //
    204 //  n/a
    205 //
    206 // --*/
    207 //
    208 #else /* USE_MYALLOC */
    209 
    210 //
    211 // Nothing to do when USE_MYALLOC is zero.
    212 //
    213 #define alloc_check(final)
    214 
    215 #endif /* USE_MYALLOC */
    216 #endif /* _MYALLOC_H_ */
    217 
    218 /* eof - MyAlloc.h */
    219