-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_allocator.c
More file actions
75 lines (56 loc) · 1.47 KB
/
Copy pathcustom_allocator.c
File metadata and controls
75 lines (56 loc) · 1.47 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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#define u8 uint8_t
#define u16 uint16_t
#define STACK_SIZE 32
#define HEAP_SIZE STACK_SIZE * 4
#define HEADER 4
static u16 IN_USE;
typedef struct virtual_memory {
u8 stack[STACK_SIZE];
char** unmapped;
u8 heap[HEAP_SIZE];
struct {
char** data;
char** bss;
char* text;
}data_t;
}virtual_memory_t;
typredef struct entity {
/* interface for using our heap */
u8* ptr;
u16 size;
}entity_t;
entity_t LIST[40]; // with all of entities in the LIST
// we will be take access to the heap
entity_t* new_entity(size_t size) {
if (LIST[0] == NULL && LIST[0].size == 0) {
static virtual_memory_t vm;
LIST[0].ptr = vm.heap;
LIST[0].size = HEAP_SIZE;
IN_USE++;
}
entity_t* best = LIST;
for(unsigned i = 0; i < IN_USE; i++) {
if (LIST[i].size >= size && LIST[i].size < best->size) {
best = &LIST[i];
}
}
return best;
}
void* my_own_malloc(size_t size) {
assert(size <= HEAP_SIZE); // max memory for object
// what can give custom allocator
size += HEADER;
entity_t* newEntityVariable = new_entity(size);
}
void my_own_free(void* ptr) {
}
void test() {
int* test_variable = my_own_malloc(sizeof(int)); // test variable
}
int main(int argc, char** argv) {
return 0;
}