Remove dynamic memory allocation (malloc/calloc/free)

- Add static memory pool implementation (se050_mem_pool.c/h)
- Replace all malloc/calloc with pool allocations
- Replace all free with pool deallocations
- Remove strdup usage (use fixed-size buffer instead)
- Update I2C HAL to use fixed-size dev_path array
- All 24 tests pass with static memory only

Suitable for embedded environments (u-boot, ESP32) without heap.
This commit is contained in:
km
2026-03-29 19:07:57 +09:00
parent 479fcd37c1
commit 11bcc5e0c3
11 changed files with 447 additions and 79 deletions
+5 -1
View File
@@ -7,6 +7,8 @@
#define SE050_I2C_HAL_H
#include <stdint.h>
#include <stddef.h>
#include <string.h>
/* Status codes */
typedef enum {
@@ -24,10 +26,12 @@ typedef enum {
} se050_status_t;
/* I2C HAL structure */
#define SE050_I2C_DEV_PATH_MAX 64
typedef struct {
void *handle; /**< I2C file descriptor */
uint8_t slave_addr; /**< I2C slave address */
const char *dev_path; /**< I2C device path */
char dev_path[SE050_I2C_DEV_PATH_MAX]; /**< I2C device path */
int wakeup_pin; /**< Wakeup GPIO pin (-1 if unused) */
} se050_i2c_hal_t;
+106
View File
@@ -0,0 +1,106 @@
/**
* @file se050_mem_pool.h
* @brief Static Memory Pool for Embedded Systems
*
* Replaces malloc/calloc with pre-allocated static pools.
* Suitable for u-boot, ESP32, and other embedded environments.
*/
#ifndef SE050_MEM_POOL_H
#define SE050_MEM_POOL_H
#include <stdint.h>
#include <stddef.h>
/* Configuration: Pool sizes */
#ifndef SE050_POOL_SESSION_COUNT
#define SE050_POOL_SESSION_COUNT 4
#endif
#ifndef SE050_POOL_SCP03_COUNT
#define SE050_POOL_SCP03_COUNT 4
#endif
#ifndef SE050_POOL_KEYSTORE_COUNT
#define SE050_POOL_KEYSTORE_COUNT 2
#endif
#ifndef SE050_POOL_KEYSTORE_MAX_OBJECTS
#define SE050_POOL_KEYSTORE_MAX_OBJECTS 8
#endif
#ifndef SE050_POOL_RNG_COUNT
#define SE050_POOL_RNG_COUNT 2
#endif
#ifndef SE050_POOL_I2C_HAL_COUNT
#define SE050_POOL_I2C_HAL_COUNT 2
#endif
/* Forward declarations */
struct se050_session_ctx;
struct se050_scp03_ctx;
struct se050_keystore_ctx;
struct se050_rng_ctx;
struct se050_i2c_hal;
/* ============================================================================
* Memory Pool API
* ============================================================================ */
/**
* @brief Initialize all memory pools
*
* Must be called before any other SE050 functions.
*
* @return 0 on success, -1 on error
*/
int se050_mem_pool_init(void);
/**
* @brief Cleanup all memory pools
*
* Zeroizes all allocated memory before freeing.
*/
void se050_mem_pool_cleanup(void);
/* Session pool */
struct se050_session_ctx *se050_session_alloc_pool(void);
void se050_session_free_pool(struct se050_session_ctx *ctx);
/* SCP03 pool */
struct se050_scp03_ctx *se050_scp03_alloc_pool(void);
void se050_scp03_free_pool(struct se050_scp03_ctx *ctx);
/* Keystore pool */
struct se050_keystore_ctx *se050_keystore_alloc_pool(void);
void se050_keystore_free_pool(struct se050_keystore_ctx *ctx);
/* RNG pool */
struct se050_rng_ctx *se050_rng_alloc_pool(void);
void se050_rng_free_pool(struct se050_rng_ctx *ctx);
/* I2C HAL pool */
struct se050_i2c_hal *se050_i2c_hal_alloc_pool(void);
void se050_i2c_hal_free_pool(struct se050_i2c_hal *hal);
/* ============================================================================
* Debug/Statistics
* ============================================================================ */
/**
* @brief Get pool statistics
*/
typedef struct {
int total;
int used;
int free;
} se050_pool_stats_t;
void se050_mem_pool_stats(se050_pool_stats_t *session,
se050_pool_stats_t *scp03,
se050_pool_stats_t *keystore,
se050_pool_stats_t *rng,
se050_pool_stats_t *i2c_hal);
#endif /* SE050_MEM_POOL_H */
+1
View File
@@ -12,6 +12,7 @@
/* Forward declarations */
typedef struct se050_session_ctx se050_session_ctx_t;
typedef struct se050_scp03_ctx se050_scp03_ctx_t;
/* SCP03 key sizes */
#define SCP03_KEY_SIZE 16
+1
View File
@@ -10,6 +10,7 @@
#ifndef SE050_SESSION_INTERNAL_H
#define SE050_SESSION_INTERNAL_H
#include "se050_i2c_hal.h"
#include "se050_wireguard.h"
#include <stdint.h>