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.

57 lines
1.5 KiB

  1. #pragma once
  2. #include "gp/utils/allocators/allocator.hpp"
  3. #include "gp/ipc/bottleneck.hpp"
  4. namespace gp {
  5. class bottleneck_allocator_front : public allocator {
  6. allocator& backend;
  7. fast_bottleneck lock;
  8. public:
  9. bottleneck_allocator_front(allocator& _backend)
  10. : backend(_backend)
  11. , lock()
  12. {}
  13. /**
  14. * @brief Allocates memory, THREAD SAFE
  15. *
  16. * @param sz the amount of bytes to allocate
  17. *
  18. * @return the allocated memory as a pointer on success
  19. * @return nullptr if it failed allocating
  20. */
  21. virtual void* allocate(size_t sz) {
  22. auto guard = lock_guard(lock);
  23. return backend.allocate(sz);
  24. }
  25. /**
  26. * @brief Deallocates memory, THREAD SAFE
  27. *
  28. * @param ptr the memory to deallocate
  29. *
  30. * @return true if the memory was successfully deallocated
  31. * @return false if the memory was not deallocated
  32. */
  33. virtual bool deallocate(void* ptr) {
  34. auto guard = lock_guard(lock);
  35. return backend.deallocate(ptr);
  36. }
  37. /**
  38. * @brief Tries to reallocate memory, THREAD SAFE
  39. *
  40. * @param ptr The memory to reallocate
  41. * @param sz The new size we want to give the memory
  42. *
  43. * @return true if reallocation was successful
  44. * @return false if the reallocation failed
  45. */
  46. virtual bool try_reallocate(void* ptr, size_t sz) {
  47. auto guard = lock_guard(lock);
  48. return backend.try_reallocate(ptr, sz);
  49. }
  50. virtual ~bottleneck_allocator_front() = default;
  51. };
  52. }