1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LITENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR TONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef FRUIT_FIXED_SIZE_ALLOTATOR_DEFN_H
18 #define FRUIT_FIXED_SIZE_ALLOTATOR_DEFN_H
19
20 #include <fruit/impl/fruit_assert.h>
21
22 #include <cassert>
23
24 #if FRUIT_EXTRA_DEBUG
25 #include <iostream>
26 #endif
27
28 // Redundant, but makes KDevelop happy.
29 #include <fruit/impl/data_structures/fixed_size_allocator.h>
30
31 namespace fruit {
32 namespace impl {
33
34 template <typename C>
destroyObject(void * p)35 void FixedSizeAllocator::destroyObject(void* p) {
36 C* cPtr = reinterpret_cast<C*>(p);
37 cPtr->C::~C();
38 }
39
40 template <typename C>
destroyExternalObject(void * p)41 void FixedSizeAllocator::destroyExternalObject(void* p) {
42 C* cPtr = reinterpret_cast<C*>(p);
43 delete cPtr; // LCOV_EXCL_BR_LINE
44 }
45
addType(TypeId typeId)46 inline void FixedSizeAllocator::FixedSizeAllocatorData::addType(TypeId typeId) {
47 #if FRUIT_EXTRA_DEBUG
48 types[typeId]++;
49 #endif
50 if (!typeId.type_info->isTriviallyDestructible()) {
51 num_types_to_destroy++;
52 }
53 total_size += maximumRequiredSpace(typeId);
54 }
55
addExternallyAllocatedType(TypeId typeId)56 inline void FixedSizeAllocator::FixedSizeAllocatorData::addExternallyAllocatedType(TypeId typeId) {
57 (void)typeId;
58 num_types_to_destroy++;
59 }
60
maximumRequiredSpace(TypeId type)61 inline std::size_t FixedSizeAllocator::FixedSizeAllocatorData::maximumRequiredSpace(TypeId type) {
62 return type.type_info->alignment() + type.type_info->size() - 1;
63 }
64
65 template <typename AnnotatedT, typename... Args>
66 FRUIT_ALWAYS_INLINE inline fruit::impl::meta::UnwrapType<
67 fruit::impl::meta::Eval<fruit::impl::meta::RemoveAnnotations(fruit::impl::meta::Type<AnnotatedT>)>>*
constructObject(Args &&...args)68 FixedSizeAllocator::constructObject(Args&&... args) {
69 using T = fruit::impl::meta::UnwrapType<
70 fruit::impl::meta::Eval<fruit::impl::meta::RemoveAnnotations(fruit::impl::meta::Type<AnnotatedT>)>>;
71
72 char* p = storage_last_used;
73 size_t misalignment = std::uintptr_t(p) % alignof(T);
74 #if FRUIT_EXTRA_DEBUG
75 FruitAssert(remaining_types[getTypeId<AnnotatedT>()] != 0);
76 remaining_types[getTypeId<AnnotatedT>()]--;
77 #endif
78 p += alignof(T) - misalignment;
79 FruitAssert(std::uintptr_t(p) % alignof(T) == 0);
80 T* x = reinterpret_cast<T*>(p);
81 storage_last_used = p + sizeof(T) - 1;
82
83 // This runs arbitrary code (T's constructor), which might end up calling
84 // constructObject recursively. We must make sure all invariants are satisfied before
85 // calling this.
86 new (x) T(std::forward<Args>(args)...); // LCOV_EXCL_BR_LINE
87
88 // We still run this later though, since if T's constructor throws we don't want to
89 // destruct this object in FixedSizeAllocator's destructor.
90 if (!std::is_trivially_destructible<T>::value) {
91 on_destruction.push_back(std::pair<destroy_t, void*>{destroyObject<T>, x});
92 }
93 return x;
94 }
95
96 template <typename T>
registerExternallyAllocatedObject(T * p)97 inline void FixedSizeAllocator::registerExternallyAllocatedObject(T* p) {
98 on_destruction.push_back(std::pair<destroy_t, void*>{destroyExternalObject<T>, p});
99 }
100
FixedSizeAllocator(FixedSizeAllocatorData allocator_data)101 inline FixedSizeAllocator::FixedSizeAllocator(FixedSizeAllocatorData allocator_data)
102 : on_destruction(allocator_data.num_types_to_destroy) {
103 // The +1 is because we waste the first byte (storage_last_used points to the beginning of storage).
104 storage_begin = new char[allocator_data.total_size + 1];
105 storage_last_used = storage_begin;
106 #if FRUIT_EXTRA_DEBUG
107 remaining_types = allocator_data.types;
108 std::cerr << "Constructing allocator for types:";
109 for (auto x : remaining_types) {
110 std::cerr << " " << x.first;
111 }
112 std::cerr << std::endl;
113 #endif
114 }
115
FixedSizeAllocator(FixedSizeAllocator && x)116 inline FixedSizeAllocator::FixedSizeAllocator(FixedSizeAllocator&& x) : FixedSizeAllocator() {
117 std::swap(storage_begin, x.storage_begin);
118 std::swap(storage_last_used, x.storage_last_used);
119 std::swap(on_destruction, x.on_destruction);
120 #if FRUIT_EXTRA_DEBUG
121 std::swap(remaining_types, x.remaining_types);
122 #endif
123 }
124
125 inline FixedSizeAllocator& FixedSizeAllocator::operator=(FixedSizeAllocator&& x) {
126 std::swap(storage_begin, x.storage_begin);
127 std::swap(storage_last_used, x.storage_last_used);
128 std::swap(on_destruction, x.on_destruction);
129 #if FRUIT_EXTRA_DEBUG
130 std::swap(remaining_types, x.remaining_types);
131 #endif
132 return *this;
133 }
134
135 } // namespace fruit
136 } // namespace impl
137
138 #endif // FRUIT_FIXED_SIZE_ALLOTATOR_DEFN_H
139