]>
Commit | Line | Data |
---|---|---|
30fdf114 | 1 | /** @file\r |
97fa0ee9 | 2 | Header file for memory allocation tracking functions.\r |
30fdf114 | 3 | \r |
f7496d71 LG |
4 | Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
30fdf114 | 12 | \r |
30fdf114 LG |
13 | **/\r |
14 | \r | |
15 | #ifndef _MYALLOC_H_\r | |
16 | #define _MYALLOC_H_\r | |
17 | \r | |
18 | #include <stdio.h>\r | |
19 | #include <stdlib.h>\r | |
20 | #include <string.h>\r | |
21 | \r | |
22 | #include <Common/BaseTypes.h>\r | |
23 | \r | |
24 | //\r | |
25 | // Default operation is to use the memory allocation tracking functions.\r | |
26 | // To over-ride add "#define USE_MYALLOC 0" to your program header and/or\r | |
27 | // source files as needed. Or, just do not include this header file in\r | |
28 | // your project.\r | |
29 | //\r | |
30 | #ifndef USE_MYALLOC\r | |
31 | #define USE_MYALLOC 1\r | |
32 | #endif\r | |
33 | \r | |
34 | #if USE_MYALLOC\r | |
35 | //\r | |
36 | // Replace C library allocation routines with MyAlloc routines.\r | |
37 | //\r | |
38 | #define malloc(size) MyAlloc ((size), __FILE__, __LINE__)\r | |
39 | #define calloc(count, size) MyAlloc ((count) * (size), __FILE__, __LINE__)\r | |
40 | #define realloc(ptr, size) MyRealloc ((ptr), (size), __FILE__, __LINE__)\r | |
41 | #define free(ptr) MyFree ((ptr), __FILE__, __LINE__)\r | |
42 | #define alloc_check(final) MyCheck ((final), __FILE__, __LINE__)\r | |
43 | \r | |
44 | //\r | |
45 | // Structure for checking/tracking memory allocations.\r | |
46 | //\r | |
47 | typedef struct MyAllocStruct {\r | |
48 | UINTN Cksum;\r | |
49 | struct MyAllocStruct *Next;\r | |
50 | UINTN Line;\r | |
51 | UINTN Size;\r | |
52 | UINT8 *File;\r | |
53 | UINT8 *Buffer;\r | |
54 | } MY_ALLOC_STRUCT;\r | |
55 | //\r | |
56 | // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +\r | |
57 | // (UINTN)Buffer;\r | |
58 | //\r | |
59 | // Next := Pointer to next allocation structure in the list.\r | |
60 | //\r | |
61 | // Line := __LINE__\r | |
62 | //\r | |
63 | // Size := Size of allocation request.\r | |
64 | //\r | |
65 | // File := Pointer to __FILE__ string stored immediately following\r | |
66 | // MY_ALLOC_STRUCT in memory.\r | |
67 | //\r | |
68 | // Buffer := Pointer to UINT32 aligned storage immediately following\r | |
69 | // the NULL terminated __FILE__ string. This is UINT32\r | |
70 | // aligned because the underflow signature is 32-bits and\r | |
71 | // this will place the first caller address on a 64-bit\r | |
72 | // boundary.\r | |
73 | //\r | |
74 | //\r | |
75 | // Signatures used to check for buffer overflow/underflow conditions.\r | |
76 | //\r | |
77 | #define MYALLOC_HEAD_MAGIK 0xBADFACED\r | |
78 | #define MYALLOC_TAIL_MAGIK 0xDEADBEEF\r | |
79 | \r | |
80 | VOID\r | |
81 | MyCheck (\r | |
82 | BOOLEAN Final,\r | |
83 | UINT8 File[],\r | |
84 | UINTN Line\r | |
85 | )\r | |
86 | ;\r | |
87 | //\r | |
88 | // *++\r | |
89 | // Description:\r | |
90 | //\r | |
91 | // Check for corruptions in the allocated memory chain. If a corruption\r | |
92 | // is detection program operation stops w/ an exit(1) call.\r | |
93 | //\r | |
94 | // Parameters:\r | |
95 | //\r | |
96 | // Final := When FALSE, MyCheck() returns if the allocated memory chain\r | |
97 | // has not been corrupted. When TRUE, MyCheck() returns if there\r | |
98 | // are no un-freed allocations. If there are un-freed allocations,\r | |
99 | // they are displayed and exit(1) is called.\r | |
100 | //\r | |
101 | //\r | |
102 | // File := Set to __FILE__ by macro expansion.\r | |
103 | //\r | |
104 | // Line := Set to __LINE__ by macro expansion.\r | |
105 | //\r | |
106 | // Returns:\r | |
107 | //\r | |
108 | // n/a\r | |
109 | //\r | |
110 | // --*/\r | |
111 | //\r | |
112 | VOID *\r | |
113 | MyAlloc (\r | |
114 | UINTN Size,\r | |
115 | UINT8 File[],\r | |
116 | UINTN Line\r | |
117 | )\r | |
118 | ;\r | |
119 | //\r | |
120 | // *++\r | |
121 | // Description:\r | |
122 | //\r | |
123 | // Allocate a new link in the allocation chain along with enough storage\r | |
124 | // for the File[] string, requested Size and alignment overhead. If\r | |
125 | // memory cannot be allocated or the allocation chain has been corrupted,\r | |
126 | // exit(1) will be called.\r | |
127 | //\r | |
128 | // Parameters:\r | |
129 | //\r | |
130 | // Size := Number of bytes (UINT8) requested by the called.\r | |
131 | // Size cannot be zero.\r | |
132 | //\r | |
133 | // File := Set to __FILE__ by macro expansion.\r | |
134 | //\r | |
135 | // Line := Set to __LINE__ by macro expansion.\r | |
136 | //\r | |
137 | // Returns:\r | |
138 | //\r | |
139 | // Pointer to the caller's buffer.\r | |
140 | //\r | |
141 | // --*/\r | |
142 | //\r | |
143 | VOID *\r | |
144 | MyRealloc (\r | |
145 | VOID *Ptr,\r | |
146 | UINTN Size,\r | |
147 | UINT8 File[],\r | |
148 | UINTN Line\r | |
149 | )\r | |
150 | ;\r | |
151 | //\r | |
152 | // *++\r | |
153 | // Description:\r | |
154 | //\r | |
155 | // This does a MyAlloc(), memcpy() and MyFree(). There is no optimization\r | |
156 | // for shrinking or expanding buffers. An invalid parameter will cause\r | |
157 | // MyRealloc() to fail with a call to exit(1).\r | |
158 | //\r | |
159 | // Parameters:\r | |
160 | //\r | |
161 | // Ptr := Pointer to the caller's buffer to be re-allocated.\r | |
162 | // Ptr cannot be NULL.\r | |
163 | //\r | |
164 | // Size := Size of new buffer. Size cannot be zero.\r | |
165 | //\r | |
166 | // File := Set to __FILE__ by macro expansion.\r | |
167 | //\r | |
168 | // Line := Set to __LINE__ by macro expansion.\r | |
169 | //\r | |
170 | // Returns:\r | |
171 | //\r | |
172 | // Pointer to new caller's buffer.\r | |
173 | //\r | |
174 | // --*/\r | |
175 | //\r | |
176 | VOID\r | |
177 | MyFree (\r | |
178 | VOID *Ptr,\r | |
179 | UINT8 File[],\r | |
180 | UINTN Line\r | |
181 | )\r | |
182 | ;\r | |
183 | //\r | |
184 | // *++\r | |
185 | // Description:\r | |
186 | //\r | |
187 | // Release a previously allocated buffer. Invalid parameters will cause\r | |
188 | // MyFree() to fail with an exit(1) call.\r | |
189 | //\r | |
190 | // Parameters:\r | |
191 | //\r | |
192 | // Ptr := Pointer to the caller's buffer to be freed.\r | |
193 | // A NULL pointer will be ignored.\r | |
194 | //\r | |
195 | // File := Set to __FILE__ by macro expansion.\r | |
196 | //\r | |
197 | // Line := Set to __LINE__ by macro expansion.\r | |
198 | //\r | |
199 | // Returns:\r | |
200 | //\r | |
201 | // n/a\r | |
202 | //\r | |
203 | // --*/\r | |
204 | //\r | |
205 | #else /* USE_MYALLOC */\r | |
206 | \r | |
207 | //\r | |
208 | // Nothing to do when USE_MYALLOC is zero.\r | |
209 | //\r | |
210 | #define alloc_check(final)\r | |
211 | \r | |
212 | #endif /* USE_MYALLOC */\r | |
213 | #endif /* _MYALLOC_H_ */\r | |
214 | \r | |
215 | /* eof - MyAlloc.h */\r |