Home > Support > Documentation

rtxMemory.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2003-2007 Objective Systems, Inc.
00003  *
00004  * This software is furnished under a license and may be used and copied
00005  * only in accordance with the terms of such license and with the
00006  * inclusion of the above copyright notice. This software or any other
00007  * copies thereof may not be provided or otherwise made available to any
00008  * other person. No title to and ownership of the software is hereby
00009  * transferred.
00010  *
00011  * The information in this software is subject to change without notice
00012  * and should not be construed as a commitment by Objective Systems, Inc.
00013  *
00014  * PROPRIETARY NOTICE
00015  *
00016  * This software is an unpublished work subject to a confidentiality agreement
00017  * and is protected by copyright and trade secret law.  Unauthorized copying,
00018  * redistribution or other use of this work is prohibited.
00019  *
00020  * The above notice of copyright on this source code product does not indicate
00021  * any actual or intended publication of such source code.
00022  *
00023  *****************************************************************************/
00028 #ifndef __RTXMEMORY_H__
00029 #define __RTXMEMORY_H__
00030 
00031 #include "rtxsrc/rtxContext.h"
00032 
00033 #define RT_MH_DONTKEEPFREE 0x1
00034 
00035 #define OSRTMH_PROPID_DEFBLKSIZE   1
00036 #define OSRTMH_PROPID_SETFLAGS     2
00037 #define OSRTMH_PROPID_CLEARFLAGS   3
00038 
00039 #define OSRTMH_PROPID_USER         10
00040 
00041 #define OSRTXM_K_MEMBLKSIZ         (4*1024)
00042 
00056 #define OSRTALLOCTYPE(pctxt,type) \
00057 (type*) rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type))
00058 
00065 #define OSRTALLOCTYPEZ(pctxt,type) \
00066 (type*) rtxMemHeapAllocZ (&(pctxt)->pMemHeap, sizeof(type))
00067 
00068 /*
00069  * Reallocate an array. This macro reallocates an array (either expands or
00070  * contracts) to hold the given number of elements. The number of elements is
00071  * specified in the <i>n</i> member variable of the pseqof argument.
00072  *
00073  * @param pctxt        - Pointer to a context block
00074  * @param pseqof       - Pointer to a generated SEQUENCE OF array structure.
00075  *                       The <i>n</i> member variable must be set to the number
00076  *                       of records to allocate.
00077  * @param type         - Data type of an array record
00078  */
00079 #define OSRTREALLOCARRAY(pctxt,pseqof,type) do {\
00080 if (sizeof(type)*(pseqof)->n < (pseqof)->n) return RTERR_NOMEM; \
00081 if (((pseqof)->elem = (type*) rtxMemHeapRealloc \
00082 (&(pctxt)->pMemHeap, (pseqof)->elem, sizeof(type)*(pseqof)->n)) == 0) \
00083 return RTERR_NOMEM; \
00084 } while (0)
00085 
00086 #ifndef _NO_MALLOC
00087 #define OSCRTMALLOC0(nbytes)       malloc(nbytes)
00088 #define OSCRTFREE0(ptr)            free(ptr)
00089 #else
00090 
00091 #ifdef _NO_THREADS
00092 extern EXTERNRTX OSCTXT g_ctxt;
00093 
00094 #define OSCRTMALLOC0(nbytes)       rtxMemAlloc(&g_ctxt,(nbytes))
00095 #define OSCRTFREE0(ptr)            rtxMemFreePtr(&g_ctxt,(ptr))
00096 #else
00097 #define OSCRTMALLOC0(nbytes)       (void*)0
00098 #define OSCRTFREE0(ptr)            (void*)0
00099 
00100 #endif /* _NO_THREADS */
00101 #endif /* _NO_MALLOC */
00102 
00103 #define OSCRTMALLOC rtxMemAlloc
00104 #define OSCRTFREE   rtxMemFreePtr
00105 
00106 struct OSRTDList;
00107 
00108 #ifdef __cplusplus
00109 extern "C" {
00110 #endif
00111 
00112 /* Alias for __cdecl modifier; if __cdecl keyword is not supported, 
00113  * redefine it as empty macro. */
00114 
00115 #if !defined(OSCDECL)
00116 #if defined(_MSC_VER) || defined(__BORLANDC__)
00117 #define OSCDECL __cdecl
00118 #else
00119 #define OSCDECL
00120 #endif
00121 #endif /* OSCDECL */
00122 
00123 /* Pointers to C Run-Time memory allocation functions *
00124  * (See rtxMemSetAllocFuncs)                          */
00125 
00126 typedef void *(OSCDECL *OSMallocFunc ) (size_t size);
00127 typedef void *(OSCDECL *OSReallocFunc) (void *ptr, size_t size);
00128 typedef void  (OSCDECL *OSFreeFunc   ) (void *ptr);
00129 
00130 #ifdef _RT_ALLOC_FUNC_WITH_CONTEXT
00131 
00132 /* Variation of the memory allocation functions only available *
00133  * after recompilation of the runtime with the macro defined.  *
00134  * (See rtxMemSetAllocCtxtFuncs)                               */
00135 
00136 typedef void *(OSCDECL *OSMallocCtxtFunc ) (OSCTXT* pctxt, size_t size);
00137 typedef void *(OSCDECL *OSReallocCtxtFunc) (OSCTXT* pctxt, void *ptr, size_t size);
00138 typedef void  (OSCDECL *OSFreeCtxtFunc   ) (OSCTXT* pctxt, void *ptr);
00139 
00140 #endif
00141 
00142 EXTERNRTX void  rtxMemHeapAddRef (void** ppvMemHeap);
00143 EXTERNRTX void* rtxMemHeapAlloc (void** ppvMemHeap, size_t nbytes);
00144 EXTERNRTX void* rtxMemHeapAllocZ (void** ppvMemHeap, size_t nbytes);
00145 EXTERNRTX int   rtxMemHeapCheckPtr (void** ppvMemHeap, void* mem_p);
00146 EXTERNRTX int   rtxMemHeapCreate (void** ppvMemHeap);
00147 EXTERNRTX void  rtxMemHeapFreeAll (void** ppvMemHeap);
00148 EXTERNRTX void  rtxMemHeapFreePtr (void** ppvMemHeap, void* mem_p);
00149 EXTERNRTX void* rtxMemHeapMarkSaved (void** ppvMemHeap, 
00150                                      const void* mem_p, 
00151                                      OSBOOL saved);
00152 EXTERNRTX void* rtxMemHeapRealloc (void** ppvMemHeap, 
00153                                    void* mem_p, size_t nbytes_);
00154 EXTERNRTX void  rtxMemHeapRelease (void** ppvMemHeap);
00155 EXTERNRTX void  rtxMemHeapReset (void** ppvMemHeap);
00156 EXTERNRTX void  rtxMemHeapSetProperty (void** ppvMemHeap, 
00157                                        OSUINT32 propId, void* pProp);
00158 
00159 
00160 
00161 EXTERNRTX void* rtxMemNewArray (size_t nbytes);
00162 EXTERNRTX void* rtxMemNewArrayZ (size_t nbytes);
00163 EXTERNRTX void rtxMemDeleteArray (void* mem_p);
00164 
00165 #ifndef __SYMBIAN32__
00166 
00181 EXTERNRTX void rtxMemSetAllocFuncs (OSMallocFunc malloc_func,
00182                                     OSReallocFunc realloc_func,
00183                                     OSFreeFunc free_func);
00184 #endif
00185 
00186 #ifdef _RT_ALLOC_FUNC_WITH_CONTEXT
00187 
00188 EXTERNRTX void rtxMemSetAllocCtxtFuncs (OSMallocCtxtFunc malloc_func,
00189                                         OSReallocCtxtFunc realloc_func,
00190                                         OSFreeCtxtFunc free_func);
00191 
00192 #endif
00193 
00194 EXTERNRTX void rtxMemFreeOpenSeqExt 
00195 (OSCTXT* pctxt, struct OSRTDList *pElemList);
00196 
00197 /*
00198  * This function returns the actual granularity of memory blocks in the context.
00199  *
00200  * @param pctxt        Pointer to a context block.
00201  */
00202 EXTERNRTX OSUINT32 rtxMemHeapGetDefBlkSize (OSCTXT* pctxt);
00203 
00204 #ifndef __SYMBIAN32__
00205 
00212 EXTERNRTX void rtxMemSetDefBlkSize (OSUINT32 blkSize);
00213 #endif
00214 
00221 EXTERNRTX OSUINT32 rtxMemGetDefBlkSize ();
00222 
00231 EXTERNRTX OSBOOL rtxMemIsZero (const void* pmem, size_t memsiz);
00232 
00233 #ifdef _STATIC_HEAP
00234 EXTERNRTX void rtxMemSetStaticBuf (void* memHeapBuf, OSUINT32 blkSize);
00235 #endif
00236 
00237 /*
00238  * Allocate memory.  This macro allocates the given number of bytes.  It is 
00239  * similar to the C \c malloc run-time function.
00240  * 
00241  * @param pctxt - Pointer to a context block
00242  * @param nbytes - Number of bytes of memory to allocate
00243  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00244  *   was available to fulfill the request.
00245  */
00246 #define rtxMemAlloc(pctxt,nbytes) \
00247 rtxMemHeapAlloc(&(pctxt)->pMemHeap,nbytes)
00248 
00249 /*
00250  * Allocate and zero memory.  This macro allocates the given number of bytes
00251  * and then initializes the memory block to zero.
00252  * 
00253  * @param pctxt - Pointer to a context block
00254  * @param nbytes - Number of bytes of memory to allocate
00255  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00256  *   was available to fulfill the request.
00257  */
00258 #define rtxMemAllocZ(pctxt,nbytes) \
00259 rtxMemHeapAllocZ(&(pctxt)->pMemHeap,nbytes)
00260 
00261 /*
00262  * Reallocate memory.  This macro reallocates a memory block (either 
00263  * expands or contracts) to the given number of bytes.  It is 
00264  * similar to the C \c realloc run-time function.
00265  * 
00266  * @param pctxt - Pointer to a context block
00267  * @param mem_p - Pointer to memory block to reallocate.  This must have been 
00268  *   allocated using the rtxMemAlloc macro or the rtxMemHeapAlloc function.
00269  * @param nbytes - Number of bytes of memory to which the block is to be 
00270  *   resized.
00271  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00272  *   was available to fulfill the request.  This may be the same as the pmem 
00273  *   pointer that was passed in if the block did not need to be relocated.
00274  */
00275 #define rtxMemRealloc(pctxt,mem_p,nbytes) \
00276 rtxMemHeapRealloc(&(pctxt)->pMemHeap, (void*)mem_p, nbytes)
00277 
00278 /*
00279  * Free memory pointer.  This macro frees memory at the given pointer.  
00280  * The memory must have been allocated using the rtxMemAlloc (or similar) 
00281  * macros or the rtxMem memory allocation macros.  This macro is 
00282  * similar to the C \c free function.
00283  * 
00284  * @param pctxt - Pointer to a context block
00285  * @param mem_p - Pointer to memory block to free.  This must have 
00286  *   been allocated using the rtxMemAlloc or rtxMemAlloc macro or the 
00287  *   rtxMemHeapAlloc function.
00288  */
00289 #define rtxMemFreePtr(pctxt,mem_p) \
00290 if (rtxMemHeapCheckPtr (&(pctxt)->pMemHeap, (void*)mem_p)) \
00291 rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
00292 
00293 /*
00294  * Free memory associated with a context.  This macro frees all memory 
00295  * held within a context.  This is all memory allocated using the 
00296  * rtxMemAlloc (and similar macros) and the rtxMem memory allocation 
00297  * functions using the given context variable.
00298  * 
00299  * @param pctxt - Pointer to a context block
00300  */
00301 #define rtxMemFree(pctxt) \
00302 rtxMemHeapFreeAll(&(pctxt)->pMemHeap)
00303 
00304 /*
00305  * Reset memory associated with a context.  This macro resets all memory 
00306  * held within a context.  This is all memory allocated using the rtxMemAlloc 
00307  * (and similar macros) and the rtxMem memory allocation functions using the 
00308  * given context variable.
00309  *
00310  * <p>The difference between this and the OSMEMFREE macro is that the 
00311  * memory blocks held within the context are not actually freed.  Internal 
00312  * pointers are reset so the existing blocks can be reused.  This can 
00313  * provide a performace improvement for repetitive tasks such as decoding 
00314  * messages in a loop.
00315  * 
00316  * @param pctxt - Pointer to a context block
00317  */
00318 #define rtxMemReset(pctxt) \
00319 rtxMemHeapReset(&(pctxt)->pMemHeap)
00320 
00330 #define rtxMemAllocType(pctxt,ctype) \
00331 (ctype*)rtxMemHeapAlloc(&(pctxt)->pMemHeap,sizeof(ctype))
00332 
00342 #define rtxMemAllocTypeZ(pctxt,ctype) \
00343 (ctype*)rtxMemHeapAllocZ(&(pctxt)->pMemHeap,sizeof(ctype))
00344 
00345 /*
00346  * Free memory pointer.  This macro frees memory at the given pointer.  
00347  * The memory must have been allocated using the rtxMemAlloc (or similar) 
00348  * macros or the rtxMem memory allocation macros.  This macro is 
00349  * similar to the C \c free function.
00350  * 
00351  * @param pctxt - Pointer to a context block
00352  * @param mem_p - Pointer to memory block to free.  This must have 
00353  *   been allocated using the rtxMemAlloc or rtxMemAlloc macro or the 
00354  *   rtxMemHeapAlloc function.
00355  */
00356 #define rtxMemFreeType(pctxt,mem_p) \
00357 rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
00358 
00368 #define rtxMemAllocArray(pctxt,n,type) \
00369 (type*)rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type)*n)
00370 
00371 #define rtxMemAllocArrayZ(pctxt,n,type) \
00372 (type*)rtxMemHeapAllocZ (&(pctxt)->pMemHeap, sizeof(type)*n)
00373 
00374 /*
00375  * Free memory pointer.  This macro frees memory at the given pointer.  
00376  * The memory must have been allocated using the rtxMemAlloc (or similar) 
00377  * macros or the rtxMem memory allocation macros.  This macro is 
00378  * similar to the C \c free function.
00379  * 
00380  * @param pctxt - Pointer to a context block
00381  * @param mem_p - Pointer to memory block to free.  This must have 
00382  *   been allocated using the rtxMemAlloc or rtxMemAlloc macro or the 
00383  *   rtxMemHeapAlloc function.
00384  */
00385 #define rtxMemFreeArray(pctxt,mem_p) \
00386 rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
00387 
00388 /*
00389  * Reallocate memory.  This macro reallocates a memory block (either 
00390  * expands or contracts) to the given number of bytes.  It is 
00391  * similar to the C \c realloc run-time function.
00392  * 
00393  * @param pctxt - Pointer to a context block
00394  * @param mem_p - Pointer to memory block to reallocate.  This must have been 
00395  *   allocated using the rtxMemAlloc macro or the rtxMemHeapAlloc function.
00396  * @param nbytes - Number of bytes of memory to which the block is to be 
00397  *   resized.
00398  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00399  *   was available to fulfill the request.  This may be the same as the pmem 
00400  *   pointer that was passed in if the block did not need to be relocated.
00401  */
00402 #define rtxMemReallocArray(pctxt,mem_p,n,type) \
00403 (type*)rtxMemHeapRealloc(&(pctxt)->pMemHeap, (void*)mem_p, sizeof(type)*n)
00404 
00405 #ifdef __cplusplus
00406 }
00407 #endif
00408 
00411 #endif /*__RTXMEMORY_H__*/