Re-organized sub-directory by category
[staging/basesystem.git] / service / native / common_library / client / include / native_service / cl_region.h
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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  * @file cl_region.h
19  * @brief region manage
20  *
21  */
22
23 #ifndef _CL_REGION_H_  // NOLINT(build/header_guard)
24 #define _CL_REGION_H_  // NOLINT(build/header_guard)
25
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <unistd.h>
29
30 #define CL_REGION_DEFAULT_SIZE    (4 * 1024)
31
32 #define CL_ALIGNOF(type) __alignof(type)
33
34 typedef void (*cl_region_cleanup_pt)(void *data);
35
36 typedef struct cl_region_cleanup_s  cl_region_cleanup_t;
37
38 /////////////////////////////////////////////////////////////////////////////////////
39 /// \ingroup Monitor
40 /// \~english @par Brief
41 ///        region cleanup struct
42 /////////////////////////////////////////////////////////////////////////////////////
43 struct cl_region_cleanup_s {  // NOLINT(readability/nolint)
44   cl_region_cleanup_pt handler;
45   void                 *data;
46   cl_region_cleanup_t  *next;
47 };
48
49
50 typedef struct cl_region_large_s  cl_region_large_t;
51
52 /////////////////////////////////////////////////////////////////////////////////////
53 /// \ingroup Monitor
54 /// \~english @par Brief
55 ///        region large struct
56 /////////////////////////////////////////////////////////////////////////////////////
57 struct cl_region_large_s {  // NOLINT(readability/nolint)
58   cl_region_large_t *next;
59   void              *alloc;
60   size_t            size;
61 };
62
63
64 typedef struct cl_region_s cl_region_t;
65
66 /////////////////////////////////////////////////////////////////////////////////////
67 /// \ingroup Monitor
68 /// \~english @par Brief
69 ///        region data struct
70 /////////////////////////////////////////////////////////////////////////////////////
71 typedef struct {
72   uint8_t     *last;
73   uint8_t     *end;
74   cl_region_t *next;
75   int         failed;
76 } cl_region_data_t;
77
78 /////////////////////////////////////////////////////////////////////////////////////
79 /// \ingroup Monitor
80 /// \~english @par Brief
81 ///        region struct
82 /////////////////////////////////////////////////////////////////////////////////////
83 struct cl_region_s {  // NOLINT(readability/nolint)
84   cl_region_data_t    d;
85   size_t              max;
86   cl_region_t         *current;
87   cl_region_large_t   *large;
88   cl_region_cleanup_t *cleanup;
89 };
90
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95 /**
96  * @file cl_region.h
97  */
98
99 /** @addtogroup BaseSystem
100  *  @{
101  */
102 /** @addtogroup native_service
103  *  @ingroup BaseSystem
104  *  @{
105  */
106 /** @addtogroup common_library
107  *  @ingroup native_service
108  *  @{
109  */
110
111 /////////////////////////////////////////////////////////////////////////////////////
112 /// \ingroup Region
113 /// \~english @par Brief
114 ///     Region create
115 /// \~english @param [in] size
116 ///     size_t - the size to create
117 /// \~english @retval 0  Success
118 /// \~english @retval -1 Error
119 /// \~english @par Prerequisite
120 ///     None
121 /// \~english @par Change of internal state
122 ///     None
123 /// \~english @par Conditions of processing failure
124 ///     None
125 /// \~english @par Classification
126 ///     Public
127 /// \~english @par Type
128 ///     Sync
129 /// \~english @par Detail
130 ///     None
131 /// \~english @see None
132 ////////////////////////////////////////////////////////////////////////////////////
133 cl_region_t *CL_RegionCreate(size_t size);  // NOLINT(readability/nolint)
134
135
136 /////////////////////////////////////////////////////////////////////////////////////
137 /// \ingroup Region
138 /// \~english @par Brief
139 ///     Region destroy
140 /// \~english @param [in] region
141 ///     cl_region_t * - the region to destroy
142 /// \~english @retval 0  Success
143 /// \~english @retval -1 Error
144 /// \~english @par Prerequisite
145 ///     None
146 /// \~english @par Change of internal state
147 ///     None
148 /// \~english @par Conditions of processing failure
149 ///     None
150 /// \~english @par Classification
151 ///     Public
152 /// \~english @par Type
153 ///     Sync
154 /// \~english @par Detail
155 ///     None
156 /// \~english @see None
157 ////////////////////////////////////////////////////////////////////////////////////
158 void CL_RegionDestroy(cl_region_t *region);  // NOLINT(readability/nolint)
159
160
161 /////////////////////////////////////////////////////////////////////////////////////
162 /// \ingroup Region
163 /// \~english @par Brief
164 ///     Region allocation
165 /// \~english @param [in] region
166 ///     cl_region_t * - the region to allocate
167 /// \~english @param [in] size
168 ///     size_t * - the size to allocate
169 /// \~english @param [in] align_size
170 ///     size_t * - the size to align
171 /// \~english @retval the pointer to region allocated
172 /// \~english @par Prerequisite
173 ///     None
174 /// \~english @par Change of internal state
175 ///     None
176 /// \~english @par Conditions of processing failure
177 ///     None
178 /// \~english @par Classification
179 ///     Public
180 /// \~english @par Type
181 ///     Sync
182 /// \~english @par Detail
183 ///     None
184 /// \~english @see None
185 ////////////////////////////////////////////////////////////////////////////////////
186 #define CL_RegionAlloc(region, type, length) \
187   cl_region_alloc(region, sizeof(type) * length, CL_ALIGNOF(type))
188 void *cl_region_alloc(cl_region_t *region, size_t size, size_t align_size);  // NOLINT(readability/nolint)
189
190 /////////////////////////////////////////////////////////////////////////////////////
191 /// \ingroup Region
192 /// \~english @par Brief
193 ///     Region free
194 /// \~english @param [in] region
195 ///     cl_region_t * - the region to free
196 /// \~english @param [in] p
197 ///     void * - the pointer to region
198 /// \~english @retval true  Success
199 /// \~english @retval false Error
200 /// \~english @par Prerequisite
201 ///     None
202 /// \~english @par Change of internal state
203 ///     None
204 /// \~english @par Conditions of processing failure
205 ///     None
206 /// \~english @par Classification
207 ///     Public
208 /// \~english @par Type
209 ///     Sync
210 /// \~english @par Detail
211 ///     None
212 /// \~english @see None
213 ////////////////////////////////////////////////////////////////////////////////////
214 bool CL_RegionFree(cl_region_t *region, void *p);  // NOLINT(readability/nolint)
215
216
217 /////////////////////////////////////////////////////////////////////////////////////
218 /// \ingroup Region
219 /// \~english @par Brief
220 ///     add region cleanup
221 /// \~english @param [in] region
222 ///     cl_region_t * - the region to allocate
223 /// \~english @param [in] size
224 ///     size_t * - the size to add
225 /// \~english @param [in] align_size
226 ///     size_t * - the size to align
227 /// \~english @retval the pointer to region cleanup added
228 /// \~english @par Prerequisite
229 ///     None
230 /// \~english @par Change of internal state
231 ///     None
232 /// \~english @par Conditions of processing failure
233 ///     None
234 /// \~english @par Classification
235 ///     Public
236 /// \~english @par Type
237 ///     Sync
238 /// \~english @par Detail
239 ///     None
240 /// \~english @see None
241 ////////////////////////////////////////////////////////////////////////////////////
242 #define CL_RegionCleanupAdd(region, type, length) \
243   cl_region_cleanup_add(region, sizeof(type) * length, CL_ALIGNOF(type))
244 cl_region_cleanup_t *cl_region_cleanup_add(cl_region_t *region, size_t size,  // NOLINT(readability/nolint)
245                                            size_t align_size);
246
247 /** @}*/  // end of common_library
248 /** @}*/  // end of NativeService
249 /** @}*/  // end of BaseSystem
250 #ifdef __cplusplus
251 }
252 #endif
253
254
255 #endif /* _CL_REGION_H_ */  // NOLINT(build/header_guard)
256
257 /* vim:set ts=8 sw=2 sts=2: */