8000 Merge pull request #123 from pycom/pr418 · pycom/pycom-micropython-sigfox@f58301e · 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 f58301e

Browse files
author
Catalin Ioana
authored
Merge pull request #123 from pycom/pr418
Standardized the memory allocations. (Extended from PR 418)
2 parents 325f89c + 8e08fab commit f58301e

21 files changed

+99
-114
lines changed

esp32/fatfs/src/drivers/sflash_diskio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ DRESULT sflash_disk_init (void) {
5454
sflash_start_address = SFLASH_START_ADDR_4MB;
5555
sflash_fs_sector_count = SFLASH_FS_SECTOR_COUNT_4MB;
5656
}
57-
sflash_block_cache = (uint8_t *)heap_caps_malloc(SFLASH_BLOCK_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
57+
sflash_block_cache = (uint8_t *)malloc(SFLASH_BLOCK_SIZE);
5858
sflash_prev_block_addr = UINT32_MAX;
5959
sflash_cache_is_dirty = false;
6060
sflash_init_done = true;

esp32/ftp/ftp.c

Lines changed: 10 additions & 10 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 = m_malloc(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-
m_free(file_relative_path);
362+
free(file_relative_path);
363363
}
364364

365365
xSemaphoreGive(littlefs->mutex);
@@ -614,10 +614,10 @@ static void ftp_return_to_previous_path (char *pwd, char *dir);
614614
******************************************************************************/
615615
void ftp_init (void) {
616616
// allocate memory for the data buffer, and the file system structs (from the RTOS heap)
617-
ftp_data.dBuffer = heap_caps_malloc(FTP_BUFFER_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
618-
ftp_path = heap_caps_malloc(FTP_MAX_PARAM_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
619-
ftp_scratch_buffer = heap_caps_malloc(FTP_MAX_PARAM_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
620-
ftp_cmd_buffer = heap_caps_malloc(FTP_MAX_PARAM_SIZE + FTP_CMD_SIZE_MAX, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
617+
ftp_data.dBuffer = malloc(FTP_BUFFER_SIZE);
618+
ftp_path = malloc(FTP_MAX_PARAM_SIZE);
619+
ftp_scratch_buffer = malloc(FTP_MAX_PARAM_SIZE);
620+
ftp_cmd_buffer = malloc(FTP_MAX_PARAM_SIZE + FTP_CMD_SIZE_MAX);
621621
SOCKETFIFO_Init (&ftp_socketfifo, (void *)ftp_fifoelements, FTP_SOCKETFIFO_ELEMENTS_MAX);
622622
ftp_data.c_sd = -1;
623623
ftp_data.d_sd = -1;
@@ -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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "py/mpconfig.h"
2323
#include "py/misc.h"
24-
#include "py/gc.h"
2524

2625
// System includes
2726
#include <stdint.h>
@@ -204,7 +203,7 @@ uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
204203
// Note, memory must be 64-bit aligned
205204
static inline void *lfs_malloc(size_t size) {
206205
#ifndef LFS_NO_MALLOC
207-
return gc_alloc(size, false);
206+
return malloc(size);
208207
#else
209208
return NULL;
210209
#endif
@@ -213,7 +212,7 @@ static inline void *lfs_malloc(size_t size) {
213212
// Deallocate memory, only used if buffers are not provided to littlefs
214213
static inline void lfs_free(void *p) {
215214
#ifndef LFS_NO_MALLOC
216-
m_free(p);
215+
free(p);
217216
#endif
218217
}
219218

esp32/littlefs/vfs_littlefs.c

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <string.h>
55
#include "py/runtime.h"
66
#include "py/mperrno.h"
7-
#include "py/gc.h"
87
#include "lib/oofatfs/ff.h"
98
#include "extmod/vfs.h"
109
#include "vfs_littlefs.h"
@@ -18,22 +17,22 @@ int lfs_statvfs_count(void *p, lfs_block_t b)
1817
return 0;
1918
}
2019

21-
// After this function m_free() must be called on the returned address after usage!!
20+
// After this function free() must be called on the returned address after usage!!
2221
const char* concat_with_cwd(vfs_lfs_struct_t* littlefs, const char* path)
2322
{
2423
char* path_out = NULL;
2524

2625
if (path[0] == '/') /* Absolute path */
2726
{
28-
path_out = (char*)gc_alloc(strlen(path) + 1, false); // Count the \0 too
27+
path_out = (char*)malloc(strlen(path) + 1); // Count the \0 too
2928
if(path_out != NULL)
3029
{
3130
strcpy(path_out, path);
3231
}
3332
}
3433
else
3534
{
36-
path_out = (char*)gc_alloc(strlen(littlefs->cwd) + 1 + strlen(path) + 1, false);
35+
path_out = (char*)malloc(strlen(littlefs->cwd) + 1 + strlen(path) + 1);
3736
if(path_out != NULL)
3837
{
3938
strcpy(path_out, littlefs->cwd);
@@ -130,9 +129,8 @@ static int change_cwd(vfs_lfs_struct_t* littlefs, const char* path_in)
130129
}
131130
else if(is_valid_directory(littlefs, new_path))
132131
{
133-
m_free(littlefs->cwd);
132+
free(littlefs->cwd);
134133
littlefs->cwd = (char*)new_path;
135-
MP_STATE_PORT(lfs_cwd) = littlefs->cwd;
136134

137135
res = LFS_ERR_OK;
138136
}
@@ -354,21 +352,21 @@ void littlefs_prepare_attributes(struct lfs_file_config *cfg)
354352
{
355353
// Currently we only have 1 attribute
356354
cfg->attr_count = 1;
357-
cfg->attrs = m_malloc(cfg->attr_count * sizeof(struct lfs_attr));
355+
cfg->attrs = malloc(cfg->attr_count * sizeof(struct lfs_attr));
358356

359357
// Set attribute for storing the timestamp
360358
cfg->attrs[0].size = sizeof(lfs_timestamp_attribute_t);
361359
cfg->attrs[0].type = LFS_ATTRIBUTE_TIMESTAMP;
362-
cfg->attrs[0].buffer = m_malloc(sizeof(lfs_timestamp_attribute_t));
360+
cfg->attrs[0].buffer = malloc(sizeof(lfs_timestamp_attribute_t));
363361

364362
}
365363

366364
void littlefs_free_up_attributes(struct lfs_file_config *cfg)
367365
{
368366
cfg->attr_count = 0;
369367
// Currently we only have 1 attribute for timestamp
370-
m_free(cfg->attrs[0].buffer);
371-
m_free(cfg->attrs);
368+
free(cfg->attrs[0].buffer);
369+
free(cfg->attrs);
372370
}
373371

374372

@@ -479,18 +477,15 @@ STATIC mp_obj_t littlefs_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args)
479477
iter->is_str = is_str_type;
480478

481479
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
482-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
483-
if(path == NULL)
484-
{
480+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
481+
if (path == NULL) {
485482
res = LFS_ERR_NOMEM;
486-
}
487-
else
488-
{
483+
} else {
489484
res = lfs_dir_open(&self->fs.littlefs.lfs, &iter->dir, path);
490485
}
491486
xSemaphoreGive(self->fs.littlefs.mutex);
492487

493-
m_free((void*)path);
488+
free((void*)path);
494489

495490
if (res != LFS_ERR_OK) {
496491
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -507,21 +502,18 @@ STATIC mp_obj_t littlefs_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_param) {
507502
const char *path_in = mp_obj_str_get_str(path_param);
508503

509504
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
510-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
511-
if(path == NULL)
512-
{
505+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
506+
if (path == NULL) {
513507
res = LFS_ERR_NOMEM;
514-
}
515-
else
516-
{
508+
} else {
517509
res = lfs_mkdir(&self->fs.littlefs.lfs, path);
518-
if(res == LFS_ERR_OK) {
510+
if (res == LFS_ERR_OK) {
519511
littlefs_update_timestamp(&self->fs.littlefs.lfs, path);
520512
}
521513
}
522514
xSemaphoreGive(self->fs.littlefs.mutex);
523515

524-
m_free((void*)path);
516+
free((void*)path);
525517

526518
if (res != LFS_ERR_OK) {
527519
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -539,18 +531,15 @@ STATIC mp_obj_t littlefs_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_param) {
539531
const char *path_in = mp_obj_str_get_str(path_param);
540532

541533
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
542-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
543-
if(path == NULL)
544-
{
534+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
535+
if (path == NULL) {
545536
res = LFS_ERR_NOMEM;
546-
}
547-
else
548-
{
537+
} else {
549538
res = lfs_remove(&self->fs.littlefs.lfs, path);
550539
}
551540
xSemaphoreGive(self->fs.littlefs.mutex);
552541

553-
m_free((void*)path);
542+
free((void*)path);
554543

555544
if (res != LFS_ERR_OK) {
556545
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -569,21 +558,18 @@ STATIC mp_obj_t littlefs_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_param_in, mp_
569558
const char *path_out = mp_obj_str_get_str(path_param_out);
570559

571560
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
572-
const char* old_path = concat_with_cwd(&self->fs.littlefs, path_in);
573-
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);
574563

575-
if(old_path == NULL || new_path == NULL)
576-
{
564+
if (old_path == NULL || new_path == NULL) {
577565
res = LFS_ERR_NOMEM;
578-
}
579-
else
580-
{
566+
} else {
581567
res = lfs_rename(&self->fs.littlefs.lfs, old_path, new_path);
582568
}
583569
xSemaphoreGive(self->fs.littlefs.mutex);
584570

585-
m_free((void*)old_path);
586-
m_free((void*)new_path);
571+
free((void*)old_path);
572+
free((void*)new_path);
587573

588574
if (res != LFS_ERR_OK) {
589575
mp_raise_OSError(littleFsErrorToErrno(res));
@@ -634,13 +620,10 @@ STATIC mp_obj_t littlefs_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_param) {
634620

635621

636622
xSemaphoreTake(self->fs.littlefs.mutex, portMAX_DELAY);
637-
const char* path = concat_with_cwd(&self->fs.littlefs, path_in);
638-
if(path == NULL)
639-
{
623+
const char *path = concat_with_cwd(&self->fs.littlefs, path_in);
624+
if (path == NULL) {
640625
res = LFS_ERR_NOMEM;
641-
}
642-
else if (path[0] == 0 || (path[0] == '/' && path[1] == 0))
643-
{
626+
} else if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
644627
// stat root directory
645628
fno.size = 0;
646629
fno.type = LFS_TYPE_DIR;
@@ -650,7 +633,7 @@ STATIC mp_obj_t littlefs_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_param) {
650633

651634
xSemaphoreGive(self->fs.littlefs.mutex);
652635

653-
m_free((void*)path);
636+
free((void*)path);
654637

655638
if (res < LFS_ERR_OK) {
656639
mp_raise_OSError(littleFsErrorToErrno(res));

esp32/littlefs/vfs_littlefs_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
171171
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-
m_free((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/lte/lteppp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void lteppp_init(void) {
159159

160160
lteppp_connstatus = LTE_PPP_IDLE;
161161
#ifdef LTE_DEBUG_BUFF
162-
lteppp_log.log = heap_caps_malloc(LTE_LOG_BUFF_SIZE, MALLOC_CAP_SPIRAM);
162+
lteppp_log.log = malloc(LTE_LOG_BUFF_SIZE);
163163
#endif
164164
}
165165

esp32/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void app_main(void) {
149149
micropy_lpwan_dio_pin_num = 23;
150150
micropy_lpwan_dio_pin = &pin_GPIO23;
151151

152-
mpTaskStack = heap_caps_malloc(MICROPY_TASK_STACK_SIZE_PSRAM, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
152+
mpTaskStack = malloc(MICROPY_TASK_STACK_SIZE_PSRAM);
153153

154154
// create the MicroPython task
155155
mpTaskHandle =
@@ -172,7 +172,7 @@ void app_main(void) {
172172
micropy_lpwan_dio_pin_num = 23;
173173
micropy_lpwan_dio_pin = &pin_GPIO23;
174174

175-
mpTaskStack = heap_caps_malloc(MICROPY_TASK_STACK_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
175+
mpTaskStack = malloc(MICROPY_TASK_STACK_SIZE);
176176

177177
// create the MicroPython task
178178
mpTaskHandle =

esp32/mods/machrmt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ STATIC mp_obj_t mach_rmt_pulses_send(mp_uint_t n_args, const mp_obj_t *pos_args,
365365

366366
/* An rmt_item32_t can contain 2 bits, calculate the number of the necessary objects needed to store the input data */
367367
mp_uint_t items_to_send_count = (data_length / 2) + (data_length % 2);
368-
rmt_item32_t* items_to_send = (rmt_item32_t*)m_malloc(items_to_send_count * sizeof(rmt_item32_t));
368+
rmt_item32_t* items_to_send = (rmt_item32_t*)malloc(items_to_send_count * sizeof(rmt_item32_t));
369369
for(mp_uint_t i = 0, j = 0; i < items_to_send_count; i++, j++) {
370370

371371
items_to_send[i].level0 = mp_obj_get_int(data_ptr[j]);
@@ -396,7 +396,7 @@ STATIC mp_obj_t mach_rmt_pulses_send(mp_uint_t n_args, const mp_obj_t *pos_args,
396396
esp_err_t retval = rmt_write_items(self->config.channel, items_to_send, items_to_send_count, wait_tx_done);
397397
MP_THREAD_GIL_ENTER();
398398

399-
m_free(items_to_send);
399+
free(items_to_send);
400400

401401
if (retval != ESP_OK) {
402402
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Could not send data!"));

esp32/mods/machtimer_alarm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "py/mpconfig.h"
99
#include "py/nlr.h"
1010
#include "py/runtime.h"
11-
#include "py/gc.h"
1211
#include "py/mperrno.h"
1312
#include "util/mpirq.h"
1413

@@ -56,7 +55,7 @@ void mach_timer_alarm_preinit(void) {
5655

5756
void mach_timer_alarm_init_heap(void) {
5857
alarm_heap.count = 0;
59-
MP_STATE_PORT(mp_alarm_heap) = gc_alloc(ALARM_HEAP_MAX_ELEMENTS * sizeof(mp_obj_alarm_t *), false);
58+
MP_STATE_PORT(mp_alarm_heap) = m_malloc(ALARM_HEAP_MAX_ELEMENTS * sizeof(mp_obj_alarm_t *));
6059
alarm_heap.data = MP_STATE_PORT(mp_alarm_heap);
6160
if (alarm_heap.data == NULL) {
6261
mp_printf(&mp_plat_print, "FATAL ERROR: not enough memory for the alarms heap\n");

0 commit comments

Comments
 (0)
0