4#include "../Foundation/Assert.h"
5#include "../Foundation/Span.h"
6#include "../Memory/Memory.h"
15enum class SegmentAllocator
23struct SC_MEMORY_EXPORT SegmentHeader;
24struct alignas(uint64_t) SegmentHeader
26 static constexpr uint32_t MaxCapacity = (~static_cast<uint32_t>(0)) >> 1;
28 SegmentHeader(uint32_t capacity = 0, SegmentAllocator allocator = SegmentAllocator::Global)
31 allocatorType =
static_cast<uint32_t>(allocator);
32 capacityBytes = capacity;
33 hasInlineData = capacity > 0;
41struct SC_MEMORY_EXPORT SegmentHeaderOffset
49inline zexact_ptrtable* filcExactPtrTable() noexcept
51 static zexact_ptrtable* table = zexact_ptrtable_new();
56inline T* filcResolveRelativePointer(
const void* self,
size_t address)
noexcept
58 const size_t lower =
reinterpret_cast<size_t>(zgetlower(
const_cast<void*
>(self)));
59 const size_t upper =
reinterpret_cast<size_t>(zgetupper(
const_cast<void*
>(self)));
60 if (address >= lower and address < upper)
61 return static_cast<T*
>(zmkptr(
const_cast<void*
>(self),
static_cast<unsigned long>(address)));
62 return static_cast<T*
>(zexact_ptrtable_decode(filcExactPtrTable(), address));
67struct SC_MEMORY_EXPORT SegmentSelfRelativePointer :
protected SegmentHeaderOffset
75 const auto address = toOffset(
this) + offset;
76 if (header.hasInlineData and offset == inlineDataOffset())
77 return static_cast<T*
>(zmkptr(
this,
static_cast<unsigned long>(address)));
78 return filcResolveRelativePointer<T>(
this, address);
80 return offset == 0 ? nullptr : toPtr(toOffset(
this) + offset);
88 const auto address = toOffset(
this) + offset;
89 if (header.hasInlineData and offset == inlineDataOffset())
90 return static_cast<const T*
>(zmkptr(
const_cast<SegmentSelfRelativePointer*
>(
this),
static_cast<unsigned long>(address)));
91 return filcResolveRelativePointer<const T>(
this, address);
93 return offset == 0 ? nullptr : toPtr(toOffset(
this) + offset);
96 SC_COMPILER_FORCE_INLINE bool isInline() const noexcept {
return (offset == inlineDataOffset()) and header.hasInlineData; }
99 struct InlineData :
public SegmentHeaderOffset
108 SC_COMPILER_FORCE_INLINE static constexpr PtrOffset inlineDataOffset() noexcept {
return sizeof(SegmentHeaderOffset) +
sizeof(uint64_t); }
109 SC_COMPILER_FORCE_INLINE static auto toOffset(
const volatile void* src)
noexcept {
return reinterpret_cast<PtrOffset
>(src); }
113 offset = mem ==
nullptr ? 0 : toOffset(mem) - toOffset(
this);
115 if (mem !=
nullptr and not (header.hasInlineData and mem == getInlineData()))
116 (void)zexact_ptrtable_encode(filcExactPtrTable(), mem);
119 SC_COMPILER_FORCE_INLINE T* getInlineData() noexcept {
return (T*)
reinterpret_cast<volatile InlineData*
>(
this)->data; }
120 SC_COMPILER_FORCE_INLINE auto getInlineCapacity() noexcept {
return reinterpret_cast<volatile InlineData*
>(
this)->capacity; }
126struct SC_MEMORY_EXPORT SegmentTrivial
129 inline static void destruct(Span<T> data)
noexcept;
131 template <
typename U>
inline static void copyConstructAs(Span<T> data, Span<const U> value)
noexcept;
132 template <
typename U>
inline static void copyConstruct(Span<T> data,
const U* src)
noexcept;
133 template <
typename U>
inline static void copyAssign(Span<T> data,
const U* src)
noexcept;
134 template <
typename U>
inline static void copyInsert(Span<T> data, Span<const U> values)
noexcept;
135 template <
typename U>
inline static void moveConstruct(Span<T> data, U* src)
noexcept;
136 template <
typename U>
inline static void moveAssign(Span<T> data, U* src)
noexcept;
138 inline static void remove(Span<T> data,
size_t numElements)
noexcept;
142template <
typename ParentSegment,
typename CommonParent,
int N = 0,
143 SegmentAllocator Allocator = SegmentAllocator::ThreadLocal>
144struct SC_MEMORY_EXPORT SegmentCustom :
public ParentSegment
146 SegmentCustom() : ParentSegment(N, Allocator) {}
147 SegmentCustom(
const CommonParent& other) : SegmentCustom() { CommonParent::operator=(other); }
148 SegmentCustom(CommonParent&& other) : SegmentCustom() { CommonParent::operator=(
move(other)); }
150 SegmentCustom(
const SegmentCustom& other) : SegmentCustom() { ParentSegment::operator=(other); }
151 SegmentCustom(SegmentCustom&& other) : SegmentCustom() { ParentSegment::operator=(
move(other)); }
153 SegmentCustom& operator=(
const SegmentCustom& other) { ParentSegment::operator=(other);
return *
this; }
154 SegmentCustom& operator=(SegmentCustom&& other) { ParentSegment::operator=(
move(other));
return *
this; }
165template <
typename VTable>
166struct SC_MEMORY_EXPORT
Segment :
public VTable
169 using T =
typename VTable::Type;
170 Segment(
uint32_t capacityInBytes, SegmentAllocator allocator = SegmentAllocator::Global)
noexcept;
182 Segment(std::initializer_list<T> list)
noexcept;
189 [[nodiscard]]
bool resize(
size_t newSize,
const T& value = T()) noexcept;
192 [[nodiscard]]
bool reserve(
size_t capacity) noexcept;
195 template <typename U = T>
196 [[nodiscard]]
bool append(
Span<const U> span) noexcept;
199 template <typename VTable2>
200 [[nodiscard]]
bool appendMove(
Segment<VTable2>&& other) noexcept;
204 [[nodiscard]]
bool shrink_to_fit() noexcept;
207 void clear() noexcept;
211 template <typename U = T>
212 [[nodiscard]]
bool assign(
Span<const U> span) noexcept;
216 template <typename VTable2>
217 [[nodiscard]]
bool assignMove(
Segment<VTable2>&& other) noexcept;
220 [[nodiscard]]
bool push_back(const T& value) noexcept {
return resize(size() + 1, value); }
226 [[nodiscard]]
bool push_front(
const T& value)
noexcept {
return insert(0, value); }
231 [[nodiscard]]
bool pop_back(T* removedValue =
nullptr) noexcept;
236 [[nodiscard]]
bool pop_front(T* removedValue =
nullptr) noexcept;
239 [[nodiscard]] T* begin() noexcept SC_LANGUAGE_LIFETIME_BOUND {
return data(); }
240 [[nodiscard]]
const T* begin() const noexcept SC_LANGUAGE_LIFETIME_BOUND {
return data(); }
241 [[nodiscard]] T* end() noexcept SC_LANGUAGE_LIFETIME_BOUND {
return data() + size(); }
242 [[nodiscard]]
const T* end() const noexcept SC_LANGUAGE_LIFETIME_BOUND {
return data() + size(); }
244 [[nodiscard]] T& back() noexcept SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_RELEASE(not isEmpty());
return *(data() + size() - 1);}
245 [[nodiscard]] T& front() noexcept SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_RELEASE(not isEmpty());
return *data();}
246 [[nodiscard]]
const T& back() const noexcept SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_RELEASE(not isEmpty());
return *(data() + size() - 1);}
247 [[nodiscard]]
const T& front() const noexcept SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_RELEASE(not isEmpty());
return *data();}
249 [[nodiscard]] T& operator[](
size_t idx)
noexcept SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_DEBUG(idx < size());
return *(data() + idx);}
250 [[nodiscard]]
const T& operator[](
size_t idx)
const noexcept SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_DEBUG(idx < size());
return *(data() + idx);}
254 [[nodiscard]]
bool isEmpty() const noexcept {
return VTable::header.sizeBytes == 0; }
257 [[nodiscard]]
Span<T> toSpan() noexcept SC_LANGUAGE_LIFETIME_BOUND {
return {data(), size()}; }
263 [[nodiscard]]
size_t size() const noexcept {
return VTable::header.sizeBytes /
sizeof(T); }
266 [[nodiscard]]
size_t capacity() const noexcept {
return VTable::header.capacityBytes /
sizeof(T); }
272 [[nodiscard]]
bool removeRange(
size_t start,
size_t length)
noexcept;
277 [[nodiscard]]
bool removeAt(
size_t index)
noexcept {
return removeRange(index, 1); }
286 template <
typename VTable2>
#define SC_ASSERT_DEBUG(e)
Assert expression e to be true.
Definition Assert.h:63
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:48
#define SC_COMPILER_FORCE_INLINE
Macro for forcing inline functions.
Definition Compiler.h:52
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:279
decltype(sizeof(0)) size_t
Platform independent unsigned size type.
Definition PrimitiveTypes.h:45
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:33
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition Segment.h:167
size_t size() const noexcept
Returns current size.
Definition Segment.h:263
bool resize(size_t newSize, const T &value=T()) noexcept
Re-allocates to the requested new size, preserving its contents and setting new items to value.
size_t capacity() const noexcept
Returns current capacity (always >= of size())
Definition Segment.h:266
Span< T > toSpan() noexcept SC_LANGUAGE_LIFETIME_BOUND
Obtains a Span of internal contents.
Definition Segment.h:257
bool isEmpty() const noexcept
Check if is empty (size() == 0)
Definition Segment.h:254
bool removeRange(size_t start, size_t length) noexcept
Removes the range [start, start + length] from the segment.
bool push_front(const T &value) noexcept
Appends a single element to the start of the segment.
Definition Segment.h:226
bool removeAt(size_t index) noexcept
Removes the element at index.
Definition Segment.h:277
bool pop_back(T *removedValue=nullptr) noexcept
Removes the last element of the segment.
bool push_back(T &&value) noexcept
Moves a single element to the end of the segment.
bool insert(size_t index, Span< const T > data) noexcept
Insert a span at the given index.
bool resizeWithoutInitializing(size_t newSize) noexcept
Re-allocates to the requested new size, preserving its contents.
Span< const T > toSpanConst() const noexcept SC_LANGUAGE_LIFETIME_BOUND
Obtains a Span of internal contents.
Definition Segment.h:260
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29