1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <sys/types.h>
class IMemoryManager {
public:
virtual void* allocate(size_t) = 0;
virtual void free(void*) = 0;
};
class MemoryManager: public IMemoryManager{
struct FreeStore
{
FreeStore *next;
FreeStore *prev;
size_t size;
bool flag;
};
void expandPoolSize ();
void cleanUp ();
FreeStore* freeStoreHead;
public:
MemoryManager () {
freeStoreHead = 0;
expandPoolSize ();
}
virtual ~MemoryManager () {
cleanUp ();
}
virtual void* allocate(size_t);
virtual void free(void*);
};
MemoryManager gMemoryManager;
class Complex {
public:
Complex (double a, double b): r (a), c (b) {}
inline void* operator new(size_t);
inline void operator delete(void*);
private:
double r;
double c;
};
inline void* MemoryManager::allocate(size_t size) {
FreeStore* mark = freeStoreHead;
do {
if ((freeStoreHead->flag == false) && (freeStoreHead->size>=size)) {
FreeStore* head = freeStoreHead;
for (int i = 0; i < size; i++) {
freeStoreHead->flag = true;
freeStoreHead = freeStoreHead->next;
}
freeStoreHead->size = head->size - size;
head->size = size;
return head;
} else {
for (int i = freeStoreHead->size; i > 0; i--) {
freeStoreHead=freeStoreHead->next;
}
}
} while (mark == freeStoreHead);
expandPoolSize ();
return gMemoryManager.allocate(size);
}
inline void MemoryManager::free(void* deleted) {
freeStoreHead = static_cast <FreeStore*> (deleted);
FreeStore* head = freeStoreHead;
for (int i = head->size; i > 0; i--) {
head->flag = false;
head = head->next;
}
if (head->flag == false) {
freeStoreHead->size = freeStoreHead->size + head->size;
head->size = 0;
}
if (freeStoreHead->prev->flag == false) {
head = freeStoreHead;
do {
freeStoreHead = freeStoreHead->prev;
} while (freeStoreHead->size != 0);
freeStoreHead->size = freeStoreHead->size + head->size;
head->size = 0;
}
}
void* Complex::operator new (size_t size) {
return gMemoryManager.allocate(size);
}
void Complex::operator delete (void* pointerToDelete) {
gMemoryManager.free(pointerToDelete);
}
#define POOLSIZE 1024
void MemoryManager::expandPoolSize () {
size_t size = (sizeof(Complex) > sizeof(FreeStore*)) ?
sizeof(Complex) : sizeof(FreeStore*);
FreeStore* head = reinterpret_cast <FreeStore*> (new char[size]);
freeStoreHead = head;
freeStoreHead->flag = false;
freeStoreHead->size = POOLSIZE;
for (int i = 0; i < POOLSIZE; i++) {
FreeStore* buf = reinterpret_cast <FreeStore*> (new char [size]);
buf->flag = false;
buf->size = 0;
buf->prev = head;
head->next = buf;
head = head->next;
}
freeStoreHead->prev = head;
head->next = freeStoreHead;
freeStoreHead->flag = false;
freeStoreHead->size = POOLSIZE;
}
void MemoryManager::cleanUp() {
FreeStore* nextPtr = freeStoreHead;
for (; nextPtr; nextPtr = freeStoreHead) {
freeStoreHead = freeStoreHead->next;
delete [] nextPtr;
}
}
int main(int argc, char* argv[]) {
Complex* array[1000];
for (int i = 0;i < 5000; i++) {
for (int j = 0; j < 1000; j++) {
array[j] = new Complex (i, j);
}
for (int j = 0; j < 1000; j++) {
delete array[j];
}
}
return 0;
}