]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/Common/MyAlloc.h
d766682ea1bffafef15837d07ef9c1be53596cb1
[mirror_edk2.git] / Tools / Source / TianoTools / Common / MyAlloc.h
1 /*++
2
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
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 #include <Base.h>
29
30 /*
31 #include "Tiano.h"
32 */
33
34 //
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
38 // your project.
39 //
40 #ifndef USE_MYALLOC
41 #define USE_MYALLOC 1
42 #endif
43
44 #if USE_MYALLOC
45 //
46 // Replace C library allocation routines with MyAlloc routines.
47 //
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__)
53
54 //
55 // Structure for checking/tracking memory allocations.
56 //
57 typedef struct MyAllocStruct {
58 UINTN Cksum;
59 struct MyAllocStruct *Next;
60 UINTN Line;
61 UINTN Size;
62 UINT8 *File;
63 UINT8 *Buffer;
64 } MY_ALLOC_STRUCT;
65 //
66 // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
67 // (UINTN)Buffer;
68 //
69 // Next := Pointer to next allocation structure in the list.
70 //
71 // Line := __LINE__
72 //
73 // Size := Size of allocation request.
74 //
75 // File := Pointer to __FILE__ string stored immediately following
76 // MY_ALLOC_STRUCT in memory.
77 //
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
82 // boundary.
83 //
84 //
85 // Signatures used to check for buffer overflow/underflow conditions.
86 //
87 #define MYALLOC_HEAD_MAGIK 0xBADFACED
88 #define MYALLOC_TAIL_MAGIK 0xDEADBEEF
89
90 VOID
91 MyCheck (
92 BOOLEAN Final,
93 UINT8 File[],
94 UINTN Line
95 )
96 ;
97 //
98 // *++
99 // Description:
100 //
101 // Check for corruptions in the allocated memory chain. If a corruption
102 // is detection program operation stops w/ an exit(1) call.
103 //
104 // Parameters:
105 //
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.
110 //
111 //
112 // File := Set to __FILE__ by macro expansion.
113 //
114 // Line := Set to __LINE__ by macro expansion.
115 //
116 // Returns:
117 //
118 // n/a
119 //
120 // --*/
121 //
122 VOID *
123 MyAlloc (
124 UINTN Size,
125 UINT8 File[],
126 UINTN Line
127 )
128 ;
129 //
130 // *++
131 // Description:
132 //
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.
137 //
138 // Parameters:
139 //
140 // Size := Number of bytes (UINT8) requested by the called.
141 // Size cannot be zero.
142 //
143 // File := Set to __FILE__ by macro expansion.
144 //
145 // Line := Set to __LINE__ by macro expansion.
146 //
147 // Returns:
148 //
149 // Pointer to the caller's buffer.
150 //
151 // --*/
152 //
153 VOID *
154 MyRealloc (
155 VOID *Ptr,
156 UINTN Size,
157 UINT8 File[],
158 UINTN Line
159 )
160 ;
161 //
162 // *++
163 // Description:
164 //
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).
168 //
169 // Parameters:
170 //
171 // Ptr := Pointer to the caller's buffer to be re-allocated.
172 // Ptr cannot be NULL.
173 //
174 // Size := Size of new buffer. Size cannot be zero.
175 //
176 // File := Set to __FILE__ by macro expansion.
177 //
178 // Line := Set to __LINE__ by macro expansion.
179 //
180 // Returns:
181 //
182 // Pointer to new caller's buffer.
183 //
184 // --*/
185 //
186 VOID
187 MyFree (
188 VOID *Ptr,
189 UINT8 File[],
190 UINTN Line
191 )
192 ;
193 //
194 // *++
195 // Description:
196 //
197 // Release a previously allocated buffer. Invalid parameters will cause
198 // MyFree() to fail with an exit(1) call.
199 //
200 // Parameters:
201 //
202 // Ptr := Pointer to the caller's buffer to be freed.
203 // A NULL pointer will be ignored.
204 //
205 // File := Set to __FILE__ by macro expansion.
206 //
207 // Line := Set to __LINE__ by macro expansion.
208 //
209 // Returns:
210 //
211 // n/a
212 //
213 // --*/
214 //
215 #else /* USE_MYALLOC */
216
217 //
218 // Nothing to do when USE_MYALLOC is zero.
219 //
220 #define alloc_check(final)
221
222 #endif /* USE_MYALLOC */
223 #endif /* _MYALLOC_H_ */
224
225 /* eof - MyAlloc.h */