]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/MyAlloc.h
3 Copyright (c) 2004 - 2008, 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
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.
18 Header file for memory allocation tracking functions.
29 #include <Common/BaseTypes.h>
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
43 // Replace C library allocation routines with MyAlloc routines.
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__)
52 // Structure for checking/tracking memory allocations.
54 typedef struct MyAllocStruct
{
56 struct MyAllocStruct
*Next
;
63 // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
66 // Next := Pointer to next allocation structure in the list.
70 // Size := Size of allocation request.
72 // File := Pointer to __FILE__ string stored immediately following
73 // MY_ALLOC_STRUCT in memory.
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
82 // Signatures used to check for buffer overflow/underflow conditions.
84 #define MYALLOC_HEAD_MAGIK 0xBADFACED
85 #define MYALLOC_TAIL_MAGIK 0xDEADBEEF
98 // Check for corruptions in the allocated memory chain. If a corruption
99 // is detection program operation stops w/ an exit(1) call.
103 // Final := When FALSE, MyCheck() returns if the allocated memory chain
104 // has not been corrupted. When TRUE, MyCheck() returns if there
105 // are no un-freed allocations. If there are un-freed allocations,
106 // they are displayed and exit(1) is called.
109 // File := Set to __FILE__ by macro expansion.
111 // Line := Set to __LINE__ by macro expansion.
130 // Allocate a new link in the allocation chain along with enough storage
131 // for the File[] string, requested Size and alignment overhead. If
132 // memory cannot be allocated or the allocation chain has been corrupted,
133 // exit(1) will be called.
137 // Size := Number of bytes (UINT8) requested by the called.
138 // Size cannot be zero.
140 // File := Set to __FILE__ by macro expansion.
142 // Line := Set to __LINE__ by macro expansion.
146 // Pointer to the caller's buffer.
162 // This does a MyAlloc(), memcpy() and MyFree(). There is no optimization
163 // for shrinking or expanding buffers. An invalid parameter will cause
164 // MyRealloc() to fail with a call to exit(1).
168 // Ptr := Pointer to the caller's buffer to be re-allocated.
169 // Ptr cannot be NULL.
171 // Size := Size of new buffer. Size cannot be zero.
173 // File := Set to __FILE__ by macro expansion.
175 // Line := Set to __LINE__ by macro expansion.
179 // Pointer to new caller's buffer.
194 // Release a previously allocated buffer. Invalid parameters will cause
195 // MyFree() to fail with an exit(1) call.
199 // Ptr := Pointer to the caller's buffer to be freed.
200 // A NULL pointer will be ignored.
202 // File := Set to __FILE__ by macro expansion.
204 // Line := Set to __LINE__ by macro expansion.
212 #else /* USE_MYALLOC */
215 // Nothing to do when USE_MYALLOC is zero.
217 #define alloc_check(final)
219 #endif /* USE_MYALLOC */
220 #endif /* _MYALLOC_H_ */
222 /* eof - MyAlloc.h */