-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallocator.h
executable file
·36 lines (29 loc) · 1010 Bytes
/
allocator.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
// allocator.h
#pragma once
#ifndef OS_CUSTOM_ALLOCATOR_H
#define OS_CUSTOM_ALLOCATOR_H
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
// maximum block size in bytes: 2^20 = 1 MiB
#define MAX_EXP 20
#define MAX_SIZE 1<<MAX_EXP
// block struct
struct block {
size_t size; // size in bytes
bool free; // whether block is available for allocation
void* data; // pointer to actual data
struct block* next; // pointer to next block in list
struct block* buddy; // pointer to buddy
struct block* merge_buddy[MAX_EXP]; // pointer list for buddies of the merged blocks up the 'tree'
};
// start of list
struct block* head;
// functions to be implemented in mm_alloc.c
void* custom_malloc(size_t size);
void* custom_realloc(void* ptr, size_t size);
void custom_free(void* ptr);
// debug functions
void print_block(struct block* b);
void print_list();
#endif