From 8db54e61bcb75b13820964dab2a1d39fca3f98c7 Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sat, 31 Dec 2011 03:02:49 +0000
Subject: [PATCH] [Add] Memory Allocation is almost ready. Avoid using this
 build!!! I am not finished.

---
 src/libUnuk/MemClass.h     | 45 +++++++++++++++++++++++++++++++
 src/libUnuk/MemManager.cpp | 55 ++++++++++++++++++++++++++++++++++++++
 src/libUnuk/MemManager.h   |  9 +++++++
 3 files changed, 109 insertions(+)

diff --git a/src/libUnuk/MemClass.h b/src/libUnuk/MemClass.h
index 03e8b5d..47185a0 100644
--- a/src/libUnuk/MemClass.h
+++ b/src/libUnuk/MemClass.h
@@ -26,4 +26,49 @@ private:
   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
diff --git a/src/libUnuk/MemManager.cpp b/src/libUnuk/MemManager.cpp
index a6b06a3..f3cb989 100644
--- a/src/libUnuk/MemManager.cpp
+++ b/src/libUnuk/MemManager.cpp
@@ -1,6 +1,8 @@
 #include "MemManager.h"
 #include "MemClass.h"
 
+MemManager gMemManager;
+
 MemManager::MemManager(void) {
 
 }
@@ -8,3 +10,56 @@ 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;
+}
diff --git a/src/libUnuk/MemManager.h b/src/libUnuk/MemManager.h
index 368cbbd..3f8efcb 100644
--- a/src/libUnuk/MemManager.h
+++ b/src/libUnuk/MemManager.h
@@ -28,6 +28,15 @@ private:
   list<void*>    _byte24PtrList;
   list<void*>    _byte32PtrList;
   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