-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.h
151 lines (138 loc) · 3.18 KB
/
alloc.h
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
#pragma once
#include <Windows.h>
#include <cassert>
#include <string>
#include <iostream>
#include <stdio.h>
const size_t PAGE_SIZE = 10485760;
class FSAPage //fixed-size allocator
{
public:
struct Metadata
{
bool isFree = false;
const int marker = INT_MAX;
ptrdiff_t freeDist = 0; //distance towards the next free memory block
};
struct Service
{
/*bool wasDestroyCalled = false;*/
size_t numPages = 0;
size_t numAlloc = 0; //the number of allocated blocks
};
FSAPage();
~FSAPage();
void init(size_t size);
void destroy();
void* alloc();
bool free(void* p);
void dumpBlocks() const;
void dumpStat() const;
size_t getOccupiedSize() const;
size_t getAllocatedSize() const;
bool isFree() const;
private:
void* pageStart;
FSAPage* nextPage;
void* fH; //free-list head ptr
size_t numLinked; //number of linked blocks on the page
size_t numBlocks; //total number of blocks on the page
size_t blockSize; //size of a block (incl. metadata size)
size_t metaSizeRounded; //size of metadata struct rounded to the next multiple of 8
Service service;
};
class CAPage //coalesce allocator
{
public:
struct Metadata
{
bool isFree = false;
const int marker = INT_MAX;
ptrdiff_t nextFreeDist = 0; //distance towards the next memory block
ptrdiff_t prevFreeDist = 0; //distance towards the previous memory block
size_t blockSize = 0;
ptrdiff_t prevBlockDist = -1000; //previous (on the page) block size (used to access its metadata)
};
struct Service
{
/*bool wasDestroyCalled = false;*/
size_t numPages = 0;
size_t numAlloc = 0; //the number of allocated blocks
size_t occupiedSize = 0;
};
CAPage();
~CAPage();
void init();
void destroy();
void* alloc(size_t size);
bool free(void* p);
void dumpBlocks() const;
void dumpStat() const;
size_t getOccupiedSize() const;
size_t getAllocatedSize() const;
bool isFree() const;
private:
void* pageStart;
CAPage* nextPage;
void* fH; //free-list head ptr
size_t numBlocks;
size_t metaSizeRounded;
Service service;
};
class FallbackAlloc
{
struct Service
{
size_t allocPages = 0;
size_t occupiedSize = 0;
};
struct FAPage
{
void* pageStart = NULL;
FAPage* nextPage = NULL;
FAPage* prevPage = NULL;
size_t pageSize = 0;
};
public:
FallbackAlloc();
virtual ~FallbackAlloc();
void init();
void destroy();
void* alloc(size_t size);
bool free(void* p);
/*void dumpBlocks() const;*/
void dumpStat() const;
size_t getOccupiedSize() const;
bool isFree() const;
private:
FAPage* first;
Service service;
};
class MemoryAllocator
{
struct Service
{
bool wasInitCalled = false;
bool wasDestroyCalled = false;
};
public:
MemoryAllocator();
virtual ~MemoryAllocator();
virtual void init();
virtual void destroy();
virtual void* alloc(size_t size);
virtual void free(void* p);
virtual void dumpStat() const;
virtual void dumpBlocks() const;
bool isFree() const;
private:
FSAPage fsAlloc16;
FSAPage fsAlloc32;
FSAPage fsAlloc64;
FSAPage fsAlloc128;
FSAPage fsAlloc256;
FSAPage fsAlloc512;
CAPage cAlloc;
FallbackAlloc fAlloc;
Service service;
};