summaryrefslogtreecommitdiff
path: root/src/memory.cpp
blob: 793c4d08f02ab282c8f392a5b4b7f2892374307e (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

#include "memory.hpp"

struct MemBlock {
	char *data;
	std::size_t size;
};

struct MemBucket {
	int64_t size;
	int64_t head;
	int64_t lostBytes;
	int64_t allocs;
	int64_t lastEmptyBlockIndex;
	int64_t maxBlockCount;
	char *blocks;
	char *ptr;
	bool transient;
};

MemBucket buckets[128];
int64_t bucketHead = 0;

int64_t InitNewBucket(int64_t sz, bool transient = false) {
	int64_t blockCount = sz * 0.01;
	auto &bkt = buckets[bucketHead];
	bkt.size = sz;
	bkt.head = 0;
	bkt.lostBytes = 0;
	bkt.allocs = 0;
	bkt.lastEmptyBlockIndex = -1;
	bkt.maxBlockCount = blockCount < 10 ? 10 : blockCount;
	bkt.blocks = reinterpret_cast<char *>(std::malloc(sz));
	bkt.ptr = bkt.blocks + (sizeof(MemBlock) * bkt.maxBlockCount);
	bkt.transient = transient;
	auto *memBlock = reinterpret_cast<MemBlock *>(bkt.blocks);
	memBlock->data = bkt.ptr;
	memBlock->size = sz - (sizeof(MemBlock) * bkt.maxBlockCount);
	return bucketHead++;
}

void DestroyBucket(MemBucket *bkt) {
	std::free(bkt->blocks);
	bkt->size = 0;
	bkt->head = 0;
	bkt->lostBytes = 0;
	bkt->allocs = 0;
	bkt->lastEmptyBlockIndex = -1;
	bkt->maxBlockCount = 0;
	bkt->blocks = CAFE_BABE(char);
	bkt->ptr = CAFE_BABE(char);
	bkt->transient = false;
}

void *Pke_New(std::size_t sz, MemBucket *bkt) {
	MemBlock *blocks = reinterpret_cast<MemBlock *>(bkt->blocks);
	int64_t availBlockIndex = -1;
	void *data = nullptr;
	for (int64_t i = 0; i <= bkt->lastEmptyBlockIndex; ++i) {
		auto blk = blocks[i];
		if (blk.size > sz) {
			availBlockIndex = i;
			break;
		}
	}
	if (availBlockIndex != -1) {
		data = blocks[availBlockIndex].data;
		blocks[availBlockIndex].data += sz;
		blocks[availBlockIndex].size -= sz;
		assert(blocks[availBlockIndex].size < bkt->size);
		if (blocks[availBlockIndex].size == 0) {
			if (availBlockIndex != bkt->lastEmptyBlockIndex) {
				blocks[availBlockIndex].data = blocks[bkt->lastEmptyBlockIndex].data;
				blocks[availBlockIndex].size = blocks[bkt->lastEmptyBlockIndex].size;
			}
			bkt->lastEmptyBlockIndex -= 1;
		}
	} else {
		assert(bkt->head + sz <= bkt->size && "memory bucket specified, but full");
		data = bkt->ptr + bkt->head;
		bkt->head += sz;
	}
	bkt->allocs++;
	return data;
}

void *Pke_New(std::size_t sz) {
	MemBucket *bkt = nullptr;
	for (long i = 0; i < bucketHead; ++i) {
		if (buckets[i].transient == false && buckets[i].size - buckets[i].head > sz) {
			bkt = &buckets[i];
		}
	}
	if (bkt == nullptr) {
		bkt = &buckets[InitNewBucket(DEFAULT_BUCKET_SIZE)];
	}
	return Pke_New(sz, bkt);
}

void Pke_Delete(const void *ptr, std::size_t sz, MemBucket *bkt) {
	assert(ptr >= bkt->ptr && ptr < bkt->ptr + bkt->size && "pointer not in memory bucket range");
	bkt->allocs--;
	if (bkt->allocs == 0) {
		bkt->head = 0;
		bkt->lastEmptyBlockIndex = -1;
		return;
	}
	if (ptr == bkt->ptr + bkt->head - sz) {
		bkt->head -= sz;
	} else {
		MemBlock *blocks = reinterpret_cast<MemBlock *>(bkt->blocks);
		for (int64_t i = 0; i < bkt->lastEmptyBlockIndex; ++i) {
			auto &blk = blocks[i];
			if (blk.data == reinterpret_cast<const char *>(ptr) + sz) {
				blk.data -= sz;
				blk.size += sz;
				return;
			}
			if (reinterpret_cast<const char *>(ptr) == blk.data + blk.size ) {
				blk.size += sz;
				return;
			}
		}
		if (bkt->lastEmptyBlockIndex != bkt->maxBlockCount) {
			bkt->lastEmptyBlockIndex += 1;
			blocks[bkt->lastEmptyBlockIndex].data = reinterpret_cast<char *>(const_cast<void *>(ptr));
			blocks[bkt->lastEmptyBlockIndex].size = sz;
		} else {
			bkt->lostBytes += sz;
		}
	}
}

void Pke_Delete(const void *ptr, std::size_t sz) {
	MemBucket *bkt = nullptr;
	for (long i = 0; i < bucketHead; ++i) {
		bkt = &buckets[i];
		if (ptr >= bkt->ptr && ptr < bkt->ptr + bkt->size) break;
	}
	assert(bkt != nullptr && "failed to determine correct memory bucket");
	Pke_Delete(ptr, sz, bkt);
}

MemBucket *Pke_BeginTransientBucket(int64_t sz) {
	return &buckets[InitNewBucket(sz, true)];
}

void Pke_EndTransientBucket(MemBucket *bkt) {
	int64_t foundIndex = -1;
	for (int64_t i = 0; i < bucketHead; ++i) {
		if (&buckets[i] == bkt) {
			foundIndex = i;
			DestroyBucket(&buckets[i]);
			break;
		}
	}
	if (foundIndex == bucketHead) {
		bucketHead--;
	}
}

void Pke_MemoryFlush() {
	for (long i = bucketHead - 2; i > -1; --i) {
		if (buckets[i].head != 0) break;
		if (buckets[i+1].head != 0) break;
		if (buckets[i].transient == true) break;
		if (buckets[i+1].transient == true) break;
		bucketHead--;
		DestroyBucket(&buckets[i + 1]);
	}
}

uint64_t Buckets_NewHandle(std::size_t bucketBytes, uint64_t bucketItemCount, uint64_t &bucketIncrementer, uint64_t &bucketCounter, uint64_t &itemCounter, void*& buckets) {
	uint64_t newHandle{itemCounter | bucketCounter};

	itemCounter += uint64_t{1ULL << 32};
	if (itemCounter > uint64_t{(bucketItemCount - 1) << 32}) {
		itemCounter = 0ULL;
		bucketCounter += 1;
	}
	if (bucketCounter > bucketIncrementer) {
		int64_t newIncrement = bucketIncrementer * 1.5;
		char * newBuckets = reinterpret_cast<char *>(Pke_New(bucketBytes * newIncrement));
		std::memcpy(newBuckets, buckets, bucketBytes * bucketIncrementer);
		Pke_Delete(buckets, bucketBytes * bucketIncrementer);
		buckets = newBuckets;
		bucketIncrementer = newIncrement;
	}

	return newHandle;
}

void Pke_DebugPrint() {
	printf("Memory Manager printout:\nBucket count: %li\n", bucketHead + 1);
	for (long i = 0; i < bucketHead; ++i) {
		printf("- bucket #%li\n", i);
		printf("\tsize:                %li\n", buckets[i].size);
		printf("\thead:                %li\n", buckets[i].head);
		printf("\tlostBytes:           %li\n", buckets[i].lostBytes);
		printf("\tallocs:              %li\n", buckets[i].allocs);
		printf("\tlastEmptyBlockIndex: %li\n", buckets[i].lastEmptyBlockIndex);
		printf("\tmaxBlockCount:       %li\n", buckets[i].maxBlockCount);
		printf("\tblocks:              %p\n", buckets[i].blocks);
		printf("\tptr:                 %p\n", buckets[i].ptr);
		printf("\ttransient:           %i\n", buckets[i].transient);
	}
}