imfs/user_allocator: Add user-configurable allocator support

- Extend IMFS and libio headers to define default allocator/deallocator ops
  and logic for configuring memfile_ops.
- Update imfs_memfile to use custom allocators for memory blocks.
- Fix imfs_statvfs.f_bfree calculation to respect user-defined ops.
- Add psximfs03 test case to validate custom allocator behavior.
- Refactor psximfs01 to share helper functions with psximfs03.
This commit is contained in:
Bhavya Shah
2025-06-06 20:56:34 +05:30
committed by Kinsey Moore
parent 05629d07e2
commit 6367099ec0
15 changed files with 747 additions and 267 deletions

View File

@@ -200,6 +200,15 @@ extern "C" {
const int imfs_memfile_bytes_per_block = CONFIGURE_IMFS_MEMFILE_BYTES_PER_BLOCK;
/*
* Using the default ops if user doesn't configure ops
*/
#ifndef CONFIGURE_IMFS_MEMFILE_OPS
#define CONFIGURE_IMFS_MEMFILE_OPS IMFS_MEMFILE_DEFAULT_OPS
#endif
IMFS_memfile_ops_t imfs_memfile_ops = CONFIGURE_IMFS_MEMFILE_OPS;
static IMFS_fs_info_t IMFS_root_fs_info;
static const rtems_filesystem_operations_table IMFS_root_ops = {

View File

@@ -61,6 +61,9 @@ extern "C" {
struct IMFS_jnode_tt;
typedef struct IMFS_jnode_tt IMFS_jnode_t;
typedef struct IMFS_memfile_ops_t IMFS_memfile_ops_t;
extern IMFS_memfile_ops_t imfs_memfile_ops;
/**
* IMFS "memfile" information
*
@@ -283,6 +286,65 @@ struct IMFS_jnode_tt {
const IMFS_node_control *control;
};
/**
* @brief The type of allocator function pointer.
*/
typedef void *(*IMFS_memfile_allocator)(void);
/**
* @brief The type of deallocator function pointer.
*/
typedef void (*IMFS_memfile_deallocator)(
void *memory
);
/**
* @brief The get_free_space function pointer.
*/
typedef size_t (*IMFS_memfile_free_space)(void);
/**
* @brief The ops table for user defined allocator-deallocator for
* IMFS memfile data blocks.
*
* @param allocate The function which will be used to allocate IMFS blocks.
* @param deallocate The function to deallocate the allocated data blocks.
* @param get_free_space The function used by kernel to get the free space.
*/
struct IMFS_memfile_ops_t {
IMFS_memfile_allocator allocate_block;
IMFS_memfile_deallocator free_block;
IMFS_memfile_free_space get_free_space;
};
/**
* @brief The default imfs block allocator
*
* @details The allocator uses the memory from the heap.
*/
void *IMFS_default_allocate_block(void);
/**
* @brief The default imfs block deallocator
*
* @details The deallocator frees the memory which was taken from heap.
*/
void IMFS_default_deallocate_block(void *);
/**
* @brief The default free space calculator
*
* @details The free_space function calculates the free space in heap.
*/
size_t IMFS_default_free_space(void);
#define IMFS_MEMFILE_DEFAULT_OPS \
{ \
.allocate_block = IMFS_default_allocate_block, \
.free_block = IMFS_default_deallocate_block, \
.get_free_space = IMFS_default_free_space \
}
typedef struct {
IMFS_jnode_t Node;
rtems_chain_control Entries;

View File

@@ -40,6 +40,7 @@
#include <rtems/imfs.h>
#include <stdlib.h>
#include <errno.h>
int IMFS_initialize_support(
@@ -52,6 +53,10 @@ int IMFS_initialize_support(
const IMFS_node_control *node_control;
IMFS_jnode_t *root_node;
_Assert(imfs_memfile_ops.allocate_block != NULL);
_Assert(imfs_memfile_ops.free_block != NULL);
_Assert(imfs_memfile_ops.get_free_space != NULL);
mount_data = data;
fs_info = mount_data->fs_info;

View File

@@ -38,6 +38,7 @@
#include "config.h"
#endif
#include <rtems/libcsupport.h>
#include <rtems/imfsimpl.h>
#include <stdlib.h>
@@ -831,7 +832,7 @@ void *memfile_alloc_block(void)
{
void *memory;
memory = (void *)calloc(1, IMFS_MEMFILE_BYTES_PER_BLOCK);
memory = imfs_memfile_ops.allocate_block();
if ( memory )
memfile_blocks_allocated++;
@@ -847,10 +848,25 @@ void memfile_free_block(
void *memory
)
{
free(memory);
imfs_memfile_ops.free_block(memory);
memfile_blocks_allocated--;
}
void *IMFS_default_allocate_block(void)
{
return (void *)calloc(1, IMFS_MEMFILE_BYTES_PER_BLOCK);
}
void IMFS_default_deallocate_block(void *memory)
{
free(memory);
}
size_t IMFS_default_free_space(void)
{
return malloc_free_space();
}
static const rtems_filesystem_file_handlers_r IMFS_memfile_handlers = {
.open_h = rtems_filesystem_default_open,
.close_h = rtems_filesystem_default_close,

View File

@@ -47,8 +47,8 @@ int IMFS_statvfs(
buf->f_bsize = IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_frsize = IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_blocks = UINT_MAX / IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_bfree = malloc_free_space() / IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_bavail = malloc_free_space() / IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_bfree = imfs_memfile_ops.get_free_space() / IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_bavail = imfs_memfile_ops.get_free_space() / IMFS_MEMFILE_BYTES_PER_BLOCK;
buf->f_files = fs_info->jnode_count;
buf->f_fsid = 1;
buf->f_flag = loc->mt_entry->writeable;