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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/* Copyright (C) 2015 Wildfire Games.
*
* 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.
*/
#ifndef INCLUDED_ALLOCATORS_UNIQUE_RANGE
#define INCLUDED_ALLOCATORS_UNIQUE_RANGE
#include "lib/lib_api.h"
#include "lib/alignment.h" // allocationAlignment
#include "lib/sysdep/vm.h"
// we usually don't hold multiple references to allocations, so unique_ptr
// can be used instead of the more complex (ICC generated incorrect code on
// 2 occasions) and expensive shared_ptr.
// a custom deleter is required because allocators such as ReserveAddressSpace need to
// pass the size to their deleter. we want to mix pointers from various allocators, but
// unique_ptr's deleter is fixed at compile-time, so it would need to be general enough
// to handle all allocators.
// storing the size and a function pointer would be one such solution, with the added
// bonus of no longer requiring a complete type at the invocation of ~unique_ptr.
// however, this inflates the pointer size to 3 words. if only a few allocator types
// are needed, we can replace the function pointer with an index stashed into the
// lower bits of the pointer (safe because all allocations' addresses are multiples
// of allocationAlignment).
typedef intptr_t IdxDeleter;
// no-op deleter (use when returning part of an existing allocation)
static const IdxDeleter idxDeleterNone = 0;
typedef void (*UniqueRangeDeleter)(void* pointer, size_t size);
/**
* register a deleter, returning its index within the table.
*
* @param deleter function pointer. must be uniquely associated with
* the idxDeleter storage location.
* @param idxDeleter location where to store the next available index.
* if it is already non-zero, skip the call to this function to
* avoid overhead.
*
* thread-safe. idxDeleter is used for mutual exclusion between
* multiple callers for the same deleter. concurrent registration of
* different deleters is also safe due to atomic increments.
*
* halts the program if more than allocationAlignment deleters are
* to be registered.
**/
LIB_API void RegisterUniqueRangeDeleter(UniqueRangeDeleter deleter, volatile IdxDeleter* idxDeleter);
LIB_API NOTHROW_DECLARE void CallUniqueRangeDeleter(void* pointer, size_t size, IdxDeleter idxDeleter);
// unfortunately, unique_ptr allows constructing without a custom deleter. to ensure callers can
// rely upon pointers being associated with a size, we introduce a `UniqueRange' replacement.
// its interface is identical to unique_ptr except for the constructors, the addition of
// size() and the removal of operator bool (which avoids implicit casts to int).
class UniqueRange
{
public:
typedef void* pointer;
typedef void element_type;
UniqueRange()
{
Clear();
}
UniqueRange(pointer p, size_t size, IdxDeleter deleter)
{
Set(p, size, deleter);
}
UniqueRange(UniqueRange&& rvalue)
{
Pilfer(rvalue);
}
UniqueRange& operator=(UniqueRange&& rvalue)
{
UniqueRange& lvalue = rvalue;
if(this != &lvalue)
{
Delete();
Pilfer(lvalue);
}
return *this;
}
~UniqueRange()
{
Delete();
}
pointer get() const
{
return pointer(address_ & ~(allocationAlignment-1));
}
IdxDeleter get_deleter() const
{
return IdxDeleter(address_ % allocationAlignment);
}
size_t size() const
{
return size_;
}
// side effect: subsequent get_deleter will return idxDeleterNone
pointer release() // relinquish ownership
{
pointer ret = get();
Clear();
return ret;
}
void reset()
{
Delete();
Clear();
}
void reset(pointer p, size_t size, IdxDeleter deleter)
{
Delete();
Set(p, size, deleter);
}
void swap(UniqueRange& rhs)
{
std::swap(address_, rhs.address_);
std::swap(size_, rhs.size_);
}
// don't define construction and assignment from lvalue,
// but the declarations must be accessible
UniqueRange(const UniqueRange&);
UniqueRange& operator=(const UniqueRange&);
private:
void Set(pointer p, size_t size, IdxDeleter deleter)
{
ASSERT((uintptr_t(p) % allocationAlignment) == 0);
ASSERT(size_t(deleter) < allocationAlignment);
address_ = uintptr_t(p) | deleter;
size_ = size;
ASSERT(get() == p);
ASSERT(get_deleter() == deleter);
ASSERT(this->size() == size);
}
void Clear()
{
Set(0, 0, idxDeleterNone);
}
void Pilfer(UniqueRange& victim)
{
const size_t size = victim.size();
const IdxDeleter idxDeleter = victim.get_deleter();
pointer p = victim.release();
Set(p, size, idxDeleter);
victim.Clear();
}
void Delete()
{
CallUniqueRangeDeleter(get(), size(), get_deleter());
}
// (IdxDeleter is stored in the lower bits of address since size might not even be a multiple of 4.)
uintptr_t address_;
size_t size_;
};
namespace std {
static inline void swap(UniqueRange& p1, UniqueRange& p2)
{
p1.swap(p2);
}
static inline void swap(UniqueRange&& p1, UniqueRange& p2)
{
p2.swap(p1);
}
static inline void swap(UniqueRange& p1, UniqueRange&& p2)
{
p1.swap(p2);
}
}
LIB_API UniqueRange AllocateAligned(size_t size, size_t alignment);
LIB_API UniqueRange AllocateVM(size_t size, vm::PageType pageSize = vm::kDefault, int prot = PROT_READ|PROT_WRITE);
#endif // #ifndef INCLUDED_ALLOCATORS_UNIQUE_RANGE
|