-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.h
226 lines (195 loc) · 7.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/// \file
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "linked_list.h"
#ifndef __heap__
#define __heap__
#define NUMBER_OF_PAGES 1
#define NO_MD "none" //no meta_data flag
#define PAGE_SIZE (size_t) 2048
// TODO: THIS IS TEMPORARY! If you are accessing the heap through this struct then you need to use a get-function (which might need creating)
struct heap{
void* meta_p; // pointer to heap's metadata (maybe unnecessary)
void* user_start_p; // pointer to start of user's allocated space
void* active_page_list;
void* passive_page_list;
struct page* compact_page_list;
void* end_p; // pointer to end of allocated space
size_t total_size; // total size of the heap (with metadata)
size_t user_size; // size of user's allocated space (total_size minus metadata)
size_t used_space; // amount of bytes allocated.
size_t avail_space; // amount of allocatable space left.
bool unsafe_stack; // whether or not unsafe stack
float gc_threshold; // garbage collector threshold (1.0 = full memory)
ll_head val_list;
};
// TODO: THIS IS TEMPORARY! This is here only for gui.c, nothing outside of heap.c should use this.
typedef struct page{
void* user_start_p; // pointer to start of user's allocated space
void* bump_p; // pointer to next free spot in user's space
void* end_p; // pointer to end of allocated space
bool active; // boolean keeping track if the page is active (true) or passive (false)
bool unsure; // boolean keeping track of whether a page is unsure or not
struct page* next_page; // a pointer to the next page
} page_t;
/**
@brief Heap struct type used to manipulate the heap.
*/
typedef struct heap heap_t;
/**
h_init
@brief Initializes the heap.
@param bytes Size of the heap, preferrably a multiple of PAGE_SIZE.
@param unsafe_stack A bool indicating whether or not there is an unsafe stack
@param gc_threshold A value between 0 and 1 indicating the threshold for when garbage collection is triggered. Should be close to, yet lower than 0.5.
@return A pointer to the heap.
The heap will use some of the allocated space to store metadata.
*/
heap_t *h_init(size_t bytes, bool unsafe_stack, float gc_threshold);
/**
h_delete
@brief Deletes the heap and frees the memory.
@param h The pointer to the heap.
*/
void h_delete(heap_t* h);
/**
h_alloc_data
@brief Allocates space in the heap. This allocation can NOT be used to store pointers as it will not follow any pointers in this allocation when marking garbage.
@param h The pointer to the heap.
@param bytes The amount of bytes to allocate, cannot be larger than PAGE_SIZE.
This will allocate data with an empty format string. It will be ignored when marking garbage.
*/
void *h_alloc_data(heap_t *h, size_t bytes);
/**
h_alloc_struct
@brief Allocates space in the heap. Allocates the appropriate amount of space based on the format string.
@param h The pointer to the heap.
@param format_string A string which describes the scruct which will be stored in the allocation.
@return The pointer to the allocated space.
*/
void* h_alloc_struct(heap_t *h, char *format_string);
/**
h_alloc_compact
@brief Moves object to the compact pages.
@param h The pointer to the heap.
@param object The pointer to the object.
@return The pointer to the newly allocated space in the heap.
*/
void* h_alloc_compact(heap_t *h, void* object);
/**
h_gc
@brief Manually trigger garbage collection.
@param h The pointer to the heap.
@return The number of bytes collected.
*/
size_t h_gc(heap_t* h);
/**
h_avail
@brief Returns the available free memory.
@param h The pointer to the heap.
@return The available free memory.
*/
size_t h_avail(heap_t *h);
/**
h_used
@brief Returns the amount of used memory. Does not count metadata.
@param h The pointer to the heap.
@return The amount of used memory.
*/
size_t h_used(heap_t *h);
// TODO: Rename to md_get_heap_start
/**
get_heap_start
@brief Returns the pointer to the start of the user-allocation area of the heap.
@param h The pointer to the heap.
@return The pointer to the start of the allocation space of the heap.
*/
void* get_heap_start(heap_t *h);
// TODO: Rename to md_get_heap_end
/**
get_heap_end
@brief Returns the pointer to the end of the heap.
@param h The pointer to the heap.
@return The pointer to the end of the heap.
*/
void* get_heap_end(heap_t *h);
/**
update_objects_pointers
@brief Goes through an object's pointers and updates their forwarding addresses.
@param object The pointer to the object.
*/
void update_objects_pointers(void* object);
/**
post_compact_page_reset
@brief Sets the collection pages as the active ones and makes all other pages passive.
@param h The pointer to the heap.
*/
void post_compact_page_reset(heap_t *h);
/************************************/
/* */
/* Metadata wrapper */
/* */
/************************************/
void *enqueue(void* to_be_added, heap_t *h);
/**
@brief Checks if an object is a valid allocated object.
@param object A pointer to the allocated object.
@param h a pointer to the Heap
@return Whether or not the pointer points to a valid object.
*/
bool validate_object(void* object, heap_t *h);
/**
@brief Rmoves object from valid list
@param object A pointer to the allocated object.
@param h a pointer to the heap
*/
void devalidate(void* to_be_devalidated, heap_t *h);
/**
@brief Returns a pointer to the format string of the object.
@param object A pointer to the allocated object.
@return A pointer to the object's format string.
*/
char* md_get_format_string(void* object);
void md_set_format_string(void* object, char* format_string);
/**
@brief Returns the bit vector of the object.
@param object A pointer to the allocated object.
@return The object's bit vector.
*/
char md_get_bit_vector(void* object);
void md_set_bit_vector(void* object, char bit_vector);
/**
@brief Returns the forwarding address of the object.
@param object A pointer to the allocated object.
@return The object's forwarding address.
*/
void* md_get_forwarding_address(void* object);
void md_set_forwarding_address(void* object, void* forward_address);
/**
@brief Returns the copied flag.
@param object A pointer to the allocated object
@return The object's copied flag
*/
bool md_get_copied_flag(void* objec);
void md_set_copied_flag(void* object, bool copied_flag);
/************************************/
/* */
/* Format string */
/* */
/************************************/
/**
@brief Returns a list of all the pointers in an object.
@param object A pointer to the allocated object.
@return A list with all pointers within an object.
Uses the format string to find all pointers in an object and puts
them in a list.
*/
ll_head fs_get_pointers_within_object(void* object);
/**
@brief Gets the size of the object.
@param object A pointer to the object.
@return The size of the object.
*/
size_t fs_get_object_size(void* object);
#endif