00001 //a simple (void *) container to help avoid memory leaks (DF/3.0) 00002 00003 class Memory { 00004 public: 00005 void *memory; 00006 Memory() {memory = NULL;} 00007 ~Memory() {Free();} 00008 void Free() {if (memory != NULL) SystemCode::MemFree(memory); memory = NULL;} 00009 void *Alloc(int size) {Free(); memory = SystemCode::MemAlloc(size);} 00010 void *ReAlloc(int size) {memory = SystemCode::MemReAlloc(memory, size);} 00011 // conversion operators 00012 operator void* () {return memory;} 00013 operator char* () {return (char*)memory;} //aka int8 00014 operator uint8* () {return (uint8*)memory;} 00015 operator uint16* () {return (uint16*)memory;} 00016 operator uint32* () {return (uint32*)memory;} 00017 operator int* () {return (int*)memory;} 00018 // operator uint* () {return (uint*)memory;} 00019 //subscript operator 00020 uint8 & operator [] ( int pos ) {return ((uint8*)memory)[pos];} 00021 }; 00022 00023 #define Memory8 Memory 00024 00025 class Memory16 : public Memory { 00026 public: 00027 Memory16() {memory = NULL;} 00028 ~Memory16() {Free();} 00029 void *Alloc(int size) {Free(); memory = SystemCode::MemAlloc(size*2);} 00030 void *ReAlloc(int size) {memory = SystemCode::MemReAlloc(memory, size*2);} 00031 //subscript operator 00032 uint16 & operator [] ( int pos ) {return ((uint16*)memory)[pos];} 00033 }; 00034 00035 class Memory32 : public Memory { 00036 public: 00037 Memory32() {memory = NULL;} 00038 ~Memory32() {Free();} 00039 void *Alloc(int size) {Free(); memory = SystemCode::MemAlloc(size*4);} 00040 void *ReAlloc(int size) {memory = SystemCode::MemReAlloc(memory, size*4);} 00041 //subscript operator 00042 uint32 & operator [] ( int pos ) {return ((uint32*)memory)[pos];} 00043 }; 00044