allocator.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <kernel/mem.h>
  3. #include <types/cplusplus.hpp>
  4. #include <types/stdint.h>
  5. #include <types/types.h>
  6. inline 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 value_type* allocate_memory(size_t count)
  29. {
  30. return static_cast<value_type*>(::k_malloc(count));
  31. }
  32. static 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 value_type* allocate_memory(size_t count)
  42. {
  43. return static_cast<value_type*>(::ki_malloc(count));
  44. }
  45. static void deallocate_memory(value_type* ptr)
  46. {
  47. ::ki_free(ptr);
  48. }
  49. };
  50. template <typename T, typename... Args>
  51. constexpr T* kernel_allocator_new(Args&&... args)
  52. {
  53. return allocator_traits<kernel_allocator<T>>::allocate_and_construct(forward<Args>(args)...);
  54. }
  55. template <typename T, typename... Args>
  56. constexpr T* kernel_ident_allocator_new(Args&&... args)
  57. {
  58. return allocator_traits<kernel_ident_allocator<T>>::allocate_and_construct(forward<Args>(args)...);
  59. }
  60. template <Allocator _allocator>
  61. class allocator_traits {
  62. public:
  63. using value_type = typename _allocator::value_type;
  64. static value_type* allocate(size_t count)
  65. {
  66. if (count == 0)
  67. return nullptr;
  68. return _allocator::allocate_memory(sizeof(value_type) * count);
  69. }
  70. template <typename... Args>
  71. static value_type* construct(value_type* ptr, Args&&... args)
  72. {
  73. new (ptr) value_type(forward<Args>(args)...);
  74. return ptr;
  75. }
  76. template <typename... Args>
  77. static value_type* allocate_and_construct(Args&&... args)
  78. {
  79. auto* ptr = allocate(1);
  80. construct(ptr, forward<Args>(args)...);
  81. return ptr;
  82. }
  83. static void deconstruct(value_type* ptr)
  84. {
  85. if (!ptr)
  86. return;
  87. ptr->~value_type();
  88. }
  89. static void deallocate(value_type* ptr)
  90. {
  91. if (!ptr)
  92. return;
  93. _allocator::deallocate_memory(ptr);
  94. }
  95. static void deconstruct_and_deallocate(value_type* ptr)
  96. {
  97. if (!ptr)
  98. return;
  99. deconstruct(ptr);
  100. deallocate(ptr);
  101. }
  102. };
  103. } // namespace types