• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if HEAP_MEMORY_DEBUG
33 
34 void osi_mem_dbg_init(void);
35 void osi_mem_dbg_record(void *p, int size, const char *func, int line);
36 void osi_mem_dbg_clean(void *p, const char *func, int line);
37 void osi_mem_dbg_show(void);
38 uint32_t osi_mem_dbg_get_max_size(void);
39 uint32_t osi_mem_dbg_get_current_size(void);
40 void osi_men_dbg_set_section_start(uint8_t index);
41 void osi_men_dbg_set_section_end(uint8_t index);
42 uint32_t osi_mem_dbg_get_max_size_section(uint8_t index);
43 
44 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
45 #define osi_malloc(size)                                \
46 ({                                                      \
47     void *p;                                            \
48     p = heap_caps_malloc_prefer(size, 2,                \
49         MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,           \
50         MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);        \
51     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
52     (void *)p;                                          \
53 })
54 
55 #define osi_calloc(size)                                \
56 ({                                                      \
57     void *p;                                            \
58     p = heap_caps_calloc_prefer(1, size, 2,             \
59         MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,           \
60         MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);        \
61     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
62     (void *)p;                                          \
63 })
64 
65 #else
66 
67 #define osi_malloc(size)                                \
68 ({                                                      \
69     void *p;                                            \
70     p = malloc((size));                                 \
71     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
72     (void *)p;                                          \
73 })
74 
75 #define osi_calloc(size)                                \
76 ({                                                      \
77     void *p;                                            \
78     p = calloc(1, (size));                              \
79     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
80     (void *)p;                                          \
81 })
82 
83 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
84 
85 
86 #if 0
87 #define osi_malloc(size)                                \
88 do {                                                    \
89     void *p;                                            \
90                                                         \
91 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST              \
92     p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
93 #else                                                   \
94     p = malloc((size));                                 \
95 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */ \
96     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
97     (void *)p;                                          \
98 }while(0)
99 
100 #define osi_calloc(size)                                \
101 do {                                                    \
102     void *p;                                            \
103                                                         \
104 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST              \
105         p = heap_caps_calloc_prefer(1, size, 2,         \
106             MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,       \
107             MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);    \
108 #else                                                   \
109     p = calloc(1, (size));                              \
110 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */ \
111     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
112     (void *)p;                                          \
113 } while(0)
114 #endif
115 
116 #define osi_free(ptr)                                   \
117 do {                                                    \
118     void *tmp_point = (void *)(ptr);                    \
119     osi_mem_dbg_clean(tmp_point, __func__, __LINE__);   \
120     free(tmp_point);                                    \
121 } while (0)
122 
123 #else
124 
125 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
126 #define osi_malloc(size)                  heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
127 #define osi_calloc(size)                  heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
128 #else
129 #define osi_malloc(size)                  malloc((size))
130 #define osi_calloc(size)                  calloc(1, (size))
131 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
132 #define osi_free(p)                       free((p))
133 
134 #endif /* HEAP_MEMORY_DEBUG */
135 
136 #define FREE_AND_RESET(a)   \
137 do {                        \
138     if (a) {                \
139         osi_free(a);        \
140         a = NULL;           \
141     }                       \
142 }while (0)
143 
144 
145 #endif /* _ALLOCATOR_H_ */
146