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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* Copyright (C) 2018 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.
*/
/*
* policy class templates for allocators.
*/
#ifndef ALLOCATOR_POLICIES
#define ALLOCATOR_POLICIES
#include "lib/alignment.h" // pageSize
#include "lib/allocators/allocator_adapters.h"
#include "lib/allocators/freelist.h"
namespace Allocators {
//-----------------------------------------------------------------------------
// Growth
// O(N) allocations, O(1) wasted space.
template<size_t increment = g_PageSize>
struct Growth_Linear
{
size_t operator()(size_t oldSize) const
{
return oldSize + increment;
}
};
// O(log r) allocations, O(N) wasted space. NB: the common choice of
// expansion factor r = 2 (e.g. in the GCC STL) prevents
// Storage_Reallocate from reusing previous memory blocks,
// thus constantly growing the heap and decreasing locality.
// Alexandrescu [C++ and Beyond 2010] recommends r < 33/25.
// we approximate this with a power of two divisor to allow shifting.
// C++ does allow reference-to-float template parameters, but
// integer arithmetic is expected to be faster.
// (Storage_Commit should use 2:1 because it is cheaper to
// compute and retains power-of-two sizes.)
template<size_t multiplier = 21, size_t divisor = 16>
struct Growth_Exponential
{
size_t operator()(size_t oldSize) const
{
const size_t product = oldSize * multiplier;
// detect overflow, but allow equality in case oldSize = 0,
// which isn't a problem because Storage_Commit::Expand
// raises it to requiredCapacity.
ASSERT(product >= oldSize);
return product / divisor;
}
};
//-----------------------------------------------------------------------------
// Storage
// a contiguous region of memory (not just an "array", because
// allocators such as Arena append variable-sized intervals).
//
// we don't store smart pointers because storage usually doesn't need
// to be copied, and ICC 11 sometimes wasn't able to inline Address().
struct Storage
{
// @return starting address (alignment depends on the allocator).
uintptr_t Address() const;
// @return size [bytes] of currently accessible memory.
size_t Capacity() const;
// @return largest possible capacity [bytes].
size_t MaxCapacity() const;
// expand Capacity() to at least requiredCapacity (possibly more
// depending on GrowthPolicy).
// @param requiredCapacity > Capacity()
// @return false and leave Capacity() unchanged if expansion failed,
// which is guaranteed to happen if requiredCapacity > MaxCapacity().
bool Expand(size_t requiredCapacity);
};
// allocate once and refuse subsequent expansion.
template<class Allocator = Allocator_Aligned<> >
class Storage_Fixed
{
NONCOPYABLE(Storage_Fixed);
public:
Storage_Fixed(size_t size)
: maxCapacity(size)
, storage(allocator.allocate(maxCapacity))
{
}
~Storage_Fixed()
{
allocator.deallocate(storage, maxCapacity);
}
uintptr_t Address() const
{
return uintptr_t(storage);
}
size_t Capacity() const
{
return maxCapacity;
}
size_t MaxCapacity() const
{
return maxCapacity;
}
bool Expand(size_t UNUSED(requiredCapacity))
{
return false;
}
private:
Allocator allocator;
size_t maxCapacity; // must be initialized before storage
void* storage;
};
// unlimited expansion by allocating larger storage and copying.
// (basically equivalent to std::vector, although Growth_Exponential
// is much more cache and allocator-friendly than the GCC STL)
template<class Allocator = Allocator_Heap, class GrowthPolicy = Growth_Exponential<> >
class Storage_Reallocate
{
NONCOPYABLE(Storage_Reallocate);
public:
Storage_Reallocate(size_t initialCapacity)
: capacity(initialCapacity)
, storage(allocator.allocate(initialCapacity))
{
}
~Storage_Reallocate()
{
allocator.deallocate(storage, capacity);
}
uintptr_t Address() const
{
return uintptr_t(storage);
}
size_t Capacity() const
{
return capacity;
}
size_t MaxCapacity() const
{
return std::numeric_limits<size_t>::max();
}
bool Expand(size_t requiredCapacity)
{
size_t newCapacity = std::max(requiredCapacity, GrowthPolicy()(capacity));
void* newStorage = allocator.allocate(newCapacity);
if(!newStorage)
return false;
memcpy(newStorage, storage, capacity);
std::swap(capacity, newCapacity);
std::swap(storage, newStorage);
allocator.deallocate(newStorage, newCapacity); // free PREVIOUS storage
return true;
}
private:
Allocator allocator;
size_t capacity; // must be initialized before storage
void* storage;
};
// expand up to the limit of the allocated address space by
// committing physical memory. this avoids copying and
// reduces wasted physical memory.
template<class Allocator = Allocator_AddressSpace<>, class GrowthPolicy = Growth_Exponential<2,1> >
class Storage_Commit
{
NONCOPYABLE(Storage_Commit);
public:
Storage_Commit(size_t maxCapacity_)
: maxCapacity(Align<g_PageSize>(maxCapacity_)) // see Expand
, storage(allocator.allocate(maxCapacity))
, capacity(0)
{
}
~Storage_Commit()
{
allocator.deallocate(storage, maxCapacity);
}
uintptr_t Address() const
{
return uintptr_t(storage);
}
size_t Capacity() const
{
return capacity;
}
size_t MaxCapacity() const
{
return maxCapacity;
}
bool Expand(size_t requiredCapacity)
{
size_t newCapacity = std::max(requiredCapacity, GrowthPolicy()(capacity));
// reduce the number of expensive commits by accurately
// reflecting the actual capacity. this is safe because
// we also round up maxCapacity.
newCapacity = Align<g_PageSize>(newCapacity);
if(newCapacity > maxCapacity)
return false;
if(!vm::Commit(Address()+capacity, newCapacity-capacity))
return false;
capacity = newCapacity;
return true;
}
private:
Allocator allocator;
size_t maxCapacity; // must be initialized before storage
void* storage;
size_t capacity;
};
// implicitly expand up to the limit of the allocated address space by
// committing physical memory when a page is first accessed.
// this is basically equivalent to Storage_Commit with Growth_Linear,
// except that there is no need to call Expand.
template<class Allocator = Allocator_AddressSpace<> >
class Storage_AutoCommit
{
NONCOPYABLE(Storage_AutoCommit);
public:
Storage_AutoCommit(size_t maxCapacity_)
: maxCapacity(Align<g_PageSize>(maxCapacity_)) // match user's expectation
, storage(allocator.allocate(maxCapacity))
{
vm::BeginOnDemandCommits();
}
~Storage_AutoCommit()
{
vm::EndOnDemandCommits();
allocator.deallocate(storage, maxCapacity);
}
uintptr_t Address() const
{
return uintptr_t(storage);
}
size_t Capacity() const
{
return maxCapacity;
}
size_t MaxCapacity() const
{
return maxCapacity;
}
bool Expand(size_t UNUSED(requiredCapacity))
{
return false;
}
private:
Allocator allocator;
size_t maxCapacity; // must be initialized before storage
void* storage;
};
// reserve and return a pointer to space at the end of storage,
// expanding it if need be.
// @param end total number of previously reserved bytes; will be
// increased by size if the allocation succeeds.
// @param size [bytes] to reserve.
// @return address of allocated space, or 0 if storage is full
// and cannot expand any further.
template<class Storage>
static inline uintptr_t StorageAppend(Storage& storage, size_t& end, size_t size)
{
size_t newEnd = end + size;
if(newEnd > storage.Capacity())
{
if(!storage.Expand(newEnd)) // NB: may change storage.Address()
return 0;
}
std::swap(end, newEnd);
return storage.Address() + newEnd;
}
// invoke operator() on default-constructed instantiations of
// Functor for reasonable combinations of Storage and their parameters.
template<template<class Storage> class Functor>
static void ForEachStorage()
{
Functor<Storage_Fixed<Allocator_Heap> >()();
Functor<Storage_Fixed<Allocator_Aligned<> > >()();
Functor<Storage_Reallocate<Allocator_Heap, Growth_Linear<> > >()();
Functor<Storage_Reallocate<Allocator_Heap, Growth_Exponential<> > >()();
Functor<Storage_Reallocate<Allocator_Aligned<>, Growth_Linear<> > >()();
Functor<Storage_Reallocate<Allocator_Aligned<>, Growth_Exponential<> > >()();
Functor<Storage_Commit<Allocator_AddressSpace<>, Growth_Linear<> > >()();
Functor<Storage_Commit<Allocator_AddressSpace<>, Growth_Exponential<> > >()();
Functor<Storage_AutoCommit<> >()();
}
} // namespace Allocators
#endif // #ifndef ALLOCATOR_POLICIES
|