28template<
typename T,
size_t ALIGNMENT = 0>
class MemBuffer
44 : dat(static_cast<T*>(my_malloc(
size * sizeof(T))))
81 [[nodiscard]]
const T*
data()
const {
return dat; }
82 [[nodiscard]] T*
data() {
return dat; }
103 [[nodiscard]]
bool empty()
const {
return !dat; }
114 dat =
static_cast<T*
>(my_realloc(dat,
size *
sizeof(T)));
153 static constexpr bool SIMPLE_MALLOC = ALIGNMENT <=
alignof(std::max_align_t);
155 [[nodiscard]]
void* my_malloc(
size_t bytes)
158 if constexpr (SIMPLE_MALLOC) {
159 result = malloc(bytes);
160 if (!result && bytes)
throw std::bad_alloc();
168 void my_free(
void* p)
170 if constexpr (SIMPLE_MALLOC) {
177 [[nodiscard]]
void* my_realloc(
void* old,
size_t bytes)
180 if constexpr (SIMPLE_MALLOC) {
181 result = realloc(old, bytes);
182 if (!result && bytes)
throw std::bad_alloc();
185 if (!result && bytes)
throw std::bad_alloc();
This class manages the lifetime of a block of memory.
~MemBuffer()
Free the memory buffer.
MemBuffer()
Construct an empty MemBuffer, no memory is allocated.
void resize(size_t size)
Grow or shrink the memory block.
MemBuffer(size_t size)
Construct a (uninitialized) memory buffer of given size.
bool empty() const
No memory allocated?
MemBuffer(MemBuffer &&other) noexcept
Move constructor.
void swap(MemBuffer &other) noexcept
Swap the managed memory block of two MemBuffers.
MemBuffer & operator=(MemBuffer &&other) noexcept
Move assignment.
const T * data() const
Returns pointer to the start of the memory buffer.
const T & operator[](size_t i) const
Access elements in the memory buffer.
void clear()
Free the allocated memory block and set the current size to 0.
void * mallocAligned(size_t alignment, size_t size)
This file implemented 3 utility functions:
void swap(openmsx::MemBuffer< T > &l, openmsx::MemBuffer< T > &r) noexcept
size_t size(std::string_view utf8)