From 76c81cd5a631f2329a4ec7a5cb53632eae017af4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 3 May 2016 00:35:11 +0300 Subject: [PATCH] esp8266/modesp: Add malloc() and free() functions. Useful for testing fragmentation issues in OS heap. E.g. freemem() may report large amount, but is it possible to actually allocate block of a given size? Issue malloc() (followed by free()) to find out. --- esp8266/etshal.h | 4 ++++ esp8266/modesp.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/esp8266/etshal.h b/esp8266/etshal.h index d8a57e8c75..0185a9e22c 100644 --- a/esp8266/etshal.h +++ b/esp8266/etshal.h @@ -20,4 +20,8 @@ void ets_timer_arm_new(os_timer_t *tim, uint32_t millis, bool repeat, bool is_mi void ets_timer_setfn(os_timer_t *tim, ETSTimerFunc callback, void *cb_data); void ets_timer_disarm(os_timer_t *tim); +// These prototypes are for recent SDKs with "malloc tracking" +void *pvPortMalloc(unsigned sz, const char *fname, int line); +void vPortFree(void *p, const char *fname, int line); + #endif // _INCLUDED_ETSHAL_H_ diff --git a/esp8266/modesp.c b/esp8266/modesp.c index 143cd6ad33..44401d3a61 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -41,6 +41,7 @@ #include "user_interface.h" #include "espconn.h" #include "spi_flash.h" +#include "mem.h" #include "espneopixel.h" #include "modpyb.h" @@ -646,6 +647,17 @@ STATIC mp_obj_t esp_meminfo() { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_meminfo_obj, esp_meminfo); +STATIC mp_obj_t esp_malloc(mp_obj_t size_in) { + return MP_OBJ_NEW_SMALL_INT((mp_uint_t)os_malloc(mp_obj_get_int(size_in))); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_malloc_obj, esp_malloc); + +STATIC mp_obj_t esp_free(mp_obj_t addr_in) { + os_free((void*)mp_obj_get_int(addr_in)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_free_obj, esp_free); + STATIC mp_obj_t esp_esf_free_bufs(mp_obj_t idx_in) { return MP_OBJ_NEW_SMALL_INT(ets_esf_free_bufs(mp_obj_get_int(idx_in))); } @@ -670,6 +682,8 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_freemem), (mp_obj_t)&esp_freemem_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_meminfo), (mp_obj_t)&esp_meminfo_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, // TODO delete/rename/move elsewhere + { MP_OBJ_NEW_QSTR(MP_QSTR_malloc), (mp_obj_t)&esp_malloc_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_free), (mp_obj_t)&esp_free_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_esf_free_bufs), (mp_obj_t)&esp_esf_free_bufs_obj }, #if MODESP_INCLUDE_CONSTANTS