]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/MyAlloc.h
2 Header file for memory allocation tracking functions.
4 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Common/BaseTypes.h>
25 // Default operation is to use the memory allocation tracking functions.
26 // To over-ride add "#define USE_MYALLOC 0" to your program header and/or
27 // source files as needed. Or, just do not include this header file in
36 // Replace C library allocation routines with MyAlloc routines.
38 #define malloc(size) MyAlloc ((size), __FILE__, __LINE__)
39 #define calloc(count, size) MyAlloc ((count) * (size), __FILE__, __LINE__)
40 #define realloc(ptr, size) MyRealloc ((ptr), (size), __FILE__, __LINE__)
41 #define free(ptr) MyFree ((ptr), __FILE__, __LINE__)
42 #define alloc_check(final) MyCheck ((final), __FILE__, __LINE__)
45 // Structure for checking/tracking memory allocations.
47 typedef struct MyAllocStruct
{
49 struct MyAllocStruct
*Next
;
56 // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
59 // Next := Pointer to next allocation structure in the list.
63 // Size := Size of allocation request.
65 // File := Pointer to __FILE__ string stored immediately following
66 // MY_ALLOC_STRUCT in memory.
68 // Buffer := Pointer to UINT32 aligned storage immediately following
69 // the NULL terminated __FILE__ string. This is UINT32
70 // aligned because the underflow signature is 32-bits and
71 // this will place the first caller address on a 64-bit
75 // Signatures used to check for buffer overflow/underflow conditions.
77 #define MYALLOC_HEAD_MAGIK 0xBADFACED
78 #define MYALLOC_TAIL_MAGIK 0xDEADBEEF
91 // Check for corruptions in the allocated memory chain. If a corruption
92 // is detection program operation stops w/ an exit(1) call.
96 // Final := When FALSE, MyCheck() returns if the allocated memory chain
97 // has not been corrupted. When TRUE, MyCheck() returns if there
98 // are no un-freed allocations. If there are un-freed allocations,
99 // they are displayed and exit(1) is called.
102 // File := Set to __FILE__ by macro expansion.
104 // Line := Set to __LINE__ by macro expansion.
123 // Allocate a new link in the allocation chain along with enough storage
124 // for the File[] string, requested Size and alignment overhead. If
125 // memory cannot be allocated or the allocation chain has been corrupted,
126 // exit(1) will be called.
130 // Size := Number of bytes (UINT8) requested by the called.
131 // Size cannot be zero.
133 // File := Set to __FILE__ by macro expansion.
135 // Line := Set to __LINE__ by macro expansion.
139 // Pointer to the caller's buffer.
155 // This does a MyAlloc(), memcpy() and MyFree(). There is no optimization
156 // for shrinking or expanding buffers. An invalid parameter will cause
157 // MyRealloc() to fail with a call to exit(1).
161 // Ptr := Pointer to the caller's buffer to be re-allocated.
162 // Ptr cannot be NULL.
164 // Size := Size of new buffer. Size cannot be zero.
166 // File := Set to __FILE__ by macro expansion.
168 // Line := Set to __LINE__ by macro expansion.
172 // Pointer to new caller's buffer.
187 // Release a previously allocated buffer. Invalid parameters will cause
188 // MyFree() to fail with an exit(1) call.
192 // Ptr := Pointer to the caller's buffer to be freed.
193 // A NULL pointer will be ignored.
195 // File := Set to __FILE__ by macro expansion.
197 // Line := Set to __LINE__ by macro expansion.
205 #else /* USE_MYALLOC */
208 // Nothing to do when USE_MYALLOC is zero.
210 #define alloc_check(final)
212 #endif /* USE_MYALLOC */
213 #endif /* _MYALLOC_H_ */
215 /* eof - MyAlloc.h */