-
Notifications
You must be signed in to change notification settings - Fork 6
/
block.c
129 lines (116 loc) · 3.24 KB
/
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
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
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "config.h"
#include "block.h"
#include "page.h"
struct block *block_new(size_t page_size) {
if (!page_size) {
return NULL;
}
struct block *block = malloc(sizeof(*block));
if (!block) {
return NULL;
}
block->page_size = page_size;
block->pages = malloc(sizeof(*block->pages));
block->offsets = malloc(sizeof(*block->offsets));
if (!block->pages || !block->offsets) {
goto error;
}
block->pages[0] = page_alloc(block->page_size);
if (!block->pages[0]) {
goto error;
}
block->offsets[0] = 0;
block->size = 1;
block->count = 1;
return block;
error:
if (block->pages) {
free(block->pages);
}
if (block->offsets) {
free(block->offsets);
}
free(block);
return NULL;
}
void block_free(struct block *block) {
for (size_t i = 0; i < block->count; i++) {
page_free(block->pages[i], block->page_size);
}
free(block->offsets);
free(block->pages);
free(block);
}
__attribute__ ((noinline))
static void *add_page(struct block *block) {
if (block->count == block->size) {
size_t new_size = block->size * 2;
void **pages = realloc(block->pages, sizeof(*pages) * new_size);
if (!pages) {
return NULL;
}
block->pages = pages;
size_t *offsets = realloc(block->offsets, sizeof(*offsets) * new_size);
if (!offsets) {
return NULL;
}
block->offsets = offsets;
block->size = new_size;
}
void *page = page_alloc(block->page_size);
if (!page) {
return NULL;
}
block->pages[block->count] = page;
block->offsets[block->count] = 0;
block->count++;
return page;
}
void *block_alloc(struct block *block, size_t size) {
if (size > block->page_size) {
return NULL;
}
size_t offset = block->offsets[block->count - 1];
void *page;
if (block->page_size - offset < size) {
page = add_page(block);
if (!page) {
return NULL;
}
offset = 0;
} else {
page = block->pages[block->count - 1];
}
block->offsets[block->count - 1] += size;
return (void *)((uintptr_t)page + offset);
}
void block_snapshot(const struct block *block,
struct block_snapshot *snapshot) {
snapshot->count = block->count;
snapshot->offset = block->offsets[block->count - 1];
}
bool block_restore(struct block *block,
const struct block_snapshot *snapshot) {
if (!snapshot->count || snapshot->count > block->count) {
return false;
}
if (snapshot->count == block->count &&
block->offsets[block->count - 1] < snapshot->offset) {
return false;
}
for (size_t i = snapshot->count; i < block->count; i++) {
page_free(block->pages[i], block->page_size);
}
block->count = snapshot->count;
block->offsets[snapshot->count - 1] = snapshot->offset;
return true;
}
size_t block_allocated_bytes(const struct block *block) {
return block->count * block->page_size +
block->size * sizeof(*block->offsets) +
block->size * sizeof(*block->pages) +
sizeof(*block);
}