-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathiallocator.h
149 lines (127 loc) · 6.08 KB
/
iallocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once
////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2018 Nicholas Frechette & Animation Compression Library contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#include "acl/version.h"
#include "acl/core/error.h"
#include "acl/core/memory_utils.h"
#include "acl/core/impl/bit_cast.impl.h"
#include "acl/core/impl/compiler_utils.h"
#include <type_traits>
#include <utility>
#include <cstdint>
ACL_IMPL_FILE_PRAGMA_PUSH
namespace acl
{
ACL_IMPL_VERSION_NAMESPACE_BEGIN
////////////////////////////////////////////////////////////////////////////////
// A simple memory allocator interface.
//
// In order to integrate this library into your own code base, you will
// need to provide some functions with an allocator instance that derives
// from this interface.
//
// See ansi_allocator.h for an implementation that uses the system malloc/free.
////////////////////////////////////////////////////////////////////////////////
class iallocator
{
public:
static constexpr size_t k_default_alignment = 16;
iallocator() noexcept = default;
virtual ~iallocator() = default;
////////////////////////////////////////////////////////////////////////////////
// Allocates memory with the specified size and alignment.
//
// size: Size in bytes to allocate.
// alignment: Alignment to allocate the memory with.
virtual void* allocate(size_t size, size_t alignment = k_default_alignment) = 0;
////////////////////////////////////////////////////////////////////////////////
// Deallocates previously allocated memory and releases it.
//
// ptr: A pointer to memory previously allocated or nullptr.
// size: Size in bytes of the allocation. This will match the original size requested through 'allocate'.
virtual void deallocate(void* ptr, size_t size) = 0;
};
//////////////////////////////////////////////////////////////////////////
template<typename allocated_type, typename... args>
allocated_type* allocate_type(iallocator& allocator, args&&... arguments)
{
allocated_type* ptr = acl_impl::bit_cast<allocated_type*>(allocator.allocate(sizeof(allocated_type), alignof(allocated_type)));
if (acl_impl::is_trivially_default_constructible<allocated_type>::value)
return ptr;
return new(ptr) allocated_type(std::forward<args>(arguments)...);
}
template<typename allocated_type, typename... args>
allocated_type* allocate_type_aligned(iallocator& allocator, size_t alignment, args&&... arguments)
{
ACL_ASSERT(is_alignment_valid<allocated_type>(alignment), "Invalid alignment: %u. Expected a power of two at least equal to %u", alignment, alignof(allocated_type));
allocated_type* ptr = acl_impl::bit_cast<allocated_type*>(allocator.allocate(sizeof(allocated_type), alignment));
if (acl_impl::is_trivially_default_constructible<allocated_type>::value)
return ptr;
return new(ptr) allocated_type(std::forward<args>(arguments)...);
}
template<typename allocated_type>
void deallocate_type(iallocator& allocator, allocated_type* ptr)
{
if (ptr == nullptr)
return;
if (!std::is_trivially_destructible<allocated_type>::value)
ptr->~allocated_type();
allocator.deallocate(ptr, sizeof(allocated_type));
}
template<typename allocated_type, typename... args>
allocated_type* allocate_type_array(iallocator& allocator, size_t num_elements, args&&... arguments)
{
allocated_type* ptr = acl_impl::bit_cast<allocated_type*>(allocator.allocate(sizeof(allocated_type) * num_elements, alignof(allocated_type)));
if (acl_impl::is_trivially_default_constructible<allocated_type>::value)
return ptr;
for (size_t element_index = 0; element_index < num_elements; ++element_index)
new(&ptr[element_index]) allocated_type(std::forward<args>(arguments)...);
return ptr;
}
template<typename allocated_type, typename... args>
allocated_type* allocate_type_array_aligned(iallocator& allocator, size_t num_elements, size_t alignment, args&&... arguments)
{
ACL_ASSERT(is_alignment_valid<allocated_type>(alignment), "Invalid alignment: %zu. Expected a power of two at least equal to %zu", alignment, alignof(allocated_type));
allocated_type* ptr = acl_impl::bit_cast<allocated_type*>(allocator.allocate(sizeof(allocated_type) * num_elements, alignment));
if (acl_impl::is_trivially_default_constructible<allocated_type>::value)
return ptr;
for (size_t element_index = 0; element_index < num_elements; ++element_index)
new(&ptr[element_index]) allocated_type(std::forward<args>(arguments)...);
return ptr;
}
template<typename allocated_type>
void deallocate_type_array(iallocator& allocator, allocated_type* elements, size_t num_elements)
{
if (elements == nullptr)
return;
if (!std::is_trivially_destructible<allocated_type>::value)
{
for (size_t element_index = 0; element_index < num_elements; ++element_index)
elements[element_index].~allocated_type();
}
allocator.deallocate(elements, sizeof(allocated_type) * num_elements);
}
ACL_IMPL_VERSION_NAMESPACE_END
}
ACL_IMPL_FILE_PRAGMA_POP