mimxrt/mbedtls: Use core-provided tracked alloc instead of custom funcs.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-05-04 12:24:43 +10:00
parent 39c96b543f
commit da31ad7aad
3 changed files with 6 additions and 68 deletions

View File

@ -88,10 +88,10 @@
// Memory allocation hooks
#include <stdlib.h>
#include <stdio.h>
void *m_calloc_mbedtls(size_t nmemb, size_t size);
void m_free_mbedtls(void *ptr);
#define MBEDTLS_PLATFORM_STD_CALLOC m_calloc_mbedtls
#define MBEDTLS_PLATFORM_STD_FREE m_free_mbedtls
void *m_tracked_calloc(size_t nmemb, size_t size);
void m_tracked_free(void *ptr);
#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc
#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf
#include "mbedtls/check_config.h"

View File

@ -26,64 +26,9 @@
#ifdef MICROPY_SSL_MBEDTLS
#include "py/runtime.h"
#include "py/gc.h"
#include "fsl_trng.h"
#include "mbedtls_config.h"
#define DEBUG (0)
#if DEBUG
static size_t count_links(uint32_t *nb) {
void **p = MP_STATE_PORT(mbedtls_memory);
size_t n = 0;
*nb = 0;
while (p != NULL) {
++n;
*nb += gc_nbytes(p);
p = (void **)p[1];
}
return n;
}
#endif
void *m_calloc_mbedtls(size_t nmemb, size_t size) {
void **ptr = m_malloc0(nmemb * size + 2 * sizeof(uintptr_t));
#if DEBUG
uint32_t nb;
size_t n = count_links(&nb);
printf("mbed_alloc(%u, %u) -> (%u;%u) %p\n", nmemb, size, n, (uint)nb, ptr);
#endif
if (MP_STATE_PORT(mbedtls_memory) != NULL) {
MP_STATE_PORT(mbedtls_memory)[0] = ptr;
}
ptr[0] = NULL;
ptr[1] = MP_STATE_PORT(mbedtls_memory);
MP_STATE_PORT(mbedtls_memory) = ptr;
return &ptr[2];
}
void m_free_mbedtls(void *ptr_in) {
if (ptr_in == NULL) {
return;
}
void **ptr = &((void **)ptr_in)[-2];
#if DEBUG
uint32_t nb;
size_t n = count_links(&nb);
printf("mbed_free(%p, [%p, %p], nbytes=%u, links=%u;%u)\n", ptr, ptr[0], ptr[1], gc_nbytes(ptr), n, (uint)nb);
#endif
if (ptr[1] != NULL) {
((void **)ptr[1])[0] = ptr[0];
}
if (ptr[0] != NULL) {
((void **)ptr[0])[1] = ptr[1];
} else {
MP_STATE_PORT(mbedtls_memory) = ptr[1];
}
m_free(ptr);
}
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
// assumes that TRNG_Init was called during startup

View File

@ -48,6 +48,7 @@ uint32_t trng_random_u32(void);
#define MICROPY_OPT_MAP_LOOKUP_CACHE (1)
// Python internal features
#define MICROPY_TRACKED_ALLOC (MICROPY_SSL_MBEDTLS)
#define MICROPY_READER_VFS (1)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_ENABLE_FINALISER (1)
@ -250,12 +251,6 @@ extern const struct _mp_obj_module_t mp_module_network;
#define SOCKET_BUILTIN_MODULE
#endif
#if MICROPY_SSL_MBEDTLS
#define MICROPY_PORT_ROOT_POINTER_MBEDTLS void **mbedtls_memory;
#else
#define MICROPY_PORT_ROOT_POINTER_MBEDTLS
#endif
#if defined(MICROPY_HW_ETH_MDC)
extern const struct _mp_obj_type_t network_lan_type;
#define MICROPY_HW_NIC_ETH { MP_ROM_QSTR(MP_QSTR_LAN), MP_ROM_PTR(&network_lan_type) },
@ -290,10 +285,8 @@ extern const struct _mp_obj_type_t network_lan_type;
void *machine_pin_irq_objects[MICROPY_HW_NUM_PIN_IRQS]; \
/* list of registered NICs */ \
mp_obj_list_t mod_network_nic_list; \
/* root pointers for sub-systems */ \
MICROPY_PORT_ROOT_POINTER_MBEDTLS \
/* root pointers defined by a board */ \
MICROPY_BOARD_ROOT_POINTERS \
MICROPY_BOARD_ROOT_POINTERS \
#define MP_STATE_PORT MP_STATE_VM