libER 0.1.4.2
ELDEN RING API library
Loading...
Searching...
No Matches
from_allocator.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#if defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0
11#error "_ITERATOR_DEBUG_LEVEL" must be defined as "0" for STL containers to be compatible with the ELDEN RING ABI.
12#endif
13
14#include <detail/preprocessor.hpp>
15
16#include <bitset>
17#include <cstddef>
18#include <cstdint>
19#include <memory>
20#include <utility>
21
22// For std::terminate
23#include <stdexcept>
24
25namespace from {
26namespace DLKR {
43public:
44 virtual ~DLAllocator() = default;
45
51 virtual int _allocator_id() = 0;
52
59 virtual int _unused() {
60 return -1;
61 }
62
70 virtual std::bitset<8> heap_flags() = 0;
71
77 virtual size_t heap_capacity() = 0;
78
84 virtual size_t heap_size() = 0;
85
91 virtual size_t backing_heap_capacity() = 0;
92
99 virtual size_t heap_allocation_count() = 0;
100
107 virtual size_t msize(void* p) = 0;
108
117 virtual void* allocate(size_t cb) = 0;
118
129 virtual void* allocate_aligned(size_t cb, size_t alignment) = 0;
130
141 virtual void* reallocate(void* p, size_t cb) = 0;
142
154 virtual void* reallocate_aligned(void* p, size_t cb, size_t alignment) = 0;
155
161 virtual void deallocate(void* p) = 0;
162
168 virtual void _unsupported() {
169 std::terminate();
170 }
171
181 virtual void* allocate_second(size_t cb) = 0;
182
194 virtual void* allocate_second_aligned(size_t cb, size_t alignment) = 0;
195
207 virtual void* reallocate_second(void* p, size_t cb) = 0;
208
221 virtual void* reallocate_second_aligned(void* p, size_t cb,
222 size_t alignment) = 0;
223
230 virtual void deallocate_second(void* p) = 0;
231
236 virtual bool _unknown_bool() {
237 return false;
238 }
239
245 virtual bool belongs_to_first(void* p) = 0;
246
252 virtual bool belongs_to_second(void* p) = 0;
253
258 virtual void lock() {}
259
264 virtual void unlock() {}
265
274 virtual void* get_memory_block(void* p) = 0;
275};
276
277struct DLBackAllocator : public DLAllocator {};
278} // namespace DLKR
279
286
295
296template <typename AllocatorTag>
298
309template <typename T, typename AllocatorTag = default_allocator_tag>
310class allocator : private allocator_base<AllocatorTag> {
313 using alloc_type = std::conditional_t<std::is_same_v<T, void>, char, T>;
314
315 base_type& base() noexcept {
316 return static_cast<base_type&>(*this);
317 }
318 const base_type& base() const noexcept {
319 return static_cast<const base_type&>(*this);
320 }
321
322 proxy_type& proxy() noexcept {
323 return this->base().get_allocator();
324 }
325 const proxy_type& proxy() const noexcept {
326 return this->base().get_allocator();
327 }
328
329public:
334 using value_type = T;
335
340 using size_type = size_t;
341
346 using difference_type = ptrdiff_t;
347
353
359 using is_always_equal = std::false_type;
360
361 allocator() noexcept : base_type() {}
362
371 template <typename U>
372 allocator(const allocator<U>& other) noexcept : base_type(other.base()) {}
373
379 allocator(DLKR::DLAllocator* dl_allocator) noexcept
380 : base_type(dl_allocator) {}
381
388 [[nodiscard]] T* allocate(size_type n) {
389 return reinterpret_cast<T*>(this->proxy().allocate_aligned(
390 n * sizeof(alloc_type), alignof(alloc_type)));
391 }
392
399 void deallocate(T* p, size_type n = 0) {
400 this->proxy().deallocate((void*)p);
401 }
402
407 template <typename T1, typename T2>
408 friend bool operator==(const allocator<T1>& lhs,
409 const allocator<T2>& rhs) noexcept;
410};
411
416template <typename T1, typename T2>
417bool operator==(const allocator<T1>& lhs, const allocator<T2>& rhs) noexcept {
418 return std::addressof(lhs.proxy()) == std::addressof(rhs.proxy());
419}
420
429template <>
432
433public:
435 LIBERAPI allocator_base() noexcept;
436
437 allocator_base(DLKR::DLAllocator* dl_allocator) noexcept
438 : allocator(dl_allocator) {}
439
440 DLKR::DLAllocator& get_allocator() const noexcept {
441 return *this->allocator;
442 }
444};
445
454template <>
456public:
458 allocator_base() noexcept = default;
459
460 allocator_base(DLKR::DLAllocator* dl_allocator) noexcept {}
461
462 LIBERAPI DLKR::DLAllocator& get_allocator() const noexcept;
464};
465} // namespace from
Common ELDEN RING allocator interface.
Definition from_allocator.hpp:42
virtual void unlock()
Unlock the allocator's mutex (if present and accessible).
Definition from_allocator.hpp:264
virtual void * reallocate(void *p, size_t cb)=0
Reallocate a memory block with a new size.
virtual void * allocate_second_aligned(size_t cb, size_t alignment)=0
Allocate a block of at least this many bytes with a given alignment. Use the second allocator if it i...
virtual size_t heap_allocation_count()=0
Total number of objects that have been allocated on the allocator's heap.
virtual int _allocator_id()=0
Unique allocator id sometimes used for comparing allocators.
virtual void * allocate_aligned(size_t cb, size_t alignment)=0
Allocate a block of at least this many bytes with a given alignment.
virtual bool _unknown_bool()
Unknown method, seemingly unused.
Definition from_allocator.hpp:236
virtual void * get_memory_block(void *p)=0
Get the memory block base that this memory belongs to.
virtual void * reallocate_second(void *p, size_t cb)=0
Reallocate a memory block with a new size. Use the second allocator if it is bound,...
virtual void deallocate_second(void *p)=0
Free previously allocated memory. Use the second allocator if it is bound, first if not.
virtual void * allocate(size_t cb)=0
Allocate a block of at least this many bytes.
virtual bool belongs_to_second(void *p)=0
Does memory block belong to the second bound allocator?
virtual size_t heap_size()=0
How many bytes out of the total capacity are allocated.
virtual void _unsupported()
Unknown method, isn't supported by any class that implements DLKR::DLAllocator.
Definition from_allocator.hpp:168
virtual size_t backing_heap_capacity()=0
The remaining capacity of the backing heap.
virtual void deallocate(void *p)=0
Free previously allocated memory.
virtual std::bitset< 8 > heap_flags()=0
Allocator and heap compatibility flags.
virtual bool belongs_to_first(void *p)=0
Does memory block belong to the first bound allocator?
virtual int _unused()
Unknown function which is defined in the interface, never overriden or used.
Definition from_allocator.hpp:59
virtual size_t msize(void *p)=0
Check how big a given memory block is.
virtual void * allocate_second(size_t cb)=0
Allocate a block of at least this many bytes. Use the second allocator if it is bound,...
virtual void * reallocate_aligned(void *p, size_t cb, size_t alignment)=0
Reallocate an aligned memory block with a new size.
virtual void * reallocate_second_aligned(void *p, size_t cb, size_t alignment)=0
Reallocate an aligned memory block with a new size. Use the second allocator if it is bound,...
virtual void lock()
Lock the allocator's mutex (if present and accessible).
Definition from_allocator.hpp:258
virtual size_t heap_capacity()=0
The total capacity of the heap, in bytes.
Definition from_allocator.hpp:297
The main libER stand-in for ER allocator proxies.
Definition from_allocator.hpp:310
allocator(DLKR::DLAllocator *dl_allocator) noexcept
Wrap an existing DLKR::DLAllocator.
Definition from_allocator.hpp:379
size_t size_type
Memory size type.
Definition from_allocator.hpp:340
T value_type
The allocated value type.
Definition from_allocator.hpp:334
ptrdiff_t difference_type
Memory difference type.
Definition from_allocator.hpp:346
allocator(const allocator< U > &other) noexcept
Allocator copy constructor.
Definition from_allocator.hpp:372
std::false_type is_always_equal
This allocator may proxy different stateful allocators.
Definition from_allocator.hpp:359
std::true_type propagate_on_container_move_assignment
This allocator is move assigned along with the contents.
Definition from_allocator.hpp:352
void deallocate(T *p, size_type n=0)
Deallocate previously allocated memory.
Definition from_allocator.hpp:399
T * allocate(size_type n)
Allocate n instances of uninitialized memory for T.
Definition from_allocator.hpp:388
friend bool operator==(const allocator< T1 > &lhs, const allocator< T2 > &rhs) noexcept
Allocator equality comparison friend declaration.
Definition from_allocator.hpp:417
Definition from_allocator.hpp:277
Default libER allocator that gets embedded in objects as needed for ER ABI compatibility.
Definition from_allocator.hpp:285
Default libER allocator with an empty base.
Definition from_allocator.hpp:294