allocator.hpp 920 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include <memory>
  3. #include <new>
  4. #include <utility>
  5. #include <type_traits>
  6. #include <cstddef>
  7. #include <stdint.h>
  8. #include <types/cplusplus.hpp>
  9. #include <types/types.h>
  10. #include <kernel/async/lock.hpp>
  11. namespace types::memory {
  12. class brk_memory_allocator {
  13. public:
  14. using byte = std::byte;
  15. using size_type = std::size_t;
  16. private:
  17. byte* p_start;
  18. byte* p_limit;
  19. byte* p_break;
  20. kernel::async::mutex mtx;
  21. constexpr byte* brk(byte* addr)
  22. {
  23. if (addr >= p_limit) [[unlikely]]
  24. return nullptr;
  25. return p_break = addr;
  26. }
  27. constexpr byte* sbrk(size_type increment)
  28. { return brk(p_break + increment); }
  29. public:
  30. explicit brk_memory_allocator(byte* start, size_type size);
  31. brk_memory_allocator(const brk_memory_allocator&) = delete;
  32. void* allocate(size_type size);
  33. void deallocate(void* ptr);
  34. };
  35. } // namespace types::memory