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