Skip to content

Commit be5ac98

Browse files
authored
Implement allocator (#12)
* Implement `allocator` * Add SPDX to new file.
1 parent b869ad8 commit be5ac98

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

include/binsparse/array.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include <assert.h>
10+
#include <binsparse/detail/allocator.h>
1011
#include <binsparse/detail/shm_tools.h>
1112
#include <binsparse/types.h>
1213
#include <stdlib.h>
@@ -16,27 +17,26 @@ typedef struct bsp_array_t {
1617
void* data;
1718
size_t size;
1819
bsp_type_t type;
19-
bool shmat_memory;
20-
bsp_shm_t shm;
20+
bsp_allocator_t allocator;
2121
} bsp_array_t;
2222

2323
bsp_array_t bsp_construct_default_array_t() {
2424
bsp_array_t array;
2525
array.data = NULL;
2626
array.size = 0;
27-
array.shmat_memory = false;
27+
array.allocator = bsp_default_allocator;
2828
return array;
2929
}
3030

3131
bsp_array_t bsp_construct_array_t(size_t size, bsp_type_t type) {
3232
size_t byte_size = size * bsp_type_size(type);
3333

3434
bsp_array_t array;
35-
array.data = malloc(byte_size);
35+
array.allocator = bsp_default_allocator;
36+
array.data = array.allocator.malloc(byte_size);
3637
assert(array.data != NULL);
3738
array.size = size;
3839
array.type = type;
39-
array.shmat_memory = false;
4040

4141
return array;
4242
}
@@ -55,6 +55,7 @@ bsp_array_t bsp_complex_array_to_fp(bsp_array_t other) {
5555
bsp_array_t array;
5656
array.data = other.data;
5757
array.size = other.size * 2;
58+
array.allocator = other.allocator;
5859

5960
if (other.type == BSP_COMPLEX_FLOAT32) {
6061
array.type = BSP_FLOAT32;
@@ -71,6 +72,7 @@ bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
7172
bsp_array_t array;
7273
array.data = other.data;
7374
array.size = other.size / 2;
75+
array.allocator = other.allocator;
7476

7577
if (other.type == BSP_FLOAT32) {
7678
array.type = BSP_COMPLEX_FLOAT32;
@@ -82,11 +84,7 @@ bsp_array_t bsp_fp_array_to_complex(bsp_array_t other) {
8284
}
8385

8486
void bsp_destroy_array_t(bsp_array_t array) {
85-
if (array.shmat_memory == false) {
86-
free(array.data);
87-
} else {
88-
bsp_shm_detach(array.data);
89-
}
87+
array.allocator.free(array.data);
9088
}
9189

9290
bool bsp_array_equal(bsp_array_t x, bsp_array_t y) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Binsparse Developers
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdlib.h>
10+
11+
typedef struct bsp_allocator_t {
12+
void* (*malloc)(size_t);
13+
void (*free)(void*);
14+
} bsp_allocator_t;
15+
16+
const static bsp_allocator_t bsp_default_allocator = {.malloc = malloc,
17+
.free = free};

include/binsparse/detail/shm_tools.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <binsparse/detail/allocator.h>
910
#include <stddef.h>
1011
#include <stdio.h>
1112
#include <sys/ipc.h>
@@ -48,3 +49,20 @@ void* bsp_shm_attach(bsp_shm_t shm) {
4849
void bsp_shm_detach(void* data) {
4950
shmdt(data);
5051
}
52+
53+
void* bsp_shm_malloc(size_t size) {
54+
bsp_shm_t shm_id = bsp_shm_new(size);
55+
56+
void* ptr = bsp_shm_attach(shm_id);
57+
58+
bsp_shm_delete(shm_id);
59+
60+
return ptr;
61+
}
62+
63+
void bsp_shm_free(void* ptr) {
64+
bsp_shm_detach(ptr);
65+
}
66+
67+
const static bsp_allocator_t bsp_shm_allocator = {.malloc = bsp_shm_malloc,
68+
.free = bsp_shm_free};

include/binsparse/hdf5_wrapper.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include <binsparse/detail/shm_tools.h>
1717

18+
#if __STDC_VERSION__ >= 201112L
19+
#include <stdatomic.h>
20+
#endif
21+
1822
// Write an array to a dataset / file
1923
// Returns 0 on success, nonzero on error.
2024
int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
@@ -71,6 +75,7 @@ int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
7175
return 0;
7276
}
7377

78+
#if __STDC_VERSION__ >= 201112L
7479
bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
7580
int num_threads) {
7681
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
@@ -97,12 +102,19 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
97102

98103
bsp_type_t type = bsp_get_bsp_type(hdf5_type);
99104

105+
// Array will be written into a POSIX shared memory.
100106
bsp_shm_t array_shm = bsp_shm_new(dims[0] * bsp_type_size(type));
101107
bsp_array_t array;
102108
array.type = type;
103109
array.size = dims[0];
104-
array.shm = array_shm;
105-
array.shmat_memory = true;
110+
array.allocator = bsp_shm_allocator;
111+
112+
bsp_shm_t active_children_shm = bsp_shm_new(sizeof(_Atomic int));
113+
114+
_Atomic int* active_children = bsp_shm_attach(active_children_shm);
115+
bsp_shm_delete(active_children_shm);
116+
117+
*active_children = num_threads - 1;
106118

107119
pid_t* pids = (pid_t*) malloc(sizeof(pid_t) * num_threads);
108120

@@ -121,7 +133,7 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
121133

122134
array.data = bsp_shm_attach(array_shm);
123135
if (thread_num == 0) {
124-
bsp_shm_delete(array.shm);
136+
bsp_shm_delete(array_shm);
125137
}
126138

127139
hsize_t chunk_size = (array.size + num_threads - 1) / num_threads;
@@ -137,22 +149,29 @@ bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
137149
hid_t memspace_id = H5Screate_simple(1, &count, NULL);
138150

139151
H5Dread(dset, bsp_get_hdf5_native_type(type), memspace_id, fspace,
140-
H5P_DEFAULT, array.data + start * bsp_type_size(type));
152+
H5P_DEFAULT, ((char*) array.data) + start * bsp_type_size(type));
141153
H5Sclose(memspace_id);
142154
}
143155

144156
H5Dclose(dset);
145157
H5Sclose(fspace);
146158

147159
if (thread_num > 0) {
160+
atomic_fetch_add_explicit(active_children, -1, memory_order_relaxed);
161+
bsp_shm_detach(active_children);
148162
bsp_shm_detach(array.data);
149163
exit(0);
150164
}
151165

152166
free(pids);
153167

168+
while (atomic_load(active_children) > 0) {
169+
}
170+
bsp_shm_detach(active_children);
171+
154172
return array;
155173
}
174+
#endif
156175

157176
bsp_array_t bsp_read_array(hid_t f, const char* label) {
158177
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);

include/binsparse/read_matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <binsparse/matrix.h>
1111
#include <cJSON/cJSON.h>
1212

13+
#if __STDC_VERSION__ >= 201112L
1314
bsp_matrix_t bsp_read_matrix_from_group_parallel(hid_t f, int num_threads) {
1415
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
1516

@@ -113,6 +114,7 @@ bsp_matrix_t bsp_read_matrix_from_group_parallel(hid_t f, int num_threads) {
113114

114115
return matrix;
115116
}
117+
#endif
116118

117119
bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
118120
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
@@ -225,6 +227,7 @@ size_t bsp_final_dot(const char* str) {
225227
return dot_idx;
226228
}
227229

230+
#if __STDC_VERSION__ >= 201112L
228231
bsp_matrix_t bsp_read_matrix_parallel(const char* file_name, const char* group,
229232
int num_threads) {
230233
if (group == NULL) {
@@ -249,6 +252,7 @@ bsp_matrix_t bsp_read_matrix_parallel(const char* file_name, const char* group,
249252
return matrix;
250253
}
251254
}
255+
#endif
252256

253257
bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group) {
254258
if (group == NULL) {

0 commit comments

Comments
 (0)