Advertisement
ulfben

Arena allocator

Jan 14th, 2023
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1.  
  2. //https://lesleylai.info/en/std-align
  3.  
  4. struct Arena {
  5.   void* ptr = 0;
  6.   std::size_t size_remain = 0;
  7.  
  8.   [[nodiscard]]
  9.   auto aligned_alloc(std::size_t alignment, std::size_t size) noexcept -> void*
  10.   {
  11.     void* res = std::align(alignment, size, ptr, size_remain);
  12.     if (res) {
  13.         ptr = static_cast<std::byte*>(res) + size;
  14.         size_remain -= size;
  15.         return res;
  16.     }
  17.     return nullptr;
  18.   }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement