-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCStorageManager.m
530 lines (427 loc) · 14.1 KB
/
TCStorageManager.m
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// TCStorage.m
// TinyC
//
// Created by Tom Cole on 4/14/14.
// Copyright (c) 2014 Forest Edge. All rights reserved.
//
#import "TCStorageManager.h"
#import "TCValue.h"
const char * typeName( TCValueType t )
{
NSMutableString * name = [NSMutableString string];
static char msgBuffer[80];
TCValueType lt = t;
if( lt > TCVALUE_POINTER) {
[name appendString:@"TCVALUE_POINTER+"];
lt = lt - TCVALUE_POINTER;
}
switch(lt) {
case TCVALUE_BOOLEAN:
[name appendString:@"TCVALUE_BOOLEAN"];
break;
case TCVALUE_CHAR:
[name appendString: @"TCVALUE_CHAR"];
break;
case TCVALUE_DOUBLE:
[name appendString: @"TCVALUE_DOUBLE"];
break;
case TCVALUE_FLOAT:
[name appendString: @"TCVALUE_FLOAT"];
break;
case TCVALUE_INT:
[name appendString: @"TCVALUE_INT"];
break;
case TCVALUE_LONG:
[name appendString: @"TCVALUE_LONG"];
break;
case TCVALUE_POINTER:
[name appendString: @"TCVALUE_POINTER"];
break;
case TCVALUE_STRING:
[name appendString: @"TCVALUE_STRING!!!"];
break;
case TCVALUE_UNDEFINED:
[name appendString: @"TCVALUE_UNDEFINED!!!"];
break;
default:
return "undefined type";
}
[name getCString:msgBuffer maxLength:78 encoding:NSUTF8StringEncoding];
return msgBuffer;
}
@implementation TCStorageManager
#pragma mark - Initialization
-(instancetype) init {
return [self initWithStorage:65536];
}
-(instancetype) initWithStorage:(long) size
{
if(( self = [super init])) {
// Allocate the space for all tinyc runtime memory.
_buffer = malloc(size);
if(! _buffer ) {
return nil;
}
_debug = NO;
// _size is the size of the entire virtual memory area
// _dynamic starts at the end of that area and grows towards
// zero as memory is allocated and freed dynamically by the
// runtime malloc() and free() calls.
_size = size;
_dynamic = size - 4L;
_autoMark = 0L;
_dynamicMark = 0L;
_maxFrames = 0L;
// The first 8 bytes are filled with 0xFF as a trap to help
// locate uninitialized pointer references. The base of
// actual allocated storage starts at byte 8
for( int i = 0; i < 8; i++)
_buffer[i] = 0xFF;
_base = 8L;
_current = _base;
// This is the stack frame; it keeps up with the automatic
// storage allocations from the runtime for each call
// frame, and can discard them as needed.
_stack = [NSMutableArray array];
// This is the list of storage items that have been
// allocated by the user and then free'd, and area
// available to re-issue as needed. Each entry
// contains an NSRange indicating the start and
// length of each allocation.
_freeList = [NSMutableArray array];
_allocList = [NSMutableArray array];
}
return self;
}
#pragma mark - Dynamic Sizing
-(long) pushStorage;
{
_frameCount++;
if( _frameCount > _maxFrames)
_maxFrames = _frameCount;
if(_debug)
NSLog(@"STORAGE: push new storage frame #%d at %ld", _frameCount, _current);
[_stack addObject:[NSNumber numberWithLong:_base]];
[_stack addObject:[NSNumber numberWithLong:_current]];
_base = _current;
return _base;
}
-(long) popStorage
{
if( _frameCount < 0 ) {
NSLog(@"STORAGE: FATAL, too many stack frames popped");
return 0;
}
long idx =_stack.count-1;
long frameSize = _current - _base;
NSNumber *oldCurrent = [_stack objectAtIndex:idx];
NSNumber *oldBase = [_stack objectAtIndex:idx];
_base = oldBase.longValue;
_current = oldCurrent.longValue;
[_stack removeObjectAtIndex:idx];
if(_debug)
NSLog(@"STORAGE: pop old storage frame #%d at %ld, discarding %ld bytes", _frameCount, _current, frameSize);
_frameCount--;
return _base;
}
-(long) allocUnpadded:(long)size
{
if( _current + size > _size) {
NSLog(@"Memory exhausted");
return 0L;
}
// Do the allocation
if(_debug)
NSLog(@"STORAGE: alloc %ld bytes at %ld", size, _current);
long newAddr = _current;
_current += size;
if( _current > _autoMark)
_autoMark = _current;
return newAddr;
}
-(TCValue*) allocateString:(NSString *)string
{
TCValue * result = nil;
if(_stringPool == nil ) {
_stringPool = [NSMutableArray array];
_stringAddress = [NSMutableArray array];
}
long pos = [_stringPool indexOfObject:(string)];
if( pos != NSNotFound) {
NSNumber * n = _stringAddress[pos];
result = [[TCValue alloc]initWithLong:[n longValue]];
[result makePointer:TCVALUE_POINTER_CHAR];
return result;
}
pos = [self allocateDynamic:string.length+1];
[_stringPool addObject:string];
[_stringAddress addObject:[[NSNumber alloc]initWithLong:pos]];
// Let's actually copy the string into the memory storage as well.
for( int idx = 0; idx < string.length; idx++) {
[self setChar:[string characterAtIndex:idx] at:pos+idx];
}
[self setChar:0 at:pos+string.length];
result = [[TCValue alloc]initWithLong:pos];
[result makePointer:TCVALUE_POINTER_CHAR];
return result;
}
-(long) allocateDynamic:(long)size
{
// First, search list of free'd allocations to see if
// we already have one this size to give away.
for( int ix = 0; ix < _freeList.count; ix++) {
NSValue * v = [_freeList objectAtIndex:ix];
NSRange r = v.rangeValue;
if( r.length == size) {
[_freeList removeObjectAtIndex:ix];
[_allocList addObject:v];
if(_debug) {
NSLog(@"STORAGE: dynalloc %ld byte @ %ld free list #%d",
size, r.location, ix);
}
return r.location;
}
}
// No, we must allocate anew from the storage area
if((_dynamic - size) <= _current) {
NSLog(@"FATAL - dynamic memory exhausted");
return 0L;
}
_dynamic = _dynamic - size;
[_allocList addObject:[NSValue valueWithRange:NSMakeRange(_dynamic, size)]];
if(_debug) {
NSLog(@"STORAGE: dynalloc %ld byte @ %ld alloc list #%ld",
size, _dynamic, _allocList.count-1);
}
if((_size - _dynamic) > _dynamicMark)
_dynamicMark = (_size - _dynamic);
return _dynamic;
}
/**
Free previously allocated memory
@param the address in virtual memory that was previously returned by an alloc() operation
@returns the number of bytes freed
*/
-(long) free:(long) address
{
// Search the allocation list to find the matching
// allocation record.
for( int ix = 0; ix < _allocList.count; ix++ ) {
NSValue *v = _allocList[ix];
NSRange r = v.rangeValue;
if( r.location == address) {
// Delete from the allocation list and put on free list
[_allocList removeObjectAtIndex:ix];
[_freeList addObject:v];
if(_debug)
NSLog(@"STORAGE: free %ld bytes at %ld",
r.length, r.location);
return r.length;
}
}
// Not an allocation we know about
if(_debug)
NSLog(@"STORAGE: attempt to free unallocated memory at %ld", address);
return 0;
}
/**
Force the storage to be aligned on the natural 'size' boundary.
@param size the natural alignment size. If the current
storage boundary is already an even multiple of this size then
no further action is taken. Othewise, the pointer to the next
available storage is moved to match the required alignment, such
that the next allocation will be properly aligned.
*/
-(void) align:(long)size
{
// Adjust the pointer to be a multiple of the storage size
long pad = _current % size;
if( pad ) {
_current = _current + (size-pad);
if(_debug)
NSLog(@"STORAGE: allocation padded by %d bytes", (int)(size-pad));
if( _current > _autoMark)
_autoMark = _current;
}
}
-(long) allocateAuto:(long)size
{
if( _current + size > _size) {
NSLog(@"Memory exhausted");
return 0L;
}
// Adjust the pointer to be a multiple of the storage size
[self align:size];
// Do the allocation
if(_debug)
NSLog(@"STORAGE: alloc %ld bytes at %ld", size, _current);
long newAddr = _current;
_current += size;
if( _current > _autoMark)
_autoMark = _current;
return newAddr;
}
-(BOOL) isFault:(long) address
{
if( address < 0L || address > _size )
return YES;
if((address >= _current) && (address < _dynamic))
return YES;
return NO;
}
#pragma mark - Memory Accessors
-(TCValue*) getValue:(long)address ofType:(TCValueType) type
{
TCValue * v = nil;
if(_debug)
NSLog(@"STORAGE: Access value of type %s at %ld", typeName(type), address);
if( type > TCVALUE_POINTER) {
v = [[[TCValue alloc]initWithLong:[self getLong:address]] makePointer:(type - TCVALUE_POINTER)];
} else {
switch(type) {
case TCVALUE_DOUBLE:
v = [[TCValue alloc]initWithDouble:[self getDouble:address]];
break;
case TCVALUE_BOOLEAN:
case TCVALUE_CHAR:
v = [[TCValue alloc]initWithInt:(int)[self getChar:address]];
break;
case TCVALUE_INT:
v = [[TCValue alloc]initWithInt:(int)[self getInt:address]];
break;
case TCVALUE_LONG:
v = [[TCValue alloc]initWithLong:[self getLong:address]];
break;
default:
NSLog(@"FATAL: Attempt to read unsupported value type %d from storage", type);
v = nil;
break;
}
}
return v;
}
/**
Given a TCValue object, store it in the virtual memory area. This can be a pointer (in
which case use the long value as it contains the underlying address) or it can be a
scalar object where the specific data type is written into storage.
@param value the TCValue containing data to be written
@param address the virtual address to write the data to
*/
-(void) setValue:(TCValue *)value at:(long)address
{
if(_debug)
NSLog(@"STORAGE: store value %@ of type %s at %ld", value, typeName(value.getType), address);
if( value.getType >= TCVALUE_POINTER )
[self setLong:value.getLong at:address];
else {
switch( value.getType) {
case TCVALUE_CHAR:
[self setChar:value.getChar at:address];
break;
case TCVALUE_INT:
[self setInt:(int)value.getInt at:address];
break;
case TCVALUE_LONG:
[self setLong:value.getLong at:address];
break;
case TCVALUE_DOUBLE:
[self setDouble:value.getDouble at:address];
break;
default:
NSLog(@"FATAL - storage setValue type %s %d not implemented", typeName(value.getType), value.getType);
}
}
}
-(char) getChar:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return 0;
}
char result = _buffer[address];
if(_debug)
NSLog(@"STORAGE: read char %d from %ld", result, address);
return result;
}
-(void) setChar:(char)value at:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return;
}
_buffer[address] = value;
}
-(int) getInt:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return 0;
}
int result = *((int*)(&( _buffer[address])));
if(_debug)
NSLog(@"STORAGE: read int %d from %ld", result, address);
return result;
}
-(void) setInt:(int)value at:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return;
}
*(int*)&( _buffer[address]) = value;
}
-(long) getLong:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return 0;
}
long result = *(long*)&( _buffer[address]);
if(_debug)
NSLog(@"STORAGE: read long %ld from %ld", result, address);
return result;
}
-(void) setLong:(long)value at:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return;
}
*(long*)&( _buffer[address]) = value;
}
-(double) getDouble:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return 0.0;
}
double result = *(double*)&( _buffer[address]);
if(_debug)
NSLog(@"STORAGE: read double %f from %ld", result, address);
return result ;
}
-(void) setDouble:(double) value at:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return;
}
*(double*)&( _buffer[address]) = value = value;
}
-(NSString*) getString:(long)address
{
if([self isFault:address]) {
NSLog(@"Address fault %08lX, _current = %ld", address, _current);
return nil;
}
NSMutableString * result = [NSMutableString string];
for( long ix = address; ix < _size; ix++ ) {
char ch = _buffer[ix];
[result appendFormat:@"%c", ch];
if( ch == 0 )
break;
}
return [NSString stringWithString:result];
}
@end