00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef STREAMMEM_HPP
00038 #define STREAMMEM_HPP
00039
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043
00044 #include "Types.hpp"
00045
00046 #include "MemMan.hpp"
00047
00048
00049
00050
00051
00052
00053 #define BLOCKSIZE_IDXNUM 16
00054 extern unsigned char blocksizeidxs[];
00055
00056 class Output;
00057
00058 struct MemStreamBlock
00059
00060 {
00061 MemStreamBlock *next;
00062 unsigned long blocksize:28;
00063 unsigned long blocksizeidxidx:4;
00064 unsigned long cursize;
00065
00066
00067
00068
00069 char data[1];
00070
00071 };
00072
00073
00074
00075
00076 struct MemStreamMarker
00077
00078
00079
00080
00081
00082 {
00083 MemStreamBlock *block;
00084 MemStreamMarker *pos;
00085
00086 };
00087
00088 class MemStreamer
00089 {
00090 MemStreamBlock *firstblock;
00091 MemStreamBlock *curblock;
00092 unsigned long overallsize:28;
00093 unsigned long curblocksizeidxidx:4;
00094 MemStreamMarker curmarker;
00095
00096
00097 void ReleaseBlock(MemStreamBlock *block)
00098
00099
00100
00101 {
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 FreeBlock((char *)block,blocksizeidxs[block->blocksizeidxidx]);
00112 }
00113
00114 MemStreamBlock *AllocateNewBlock()
00115
00116
00117 {
00118 MemStreamBlock *newblock;
00119
00120
00121 newblock=(MemStreamBlock *)AllocateBlock(blocksizeidxs[curblocksizeidxidx]);
00122
00123
00124 newblock->blocksize=GetBlockSize(blocksizeidxs[curblocksizeidxidx])-(sizeof(MemStreamBlock)-1);
00125 newblock->blocksizeidxidx=curblocksizeidxidx;
00126
00127
00128
00129 if(curblocksizeidxidx<BLOCKSIZE_IDXNUM-1)
00130 curblocksizeidxidx++;
00131
00132 newblock->next=NULL;
00133 newblock->cursize=0;
00134
00135
00136
00137
00138
00139
00140
00141
00142 return newblock;
00143 }
00144
00145 public:
00146
00147 void Initialize(unsigned long firstblocksizeidx=0)
00148
00149 {
00150 if(firstblocksizeidx>=BLOCKSIZE_IDXNUM)
00151 firstblocksizeidx=BLOCKSIZE_IDXNUM-1;
00152
00153 curblocksizeidxidx=firstblocksizeidx;
00154 overallsize=0;
00155
00156 curmarker.block=NULL;
00157 curmarker.pos=NULL;
00158
00159 firstblock=curblock=NULL;
00160 }
00161
00162 MemStreamer(unsigned long firstblocksizeidx=0)
00163 {
00164 Initialize(firstblocksizeidx);
00165 }
00166
00167 ~MemStreamer()
00168 {
00169 ReleaseMemory(0);
00170 }
00171
00172 int GetSize() { return overallsize; }
00173
00174 MemStreamBlock *GetFirstBlock() { return firstblock; }
00175
00176 void WordAlign()
00177
00178
00179 {
00180 if(curblock==NULL)
00181 return;
00182
00183 int addsize=3-((curblock->cursize+3)&3);
00184 if(addsize>0)
00185 {
00186 curblock->cursize+=addsize;
00187 overallsize+=addsize;
00188 }
00189 }
00190
00191 char *GetByteBlock(unsigned len)
00192
00193
00194
00195 {
00196 if(len+sizeof(MemStreamBlock)-1>LARGEBLOCK_SIZE)
00197 {
00198 char str[100];
00199 sprintf(str,"Could not allocate %lu bytes (largest possible block size=%lu bytes) !",
00200 sizeof(MemStreamBlock)-1+len,
00201 LARGEBLOCK_SIZE);
00202 Error(str);
00203 Exit();
00204 }
00205
00206 if(curblock==NULL)
00207
00208 firstblock=curblock=AllocateNewBlock();
00209
00210 if(curblock->blocksize-curblock->cursize>=len)
00211
00212 {
00213 char *ptr=curblock->data+curblock->cursize;
00214 curblock->cursize+=len;
00215 overallsize+=len;
00216 return ptr;
00217 }
00218 else
00219 {
00220 do
00221 {
00222 curblock->next=AllocateNewBlock();
00223 curblock=curblock->next;
00224 }
00225 while(curblock->blocksize<len);
00226
00227
00228 curblock->cursize=len;
00229 overallsize+=len;
00230
00231 return curblock->data;
00232 }
00233 }
00234
00235 void ReleaseByteBlock(unsigned len)
00236
00237 {
00238
00239
00240 if(curblock->cursize<len)
00241 {
00242 Error("Fatal error in ReleaseBlock !\n");
00243 Exit();
00244 }
00245
00246 curblock->cursize-=len;
00247 overallsize-=len;
00248
00249 #ifdef RELEASEMEM_SAFE
00250 memset(curblock->data+curblock->cursize,0xcd,len);
00251 #endif
00252 }
00253
00254
00255
00256 void StoreData(char *data,unsigned len)
00257
00258
00259 {
00260 if(curblock==NULL)
00261
00262 firstblock=curblock=AllocateNewBlock();
00263
00264 while(len>curblock->blocksize-curblock->cursize)
00265
00266
00267 {
00268 mymemcpy(curblock->data+curblock->cursize,data,
00269 curblock->blocksize-curblock->cursize);
00270
00271 data +=curblock->blocksize-curblock->cursize;
00272 overallsize +=curblock->blocksize-curblock->cursize;
00273 len -=curblock->blocksize-curblock->cursize;
00274 curblock->cursize=curblock->blocksize;
00275
00276 curblock->next=AllocateNewBlock();
00277
00278 curblock=curblock->next;
00279 }
00280 mymemcpy(curblock->data+curblock->cursize,data,len);
00281 curblock->cursize+=len;
00282 overallsize+=len;
00283 }
00284
00285
00286
00287
00288
00289 void StoreChar(unsigned char c)
00290
00291 {
00292 if(curblock==NULL)
00293
00294 firstblock=curblock=AllocateNewBlock();
00295 else
00296 {
00297 if(curblock->blocksize-curblock->cursize==0)
00298
00299 {
00300 curblock->next=AllocateNewBlock();
00301 curblock=curblock->next;
00302 }
00303 }
00304 curblock->data[curblock->cursize]=c;
00305 curblock->cursize++;
00306 overallsize++;
00307 }
00308
00309 void StoreCompressedUInt(unsigned long val,unsigned char offs=0)
00310
00311
00312
00313
00314 {
00315 if(val<128-(unsigned)offs)
00316 StoreChar((unsigned char)val+offs);
00317 else
00318 {
00319 if(val<16384)
00320 {
00321 StoreChar((unsigned char)(val>>8)+128);
00322 StoreChar((unsigned char)val);
00323 }
00324 else
00325 {
00326 StoreChar((unsigned char)(val>>24)+192);
00327 StoreChar((unsigned char)(val>>16));
00328 StoreChar((unsigned char)(val>>8));
00329 StoreChar((unsigned char)(val));
00330 }
00331 }
00332 }
00333
00334 void StoreCompressedSInt(char isneg,unsigned long val)
00335
00336
00337 {
00338 if(val<64)
00339 StoreChar((unsigned char)val+(isneg ? 64 : 0));
00340 else
00341 {
00342 if(val<8192)
00343 {
00344 StoreChar((unsigned char)(val>>8)+(isneg ? (128+32) : 128));
00345 StoreChar((unsigned char)val);
00346 }
00347 else
00348 {
00349 StoreChar((unsigned char)(val>>24)+(isneg ? (192+32) : 192));
00350 StoreChar((unsigned char)(val>>16));
00351 StoreChar((unsigned char)(val>>8));
00352 StoreChar((unsigned char)val);
00353 }
00354 }
00355 }
00356
00357 void StoreCompressedSInt(long val)
00358
00359 {
00360 if(val&0x80000000L)
00361 StoreCompressedSInt(1,-(long)val);
00362 else
00363 StoreCompressedSInt(0,val);
00364 }
00365
00366 void StoreUInt32(unsigned long val)
00367
00368 {
00369 StoreCompressedUInt(val);
00370 }
00371
00372 void StoreSInt32(char isneg,unsigned long val)
00373
00374 {
00375 StoreCompressedSInt(isneg,val);
00376 }
00377
00378
00379
00380
00381 void StartNewMemBlock()
00382
00383 {
00384 MemStreamMarker *newmarker=(MemStreamMarker *)GetByteBlock(sizeof(MemStreamMarker));
00385
00386 *newmarker=curmarker;
00387
00388
00389 curmarker.block=curblock;
00390 curmarker.pos=newmarker;
00391 }
00392
00393 void RemoveLastMemBlock()
00394
00395 {
00396 MemStreamBlock *block,*nextblock;
00397 int newsize;
00398
00399
00400
00401 block=curmarker.block->next;
00402
00403 while(block!=NULL)
00404 {
00405 overallsize-=block->cursize;
00406 nextblock=block->next;
00407 ReleaseBlock(block);
00408 block=nextblock;
00409 }
00410 curmarker.block->next=NULL;
00411
00412
00413 block=curblock=curmarker.block;
00414
00415
00416 newsize=(char *)curmarker.pos-block->data;
00417
00418
00419 curmarker=*(curmarker.pos);
00420
00421 #ifdef RELEASEMEM_SAFE
00422
00423
00424 memset(block->data+newsize,0xcd,block->cursize-newsize);
00425 #endif
00426
00427 overallsize-=(block->cursize-newsize);
00428 block->cursize=newsize;
00429
00430 if(block->blocksizeidxidx<BLOCKSIZE_IDXNUM-1)
00431
00432 curblocksizeidxidx=block->blocksizeidxidx+1;
00433 else
00434 curblocksizeidxidx=block->blocksizeidxidx;
00435 }
00436
00437 void ReleaseMemory(unsigned long firstblocksizeidx)
00438
00439 {
00440 MemStreamBlock *block=firstblock,
00441 *nextblock;
00442 while(block!=NULL)
00443 {
00444 nextblock=block->next;
00445 ReleaseBlock(block);
00446 block=nextblock;
00447 }
00448 Initialize(firstblocksizeidx);
00449 }
00450
00451 void *operator new(size_t size, MemStreamer *mem) { return mem->GetByteBlock(size); }
00452 void operator delete(void *ptr) {}
00453 };
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 #endif