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 <memory> |
12 | |
13 | namespace immer { |
14 | |
15 | /*! |
16 | * A heap that uses `operator new` and `operator delete`. |
17 | */ |
18 | struct cpp_heap |
19 | { |
20 | /*! |
21 | * Returns a pointer to a memory region of size `size`, if the |
22 | * allocation was successful, and throws otherwise. |
23 | */ |
24 | template <typename... Tags> |
25 | static void* allocate(std::size_t size, Tags...) |
26 | { |
27 | return ::operator new(size); |
28 | } |
29 | |
30 | /*! |
31 | * Releases a memory region `data` that was previously returned by |
32 | * `allocate`. One must not use nor deallocate again a memory |
33 | * region that once it has been deallocated. |
34 | */ |
35 | static void deallocate(std::size_t size, void* data) |
36 | { |
37 | ::operator delete(data); |
38 | } |
39 | }; |
40 | |
41 | } // namespace immer |
42 | |