-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathheap.h
80 lines (64 loc) · 2 KB
/
heap.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
/*
* heap.h
* Copyright (C) 2018 hzshang <[email protected]>
*
* Distributed under terms of the MIT license.
*/
#ifndef HEAP_H
#define HEAP_H
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HEAP_SIZE 0x100000
#define BINS_SIZE 0x50
#define USE 0x1
#define NOUSE 0x0
#define MAP 0x2
#define NOMAP 0x0
/*
* Chunk struct
* |--------------------|
* | Last Chunk Size | last bit 0:free, 1:use
* |--------------------| last two bit: 1:mmaped chunk
* | Chunk Size |
* |--------------------|
* | Content | Next ptr |
* |--------------------|
*/
struct chunk{
size_t last_size;
size_t size;
struct chunk* next;
};
typedef struct chunk* chunk_ptr;
//READ/WRITE
#define chunk_content(ptr) ((void *)&(ptr->next))
#define get_chunk_by_content(content) ((chunk_ptr)(content-2*sizeof(size_t)))
//ONLY READ
#define chunk_is_use(ptr) (ptr->last_size & 0x1)
#define chunk_is_map(ptr) (ptr->last_size & 0x2)
#define chunk_flag(ptr) (ptr->last_size & 0x7)
#define chunk_last_size(ptr) (ptr->last_size & (~0x7))
#define chunk_down(ptr) ((chunk_ptr)((void*)ptr + ptr->size))
#define chunk_up(ptr) ((chunk_ptr)((void*)ptr - chunk_last_size(ptr)))
#define index_of_bins(size) ((size-2*sizeof(void*))/sizeof(chunk_ptr))
#define mem_chunk_size(size) (((size+sizeof(void*)-1) & (~(sizeof(void*)-1))) + 2*sizeof(void*))
#define MAX_SMALL_BIN_SIZE (BINS_SIZE*sizeof(void*)+sizeof(void*))
#define update_chunk(ptr,self_size,l_size,flag) do{ \
ptr->size=self_size; \
ptr->last_size = (l_size & (~0x7))|flag; \
} while(0)
#define update_chunk_flag(ptr,flag) do{ \
ptr->last_size=(ptr->last_size & (~0x7))|flag; \
} while(0)
#define malloc(size) mymalloc(size)
#define free(ptr) myfree(ptr)
static chunk_ptr merge_chunk(chunk_ptr, chunk_ptr);
static void* malloc_from_bins(int, size_t);
static void install_chunk(chunk_ptr);
static chunk_ptr uninstall_chunk(chunk_ptr);
int heap_init();
void* mymalloc(size_t size);
int myfree(void*);
#endif /* !HEAP_H */