10 #include <mm_malloc.h>
24 return {
nullptr, 0 };
33 block.
ptr = _mm_malloc(size, align);
51 template <std::
size_t TSize, Size TAlign = 16>
54 alignas(TAlign) uint8_t data[TSize];
63 const std::size_t actSize = roundToAlignment(size);
64 if (pos - data + actSize > TSize) {
77 if ((uint8_t*)block.ptr + block.size == pos) {
78 pos =
static_cast<uint8_t*
>(block.ptr);
84 return block.ptr >= data && block.ptr < data + TSize;
88 INLINE constexpr
static std::size_t roundToAlignment(
const std::size_t value) noexcept {
89 const std::size_t remainder = value % TAlign;
90 return value + ((remainder == 0) ? 0 : (TAlign - remainder));
100 template <
typename TPrimary,
typename TFallback>
104 MemoryBlock block = TPrimary::allocate(size, align);
108 return TFallback::allocate(size, align);
113 if (TPrimary::owns(block)) {
114 TPrimary::deallocate(block);
116 TFallback::deallocate(block);
131 template <std::
size_t TLimit,
typename TSmall,
typename TLarge>
135 if (size <= TLimit) {
136 return TSmall::allocate(size, align);
138 return TLarge::allocate(size, align);
143 if (block.size <= TLimit) {
144 TSmall::deallocate(block);
146 TLarge::deallocate(block);
151 if (block.size <= TLimit) {
152 return TSmall::owns(block);
154 return TLarge::owns(block);
168 template <
typename TAllocator>
171 std::size_t memory = 0;
175 MemoryBlock block = TAllocator::allocate(size, align);
184 memory -= block.size;
186 TAllocator::deallocate(block);
190 return TAllocator::owns(block);
206 template <
typename TResource>
209 TResource* resource =
nullptr;
213 resource = std::addressof(other);
218 return resource->allocate(size, align);
228 return resource->owns(block);
236 template <
typename TAllocator>
239 std::size_t position = 0;
243 resource = TAllocator::allocate(size, align);
247 TAllocator::deallocate(resource);
251 if (position + size <= resource.
size) {
253 block.
ptr = (uint8_t*)resource.
ptr + position;
263 return block.ptr >= resource.
ptr && block.ptr < (uint8_t*)resource.
ptr + resource.
size;
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
Allocator that attemps to allocate using given primary allocator, and if the allocation fails,...
INLINE const TFallback & fallback() const
INLINE void deallocate(MemoryBlock &block) noexcept
INLINE const TPrimary & primary() const
INLINE MemoryBlock allocate(const std::size_t size, const std::size_t align) noexcept
Object intended to only be constructed on stack.
Default allocator, simply wrapping _mm_malloc and _mm_free calls.
INLINE void deallocate(MemoryBlock &block) noexcept
INLINE MemoryBlock allocate(const std::size_t size, const std::size_t align) noexcept
Allocator that obtains memory blocks from given memory resource.
INLINE MemoryBlock allocate(const std::size_t size, const Size align) noexcept
INLINE void deallocate(MemoryBlock &UNUSED(block)) noexcept
INLINE bool owns(const MemoryBlock &block) const noexcept
INLINE void bind(TResource &other)
Simple memory resource with pre-allocated contiguous memory buffer.
INLINE bool owns(const MemoryBlock &block) const noexcept
~MonotonicMemoryResource()
INLINE MemoryBlock allocate(const std::size_t size, const std::size_t UNUSED(align)) noexcept
MonotonicMemoryResource(const std::size_t size, const std::size_t align)
Compositor that uses one allocator for small allocations and another allocator for large allocations.
INLINE void deallocate(MemoryBlock &block) noexcept
INLINE const TSmall & small() const
INLINE const TLarge & large() const
INLINE MemoryBlock allocate(const std::size_t size, const std::size_t align) noexcept
INLINE bool owns(const MemoryBlock &block) const noexcept
Allocator used pre-allocated fixed-size buffer on stack.
INLINE MemoryBlock allocate(const std::size_t size, const std::size_t UNUSED(align)) noexcept
INLINE void deallocate(MemoryBlock &block) noexcept
INLINE bool owns(const MemoryBlock &block) const noexcept
Helper allocator that keeps track on allocated memory.
INLINE void deallocate(MemoryBlock &block) noexcept
INLINE std::size_t allocated() const
INLINE MemoryBlock allocate(const std::size_t size, const std::size_t align) noexcept
INLINE bool owns(const MemoryBlock &block) const noexcept
INLINE const TAllocator & underlying() const
Object with deleted copy and move constructor and copy and move operator.
MemoryBlock(void *ptr, const std::size_t size)
static INLINE MemoryBlock EMPTY()