Skip to content

Commit 13a5471

Browse files
committed
Add option to use a specific allocator.
1 parent 8405a28 commit 13a5471

File tree

5 files changed

+83
-30
lines changed

5 files changed

+83
-30
lines changed

include/binsparse/array.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ typedef struct bsp_array_t {
2020
bsp_allocator_t allocator;
2121
} bsp_array_t;
2222

23-
static inline bsp_error_t bsp_construct_default_array_t(bsp_array_t* array) {
23+
static inline bsp_error_t
24+
bsp_construct_default_array_t_allocator(bsp_array_t* array,
25+
bsp_allocator_t allocator) {
2426
array->data = NULL;
2527
array->size = 0;
26-
array->allocator = bsp_default_allocator;
28+
array->allocator = allocator;
2729
return BSP_SUCCESS;
2830
}
2931

30-
static inline bsp_error_t bsp_construct_array_t(bsp_array_t* array, size_t size,
31-
bsp_type_t type) {
32+
static inline bsp_error_t bsp_construct_default_array_t(bsp_array_t* array) {
33+
return bsp_construct_default_array_t_allocator(array, bsp_default_allocator);
34+
}
35+
36+
static inline bsp_error_t
37+
bsp_construct_array_t_allocator(bsp_array_t* array, size_t size,
38+
bsp_type_t type, bsp_allocator_t allocator) {
3239
size_t byte_size = size * bsp_type_size(type);
3340

34-
array->allocator = bsp_default_allocator;
41+
array->allocator = allocator;
3542
array->data = array->allocator.malloc(byte_size);
3643

3744
if (array->data == NULL) {
@@ -44,6 +51,12 @@ static inline bsp_error_t bsp_construct_array_t(bsp_array_t* array, size_t size,
4451
return BSP_SUCCESS;
4552
}
4653

54+
static inline bsp_error_t bsp_construct_array_t(bsp_array_t* array, size_t size,
55+
bsp_type_t type) {
56+
return bsp_construct_array_t_allocator(array, size, type,
57+
bsp_default_allocator);
58+
}
59+
4760
static inline bsp_error_t bsp_copy_construct_array_t(bsp_array_t* array,
4861
bsp_array_t other) {
4962
bsp_error_t error = bsp_construct_array_t(array, other.size, other.type);

include/binsparse/hdf5_wrapper.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,13 @@ static inline bsp_error_t bsp_read_array_parallel(bsp_array_t* array, hid_t f,
193193
}
194194
#endif
195195

196-
static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
197-
const char* label) {
196+
static inline bsp_error_t bsp_read_array_allocator(bsp_array_t* array, hid_t f,
197+
const char* label,
198+
bsp_allocator_t allocator) {
198199
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
199200

200201
if (dset == H5I_INVALID_HID) {
201-
bsp_construct_default_array_t(array);
202+
bsp_construct_default_array_t_allocator(array, allocator);
202203
return BSP_ERROR_IO;
203204
}
204205

@@ -207,7 +208,7 @@ static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
207208
if (fspace == H5I_INVALID_HID) {
208209
H5Sclose(fspace);
209210
H5Dclose(dset);
210-
bsp_construct_default_array_t(array);
211+
bsp_construct_default_array_t_allocator(array, allocator);
211212
return BSP_ERROR_IO;
212213
}
213214

@@ -218,19 +219,20 @@ static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
218219
if (r < 0) {
219220
H5Dclose(dset);
220221
H5Sclose(fspace);
221-
bsp_construct_default_array_t(array);
222+
bsp_construct_default_array_t_allocator(array, allocator);
222223
return BSP_ERROR_IO;
223224
}
224225

225226
hid_t hdf5_type = H5Dget_type(dset);
226227

227228
bsp_type_t type = bsp_get_bsp_type(hdf5_type);
228229

229-
bsp_error_t error = bsp_construct_array_t(array, dims[0], type);
230+
bsp_error_t error =
231+
bsp_construct_array_t_allocator(array, dims[0], type, allocator);
230232
if (error != BSP_SUCCESS) {
231233
H5Dclose(dset);
232234
H5Sclose(fspace);
233-
bsp_construct_default_array_t(array);
235+
bsp_construct_default_array_t_allocator(array, allocator);
234236
return BSP_ERROR_MEMORY;
235237
}
236238

@@ -241,7 +243,7 @@ static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
241243
bsp_destroy_array_t(array);
242244
H5Sclose(fspace);
243245
H5Dclose(dset);
244-
bsp_construct_default_array_t(array);
246+
bsp_construct_default_array_t_allocator(array, allocator);
245247
return BSP_ERROR_IO;
246248
}
247249

@@ -250,6 +252,11 @@ static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
250252
return BSP_SUCCESS;
251253
}
252254

255+
static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
256+
const char* label) {
257+
return bsp_read_array_allocator(array, f, label, bsp_default_allocator);
258+
}
259+
253260
static inline bsp_error_t bsp_write_attribute(hid_t f, const char* label,
254261
const char* string) {
255262
hid_t strtype = H5Tcopy(H5T_C_S1);
@@ -269,8 +276,9 @@ static inline bsp_error_t bsp_write_attribute(hid_t f, const char* label,
269276
return BSP_SUCCESS;
270277
}
271278

272-
static inline bsp_error_t bsp_read_attribute(char** string, hid_t f,
273-
const char* label) {
279+
static inline bsp_error_t
280+
bsp_read_attribute_allocator(char** string, hid_t f, const char* label,
281+
bsp_allocator_t allocator) {
274282
hid_t attribute = H5Aopen(f, label, H5P_DEFAULT);
275283

276284
if (attribute == H5I_INVALID_HID) {
@@ -281,7 +289,7 @@ static inline bsp_error_t bsp_read_attribute(char** string, hid_t f,
281289

282290
size_t size = H5Tget_size(strtype);
283291

284-
*string = (char*) malloc(size + 1);
292+
*string = (char*) allocator.malloc(size + 1);
285293

286294
H5Aread(attribute, strtype, *string);
287295

@@ -290,3 +298,8 @@ static inline bsp_error_t bsp_read_attribute(char** string, hid_t f,
290298

291299
return BSP_SUCCESS;
292300
}
301+
302+
static inline bsp_error_t bsp_read_attribute(char** string, hid_t f,
303+
const char* label) {
304+
return bsp_read_attribute_allocator(string, f, label, bsp_default_allocator);
305+
}

include/binsparse/matrix.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,22 @@ typedef struct bsp_matrix_t {
2727
bsp_structure_t structure;
2828
} bsp_matrix_t;
2929

30-
static inline void bsp_construct_default_matrix_t(bsp_matrix_t* matrix) {
30+
static inline void
31+
bsp_construct_default_matrix_t_allocator(bsp_matrix_t* matrix,
32+
bsp_allocator_t allocator) {
3133
bsp_construct_default_array_t(&matrix->values);
32-
bsp_construct_default_array_t(&matrix->indices_0);
33-
bsp_construct_default_array_t(&matrix->indices_1);
34-
bsp_construct_default_array_t(&matrix->pointers_to_1);
34+
bsp_construct_default_array_t_allocator(&matrix->indices_0, allocator);
35+
bsp_construct_default_array_t_allocator(&matrix->indices_1, allocator);
36+
bsp_construct_default_array_t_allocator(&matrix->pointers_to_1, allocator);
3537
matrix->nrows = matrix->ncols = matrix->nnz = 0;
3638
matrix->is_iso = false;
3739
matrix->structure = BSP_GENERAL;
3840
}
3941

42+
static inline void bsp_construct_default_matrix_t(bsp_matrix_t* matrix) {
43+
bsp_construct_default_matrix_t_allocator(matrix, bsp_default_allocator);
44+
}
45+
4046
static inline void bsp_destroy_matrix_t(bsp_matrix_t* matrix) {
4147
bsp_destroy_array_t(&matrix->values);
4248
bsp_destroy_array_t(&matrix->indices_0);

include/binsparse/read_matrix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ extern "C" {
1111
#endif
1212

1313
#ifdef BSP_USE_HDF5
14+
#include <binsparse/detail/allocator.h>
1415
#include <hdf5.h>
1516

1617
#if __STDC_VERSION__ >= 201112L
1718
bsp_matrix_t bsp_read_matrix_from_group_parallel(hid_t f, int num_threads);
1819
#endif
1920

2021
bsp_matrix_t bsp_read_matrix_from_group(hid_t f);
22+
bsp_matrix_t bsp_read_matrix_from_group_allocator(hid_t f,
23+
bsp_allocator_t allocator);
2124
#endif
2225

2326
#if __STDC_VERSION__ >= 201112L
@@ -26,6 +29,8 @@ bsp_matrix_t bsp_read_matrix_parallel(const char* file_name, const char* group,
2629
#endif
2730

2831
bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group);
32+
bsp_matrix_t bsp_read_matrix_allocator(const char* file_name, const char* group,
33+
bsp_allocator_t allocator);
2934

3035
#ifdef __cplusplus
3136
}

src/read_matrix.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <assert.h>
8+
#include <binsparse/detail/allocator.h>
89
#include <binsparse/hdf5_wrapper.h>
910
#include <binsparse/matrix.h>
1011
#include <binsparse/matrix_market/matrix_market_read.h>
@@ -147,12 +148,14 @@ bsp_matrix_t bsp_read_matrix_from_group_parallel(hid_t f, int num_threads) {
147148
}
148149
#endif
149150

150-
bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
151+
bsp_matrix_t bsp_read_matrix_from_group_allocator(hid_t f,
152+
bsp_allocator_t allocator) {
151153
bsp_matrix_t matrix;
152-
bsp_construct_default_matrix_t(&matrix);
154+
bsp_construct_default_matrix_t_allocator(&matrix, allocator);
153155

154156
char* json_string;
155-
bsp_error_t error = bsp_read_attribute(&json_string, f, (char*) "binsparse");
157+
bsp_error_t error = bsp_read_attribute_allocator(
158+
&json_string, f, (char*) "binsparse", allocator);
156159
if (error != BSP_SUCCESS) {
157160
return matrix;
158161
}
@@ -213,7 +216,8 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
213216
assert(data_types_ != NULL);
214217

215218
if (cJSON_HasObjectItem(data_types_, "values")) {
216-
error = bsp_read_array(&matrix.values, f, (char*) "values");
219+
error = bsp_read_array_allocator(&matrix.values, f, (char*) "values",
220+
allocator);
217221
if (error != BSP_SUCCESS) {
218222
free(json_string);
219223
return matrix;
@@ -236,7 +240,8 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
236240
}
237241

238242
if (cJSON_HasObjectItem(data_types_, "indices_0")) {
239-
error = bsp_read_array(&matrix.indices_0, f, (char*) "indices_0");
243+
error = bsp_read_array_allocator(&matrix.indices_0, f, (char*) "indices_0",
244+
allocator);
240245
if (error != BSP_SUCCESS) {
241246
free(json_string);
242247
bsp_destroy_array_t(&matrix.values);
@@ -245,7 +250,8 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
245250
}
246251

247252
if (cJSON_HasObjectItem(data_types_, "indices_1")) {
248-
error = bsp_read_array(&matrix.indices_1, f, (char*) "indices_1");
253+
error = bsp_read_array_allocator(&matrix.indices_1, f, (char*) "indices_1",
254+
allocator);
249255
if (error != BSP_SUCCESS) {
250256
free(json_string);
251257
bsp_destroy_array_t(&matrix.values);
@@ -255,7 +261,8 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
255261
}
256262

257263
if (cJSON_HasObjectItem(data_types_, "pointers_to_1")) {
258-
error = bsp_read_array(&matrix.pointers_to_1, f, (char*) "pointers_to_1");
264+
error = bsp_read_array_allocator(&matrix.pointers_to_1, f,
265+
(char*) "pointers_to_1", allocator);
259266
if (error != BSP_SUCCESS) {
260267
free(json_string);
261268
bsp_destroy_array_t(&matrix.values);
@@ -278,6 +285,10 @@ bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
278285
return matrix;
279286
}
280287

288+
bsp_matrix_t bsp_read_matrix_from_group(hid_t f) {
289+
return bsp_read_matrix_from_group_allocator(f, bsp_default_allocator);
290+
}
291+
281292
static inline size_t bsp_final_dot(const char* str) {
282293
size_t dot_idx = 0;
283294
for (size_t i = 0; str[i] != '\0'; i++) {
@@ -315,13 +326,14 @@ bsp_matrix_t bsp_read_matrix_parallel(const char* file_name, const char* group,
315326
}
316327
#endif
317328

318-
bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group) {
329+
bsp_matrix_t bsp_read_matrix_allocator(const char* file_name, const char* group,
330+
bsp_allocator_t allocator) {
319331
if (group == NULL) {
320332
size_t idx = bsp_final_dot(file_name);
321333
if (strcmp(file_name + idx, ".hdf5") == 0 ||
322334
strcmp(file_name + idx, ".h5") == 0) {
323335
hid_t f = H5Fopen(file_name, H5F_ACC_RDONLY, H5P_DEFAULT);
324-
bsp_matrix_t matrix = bsp_read_matrix_from_group(f);
336+
bsp_matrix_t matrix = bsp_read_matrix_from_group_allocator(f, allocator);
325337
H5Fclose(f);
326338
return matrix;
327339
} else if (strcmp(file_name + idx, ".mtx") == 0) {
@@ -332,9 +344,13 @@ bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group) {
332344
} else {
333345
hid_t f = H5Fopen(file_name, H5F_ACC_RDONLY, H5P_DEFAULT);
334346
hid_t g = H5Gopen1(f, group);
335-
bsp_matrix_t matrix = bsp_read_matrix_from_group(g);
347+
bsp_matrix_t matrix = bsp_read_matrix_from_group_allocator(g, allocator);
336348
H5Gclose(g);
337349
H5Fclose(f);
338350
return matrix;
339351
}
340352
}
353+
354+
bsp_matrix_t bsp_read_matrix(const char* file_name, const char* group) {
355+
return bsp_read_matrix_allocator(file_name, group, bsp_default_allocator);
356+
}

0 commit comments

Comments
 (0)