-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_2.cpp
More file actions
162 lines (157 loc) · 4.25 KB
/
Copy pathmalloc_2.cpp
File metadata and controls
162 lines (157 loc) · 4.25 KB
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
#include <unistd.h>
#include <cstring>
struct MetaData {
size_t size;
bool is_free;
MetaData* next;
MetaData* prev;
};
class Allocator {
private:
MetaData* head;
Allocator() : head(nullptr) {}
public:
Allocator& operator=(const Allocator&) = delete;
Allocator(const Allocator&) = delete;
void* smalloc(size_t size) {
if (size == 0 || size > 100000000){
return nullptr;
}
MetaData* curr = head;
MetaData* last = nullptr;
while (curr) {
if (curr->is_free && curr->size >= size) {
curr->is_free = false;
return (char*)curr + sizeof(MetaData);
}
last = curr;
curr = curr->next;
}
void* brk = sbrk(size + sizeof(MetaData));
if (brk == (void*)-1){
return nullptr;
}
MetaData* meta = (MetaData*)brk;
meta->size = size;
meta->is_free = false;
meta->next = nullptr;
meta->prev = last;
if (last){
last->next = meta;
}
else{
head = meta;
}
return (char*)meta + sizeof(MetaData);
}
static Allocator& get() {
static Allocator instance;
return instance;
}
void* scalloc(size_t num, size_t size) {
if (num == 0 || size == 0){
return nullptr;
}
size_t total = num * size;
void* p = smalloc(total);
if (p){
std::memset(p, 0, total);
}
return p;
}
void sfree(void* p) {
if (!p){
return;
}
MetaData* meta = (MetaData*)((char*)p - sizeof(MetaData));
if (!meta->is_free){
meta->is_free = true;
}
}
void* srealloc(void* oldp, size_t size) {
if (size == 0 || size > 100000000){
return nullptr;
}
if (!oldp){
return smalloc(size);
}
MetaData* meta = (MetaData*)((char*)oldp - sizeof(MetaData));
if (meta->size >= size){
return oldp;
}
void* newp = smalloc(size);
if (!newp){
return nullptr;
}
std::memmove(newp, oldp, meta->size);
sfree(oldp);
return newp;
}
size_t free_bytes() const {
size_t sum = 0;
MetaData* curr = head;
while (curr) {
if (curr->is_free)
sum += curr->size;
curr = curr->next;
}
return sum;
}
size_t meta_size() const {
return sizeof(MetaData);
}
size_t free_blocks() const {
size_t count = 0;
MetaData* curr = head;
while (curr) {
if (curr->is_free)
count++;
curr = curr->next;
}
return count;
}
size_t allocated_bytes() const {
size_t sum = 0;
MetaData* curr = head;
while (curr) {
sum += curr->size;
curr = curr->next;
}
return sum;
}
size_t allocated_blocks() const {
size_t count = 0;
MetaData* curr = head;
while (curr) {
count++;
curr = curr->next;
}
return count;
}
size_t meta_bytes() const {
return allocated_blocks() * sizeof(MetaData);
}
};
void* smalloc(size_t size) {
return Allocator::get().smalloc(size);
}
void* scalloc(size_t num, size_t size) {
return Allocator::get().scalloc(num, size);
}
void sfree(void* p) {
Allocator::get().sfree(p);
}
void* srealloc(void* p, size_t size) {
return Allocator::get().srealloc(p, size);}
size_t _num_free_blocks() {
return Allocator::get().free_blocks();}
size_t _num_free_bytes() {
return Allocator::get().free_bytes();}
size_t _num_allocated_blocks() {
return Allocator::get().allocated_blocks();}
size_t _num_allocated_bytes() {
return Allocator::get().allocated_bytes();}
size_t _num_meta_data_bytes() {
return Allocator::get().meta_bytes();}
size_t _size_meta_data() {
return Allocator::get().meta_size();}