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

61 рядки
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;
};
}