Files

57 lines
1.2 KiB
C++

#ifndef FREERTOS_CPP_FREE_RTOS_ALLOCATOR_HPP
#define FREERTOS_CPP_FREE_RTOS_ALLOCATOR_HPP
#include <stddef.h>
#include <stdint.h>
#include "freertos/memory_resource.hpp"
namespace freertos
{
template< typename T >
class FreeRtosAllocator final
{
public:
explicit constexpr FreeRtosAllocator( MemoryResource resource ) noexcept
: resource_ { resource }
{
}
T * try_allocate( size_t count ) const noexcept
{
if( count == 0U || count > SIZE_MAX / sizeof( T ) )
{
return nullptr;
}
return static_cast< T * >(
resource_.allocate_bytes( count * sizeof( T ), alignof( T ) ) );
}
void deallocate( T * memory, size_t count ) const noexcept
{
if( memory != nullptr && count <= SIZE_MAX / sizeof( T ) )
{
resource_.deallocate_bytes(
memory, count * sizeof( T ), alignof( T ) );
}
}
static constexpr bool count_overflows( size_t count ) noexcept
{
return count > SIZE_MAX / sizeof( T );
}
constexpr MemoryResource resource() const noexcept
{
return resource_;
}
private:
MemoryResource resource_;
};
} // namespace freertos
#endif