/src/croaring/src/memory.c
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | |
3 | | #include <roaring/memory.h> |
4 | | |
5 | | // without the following, we get lots of warnings about posix_memalign |
6 | | #ifndef __cplusplus |
7 | | extern int posix_memalign(void** __memptr, size_t __alignment, size_t __size); |
8 | | #endif //__cplusplus // C++ does not have a well defined signature |
9 | | |
10 | | // portable version of posix_memalign |
11 | 293k | static void* roaring_bitmap_aligned_malloc(size_t alignment, size_t size) { |
12 | 293k | void* p; |
13 | | #ifdef _MSC_VER |
14 | | p = _aligned_malloc(size, alignment); |
15 | | #elif defined(__MINGW32__) || defined(__MINGW64__) |
16 | | p = __mingw_aligned_malloc(size, alignment); |
17 | | #else |
18 | | // somehow, if this is used before including "x86intrin.h", it creates an |
19 | | // implicit defined warning. |
20 | 293k | if (posix_memalign(&p, alignment, size) != 0) return NULL; |
21 | 293k | #endif |
22 | 293k | return p; |
23 | 293k | } |
24 | | |
25 | 293k | static void roaring_bitmap_aligned_free(void* memblock) { |
26 | | #ifdef _MSC_VER |
27 | | _aligned_free(memblock); |
28 | | #elif defined(__MINGW32__) || defined(__MINGW64__) |
29 | | __mingw_aligned_free(memblock); |
30 | | #else |
31 | 293k | free(memblock); |
32 | 293k | #endif |
33 | 293k | } |
34 | | |
35 | | static roaring_memory_t global_memory_hook = { |
36 | | .malloc = malloc, |
37 | | .realloc = realloc, |
38 | | .calloc = calloc, |
39 | | .free = free, |
40 | | .aligned_malloc = roaring_bitmap_aligned_malloc, |
41 | | .aligned_free = roaring_bitmap_aligned_free, |
42 | | }; |
43 | | |
44 | 0 | void roaring_init_memory_hook(roaring_memory_t memory_hook) { |
45 | 0 | global_memory_hook = memory_hook; |
46 | 0 | } |
47 | | |
48 | 1.38M | void* roaring_malloc(size_t n) { return global_memory_hook.malloc(n); } |
49 | | |
50 | 109k | void* roaring_realloc(void* p, size_t new_sz) { |
51 | 109k | return global_memory_hook.realloc(p, new_sz); |
52 | 109k | } |
53 | | |
54 | 0 | void* roaring_calloc(size_t n_elements, size_t element_size) { |
55 | 0 | return global_memory_hook.calloc(n_elements, element_size); |
56 | 0 | } |
57 | | |
58 | 1.62M | void roaring_free(void* p) { global_memory_hook.free(p); } |
59 | | |
60 | 293k | void* roaring_aligned_malloc(size_t alignment, size_t size) { |
61 | 293k | return global_memory_hook.aligned_malloc(alignment, size); |
62 | 293k | } |
63 | | |
64 | 293k | void roaring_aligned_free(void* p) { global_memory_hook.aligned_free(p); } |