allocator.hpp 970 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include <cstddef>
  3. #include <memory>
  4. #include <new>
  5. #include <type_traits>
  6. #include <utility>
  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. byte* p_allocated;
  21. kernel::async::mutex mtx;
  22. byte* brk(byte* addr);
  23. byte* sbrk(size_type increment);
  24. constexpr byte* sbrk() const noexcept { return p_break; }
  25. public:
  26. explicit brk_memory_allocator(byte* start, size_type size);
  27. brk_memory_allocator(const brk_memory_allocator&) = delete;
  28. void* allocate(size_type size);
  29. void deallocate(void* ptr);
  30. bool allocated(void* ptr) const noexcept;
  31. };
  32. } // namespace types::memory
  33. namespace kernel::kinit {
  34. void init_allocator();
  35. } // namespace kernel::kinit