Skip to content

Commit ab7b481

Browse files
committed
Add allocator wrapper for c-style allocation
1 parent 5cec281 commit ab7b481

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

examples/c_style.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <binsparse/binsparse.hpp>
2+
#include <iostream>
3+
4+
int main(int argc, char** argv) {
5+
6+
binsparse::allocator_wrapper<float> alloc(malloc, free);
7+
8+
auto mat = binsparse::read_coo_matrix<float, std::size_t>("data/matrix.hdf5", alloc);
9+
10+
for (size_t i = 0; i < mat.nnz; i++) {
11+
std::cout << mat.rowind[i] << ", " << mat.colind[i] << ": " << mat.values[i] << std::endl;
12+
}
13+
14+
// binsparse::write_coo_matrix("new_matrix.hdf5", mat);
15+
16+
return 0;
17+
}

include/binsparse/binsparse.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <binsparse/containers/matrices.hpp>
66
#include "hdf5_tools.hpp"
77
#include <memory>
8+
#include <type_traits>
9+
10+
#include <binsparse/c_bindings/allocator_wrapper.hpp>
811

912
namespace binsparse {
1013

@@ -50,7 +53,7 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
5053
auto ncols = data["shape"][1];
5154
auto nnz = data["nnz"];
5255

53-
typename std::allocator_traits<Allocator>:: template rebind_alloc<I> i_alloc(alloc);
56+
typename std::allocator_traits<std::remove_cvref_t<Allocator>>:: template rebind_alloc<I> i_alloc(alloc);
5457

5558
auto values = hdf5_tools::read_dataset<T>(f, "values", alloc);
5659
auto rows = hdf5_tools::read_dataset<I>(f, "indices_0", i_alloc);
@@ -62,7 +65,7 @@ coo_matrix<T, I> read_coo_matrix(std::string fname, Allocator&& alloc) {
6265
}
6366
}
6467

65-
template <typename T, typename I = std::size_t>
68+
template <typename T, typename I>
6669
coo_matrix<T, I> read_coo_matrix(std::string fname) {
6770
return read_coo_matrix<T, I>(fname, std::allocator<T>{});
6871
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
namespace binsparse {
4+
5+
template <typename T>
6+
class allocator_wrapper {
7+
public:
8+
using value_type = T;
9+
using pointer = T*;
10+
using const_pointer = const T*;
11+
using reference = T&;
12+
using const_reference = const T&;
13+
using size_type = std::size_t;
14+
using difference_type = std::ptrdiff_t;
15+
16+
template <typename U>
17+
allocator_wrapper(const allocator_wrapper<U> &other) noexcept
18+
: malloc_fn_(other.malloc_fn_), free_fn_(other.free_fn_) {}
19+
20+
allocator_wrapper(void* (*malloc_fn)(size_t), void (*free_fn)(void*)) noexcept
21+
: malloc_fn_(malloc_fn), free_fn_(free_fn) {}
22+
23+
allocator_wrapper(const allocator_wrapper &) = default;
24+
allocator_wrapper &operator=(const allocator_wrapper &) = default;
25+
~allocator_wrapper() = default;
26+
27+
using is_always_equal = std::false_type;
28+
29+
pointer allocate(std::size_t size) {
30+
return reinterpret_cast<T*>(malloc_fn_(size*sizeof(T)));
31+
}
32+
33+
void deallocate(pointer ptr, std::size_t n) {
34+
free_fn_(ptr);
35+
}
36+
37+
bool operator==(const allocator_wrapper &) const = default;
38+
bool operator!=(const allocator_wrapper &) const = default;
39+
40+
template <typename U> struct rebind {
41+
using other = allocator_wrapper<U>;
42+
};
43+
44+
45+
void* (*malloc_fn_)(size_t);
46+
void (*free_fn_)(void*);
47+
};
48+
49+
50+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <allocator_wrapper.hpp>
4+
5+
extern "C" {
6+
7+
template <typename T, typename I = std::size_t>
8+
coo_matrix<T, I> read_coo_matrix(std::string fname) {
9+
return read_coo_matrix<T, I>(fname, std::allocator<T>{});
10+
}
11+
12+
};

0 commit comments

Comments
 (0)