Objective Systems, Inc.  
Home
About ASN.1
Products
Free Software
Open Source
Documents
Services
Resources
Resellers
Customers
Careers
About Us
Contact Us
 

Google


Objective Systems, Inc.

Memory Allocation Macros and Functions
[C Runtime Common Functions]


Detailed Description

Memory allocation functions and macros handle memory management for the ASN1C run-time. Special algorithms are used for allocation and deallocation of memory to improve the run-time performance.


Defines

#define ALLOC_ASN1ARRAY(pctxt, pseqof, type)
#define ALLOC_ASN1ARRAY1(pctxt, pseqof, type)
#define ALLOC_ASN1ARRAY2(pctxt, n, type)
#define ALLOC_ASN1ELEM(pctxt, type)   (type*) rtMemHeapAllocZ (&(pctxt)->pTypeMemHeap, sizeof(type))
#define ASN1ARRAYSIZE(x)   (sizeof(x)/sizeof(x[0]))
#define ASN1MALLOC(pctxt, nbytes)   rtMemHeapAlloc(&(pctxt)->pTypeMemHeap, nbytes)
#define ASN1REALLOC(pctxt, pmem, nbytes)   rtMemHeapRealloc(&(pctxt)->pTypeMemHeap, pmem, nbytes)
#define REALLOC_ASN1ARRAY(pctxt, pseqof, type)
#define ASN1MEMFREE(pctxt)   rtMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
#define ASN1MEMFREEPTR(pctxt, pmem)   rtMemHeapFreePtr(&(pctxt)->pTypeMemHeap, (void*)pmem)
#define ASN1MEMRESET(pctxt)   rtMemHeapReset(&(pctxt)->pTypeMemHeap)
#define rtMemAlloc(pctxt, nbytes)   rtMemHeapAlloc(&(pctxt)->pTypeMemHeap,nbytes)
#define rtMemAllocType(pctxt, ctype)   (ctype*)rtMemHeapAlloc(&(pctxt)->pTypeMemHeap,sizeof(ctype))
#define rtMemAllocZ(pctxt, nbytes)   rtMemHeapAllocZ(&(pctxt)->pTypeMemHeap,nbytes)
#define rtMemAllocTypeZ(pctxt, ctype)   (ctype*)rtMemHeapAllocZ(&(pctxt)->pTypeMemHeap,sizeof(ctype))
#define rtMemRealloc(pctxt, mem_p, nbytes)   rtMemHeapRealloc(&(pctxt)->pTypeMemHeap, (void*)mem_p, nbytes)
#define rtMemFreePtr(pctxt, mem_p)
#define rtMemFree(pctxt)   rtMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
#define rtMemReset(pctxt)   rtMemHeapReset(&(pctxt)->pTypeMemHeap)
#define OSCDECL

Typedefs

typedef void *OSCDECL * OSMallocFunc (size_t size)
typedef void *OSCDECL * OSReallocFunc (void *ptr, size_t size)

Functions

typedef void (OSCDECL *OSFreeFunc)(void *ptr)
void rtMemHeapAddRef (void **ppvMemHeap)
void * rtMemHeapAlloc (void **ppvMemHeap, size_t nbytes)
void * rtMemHeapAllocZ (void **ppvMemHeap, size_t nbytes)
int rtMemHeapCheckPtr (void **ppvMemHeap, void *mem_p)
int rtMemHeapCreate (void **ppvMemHeap)
void rtMemHeapFreeAll (void **ppvMemHeap)
void rtMemHeapFreePtr (void **ppvMemHeap, void *mem_p)
void * rtMemHeapMarkSaved (void **ppvMemHeap, ASN1ConstVoidPtr mem_p, ASN1BOOL saved)
void * rtMemHeapRealloc (void **ppvMemHeap, void *mem_p, size_t nbytes_)
void rtMemHeapRelease (void **ppvMemHeap)
void rtMemHeapReset (void **ppvMemHeap)
void rtMemHeapSetProperty (void **ppvMemHeap, ASN1UINT propId, void *pProp)
void rtMemSetAllocFuncs (OSMallocFunc malloc_func, OSReallocFunc realloc_func, OSFreeFunc free_func)
void rtMemFreeOpenSeqExt (ASN1CTXT *pctxt, Asn1RTDList *pElemList)
void rtMemHeapSetFlags (ASN1CTXT *pctxt, ASN1UINT flags)
void rtMemHeapClearFlags (ASN1CTXT *pctxt, ASN1UINT flags)
void rtMemHeapSetDefBlkSize (ASN1CTXT *pctxt, ASN1UINT blkSize)
ASN1UINT rtMemHeapGetDefBlkSize (ASN1CTXT *pctxt)


Define Documentation

#define ALLOC_ASN1ARRAY pctxt,
pseqof,
type   ) 
 

Value:

do {\
if (sizeof(type)*(pseqof)->n < (pseqof)->n) return ASN_E_NOMEM; \
if (((pseqof)->elem = (type*) rtMemHeapAlloc \
(&(pctxt)->pTypeMemHeap, sizeof(type)*(pseqof)->n)) == 0) return ASN_E_NOMEM; \
} while (0)
Allocate a dynamic array. This macro allocates a dynamic array of records of the given type. This version of the macro will return the ASN_E_NOMEM error status if the memory request cannot be fulfilled.

Parameters:
pctxt - Pointer to a context block
pseqof - Pointer to a generated SEQUENCE OF array structure. The n member variable must be set to the number of records to allocate.
type - Data type of an array record

#define ALLOC_ASN1ARRAY1 pctxt,
pseqof,
type   ) 
 

Value:

do {\
if (sizeof(type)*(pseqof)->n < (pseqof)->n) (pseqof)->elem = 0; \
else (pseqof)->elem = (type*) rtMemHeapAlloc \
(&(pctxt)->pTypeMemHeap, sizeof(type)*(pseqof)->n); \
} while (0)
Allocate a dynamic array. This macro allocates a dynamic array of records of the given type. This version of the macro will set the internal parameters of the SEQUENCE OF structure to NULL if the memory request cannot be fulfilled.

Parameters:
pctxt - Pointer to a context block
pseqof - Pointer to a generated SEQUENCE OF array structure. The n member variable must be set to the number of records to allocate.
type - Data type of an array record

#define ALLOC_ASN1ARRAY2 pctxt,
n,
type   ) 
 

Value:

((type*) ((sizeof(type)*n < n) ? 0 : \
rtMemHeapAlloc (&(pctxt)->pTypeMemHeap, sizeof(type)*n)))
Allocate a dynamic array. This macro allocates a dynamic array of records of the given type. This version returns the pointer to the allocated array directly to the caller. It does not use a generated SEQUENCE OF structure.

Parameters:
pctxt - Pointer to a context block
n - Number of records to allocate
type - Data type of an array record

#define ALLOC_ASN1ELEM pctxt,
type   )     (type*) rtMemHeapAllocZ (&(pctxt)->pTypeMemHeap, sizeof(type))
 

Allocate and zero an ASN.1 element. This macro allocates and zeros a single element of the given type. (Note: this macro is deprecated. Users should now use rtMemAllocTypeZ instead).

Parameters:
pctxt - Pointer to a context block
type - Data type of record to allocate

#define ASN1ARRAYSIZE  )     (sizeof(x)/sizeof(x[0]))
 

Get array size. This macro returns the number of elements in an array.

Parameters:
x - Array varaible

#define ASN1MALLOC pctxt,
nbytes   )     rtMemHeapAlloc(&(pctxt)->pTypeMemHeap, nbytes)
 

Allocate memory. This macro allocates the given number of bytes. It is similar to the C malloc run-time function. (Note: this macro is deprecated. Users should now use rtMemAlloc instead).

Parameters:
pctxt - Pointer to a context block
nbytes - Number of bytes of memory to allocate
Returns:
- Void pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define ASN1MEMFREE pctxt   )     rtMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
 

Free memory associated with a context. This macro frees all memory held within a context. This is all memory allocated using the ASN1MALLOC (and similar macros) and the rtMem memory allocation functions using the given context variable.

Parameters:
pctxt - Pointer to a context block

#define ASN1MEMFREEPTR pctxt,
pmem   )     rtMemHeapFreePtr(&(pctxt)->pTypeMemHeap, (void*)pmem)
 

Free memory pointer. This macro frees memory at the given pointer. The memory must have been allocated using the ASN1MALLOC (or similar) macros or the rtMem memory allocation functions. This macro is similar to the C free function.

Parameters:
pctxt - Pointer to a context block
pmem - Pointer to memory block to free. This must have been allocated using the ASN1MALLOC macro or the rtMemHeapAlloc function.

#define ASN1MEMRESET pctxt   )     rtMemHeapReset(&(pctxt)->pTypeMemHeap)
 

Reset memory associated with a context. This macro resets all memory held within a context. This is all memory allocated using the ASN1MALLOC (and similar macros) and the rtMem memory allocation functions using the given context variable.

The difference between this and the ASN1MEMFREE macro is that the memory blocks held within the context are not actually freed. Internal pointers are reset so the existing blocks can be reused. This can provide a performace improvement for repetitive tasks such as decoding messages in a loop.

Parameters:
pctxt - Pointer to a context block

#define ASN1REALLOC pctxt,
pmem,
nbytes   )     rtMemHeapRealloc(&(pctxt)->pTypeMemHeap, pmem, nbytes)
 

Reallocate memory. This macro reallocates a memory block (either expands or contracts) to the given number of bytes. It is similar to the C realloc run-time function. (Note: this macro is deprecated. Users should now use rtMemRealloc instead).

Parameters:
pctxt - Pointer to a context block
pmem - Pointer to memory block to reallocate. This must have been allocated using the ASN1MALLOC macro or the rtMemHeapAlloc function.
nbytes - Number of bytes of memory to which the block is to be resized.
Returns:
- Void pointer to allocated memory or NULL if insufficient memory was available to fulfill the request. This may be the same as the pmem pointer that was passed in if the block did not need to be relocated.

#define REALLOC_ASN1ARRAY pctxt,
pseqof,
type   ) 
 

Value:

do {\
if (sizeof(type)*(pseqof)->n < (pseqof)->n) return ASN_E_NOMEM; \
if (((pseqof)->elem = (type*) rtMemHeapRealloc \
(&(pctxt)->pTypeMemHeap, (pseqof)->elem, sizeof(type)*(pseqof)->n)) == 0) \
return ASN_E_NOMEM; \
} while (0)
Reallocate an array. This macro reallocates an array (either expands or contracts) to hold the given number of elements. The number of elements is specified in the n member variable of the pseqof argument.

Parameters:
pctxt - Pointer to a context block
pseqof - Pointer to a generated SEQUENCE OF array structure. The n member variable must be set to the number of records to allocate.
type - Data type of an array record

#define rtMemAlloc pctxt,
nbytes   )     rtMemHeapAlloc(&(pctxt)->pTypeMemHeap,nbytes)
 

Allocate memory. This macro allocates the given number of bytes. It is similar to the C malloc run-time function.

Parameters:
pctxt - Pointer to a context block
nbytes - Number of bytes of memory to allocate
Returns:
- Void pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define rtMemAllocType pctxt,
ctype   )     (ctype*)rtMemHeapAlloc(&(pctxt)->pTypeMemHeap,sizeof(ctype))
 

Allocate type. This macro allocates memory to hold a variable of the given type.

Parameters:
pctxt - Pointer to a context block
ctype - Name of C typedef
Returns:
- Pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define rtMemAllocTypeZ pctxt,
ctype   )     (ctype*)rtMemHeapAllocZ(&(pctxt)->pTypeMemHeap,sizeof(ctype))
 

Allocate type and zero memory. This macro allocates memory to hold a variable of the given type and initializes the allocated memory to zero.

Parameters:
pctxt - Pointer to a context block
ctype - Name of C typedef
Returns:
- Pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define rtMemAllocZ pctxt,
nbytes   )     rtMemHeapAllocZ(&(pctxt)->pTypeMemHeap,nbytes)
 

Allocate and zero memory. This macro allocates the given number of bytes and then initializes the memory block to zero.

Parameters:
pctxt - Pointer to a context block
nbytes - Number of bytes of memory to allocate
Returns:
- Void pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define rtMemFree pctxt   )     rtMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
 

Free memory associated with a context. This macro frees all memory held within a context. This is all memory allocated using the rtMem memory allocation functions and macros using the given context variable.

Parameters:
pctxt - Pointer to a context block

#define rtMemFreePtr pctxt,
mem_p   ) 
 

Value:

if (rtMemHeapCheckPtr (&(pctxt)->pTypeMemHeap, (void*)mem_p)) \
rtMemHeapFreePtr(&(pctxt)->pTypeMemHeap, (void*)mem_p)
Free memory pointer. This macro frees memory at the given pointer. The memory must have been allocated using the ASN1MALLOC (or similar) macros or the rtMem memory allocation macros. This macro is similar to the C free function.

Parameters:
pctxt - Pointer to a context block
mem_p - Pointer to memory block to free. This must have been allocated using the ASN1MALLOC or rtMemAlloc macro or the rtMemHeapAlloc function.

#define rtMemRealloc pctxt,
mem_p,
nbytes   )     rtMemHeapRealloc(&(pctxt)->pTypeMemHeap, (void*)mem_p, nbytes)
 

Reallocate memory. This macro reallocates a memory block (either expands or contracts) to the given number of bytes. It is similar to the C realloc run-time function.

Parameters:
pctxt - Pointer to a context block
mem_p - Pointer to memory block to reallocate. This must have been allocated using the ASN1MALLOC macro or the rtMemHeapAlloc function.
nbytes - Number of bytes of memory to which the block is to be resized.
Returns:
- Void pointer to allocated memory or NULL if insufficient memory was available to fulfill the request. This may be the same as the pmem pointer that was passed in if the block did not need to be relocated.

#define rtMemReset pctxt   )     rtMemHeapReset(&(pctxt)->pTypeMemHeap)
 

Reset memory associated with a context. This macro resets all memory held within a context. This is all memory allocated using the ASN1MALLOC (and similar macros) and the rtMem memory allocation functions using the given context variable.

The difference between this and the rtMemFree macro is that the memory blocks held within the context are not actually freed. Internal pointers are reset so the existing blocks can be reused. This can provide a performace improvement for repetitive tasks such as decoding messages in a loop.

Parameters:
pctxt - Pointer to a context block


Function Documentation

ASN1UINT rtMemHeapGetDefBlkSize ASN1CTXT *  pctxt  ) 
 

This function returns the actual granularity of memory blocks.

Parameters:
pctxt Pointer to a context block.

void rtMemHeapSetDefBlkSize ASN1CTXT *  pctxt,
ASN1UINT  blkSize
 

This function sets the pointer to standard allocation functions. These functions are used to allocate/reallocate/free the memory blocks. By default, standard C functions - malloc, realloc, and free - are used. But if some platforms do not support these functions or some other reasons exist) they can be overloaded. The functions being overloaded should have the same prototypes as standard ones.

Parameters:
pctxt Pointer to a context block.
blkSize The currently used minimum size and the granularity of memory blocks.

void rtMemSetAllocFuncs OSMallocFunc  malloc_func,
OSReallocFunc  realloc_func,
OSFreeFunc  free_func
 

This function sets the pointers to standard allocation functions. These functions are used to allocate/reallocate/free the memory blocks. By default, standard C functions - 'malloc', 'realloc' and 'free' - are used. But if some platforms do not support these functions (or some other reasons exist) they can be overloaded. The functions being overloaded should have the same prototypes as standard ones.

Parameters:
malloc_func Pointer to the memory allocation function ('malloc' by default).
realloc_func Pointer to the memory reallocation function ('realloc' by default).
free_func Pointer to the memory deallocation function ('free' by default).


Copyright © 1997-2005 Objective Systems,Inc.
All Rights Reserved.
This document may be distributed in any form, electronic
or otherwise, provided that it is distributed in its entirety
and that the copyright and this notice are included.

This file was last modified on 8 Sep 2005.
ASN1C C/C++ Common Runtime, ASN1C v5.8x