[Glacier] Add buffer types for holding bytes and capabilities.

This commit is contained in:
Drew Galbraith 2023-10-24 12:35:37 -07:00
parent ca5361b847
commit d45f831b46
3 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,42 @@
#pragma once
#include <stdint.h>
#include "glacier/string/string.h"
namespace glcr {
class ByteBuffer {
public:
ByteBuffer(uint64_t size) : buffer_(new uint8_t[size]) {}
ByteBuffer(const ByteBuffer&) = delete;
ByteBuffer(ByteBuffer&&) = delete;
~ByteBuffer() { delete[] buffer_; }
template <typename T>
void WriteAt(uint64_t offset, const T& object) {
// FIXME: Add bounds check here.
*reinterpret_cast<T*>(buffer_ + offset) = object;
}
void WriteStringAt(uint64_t offset, const String& string) {
for (uint64_t i = 0; i < string.length(); i++) {
buffer_[offset + i] = string[i];
}
}
template <typename T>
const T& At(uint64_t offset) const {
return *reinterpret_cast<T*>(buffer_ + offset);
}
String StringAt(uint64_t offset, uint64_t length) const {
return String(reinterpret_cast<char*>(buffer_ + offset), length);
}
// private:
uint8_t* buffer_;
};
} // namespace glcr

View File

@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
namespace glcr {
// TODO: Hold cap type instead of uint64_t
class CapBuffer {
public:
CapBuffer(uint64_t size) : buffer_(new uint64_t[size]) {}
CapBuffer(const CapBuffer&) = delete;
CapBuffer(CapBuffer&&) = delete;
~CapBuffer() { delete[] buffer_; }
uint64_t At(uint64_t offset) const { return buffer_[offset]; }
void WriteAt(uint64_t offset, uint64_t cap) { buffer_[offset] = cap; }
private:
uint64_t* buffer_;
};
} // namespace glcr

View File

@ -5,4 +5,5 @@
[[nodiscard]] void* operator new[](uint64_t size) { return malloc(size); } [[nodiscard]] void* operator new[](uint64_t size) { return malloc(size); }
void operator delete(void*, uint64_t) {} void operator delete(void*, uint64_t) {}
void operator delete[](void*) {}
void operator delete[](void*, uint64_t) {} void operator delete[](void*, uint64_t) {}