allocator.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #include <kernel/mem.h>
  3. #include <stdint.h>
  4. #include <types/cplusplus.hpp>
  5. #include <types/types.h>
  6. constexpr void* operator new(size_t, void* ptr)
  7. {
  8. return ptr;
  9. }
  10. namespace types {
  11. template <typename T>
  12. concept Allocator = requires(size_t size, typename T::value_type* ptr)
  13. {
  14. typename T::value_type;
  15. {
  16. T::allocate_memory(size)
  17. } -> same_as<typename T::value_type*>;
  18. {
  19. T::deallocate_memory(ptr)
  20. } -> same_as<void>;
  21. };
  22. template <Allocator T>
  23. class allocator_traits;
  24. template <typename T>
  25. class kernel_allocator {
  26. public:
  27. using value_type = T;
  28. static constexpr value_type* allocate_memory(size_t count)
  29. {
  30. return static_cast<value_type*>(::k_malloc(count));
  31. }
  32. static constexpr void deallocate_memory(value_type* ptr)
  33. {
  34. ::k_free(ptr);
  35. }
  36. };
  37. template <typename T>
  38. class kernel_ident_allocator {
  39. public:
  40. using value_type = T;
  41. static constexpr value_type* allocate_memory(size_t count)
  42. {
  43. return static_cast<value_type*>(::ki_malloc(count));
  44. }
  45. static constexpr void deallocate_memory(value_type* ptr)
  46. {
  47. ::ki_free(ptr);
  48. }
  49. };
  50. template <template <typename _T> class Allocator, typename T, typename... Args>
  51. constexpr T* _new(Args&&... args)
  52. {
  53. return allocator_traits<Allocator<T>>::allocate_and_construct(forward<Args>(args)...);
  54. }
  55. template <template <typename _T> class Allocator, typename T, typename... Args>
  56. constexpr T* pnew(T* = nullptr, Args&&... args)
  57. {
  58. return _new<Allocator, T, Args...>(forward<Args>(args)...);
  59. }
  60. template <template <typename _T> class Allocator, typename T>
  61. constexpr void pdelete(T* ptr)
  62. {
  63. allocator_traits<Allocator<T>>::deconstruct_and_deallocate(ptr);
  64. }
  65. template <Allocator _allocator>
  66. class allocator_traits {
  67. public:
  68. using value_type = typename _allocator::value_type;
  69. static constexpr value_type* allocate(size_t count)
  70. {
  71. if (count == 0)
  72. return nullptr;
  73. return _allocator::allocate_memory(sizeof(value_type) * count);
  74. }
  75. template <typename... Args>
  76. static constexpr value_type* construct(value_type* ptr, Args&&... args)
  77. {
  78. new (ptr) value_type(forward<Args>(args)...);
  79. return ptr;
  80. }
  81. template <typename... Args>
  82. static constexpr value_type* allocate_and_construct(Args&&... args)
  83. {
  84. auto* ptr = allocate(1);
  85. construct(ptr, forward<Args>(args)...);
  86. return ptr;
  87. }
  88. static constexpr void deconstruct(value_type* ptr)
  89. {
  90. if (!ptr)
  91. return;
  92. ptr->~value_type();
  93. }
  94. static constexpr void deallocate(value_type* ptr)
  95. {
  96. if (!ptr)
  97. return;
  98. _allocator::deallocate_memory(ptr);
  99. }
  100. static constexpr void deconstruct_and_deallocate(value_type* ptr)
  101. {
  102. if (!ptr)
  103. return;
  104. deconstruct(ptr);
  105. deallocate(ptr);
  106. }
  107. };
  108. } // namespace types