Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Segment.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Assert.h"
5#include "../Foundation/Span.h"
6#include "../Memory/Memory.h"
7#if SC_COMPILER_FILC
8#include <stdfil.h>
9#endif
10
11namespace SC
12{
15enum class SegmentAllocator
16{
17 Global = 0,
18 ThreadLocal = 1,
19};
20
21namespace detail
22{
23struct SC_MEMORY_EXPORT SegmentHeader;
24struct alignas(uint64_t) SegmentHeader
25{
26 static constexpr uint32_t MaxCapacity = (~static_cast<uint32_t>(0)) >> 1;
27
28 SegmentHeader(uint32_t capacity = 0, SegmentAllocator allocator = SegmentAllocator::Global)
29 {
30 sizeBytes = 0;
31 allocatorType = static_cast<uint32_t>(allocator);
32 capacityBytes = capacity;
33 hasInlineData = capacity > 0;
34 }
35 uint32_t sizeBytes : sizeof(uint32_t) * 8 - 1;
36 uint32_t allocatorType : 1;
37 uint32_t capacityBytes : sizeof(uint32_t) * 8 - 1;
38 uint32_t hasInlineData : 1;
39};
40
41struct SC_MEMORY_EXPORT SegmentHeaderOffset
42{
43 using PtrOffset = size_t;
44 SegmentHeader header;
45 PtrOffset offset = 0; // memory offset representing relative pointer to data from "this"
46};
47
48#if SC_COMPILER_FILC
49inline zexact_ptrtable* filcExactPtrTable() noexcept
50{
51 static zexact_ptrtable* table = zexact_ptrtable_new();
52 return table;
53}
54
55template <typename T>
56inline T* filcResolveRelativePointer(const void* self, size_t address) noexcept
57{
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));
63}
64#endif
65
66template <typename T>
67struct SC_MEMORY_EXPORT SegmentSelfRelativePointer : protected SegmentHeaderOffset
68{
69 // clang-format off
70 SC_COMPILER_FORCE_INLINE T* data() noexcept
71 {
72#if SC_COMPILER_FILC
73 if (offset == 0)
74 return nullptr;
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);
79#else
80 return offset == 0 ? nullptr : toPtr(toOffset(this) + offset);
81#endif
82 }
83 SC_COMPILER_FORCE_INLINE const T* data() const noexcept
84 {
85#if SC_COMPILER_FILC
86 if (offset == 0)
87 return nullptr;
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);
92#else
93 return offset == 0 ? nullptr : toPtr(toOffset(this) + offset);
94#endif
95 }
96 SC_COMPILER_FORCE_INLINE bool isInline() const noexcept { return (offset == inlineDataOffset()) and header.hasInlineData; }
97
98 protected:
99 struct InlineData : public SegmentHeaderOffset // Data layout corresponds to SmallBuffer, SmallVector etc.
100 {
101 uint64_t capacity; // Could use uint32_t but we need to align data to 64 bit anyway
102 ~InlineData() {}
103 union
104 {
105 T data[1]; // Accessing the whole class through volatile cast anyway so array size can be whatever
106 };
107 };
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); }
110 SC_COMPILER_FORCE_INLINE static T* toPtr(PtrOffset src) noexcept { return reinterpret_cast<T*>(src); }
111 SC_COMPILER_FORCE_INLINE void setData(T* mem) noexcept
112 {
113 offset = mem == nullptr ? 0 : toOffset(mem) - toOffset(this);
114#if SC_COMPILER_FILC
115 if (mem != nullptr and not (header.hasInlineData and mem == getInlineData()))
116 (void)zexact_ptrtable_encode(filcExactPtrTable(), mem);
117#endif
118 }
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; }
121 // clang-format on
122};
123
125template <typename T>
126struct SC_MEMORY_EXPORT SegmentTrivial
127{
128 using Type = T;
129 inline static void destruct(Span<T> data) noexcept;
130 // clang-format off
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;
137 // clang-format on
138 inline static void remove(Span<T> data, size_t numElements) noexcept;
139};
140
142template <typename ParentSegment, typename CommonParent, int N = 0,
143 SegmentAllocator Allocator = SegmentAllocator::ThreadLocal>
144struct SC_MEMORY_EXPORT SegmentCustom : public ParentSegment
145{
146 SegmentCustom() : ParentSegment(N, Allocator) {}
147 SegmentCustom(const CommonParent& other) : SegmentCustom() { CommonParent::operator=(other); }
148 SegmentCustom(CommonParent&& other) : SegmentCustom() { CommonParent::operator=(move(other)); }
149
150 SegmentCustom(const SegmentCustom& other) : SegmentCustom() { ParentSegment::operator=(other); }
151 SegmentCustom(SegmentCustom&& other) : SegmentCustom() { ParentSegment::operator=(move(other)); }
152 // clang-format off
153 SegmentCustom& operator=(const SegmentCustom& other) { ParentSegment::operator=(other); return *this; }
154 SegmentCustom& operator=(SegmentCustom&& other) { ParentSegment::operator=(move(other)); return *this; }
155 // clang-format on
156};
157} // namespace detail
158
165template <typename VTable>
166struct SC_MEMORY_EXPORT Segment : public VTable
167{
168 using VTable::data;
169 using T = typename VTable::Type;
170 Segment(uint32_t capacityInBytes, SegmentAllocator allocator = SegmentAllocator::Global) noexcept;
171
172 Segment() noexcept;
173 ~Segment() noexcept;
174 Segment(Segment&& other) noexcept;
175 Segment(const Segment& other) noexcept;
176 Segment& operator=(Segment&& other) noexcept;
177 Segment& operator=(const Segment& other) noexcept;
178
179 // clang-format off
180 template <typename U = T> Segment(Span<const U> span) noexcept : Segment() { SC_ASSERT_RELEASE(assign(span)); }
181 // clang-format on
182 Segment(std::initializer_list<T> list) noexcept;
183
186 [[nodiscard]] bool resizeWithoutInitializing(size_t newSize) noexcept;
187
189 [[nodiscard]] bool resize(size_t newSize, const T& value = T()) noexcept;
190
192 [[nodiscard]] bool reserve(size_t capacity) noexcept;
193
195 template <typename U = T>
196 [[nodiscard]] bool append(Span<const U> span) noexcept;
197
199 template <typename VTable2>
200 [[nodiscard]] bool appendMove(Segment<VTable2>&& other) noexcept;
201
204 [[nodiscard]] bool shrink_to_fit() noexcept;
205
207 void clear() noexcept;
208
211 template <typename U = T>
212 [[nodiscard]] bool assign(Span<const U> span) noexcept;
213
216 template <typename VTable2>
217 [[nodiscard]] bool assignMove(Segment<VTable2>&& other) noexcept;
218
220 [[nodiscard]] bool push_back(const T& value) noexcept { return resize(size() + 1, value); }
221
223 [[nodiscard]] bool push_back(T&& value) noexcept;
224
226 [[nodiscard]] bool push_front(const T& value) noexcept { return insert(0, value); }
227
231 [[nodiscard]] bool pop_back(T* removedValue = nullptr) noexcept;
232
236 [[nodiscard]] bool pop_front(T* removedValue = nullptr) noexcept;
237
238 // clang-format off
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(); }
243
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();}
248
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);}
251 // clang-format on
252
254 [[nodiscard]] bool isEmpty() const noexcept { return VTable::header.sizeBytes == 0; }
255
257 [[nodiscard]] Span<T> toSpan() noexcept SC_LANGUAGE_LIFETIME_BOUND { return {data(), size()}; }
258
260 [[nodiscard]] Span<const T> toSpanConst() const noexcept SC_LANGUAGE_LIFETIME_BOUND { return {data(), size()}; }
261
263 [[nodiscard]] size_t size() const noexcept { return VTable::header.sizeBytes / sizeof(T); }
264
266 [[nodiscard]] size_t capacity() const noexcept { return VTable::header.capacityBytes / sizeof(T); }
267
272 [[nodiscard]] bool removeRange(size_t start, size_t length) noexcept;
273
277 [[nodiscard]] bool removeAt(size_t index) noexcept { return removeRange(index, 1); }
278
283 [[nodiscard]] bool insert(size_t index, Span<const T> data) noexcept;
284
285 protected:
286 template <typename VTable2>
287 friend struct Segment;
288 struct Internal;
289};
290
292} // namespace SC
#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