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.
This commit is contained in:
Paul Sokolovsky 2016-05-03 00:35:11 +03:00
parent 2123ced3f4
commit 76c81cd5a6
2 changed files with 18 additions and 0 deletions

View File

@ -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_

View File

@ -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