1#include "MemoryBuffer.h"
3#include "../StringView.h"
11std::string Cogs::Memory::MemToHexString(
const void* memory,
size_t size) {
12 const char* digits =
"0123456789ABCDEF";
14 const uint8_t* ptr =
static_cast<const uint8_t*
>(memory);
15 constexpr size_t bytesPerLine = 32;
19 output.reserve(size * 4);
21 for (; size >= bytesPerLine; size -= bytesPerLine, offset += bytesPerLine) {
22 snprintf(buffer,
sizeof(buffer),
"0x%08X: ", offset);
25 for (
size_t count = 0; count++ < bytesPerLine; ) {
26 output += digits[*ptr >> 4];
27 output += digits[*ptr++ & 0x0F];
30 if (!(count & 0x03)) {
37 snprintf(buffer,
sizeof(buffer),
"0x%08X: ", offset);
40 for (
size_t count = 0; size--; ) {
41 output += digits[*ptr >> 4];
42 output += digits[*ptr++ & 0x0F];
45 if (!(++count & 0x03)) {
54Cogs::Memory::MemoryBuffer::MemoryBuffer(
const void* memory,
size_t size) {
55 buffer =
const_cast<void*
>(memory);
62void Cogs::Memory::MemoryBuffer::reset(
size_t size, Allocator * allocator)
64 if (allocator == this->allocator) {
65 resize(size,
false,
false);
69 this->allocator = allocator;
70 resize(size,
false,
false);
74bool Cogs::Memory::MemoryBuffer::resize(
size_t size,
bool keep,
bool forceRealloc) {
77 if (currentSize == size) {
81 if (forceRealloc || (size > storageSize)) {
82 void* newBuffer =
nullptr;
85 newBuffer = allocator->allocate(size, 0, type);
91 std::memcpy(newBuffer, buffer, std::min(size, currentSize));
100 currentPos = std::min(currentPos, currentSize);
104bool Cogs::Memory::MemoryBuffer::reserve(
size_t size) {
107 if (size > storageSize) {
108 void* newBuffer = allocator->allocate(size, 0, type);
114 std::memcpy(newBuffer, buffer, currentSize);
115 allocator->deallocate(buffer, storageSize, type);
123void Cogs::Memory::MemoryBuffer::copy(
const MemoryBuffer & other) {
124 resize(other.size(),
false);
127 std::memcpy(buffer, other.buffer, currentSize);
132void Cogs::Memory::MemoryBuffer::swap(MemoryBuffer & other) {
133 std::swap(buffer, other.buffer);
134 std::swap(storageSize, other.storageSize);
135 std::swap(currentSize, other.currentSize);
136 std::swap(currentPos, other.currentPos);
137 std::swap(allocator, other.allocator);
138 std::swap(type, other.type);
141size_t Cogs::Memory::MemoryBuffer::read(
void* dest,
size_t noofbytes) {
142 size_t bytesRead = std::min(noofbytes, unreadSize());
144 std::memcpy(dest,
static_cast<uint8_t*
>(buffer) + currentPos, bytesRead);
145 currentPos += bytesRead;
150glm::vec2 Cogs::Memory::MemoryBuffer::readFloat2() {
158glm::vec3 Cogs::Memory::MemoryBuffer::readFloat3() {
161 for (
int i = 0; i < 3; ++i) {
167glm::vec4 Cogs::Memory::MemoryBuffer::readFloat4() {
170 for (
int i = 0; i < 4; ++i) {
176glm::quat Cogs::Memory::MemoryBuffer::readQuaternion() {
179 for (
int i = 0; i < 4; ++i) {
185glm::mat4 Cogs::Memory::MemoryBuffer::readMatrix() {
188 for (
int col = 0; col < 4; ++col) {
189 m[col] = readFloat4();
194std::string Cogs::Memory::MemoryBuffer::readString() {
195 size_t length = read32();
199 str.resize(--length);
200 read(str.data(), length);
210bool Cogs::Memory::MemoryBuffer::write(
const void* src,
size_t bytes) {
214 if ((currentSize + bytes) > storageSize) {
215 if (!reserve(std::max<size_t>(1024, std::max<size_t>(currentSize + bytes, storageSize * 3 / 2)))) {
219 memcpy(
static_cast<uint8_t*
>(buffer) + currentPos, src, bytes);
221 currentSize = currentPos;
225bool Cogs::Memory::MemoryBuffer::writeFloat2(
const glm::vec2& value) {
228 result &= writeFloat(value.x);
229 result &= writeFloat(value.y);
234bool Cogs::Memory::MemoryBuffer::writeFloat3(
const glm::vec3& value) {
237 for (
int i = 0; i < 3; ++i) {
238 result &= writeFloat(value[i]);
243bool Cogs::Memory::MemoryBuffer::writeFloat4(
const glm::vec4& value) {
246 for (
int i = 0; i < 4; ++i) {
247 result &= writeFloat(value[i]);
252bool Cogs::Memory::MemoryBuffer::writeQuaternion(
const glm::quat& value) {
255 for (
int i = 0; i < 4; ++i) {
256 result &= writeFloat(value[i]);
261bool Cogs::Memory::MemoryBuffer::writeMatrix(
const glm::mat4& value) {
264 for (
int col = 0; col < 4; ++col) {
265 result &= writeFloat4(value[col]);
270bool Cogs::Memory::MemoryBuffer::writeString(
const StringView& str) {
271 size_t length = str.size();
276 success &= write32(
static_cast<uint32_t
>(length + 1));
277 success &= write(str.data(), length);
278 success &= write8(0);
283void Cogs::Memory::MemoryBuffer::seek(int64_t offset, Anchor from) {
285 case Anchor::Start: currentPos = offset;
break;
286 case Anchor::Current: currentPos += offset;
break;
287 case Anchor::End: currentPos = currentSize + offset;
break;
289 currentPos = std::clamp<int64_t>(currentPos, 0, currentSize);
292bool Cogs::Memory::MemoryBuffer::areContentsEqual(
const MemoryBuffer& rhs)
const {
293 if (size() == rhs.size()) {
294 return !memcmp(data(), rhs.data(), size());
299void Cogs::Memory::MemoryBuffer::release() {
302 allocator->deallocate(buffer, storageSize, type);
310void Cogs::Memory::MemoryBuffer::changeType(MemBlockType newType)
312 if (buffer && allocator) {
313 allocator->changeType(buffer, storageSize, type, newType);