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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/* 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$
*
*/
#ifndef SCI_ENGINE_SEGMAN_H
#define SCI_ENGINE_SEGMAN_H
#include "common/scummsys.h"
#include "common/serializer.h"
#include "sci/engine/vm.h"
#include "sci/engine/segment.h"
namespace Sci {
/**
* Verify the the given condition is true, output the message if condition is false, and exit.
* @param cond condition to be verified
* @param msg the message to be printed if condition fails
*/
#define VERIFY( cond, msg ) if (!(cond)) {\
error("%s, line, %d, %s", __FILE__, __LINE__, msg); \
}
/**
* Parameters for getScriptSegment().
*/
enum ScriptLoadType {
SCRIPT_GET_DONT_LOAD = 0, /**< Fail if not loaded */
SCRIPT_GET_LOAD = 1, /**< Load, if neccessary */
SCRIPT_GET_LOCK = 3 /**< Load, if neccessary, and lock */
};
class SegManager : public Common::Serializable {
friend class Console;
public:
/**
* Initialize the segment manager.
*/
SegManager(ResourceManager *resMan);
/**
* Deallocate all memory associated with the segment manager.
*/
~SegManager();
void resetSegMan();
virtual void saveLoadWithSerializer(Common::Serializer &ser);
// 1. Scripts
/**
* Allocate a script into the segment manager.
* @param script_nr The number of the script to load
* @param seg_id The segment ID of the newly allocated segment,
* on success
* @return 0 on failure, 1 on success
*/
Script *allocateScript(int script_nr, SegmentId *seg_id);
// The script must then be initialised; see section (1b.), below.
/**
* Forcefully deallocate a previously allocated script.
* @param script_nr number of the script to deallocate
*/
void deallocateScript(int script_nr);
/**
* Reconstructs scripts. Used when restoring saved games
*/
void reconstructScripts(EngineState *s);
/**
* Reconstructs the stack. Used when restoring saved games
*/
void reconstructStack(EngineState *s);
/**
* Determines the segment occupied by a certain script, if any.
* @param script_nr Number of the script to look up
* @return The script's segment ID, or 0 on failure
*/
SegmentId getScriptSegment(int script_nr) const;
/**
* Determines the segment occupied by a certain script. Optionally
* load it, or load & lock it.
* @param[in] script_nr Number of the script to look up
* @param[in] load flag determining whether to load/lock the script
* @return The script's segment ID, or 0 on failure
*/
SegmentId getScriptSegment(int script_nr, ScriptLoadType load);
// TODO: document this
reg_t getClassAddress(int classnr, ScriptLoadType lock, reg_t caller);
/**
* Return a pointer to the specified script.
* If the id is invalid, does not refer to a script or the script is
* not loaded, this will invoke error().
* @param seg ID of the script segment to check for
* @return A pointer to the Script object
*/
Script *getScript(SegmentId seg);
/**
* Return a pointer to the specified script.
* If the id is invalid, does not refer to a script, or
* the script is not loaded, this will return NULL
* @param seg ID of the script segment to check for
* @return A pointer to the Script object, or NULL
*/
Script *getScriptIfLoaded(SegmentId seg);
// 1b. Script Initialisation
// The set of functions below are intended
// to be used during script instantiation,
// i.e. loading and linking.
/**
* Initializes a script's local variable block according to a prototype
* @param segmentId Segment containing the script to initialize
*/
void scriptInitialiseLocals(SegmentId segmentId);
// 2. Clones
/**
* Allocate a fresh clone
* @param addr The offset of the freshly allocated clone
* @return Reference to the memory allocated for the clone
*/
Clone *allocateClone(reg_t *addr);
/**
* Reconstructs clones. Used when restoring saved games
*/
void reconstructClones();
// 4. Stack
/**
* Allocates a data stack
* @param size Number of stack entries to reserve
* @param segid Segment ID of the stack
* @return The physical stack
*/
DataStack *allocateStack(int size, SegmentId *segid);
// 5. System Strings
/**
* Initializes the system string table.
*/
void initSysStrings();
// 6, 7. Lists and Nodes
/**
* Allocate a fresh list
* @param[in] addr The offset of the freshly allocated list
* @return Reference to the memory allocated for the list
*/
List *allocateList(reg_t *addr);
/**
* Allocate a fresh node
* @param[in] addr The offset of the freshly allocated node
* @return Reference to the memory allocated for the node
*/
Node *allocateNode(reg_t *addr);
/**
* Allocate and initialize a new list node.
* @param[in] value The value to set the node to
* @param[in] key The key to set
* @return Pointer to the newly initialized list node
*/
reg_t newNode(reg_t value, reg_t key);
/**
* Resolves a list pointer to a list.
* @param addr The address to resolve
* @return The list referenced, or NULL on error
*/
List *lookupList(reg_t addr);
/**
* Resolves an address into a list node.
* @param addr The address to resolve
* @return The list node referenced, or NULL on error
*/
Node *lookupNode(reg_t addr);
// 8. Hunk Memory
/**
* Allocate a fresh chunk of the hunk
* @param[in] size Number of bytes to allocate for the hunk entry
* @param[in] hunk_type A descriptive string for the hunk entry, for
* debugging purposes
* @return The offset of the freshly allocated hunk entry
*/
reg_t allocateHunkEntry(const char *hunk_type, int size);
/**
* Deallocates a hunk entry
* @param[in] addr Offset of the hunk entry to delete
*/
void freeHunkEntry(reg_t addr);
/**
* Gets a pointer to the hunk memory referenced by a specified handle
* @param[in] addr Offset of the hunk entry
*/
byte *getHunkPointer(reg_t addr);
// 9. Dynamic Memory
/**
* Allocate some dynamic memory
* @param[in] size Number of bytes to allocate
* @param[in] description A descriptive string for debugging purposes
* @param[out] addr The offset of the freshly allocated X
* @return Raw pointer into the allocated dynamic
* memory
*/
byte *allocDynmem(int size, const char *description, reg_t *addr);
/**
* Deallocates a piece of dynamic memory
* @param[in] addr Offset of the dynmem chunk to free
*/
int freeDynmem(reg_t addr);
// Generic Operations on Segments and Addresses
/**
* Dereferences a raw memory pointer
* @param[in] reg The reference to dereference
* @return The data block referenced
*/
SegmentRef dereference(reg_t pointer);
/**
* Dereferences a heap pointer pointing to raw memory.
* @param pointer The pointer to dereference
* @parm entries The number of values expected (for checkingO
* @return A physical reference to the address pointed to, or NULL on error or
* if not enough entries were available.
*/
byte *derefBulkPtr(reg_t pointer, int entries);
/**
* Dereferences a heap pointer pointing to a (list of) register(s).
* Ensures alignedness of data.
* @param pointer The pointer to dereference
* @parm entries The number of values expected (for checking)
* @return A physical reference to the address pointed to, or NULL on error or
* if not enough entries were available.
*/
reg_t *derefRegPtr(reg_t pointer, int entries);
/**
* Dereferences a heap pointer pointing to raw memory.
* @param pointer The pointer to dereference
* @parm entries The number of values expected (for checking)
* @return A physical reference to the address pointed to, or NULL on error or
* if not enough entries were available.
*/
char *derefString(reg_t pointer, int entries = 0);
/**
* Return the string referenced by pointer.
* pointer can point to either a raw or non-raw segment.
* @param pointer The pointer to dereference
* @parm entries The number of values expected (for checking)
* @return The string referenced, or an empty string if not enough
* entries were available.
*/
Common::String getString(reg_t pointer, int entries = 0);
/**
* Copies a string from src to dest.
* src and dest can point to raw and non-raw segments.
* Conversion is performed as required.
*/
void strcpy(reg_t dest, reg_t src);
/**
* Copies a string from src to dest.
* dest can point to a raw or non-raw segment.
* Conversion is performed as required.
*/
void strcpy(reg_t dest, const char *src);
/**
* Copies a string from src to dest.
* src and dest can point to raw and non-raw segments.
* Conversion is performed as required. At most n characters are copied.
* TODO: determine if dest should always be null-terminated.
*/
void strncpy(reg_t dest, reg_t src, size_t n);
/**
* Copies a string from src to dest.
* dest can point to a raw or non-raw segment.
* Conversion is performed as required. At most n characters are copied.
* TODO: determine if dest should always be null-terminated.
*/
void strncpy(reg_t dest, const char *src, size_t n);
/**
* Copies n bytes of data from src to dest.
* src and dest can point to raw and non-raw segments.
* Conversion is performed as required.
*/
void memcpy(reg_t dest, reg_t src, size_t n);
/**
* Copies n bytes of data from src to dest.
* dest can point to a raw or non-raw segment.
* Conversion is performed as required.
*/
void memcpy(reg_t dest, const byte* src, size_t n);
/**
* Copies n bytes of data from src to dest.
* src can point to raw and non-raw segments.
* Conversion is performed as required.
*/
void memcpy(byte *dest, reg_t src, size_t n);
/**
* Determine length of string at str.
* str can point to a raw or non-raw segment.
*/
size_t strlen(reg_t str);
/**
* Finds a unique segment by type
* @param type The type of the segment to find
* @return The segment number, or -1 if the segment wasn't found
*/
SegmentId findSegmentByType(int type);
// TODO: document this
SegmentObj *getSegmentObj(SegmentId seg);
// TODO: document this
SegmentType getSegmentType(SegmentId seg);
// TODO: document this
SegmentObj *getSegment(SegmentId seg, SegmentType type);
/**
* Retrieves an object from the specified location
* @param[in] offset Location (segment, offset) of the object
* @return The object in question, or NULL if there is none
*/
Object *getObject(reg_t pos);
/**
* Checks whether a heap address contains an object
* @parm obj The address to check
* @return True if it is an object, false otherwise
*/
bool isObject(reg_t obj) { return getObject(obj) != NULL; }
// TODO: document this
bool isHeapObject(reg_t pos);
/**
* Determines the name of an object
* @param[in] pos Location (segment, offset) of the object
* @return A name for that object, or a string describing an error
* that occurred while looking it up. The string is stored
* in a static buffer and need not be freed (neither may
* it be modified).
*/
const char *getObjectName(reg_t pos);
/**
* Find the address of an object by its name. In case multiple objects
* with the same name occur, the optional index parameter can be used
* to distinguish between them. If index is -1, then if there is a
* unique object with the specified name, its address is returned;
* if there are multiple matches, or none, then NULL_REG is returned.
*
* @param name the name of the object we are looking for
* @param index the index of the object in case there are multiple
* @return the address of the object, or NULL_REG
*/
reg_t findObjectByName(const Common::String &name, int index = -1);
void scriptInitialiseClasses(SegmentId seg);
void scriptInitialiseObjectsSci0(SegmentId seg);
void scriptInitialiseObjectsSci11(SegmentId seg);
uint32 classTableSize() { return _classTable.size(); }
Class getClass(int index) { return _classTable[index]; }
void setClassOffset(int index, reg_t offset) { _classTable[index].reg = offset; }
void resizeClassTable(uint32 size) { _classTable.resize(size); }
/**
* Obtains the system strings segment ID
*/
SegmentId getSysStringsSegment() { return sysStringsSegment; }
public: // TODO: make private
Common::Array<SegmentObj *> _heap;
// Only accessible from saveLoadWithSerializer()
Common::Array<Class> _classTable; /**< Table of all classes */
#ifdef ENABLE_SCI32
SciArray<reg_t> *allocateArray(reg_t *addr);
SciArray<reg_t> *lookupArray(reg_t addr);
void freeArray(reg_t addr);
SciString *allocateString(reg_t *addr);
SciString *lookupString(reg_t addr);
void freeString(reg_t addr);
SegmentId getStringSegmentId() { return String_seg_id; }
#endif
private:
/** Map script ids to segment ids. */
Common::HashMap<int, SegmentId> _scriptSegMap;
ResourceManager *_resMan;
SegmentId Clones_seg_id; ///< ID of the (a) clones segment
SegmentId Lists_seg_id; ///< ID of the (a) list segment
SegmentId Nodes_seg_id; ///< ID of the (a) node segment
SegmentId Hunks_seg_id; ///< ID of the (a) hunk segment
/* System strings */
SegmentId sysStringsSegment;
public: // TODO: make private. Only kString() needs direct access
SystemStrings *sysStrings;
private:
#ifdef ENABLE_SCI32
SegmentId Arrays_seg_id;
SegmentId String_seg_id;
#endif
private:
SegmentObj *allocSegment(SegmentObj *mem, SegmentId *segid);
LocalVariables *allocLocalsSegment(Script *scr);
int deallocate(SegmentId seg, bool recursive);
void createClassTable();
SegmentId findFreeSegment() const;
/**
* Check segment validity
* @param[in] seg The segment to validate
* @return false if 'seg' is an invalid segment, true if
* 'seg' is a valid segment
*/
bool check(SegmentId seg);
};
} // End of namespace Sci
#endif // SCI_ENGINE_SEGMAN_H
|