1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef _ALLOCATOR_H_
20 #define _ALLOCATOR_H_
21 
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include "esp_heap_caps.h"
25 
26 char *osi_strdup(const char *str);
27 
28 void *osi_malloc_func(size_t size);
29 void *osi_calloc_func(size_t size);
30 void osi_free_func(void *ptr);
31 
32 // Memory alloc function without print and assertion
33 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
34 #define osi_malloc_base(size)             heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
35 #define osi_calloc_base(size)             heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
36 #else
37 #define osi_malloc_base(size)             malloc((size))
38 #define osi_calloc_base(size)             calloc(1, (size))
39 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
40 
41 #if HEAP_MEMORY_DEBUG
42 
43 void osi_mem_dbg_init(void);
44 void osi_mem_dbg_record(void *p, int size, const char *func, int line);
45 void osi_mem_dbg_clean(void *p, const char *func, int line);
46 void osi_mem_dbg_show(void);
47 uint32_t osi_mem_dbg_get_max_size(void);
48 uint32_t osi_mem_dbg_get_current_size(void);
49 void osi_men_dbg_set_section_start(uint8_t index);
50 void osi_men_dbg_set_section_end(uint8_t index);
51 uint32_t osi_mem_dbg_get_max_size_section(uint8_t index);
52 
53 #define osi_malloc(size)                                \
54 ({                                                      \
55     void *p;                                            \
56     p = osi_malloc_base(size);                          \
57     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
58     (void *)p;                                          \
59 })
60 
61 #define osi_calloc(size)                                \
62 ({                                                      \
63     void *p;                                            \
64     p = osi_calloc_base(size);                          \
65     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
66     (void *)p;                                          \
67 })
68 
69 #if 0
70 #define osi_malloc(size)                                \
71 do {                                                    \
72     void *p;                                            \
73                                                         \
74 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST              \
75     p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
76 #else                                                   \
77     p = malloc((size));                                 \
78 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */ \
79     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
80     (void *)p;                                          \
81 }while(0)
82 
83 #define osi_calloc(size)                                \
84 do {                                                    \
85     void *p;                                            \
86                                                         \
87 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST              \
88         p = heap_caps_calloc_prefer(1, size, 2,         \
89             MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,       \
90             MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);    \
91 #else                                                   \
92     p = calloc(1, (size));                              \
93 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */ \
94     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
95     (void *)p;                                          \
96 } while(0)
97 #endif
98 
99 #define osi_free(ptr)                                   \
100 do {                                                    \
101     void *tmp_point = (void *)(ptr);                    \
102     osi_mem_dbg_clean(tmp_point, __func__, __LINE__);   \
103     free(tmp_point);                                    \
104 } while (0)
105 
106 #else
107 
108 // Memory alloc function with print and assertion when fails
109 #define osi_malloc(size)                  osi_malloc_func((size))
110 #define osi_calloc(size)                  osi_calloc_func((size))
111 #define osi_free(p)                       free((p))
112 
113 #endif /* HEAP_MEMORY_DEBUG */
114 
115 #define FREE_AND_RESET(a)   \
116 do {                        \
117     if (a) {                \
118         osi_free(a);        \
119         a = NULL;           \
120     }                       \
121 }while (0)
122 
123 
124 #endif /* _ALLOCATOR_H_ */
125