General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 rivejä
1.2 KiB

  1. #pragma once
  2. #include "gp_config.hpp"
  3. #include "gp/exception.hpp"
  4. #include <type_traits>
  5. namespace gp{
  6. template<class T, typename I = std::enable_if_t<gp_config::memory_module::is_ok,int>>
  7. class default_memory_allocator
  8. {
  9. public:
  10. using pointer_type = T*;
  11. using reference_type = T&;
  12. using const_pointer_type = const T*;
  13. using const_reference_type = const T&;
  14. pointer_type allocate(size_t sz)
  15. {
  16. return reinterpret_cast <pointer_type> (gp_config::memory_module::memory_allocator<gp_config::memory_module::memory_mode>(sizeof(T) * sz));
  17. }
  18. void deallocate(pointer_type ptr)
  19. {
  20. gp_config::memory_module::memory_deallocator<gp_config::memory_module::memory_mode>(ptr);
  21. }
  22. template<typename ...params>
  23. pointer_type construct(pointer_type ptr, params... args)
  24. {
  25. new(ptr) T(args...);
  26. return ptr;
  27. }
  28. void destroy(pointer_type v)
  29. {
  30. v->~T();
  31. }
  32. };
  33. }
  34. void* operator new(size_t sz)
  35. {
  36. auto ptr = gp_config::memory_module::memory_allocator<gp_config::memory_module::memory_mode>(sz);
  37. if constexpr (gp_config::has_exceptions)
  38. {
  39. if(!ptr)
  40. {
  41. throw gp::bad_alloc{};
  42. }
  43. }
  44. return ptr;
  45. }
  46. void operator delete (void* ptr) noexcept
  47. {
  48. gp_config::memory_module::memory_deallocator<gp_config::memory_module::memory_mode>(ptr);
  49. }