ReactOS 0.4.15-dev-5666-gc548b97
mem_tools.h
Go to the documentation of this file.
1
2// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
3// All rights reserved
4// This file was released under the GPLv2 on June 2015.
6
7#ifndef __MY_MEM_TOOLS_H__
8#define __MY_MEM_TOOLS_H__
9
10#define MY_HEAP_FLAG_USED 0x00000001
11#define MY_HEAP_FLAG_LEN_MASK 0xfffffffe
12
13#define MyFreeMemoryAndPointer(ptr) \
14 if(ptr) { \
15 MyFreePool__(ptr); \
16 ptr = NULL; \
17 }
18
19#define MY_USE_ALIGN
20//#define MY_MEM_BOUNDS_CHECK
21
22typedef struct _MEM_ALLOC_DESC {
25#ifdef MY_HEAP_TRACK_OWNERS
26 USHORT Src;
28#endif
29#ifdef MY_HEAP_TRACK_REF
30// PCHAR Ref;
31 PCHAR Tag;
32#endif
34
35typedef struct _MEM_FRAME_ALLOC_DESC {
41
42extern PCHAR BreakAddr;
44
45#define MY_HEAP_FRAME_SIZE (256*1024)
46#define MY_HEAP_MAX_FRAMES 512
47#define MY_HEAP_MAX_BLOCKS 4*1024 // blocks per frame
48
49#ifdef USE_THREAD_HEAPS
50//extern HANDLE MemLock;
51extern "C" VOID ExInitThreadPools();
52extern "C" VOID ExDeInitThreadPools();
53extern "C" VOID ExFreeThreadPool();
54#endif //USE_THREAD_HEAPS
55
56// Mem
59#ifdef MY_HEAP_TRACK_OWNERS
61 #ifdef MY_HEAP_TRACK_REF
62 ,PCHAR Tag
63 #endif //MY_HEAP_TRACK_REF
64 );
66#else
68 #ifdef MY_HEAP_TRACK_REF
69 ,PCHAR Tag
70 #endif //MY_HEAP_TRACK_REF
71 );
73#endif
75
76#ifdef MY_HEAP_CHECK_BOUNDS
77 #define MY_HEAP_ALIGN 63
78#else
79 #define MY_HEAP_ALIGN 63
80#endif
81#define PAGE_SIZE_ALIGN (PAGE_SIZE - 1)
82
83#define AlignToPageSize(size) (((size)+PAGE_SIZE_ALIGN)&(~PAGE_SIZE_ALIGN))
84#define MyAlignSize__(size) (((size)+MY_HEAP_ALIGN)&(~MY_HEAP_ALIGN))
85#ifdef MY_HEAP_FORCE_NONPAGED
86#define MyFixMemType(type) NonPagedPool
87#else //MY_HEAP_FORCE_NONPAGED
88#define MyFixMemType(type) (type)
89#endif //MY_HEAP_FORCE_NONPAGED
90
91#ifdef MY_USE_INTERNAL_MEMMANAGER
92
93#ifdef MY_HEAP_TRACK_OWNERS
94 #ifdef MY_HEAP_TRACK_REF
95 #define MyAllocatePool__(type,size) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__, NULL)
96 #define MyAllocatePoolTag__(type,size,tag) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__, (PCHAR)(tag))
97 #else
98 #define MyAllocatePool__(type,size) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__)
99 #define MyAllocatePoolTag__(type,size,tag) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__)
100 #endif //MY_HEAP_TRACK_REF
101#else //MY_HEAP_TRACK_OWNERS
102 #ifdef MY_HEAP_TRACK_REF
103 #define MyAllocatePool__(type,size) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size), NULL)
104 #define MyAllocatePoolTag__(type,size,tag) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size), (PCHAR)(tag))
105 #else
106 #define MyAllocatePool__(type,size) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size))
107 #define MyAllocatePoolTag__(type,size,tag) MyAllocatePool(MyFixMemType(type), MyAlignSize__(size))
108 #endif //MY_HEAP_TRACK_REF
109#endif //MY_HEAP_TRACK_OWNERS
110
111#define MyFreePool__(addr) MyFreePool((PCHAR)(addr))
112
113#ifdef MY_HEAP_TRACK_OWNERS
114#define MyReallocPool__(addr, len, pnewaddr, newlen) MyReallocPool((PCHAR)(addr), MyAlignSize__(len), pnewaddr, MyAlignSize__(newlen), UDF_BUG_CHECK_ID, __LINE__)
115#else
116#define MyReallocPool__(addr, len, pnewaddr, newlen) MyReallocPool((PCHAR)(addr), MyAlignSize__(len), pnewaddr, MyAlignSize__(newlen))
117#endif
118
119#ifdef UDF_DBG
120LONG
121MyFindMemBaseByAddr(
122 PCHAR addr
123 );
124
125#define MyCheckArray(base, index) \
126 ASSERT(MyFindMemBaseByAddr((PCHAR)(base)) == MyFindMemBaseByAddr((PCHAR)(base+(index))))
127
128#else
129#define MyCheckArray(base, index)
130#endif //UDF_DBG
131
132
133#else //MY_USE_INTERNAL_MEMMANAGER
134
135#ifndef MY_USE_ALIGN
136#undef MyAlignSize__
137#define MyAlignSize__(size) (size)
138#endif
139
140BOOLEAN inline MyAllocInit(VOID) {return TRUE;}
141#define MyAllocRelease()
142
143#ifndef MY_MEM_BOUNDS_CHECK
144
145 #ifdef TRACK_SYS_ALLOC_CALLERS
146 #define MyAllocatePool__(type,size) DebugAllocatePool(NonPagedPool,MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__)
147 #define MyAllocatePoolTag__(type,size,tag) DebugAllocatePool(NonPagedPool,MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__)
148 #else //TRACK_SYS_ALLOC_CALLERS
149 #define MyAllocatePool__(type,size) DbgAllocatePoolWithTag(NonPagedPool,MyAlignSize__(size), 'fNWD')
150 #define MyAllocatePoolTag__(type,size,tag) DbgAllocatePoolWithTag(NonPagedPool,MyAlignSize__(size), tag)
151 #endif //TRACK_SYS_ALLOC_CALLERS
152 #define MyFreePool__(addr) DbgFreePool((PCHAR)(addr))
153
154#else //MY_MEM_BOUNDS_CHECK
155
156 #ifdef TRACK_SYS_ALLOC_CALLERS
157 #define MyAllocatePool_(type,size) DebugAllocatePool(NonPagedPool,MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__)
158 #define MyAllocatePoolTag_(type,size,tag) DebugAllocatePool(NonPagedPool,MyAlignSize__(size), UDF_BUG_CHECK_ID, __LINE__)
159 #else //TRACK_SYS_ALLOC_CALLERS
160 #define MyAllocatePool_(type,size) DbgAllocatePoolWithTag(NonPagedPool,MyAlignSize__(size), 'mNWD')
161 #define MyAllocatePoolTag_(type,size,tag) DbgAllocatePoolWithTag(NonPagedPool,MyAlignSize__(size), tag)
162 #endif //TRACK_SYS_ALLOC_CALLERS
163 #define MyFreePool_(addr) DbgFreePool((PCHAR)(addr))
164
165#define MyAllocatePool__(type,size) MyAllocatePoolTag__(type,size,'mNWD')
166
167/*
168PVOID inline MyAllocatePool__(ULONG type, ULONG len) {
169 PCHAR newaddr;
170 ULONG i;
171// newaddr = (PCHAR)MyAllocatePool_(type, len+MY_HEAP_ALIGN+1);
172#ifdef TRACK_SYS_ALLOC_CALLERS
173 newaddr = (PCHAR)DebugAllocatePool(type,len+MY_HEAP_ALIGN+1, 0x202, __LINE__);
174#else //TRACK_SYS_ALLOC_CALLERS
175 newaddr = (PCHAR)MyAllocatePool_(type,len+MY_HEAP_ALIGN+1);
176#endif //TRACK_SYS_ALLOC_CALLERS
177 if(!newaddr)
178 return NULL;
179 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
180 newaddr[len+i] = (UCHAR)('A'+i);
181 }
182 return newaddr;
183}
184*/
185
187 PCHAR newaddr;
188 ULONG i;
189// newaddr = (PCHAR)MyAllocatePoolTag_(type, len+MY_HEAP_ALIGN+1, tag);
190#ifdef TRACK_SYS_ALLOC_CALLERS
191 newaddr = (PCHAR)DebugAllocatePool(type,len+MY_HEAP_ALIGN+1, 0x202, __LINE__);
192#else //TRACK_SYS_ALLOC_CALLERS
193 newaddr = (PCHAR)MyAllocatePoolTag_(type,len+MY_HEAP_ALIGN+1, tag);
194#endif //TRACK_SYS_ALLOC_CALLERS
195 if(!newaddr)
196 return NULL;
197 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
198 newaddr[len+i] = (UCHAR)('A'+i);
199 }
200 return newaddr;
201}
202
203VOID inline MyFreePool__(PVOID addr) {
204 PCHAR newaddr;
205// ULONG i;
206 newaddr = (PCHAR)addr;
207 if(!newaddr) {
208 __asm int 3;
209 return;
210 }
211/*
212 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
213 if(newaddr[len+i] != (UCHAR)('A'+i)) {
214 __asm int 3;
215 break;
216 }
217 }
218*/
219 MyFreePool_(newaddr);
220}
221
222#endif //MY_MEM_BOUNDS_CHECK
223
224/* This function just scares the hell out of GCC */
225#ifdef __GNUC__
226#pragma GCC diagnostic push
227#pragma GCC diagnostic ignored "-Wstringop-overflow"
228#endif
229
230ULONG inline MyReallocPool__(PCHAR addr, ULONG len, PCHAR *pnewaddr, ULONG newlen) {
231 ULONG _len, _newlen;
232 _newlen = MyAlignSize__(newlen);
233 _len = MyAlignSize__(len);
234 PCHAR newaddr;
235
236 ASSERT(len && newlen);
237
238#ifdef MY_MEM_BOUNDS_CHECK
239 ULONG i;
240
241 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
242 if((UCHAR)(addr[len+i]) != (UCHAR)('A'+i)) {
243 __asm int 3;
244 break;
245 }
246 }
247#endif //MY_MEM_BOUNDS_CHECK
248
249 if ((_newlen != _len)
250#ifdef MY_MEM_BOUNDS_CHECK
251 || TRUE
252#endif //MY_MEM_BOUNDS_CHECK
253 ) {
254#ifdef TRACK_SYS_ALLOC_CALLERS
255 newaddr = (PCHAR)DebugAllocatePool(NonPagedPool,_newlen, 0x202, __LINE__);
256#else //TRACK_SYS_ALLOC_CALLERS
257 newaddr = (PCHAR)MyAllocatePool__(NonPagedPool,_newlen);
258#endif //TRACK_SYS_ALLOC_CALLERS
259 if (!newaddr) {
260 __debugbreak();
261 *pnewaddr = addr;
262 return 0;
263 }
264#ifdef MY_MEM_BOUNDS_CHECK
265 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
266 newaddr[newlen+i] = (UCHAR)('A'+i);
267 }
268#endif //MY_MEM_BOUNDS_CHECK
269 *pnewaddr = newaddr;
270 if(_newlen <= _len) {
271 RtlCopyMemory(newaddr, addr, newlen);
272 } else {
273 RtlCopyMemory(newaddr, addr, len);
274 RtlZeroMemory(newaddr+len, _newlen - len);
275 }
276#ifdef MY_MEM_BOUNDS_CHECK
277 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
278 if((UCHAR)(newaddr[newlen+i]) != (UCHAR)('A'+i)) {
279 __asm int 3;
280 break;
281 }
282 }
283#endif //MY_MEM_BOUNDS_CHECK
284
286 } else {
287 *pnewaddr = addr;
288 }
289 if(newlen > len) {
290 //RtlZeroMemory(newaddr+len, newlen - len);
291 }
292/*
293#ifdef MY_MEM_BOUNDS_CHECK
294 for(i=0; i<MY_HEAP_ALIGN+1; i++) {
295 newaddr[newlen+i] = (UCHAR)('A'+i);
296 }
297#endif //MY_MEM_BOUNDS_CHECK
298*/
299 return newlen;
300}
301#ifdef __GNUC__
302#pragma GCC diagnostic pop
303#endif
304
305#ifndef MY_USE_ALIGN
306#undef MyAlignSize__
307#define MyAlignSize__(size) (((size)+MY_HEAP_ALIGN)&(~MY_HEAP_ALIGN))
308#endif
309
310#define MyCheckArray(base, index)
311
312#endif // MY_USE_INTERNAL_MEMMANAGER
313
314#endif // __MY_MEM_TOOLS_H__
static USHORT USHORT * NewLength
unsigned char BOOLEAN
Type
Definition: Type.h:7
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define NonPagedPool
Definition: env_spec_w32.h:307
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLsizeiptr size
Definition: glext.h:5919
GLenum const GLvoid * addr
Definition: glext.h:9621
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
signed char * PCHAR
Definition: retypes.h:7
unsigned int ULONG
Definition: retypes.h:1
#define MyAllocatePool__(type, size)
Definition: mem_tools.h:149
struct _MEM_FRAME_ALLOC_DESC * PMEM_FRAME_ALLOC_DESC
BOOLEAN MyAllocInit(VOID)
Definition: mem_tools.h:140
#define MyAlignSize__(size)
Definition: mem_tools.h:84
#define MyAllocRelease()
Definition: mem_tools.h:141
#define MY_HEAP_ALIGN
Definition: mem_tools.h:79
#define MyFreePool__(addr)
Definition: mem_tools.h:152
PCHAR BreakAddr
ULONG MyReallocPool__(PCHAR addr, ULONG len, PCHAR *pnewaddr, ULONG newlen)
Definition: mem_tools.h:230
PCHAR __fastcall MyAllocatePool(ULONG Type, ULONG size)
ULONG MemTotalAllocated
ULONG __fastcall MyReallocPool(PCHAR addr, ULONG OldLength, PCHAR *NewBuff, ULONG NewLength)
struct _MEM_ALLOC_DESC * PMEM_ALLOC_DESC
struct _MEM_ALLOC_DESC MEM_ALLOC_DESC
VOID __fastcall MyFreePool(PCHAR addr)
struct _MEM_FRAME_ALLOC_DESC MEM_FRAME_ALLOC_DESC
#define MyAllocatePoolTag__(type, size, tag)
Definition: mem_tools.h:150
#define PCHAR
Definition: match.c:90
#define ASSERT(a)
Definition: mode.c:44
#define __fastcall
Definition: sync.c:38
long LONG
Definition: pedump.c:60
unsigned short USHORT
Definition: pedump.c:61
Definition: ncftp.h:79
PMEM_ALLOC_DESC Frame
Definition: mem_tools.h:36
Definition: ecma_167.h:138
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID Tag
Definition: wdfdevice.h:4065
unsigned char UCHAR
Definition: xmlstorage.h:181