/
heap_block.c
87 lines (76 loc) · 2.02 KB
/
heap_block.c
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
//==============
// heap_block.c
//==============
// Copyright 2024, Sven Bieg (svenbieg@web.de)
// http://github.com/svenbieg/Heap
//=======
// Using
//=======
#include "heap_block.h"
//==================
// Con-/Destructors
//==================
void* heap_block_init(heap_handle_t heap, heap_block_info_t const* info)
{
heap_t* heap_ptr=(heap_t*)heap;
assert(info->size%sizeof(size_t)==0);
assert(info->offset>=(size_t)heap+sizeof(heap_t));
assert(info->offset+info->size<=(size_t)heap+heap_ptr->size);
size_t* head_ptr=(size_t*)info->offset;
*head_ptr=info->header;
head_ptr++;
size_t* foot_ptr=(size_t*)(info->offset+info->size);
foot_ptr--;
*foot_ptr=info->header;
return head_ptr;
}
//========
// Access
//========
void heap_block_get_chain(heap_handle_t heap, void* ptr, heap_block_chain_t* info)
{
heap_t* heap_ptr=(heap_t*)heap;
size_t heap_offset=(size_t)heap;
size_t heap_start=heap_offset+sizeof(heap_t);
size_t offset=heap_block_get_offset(ptr);
size_t* head_ptr=(size_t*)offset;
info->current.offset=offset;
info->current.header=*head_ptr;
if(offset>heap_start)
{
size_t* foot_ptr=(size_t*)offset;
foot_ptr--;
info->previous.header=*foot_ptr;
info->previous.offset=offset-info->previous.size;
}
else
{
info->previous.header=0;
info->previous.offset=0;
}
size_t heap_end=heap_offset+heap_ptr->used;
size_t next_offset=offset+info->current.size;
if(next_offset<heap_end)
{
size_t* head_ptr=(size_t*)next_offset;
info->next.offset=next_offset;
info->next.header=*head_ptr;
}
else
{
info->next.header=0;
info->next.offset=0;
}
}
void heap_block_get_info(heap_handle_t heap, void* ptr, heap_block_info_t* info)
{
heap_t* heap_ptr=(heap_t*)heap;
info->offset=heap_block_get_offset(ptr);
assert(info->offset>=(size_t)heap+sizeof(heap_t));
assert(info->offset<(size_t)heap+heap_ptr->used);
size_t* head_ptr=(size_t*)info->offset;
info->header=*head_ptr;
assert(info->size>=3*sizeof(size_t));
assert(info->offset+info->size<=(size_t)heap+heap_ptr->used);
assert(*((size_t*)(info->offset+info->size-sizeof(size_t)))==*head_ptr);
}