Skip to content

Commit 3ccb7ef

Browse files
create an alloc api
1 parent 4e119ec commit 3ccb7ef

File tree

7 files changed

+77
-95
lines changed

7 files changed

+77
-95
lines changed

makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
CC=gcc
2+
CFLAGS=-Wall -We -g -pedantic -std=c99
23

3-
all: build-relese
4+
all: build-release
45

56
build-debug:
67
@echo "Building debug..."
7-
@gcc -g -D DEBUG -pedantic -std=c99 src/forest_test.c -lcrypto -o main
8+
@gcc -g -D DEBUG $(CFLAGS) src/forest_test.c -lcrypto -o main
89

910
build-release:
1011
@echo "Building release..."
11-
@$(MAKE) -f makefile.release
12+
@$(CC) src/mmap_forest.c -c -o libforest.o
13+
@ar rcs libforest.a libforest.o
14+
@$(CC) src/mmap_forest.c -o libforest.so -fPIC -rdynamic -shared
15+
1216
tests:
1317
@echo "Building tests..."
14-
@gcc -g -pedantic -std=c99 src/forest_test.c -lcrypto -o forest_tests
15-
@gcc -g -pedantic -std=c99 src/test_flat_file.c -lcrypto -o flat_file_tests
18+
@gcc $(CFLAGS) src/forest_test.c -lcrypto -o forest_tests
19+
@gcc $(CFLAGS) src/test_flat_file.c -lcrypto -o flat_file_tests
1620

1721
test: tests
1822
@./flat_file_tests

src/flat_file.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,9 @@ static inline void utreexo_forest_file_close(struct utreexo_forest_file *file);
8686
static inline void utreexo_forest_file_init(struct utreexo_forest_file **file,
8787
const char *filename);
8888

89-
/* Puts a new node in the file, returns the offset for future referencing */
90-
static inline void
91-
utreexo_forest_file_node_put(struct utreexo_forest_file *file,
92-
utreexo_forest_node **_ptr,
93-
utreexo_forest_node node);
89+
/* Allocs a new node and returns a pointer to it */
90+
static inline utreexo_forest_node *
91+
utreexo_forest_file_node_alloc(struct utreexo_forest_file *file);
9492

9593
/* Initialize a new page */
9694
static inline void utreexo_forest_mkpg(struct utreexo_forest_file *file,

src/flat_file_impl.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ static inline void utreexo_forest_mkpg(struct utreexo_forest_file *file,
116116
DEBUG_ASSERT(pg->pg_magic == MAGIC)
117117
}
118118

119-
static inline void
120-
utreexo_forest_file_node_put(struct utreexo_forest_file *file,
121-
utreexo_forest_node **_ptr,
122-
utreexo_forest_node node) {
119+
static inline utreexo_forest_node *
120+
utreexo_forest_file_node_alloc(struct utreexo_forest_file *file) {
123121
uint64_t page_nodes = file->wrt_page->n_nodes;
124122
if (page_nodes == NODES_PER_PAGE) {
125123
DEBUG_PRINT("Page is full, allocating new page\n");
@@ -129,41 +127,39 @@ utreexo_forest_file_node_put(struct utreexo_forest_file *file,
129127
}
130128
page_nodes = 0; // we've just created a new page
131129
}
132-
const uint64_t page_offset = page_nodes;
133130
DEBUG_PRINT("Writing node %d to page %d offset=%d\n", page_nodes,
134-
file->n_pages - 1, page_offset);
131+
file->n_pages - 1, page_nodes / sizeof(utreexo_forest_node));
135132
utreexo_forest_node *ptr =
136133
(utreexo_forest_node *)((char *)file->wrt_page + 16) + page_nodes;
137-
memcpy(ptr, &node, sizeof(node));
138134
++(file->wrt_page->n_nodes);
139-
*_ptr = ptr;
135+
return ptr;
140136
}
141137

142138
static inline void
143139
utreexo_forest_file_node_del(struct utreexo_forest_file *file,
144140
const utreexo_forest_node *node) {
145-
uint64_t nPage = ((uint64_t)((char *)node - file->map)) / PAGE_SIZE;
141+
uint64_t npage = ((uint64_t)((char *)node - file->map)) / PAGE_SIZE;
146142

147143
struct utreexo_forest_page_header *pg =
148-
(struct utreexo_forest_page_header *)PAGE(file->map, nPage);
144+
(struct utreexo_forest_page_header *)PAGE(file->map, npage);
149145

150-
DEBUG_PRINT("Deleting node from page %d remaining: %u\n", nPage, pg->n_nodes);
146+
DEBUG_PRINT("Deleting node from page %d remaining: %u\n", npage, pg->n_nodes);
151147
DEBUG_ASSERT(pg->n_nodes != 0);
152-
DEBUG_ASSERT(file->n_pages >= nPage);
148+
DEBUG_ASSERT(file->n_pages >= npage);
153149

154150
if (--pg->n_nodes == 0) {
155-
DEBUG_PRINT("Deallocating page %d\n", nPage);
151+
DEBUG_PRINT("Deallocating page %d\n", npage);
156152
utreexo_forest_free_page *pg = file->fpg;
157153
// This is the first free page
158154
if (pg == NULL) {
159-
file->fpg = (utreexo_forest_free_page *)PAGE(file->map, nPage);
155+
file->fpg = (utreexo_forest_free_page *)PAGE(file->map, npage);
160156
return;
161157
}
162158
// Walk the list until find the last element
163159
while (pg->next != NULL)
164160
pg = (utreexo_forest_free_page *)pg->next;
165161

166-
pg->next = (utreexo_forest_free_page *)PAGE(file->map, nPage);
162+
pg->next = (utreexo_forest_free_page *)PAGE(file->map, npage);
167163
}
168164
}
169165
#endif

src/forest_test.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,18 @@ void test_grab_node() {
247247

248248
utreexo_forest_node *node = NULL, *sibling = NULL, *parent = NULL;
249249
grab_node(&p, &node, &sibling, &parent, 4);
250-
utreexo_forest_print(p.roots[1]);
251250
for (size_t i = 0; i < 32; ++i)
252251
printf("%02x", node->hash.hash[i]);
253252

254253
printf("\n");
255254
}
256255

257256
int main() {
258-
// test_parent_hash();
259-
// test_add_single();
260-
// test_add_two();
261-
// test_add_many();
262-
// test_from_test_cases();
263-
test_grab_node();
257+
test_parent_hash();
258+
test_add_single();
259+
test_add_two();
260+
test_add_many();
261+
test_from_test_cases();
262+
// test_grab_node();
264263
return 0;
265264
}

src/map_forest_impl.h

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
static const char UTREEXO_ZERO_HASH[32] = {0};
2121
static inline void utreexo_forest_add(struct utreexo_forest *p,
2222
utreexo_node_hash leaf) {
23-
utreexo_forest_node node = {
24-
.hash = {0}, .parent = NULL, .left_child = NULL, .right_child = NULL};
25-
memcpy(node.hash.hash, leaf.hash, 32);
23+
utreexo_forest_node *pnode = utreexo_forest_file_node_alloc(p->data);
24+
*pnode = (utreexo_forest_node){
25+
.hash = {{0}}, .parent = NULL, .left_child = NULL, .right_child = NULL};
26+
memcpy(pnode->hash.hash, leaf.hash, 32);
27+
2628
const uint64_t nLeaves = p->nLeaf;
2729
uint8_t height = 0;
28-
utreexo_forest_node *pnode = NULL;
29-
utreexo_forest_file_node_put(p->data, &pnode, node);
3030

3131
while ((nLeaves >> height & 1) == 1) {
3232
utreexo_forest_node *root = p->roots[height];
@@ -36,18 +36,16 @@ static inline void utreexo_forest_add(struct utreexo_forest *p,
3636
break;
3737
}
3838

39-
utreexo_forest_node new_root = {
39+
utreexo_forest_node *proot = utreexo_forest_file_node_alloc(p->data);
40+
*proot = (utreexo_forest_node){
4041
.parent = NULL, .left_child = root, .right_child = pnode};
4142

42-
parent_hash(new_root.hash.hash, root->hash.hash, pnode->hash.hash);
43-
44-
utreexo_forest_node *new_root_pos = NULL;
45-
utreexo_forest_file_node_put(p->data, &new_root_pos, new_root);
43+
parent_hash(proot->hash.hash, root->hash.hash, pnode->hash.hash);
4644

47-
pnode->parent = new_root_pos;
48-
root->parent = new_root_pos;
45+
pnode->parent = proot;
46+
root->parent = proot;
4947

50-
pnode = new_root_pos;
48+
pnode = proot;
5149
height++;
5250
}
5351
DEBUG_ASSERT(p->roots[height] == NULL);
@@ -87,26 +85,14 @@ static inline void grab_node(struct utreexo_forest *f,
8785
*parent = pparent;
8886
}
8987

90-
void utreexo_forest_print(utreexo_forest_node *f) {
91-
utreexo_forest_node **nodes;
92-
*nodes = (utreexo_forest_node *)malloc(1024);
93-
nodes[0] = f;
94-
95-
utreexo_forest_node *next[1000] = {0};
96-
size_t i = 0;
97-
do {
98-
utreexo_forest_node *pnode = nodes[i];
99-
if (pnode == NULL) {
100-
printf("=====", pnode->hash.hash[0], pnode->hash.hash[1]);
101-
next[i * 2] = NULL;
102-
next[i * 2 + 1] = NULL;
103-
}
104-
printf("%02x%02x", pnode->hash.hash[0], pnode->hash.hash[1]);
88+
static inline void utreexo_forest_remove(struct utreexo_forest *p,
89+
uint64_t leaf_number) {}
10590

106-
next[i * 2] = pnode->left_child;
107-
next[i * 2 + 1] = pnode->right_child;
108-
} while (21);
91+
static inline void utreexo_forest_free(struct utreexo_forest *forest) {
92+
utreexo_forest_file_close(forest->data);
93+
free(forest);
10994
}
95+
11096
static inline void delete_single(struct utreexo_forest *f, uint64_t pos) {
11197
utreexo_forest_node *pnode, *psibling, *pparent;
11298

src/mmap_forest.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ static const int utreexo_forest_version_major = 0;
1111
#include "mmap_forest.h"
1212
#include "util.h"
1313

14+
#define CHECK_PTR(x) \
15+
if (x == NULL) { \
16+
errno = -1; \
17+
return; \
18+
}
19+
1420
void utreexo_forest_modify(struct utreexo_forest *forest,
1521
utreexo_node_hash *leaf, int leaf_count) {
1622
for (int i = 0; i < leaf_count; i++) {
@@ -19,18 +25,15 @@ void utreexo_forest_modify(struct utreexo_forest *forest,
1925
}
2026

2127
void utreexo_forest_init(struct utreexo_forest **p, const char *filename) {
28+
2229
struct utreexo_forest *forest = malloc(sizeof(struct utreexo_forest));
2330
struct utreexo_forest_file *file = NULL;
24-
void *roots = NULL;
25-
utreexo_forest_file_init(&file, filename, &roots);
31+
32+
utreexo_forest_file_init(&file, filename);
33+
2634
forest->data = file;
2735
forest->nLeaf = 0;
28-
(*p)->roots = roots;
36+
2937
DEBUG_ASSERT(*(uint32_t *)(*file).map == MAGIC);
3038
*p = forest;
3139
}
32-
33-
void utreexo_forest_free(struct utreexo_forest *forest) {
34-
utreexo_forest_file_close(forest->data);
35-
free(forest);
36-
}

src/test_flat_file.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,28 @@ utreexo_forest_node *test_create_nodes(struct utreexo_forest_file *file) {
5656
utreexo_forest_node *left_child_pos;
5757
utreexo_forest_node *parent_pos;
5858

59-
utreexo_forest_node right_child = {
60-
.hash = {0x00, 0x01, 0x02, 0x05},
59+
right_child_pos = utreexo_forest_file_node_alloc(file);
60+
*right_child_pos = (utreexo_forest_node){
61+
.hash = {{0x00, 0x01, 0x02, 0x05}},
6162
.left_child = 0,
6263
.right_child = 0,
6364
.parent = 0,
6465
};
65-
utreexo_forest_file_node_put(file, &right_child_pos, right_child);
66-
utreexo_forest_node left_child = {
67-
.hash = {0x00, 0x01, 0x02, 0x04},
66+
67+
left_child_pos = utreexo_forest_file_node_alloc(file);
68+
*left_child_pos = (utreexo_forest_node){
69+
.hash = {{0x00, 0x01, 0x02, 0x04}},
6870
.left_child = 0,
6971
.right_child = 0,
7072
.parent = 0,
7173
};
72-
73-
utreexo_forest_file_node_put(file, &left_child_pos, left_child);
74-
utreexo_forest_node parent = {
75-
.hash = {0x00, 0x01, 0x02, 0x03},
74+
parent_pos = utreexo_forest_file_node_alloc(file);
75+
*parent_pos = (utreexo_forest_node){
76+
.hash = {{0x00, 0x01, 0x02, 0x03}},
7677
.parent = 0,
7778
.left_child = left_child_pos,
7879
.right_child = right_child_pos,
7980
};
80-
utreexo_forest_file_node_put(file, &parent_pos, parent);
8181
TEST_END;
8282

8383
return parent_pos;
@@ -113,17 +113,11 @@ void test_add_many(int n_adds) {
113113

114114
struct utreexo_forest_file *file;
115115
utreexo_forest_file_init(&file, "add_many.bin");
116-
utreexo_forest_node *node_pos;
117-
utreexo_forest_node node = {
118-
.hash = {0x00},
119-
.left_child = NULL,
120-
.right_child = NULL,
121-
.parent = (void *)0xffffffff,
122-
};
123-
memset(node.hash.hash, 0xff, 32);
116+
124117
for (int i = 0; i < n_adds; i++) {
125-
utreexo_forest_file_node_put(file, &node_pos, node);
118+
utreexo_forest_file_node_alloc(file);
126119
}
120+
127121
utreexo_forest_file_close(file);
128122
TEST_END;
129123
}
@@ -132,23 +126,25 @@ void test_free_page_list() {
132126
TEST_BEGIN("test free page reallocation");
133127
struct utreexo_forest_file *file;
134128
utreexo_forest_file_init(&file, "reallocation.bin");
135-
utreexo_forest_node *nodes[NODES_PER_PAGE];
129+
utreexo_forest_node *nodes[NODES_PER_PAGE] = {0};
136130
const utreexo_forest_node node = {
137-
.hash = {0},
131+
.hash = {{0}},
138132
.parent = NULL,
139133
.left_child = NULL,
140134
.right_child = NULL,
141135
};
142136

143137
// Fills up a page
144138
for (int i = 0; i < NODES_PER_PAGE; ++i) {
145-
utreexo_forest_file_node_put(file, &nodes[i], node);
139+
nodes[i] = utreexo_forest_file_node_alloc(file);
140+
memcpy(nodes[i], &node, sizeof(utreexo_forest_node));
146141
}
147142
utreexo_forest_node *pnode = NULL;
148143

149144
// triggers the allocation of a new page
150145
for (int i = 0; i < NODES_PER_PAGE; ++i) {
151-
utreexo_forest_file_node_put(file, &pnode, node);
146+
pnode = utreexo_forest_file_node_alloc(file);
147+
memcpy(pnode, &node, sizeof(utreexo_forest_node));
152148
}
153149

154150
// remove all nodes from the first page
@@ -157,7 +153,7 @@ void test_free_page_list() {
157153
}
158154

159155
// Add one node
160-
utreexo_forest_file_node_put(file, &pnode, node);
156+
pnode = utreexo_forest_file_node_alloc(file);
161157

162158
// It should be where nodes[0] was (the beggining of the first page)
163159
ASSERT_EQ(pnode, nodes[0]);

0 commit comments

Comments
 (0)