Bit vector  0.9
Fast and space efficient dynamic bit vector library.
allocator.hpp
1 #ifndef BV_ALLOCATOR_HPP
2 #define BV_ALLOCATOR_HPP
3 
4 #include <cstdint>
5 #include <cstdlib>
6 #include <cstring>
7 #include <iostream>
8 #include <new>
9 
10 namespace bv {
11 
20 class malloc_alloc {
21  protected:
22  uint64_t allocations_;
23 
24  public:
25  malloc_alloc() { allocations_ = 0; }
26 
34  template <class node_type>
35  node_type* allocate_node() {
36  allocations_++;
37  void* nd = malloc(sizeof(node_type));
38  return new (nd) node_type();
39  }
40 
50  template <class node_type>
51  void deallocate_node(node_type* node) {
52  allocations_--;
53  free(node);
54  }
55 
67  template <class leaf_type>
68  leaf_type* allocate_leaf(uint64_t size) {
69  allocations_++;
70  constexpr size_t leaf_bytes = sizeof(leaf_type) + sizeof(leaf_type) % 8;
71  void* leaf = malloc(leaf_bytes + size * sizeof(uint64_t));
72  uint8_t* data_ptr =
73  reinterpret_cast<uint8_t*>(leaf) + leaf_bytes;
74  memset(data_ptr, 0, size * sizeof(uint64_t));
75  return new (leaf)
76  leaf_type(size, reinterpret_cast<uint64_t*>(data_ptr));
77  }
78 
86  template <class leaf_type>
87  void deallocate_leaf(leaf_type* leaf) {
88  allocations_--;
89  free(leaf);
90  }
91 
103  template <class leaf_type>
104  leaf_type* reallocate_leaf(leaf_type* leaf, uint64_t old_size,
105  uint64_t new_size) {
106  constexpr size_t leaf_bytes = sizeof(leaf_type) + sizeof(leaf_type) % 8;
107 #pragma GCC diagnostic ignored "-Wclass-memaccess"
108  leaf_type* n_leaf = reinterpret_cast<leaf_type*>(
109  realloc(leaf, leaf_bytes + new_size * sizeof(uint64_t)));
110 #pragma GCC diagnostic pop
111  uint8_t* data_ptr =
112  reinterpret_cast<uint8_t*>(n_leaf) + leaf_bytes;
113  memset(data_ptr + sizeof(uint64_t) * old_size, 0,
114  sizeof(uint64_t) * (new_size - old_size));
115  n_leaf->set_data_ptr(reinterpret_cast<uint64_t*>(data_ptr));
116  n_leaf->capacity(new_size);
117  return n_leaf;
118  }
119 
125  uint64_t live_allocations() const { return allocations_; }
126 };
127 } // namespace bv
128 #endif
bv::malloc_alloc::allocations_
uint64_t allocations_
Number of objects currently allocated.
Definition: allocator.hpp:22
bv::malloc_alloc::deallocate_leaf
void deallocate_leaf(leaf_type *leaf)
Deallocates leaf node.
Definition: allocator.hpp:87
bv::bv
simple_bv< 8, 16384, 64 > bv
Default dynamic bit vector type.
Definition: bv.hpp:94
bv::leaf
Simple flat dynamic bit vector for use as a leaf for a dynamic b-tree bit vector.
Definition: leaf.hpp:47
bv::malloc_alloc::deallocate_node
void deallocate_node(node_type *node)
Deallocate internal node.
Definition: allocator.hpp:51
bv::malloc_alloc
Simple malloc based allocation for internal nodes and leaves.
Definition: allocator.hpp:20
bv::malloc_alloc::allocate_node
node_type * allocate_node()
Allocate new internal node.
Definition: allocator.hpp:35
bv::node
Internal node for use with bit vector b-tree structures.
Definition: node.hpp:65
bv::malloc_alloc::live_allocations
uint64_t live_allocations() const
Get the number of blocks currenty allocated by this allocator instance.
Definition: allocator.hpp:125
bv::malloc_alloc::reallocate_leaf
leaf_type * reallocate_leaf(leaf_type *leaf, uint64_t old_size, uint64_t new_size)
Reallocator for leaf nodes.
Definition: allocator.hpp:104
bv::malloc_alloc::allocate_leaf
leaf_type * allocate_leaf(uint64_t size)
Allocates a new leaf with space for storing size * 64 bits.
Definition: allocator.hpp:68