]>
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 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Common/BaseTypes.h>
19 // Default operation is to use the memory allocation tracking functions.
20 // To over-ride add "#define USE_MYALLOC 0" to your program header and/or
21 // source files as needed. Or, just do not include this header file in
30 // Replace C library allocation routines with MyAlloc routines.
32 #define malloc(size) MyAlloc ((size), __FILE__, __LINE__)
33 #define calloc(count, size) MyAlloc ((count) * (size), __FILE__, __LINE__)
34 #define realloc(ptr, size) MyRealloc ((ptr), (size), __FILE__, __LINE__)
35 #define free(ptr) MyFree ((ptr), __FILE__, __LINE__)
36 #define alloc_check(final) MyCheck ((final), __FILE__, __LINE__)
39 // Structure for checking/tracking memory allocations.
41 typedef struct MyAllocStruct
{
43 struct MyAllocStruct
*Next
;
50 // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
53 // Next := Pointer to next allocation structure in the list.
57 // Size := Size of allocation request.
59 // File := Pointer to __FILE__ string stored immediately following
60 // MY_ALLOC_STRUCT in memory.
62 // Buffer := Pointer to UINT32 aligned storage immediately following
63 // the NULL terminated __FILE__ string. This is UINT32
64 // aligned because the underflow signature is 32-bits and
65 // this will place the first caller address on a 64-bit
69 // Signatures used to check for buffer overflow/underflow conditions.
71 #define MYALLOC_HEAD_MAGIK 0xBADFACED
72 #define MYALLOC_TAIL_MAGIK 0xDEADBEEF
85 // Check for corruptions in the allocated memory chain. If a corruption
86 // is detection program operation stops w/ an exit(1) call.
90 // Final := When FALSE, MyCheck() returns if the allocated memory chain
91 // has not been corrupted. When TRUE, MyCheck() returns if there
92 // are no un-freed allocations. If there are un-freed allocations,
93 // they are displayed and exit(1) is called.
96 // File := Set to __FILE__ by macro expansion.
98 // Line := Set to __LINE__ by macro expansion.
117 // Allocate a new link in the allocation chain along with enough storage
118 // for the File[] string, requested Size and alignment overhead. If
119 // memory cannot be allocated or the allocation chain has been corrupted,
120 // exit(1) will be called.
124 // Size := Number of bytes (UINT8) requested by the called.
125 // Size cannot be zero.
127 // File := Set to __FILE__ by macro expansion.
129 // Line := Set to __LINE__ by macro expansion.
133 // Pointer to the caller's buffer.
149 // This does a MyAlloc(), memcpy() and MyFree(). There is no optimization
150 // for shrinking or expanding buffers. An invalid parameter will cause
151 // MyRealloc() to fail with a call to exit(1).
155 // Ptr := Pointer to the caller's buffer to be re-allocated.
156 // Ptr cannot be NULL.
158 // Size := Size of new buffer. Size cannot be zero.
160 // File := Set to __FILE__ by macro expansion.
162 // Line := Set to __LINE__ by macro expansion.
166 // Pointer to new caller's buffer.
181 // Release a previously allocated buffer. Invalid parameters will cause
182 // MyFree() to fail with an exit(1) call.
186 // Ptr := Pointer to the caller's buffer to be freed.
187 // A NULL pointer will be ignored.
189 // File := Set to __FILE__ by macro expansion.
191 // Line := Set to __LINE__ by macro expansion.
199 #else /* USE_MYALLOC */
202 // Nothing to do when USE_MYALLOC is zero.
204 #define alloc_check(final)
206 #endif /* USE_MYALLOC */
207 #endif /* _MYALLOC_H_ */
209 /* eof - MyAlloc.h */