57#if LWIP_ALTCP_TLS && LWIP_ALTCP_TLS_MBEDTLS
67#ifndef ALTCP_MBEDTLS_MEM_DEBUG
68#define ALTCP_MBEDTLS_MEM_DEBUG LWIP_DBG_OFF
71#if defined(MBEDTLS_PLATFORM_MEMORY) && \
72 (!defined(MBEDTLS_PLATFORM_FREE_MACRO) || \
73 !defined(MBEDTLS_PLATFORM_CALLOC_MACRO))
74#define ALTCP_MBEDTLS_PLATFORM_ALLOC 1
76#define ALTCP_MBEDTLS_PLATFORM_ALLOC 0
79#if ALTCP_MBEDTLS_PLATFORM_ALLOC
81#ifndef ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
82#define ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS 0
86typedef struct altcp_mbedtls_malloc_helper_s {
89} altcp_mbedtls_malloc_helper_t;
91#if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
92typedef struct altcp_mbedtls_malloc_stats_s {
97} altcp_mbedtls_malloc_stats_t;
98altcp_mbedtls_malloc_stats_t altcp_mbedtls_malloc_stats;
99volatile int altcp_mbedtls_malloc_clear_stats;
103tls_malloc(
size_t c,
size_t len)
105 altcp_mbedtls_malloc_helper_t *hlpr;
108#if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
109 if (altcp_mbedtls_malloc_clear_stats) {
110 altcp_mbedtls_malloc_clear_stats = 0;
111 memset(&altcp_mbedtls_malloc_stats, 0,
sizeof(altcp_mbedtls_malloc_stats));
114 alloc_size =
sizeof(altcp_mbedtls_malloc_helper_t) + (
c *
len);
117 LWIP_DEBUGF(ALTCP_MBEDTLS_MEM_DEBUG, (
"mbedtls allocation too big: %c * %d bytes vs MEM_SIZE=%d\n",
123 LWIP_DEBUGF(ALTCP_MBEDTLS_MEM_DEBUG, (
"mbedtls alloc callback failed for %c * %d bytes\n", (
int)
c, (
int)
len));
126#if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
127 altcp_mbedtls_malloc_stats.allocCnt++;
128 altcp_mbedtls_malloc_stats.allocedBytes +=
c *
len;
129 if (altcp_mbedtls_malloc_stats.allocedBytes > altcp_mbedtls_malloc_stats.maxBytes) {
130 altcp_mbedtls_malloc_stats.maxBytes = altcp_mbedtls_malloc_stats.allocedBytes;
132 altcp_mbedtls_malloc_stats.totalBytes +=
c *
len;
145 altcp_mbedtls_malloc_helper_t *hlpr;
150 hlpr = ((altcp_mbedtls_malloc_helper_t *)
ptr) - 1;
151#if ALTCP_MBEDTLS_PLATFORM_ALLOC_STATS
152 if (!altcp_mbedtls_malloc_clear_stats) {
153 altcp_mbedtls_malloc_stats.allocedBytes -= hlpr->c * hlpr->len;
161altcp_mbedtls_mem_init(
void)
165#if ALTCP_MBEDTLS_PLATFORM_ALLOC
167 mbedtls_platform_set_calloc_free(&tls_malloc, &tls_free);
171altcp_mbedtls_state_t *
172altcp_mbedtls_alloc(
void *conf)
174 altcp_mbedtls_state_t *
ret = (altcp_mbedtls_state_t *)
mem_calloc(1,
sizeof(altcp_mbedtls_state_t));
182altcp_mbedtls_free(
void *conf, altcp_mbedtls_state_t *
state)
190altcp_mbedtls_alloc_config(
size_t size)
194 if (
size != checked_size) {
203altcp_mbedtls_free_config(
void *
item)
#define mem_free(ptr, bsize)
void * mem_calloc(mem_size_t count, mem_size_t size)
void * mem_malloc(mem_size_t size_in)
#define LWIP_DEBUGF(debug, message)
#define LWIP_ASSERT(message, assertion)
#define LWIP_UNUSED_ARG(x)