forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simmem.cc
235 lines (177 loc) · 4.79 KB
/
simmem.cc
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
/*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
#include <stdio.h> // since BeOS needs size_t from there ...
#include "simmem.h"
#undef guarded_free
// use this for stress test; but it will return less than 32000 handles ...
//#define HARD_DEBUG
#if defined(_WIN32) && defined(HARD_DEBUG)
#include <assert.h>
#include "simdebug.h"
// use the ct routines for debugging
#include "dbgheap.cpp"
void guarded_free(void *p)
{
assert(p!=0);
free(p);
}
#else
#include <stdio.h>
#include <stdlib.h>
#include "simdebug.h"
/*
* Define this to use a key-lock mechanism for sanity checks.
* This can detect some kinds of overruns, underruns and double free's of
* the same memory area.
* @author Hj. Malthaner
*/
//#define USE_KEYLOCK
#ifdef USE_KEYLOCK
#include "simdebug.h"
static long count=0;
static long low_mark=0;
#endif
//#define DO_STATS
#ifdef DO_STATS
static long block_count = 0;
#endif
void guarded_free(void *p)
{
#ifdef USE_KEYLOCK
if(p != NULL) {
unsigned char * base = ((unsigned char *) p) - 8;
int * p = (int *)base;
const int check = *p;
const int size = *(p+1);
const unsigned int dead =
(base[size+8] << 24) +
(base[size+9] << 16) +
(base[size+10] << 8) +
(base[size+11] << 0);
// fprintf(stderr, "Message: guarded_free(): freeing %d bytes at %p, check %d, dead %x\n", size, base, check, dead);
// check sig
if(check < low_mark || check >= count) {
printf("guarded_free(): check is %d, valid range is 0..%d\n", check, count);
exit(0);
// dbg->fatal("guarded_free()","check is %d, valid range is 0..%d\n", check, count);
}
// destroy sig, set to invalid range
*p = -1;
// check size
if(size < 0) {
dbg->fatal("guarded_free()",
"size is %d, valid range is >= 0\n", size);
}
// destroy size, set to invalid range
*(p+1) = -1;
if(check == low_mark) {
low_mark ++;
}
// check end marker
if(dead != 0xdeaddead) {
dbg->fatal("guarded_free()",
"dead marker for %p (%d bytes) is %d, should be 0xdeaddead\n", base, size, dead);
}
// hand back mem to os
free( base );
}
#else
#ifdef DO_STATS
block_count --;
#endif
free( p );
#endif
}
#ifdef USE_KEYLOCK
#error "operator new [] missing!"
void* operator new(size_t const size)
{
return xmalloc(size);
}
void operator delete(void *p)
{
guarded_free(p);
}
#endif
#endif
void* xmalloc(size_t const size)
{
#if defined USE_KEYLOCK
void* const p = malloc(size + sizeof(int) * 3);
#else
# if defined DO_STATS
++block_count;
printf("Message: xmalloc(): allocating %d bytes, %ld block\n", size, block_count);
# endif
void* const p = malloc(size);
#endif
if (!p) {
// use unified error handler instead, since BeOS need this as C style file!
dbg->fatal("xmalloc()", "Could not alloc %li bytes.", (long)size );
}
#if defined USE_KEYLOCK
((int*)p)[0] = count; // write sig
count = (count + 1) & 0x7FFFFFF;
((int*)p)[1] = size; // write size
// write end sig
((unsigned char*)p)[size + 8] = 0xde;
((unsigned char*)p)[size + 9] = 0xad;
((unsigned char*)p)[size + 10] = 0xde;
((unsigned char*)p)[size + 11] = 0xad;
// hand back pointer behind sig and count, so that sig and count are hidden
return (unsigned char*)p + 8;
#else
return p;
#endif
}
void* xrealloc(void* const ptr, size_t const size)
{
#ifdef USE_KEYLOCK
if (ptr != NULL) {
unsigned char* const base = ((unsigned char*)ptr) - 8;
int const check = ((int*)p)[0];
int const size = ((int*)p)[1];
unsigned int const dead =
(base[size + 8] << 24) +
(base[size + 9] << 16) +
(base[size + 10] << 8) +
(base[size + 11] << 0);
if (check < low_mark || check >= count) { // check sig
dbg->fatal("xrealloc()", "check is %d, valid range is 0..%d\n", check, count);
}
if (size < 0) { // check size
dbg->fatal("xrealloc()", "size is %d, valid range is >= 0\n", size);
}
if (dead != 0xdeaddead) { // check end marker
dbg->fatal("xrealloc()", "dead marker for %p (%d bytes) is %d, should be 0xdeaddead\n", base, size, dead);
}
}
void* const p = realloc(ptr, size + sizeof(int) * 3);
#else
void* const p = realloc(ptr, size);
#endif
if (!p) {
// use unified error handler instead, since BeOS need this as C style file!
dbg->fatal("realloc()", "Could not alloc %li bytes.", (long)size );
}
#if defined USE_KEYLOCK
if (ptr == NULL) {
((int*)p)[0] = count; // write sig
count = (count + 1) & 0x7FFFFFF;
}
((int*)p)[1] = size; // write size
// write end sig
((unsigned char*)p)[size + 8] = 0xde;
((unsigned char*)p)[size + 9] = 0xad;
((unsigned char*)p)[size + 10] = 0xde;
((unsigned char*)p)[size + 11] = 0xad;
// hand back pointer behind sig and count, so that sig and count are hidden
return (unsigned char*)p + 8;
#else
return p;
#endif
}