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