aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gui/gui_memmgr.cpp
blob: 3b42da274e7c97b2e26078cc2d1995a67d796a02 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 "sci/sci.h"
#include "sci/gui/gui_helpers.h"
#include "sci/gui/gui_memmgr.h"

namespace Sci {

static tagHandle *pHandles = 0;
static int16 nHandles = 0;
static byte *_heap = 0;
SCIHANDLE _firstfree = 0;
Common::HashMap<HEAPHANDLE, tagHeapInfo> heapInfo;
#define HEAP_BOTTOM 1000

//----------------------
void FreeMem(void) {
	// deleting handles
	if (pHandles) {
		for (uint16 i = 0; i < nHandles; i++)
			hunkDisposeHandle(i);
		delete[] pHandles;
		pHandles = 0;
	}
	// deleting heap
	if (_heap) {
		delete[] _heap;
		_heap = 0;
	}
}
//----------------------
bool InitMem(int16 max_handles) {
	// heap
	_heap = new byte[HEAP_SIZE];
	memset(_heap, 0x55, HEAP_SIZE);
	_firstfree = HEAP_START;
	heapSetBlockSize(_firstfree, HEAP_SIZE - 1 - HEAP_START);
	heapSetBlockNext(_firstfree, (SCIHANDLE)(HEAP_SIZE - 1));
	//hunk
	pHandles = new tagHandle[max_handles];
	if (pHandles) {
		nHandles = max_handles;
		memset(pHandles, 0, sizeof(tagHandle) * max_handles); // zerofy all
		return 1;
	}
	return 0;
}
//----------------------
SCIHANDLE hunkNeedHandle(uint16 size, uint32 resId) {
	SCIHANDLE newh = 0;
	// see if there is an empty handle available
	for (int16 i = 1; i < nHandles; i++)
		if (pHandles[i].ptr == 0) {
			newh = i;
			break;
		}
	// if unused handle was found - allocate memory and return it
	if (newh == nHandles)
		return 0;
	pHandles[newh].ptr = (byte*)malloc(size);
	assert(pHandles[newh].ptr);
	if (pHandles[newh].ptr) {
		pHandles[newh].size = size;
		pHandles[newh].resId = resId;
		debug(
				5,
				"   MemMgr: Requested %6db for res %08X, allocated handle %04X",
				size, resId, newh);
		return newh;
	}
	return 0;
}
//----------------------
void hunkDisposeHandle(SCIHANDLE handle) {
// 	if (handle > 0 && handle < nHandles && pHandles[handle].ptr) {
// 		debug(
// 				5,
// 				"   MemMgr: disposing handle 0x%04X, size=%8db, associated res %08X",
// 				handle, pHandles[handle].size, pHandles[handle].resId);
// 		free(pHandles[handle].ptr);
// 		pHandles[handle].ptr = 0;
// 		// deleting associated resource handler
// 		// Check that this don't fail on delete as ResGetLoaded could return 0;
// 		if (pHandles[handle].resId != 0xFFFFFFFF)
// 			delete g_sci->ResMgr.ResGetLoaded(pHandles[handle].resId);
// 	}
}
//----------------------
byte *hunk2Ptr(SCIHANDLE handle) {
	if (handle > 0 && handle < nHandles && pHandles[handle].ptr)
		return (byte *)pHandles[handle].ptr;
	return 0;
}
//----------------------
SCIHANDLE ptr2hunk(byte *ptr) {
	for (int i = 0; i < nHandles; i++) {
		if (ptr >= pHandles[i].ptr && ptr <= pHandles[i].ptr + pHandles[i].size)
			return i;
	}
return 0;
}
//----------------------
uint32 hunkHandleSize(SCIHANDLE handle) {
	if (handle > 0 && handle < nHandles && pHandles[handle].ptr)
		return pHandles[handle].size;
	return 0;
}
//----------------------
// int16 hunkResourceNum(SCIHANDLE handle) {
// 	if (handle > 0 && handle < nHandles && pHandles[handle].ptr)
// 		return RES_ID2NUM(pHandles[handle].resId);
// 	return -1;
// }

//----------------------
void hunkDump() {
	debug("\nMemMgr: Hunk dump:");
	uint32 size = 0;
	for (int i = 0; i < nHandles; i++)
		if (pHandles[i].ptr) {
			debug("  %04X, size=%8db, associated res %08X", i,
					pHandles[i].size, pHandles[i].resId);
			size += pHandles[i].size;
		}
	debug("Total memory allocated: %db", size);
	debug("End dump");
}

uint32 hunkUsed() {
	uint32 size = 0;
	for (int i = 0; i < nHandles; i++)
		if (pHandles[i].ptr)
			size += pHandles[i].size;
	return size;
}
//----------------------
// HEAP
//----------------------
//---------------------------------------------
// TODO : make sure that STRING, STACK and SCRIPT DATA is never allocated below HEAP_BOTTOM=1000
// otherwise it will cause problems with kernel string fuctions that assumes that anything below 1000 is res number
HEAPHANDLE heapNewPtr(uint16 size, kDataType type, const char *info) {
	if (size == 0)
		warning("Zero Heap Allocation Request!");

	HEAPHANDLE ptr, prev, next;
	ptr = prev = _firstfree;
	// 2 bytes block header header + block size must be odd
	size += 2 + (size & 1);
	// looking for an empty block to use
	uint16 blocksize;
	bool bBottomSafe = !(type == kDataString || type == kDataUnknown);

	while (ptr < HEAP_SIZE - 1) {
		blocksize = heapGetBlockSize(ptr);
		next = heapGetBlockNext(ptr);
		if (blocksize >= size) {
			if (bBottomSafe || (!bBottomSafe && ptr > HEAP_BOTTOM)) {
				if (blocksize <= size + 4) { // use all block
					size = blocksize;
					heapSetBlockNext(prev, next);
				} else { // split block in 2 blocks
					HEAPHANDLE newblock = ptr + size;
					heapSetBlockNext(prev, newblock);
					heapSetBlockSize(newblock, blocksize - size);
					heapSetBlockNext(newblock, next);
					next = newblock;
				}
				// setting allocated block
				heapSetBlockSize(ptr, size);
				// updating firstfree pointer
				if (ptr == _firstfree)
					_firstfree = next;
				setHeapInfo(ptr, type, info);
				return ptr;
			} //if (bBottomSafe || (!bBottomSafe && ptr>HEAP_BOTTOM))
			else { // !bottomsafe && ptr < HEAP_BOTTOM
				if (blocksize + ptr - HEAP_BOTTOM >= size) {
					// splitting the block into 3 parts
					// [2][ptr...999] [2][1002...1000+size-1] [2][1002+size...ptr+blocksize]
					//   free              returned              free
					heapSetBlockSize(ptr, HEAP_BOTTOM-ptr + 2);
					heapSetBlockSize(HEAP_BOTTOM+2, size);
					heapSetBlockSize(HEAP_BOTTOM+2 + size, blocksize - size
							- (HEAP_BOTTOM-ptr + 2));

					heapSetBlockNext(HEAP_BOTTOM+2 + size, next);
					heapSetBlockNext(ptr, HEAP_BOTTOM+2 + size);
					setHeapInfo(HEAP_BOTTOM+2, type, info);
					return HEAP_BOTTOM + 2;
				}
			}
		} // if (blocksize >= size)
		// block too small - moving to next one
		prev = ptr;
		ptr = next;
	}
	// allocation error - out of heap
	warning("Out of heap space");
	return 0;
}
//--------------------------------------------
void heapDisposePtr(HEAPHANDLE handle) {
	HEAPHANDLE prev, next;
	prev = next = _firstfree;
	// searching for prev and next free blocks (before & after deleted block)
	while (next < handle) {
		prev = next;
		next = heapGetBlockNext(prev);
	}
	// if deleted block is before 1st free space then it'll be 1st free space
	if (handle < _firstfree) {
		next = _firstfree;
		_firstfree = handle;
	} else
		heapSetBlockNext(prev, handle);

	heapSetBlockNext(handle, next);
	//Try to merge with previous
	if (prev + heapGetBlockSize(prev) == handle) {
		heapSetBlockSize(prev, heapGetBlockSize(prev)
				+ heapGetBlockSize(handle));
		heapSetBlockNext(prev, next);
		handle = prev;
	}
	//Try to merge with next
	if (handle + heapGetBlockSize(handle) == next && next != (HEAP_SIZE - 1)) {
		heapSetBlockSize(handle, heapGetBlockSize(handle) + heapGetBlockSize(
				next));
		heapSetBlockNext(handle, heapGetBlockNext(next));
	}
}
//-------------------------------------
uint16 heapFreeSize() {
	HEAPHANDLE free = _firstfree;
	uint16 szFree = 0;
	while (free < HEAP_SIZE - 1) {
		int size = heapGetBlockSize(free);
		free = heapGetBlockNext(free);
		szFree += size;
	}
	return szFree;
}
//-------------------------------------
uint16 heapLargestFreeSize() {
	HEAPHANDLE free = _firstfree;
	uint16 maxFree = 0;
	while (free < HEAP_SIZE - 1) {
		int size = heapGetBlockSize(free);
		free = heapGetBlockNext(free);
		if (size > maxFree)
			maxFree = size;
	}
	return maxFree ? maxFree - 2 : 0;
}
//-------------------------------------
void heapDump() {
	debug("\nMemMgr:Heap dump:");
	HEAPHANDLE ptr = HEAP_START;
	HEAPHANDLE free = _firstfree;

	uint16 szFree = 0, szUsed = 0;
	while (ptr < HEAP_SIZE - 1) {
		int size = heapGetBlockSize(ptr);
		if (ptr == free) {
			free = heapGetBlockNext(free);
			debug("  %04X %6u size:%6d FREE next=%04X", ptr, ptr,
					heapGetDataSize(ptr), free);
			szFree += size;
		} else {
			debug("  %04X %6u size:%6d USED", ptr, ptr, heapGetDataSize(ptr));
			szUsed += size;
		}
		ptr += size;
	}
	debug("Total %db used, %db free\nEnd Dump", szUsed, szFree);
}
//----------------------------
inline byte *heap2Ptr(HEAPHANDLE ptr) {
	return (ptr >= HEAP_START) ? (byte *)(_heap + ptr) : NULL;
}
//----------------------------
HEAPHANDLE ptr2heap(byte *ptr) {
	return ptr > _heap && ptr < (_heap + HEAP_SIZE) ? (HEAPHANDLE)(ptr - _heap)
			: 0;
}
//----------------------------
uint16 heapGetBlockSize(HEAPHANDLE ptr) {
	return READ_UINT16(_heap + ptr - 2);
}
//----------------------------
uint16 heapGetDataSize(HEAPHANDLE ptr) {
	return heapGetBlockSize(ptr) - 2;
}
//----------------------------
void heapFillPtr(HEAPHANDLE ptr, byte filler) {
	memset(heap2Ptr(ptr), filler, heapGetDataSize(ptr));
}
//----------------------------
void heapClearPtr(HEAPHANDLE ptr) {
	heapFillPtr(ptr, 0);
}
//----------------------------
void heapSetBlockSize(HEAPHANDLE ptr, uint16 nbytes) {
	WRITE_UINT16(_heap + ptr - 2, nbytes);
}
//----------------------------
HEAPHANDLE heapGetBlockNext(HEAPHANDLE ptr) {
	return READ_UINT16(_heap + ptr);
}
//----------------------------
void heapSetBlockNext(HEAPHANDLE ptr, SCIHANDLE next) {
	WRITE_UINT16(_heap + ptr, next);
}
//----------------------------
void heapDisposePtr(byte *ptr) {
	heapDisposePtr(ptr - _heap);
}
//---------------------------------------------
//
//
void setHeapInfo(HEAPHANDLE hnd, kDataType type, const char *szinfo) {
	tagHeapInfo info = { kDataUnknown, 0 };
	info.type = type;
	if (szinfo)
		strncpy(info.info, szinfo, 20);
	heapInfo.setVal(hnd, info);
}
//--------------------------------
bool saveMemState(Common::OutSaveFile *pFile) {
	pFile->writeString("MEMORY\n");
	// saving heap
	pFile->write(_heap, HEAP_SIZE);
	pFile->writeUint16LE(_firstfree);
	// TODO : process and write hunk handles
	//pFile->writeString("HUNK\n");
	return true;
}
//--------------------------------
bool restoreMemState(Common::InSaveFile *pFile) {
	if (pFile->readLine() != "MEMORY")
		return false;
	pFile->read(_heap, HEAP_SIZE);
	_firstfree = pFile->readUint16LE();

	return true;
}

//-------------------------------
}// end of namespace SCI