8000 Fixed memory allocations in LittleFS files. · pycom/pycom-micropython-sigfox@8495c7d · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 8495c7d

Browse files
committed
Fixed memory allocations in LittleFS files.
1 parent 618f71d commit 8495c7d

File tree

6 files changed

+63
-81
lines changed

6 files changed

+63
-81
lines changed

esp32/ftp/ftp.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ STATIC FRESULT f_readdir_helper(ftp_dir_t *dp, ftp_fileinfo_t *fno ) {
338338
if(length_of_relative_path > 1) {
339339
path_length++;
340340
}
341-
char* file_relative_path = pvPortMalloc(path_length);
341+
char* file_relative_path = malloc(path_length);
342342

343343
// Copy the current working directory (relative path)
344344
memcpy(file_relative_path, path_relative, length_of_relative_path);
@@ -359,7 +359,7 @@ STATIC FRESULT f_readdir_helper(ftp_dir_t *dp, ftp_fileinfo_t *fno ) {
359359
fno->u.fpinfo_lfs.timestamp.ftime = 0;
360360
}
361361

362-
vPortFree(file_relative_path);
362+
free(file_relative_path);
363363
}
364364

365365
xSemaphoreGive(littlefs->mutex);
@@ -933,7 +933,7 @@ static void ftp_send_reply (uint32_t status, char *message) {
933933
strcat ((char *)ftp_cmd_buffer, "\r\n");
934934
fifoelement.sd = &ftp_data.c_sd;
935935
fifoelement.datasize = strlen((char *)ftp_cmd_buffer);
936-
fifoelement.data = pvPortMalloc(fifoelement.datasize);
936+
fifoelement.data = malloc(fifoelement.datasize);
937937
if (status == 221) {
938938
fifoelement.closesockets = E_FTP_CLOSE_CMD_AND_DATA;
939939
} else if (status == 426 || status == 451 || status == 550) {
@@ -945,7 +945,7 @@ static void ftp_send_reply (uint32_t status, char *message) {
945945
if (fifoelement.data) {
946946
memcpy (fifoelement.data, ftp_cmd_buffer, fifoelement.datasize);
947947
if (!SOCKETFIFO_Push (&fifoelement)) {
948-
vPortFree(fifoelement.data);
948+
free(fifoelement.data);
949949
}
950950
}
951951
}
@@ -979,13 +979,13 @@ static void ftp_send_from_fifo (void) {
979979
ftp_close_filesystem_on_error();
980980
}
981981
if (fifoelement.freedata) {
982-
vPortFree(fifoelement.data);
982+
free(fifoelement.data);
983983
}
984984
}
985985
} else { // socket closed, remove it from the queue
986986
SOCKETFIFO_Pop (&fifoelement);
987987
if (fifoelement.freedata) {
988-
vPortFree(fifoelement.data);
988+
free(fifoelement.data);
989989
}
990990
}
991991
} else if (ftp_data.state == E_FTP_STE_END_TRANSFER && (ftp_data.d_sd > 0)) {

esp32/littlefs/lfs_util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
203203
// Note, memory must be 64-bit aligned
204204
static inline void *lfs_malloc(size_t size) {
205205
#ifndef LFS_NO_MALLOC
206-
return pvPortMalloc(size);
206+
return malloc(size);
207207
#else
208208
return NULL;
209209
#endif
@@ -212,7 +212,7 @@ static inline void *lfs_malloc(size_t size) {
212212
// Deallocate memory, only used if buffers are not provided to littlefs
213213
static inline void lfs_free(void *p) {
214214
#ifndef LFS_NO_MALLOC
215-
vPortFree(p);
215+
free(p);
216216
#endif
217217
}
218218

esp32/littlefs/vfs_littlefs.c

Lines changed: 51 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ int lfs_statvfs_count(void *p, lfs_block_t b)
1717
return 0;
1818
}
1919

20-
// After this function vPortFree() must be called on the returned address after usage!!
20+
// After this function free() must be called on the returned address after usage!!
2121
const char* concat_with_cwd(vfs_lfs_struct_t* littlefs, const char* path)
< F438 code>2222
{
2323
char* path_out = NULL;
2424

2525
if (path[0] == '/') /* Absolute path */
2626
{
27-
path_out = (char*)pvPortMalloc(strlen(path) + 1); // Count the \0 too
27+
path_out = (char*)malloc(strlen(path) + 1); // Count the \0 too
2828
if(path_out != NULL)
2929
{
3030
strcpy(path_out, path);
3131
}
3232
}
3333
else
3434
{
35-
path_out = (char*)pvPortMalloc(strlen(littlefs->cwd) + 1 + strlen(path) + 1);
35+
path_out = (char*)malloc(strlen(littlefs->cwd) + 1 + strlen(path) + 1);
3636
if(path_out != NULL)
3737
{
3838
strcpy(path_out, littlefs->cwd);
@@ -129,9 +129,8 @@ static int change_cwd(vfs_lfs_struct_t* littlefs, const char* path_in)
129129
}
130130
else if(is_valid_directory(littlefs, new_path))
131131
{
132-
vPortFree(littlefs->cwd);
132+
free(littlefs->cwd);
133133
littlefs->cwd = (char*)new_path;
134-
MP_STATE_PORT(lfs_cwd) = littlefs->cwd;
135134

136135
res = LFS_ERR_OK;
137136
}
@@ -353,21 +352,21 @@ void littlefs_prepare_attributes(struct lfs_file_config *cfg)
353352
{
354353
// Currently we only have 1 attribute
355354
cfg->attr_count = 1;
356-
cfg->attrs = pvPortMalloc(cfg->attr_count * sizeof(struct lfs_attr));
355+
cfg->attrs = malloc(cfg->attr_count * sizeof(struct lfs_attr));
357356

358357
// Set attribute for storing the timestamp
359358
cfg->attrs[0].size = sizeof(lfs_timestamp_attribute_t);
360359
cfg->attrs[0].type = LFS_ATTRIBUTE_TIMESTAMP;
361-
cfg->attrs[0].buffer = pvPortMalloc(sizeof(lfs_timestamp_attribute_t));
360+
cfg->attrs[0].buffer = malloc(sizeof(lfs_timestamp_attribute_t));
362361

363362
}
364363

365364
void littlefs_free_up_attributes(struct lfs_file_config *cfg)
366365
{
367366
cfg->attr_count = 0;
368367
// Currently we only have 1 attribute for timestamp
369-
vPortFree(cfg->attrs[0].buffer);
370-
vPortFree(cfg->attrs);
368+
free(cfg->attrs[0].buffer);
369+
free(cfg->attrs);
371370
}
372371

373372

@@ -478,18 +477,15 @@ STATIC mp_obj_t littlefs_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args)
478477
iter->is_str = is_str_type;
479478

480479
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
481-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
482-
if(path == NULL)
483-
{
484-
res = LFS_ERR_NOMEM;
485-
}
486-
else
487-
{
488-
res = lfs_dir_open(&self->fs.littlefs.lfs, &iter->dir, path);
489-
}
480+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
481+
if (path == NULL) {
482+
res = LFS_ERR_NOMEM;
483+
} else {
484+
res = lfs_dir_open(&self->fs.littlefs.lfs, &iter->dir, path);
485+
}
490486
xSemaphoreGive(self->fs.littlefs.mutex);
491487

492-
vPortFree((void*)path);
488+
free((void*)path);
493489

494490
if (res != LFS_ERR_OK) {
495491
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -506,21 +502,18 @@ STATIC mp_obj_t littlefs_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_param) {
506502
const char *path_in = mp_obj_str_get_str(path_param);
507503

508504
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
509-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
510-
if(path == NULL)
511-
{
512-
res = LFS_ERR_NOMEM;
513-
}
514-
else
515-
{
516-
res = lfs_mkdir(&self->fs.littlefs.lfs, path);
517-
if(res == LFS_ERR_OK) {
518-
littlefs_update_timestamp(&self->fs.littlefs.lfs, path);
519-
}
505+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
506+
if (path == NULL) {
507+
res = LFS_ERR_NOMEM;
508+
} else {
509+
res = lfs_mkdir(&self->fs.littlefs.lfs, path);
510+
if (res == LFS_ERR_OK) {
511+
littlefs_update_timestamp(&self->fs.littlefs.lfs, path);
520512
}
513+
}
521514
xSemaphoreGive(self->fs.littlefs.mutex);
522515

523-
vPortFree((void*)path);
516+
free((void*)path);
524517

525518
if (res != LFS_ERR_OK) {
526519
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -538,18 +531,15 @@ STATIC mp_obj_t littlefs_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_param) {
538531
const char *path_in = mp_obj_str_get_str(path_param);
539532

540533
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
541-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
542-
if(path == NULL)
543-
{
544-
res = LFS_ERR_NOMEM;
545-
}
546-
else
547-
{
548-
res = lfs_remove(&self->fs.littlefs.lfs, path);
549-
}
534+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
535+
if (path == NULL) {
536+
res = LFS_ERR_NOMEM;
537+
} else {
538+
res = lfs_remove(&self->fs.littlefs.lfs, path);
539+
}
550540
xSemaphoreGive(self->fs.littlefs.mutex);
551541

552-
vPortFree((void*)path);
542+
free((void*)path);
553543

554544
if (res != LFS_ERR_OK) {
555545
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -568,21 +558,18 @@ STATIC mp_obj_t littlefs_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_param_in, mp_
568558
const char *path_out = mp_obj_str_get_str(path_param_out);
569559

570560
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
571-
const char* old_path = concat_with_cwd(&self->fs.littlefs, path_in);
572-
const char* new_path = concat_with_cwd(&self->fs.littlefs, path_out);
561+
const char *old_path = concat_with_cwd(&self->fs.littlefs, path_in);
562+
const char *new_path = concat_with_cwd(&self->fs.littlefs, path_out);
573563

574-
if(old_path == NULL || new_path == NULL)
575-
{
576-
res = LFS_ERR_NOMEM;
577-
}
578-
else
579-
{
580-
res = lfs_rename(&self->fs.littlefs.lfs, old_path, new_path);
581-
}
564+
if (old_path == NULL || new_path == NULL) {
565+
res = LFS_ERR_NOMEM;
566+
} else {
567+
res = lfs_rename(&self->fs.littlefs.lfs, old_path, new_path);
568+
}
582569
xSemaphoreGive(self->fs.littlefs.mutex);
583570

584-
vPortFree((void*)old_path);
585-
vPortFree((void*)new_path);
571+
free((void*)old_path);
572+
free((void*)new_path);
586573

587574
if (res != LFS_ERR_OK) {
588575
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -633,23 +620,20 @@ STATIC mp_obj_t littlefs_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_param) {
633620

634621

635622
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
636-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
637-
if(path == NULL)
638-
{
639-
res = LFS_ERR_NOMEM;
640-
}
641-
else if (path[0] == 0 || (path[0] == '/' && path[1] == 0))
642-
{
643-
// stat root directory
644-
fno.size = 0;
645-
fno.type = LFS_TYPE_DIR;
646-
} else {
647-
res = littlefs_stat_common_helper(&self->fs.littlefs.lfs, path, &fno, &ts);
648-
}
623+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
624+
if (path == NULL) {
625+
res = LFS_ERR_NOMEM;
626+
} else if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
627+
// stat root directory
628+
fno.size = 0;
629+
fno.type = LFS_TYPE_DIR;
630+
} else {
631+
res = littlefs_stat_common_helper(&self->fs.littlefs.lfs, path, &fno, &ts);
632+
}
649633

650634
xSemaphoreGive(self->fs.littlefs.mutex);
651635

652-
vPortFree((void*)path);
636+
free((void*)path);
653637

654638
if (res < LFS_ERR_OK) {
655639
mp_raise_OSError(littleFsErrorToErrno(res));

esp32/littlefs/vfs_littlefs_file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
167167
o->timestamp_update = false;
168168

169169
xSemaphoreTake(vfs->fs.littlefs.mutex, portMAX_DELAY);
170-
const char *fname = concat_with_cwd(&vfs->fs.littlefs, mp_obj_str_get_str(args[0].u_obj));
171-
int res = littlefs_open_common_helper(&vfs->fs.littlefs.lfs, fname, &o->fp, mode, &o->cfg, &o->timestamp_update);
170+
const char *fname = concat_with_cwd(&vfs->fs.littlefs, mp_obj_str_get_str(args[0].u_obj));
171+
int res = littlefs_open_common_helper(&vfs->fs.littlefs.lfs, fname, &o->fp, mode, &o->cfg, &o->timestamp_update);
172172
xSemaphoreGive(vfs->fs.littlefs.mutex);
173173

174-
vPortFree((void*)fname);
174+
free((void*)fname);
175175
if (res < LFS_ERR_OK) {
176176
m_del_obj(pyb_file_obj_t, o);
177177
mp_raise_OSError(littleFsErrorToErrno(res));

esp32/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ extern const struct _mp_obj_module_t mp_module_uqueue;
250250
mp_obj_list_t btc_conn_list; \
251251
mp_obj_list_t bts_srv_list; \
252252
mp_obj_list_t bts_attr_list; \
253-
char* lfs_cwd; \
254253
mp_obj_t coap_ptr; \
255254

256255
// we need to provide a declaration/definition of alloca()

esp32/mptask.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,10 @@ STATIC void mptask_init_sflash_filesystem_littlefs(void) {
534534
MP_STATE_PORT(vfs_cur) = vfs;
535535

536536
//Initialize the current working directory (cwd)
537-
vfs_littlefs->fs.littlefs.cwd = (char*)pvPortMalloc(2);
537+
vfs_littlefs->fs.littlefs.cwd = (char*)malloc(2);
538538
vfs_littlefs->fs.littlefs.cwd[0] = '/';
539539
vfs_littlefs->fs.littlefs.cwd[1] = '\0';
540540

541-
MP_STATE_PORT(lfs_cwd) = vfs_littlefs->fs.littlefs.cwd;
542541
vfs_littlefs->fs.littlefs.mutex = xSemaphoreCreateMutex();
543542

544543
xSemaphoreTake(vfs_littlefs->fs.littlefs.mutex, portMAX_DELAY);

0 commit comments

Comments
 (0)
0