feat: add lab-rv32i-freertos-allocator-resource card

This commit is contained in:
user
2026-07-21 19:13:55 +02:00
commit 5432095596
65 changed files with 34038 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/* Hazard3 testbench: mtime advances once per simulated cycle. */
#define configCPU_CLOCK_HZ ( 1000000UL )
#define configTICK_RATE_HZ ( 1000U )
#define configMTIME_BASE_ADDRESS ( 0xC0000100UL )
#define configMTIMECMP_BASE_ADDRESS ( 0xC0000108UL )
#define configUSE_PREEMPTION 1
#define configUSE_TIME_SLICING 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configMAX_PRIORITIES 4
#define configMINIMAL_STACK_SIZE 128
#define configMAX_TASK_NAME_LEN 12
#define configTICK_TYPE_WIDTH_IN_BITS TICK_TYPE_WIDTH_32_BITS
#define configIDLE_SHOULD_YIELD 1
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configSUPPORT_STATIC_ALLOCATION 0
#define configTOTAL_HEAP_SIZE ( 16U * 1024U )
#define configAPPLICATION_ALLOCATED_HEAP 1
#define configUSE_MALLOC_FAILED_HOOK 0
#define configHEAP_CLEAR_MEMORY_ON_FREE 0
#define configUSE_TIMERS 0
#define configUSE_MUTEXES 0
#define configUSE_RECURSIVE_MUTEXES 0
#define configUSE_COUNTING_SEMAPHORES 0
#define configUSE_TASK_NOTIFICATIONS 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_CO_ROUTINES 0
#define configUSE_NEWLIB_REENTRANT 0
#define configUSE_POSIX_ERRNO 0
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configRECORD_STACK_HIGH_ADDRESS 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_vTaskDelayUntil 0
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 0
#define INCLUDE_xTaskGetSchedulerState 0
#define INCLUDE_uxTaskGetStackHighWaterMark 1
/* Hazard3 is RV32I: no floating-point or vector register file to save. */
#define configENABLE_FPU 0
#define configENABLE_VPU 0
/* Required by the pinned RISC-V port; K03 does not start the scheduler. */
#define configISR_STACK_SIZE_WORDS 128
#ifndef __ASSEMBLER__
#ifdef __cplusplus
extern "C" void lab_assert_fail( const char * file, int line );
#else
void lab_assert_fail( const char * file, int line );
#endif
#define configASSERT( condition ) do { if( !( condition ) ) { lab_assert_fail( __FILE__, __LINE__ ); } } while( 0 )
#else
#define configASSERT( condition )
#endif
#endif
+56
View File
@@ -0,0 +1,56 @@
#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
+243
View File
@@ -0,0 +1,243 @@
#ifndef FREERTOS_CPP_MEMORY_RESOURCE_HPP
#define FREERTOS_CPP_MEMORY_RESOURCE_HPP
#include <stddef.h>
#include <stdint.h>
extern "C"
{
#include "FreeRTOS.h"
}
namespace freertos
{
class MemoryResource final
{
public:
using Allocate = void *( * )( void *, size_t, size_t ) noexcept;
using Deallocate = void ( * )( void *, void *, size_t, size_t ) noexcept;
constexpr MemoryResource( void * context,
Allocate allocate,
Deallocate deallocate ) noexcept
: context_ { context },
allocate_ { allocate },
deallocate_ { deallocate }
{
}
void * allocate_bytes( size_t bytes, size_t alignment ) const noexcept
{
return allocate_( context_, bytes, alignment );
}
void deallocate_bytes( void * memory,
size_t bytes,
size_t alignment ) const noexcept
{
deallocate_( context_, memory, bytes, alignment );
}
constexpr void * context() const noexcept
{
return context_;
}
constexpr Allocate allocate_function() const noexcept
{
return allocate_;
}
constexpr Deallocate deallocate_function() const noexcept
{
return deallocate_;
}
private:
void * context_;
Allocate allocate_;
Deallocate deallocate_;
};
constexpr bool valid_alignment( size_t alignment ) noexcept
{
return alignment != 0U &&
( alignment & ( alignment - 1U ) ) == 0U;
}
class HeapResource final
{
public:
MemoryResource resource() noexcept
{
return MemoryResource { this, &allocate, &deallocate };
}
constexpr size_t successful_allocations() const noexcept
{
return successful_allocations_;
}
constexpr size_t failed_allocations() const noexcept
{
return failed_allocations_;
}
constexpr size_t deallocations() const noexcept
{
return deallocations_;
}
private:
static void * allocate( void * context,
size_t bytes,
size_t alignment ) noexcept
{
auto & self = *static_cast< HeapResource * >( context );
if( bytes == 0U || !valid_alignment( alignment ) ||
alignment > portBYTE_ALIGNMENT )
{
++self.failed_allocations_;
return nullptr;
}
void * const memory = pvPortMalloc( bytes );
if( memory == nullptr )
{
++self.failed_allocations_;
}
else
{
++self.successful_allocations_;
}
return memory;
}
static void deallocate( void * context,
void * memory,
size_t,
size_t ) noexcept
{
if( memory != nullptr )
{
auto & self = *static_cast< HeapResource * >( context );
vPortFree( memory );
++self.deallocations_;
}
}
size_t successful_allocations_ {};
size_t failed_allocations_ {};
size_t deallocations_ {};
};
class StaticArenaResource final
{
public:
constexpr StaticArenaResource( uint8_t * storage,
size_t capacity,
size_t maximum_alignment ) noexcept
: storage_ { storage },
capacity_ { capacity },
maximum_alignment_ { maximum_alignment }
{
}
MemoryResource resource() noexcept
{
return MemoryResource { this, &allocate, &deallocate };
}
void reset() noexcept
{
used_ = 0U;
++resets_;
}
constexpr size_t used() const noexcept
{
return used_;
}
constexpr size_t remaining() const noexcept
{
return capacity_ - used_;
}
constexpr size_t successful_allocations() const noexcept
{
return successful_allocations_;
}
constexpr size_t failed_allocations() const noexcept
{
return failed_allocations_;
}
constexpr size_t resets() const noexcept
{
return resets_;
}
bool owns( const void * memory ) const noexcept
{
const uintptr_t address = reinterpret_cast< uintptr_t >( memory );
const uintptr_t first = reinterpret_cast< uintptr_t >( storage_ );
return address >= first && address < first + capacity_;
}
private:
static void * allocate( void * context,
size_t bytes,
size_t alignment ) noexcept
{
auto & self = *static_cast< StaticArenaResource * >( context );
if( bytes == 0U || !valid_alignment( alignment ) ||
alignment > self.maximum_alignment_ )
{
++self.failed_allocations_;
return nullptr;
}
const uintptr_t base =
reinterpret_cast< uintptr_t >( self.storage_ );
const uintptr_t current = base + self.used_;
if( current > UINTPTR_MAX - ( alignment - 1U ) )
{
++self.failed_allocations_;
return nullptr;
}
const uintptr_t aligned =
( current + alignment - 1U ) & ~( alignment - 1U );
const size_t padding = static_cast< size_t >( aligned - current );
const size_t remaining = self.capacity_ - self.used_;
if( padding > remaining || bytes > remaining - padding )
{
++self.failed_allocations_;
return nullptr;
}
self.used_ += padding + bytes;
++self.successful_allocations_;
return reinterpret_cast< void * >( aligned );
}
static void deallocate( void *, void *, size_t, size_t ) noexcept
{
/* Monotonic arena: individual deallocation deliberately does nothing. */
}
uint8_t * storage_;
size_t capacity_;
size_t maximum_alignment_;
size_t used_ {};
size_t successful_allocations_ {};
size_t failed_allocations_ {};
size_t resets_ {};
};
} // namespace freertos
#endif
@@ -0,0 +1,15 @@
#ifndef FREERTOS_RISC_V_CHIP_SPECIFIC_EXTENSIONS_H
#define FREERTOS_RISC_V_CHIP_SPECIFIC_EXTENSIONS_H
/* Hazard3 adds no architectural registers to the base RV32I context. */
#define portasmHAS_MTIME 1
#define portasmHAS_SIFIVE_CLINT 0
#define portasmADDITIONAL_CONTEXT_SIZE 0
.macro portasmSAVE_ADDITIONAL_REGISTERS
.endm
.macro portasmRESTORE_ADDITIONAL_REGISTERS
.endm
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef LAB_FREESTANDING_STDLIB_H
#define LAB_FREESTANDING_STDLIB_H
/*
* The selected FreeRTOS sources include <stdlib.h> for standard types, but the
* configuration used by this card does not call hosted stdlib functions.
* GCC's freestanding headers provide size_t through <stddef.h>.
*/
#include <stddef.h>
#endif
+17
View File
@@ -0,0 +1,17 @@
#ifndef LAB_FREESTANDING_STRING_H
#define LAB_FREESTANDING_STRING_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
void * memcpy( void * destination, const void * source, size_t count );
void * memset( void * destination, int value, size_t count );
size_t strlen( const char * text );
#ifdef __cplusplus
}
#endif
#endif