1 // Copyright 2015-2016 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 <string.h>
16 #include <stdlib.h>
17 #include <sys/reent.h>
18 #include <malloc.h>
19 #include "esp_heap_caps.h"
20
21
22 /*
23 These contain the business logic for the malloc() and realloc() implementation. Because of heap tracing
24 wrapping reasons, we do not want these to be a public api, however, so they're not defined publicly.
25 */
26 extern void *heap_caps_malloc_default( size_t size );
27 extern void *heap_caps_realloc_default( void *ptr, size_t size );
28
malloc(size_t size)29 void* malloc(size_t size)
30 {
31 return heap_caps_malloc_default(size);
32 }
33
calloc(size_t n,size_t size)34 void* calloc(size_t n, size_t size)
35 {
36 return _calloc_r(_REENT, n, size);
37 }
38
realloc(void * ptr,size_t size)39 void* realloc(void* ptr, size_t size)
40 {
41 return heap_caps_realloc_default(ptr, size);
42 }
43
free(void * ptr)44 void free(void *ptr)
45 {
46 heap_caps_free(ptr);
47 }
48
_malloc_r(struct _reent * r,size_t size)49 void* _malloc_r(struct _reent *r, size_t size)
50 {
51 return heap_caps_malloc_default(size);
52 }
53
_free_r(struct _reent * r,void * ptr)54 void _free_r(struct _reent *r, void* ptr)
55 {
56 heap_caps_free(ptr);
57 }
58
_realloc_r(struct _reent * r,void * ptr,size_t size)59 void* _realloc_r(struct _reent *r, void* ptr, size_t size)
60 {
61 return heap_caps_realloc_default( ptr, size );
62 }
63
_calloc_r(struct _reent * r,size_t nmemb,size_t size)64 void* _calloc_r(struct _reent *r, size_t nmemb, size_t size)
65 {
66 void *result;
67 size_t size_bytes;
68 if (__builtin_mul_overflow(nmemb, size, &size_bytes)) {
69 return NULL;
70 }
71
72 result = heap_caps_malloc_default(size_bytes);
73 if (result != NULL) {
74 bzero(result, size_bytes);
75 }
76 return result;
77 }
78
memalign(size_t alignment,size_t n)79 void* memalign(size_t alignment, size_t n)
80 {
81 return heap_caps_aligned_alloc(alignment, n, MALLOC_CAP_DEFAULT);
82 }
83
84 /* No-op function, used to force linking this file,
85 instead of the heap implementation from newlib.
86 */
newlib_include_heap_impl(void)87 void newlib_include_heap_impl(void)
88 {
89 }
90
91 /* The following functions are implemented by newlib's heap allocator,
92 but aren't available in the heap component.
93 Define them as non-functional stubs here, so that the application
94 can not cause the newlib heap implementation to be linked in
95 */
96
malloc_trim(size_t pad)97 int malloc_trim(size_t pad)
98 {
99 return 0; // indicates failure
100 }
101
malloc_usable_size(void * p)102 size_t malloc_usable_size(void* p)
103 {
104 return 0;
105 }
106
malloc_stats(void)107 void malloc_stats(void)
108 {
109 }
110
mallopt(int parameter_number,int parameter_value)111 int mallopt(int parameter_number, int parameter_value)
112 {
113 return 0; // indicates failure
114 }
115
mallinfo(void)116 struct mallinfo mallinfo(void)
117 {
118 struct mallinfo dummy = {0};
119 return dummy;
120 }
121
122 void* valloc(size_t n) __attribute__((alias("malloc")));
123 void* pvalloc(size_t n) __attribute__((alias("malloc")));
124 void cfree(void* p) __attribute__((alias("free")));
125