|
|
@@ -8,7 +8,7 @@ namespace saura {
|
|
|
MemoryArena::MemoryArena() {
|
|
|
this->data = (uint8_t *)std::malloc(DEFAULT_ARENA_SIZE);
|
|
|
if (this->data == nullptr) {
|
|
|
- spdlog::critical("Failed malloc!");
|
|
|
+ spdlog::critical("Failed malloc!");
|
|
|
}
|
|
|
|
|
|
this->size = DEFAULT_ARENA_SIZE;
|
|
|
@@ -18,13 +18,13 @@ MemoryArena::MemoryArena() {
|
|
|
MemoryArena::MemoryArena(uint64_t size) {
|
|
|
this->data = (uint8_t *)std::malloc(size);
|
|
|
if (this->data == nullptr) {
|
|
|
- spdlog::critical("Failed malloc!");
|
|
|
+ spdlog::critical("Failed malloc!");
|
|
|
}
|
|
|
this->size = size;
|
|
|
this->offset = 0;
|
|
|
}
|
|
|
|
|
|
-MemoryArena::~MemoryArena() { }
|
|
|
+MemoryArena::~MemoryArena() {}
|
|
|
|
|
|
uint8_t *MemoryArena::push(uint64_t size) {
|
|
|
if (this->offset + size > this->size) {
|
|
|
@@ -53,27 +53,43 @@ uint8_t *MemoryArena::pop(uint64_t size) {
|
|
|
return pos;
|
|
|
}
|
|
|
|
|
|
+uint8_t *MemoryArena::pop_zero(uint64_t size) {
|
|
|
+ if (this->offset == 0) {
|
|
|
+ spdlog::critical("Handle out-of-memory");
|
|
|
+ }
|
|
|
+
|
|
|
+ std::memset((uint8_t *)this->data + this->offset - size, 0, size);
|
|
|
+
|
|
|
+ this->offset -= size;
|
|
|
+ uint8_t *pos = (uint8_t *)this->offset - size;
|
|
|
+ return pos;
|
|
|
+}
|
|
|
+
|
|
|
void MemoryArena::clear() { this->offset = 0; }
|
|
|
|
|
|
uint64_t MemoryArena::get_offset() { return this->offset; }
|
|
|
|
|
|
-MemoryArenaTemp MemoryArenaTemp::begin(MemoryArena *arena) {
|
|
|
- MemoryArenaTemp result;
|
|
|
- std::memset(&result, 0, sizeof(result));
|
|
|
- result.arena = arena;
|
|
|
- result.offset = arena->offset;
|
|
|
- return result;
|
|
|
+void MemoryArenaTemp::begin(MemoryArena *arena) {
|
|
|
+ this->arena = arena;
|
|
|
+ this->offset = arena->offset;
|
|
|
+}
|
|
|
+
|
|
|
+void MemoryArenaTemp::end() { this->arena->offset = this->offset; }
|
|
|
+
|
|
|
+uint8_t *MemoryArenaTemp::push(uint64_t size) {
|
|
|
+ return this->arena->push(size);
|
|
|
+}
|
|
|
+
|
|
|
+uint8_t *MemoryArenaTemp::push_zero(uint64_t size) {
|
|
|
+ return this->arena->push_zero(size);
|
|
|
}
|
|
|
|
|
|
-void MemoryArenaTemp::end(MemoryArenaTemp temp) {
|
|
|
- temp.arena->offset = temp.offset;
|
|
|
+uint8_t *MemoryArenaTemp::pop() {
|
|
|
+ return this->arena->pop(this->arena->offset - this->offset);
|
|
|
}
|
|
|
|
|
|
-MemoryArenaTemp MemoryArenaTemp::get_scratch(MemoryArena *arena) {
|
|
|
- MemoryArenaTemp temp;
|
|
|
- std::memset(&temp, 0, sizeof(temp));
|
|
|
- temp.arena = arena;
|
|
|
- temp.offset = arena->offset;
|
|
|
- return temp;
|
|
|
+uint8_t *MemoryArenaTemp::pop_zero() {
|
|
|
+ return this->arena->pop_zero(this->arena->offset - this->offset);
|
|
|
}
|
|
|
+
|
|
|
} // namespace saura
|