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.
 
 

61 lines
1.6 KiB

#pragma once
#include "gp/utils/allocators/allocator.hpp"
#include "gp/ipc/bottleneck.hpp"
namespace gp {
/**
* @brief A front to make allocators thread safe
*/
class bottleneck_allocator_front : public allocator {
allocator& backend;
fast_bottleneck lock;
public:
bottleneck_allocator_front(allocator& _backend)
: backend(_backend)
, lock()
{}
/**
* @brief Allocates memory, THREAD SAFE
*
* @param sz the amount of bytes to allocate
*
* @return the allocated memory as a pointer on success
* @return nullptr if it failed allocating
*/
virtual void* allocate(size_t sz) {
auto guard = lock_guard(lock);
return backend.allocate(sz);
}
/**
* @brief Deallocates memory, THREAD SAFE
*
* @param ptr the memory to deallocate
*
* @return true if the memory was successfully deallocated
* @return false if the memory was not deallocated
*/
virtual bool deallocate(void* ptr) {
auto guard = lock_guard(lock);
return backend.deallocate(ptr);
}
/**
* @brief Tries to reallocate memory, THREAD SAFE
*
* @param ptr The memory to reallocate
* @param sz The new size we want to give the memory
*
* @return true if reallocation was successful
* @return false if the reallocation failed
*/
virtual bool try_reallocate(void* ptr, size_t sz) {
auto guard = lock_guard(lock);
return backend.try_reallocate(ptr, sz);
}
virtual ~bottleneck_allocator_front() = default;
};
}