-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple_allocator.cpp
139 lines (100 loc) · 3.24 KB
/
simple_allocator.cpp
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
#include <iostream>
#include <cassert>
#include <dstruct.hpp>
/*
g++ dstruct/simple_allocator.cpp -I libs/DStruct && ./a.out
*/
//#define DEBUG
#define LOGI(...) printf("%s: ", __func__); printf(__VA_ARGS__); printf("\n")
#ifdef DEBUG
#define LOGD(...) LOGI(__VA_ARGS__)
#else
#define LOGD(...)
#endif
char array[1024] { 0 }; // 1k memory
// mem-flag: 0 free, other allocated
#define MEM_BLOCK_SIZE 32
struct SimpleAllocator {
// Unused-Flag 0
using MemBlockFlag = unsigned long long;
struct MemBlock {
MemBlockFlag flag; // flag area
char mem[MEM_BLOCK_SIZE - sizeof(MemBlockFlag)]; // useable area
};
static void init() {
LOGI("init allocate: memory address %p, size %ld", array, sizeof(array));
char *memPtr = array;
while (memPtr + sizeof(MemBlock) < array + sizeof(array)) {
auto mbPtr = (MemBlock *)memPtr;
mbPtr->flag = 0;
memPtr = memPtr + MEM_BLOCK_SIZE;
LOGD("address %p, block-size %ld", mbPtr, sizeof(MemBlock));
}
}
static void * malloc(int size) {
assert(size <= sizeof(MemBlock) - sizeof(MemBlockFlag));
bool allocateFailed = true;
MemBlock *mbPtr = nullptr;
for (int i = 0; i <= sizeof(array) - sizeof(MemBlock); i += sizeof(MemBlock)) {
mbPtr = (MemBlock *)(array + i);
if (mbPtr->flag == 0) {
allocateFailed = false;
break;
}
}
if (allocateFailed) {
LOGI("request size %d, memory allocate failed...", size);
return nullptr;
}
LOGD("addr %p, request size %d", mbPtr, size);
mbPtr->flag = size;
return &(mbPtr->mem);
}
static void free(void *ptr) {
auto mbPtr = (MemBlock *)((char *)ptr - sizeof(MemBlockFlag));
LOGD("addr %p, size %lld, block-size %d", mbPtr, mbPtr->flag, MEM_BLOCK_SIZE);
assert(
array <= (char *)mbPtr &&
(char *)mbPtr <= array + sizeof(array) - sizeof(MemBlock) &&
"memory free failed - range"
);
assert(mbPtr->flag != 0 && "memory free failed - flag error(double free)");
mbPtr->flag = 0;
}
};
struct RGB {
char r;
short g;
char b;
};
// test SimpleAllocator
int main() {
dstruct::Vector<int *> ptrVec;
SimpleAllocator::init();
for (int i = 0; i < 50; i++) {
auto intPtr = (int *) SimpleAllocator::malloc(sizeof(int));
if (intPtr == nullptr)
break;
*intPtr = i;
ptrVec.push_back(intPtr);
}
RGB *rgbPtr = (RGB *) SimpleAllocator::malloc(sizeof(RGB));
if (rgbPtr == nullptr) {
int *intPtr = ptrVec.back();
LOGI("free %d, addr %p", *intPtr, intPtr);
SimpleAllocator::free(ptrVec.back());
ptrVec.pop_back();
rgbPtr = (RGB *) SimpleAllocator::malloc(sizeof(RGB));
}
rgbPtr->r = 1;
rgbPtr->g = 2;
rgbPtr->b = 3;
LOGI("rgb: (%d, %d, %d)", rgbPtr->r, rgbPtr->g, rgbPtr->b);
for (int i = 0; i < ptrVec.size(); i++) {
SimpleAllocator::free(ptrVec[i]);
}
// failed test set
//SimpleAllocator::malloc(25); // size limit
//SimpleAllocator::free(ptrVec[0]); // test double free
return 0;
}