General Purpose library for Freestanding C++ and POSIX systems
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

56 строки
1.3 KiB

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