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.

58 lines
1.3 KiB

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