1 // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <esp_attr.h>
16 #include <esp_heap_caps.h>
17 #include <sdkconfig.h>
18 #include "esp_mem.h"
19
20 #ifndef CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC
21
esp_mbedtls_mem_calloc(size_t n,size_t size)22 IRAM_ATTR void *esp_mbedtls_mem_calloc(size_t n, size_t size)
23 {
24 #ifdef CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC
25 return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
26 #elif CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC
27 return heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT);
28 #elif CONFIG_MBEDTLS_IRAM_8BIT_MEM_ALLOC
29 #ifdef CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN
30 if ((n*size) >= CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN || (n*size) >= CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN) {
31 #else
32 if ((n*size) >= CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN) {
33 #endif
34 return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
35 } else {
36 return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
37 }
38
39 #else
40 return calloc(n, size);
41 #endif
42 }
43
44 IRAM_ATTR void esp_mbedtls_mem_free(void *ptr)
45 {
46 return heap_caps_free(ptr);
47 }
48
49 #endif /* !CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC */
50