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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001-2006 The ScummVM project
* Copyright (C) 2006 Won Star - GP32 Backend
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "stdafx.h"
#include "common/scummsys.h"
//#include "graphics/scaler.h"
#include "common/system.h"
#include "backends/intern.h"
#include "base/engine.h"
#include "backends/gp32/gp32std.h"
#include "backends/gp32/gp32std_grap.h"
#include "backends/gp32/gp32std_memory.h"
/////////////////////
//Memory management
#define USER_MEMORY_SIZE (256 * 1024)
#define USER_BLOCK_SIZE 24
#define NUM_BLOCK (USER_MEMORY_SIZE / USER_BLOCK_SIZE)
// use fixed block size for small allocations
// consumes about 128k for block array
// consumes 256k for user memory
class MemBlock {
protected:
static byte *userMem;
// static size_t allocSize;
// static MemBlock *head;
static int numBlock;
static int prevBlock;
// Linked list is slow for this task. :)
static MemBlock block[NUM_BLOCK];
byte *block;
size_t size;
// MemBlock *next;
int used;
public:
static void init();
static void deinit();
static void *addBlock(size_t size);
static void deleteBlock(void *block);
};
byte *MemBlock::userMem = NULL;
//MemBlock *MemBlock::head = NULL;
//size_t MemBlock::allocSize = 0;
int MemBlock::numBlock = 0;
int MemBlock::prevBlock = 0;
MemBlock MemBlock::block[NUM_BLOCK];
void MemBlock::init()
{
userMem = (byte *)gm_malloc(USER_MEMORY_SIZE + USER_BLOCK_SIZE);
if (!userMem) {
//error
}
}
void MemBlock::deinit()
{
if (!userMem) {
//error
}
gm_free(userMem);
userMem = NULL;
}
void *MemBlock::addBlock(size_t size)
{
int i;
MemBlock *blk = &block[prevBlock];
// some optimizaion with loop
for (i = prevBlock; i < NUM_BLOCK; i++) {
if (!blk->used) {
break;
}
blk++;
}
if(i == NUM_BLOCK) {
blk = &block[0];
for (i = 0; i < prevBlock; i++) {
if (!blk->used) {
break;
}
blk++;
}
if(i == prevBlock)
return NULL;
}
byte *ptr = userMem + (i * USER_BLOCK_SIZE);
blk->size = size;
blk->block = ptr;
blk->used = 1;
prevBlock = i;
return (void *) ptr;
}
void MemBlock::deleteBlock(void *block)
{
uint32 np = (uint32) block - (uint32) userMem;
if ((np / USER_BLOCK_SIZE) * USER_BLOCK_SIZE != np) {
gm_free(block);
// warning("wrong block! (%d / %d)", (np / USER_BLOCK_SIZE) * USER_BLOCK_SIZE, np);
}
int i = np / USER_BLOCK_SIZE;
if (i > NUM_BLOCK) {
gm_free(block);
return;
}
block[i].used = 0;
/*
int i = 0;
for (i = 0; i < NUM_BLOCK; i++) {
if (block[i].block == block)
break;
}
if (i == NUM_BLOCK) {
gm_free(block); //fixme?
//warning("wrong block! %x", (uint32)block - (uint32)userMem);
} else {
GPDEBUG("deleteing block %d", i);
block[i].used = 0;
}*/
}
// HACK not to include struct MemBlock
void memBlockInit()
{
MemBlock::init();
}
void memBlockDeinit()
{
MemBlock::deinit();
}
#undef memcpy
#undef memset
void *gp_memcpy(void *dst, const void *src, size_t count) {
return memcpy(dst, src, count);
}
void *gp_memset(void *dst, int val, size_t count) {
return memset(dst, val, count);
}
#define MALLOC_MASK 0xAB800000
void *gp_malloc(size_t size) {
uint32 np;
uint32 *up;
// size + 8 bytes : stores block size
int allocSize = size + sizeof(uint32) + sizeof(uint32);
if (allocSize <= USER_BLOCK_SIZE) {
np = (uint32) MemBlock::addBlock(allocSize);
if (!np) {
// GPDEBUG("falling back to gm_malloc");
np = (uint32) gm_malloc(allocSize);
}
} else {
np = (uint32) gm_malloc(allocSize);
}
if (np) {
up = (uint32 *)np;
*up = size | MALLOC_MASK; // mem size: up to 8mb
up = (uint32 *)(np + size + sizeof(uint32));
*up = 0x1234; // catches oob acess
return (void *)(np + sizeof(uint32));
}
return NULL;
}
void gp_free(void *block) {
uint32 np;
uint32 *up;
if (!block) {
return;
}
np = ((uint32) block) - sizeof(uint32);
up = (uint32 *) np;
if (*up == 0x43210900) {
warning("%s: double deallocation", __FUNCTION__);
return;
}
if (*up & MALLOC_MASK != MALLOC_MASK) {
warning("%s: corrupt block", __FUNCTION__);
return;
}
int blockSize = (*up & 0x7fffff);
up = (uint32 *)(np + blockSize + sizeof(uint32));
if (*up != 0x1234) {
warning("gp_free: corrupt block - OOB access", __FUNCTION__);
return;
}
*up = 0x43210900;
np = ((uint32) block) - sizeof(uint32);
up = (uint32 *) np;
if (blockSize + 8 <= USER_BLOCK_SIZE) {
MemBlock::deleteBlock(up);
} else {
gm_free(up);
}
}
void *gp_calloc(size_t nitems, size_t size) {
void *p = gp_malloc(nitems * size); //gpcalloc doesnt clear?
gp_memset(p, 0, nitems * size);
// if (*(uint8 *)p != 0)
// warning("%s: calloc doesn't clear", __FUNCTION__); //fixme: was error
return p;
}
//////////////////////////////////////////////////
// GP32 stuff
//////////////////////////////////////////////////
static char usedMemStr[16];
int gUsedMem = 1024 * 1024;
//#define CLEAN_MEMORY_WITH_0xE7
#define CHECK_USED_MEMORY
//#define CHECK_NEW_TIME
//#define CHECK_NEW_SIZE
void *operator new(size_t size) {
#if defined(CHECK_NEW_TIME)
static int ftick;
ftick = GpTickCountGet();
#endif
// printf("BP:operator new(%d)", size);
void *ptr = gp_malloc(size);
#if defined(CLEAN_MEMORY_WITH_0xE7)
if(ptr != NULL) {
gp_memset(ptr, 0xE7, size);
}
#endif
#if defined(CHECK_USED_MEMORY)
// Check free memory.
gUsedMem = ((int)(ptr) + size) - 0xc000000;
sprintf(usedMemStr, "%8d", gUsedMem);
gp_fillRect(frameBuffer1, 0, 0, 64, 12, 0);
gp_textOut(frameBuffer1, 0, 0, usedMemStr, 0xfffff);
#endif
#if defined(CHECK_NEW_TIME)
sprintf(usedMemStr, "%2d", GpTickCountGet() - ftick);
gp_fillRect(frameBuffer1, 72, 0, 24, 12, 0);
gp_textOut(frameBuffer1, 72, 0, usedMemStr, 0xfffff);
#endif
#if defined(CHECK_NEW_SIZE)
sprintf(usedMemStr, "%8d", size);
gp_fillRect(frameBuffer1, 108, 0, 64, 12, 0);
gp_textOut(frameBuffer1, 108, 0, usedMemStr, 0xfffff);
#endif
return ptr;
}
void operator delete(void *ptr) {
// printf("operator delete(%x)", ptr);
gp_free(ptr);
}
|