00001 /* 00002 * vm/instruction/storage.cc 00003 * 00004 * VM instruction (program) storage. 00005 * 00006 * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 00007 * 00008 * This file may be distributed and/or modified under the terms of the 00009 * GNU General Public License version 2 as published by the Free Software 00010 * Foundation. (See COPYING.GPL for details.) 00011 * 00012 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00013 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00014 * 00015 */ 00016 00017 #include "storage.h" 00018 00019 00020 namespace VM 00021 { 00022 00023 void InstructionStorage::_ReallocStore(PrgAdr needlen) 00024 { 00025 Assert(!(prg_len&1)); // program len must be even 00026 Assert(!(prg_asize&1)); // allocated size as well 00027 00028 Assert(prg_asize<needlen); 00029 00030 // Re-allocate. 00031 // We will not cycle this loop very often. 00032 while(prg_len>prg_asize) 00033 { 00034 if(prg_asize<256) prg_asize=256; 00035 else if(prg_asize<16384) prg_asize*=2; 00036 else prg_asize+=16384; 00037 } 00038 prg=REALLOC(prg,prg_asize); 00039 } 00040 00041 00042 void InstructionStorage::TightenSize() 00043 { 00044 if(prg_asize>prg_len) 00045 { 00046 prg_asize=prg_len; 00047 prg=REALLOC(prg,prg_asize); 00048 } 00049 } 00050 00051 00052 InstructionStorage::InstructionStorage(PrgAdr prealloc_size) 00053 { 00054 prg_len=0; 00055 prg_asize=prealloc_size; 00056 prg=ALLOC<char>(prg_asize); // NULL for prg_size=0. 00057 } 00058 00059 InstructionStorage::~InstructionStorage() 00060 { 00061 prg=FREE(prg); 00062 prg_asize=0; 00063 prg_len=0; 00064 } 00065 00066 } // end of namespace VM