1 | // |
2 | // immer: immutable data structures for C++ |
3 | // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente |
4 | // |
5 | // This software is distributed under the Boost Software License, Version 1.0. |
6 | // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt |
7 | // |
8 | |
9 | #pragma once |
10 | |
11 | #include <atomic> |
12 | #include <cassert> |
13 | |
14 | namespace immer { |
15 | |
16 | /*! |
17 | * Adaptor that uses `SmallHeap` for allocations that are smaller or |
18 | * equal to `Size` and `BigHeap` otherwise. |
19 | */ |
20 | template <std::size_t Size, typename SmallHeap, typename BigHeap> |
21 | struct split_heap |
22 | { |
23 | template <typename... Tags> |
24 | static void* allocate(std::size_t size, Tags... tags) |
25 | { |
26 | return size <= Size |
27 | ? SmallHeap::allocate(size, tags...) |
28 | : BigHeap::allocate(size, tags...); |
29 | } |
30 | |
31 | template <typename... Tags> |
32 | static void deallocate(std::size_t size, void* data, Tags... tags) |
33 | { |
34 | if (size <= Size) |
35 | SmallHeap::deallocate(size, data, tags...); |
36 | else |
37 | BigHeap::deallocate(size, data, tags...); |
38 | } |
39 | }; |
40 | |
41 | } // namespace immer |
42 | |