-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmemalloc.c
280 lines (244 loc) · 7.75 KB
/
memalloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: Memory allocation routines.
*
****************************************************************************/
#if defined(__UNIX__) && defined(__GNUC__)
#include <sys/mman.h>
#endif
#include "globals.h"
#include "memalloc.h"
/* what items are stored in the heap?
* - symbols + symbol names ( asym, dsym; symbol.c )
* - macro lines ( StoreMacro(); macro.c )
* - file names ( CurrFName[]; assemble.c )
* - temp items + buffers ( omf.c, bin.c, coff.c, elf.c )
* - contexts ( reused; context.c )
* - codeview debug info ( dbgcv.c )
* - library names ( includelib; directiv.c )
* - src lines for FASTPASS ( fastpass.c )
* - fixups ( fixup.c )
* - hll items (reused; .IF, .WHILE, .REPEAT; hll.c )
* - one big input buffer ( src line buffer, tokenarray, string buffer; input.c )
* - src filenames array ( AddFile(); input.c )
* - line number info ( -Zd, -Zi; linnum.c )
* - macro parameter array + default values ( macro.c )
* - prologue, epilogue macro names ??? ( option.c )
* - dll names ( OPTION DLLIMPORT; option.c )
* - std queue items ( see queues in struct module_vars; globals.h, queue.c )
* - renamed keyword queue ( reswords.c )
* - safeseh queue ( safeseh.c )
* - segment alias names ( segment.c )
* - segment stack ( segment.c )
* - segment buffers ( 1024 for omf, else may be HUGE ) ( segment.c )
* - segment names for simplified segment directives (simsegm.c )
* - strings of text macros ( string.c )
* - struct/union/record member items + default values ( types.c )
* - procedure prologue argument, debug info ( proc.c )
*/
#ifndef alloca
#define alloca(x) __builtin_alloca(x)
#endif
#if FASTMEM
/* FASTMEM is a simple memory alloc approach which allocates chunks of 512 kB
* and will release it only at MemFini().
*
* May be considered to create an additional "line heap" to store lines of
* loop macros and generated code - since this is hierarchical, a simple
* Mark/Release mechanism will do the memory management.
* currently generated code lines are stored in the C heap, while
* loop macro lines go to the "fastmem" heap.
*/
#define BLKSIZE 0x80000
#ifndef __UNIX__
#if defined(__OS2__)
#include <os2.h>
#elif defined(__DJGPP__)
#include <dpmi.h>
#else
#include "win32.h"
#endif
#endif
struct linked_list {
struct linked_list *next;
};
static struct linked_list *pBase; /* start list of 512 kB blocks; to be moved to ModuleInfo.g */
static uint_8 *pCurr; /* points into current block; to be moved to ModuleInfo.g */
static uint_32 currfree; /* free memory left in current block; to be moved to ModuleInfo.g */
#ifdef DEBUG_OUT
static int blocks; /* number of blocks allocated so far; to be moved to ModuleInfo.g */
#endif
#if defined(__UNIX__) && defined(__WATCOMC__)
#define SYS_mmap 90
#define SYS_munmap 91
uint_32 sys_call1( uint_32 func, uint_32 r_ebx );
#pragma aux sys_call1 = \
"int 0x80" \
parm [eax] [ebx] \
value [eax];
uint_32 sys_call2( uint_32 func, uint_32 r_ebx, uint_32 r_ecx );
#pragma aux sys_call2 = \
"int 0x80" \
parm [eax] [ebx] [ecx] \
value [eax];
struct mmap {
uint_32 base; /* linear base (or 0) */
uint_32 size; /* size in bytes */
uint_32 access; /* 3 = PROT_READ | PROT_WRITE */
uint_32 flags; /* 0x22 = MAP_PRIVATE | MAP_ANON */
uint_32 fd; /* should be -1 */
uint_32 offset; /* ignored */
};
static struct mmap mymmap = { 0, 0, 3, 0x22, -1, 0 };
#endif
/* Allow clang on Linux to build */
#ifndef MAP_ANON
#define MAP_ANON 0x20
#endif
#ifndef __UNIX__
#if defined(__OS2__)
#define BLKALLOC( p, size ) DosAllocMem( (void**)&p, size, PAG_COMMIT|PAG_READ|PAG_WRITE )
#define BLKFREE( p ) DosFreeMem( p )
#elif defined(__NT__) || defined(_WIN64)
#define BLKALLOC( p, size ) p = (uint_8 *)VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_READWRITE )
#define BLKFREE( p ) VirtualFree( p, 0, MEM_RELEASE )
#else
#define BLKALLOC( p, size ) p = malloc( size )
#define BLKFREE( p ) free( p )
#endif
#else
#if defined(__WATCOMC__)
#define BLKALLOC( p, size_ ) mymmap.size = size_; \
p = (uint_8 *)sys_call1( SYS_mmap, (uint_32)&mymmap )
#define BLKFREE( p ) sys_call2( SYS_munmap, (uint_32)p, 0 )
#else
#define BLKALLOC( p, size ) p = (uint_8 *)mmap( 0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0 ); \
if ( p == MAP_FAILED ) p = NULL
#define BLKFREE( p ) munmap( (void *)p, 0 )
#endif
#endif
#endif /* FASTMEM */
#ifdef DEBUG_OUT
static uint_32 memcalls = 0;
static uint_32 memstart;
#endif
#ifdef TRMEM /* track memory allocation? */
#include "trmem.h"
extern _trmem_hdl hTrmem;
#define malloc( x ) _trmem_alloc( x, _trmem_guess_who( &x ), hTrmem )
#define free( x ) _trmem_free( x, _trmem_guess_who( &x ), hTrmem )
#endif
void MemInit( void )
/******************/
{
#if FASTMEM
pBase = NULL;
currfree = 0;
DebugCmd( blocks = 0 );
#endif
DebugCmd( memstart = memcalls );
}
void MemFini( void )
/******************/
{
#if FASTMEM
#ifdef DEBUG_OUT
if ( Options.quiet == FALSE )
printf( "memory used: %u kB\n", (blocks * BLKSIZE - currfree) / 1024 );
#endif
while ( pBase ) {
struct linked_list *pNext = pBase->next;
BLKFREE( pBase );
pBase = pNext;
}
#endif
#ifdef DEBUG_OUT
if ( memcalls != memstart )
printf("still allocated memory blocks : %u\n", memcalls - memstart );
#endif
}
void *LclAlloc( size_t size )
/***************************/
{
void *ptr;
#if FASTMEM
size = (size + sizeof(void *)-1) & ~(sizeof(void *)-1);
if ( currfree < size ) {
DebugMsg(("LclAlloc: new block needed, req. size=%Xh > currfree=%Xh\n", size, currfree ));
currfree = ( size <= ( BLKSIZE - sizeof( struct linked_list ) ) ? BLKSIZE - sizeof( struct linked_list ) : size );
BLKALLOC( pCurr, currfree + sizeof( struct linked_list ) );
if ( !pCurr ) {
currfree = 0;
Fatal( OUT_OF_MEMORY );
}
((struct linked_list *)pCurr)->next = pBase;
pBase = (struct linked_list *)pCurr;
pCurr += sizeof( struct linked_list );
DebugCmd( blocks++ );
}
ptr = pCurr;
pCurr += size;
currfree -= size;
#else /* ! FASTMEM */
ptr = malloc( size );
#ifdef TRMEM
DebugMsg1(("LclAlloc(0x%X)=%p cnt=%" I32_SPEC "u\n", size, ptr, ++memcalls ));
#endif
if( ptr == NULL ) {
Fatal( OUT_OF_MEMORY );
}
#endif
#ifdef _DEBUG
memset(ptr, 0, size);
#endif
return( ptr );
}
#if FASTMEM==0
void LclFree( void *ptr )
/***********************/
{
if( ptr != NULL ) {
#ifdef TRMEM
DebugMsg1(("LclFree(0x%p) cnt=%" I32_SPEC "u\n", ptr, --memcalls ));
#endif
free( ptr );
}
}
#endif
void *MemAlloc( size_t size )
/***************************/
{
void *ptr;
ptr = malloc( size );
DebugMsg1(("MemAlloc(0x%X)=%p cnt=%" I32_SPEC "u\n", size, ptr, ++memcalls ));
if( ptr == NULL ) {
Fatal( OUT_OF_MEMORY );
}
#ifdef _DEBUG
memset( ptr, 0x0, size );
#endif
return( ptr );
}
void MemFree( void *ptr )
/***********************/
{
DebugMsg1(("MemFree(0x%p) cnt=%" I32_SPEC "u\n", ptr, --memcalls ));
free( ptr );
return;
}
#if 0
void *MemRealloc( void *ptr, size_t size )
/****************************************/
{
void *new;
new = realloc( ptr, size );
if( new == NULL && size != 0 ) {
Fatal( OUT_OF_MEMORY );
}
return( new );
}
#endif