0xc3 1 lună în urmă
părinte
comite
f2a72bc280

+ 13 - 7
src/saura/cmd/sandbox/sandbox.cpp

@@ -26,14 +26,16 @@ struct SandboxApp : AppBase {
   std::vector<ClientInfo> clients;
   std::vector<UserInfo> users;
   Folder category_root;
-  std::unique_ptr<MemoryArena> default_memory_arena;
+  std::unique_ptr<MemoryArena> def_mem_arena;
+  std::unique_ptr<MemoryArenaTemp> mem_arena_temp;
 
   void start() override {
     spdlog::info("Sandbox start!");
 
-    // TODO: FIX FATAL
-    this->default_memory_arena =
-        std::make_unique<MemoryArena>(MemoryArena(Megabytes(256)));
+    this->def_mem_arena = std::make_unique<MemoryArena>();
+    this->mem_arena_temp = std::make_unique<MemoryArenaTemp>();
+
+    this->def_mem_arena->push_zero(Kilobytes(1024));
 
     this->server_api_ctx = std::make_shared<ServerAPI>();
     this->clients = server_api_ctx->fetch_clients(10, 0);
@@ -266,14 +268,20 @@ struct SandboxApp : AppBase {
           }
 
           // ФИО
+          this->mem_arena_temp->begin(this->def_mem_arena.get());
+          uint8_t *buf = this->mem_arena_temp->push(1024);
           {
-            uint8_t *buf = this->default_memory_arena->push_zero(1024);
+
             ImGui::InputText("ФИО", (char *)buf, sizeof(buf));
           }
 
           if (ImGui::Button("Close", ImVec2(120, 0))) {
+            spdlog::info("ФИО: {}", (char *)buf);
+            this->mem_arena_temp->pop_zero();
             ImGui::CloseCurrentPopup();
           }
+
+          this->mem_arena_temp->end();
           ImGui::EndPopup();
         }
 
@@ -454,8 +462,6 @@ struct SandboxApp : AppBase {
     ImGui::End();
 
     ImGui::ShowDemoWindow();
-
-    this->default_memory_arena->clear();
   }
 
   void stop() override { spdlog::info("Sandbox stop!"); }

+ 33 - 17
src/saura/core/memory/arena.cpp

@@ -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

+ 8 - 14
src/saura/core/memory/arena.hpp

@@ -27,30 +27,24 @@ public:
   uint8_t *push(uint64_t size);
   uint8_t *push_zero(uint64_t size);
   uint8_t *pop(uint64_t size);
+  uint8_t *pop_zero(uint64_t size);
   void clear();
   uint64_t get_offset();
 };
 
-// Helpers
-
-// #define push_array(arena, type, count) \
-//   (type *)arena_push(arena, sizeof(type) * count)
-// #define push_array_zero(arena, type, count) \
-//   (type *)arena_push_zero(arena, sizeof(type) * count)
-// #define push_struct(arena, type) (type *)push_array(arena, type, 1)
-// #define push_struct_zero(arena, type) (type *)push_array_zero(arena, type, 1)
-// #define pop_array(arena, type, count) \
-//   (type *)arena_pop(arena, sizeof(type) * count)
-
 class MemoryArenaTemp {
  public:
   MemoryArena *arena;
   uint64_t offset;
 
 public:
-  MemoryArenaTemp begin(MemoryArena *arena);
-  void end(MemoryArenaTemp temp);
-  MemoryArenaTemp get_scratch(MemoryArena *arena);
+  void begin(MemoryArena *arena);
+  void end();
+
+  uint8_t *push(uint64_t size);
+  uint8_t *push_zero(uint64_t size);
+  uint8_t *pop();
+  uint8_t *pop_zero();
 };
 } // namespace saura