-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemalloc.h
More file actions
162 lines (120 loc) · 3.34 KB
/
Copy pathemalloc.h
File metadata and controls
162 lines (120 loc) · 3.34 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
#ifndef EMALLOC_H
#define EMALLOC_H
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdatomic.h>
#include "econfig.h"
enum EBitmapSlot
{
E_BIT_FREE = 0,
E_BIT_USED = 1,
};
enum EResult
{
E_RESULT_OK = 0,
E_RESULT_ERROR = 1,
E_RESULT_SLAB_ALLOCATION = 2,
};
typedef void slab_t;
struct slab_chain_head
{
uint16_t object_size;
uint32_t slab_count;
uint32_t first_free_index;
slab_t* first_free_slab;
slab_t* first_slab;
};
typedef struct slab_chain_head slab_chain_head_t;
struct slab_header
{
uint32_t magic1;
// Size and count of each object in this slab (count includes the objects used by the header)
uint16_t object_size;
uint16_t object_count;
// Number of free objects in this slab
uint16_t free_object_count;
// First object that is not a header
uint16_t first_usable_index;
// First free object (refilled after a free or allocation)
uint16_t first_free_index;
// Bookkeeping info for the slabs
uint32_t index_in_chain;
slab_chain_head_t* head;
slab_t* next_slab;
uint32_t magic2;
// Bitmap of free objects
uint8_t bitmap[];
};
typedef struct slab_header slab_header_t;
typedef void buddy_arena_t;
struct buddy_chain_head
{
uint32_t arena_count;
uint16_t first_free_of_size_index[BUDDY_ALLOCATOR_SIZES];
buddy_arena_t* head_arena;
buddy_arena_t* tail_arena;
buddy_arena_t* first_free_of_size[BUDDY_ALLOCATOR_SIZES];
};
typedef struct buddy_chain_head buddy_chain_head_t;
struct buddy_header
{
uint32_t magic1;
// Bookkeeping for the arena chain
uint32_t index_in_chain;
buddy_arena_t* next_arena;
// Free slot counts
uint16_t free_slot_count_of_size[BUDDY_ALLOCATOR_SIZES];
// Bitmap of slots used by the slabs
uint8_t slab_bitmap[BUDDY_SLAB_BITMAP_BYTES];
uint16_t magic2;
// Bitmaps of all used slots for each slot size
// Stacked one after the other
// (512 bits - 1 slot), (256 bits - 2 slots), (128 bits - 4 slots), ( ... )
uint8_t slot_bitmap[BUDDY_SLOT_BITMAP_BYTES];
};
typedef struct buddy_header buddy_header_t;
struct mmap_header
{
uint32_t magic1;
void* allocation_address;
size_t allocation_length;
uint32_t magic2;
};
typedef struct mmap_header mmap_header_t;
struct spinlock
{
atomic_flag locked;
};
typedef struct spinlock spinlock_t;
#define SPINLOCK_INIT { ATOMIC_FLAG_INIT }
void panic(const char* error);
void spin_lock(spinlock_t* lock);
void spin_unlock(spinlock_t* lock);
void* brk_allocate_buddy_arena();
void *brk_get_allocated_heap_end(void);
void* buddy_alloc(size_t size);
void* buddy_alloc_slab(size_t size);
void buddy_free(void* ptr);
size_t buddy_usable_size(void* ptr);
void* slab_alloc(size_t size);
void slab_free(void* ptr);
size_t slab_usable_size(void* ptr);
void* mmap_alloc(size_t size);
void mmap_free(void* ptr);
size_t mmap_usable_size(void* ptr);
void* emalloc(size_t size);
void* ecalloc(size_t count, size_t size);
void* erealloc(void *ptr, size_t size);
void efree(void* ptr);
size_t emalloc_usable_size(void* ptr);
void log_malloc_call_start(size_t size);
void log_malloc_call_end(void *ptr);
void log_free_call(void *ptr);
void log_heap_extension(void *ptr);
void log_heap_tamper(void *old_ptr, void* new_ptr);
#endif