Skip to content

Commit a45b12a

Browse files
committed
feat(storage/vfs): improve mountpoint table memory usage
1 parent 23ce1cc commit a45b12a

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

components/vfs/private_include/esp_vfs_private.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#include "sdkconfig.h"
88
#include "esp_vfs.h"
99
#include "esp_vfs_common.h"
10+
#include <stddef.h>
1011

1112
#ifdef __cplusplus
1213
extern "C" {
@@ -19,12 +20,12 @@ extern "C" {
1920
#endif
2021

2122
typedef struct vfs_entry_ {
22-
int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR and/or ESP_VFS_FLAG_READONLY_FS or ESP_VFS_FLAG_DEFAULT */
23-
const esp_vfs_fs_ops_t *vfs; // contains pointers to VFS functions
24-
char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
25-
size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
26-
void* ctx; // optional pointer which can be passed to VFS
27-
int offset; // index of this structure in s_vfs array
23+
int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR and/or ESP_VFS_FLAG_READONLY_FS or ESP_VFS_FLAG_DEFAULT */
24+
const esp_vfs_fs_ops_t *vfs; // contains pointers to VFS functions
25+
void *ctx; // optional pointer which can be passed to VFS
26+
int offset; // index of this structure in s_vfs array
27+
size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
28+
const char path_prefix[] __attribute__ ((counted_by (path_prefix_len))); // path prefix mapped to this VFS
2829
} vfs_entry_t;
2930

3031
/**
@@ -53,7 +54,7 @@ typedef struct vfs_entry_ {
5354
* ESP_ERR_NO_MEM if too many VFSes are registered.
5455
* ESP_ERR_INVALID_ARG if given an invalid parameter.
5556
*/
56-
esp_err_t esp_vfs_register_common(const char *base_path, size_t len, const esp_vfs_t* vfs, void* ctx, int *vfs_index);
57+
esp_err_t esp_vfs_register_common(const char *base_path, size_t len, const esp_vfs_t *vfs, void *ctx, int *vfs_index);
5758

5859
/**
5960
* Get vfs fd with given path.

components/vfs/vfs.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -395,20 +395,30 @@ static esp_err_t esp_vfs_make_fs_ops(const esp_vfs_t *vfs, esp_vfs_fs_ops_t **mi
395395
return ESP_ERR_NO_MEM;
396396
}
397397

398-
static esp_err_t esp_vfs_register_fs_common(const char* base_path, size_t len, const esp_vfs_fs_ops_t* vfs, int flags, void* ctx, int *vfs_index)
398+
static esp_err_t esp_vfs_register_fs_common(
399+
const char *base_path,
400+
size_t base_path_len,
401+
const esp_vfs_fs_ops_t *vfs,
402+
int flags,
403+
void *ctx,
404+
int *vfs_index)
399405
{
406+
if (s_vfs_count >= VFS_MAX_COUNT) {
407+
return ESP_ERR_NO_MEM;
408+
}
409+
400410
if (vfs == NULL) {
401411
ESP_LOGE(TAG, "VFS is NULL");
402412
return ESP_ERR_INVALID_ARG;
403413
}
404414

405-
if (len != LEN_PATH_PREFIX_IGNORED) {
415+
if (base_path_len != LEN_PATH_PREFIX_IGNORED) {
406416
/* empty prefix is allowed, "/" is not allowed */
407-
if ((len == 1) || (len > ESP_VFS_PATH_MAX)) {
417+
if ((base_path_len == 1) || (base_path_len > ESP_VFS_PATH_MAX)) {
408418
return ESP_ERR_INVALID_ARG;
409419
}
410420
/* prefix has to start with "/" and not end with "/" */
411-
if (len >= 2 && ((base_path[0] != '/') || (base_path[len - 1] == '/'))) {
421+
if (base_path_len >= 2 && ((base_path[0] != '/') || (base_path[base_path_len - 1] == '/'))) {
412422
return ESP_ERR_INVALID_ARG;
413423
}
414424
}
@@ -426,23 +436,26 @@ static esp_err_t esp_vfs_register_fs_common(const char* base_path, size_t len, c
426436
s_vfs_count++;
427437
}
428438

429-
vfs_entry_t *entry = (vfs_entry_t*) heap_caps_malloc(sizeof(vfs_entry_t), VFS_MALLOC_FLAGS);
439+
size_t alloc_size = sizeof(vfs_entry_t)
440+
+ (base_path_len == LEN_PATH_PREFIX_IGNORED ? 0 : base_path_len + 1);
441+
442+
vfs_entry_t *entry = (vfs_entry_t*) heap_caps_malloc(alloc_size, VFS_MALLOC_FLAGS);
430443
if (entry == NULL) {
431444
return ESP_ERR_NO_MEM;
432445
}
433446

434447
s_vfs[index] = entry;
435-
if (len != LEN_PATH_PREFIX_IGNORED) {
436-
strcpy(entry->path_prefix, base_path); // we have already verified argument length
437-
} else {
438-
bzero(entry->path_prefix, sizeof(entry->path_prefix));
439-
}
440-
entry->path_prefix_len = len;
448+
449+
entry->path_prefix_len = base_path_len;
441450
entry->vfs = vfs;
442451
entry->ctx = ctx;
443452
entry->offset = index;
444453
entry->flags = flags;
445454

455+
if (base_path_len != LEN_PATH_PREFIX_IGNORED) {
456+
memcpy((char *)(entry->path_prefix), base_path, base_path_len + 1);
457+
}
458+
446459
if (vfs_index) {
447460
*vfs_index = index;
448461
}

0 commit comments

Comments
 (0)