[Add] Memory Allocation is almost ready. Avoid using this build!!! I am not finished.

This commit is contained in:
Rtch90 2011-12-31 03:02:49 +00:00
parent 23e8686dad
commit 8db54e61bc
3 changed files with 109 additions and 0 deletions

View File

@ -26,4 +26,49 @@ private:
double c; double c;
}; };
class Coordinate {
int coordX;
int coordY;
int coordZ;
std::string name;
public:
Coordinate(void) : coordX(0), coordY(0), coordZ(0), name("") {}
inline void* operator new(size_t size) {
return gMemManager.Allocate(sizeof(Coordinate));
}
inline void operator delete(void* object) {
gMemManager.Free(object);
}
};
class Scheduler {
std::vector<int> jobNumber;
std::vector<int> maxJobTime;
int startTime;
public:
Scheduler(void) {}
inline void* operator new(size_t size) {
return gMemManager.Allocate(sizeof(Scheduler));
}
inline void operator delete(void* object) {
gMemManager.Free(object);
}
};
const int SCHEDULER_SIZE = sizeof(Scheduler);
const int COMPLEX_SIZE = sizeof(Complex);
const int COORDINATE_SIZE = sizeof(Coordinate);
// Number of elements in a single pool can be chosen on
// application requirement.
const int POOL_SIZE = 1024;
// Depending on the application this may change.
const int MAX_BLOCK_SIZE = 36;
#endif #endif

View File

@ -1,6 +1,8 @@
#include "MemManager.h" #include "MemManager.h"
#include "MemClass.h" #include "MemClass.h"
MemManager gMemManager;
MemManager::MemManager(void) { MemManager::MemManager(void) {
} }
@ -8,3 +10,56 @@ MemManager::MemManager(void) {
MemManager::~MemManager(void) { MemManager::~MemManager(void) {
} }
void* MemManager::Allocate(size_t size) {
void* base = 0;
switch(size) {
case SCHEDULER_SIZE: { // 28
if(_byte32PtrList.empty()) {
base = new char[32 * POOL_SIZE];
_memoryPoolList.push_back(base);
InitByte32List(base);
}
void* blockPtr = _byte32PtrList.front();
// Size of block set.
*((static_cast<char*>(blockPtr)) + 30) = 32;
// Block is no longer free.
*((static_cast<char*>(blockPtr)) + 31) = 0;
_byte32PtrList.pop_front();
return blockPtr;
}
case COORDINATE_SIZE: { // 36
if(_byte40PtrList.empty()) {
base = new char[40 * POOL_SIZE];
_memoryPoolList.push_back(base);
InitByte40List(base);
}
void* blockPtr = _byte40PtrList.front();
// Size of block set.
*((static_cast<char*>(blockPtr)) + 38) = 40;
// Block is no longer free.
*((static_cast<char*>(blockPtr)) + 39) = 0;
_byte40PtrList.pop_front();
return blockPtr;
}
case COMPLEX_SIZE: { // 16
if(_byte24PtrList.empty()) {
base = new char[24 * POOL_SIZE];
_memoryPoolList.push_back(base);
InitByte24List(base);
}
void* blockPtr = _byte24PtrList.front();
// Size of block set.
*((static_cast<char*>(blockPtr)) + 22) = 32;
// Block is no longer free.
*((static_cast<char*>(blockPtr)) + 23) = 0;
_byte24PtrList.pop_front();
return blockPtr;
}
default:
break;
}
return 0;
}

View File

@ -28,6 +28,15 @@ private:
list<void*> _byte24PtrList; list<void*> _byte24PtrList;
list<void*> _byte32PtrList; list<void*> _byte32PtrList;
list<void*> _byte40PtrList; list<void*> _byte40PtrList;
list<void*> _memoryPoolList;
friend class Scheduler;
friend class Coordinate;
friend class Complex;
void InitByte24List(void* base);
void InitByte32List(void* base);
void InitByte40List(void* base);
}; };
#endif #endif