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