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 File Reference

The Header File for the memory management functions and macros. More...

#include "aida_pva.h"

Go to the source code of this file.

Macros

#define ALLOCATE_AND_COPY_MEMORY(_env, _source, _size, _purpose)   allocateMemory(_env, _source, _size, false, "Could not allocate space for " _purpose)
 Allocate memory and set its contents to the given buffer of given size. More...
 
#define ALLOCATE_AND_TRACK_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_(_env, _var, _string, _size, _purpose, _r)
 Allocate space for a fixed length string and copy date from the given string into the newly allocated space. More...
 
#define ALLOCATE_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_(_env, _var, _size, _purpose, _r)
 Allocate memory and add it to the tracked memory list so that it can be freed automatically later. More...
 
#define ALLOCATE_COPY_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_(_env, _var, _source, _size, _purpose, _r)
 Allocate memory and set its contents to the given buffer of given size. More...
 
#define ALLOCATE_COPY_AND_TRACK_STRING_AND_ON_ERROR_RETURN_(_env, _var, _string, _purpose, _r)
 Allocate and track a string. More...
 
#define ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose)   allocateMemory(_env, _string, _size, true, "Could not allocate space for " _purpose)
 Allocate space for a fixed length string and copy data from the given string into the newly allocated space. More...
 
#define ALLOCATE_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_VOID(_env, _var, _string, _size, _purpose)
 Allocate space for a fixed length string and copy date from the given string into the newly allocated space. More...
 
#define ALLOCATE_MEMORY(_env, _size, _purpose)   allocateMemory(_env, NULL, _size, false, "Could not allocate space for " _purpose)
 Allocate memory. More...
 
#define ALLOCATE_MEMORY_AND_ON_ERROR_RETURN_(_env, _var, _size, _purpose, _r)
 Allocate memory and on error return the given value. More...
 
#define ALLOCATE_STRING(_env, _string, _purpose)   ALLOCATE_AND_COPY_MEMORY(_env, _string, strlen(_string)+1, _purpose)
 Allocate memory for a string and copy the given string into this allocated space. More...
 
#define ALLOCATE_STRING_AND_ON_ERROR_RETURN_(_env, _var, _string, _purpose, _r)
 Allocate memory for a string and copy the given string into this allocated space. More...
 
#define ALLOCATE_STRING_AND_ON_ERROR_RETURN_VOID(_env, _var, _string, _purpose)
 Allocate memory for a string and copy the given string into this allocated space The specified variable is set to point to the allocated memory The given purpose is a string that will be contained in the error message if the allocation fails. More...
 
#define FREE_JSON
 Free any allocated json memory. More...
 
#define FREE_MEMORY
 Free any allocated memory. More...
 
#define FREE_TRACKED_MEMORY(_ptr)
 Free a single tracked memory allocation and remove from list. More...
 
#define MAX_POINTERS   100
 The maximum number of pointers that the Memory tracking and management functions can handle. More...
 
#define TRACK_ALLOCATED_MEMORY
 Create tracking variables so that memory can be freed with FREE_MEMORY macro. More...
 
#define TRACK_JSON(_ptr)    if (_ptr) _jsonValuesToFree[_n_jsonValuesToFree++] = (_ptr);
 Register this newly allocated json value so that it will be freed by FREE_JSON_MEMORY. More...
 
#define TRACK_MEMORY(_ptr)    if (_ptr) _memoryAllocationsToFree[_nAllocationsToFree++] = (_ptr);
 Register this newly allocated memory so that it will be freed by FREE_MEMORY. More...
 

Detailed Description

The Header File for the memory management functions and macros.

CMS=C_INC

Definition in file aida_pva_memory.h.

Macro Definition Documentation

◆ ALLOCATE_AND_COPY_MEMORY

#define ALLOCATE_AND_COPY_MEMORY (   _env,
  _source,
  _size,
  _purpose 
)    allocateMemory(_env, _source, _size, false, "Could not allocate space for " _purpose)

Allocate memory and set its contents to the given buffer of given size.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_sourcebuffer to copy contents from
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails

Definition at line 59 of file aida_pva_memory.h.

◆ ALLOCATE_AND_TRACK_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_

#define ALLOCATE_AND_TRACK_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_ (   _env,
  _var,
  _string,
  _size,
  _purpose,
  _r 
)
Value:
{ \
void *_aptr = ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose); \
if ( !_aptr ) { \
FREE_MEMORY \
return _r; \
} \
TRACK_MEMORY(_aptr) \
(_var) = _aptr; \
}
#define ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose)
Allocate space for a fixed length string and copy data from the given string into the newly allocated...

Allocate space for a fixed length string and copy date from the given string into the newly allocated space.

You need to specify size as one bigger than the fixed length string so that it can be null terminated

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_stringbuffer to copy contents from
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
_rthe specified return value
Returns
This MACRO will return the specified return value from your function if it fails

Definition at line 160 of file aida_pva_memory.h.

◆ ALLOCATE_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_

#define ALLOCATE_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_ (   _env,
  _var,
  _size,
  _purpose,
  _r 
)
Value:
{ \
void *_aptr = ALLOCATE_MEMORY(_env, _size, _purpose); \
if ( !_aptr ) { \
FREE_MEMORY \
return _r; \
} \
TRACK_MEMORY(_aptr) \
(_var) = _aptr; \
}
#define ALLOCATE_MEMORY(_env, _size, _purpose)
Allocate memory.

Allocate memory and add it to the tracked memory list so that it can be freed automatically later.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
_rthe specified return value
Returns
This MACRO will return the specified return value from your function if it fails

Definition at line 181 of file aida_pva_memory.h.

◆ ALLOCATE_COPY_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_

#define ALLOCATE_COPY_AND_TRACK_MEMORY_AND_ON_ERROR_RETURN_ (   _env,
  _var,
  _source,
  _size,
  _purpose,
  _r 
)
Value:
{ \
void *_aptr = ALLOCATE_AND_COPY_MEMORY(_env, _source, _size, _purpose); \
if ( !_aptr ) { \
FREE_MEMORY \
return _r; \
} \
TRACK_MEMORY(_aptr) \
(_var) = _aptr; \
}
#define ALLOCATE_AND_COPY_MEMORY(_env, _source, _size, _purpose)
Allocate memory and set its contents to the given buffer of given size.

Allocate memory and set its contents to the given buffer of given size.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_sourcebuffer to copy contents from
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
_rthe specified return value
Returns
This MACRO will return the specified return value from your function if it fails

Definition at line 203 of file aida_pva_memory.h.

◆ ALLOCATE_COPY_AND_TRACK_STRING_AND_ON_ERROR_RETURN_

#define ALLOCATE_COPY_AND_TRACK_STRING_AND_ON_ERROR_RETURN_ (   _env,
  _var,
  _string,
  _purpose,
  _r 
)
Value:
{ \
void *_aptr = ALLOCATE_STRING(_env, _string, _purpose); \
if ( !_aptr ) { \
FREE_MEMORY \
return _r; \
} \
TRACK_MEMORY(_aptr) \
(_var) = _aptr; \
}
#define ALLOCATE_STRING(_env, _string, _purpose)
Allocate memory for a string and copy the given string into this allocated space.

Allocate and track a string.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_stringbuffer to copy contents from
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
_rthe specified return value
Returns
This MACRO will return the specified return value from your function if it fails

Definition at line 224 of file aida_pva_memory.h.

◆ ALLOCATE_FIXED_LENGTH_STRING

#define ALLOCATE_FIXED_LENGTH_STRING (   _env,
  _string,
  _size,
  _purpose 
)    allocateMemory(_env, _string, _size, true, "Could not allocate space for " _purpose)

Allocate space for a fixed length string and copy data from the given string into the newly allocated space.

You need to specify size as one bigger than the fixed length string so that it can be null terminated

Parameters
_envThe JNI environment. Used in all functions involving JNI
_stringbuffer to copy contents from
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails

Definition at line 80 of file aida_pva_memory.h.

◆ ALLOCATE_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_VOID

#define ALLOCATE_FIXED_LENGTH_STRING_AND_ON_ERROR_RETURN_VOID (   _env,
  _var,
  _string,
  _size,
  _purpose 
)
Value:
if (!( (_var) = ALLOCATE_FIXED_LENGTH_STRING(_env, _string, _size, _purpose))) { \
return; \
}

Allocate space for a fixed length string and copy date from the given string into the newly allocated space.

You need to specify size as one bigger than the fixed length string so that it can be null terminated

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_stringbuffer to copy contents from
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
Returns
This MACRO will return from your function if it fails

Definition at line 142 of file aida_pva_memory.h.

◆ ALLOCATE_MEMORY

#define ALLOCATE_MEMORY (   _env,
  _size,
  _purpose 
)    allocateMemory(_env, NULL, _size, false, "Could not allocate space for " _purpose)

Allocate memory.

Allocates memory of the given size

Parameters
_envThe JNI environment. Used in all functions involving JNI
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails

Definition at line 49 of file aida_pva_memory.h.

◆ ALLOCATE_MEMORY_AND_ON_ERROR_RETURN_

#define ALLOCATE_MEMORY_AND_ON_ERROR_RETURN_ (   _env,
  _var,
  _size,
  _purpose,
  _r 
)
Value:
{ \
void * _aptr = allocateMemory(_env, NULL, _size, false, "Could not allocate space for " _purpose); \
if (!_aptr) \
return _r; \
(_var) = _aptr; \
}
void * allocateMemory(JNIEnv *env, void *source, size_t size, bool nullTerminate, char *message)
Allocate memory and copy the source to it if specified.

Allocate memory and on error return the given value.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_sizesize of memory to allocate
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
_rthe specified return value
Returns
This MACRO will return the specified return value from your function if it fails

Definition at line 92 of file aida_pva_memory.h.

◆ ALLOCATE_STRING

#define ALLOCATE_STRING (   _env,
  _string,
  _purpose 
)    ALLOCATE_AND_COPY_MEMORY(_env, _string, strlen(_string)+1, _purpose)

Allocate memory for a string and copy the given string into this allocated space.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_stringbuffer to copy contents from
_purposethe given purpose is a string that will be contained in the error message if the allocation fails

Definition at line 68 of file aida_pva_memory.h.

◆ ALLOCATE_STRING_AND_ON_ERROR_RETURN_

#define ALLOCATE_STRING_AND_ON_ERROR_RETURN_ (   _env,
  _var,
  _string,
  _purpose,
  _r 
)
Value:
if (!( (_var) = ALLOCATE_STRING(_env, _string, _purpose))) { \
return _r; \
}

Allocate memory for a string and copy the given string into this allocated space.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_stringbuffer to copy contents from
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
_rthe specified return value
Returns
This MACRO will return the specified return value from your function if it fails

Definition at line 109 of file aida_pva_memory.h.

◆ ALLOCATE_STRING_AND_ON_ERROR_RETURN_VOID

#define ALLOCATE_STRING_AND_ON_ERROR_RETURN_VOID (   _env,
  _var,
  _string,
  _purpose 
)
Value:
if (!( (_var) = ALLOCATE_STRING(_env, _string, _purpose))) { \
return; \
}

Allocate memory for a string and copy the given string into this allocated space The specified variable is set to point to the allocated memory The given purpose is a string that will be contained in the error message if the allocation fails.

Parameters
_envThe JNI environment. Used in all functions involving JNI
_varthe specified variable is set to point to the allocated memory
_stringbuffer to copy contents from
_purposethe given purpose is a string that will be contained in the error message if the allocation fails
Returns
This MACRO will return from your function if it fails

Definition at line 125 of file aida_pva_memory.h.

◆ FREE_JSON

#define FREE_JSON
Value:
{ \
while ( _n_jsonValuesToFree-- > 0) { \
if ( _jsonValuesToFree[_n_jsonValuesToFree] ) \
json_value_free(_jsonValuesToFree[_n_jsonValuesToFree]); \
} \
}

Free any allocated json memory.

Definition at line 238 of file aida_pva_memory.h.

◆ FREE_MEMORY

#define FREE_MEMORY
Value:
{ \
while ( _nAllocationsToFree-- > 0) { \
if ( _memoryAllocationsToFree[_nAllocationsToFree]) \
free (_memoryAllocationsToFree[_nAllocationsToFree]); \
} \
}
#define FREE_JSON
Free any allocated json memory.

Free any allocated memory.

Definition at line 249 of file aida_pva_memory.h.

◆ FREE_TRACKED_MEMORY

#define FREE_TRACKED_MEMORY (   _ptr)
Value:
{ \
if ( _ptr) { \
\
bool found = false; \
for ( int i = 0 ; i < _nAllocationsToFree; i++ ) { \
if ( (_ptr) == _memoryAllocationsToFree[i] ) { \
free (_ptr); \
found = true; \
} \
if ( found && i != (_nAllocationsToFree-1) ) \
_memoryAllocationsToFree[i] = _memoryAllocationsToFree[i+1]; \
} \
if ( found ) \
_nAllocationsToFree--; \
}\
}

Free a single tracked memory allocation and remove from list.

Parameters
_ptrname of a pointer that points to the memory to free

Definition at line 263 of file aida_pva_memory.h.

◆ MAX_POINTERS

#define MAX_POINTERS   100

The maximum number of pointers that the Memory tracking and management functions can handle.

Definition at line 16 of file aida_pva_memory.h.

◆ TRACK_ALLOCATED_MEMORY

#define TRACK_ALLOCATED_MEMORY
Value:
int _nAllocationsToFree = 0, _n_jsonValuesToFree = 0; \
void *_memoryAllocationsToFree[MAX_POINTERS] ; \
json_value *_jsonValuesToFree[MAX_POINTERS] ;
#define MAX_POINTERS
The maximum number of pointers that the Memory tracking and management functions can handle.

Create tracking variables so that memory can be freed with FREE_MEMORY macro.

Creates up to MAX_POINTERS pointers to track all memory allocations so that they can be safely freed, when needed. Creates local variables to store the tracking information so these macros can only be used within a single block.

Definition at line 25 of file aida_pva_memory.h.

◆ TRACK_JSON

#define TRACK_JSON (   _ptr)     if (_ptr) _jsonValuesToFree[_n_jsonValuesToFree++] = (_ptr);

Register this newly allocated json value so that it will be freed by FREE_JSON_MEMORY.

Definition at line 39 of file aida_pva_memory.h.

◆ TRACK_MEMORY

#define TRACK_MEMORY (   _ptr)     if (_ptr) _memoryAllocationsToFree[_nAllocationsToFree++] = (_ptr);

Register this newly allocated memory so that it will be freed by FREE_MEMORY.

Definition at line 33 of file aida_pva_memory.h.