17#include "exceptions.h"
27 void* heap_start, * heap_end;
37 void* previous, * next;
50 return structure >
header->heap_start && structure <
header->heap_end;
65u_dword
size(
void* structure) {
70 if (!
header->first_address)
return 0.0;
74 void* current_address =
header->first_address;
75 while (current_address) {
77 sum += current_block->size;
79 current_address = current_block->next;
101 if (previous)
get_header(previous)->next = memory_address;
102 if (next)
get_header(next)->previous = memory_address;
103 return memory_address;
118 memory_clear(start_address,
sizeof(
heap_header), 0);
120 header->heap_end = start_address + initial_size;
121 header->first_address =
nullptr;
126 if (!
header->first_address) {
128 return header->first_address;
131 void* best_address =
nullptr, * best_previous =
nullptr, * best_next =
nullptr;
132 u_dword best_size =
header->heap_end -
header->heap_start;
134 void* current_address =
header->heap_start;
135 void* next_address =
header->first_address;
137 u_dword free_space_size = next_address - current_address -
sizeof(
heap_block_header);
139 best_size = free_space_size;
140 best_address = current_address; best_previous =
nullptr; best_next = next_address;
143 current_address = next_address;
145 next_address = current_block->next;
147 while (next_address) {
148 free_space_size = next_address - current_address - current_block->size -
sizeof(
heap_block_header);
150 best_size = free_space_size;
151 best_address = current_address + current_block->size; best_previous = current_address; best_next = next_address;
154 current_address = next_address;
156 next_address = current_block->next;
161 best_address = current_address + current_block->size; best_previous = current_address; best_next =
nullptr;
164 void* final_address =
nullptr;
165 if (best_address !=
nullptr) final_address =
allocate_space(best_address,
size, best_previous, best_next);
166 if (best_address ==
header->heap_start)
header->first_address = final_address;
168 return final_address;
173 memory_clear((
byte*) result,
size, 0);
177void*
challoc(
void* structure, u_dword new_size) {
179 if (new_size < block->
size) {
180 block->size = new_size;
181 }
else if (new_size > block->size) {
182 if (new_size < block->next - structure) block->size = new_size;
184 void* new_structure =
ralloc(new_size);
185 memory_copy(structure, new_structure, block->size);
187 structure = new_structure;
195 if (structure ==
header->first_address)
header->first_address = block->next;
196 if (block->next)
get_header(block->next)->previous = block->previous;
197 if (block->previous)
get_header(block->previous)->next = block->next;
This is a standard MushLib heap header. See standard implementation in lib/base/heap....
#define heap_exception_id
#define allocation_exception_id
#define allocation_exception_type
#define heap_exception_type
void * ralloc(u_dword size)
void unalloc(void *structure)
u_dword size(void *structure)
void * zalloc(u_dword size)
void * challoc(void *structure, u_dword new_size)
static void handle_allocation_exception()
void initialize_heap(void *start_address, u_dword initial_size)
static heap_block_header * get_header(void *structure)
static void * allocate_space(void *free_pointer, u_dword size, heap_block_header *previous, heap_block_header *next)
boolean is_in_heap(void *structure)