]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/Common/MyAlloc.h
3 Copyright (c) 2004, Intel Corporation
4 All rights reserved. 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.
35 // Default operation is to use the memory allocation tracking functions.
36 // To over-ride add "#define USE_MYALLOC 0" to your program header and/or
37 // source files as needed. Or, just do not include this header file in
46 // Replace C library allocation routines with MyAlloc routines.
48 #define malloc(size) MyAlloc ((size), __FILE__, __LINE__)
49 #define calloc(count, size) MyAlloc ((count) * (size), __FILE__, __LINE__)
50 #define realloc(ptr, size) MyRealloc ((ptr), (size), __FILE__, __LINE__)
51 #define free(ptr) MyFree ((ptr), __FILE__, __LINE__)
52 #define alloc_check(final) MyCheck ((final), __FILE__, __LINE__)
55 // Structure for checking/tracking memory allocations.
57 typedef struct MyAllocStruct
{
59 struct MyAllocStruct
*Next
;
66 // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
69 // Next := Pointer to next allocation structure in the list.
73 // Size := Size of allocation request.
75 // File := Pointer to __FILE__ string stored immediately following
76 // MY_ALLOC_STRUCT in memory.
78 // Buffer := Pointer to UINT32 aligned storage immediately following
79 // the NULL terminated __FILE__ string. This is UINT32
80 // aligned because the underflow signature is 32-bits and
81 // this will place the first caller address on a 64-bit
85 // Signatures used to check for buffer overflow/underflow conditions.
87 #define MYALLOC_HEAD_MAGIK 0xBADFACED
88 #define MYALLOC_TAIL_MAGIK 0xDEADBEEF
101 // Check for corruptions in the allocated memory chain. If a corruption
102 // is detection program operation stops w/ an exit(1) call.
106 // Final := When FALSE, MyCheck() returns if the allocated memory chain
107 // has not been corrupted. When TRUE, MyCheck() returns if there
108 // are no un-freed allocations. If there are un-freed allocations,
109 // they are displayed and exit(1) is called.
112 // File := Set to __FILE__ by macro expansion.
114 // Line := Set to __LINE__ by macro expansion.
133 // Allocate a new link in the allocation chain along with enough storage
134 // for the File[] string, requested Size and alignment overhead. If
135 // memory cannot be allocated or the allocation chain has been corrupted,
136 // exit(1) will be called.
140 // Size := Number of bytes (UINT8) requested by the called.
141 // Size cannot be zero.
143 // File := Set to __FILE__ by macro expansion.
145 // Line := Set to __LINE__ by macro expansion.
149 // Pointer to the caller's buffer.
165 // This does a MyAlloc(), memcpy() and MyFree(). There is no optimization
166 // for shrinking or expanding buffers. An invalid parameter will cause
167 // MyRealloc() to fail with a call to exit(1).
171 // Ptr := Pointer to the caller's buffer to be re-allocated.
172 // Ptr cannot be NULL.
174 // Size := Size of new buffer. Size cannot be zero.
176 // File := Set to __FILE__ by macro expansion.
178 // Line := Set to __LINE__ by macro expansion.
182 // Pointer to new caller's buffer.
197 // Release a previously allocated buffer. Invalid parameters will cause
198 // MyFree() to fail with an exit(1) call.
202 // Ptr := Pointer to the caller's buffer to be freed.
203 // A NULL pointer will be ignored.
205 // File := Set to __FILE__ by macro expansion.
207 // Line := Set to __LINE__ by macro expansion.
215 #else /* USE_MYALLOC */
218 // Nothing to do when USE_MYALLOC is zero.
220 #define alloc_check(final)
222 #endif /* USE_MYALLOC */
223 #endif /* _MYALLOC_H_ */
225 /* eof - MyAlloc.h */