|
| 1 | +//===-- A data structure which stores data in blocks -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_SUPPORT_CPP_BLOCKSTORE_H |
| 10 | +#define LLVM_LIBC_SUPPORT_CPP_BLOCKSTORE_H |
| 11 | + |
| 12 | +#include <stddef.h> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +namespace __llvm_libc { |
| 17 | +namespace cpp { |
| 18 | + |
| 19 | +// The difference between BlockStore a traditional vector types is that, |
| 20 | +// when more capacity is desired, a new block is added instead of allocating |
| 21 | +// a larger sized array and copying over existing items to the new allocation. |
| 22 | +// Also, the initial block does not need heap allocation. Hence, a BlockStore is |
| 23 | +// suitable for global objects as it does not require explicit construction. |
| 24 | +// Also, the destructor of this class does nothing, which eliminates the need |
| 25 | +// for an atexit global object destruction. But, it also means that the global |
| 26 | +// object should be explicitly cleaned up at the appropriate time. |
| 27 | +// |
| 28 | +// If REVERSE_ORDER is true, the iteration of elements will in the reverse |
| 29 | +// order. Also, since REVERSE_ORDER is a constexpr, conditionals branching |
| 30 | +// on its value will be optimized out in the code below. |
| 31 | +template <typename T, size_t BLOCK_SIZE, bool REVERSE_ORDER = false> |
| 32 | +class BlockStore { |
| 33 | +protected: |
| 34 | + struct Block { |
| 35 | + alignas(T) uint8_t data[BLOCK_SIZE * sizeof(T)] = {0}; |
| 36 | + Block *next = nullptr; |
| 37 | + }; |
| 38 | + |
| 39 | + Block first; |
| 40 | + Block *current = &first; |
| 41 | + size_t fill_count = 0; |
| 42 | + |
| 43 | +public: |
| 44 | + constexpr BlockStore() = default; |
| 45 | + ~BlockStore() = default; |
| 46 | + |
| 47 | + class iterator { |
| 48 | + Block *block; |
| 49 | + size_t index; |
| 50 | + |
| 51 | + public: |
| 52 | + constexpr iterator(Block *b, size_t i) : block(b), index(i) {} |
| 53 | + |
| 54 | + iterator &operator++() { |
| 55 | + if (REVERSE_ORDER) { |
| 56 | + if (index == 0) |
| 57 | + return *this; |
| 58 | + |
| 59 | + --index; |
| 60 | + if (index == 0 && block->next != nullptr) { |
| 61 | + index = BLOCK_SIZE; |
| 62 | + block = block->next; |
| 63 | + } |
| 64 | + } else { |
| 65 | + if (index == BLOCK_SIZE) |
| 66 | + return *this; |
| 67 | + |
| 68 | + ++index; |
| 69 | + if (index == BLOCK_SIZE && block->next != nullptr) { |
| 70 | + index = 0; |
| 71 | + block = block->next; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return *this; |
| 76 | + } |
| 77 | + |
| 78 | + T &operator*() { |
| 79 | + size_t true_index = REVERSE_ORDER ? index - 1 : index; |
| 80 | + return *reinterpret_cast<T *>(block->data + sizeof(T) * true_index); |
| 81 | + } |
| 82 | + |
| 83 | + bool operator==(const iterator &rhs) const { |
| 84 | + return block == rhs.block && index == rhs.index; |
| 85 | + } |
| 86 | + |
| 87 | + bool operator!=(const iterator &rhs) const { |
| 88 | + return block != rhs.block || index != rhs.index; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + static void destroy(BlockStore<T, BLOCK_SIZE, REVERSE_ORDER> *block_store); |
| 93 | + |
| 94 | + T *new_obj() { |
| 95 | + if (fill_count == BLOCK_SIZE) { |
| 96 | + auto new_block = reinterpret_cast<Block *>(::malloc(sizeof(Block))); |
| 97 | + if (REVERSE_ORDER) { |
| 98 | + new_block->next = current; |
| 99 | + } else { |
| 100 | + new_block->next = nullptr; |
| 101 | + current->next = new_block; |
| 102 | + } |
| 103 | + current = new_block; |
| 104 | + fill_count = 0; |
| 105 | + } |
| 106 | + T *obj = reinterpret_cast<T *>(current->data + fill_count * sizeof(T)); |
| 107 | + ++fill_count; |
| 108 | + return obj; |
| 109 | + } |
| 110 | + |
| 111 | + void push_back(const T &value) { |
| 112 | + T *ptr = new_obj(); |
| 113 | + *ptr = value; |
| 114 | + } |
| 115 | + |
| 116 | + iterator begin() { |
| 117 | + if (REVERSE_ORDER) |
| 118 | + return iterator(current, fill_count); |
| 119 | + else |
| 120 | + return iterator(&first, 0); |
| 121 | + } |
| 122 | + |
| 123 | + iterator end() { |
| 124 | + if (REVERSE_ORDER) |
| 125 | + return iterator(&first, 0); |
| 126 | + else |
| 127 | + return iterator(current, fill_count); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +template <typename T, size_t BLOCK_SIZE, bool REVERSE_ORDER> |
| 132 | +void BlockStore<T, BLOCK_SIZE, REVERSE_ORDER>::destroy( |
| 133 | + BlockStore<T, BLOCK_SIZE, REVERSE_ORDER> *block_store) { |
| 134 | + if (REVERSE_ORDER) { |
| 135 | + auto current = block_store->current; |
| 136 | + while (current->next != nullptr) { |
| 137 | + auto temp = current; |
| 138 | + current = current->next; |
| 139 | + free(temp); |
| 140 | + } |
| 141 | + } else { |
| 142 | + auto current = block_store->first.next; |
| 143 | + while (current != nullptr) { |
| 144 | + auto temp = current; |
| 145 | + current = current->next; |
| 146 | + free(temp); |
| 147 | + } |
| 148 | + } |
| 149 | + block_store->current = nullptr; |
| 150 | + block_store->fill_count = 0; |
| 151 | +} |
| 152 | + |
| 153 | +// A convenience type for reverse order block stores. |
| 154 | +template <typename T, size_t BLOCK_SIZE> |
| 155 | +using ReverseOrderBlockStore = BlockStore<T, BLOCK_SIZE, true>; |
| 156 | + |
| 157 | +} // namespace cpp |
| 158 | +} // namespace __llvm_libc |
| 159 | + |
| 160 | +#endif // LLVM_LIBC_SUPPORT_CPP_BLOCKSTORE_H |
0 commit comments