Skip to content

Commit

Permalink
example readme & fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h committed Jan 7, 2025
1 parent 98284e6 commit f231607
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ It contains:
- `for_{types,values,range}`: Compile time for loops for types, values or ranges
- `polymorphic_allocator`: Like `std::pmr::polymorphic_allocator` but with static dispatch
- `limit_allocator`: Allocator wrapper that limits the amount of memory that is allowed to be allocated
- `pool` & `pool_allocator`: Arena/pool allocator optimized for a limited number of known allocation sizes.
- `DICE_DEFER`/`DICE_DEFER_TO_SUCCES`/`DICE_DEFER_TO_FAIL`: On-the-fly RAII for types that do not support it natively (similar to go's `defer` keyword)
- `overloaded`: Composition for `std::variant` visitor lambdas
- `flex_array`: A combination of `std::array`, `std::span` and a `vector` with small buffer optimization
Expand Down Expand Up @@ -69,6 +70,11 @@ Which means: vtables will not work (because they use absolute pointers) and ther
Allocator wrapper that limits the amount of memory that can be allocated through the inner allocator.
If the limit is exceeded it will throw `std::bad_alloc`.

### `pool_allocator`
A memory arena/pool allocator with configurable allocation sizes. This is implemented
as a collection of pools with varying allocation sizes. Allocations that do not
fit into any of its pools are directly served via `new`.

### `DICE_DEFER`/`DICE_DEFER_TO_SUCCES`/`DICE_DEFER_TO_FAIL`
A mechanism similar to go's `defer` keyword, which can be used to defer some action to scope exit.
The primary use-case for this is on-the-fly RAII-like resource management for types that do not support RAII (for example C types).
Expand Down
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ target_link_libraries(example_limit_allocator
dice-template-library::dice-template-library
)

add_executable(example_pool_allocator
example_pool_allocator.cpp)
target_link_libraries(example_pool_allocator
PRIVATE
dice-template-library::dice-template-library
)
38 changes: 38 additions & 0 deletions examples/example_pool_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <dice/template-library/pool_allocator.hpp>

#include <cstdint>
#include <iostream>
#include <vector>

struct list {
uint64_t elem;
list *next;
};

int main() {
dice::template_library::pool<sizeof(list)> pool;

{ // efficient pool allocations for elements of known size
auto list_alloc = pool.get_allocator<list>();

auto *head = list_alloc.allocate(1); // efficient pool allocation
new (head) list{.elem = 0, .next = nullptr};

head->next = list_alloc.allocate(1); // efficient pool allocation
new (head->next) list{.elem = 1, .next = nullptr};

auto const *cur = head;
while (cur != nullptr) {
std::cout << cur->elem << " ";
cur = cur->next;
}

list_alloc.deallocate(head->next, 1);
list_alloc.deallocate(head, 1);
}

{ // fallback allocation with new & support as container allocator
std::vector<uint64_t, dice::template_library::pool_allocator<uint64_t, sizeof(list)>> vec(pool.get_allocator());
vec.resize(1024);
}
}
2 changes: 1 addition & 1 deletion include/dice/template-library/pool_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace dice::template_library {

template<typename U>
constexpr pool_allocator(pool_allocator<U, bucket_sizes...> const &other) noexcept
: pool_{other.state_} {
: pool_{other.pool_} {
}

constexpr pointer allocate(size_t n) {
Expand Down

0 comments on commit f231607

Please sign in to comment.