Accelerator Independent Data Access / PVAccess 2.0
AIDA-PVA is the latest version of the AIDA framework. Built on top of EPICS 7 it enables client applications to programmatically access and manage any device or database on the SLAC Network using simple channel names.
Loading...
Searching...
No Matches
aida_pva_memory.h
Go to the documentation of this file.
1/** @file
2 * @brief The Header File for the memory management functions and macros.
3 * **CMS**=C_INC
4 */
5#ifndef aida_pva_memory_h
6#define aida_pva_memory_h
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include "aida_pva.h"
12
13/**
14 * The maximum number of pointers that the Memory tracking and management functions can handle
15 */
16#define MAX_POINTERS 100
17
18/**
19 * Create tracking variables so that memory can be freed with FREE_MEMORY macro.
20 * Creates up to MAX_POINTERS pointers to track all memory allocations so that
21 * they can be safely freed, when needed.
22 * Creates local variables to store the tracking information so these macros can only
23 * be used within a single block.
24 */
25#define TRACK_ALLOCATED_MEMORY \
26 int _nAllocationsToFree = 0, _n_jsonValuesToFree = 0; \
27 void *_memoryAllocationsToFree[MAX_POINTERS] ; \
28 json_value *_jsonValuesToFree[MAX_POINTERS] ;
29
30/**
31 * Register this newly allocated memory so that it will be freed by FREE_MEMORY.
32 */
33#define TRACK_MEMORY(_ptr) \
34 if (_ptr) _memoryAllocationsToFree[_nAllocationsToFree++] = (_ptr);
35
36/**
37 * Register this newly allocated json value so that it will be freed by FREE_JSON_MEMORY.
38 */
39#define TRACK_JSON(_ptr) \
40 if (_ptr) _jsonValuesToFree[_n_jsonValuesToFree++] = (_ptr);
41
42/**
43 * Allocate memory. Allocates memory of the given size
44 *
45 * @param _env The JNI environment. Used in all functions involving JNI
46 * @param _size size of memory to allocate
47 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
48 */
49#define ALLOCATE_MEMORY(_env, _size, _purpose) allocateMemory(_env, NULL, _size, false, "Could not allocate space for " _purpose)
50
51/**
52 * Allocate memory and set its contents to the given buffer of given size.
53 *
54 * @param _env The JNI environment. Used in all functions involving JNI
55 * @param _source buffer to copy contents from
56 * @param _size size of memory to allocate
57 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
58 */
59#define ALLOCATE_AND_COPY_MEMORY(_env, _source, _size, _purpose) allocateMemory(_env, _source, _size, false, "Could not allocate space for " _purpose)
60
61/**
62 * Allocate memory for a string and copy the given string into this allocated space.
63 *
64 * @param _env The JNI environment. Used in all functions involving JNI
65 * @param _string buffer to copy contents from
66 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
67 */
68#define ALLOCATE_STRING(_env, _string, _purpose) ALLOCATE_AND_COPY_MEMORY(_env, _string, strlen(_string)+1, _purpose)
69
70/**
71 * Allocate space for a fixed length string and copy data from the given string into
72 * the newly allocated space. You need to specify size as one bigger than the
73 * fixed length string so that it can be null terminated
74 *
75 * @param _env The JNI environment. Used in all functions involving JNI
76 * @param _string buffer to copy contents from
77 * @param _size size of memory to allocate
78 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
79 */
80#define ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose) allocateMemory(_env, _string, _size, true, "Could not allocate space for " _purpose)
81
82/**
83 * Allocate memory and on error return the given value.
84 *
85 * @param _env The JNI environment. Used in all functions involving JNI
86 * @param _var the specified variable is set to point to the allocated memory
87 * @param _size size of memory to allocate
88 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
89 * @param _r the specified return value
90 * @return This MACRO will return the specified return value from your function if it fails
91 */
92#define ALLOCATE_MEMORY_AND_ON_ERROR_RETURN_(_env, _var, _size, _purpose, _r) { \
93 void * _aptr = allocateMemory(_env, NULL, _size, false, "Could not allocate space for " _purpose); \
94 if (!_aptr) \
95 return _r; \
96 (_var) = _aptr; \
97}
98
99/**
100 * Allocate memory for a string and copy the given string into this allocated space
101 *
102 * @param _env The JNI environment. Used in all functions involving JNI
103 * @param _var the specified variable is set to point to the allocated memory
104 * @param _string buffer to copy contents from
105 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
106 * @param _r the specified return value
107 * @return This MACRO will return the specified return value from your function if it fails
108 */
109#define ALLOCATE_STRING_AND_ON_ERROR_RETURN_(_env, _var, _string, _purpose, _r) \
110if (!( (_var) = ALLOCATE_STRING(_env, _string, _purpose))) { \
111 return _r; \
112}
113
114/**
115 * Allocate memory for a string and copy the given string into this allocated space
116 * The specified variable is set to point to the allocated memory
117 * The given purpose is a string that will be contained in the error message if the allocation fails
118 *
119 * @param _env The JNI environment. Used in all functions involving JNI
120 * @param _var the specified variable is set to point to the allocated memory
121 * @param _string buffer to copy contents from
122 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
123 * @return This MACRO will return from your function if it fails
124 */
125#define ALLOCATE_STRING_AND_ON_ERROR_RETURN_VOID(_env, _var, _string, _purpose) \
126if (!( (_var) = ALLOCATE_STRING(_env, _string, _purpose))) { \
127 return; \
128}
129
130/**
131 * Allocate space for a fixed length string and copy date from the given string into
132 * the newly allocated space. You need to specify size as one bigger than the
133 * fixed length string so that it can be null terminated
134 *
135 * @param _env The JNI environment. Used in all functions involving JNI
136 * @param _var the specified variable is set to point to the allocated memory
137 * @param _string buffer to copy contents from
138 * @param _size size of memory to allocate
139 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
140 * @return This MACRO will return from your function if it fails
141 */
142#define ALLOCATE_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_VOID(_env, _var, _string, _size, _purpose) \
143if (!( (_var) = ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose))) { \
144 return; \
145}
146
147/**
148 * Allocate space for a fixed length string and copy date from the given string into
149 * the newly allocated space. You need to specify size as one bigger than the
150 * fixed length string so that it can be null terminated
151 *
152 * @param _env The JNI environment. Used in all functions involving JNI
153 * @param _var the specified variable is set to point to the allocated memory
154 * @param _string buffer to copy contents from
155 * @param _size size of memory to allocate
156 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
157 * @param _r the specified return value
158 * @return This MACRO will return the specified return value from your function if it fails
159 */
160#define ALLOCATE_AND_TRACK_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_(_env, _var, _string, _size, _purpose, _r) \
161{ \
162 void *_aptr = ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose); \
163 if ( !_aptr ) { \
164 FREE_MEMORY \
165 return _r; \
166 } \
167 TRACK_MEMORY(_aptr) \
168 (_var) = _aptr; \
169}
170
171/**
172 * Allocate memory and add it to the tracked memory list so that it can be freed automatically later
173 *
174 * @param _env The JNI environment. Used in all functions involving JNI
175 * @param _var the specified variable is set to point to the allocated memory
176 * @param _size size of memory to allocate
177 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
178 * @param _r the specified return value
179 * @return This MACRO will return the specified return value from your function if it fails
180 */
181#define ALLOCATE_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_(_env, _var, _size, _purpose, _r) \
182{ \
183 void *_aptr = ALLOCATE_MEMORY(_env, _size, _purpose); \
184 if ( !_aptr ) { \
185 FREE_MEMORY \
186 return _r; \
187 } \
188 TRACK_MEMORY(_aptr) \
189 (_var) = _aptr; \
190}
191
192/**
193 * Allocate memory and set its contents to the given buffer of given size
194 *
195 * @param _env The JNI environment. Used in all functions involving JNI
196 * @param _var the specified variable is set to point to the allocated memory
197 * @param _source buffer to copy contents from
198 * @param _size size of memory to allocate
199 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
200 * @param _r the specified return value
201 * @return This MACRO will return the specified return value from your function if it fails
202 */
203#define ALLOCATE_COPY_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_(_env, _var, _source, _size, _purpose, _r) \
204{ \
205 void *_aptr = ALLOCATE_AND_COPY_MEMORY(_env, _source, _size, _purpose); \
206 if ( !_aptr ) { \
207 FREE_MEMORY \
208 return _r; \
209 } \
210 TRACK_MEMORY(_aptr) \
211 (_var) = _aptr; \
212}
213
214/**
215 * Allocate and track a string
216 *
217 * @param _env The JNI environment. Used in all functions involving JNI
218 * @param _var the specified variable is set to point to the allocated memory
219 * @param _string buffer to copy contents from
220 * @param _purpose the given purpose is a string that will be contained in the error message if the allocation fails
221 * @param _r the specified return value
222 * @return This MACRO will return the specified return value from your function if it fails
223 */
224#define ALLOCATE_COPY_AND_TRACK_STRING_AND_ON_ERROR_RETURN_(_env, _var, _string, _purpose, _r) \
225{ \
226 void *_aptr = ALLOCATE_STRING(_env, _string, _purpose); \
227 if ( !_aptr ) { \
228 FREE_MEMORY \
229 return _r; \
230 } \
231 TRACK_MEMORY(_aptr) \
232 (_var) = _aptr; \
233}
234
235/**
236 * Free any allocated json memory
237 */
238#define FREE_JSON \
239{ \
240 while ( _n_jsonValuesToFree-- > 0) { \
241 if ( _jsonValuesToFree[_n_jsonValuesToFree] ) \
242 json_value_free(_jsonValuesToFree[_n_jsonValuesToFree]); \
243 } \
244}
245
246/**
247 * Free any allocated memory
248 */
249#define FREE_MEMORY \
250{ \
251 while ( _nAllocationsToFree-- > 0) { \
252 if ( _memoryAllocationsToFree[_nAllocationsToFree]) \
253 free (_memoryAllocationsToFree[_nAllocationsToFree]); \
254 } \
255 FREE_JSON \
256}
257
258/**
259 * Free a single tracked memory allocation and remove from list
260 *
261 * @param _ptr name of a pointer that points to the memory to free
262 */
263#define FREE_TRACKED_MEMORY(_ptr) \
264{ \
265 if ( _ptr) { \
266 \
267 bool found = false; \
268 for ( int i = 0 ; i < _nAllocationsToFree; i++ ) { \
269 if ( (_ptr) == _memoryAllocationsToFree[i] ) { \
270 free (_ptr); \
271 found = true; \
272 } \
273 if ( found && i != (_nAllocationsToFree-1) ) \
274 _memoryAllocationsToFree[i] = _memoryAllocationsToFree[i+1]; \
275 } \
276 if ( found ) \
277 _nAllocationsToFree--; \
278 }\
279}
280
281#ifdef __cplusplus
282}
283#endif
284#endif
285
The Header File for the AIDA-PVA Module functions.