General Purpose library for Freestanding C++ and POSIX systems
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

60 řádky
1.6 KiB

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