• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
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 
16 /*****************************************************************************
17 *
18 * File Name : wm_mem.h
19 *
20 * Description: memory manager Module
21 *
22 * Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd.
23 * All rights reserved.
24 *
25 * Author : dave
26 *
27 * Date : 2014-6-12
28 *****************************************************************************/
29 
30 #ifndef WM_MEM_H
31 #define WM_MEM_H
32 
33 #include "csi_config.h"
34 #include "wm_type_def.h"
35 
36 #if 1 // for doxygen
37 // #ifdef CONFIG_KERNEL_FREERTOS
38 #if WM_MEM_DEBUG
39 
40 #include "list.h"
41 
42 #define  MEM_HEADER_PATTERN          0x76028412
43 #define  MEM_TAILER_PATTERN          0x83395627
44 #define  MEM_FREED_PATTERN           0x82962503
45 
46 extern u32 alloc_heap_mem_bytes;
47 extern u32 alloc_heap_mem_blk_cnt;
48 extern u32 alloc_heap_mem_max_size;
49 //
50 // Note: it's important that the size of MP_MEMORY_BLOCK structure
51 //       be multiple of 16 bytes.
52 //
53 typedef struct _MEMORY_BLOCK {
54     struct dl_list  list;    /**< Pointer to next and previous blocks */
55     char  *file;             /**< name of the file which is doing the allocation */
56     u32    pad;              /**< pad to make the size of whole structure multiple of 16 bytes */
57     u32    line;             /**< line number where allocated */
58     u32    length;           /**< ulong index of trailer (=(length/4)-1 relative to data start */
59     u32    header_pattern;   /**< To help detect underflows. A trailer is also added to find overflows */
60 } MEMORY_BLOCK, *PMEMORY_BLOCK;
61 typedef struct _MEMORY_PATTERN {
62     u32 pattern0;
63 }MEMORY_PATTERN, *PMEMORY_PATTERN;
64 void mem_free_debug(void *p, char *file, int line);
65 #define tls_mem_free(p)   mem_free_debug(p, __FILE__, __LINE__)
66 void *mem_alloc_debug(u32 size, char *file, int line);
67 void mem_free_allocated_blocks(void);
68 #define tls_mem_alloc(size)   mem_alloc_debug(size, __FILE__, __LINE__)
69 void *mem_realloc_debug(void *mem_address, u32 size, char *file, int line);
70 #define tls_mem_realloc(mem_address, size)   mem_realloc_debug(mem_address, size, __FILE__, __LINE__)
71 
72 void *mem_calloc_debug(u32 n, u32 size, char *file, int line);
73 #define tls_mem_calloc(n, size) mem_calloc_debug(n, size, __FILE__, __LINE__)
74 void tls_mem_alloc_info(void);
75 int  is_safe_addr_debug(void *p, u32 len, char *file, int line);
76 #define tls_is_safe_addr(p, len)           is_safe_addr_debug(p, len, __FILE__, __LINE__)
77 #if 1
78 #define MEMCPY memcpy
79 #define SMEMCPY MEMCPY
80 #else
81 #define MEMCPY(dst, src, len)             do { \
82     if (tls_is_safe_addr(dst, len)) { \
83     memcpy_s(dst, sizeof(dst), src, len);}}while (0)
84 
85 #define SMEMCPY(dst, src, len)            do { \
86     if (tls_is_safe_addr(dst, len)) { \
87     memcpy_s(dst, sizeof(dst), src, len);}}while (0)
88 #endif
89 #else /* WM_MEM_DEBUG */
90 
91 void *mem_alloc_debug(u32 size);
92 void mem_free_debug(void *p);
93 void *mem_realloc_debug(void *mem_address, u32 size);
94 void *mem_calloc_debug(u32 length, u32 size);
95 
96 /**
97  * @defgroup System_APIs System APIs
98  * @brief System APIs
99  */
100 
101 /**
102  * @addtogroup System_APIs
103  * @{
104  */
105 
106 /**
107  * @defgroup MEM_APIs MEM APIs
108  * @brief memory manager APIs
109  */
110 
111 /**
112  * @addtogroup MEM_APIs
113  * @{
114  */
115 
116 /**
117  * @brief          This function is called by memory allocation
118  *
119  * @param[in]      size
120  *
121  * @retval         NULL    malloc failed
122  * @retval         pointer pointer to the address of the allocated memory
123  *
124  * @note           None
125  */
126 #define tls_mem_alloc(size)   mem_alloc_debug(size)
127 
128 /**
129  * @brief      This function is used to free memory
130  *
131  * @param      None
132  *
133  * @return     None
134  *
135  * @note       None
136  */
137 #define tls_mem_free       mem_free_debug
138 
139 /**
140  * @brief          This function is used to realloc memory
141  *
142  * @param             None
143  *
144  * @retval         NULL    realloc failed
145  * @retval         Pointer pointer to the address of the allocated memory
146  *
147  * @note           None
148  */
149 #define tls_mem_realloc   mem_realloc_debug
150 
151 /**
152  * @brief          This function is used to calloc memory
153  *
154  * @param             None
155  *
156  * @retval         NULL    realloc failed
157  * @retval         Pointer pointer to the address of the allocated memory
158  *
159  * @note           None
160  */
161 #define tls_mem_calloc    mem_calloc_debug
162 
163 /**
164  * @brief          This function is used to copy memory content from one address to another address
165  *
166  * @param[in]      dst   pointer to destination address
167  * @param[in]      src   pointer to source address
168  * @param[in]      len   length to copy
169  *
170  * @retval         dst
171  *
172  * @note           None
173  */
174 #define MEMCPY(dst, src, len)      memcpy(dst, src, len)
175 
176 /**
177  * @brief          This function is used to copy memory content from one address to another address
178  *
179  * @param[in]      dst   pointer to destination address
180  * @param[in]      src   pointer to source address
181  * @param[in]      len   length to copy
182  *
183  * @retval         dst
184  *
185  * @note           None
186  */
187 #define SMEMCPY(dst, src, len)    memcpy_s(dst, sizeof(dst), src, len)
188 
189 /**
190  * @}
191  */
192 
193 /**
194  * @}
195  */
196 
197 #endif /* WM_MEM_DEBUG */
198 #else /* CONFIG_KERNEL_FREERTOS */
199 #define tls_mem_alloc(size)   malloc(size)
200 #define tls_mem_free       free
201 #define tls_mem_realloc   realloc
202 #define tls_mem_calloc    calloc
203 #define MEMCPY(dst, src, len)      memcpy_s(dst, sizeof(dst), src, len)
204 #define SMEMCPY(dst, src, len)    memcpy_s(dst, sizeof(dst), src, len)
205 #endif /* CONFIG_KERNEL_FREERTOS */
206 
207 #endif /* TLS_MEM_H */