VSF Documented
code_region.h
Go to the documentation of this file.
1/****************************************************************************
2* Copyright 2020 by Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
3* *
4* Licensed under the Apache License, Version 2.0 (the "License"); *
5* you may not use this file except in compliance with the License. *
6* You may obtain a copy of the License at *
7* *
8* http://www.apache.org/licenses/LICENSE-2.0 *
9* *
10* Unless required by applicable law or agreed to in writing, software *
11* distributed under the License is distributed on an "AS IS" BASIS, *
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13* See the License for the specific language governing permissions and *
14* limitations under the License. *
15* *
16****************************************************************************/
17
18/*****************************************************************************
19 * Copyright(C)2009-2022 by VSF Team *
20 * *
21 * Licensed under the Apache License, Version 2.0 (the "License"); *
22 * you may not use this file except in compliance with the License. *
23 * You may obtain a copy of the License at *
24 * *
25 * http://www.apache.org/licenses/LICENSE-2.0 *
26 * *
27 * Unless required by applicable law or agreed to in writing, software *
28 * distributed under the License is distributed on an "AS IS" BASIS, *
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
30 * See the License for the specific language governing permissions and *
31 * limitations under the License. *
32 * *
33 ****************************************************************************/
34
35#ifndef __CODE_REGION_H__
36#define __CODE_REGION_H__
37
38/*============================ INCLUDES ======================================*/
39#include "../compiler/compiler.h"
40
101#ifdef __cplusplus
102extern "C" {
103#endif
104
105/*============================ MACROS ========================================*/
106#ifndef COMPILER_PATCH_CODE_REGION_LOCAL_SIZE
107# define COMPILER_PATCH_CODE_REGION_LOCAL_SIZE 4
108#endif
109
110/*============================ MACROFIED FUNCTIONS ===========================*/
111
112#if __IS_COMPILER_IAR__
113# define __CODE_REGION(__REGION_ADDR) \
114 for(code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
115 NULL != code_region_ptr; \
116 code_region_ptr = NULL) \
117 for(uint8_t local[COMPILER_PATCH_CODE_REGION_LOCAL_SIZE], \
118 VSF_MCONNECT2(__code_region_, __LINE__) = 1; \
119 VSF_MCONNECT2(__code_region_, __LINE__)-- ? \
120 (code_region_ptr->methods_ptr->OnEnter( \
121 code_region_ptr->target_ptr, local) \
122 ,1) \
123 : 0; \
124 code_region_ptr->methods_ptr->OnLeave( \
125 code_region_ptr->target_ptr, local))
126
127# define __CODE_REGION_START(__REGION_ADDR) __CODE_REGION(__REGION_ADDR) {
128# define __CODE_REGION_END() }
129
130# define __CODE_REGION_SIMPLE(__REGION_ADDR, ...) \
131 do {if (NULL != (__REGION_ADDR)) { \
132 code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
133 uint8_t local[COMPILER_PATCH_CODE_REGION_LOCAL_SIZE]; \
134 code_region_ptr->methods_ptr->OnEnter( \
135 code_region_ptr->target_ptr, local); \
136 __VA_ARGS__; \
137 code_region_ptr->methods_ptr->OnLeave( \
138 code_region_ptr->target_ptr, local); \
139 } } while(0);
140
141# define __CODE_REGION_SIMPLE_START(__REGION_ADDR, ...) \
142 do {if (NULL != (__REGION_ADDR)) { \
143 code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
144 uint8_t local[COMPILER_PATCH_CODE_REGION_LOCAL_SIZE]; \
145 code_region_ptr->methods_ptr->OnEnter(code_region_ptr->target_ptr, local);
146
147# define __CODE_REGION_SIMPLE_END(__REGION_ADDR, ...) \
148 code_region_ptr->methods_ptr->OnLeave( \
149 code_region_ptr->target_ptr, local); \
150 } } while(0);
151
152
153# define EXIT_CODE_REGION() \
154 code_region_ptr->ptMethods->OnLeave(code_region_ptr->target_ptr, local)
155# define exit_code_region() EXIT_CODE_REGION()
156
157# define CODE_REGION(__REGION_ADDR) __CODE_REGION((__REGION_ADDR))
158# define code_region(__region_addr) __CODE_REGION((__region_addr))
159
160#else
161
162
163# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
164# define __CODE_REGION_SIMPLE(__REGION_ADDR, __CODE) \
165 do {if (NULL != (__REGION_ADDR)) { \
166 code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
167 uint8_t local[COMPILER_PATCH_CODE_REGION_LOCAL_SIZE]; \
168 code_region_ptr->methods_ptr->OnEnter( \
169 code_region_ptr->target_ptr, local); \
170 __CODE; \
171 code_region_ptr->methods_ptr->OnLeave( \
172 code_region_ptr->target_ptr, local); \
173 } }while(0);
174
175# define __CODE_REGION_SIMPLE_START(__REGION_ADDR) \
176 do {if (NULL != (__REGION_ADDR)) { \
177 code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
178 uint8_t local[COMPILER_PATCH_CODE_REGION_LOCAL_SIZE]; \
179 code_region_ptr->methods_ptr->OnEnter(code_region_ptr->target_ptr, local);
180
181# define __CODE_REGION_SIMPLE_END(__REGION_ADDR) \
182 code_region_ptr->methods_ptr->OnLeave( \
183 code_region_ptr->target_ptr, local); \
184 } } while(0);
185# else
186
187/* code region require C99 and above */
188# define __CODE_REGION(__REGION_ADDR) \
189 for(code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
190 NULL != code_region_ptr; \
191 code_region_ptr = NULL) \
192 for(uint8_t local[code_region_ptr->methods_ptr->local_obj_size], \
193 VSF_MCONNECT2(__code_region_, __LINE__) = 1; \
194 VSF_MCONNECT2(__code_region_, __LINE__)-- ? \
195 (code_region_ptr->methods_ptr->OnEnter( \
196 code_region_ptr->target_ptr, local) \
197 ,1) \
198 : 0; \
199 code_region_ptr->methods_ptr->OnLeave( \
200 code_region_ptr->target_ptr, local))
201
202# define __CODE_REGION_START(__REGION_ADDR) __CODE_REGION(__REGION_ADDR) {
203# define __CODE_REGION_END() }
204
205
206# define __CODE_REGION_SIMPLE(__REGION_ADDR, ...) \
207 do {if (NULL != (__REGION_ADDR)) { \
208 code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
209 uint8_t local[code_region_ptr->methods_ptr->local_obj_size]; \
210 code_region_ptr->methods_ptr->OnEnter( \
211 code_region_ptr->target_ptr, local); \
212 __VA_ARGS__; \
213 code_region_ptr->methods_ptr->OnLeave( \
214 code_region_ptr->target_ptr, local); \
215 } }while(0);
216
217# define __CODE_REGION_SIMPLE_START(__REGION_ADDR, ...) \
218 do {if (NULL != (__REGION_ADDR)) { \
219 code_region_t *code_region_ptr = (code_region_t *)(__REGION_ADDR); \
220 uint8_t local[code_region_ptr->methods_ptr->local_obj_size]; \
221 code_region_ptr->methods_ptr->OnEnter(code_region_ptr->target_ptr, local);
222
223# define __CODE_REGION_SIMPLE_END(__REGION_ADDR, ...) \
224 code_region_ptr->methods_ptr->OnLeave( \
225 code_region_ptr->target_ptr, local); \
226 } } while(0);
227# endif
228
229# define EXIT_CODE_REGION() \
230 code_region_ptr->ptMethods->OnLeave(code_region_ptr->target_ptr, local)
231# define exit_code_region() EXIT_CODE_REGION()
232
233# define CODE_REGION(__REGION_ADDR) __CODE_REGION((__REGION_ADDR))
234# define code_region(__region_addr) __CODE_REGION((__region_addr))
235
236#endif
237
238
239#define CODE_REGION_START(__REGION_ADDR) __CODE_REGION_START((__REGION_ADDR))
240#define CODE_REGION_END() __CODE_REGION_END()
241
242#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
243#define CODE_REGION_SIMPLE(__REGION_ADDR, __CODE) \
244 __CODE_REGION_SIMPLE((__REGION_ADDR), __CODE)
245#define code_region_simple(__REGION_ADDR, __CODE) \
246 __CODE_REGION_SIMPLE((__REGION_ADDR), __CODE)
247#else
248#define CODE_REGION_SIMPLE(__REGION_ADDR, ...) \
249 __CODE_REGION_SIMPLE((__REGION_ADDR), __VA_ARGS__)
250#define code_region_simple(__REGION_ADDR, ...) \
251 __CODE_REGION_SIMPLE((__REGION_ADDR), __VA_ARGS__)
252#endif
253
254#define CODE_REGION_SIMPLE_START(__REGION_ADDR) \
255 __CODE_REGION_SIMPLE_START((__REGION_ADDR))
256#define CODE_REGION_SIMPLE_END() \
257 __CODE_REGION_SIMPLE_END()
258
259
260
261/*============================ TYPES =========================================*/
262typedef struct {
264 void (*OnEnter)(void *obj_ptr, void *local_ptr);
265 void (*OnLeave)(void *obj_ptr, void *local_ptr);
267
268typedef struct {
272
273/*============================ GLOBAL VARIABLES ==============================*/
275/*============================ PROTOTYPES ====================================*/
276
277#ifdef __cplusplus
278}
279#endif
280
281#endif
const code_region_t DEFAULT_CODE_REGION_NONE
Definition code_region.c:56
unsigned char uint_fast8_t
Definition stdint.h:23
Definition code_region.h:268
void * target_ptr
Definition code_region.h:269
i_code_region_t * methods_ptr
Definition code_region.h:270
Definition code_region.h:262
uint_fast8_t local_obj_size
Definition code_region.h:263