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

Google


Objective Systems, Inc.

rtxMemory.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2003-2006 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/rtxCommon.h"
00032 #include "rtxsrc/rtxSysTypes.h"
00033 
00034 #define RT_MH_DONTKEEPFREE 0x1
00035 
00036 #define OSRTMH_PROPID_DEFBLKSIZE   1
00037 #define OSRTMH_PROPID_SETFLAGS     2
00038 #define OSRTMH_PROPID_CLEARFLAGS   3
00039 
00040 #define OSRTMH_PROPID_USER         10
00041 
00042 #ifndef OSRTXM_K_MEMBLKSIZ
00043 #define OSRTXM_K_MEMBLKSIZ         (4*1024)
00044 #endif
00045 
00059 #define OSRTALLOCTYPE(pctxt,type) \
00060 (type*) rtxMemHeapAlloc (&(pctxt)->pTypeMemHeap, sizeof(type))
00061 
00068 #define OSRTALLOCTYPEZ(pctxt,type) \
00069 (type*) rtxMemHeapAllocZ (&(pctxt)->pTypeMemHeap, sizeof(type))
00070 
00076 #define OSRTARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
00077 
00078 #ifndef _NO_MALLOC
00079 #define OSCRTMALLOC0(nbytes)       malloc(nbytes)
00080 #define OSCRTFREE0(ptr)            free(ptr)
00081 #else
00082 
00083 #ifdef _NO_THREADS
00084 extern EXTERNRTX OSCTXT g_ctxt;
00085 
00086 #define OSCRTMALLOC0(nbytes)       rtxMemAlloc(&g_ctxt,(nbytes))
00087 #define OSCRTFREE0(ptr)            rtxMemFreePtr(&g_ctxt,(ptr))
00088 #else
00089 #define OSCRTMALLOC0(nbytes)       (void*)0
00090 #define OSCRTFREE0(ptr)            (void*)0
00091 
00092 #endif /* _NO_THREADS */
00093 #endif /* _NO_MALLOC */
00094 
00095 #define OSCRTMALLOC OSMALLOC
00096 #define OSCRTFREE   OSFREEPTR
00097 
00098 struct OSRTDList;
00099 
00100 #ifdef __cplusplus
00101 extern "C" {
00102 #endif
00103 
00104 /* Alias for __cdecl modifier; if __cdecl keyword is not supported, 
00105  * redefine it as empty macro. */
00106 
00107 #if !defined(OSCDECL)
00108 #if defined(_MSC_VER) || defined(__BORLANDC__)
00109 #define OSCDECL __cdecl
00110 #else
00111 #define OSCDECL
00112 #endif
00113 #endif /* OSCDECL */
00114 
00115 /* Pointers to C Run-Time memory allocation functions *
00116  * (See rtxMemSetAllocFuncs)                          */
00117 
00118 typedef void *(OSCDECL *OSMallocFunc ) (size_t size);
00119 typedef void *(OSCDECL *OSReallocFunc) (void *ptr, size_t size);
00120 typedef void  (OSCDECL *OSFreeFunc   ) (void *ptr);
00121 
00122 #ifdef _RT_ALLOC_FUNC_WITH_CONTEXT
00123 
00124 /* Variation of the memory allocation functions only available *
00125  * after recompilation of the runtime with the                 *
00126  * (See rtxMemSetAllocCtxtFuncs)                               */
00127 
00128 typedef void *(OSCDECL *OSMallocCtxtFunc ) (OSCTXT* pctxt, size_t size);
00129 typedef void *(OSCDECL *OSReallocCtxtFunc) (OSCTXT* pctxt, void *ptr, size_t size);
00130 typedef void  (OSCDECL *OSFreeCtxtFunc   ) (OSCTXT* pctxt, void *ptr);
00131 
00132 #endif
00133 
00134 EXTERNRTX void  rtxMemHeapAddRef (void** ppvMemHeap);
00135 EXTERNRTX void* rtxMemHeapAlloc (void** ppvMemHeap, size_t nbytes);
00136 EXTERNRTX void* rtxMemHeapAllocZ (void** ppvMemHeap, size_t nbytes);
00137 EXTERNRTX int   rtxMemHeapCheckPtr (void** ppvMemHeap, void* mem_p);
00138 EXTERNRTX int   rtxMemHeapCreate (void** ppvMemHeap);
00139 EXTERNRTX void  rtxMemHeapFreeAll (void** ppvMemHeap);
00140 EXTERNRTX void  rtxMemHeapFreePtr (void** ppvMemHeap, void* mem_p);
00141 EXTERNRTX void* rtxMemHeapMarkSaved (void** ppvMemHeap, 
00142                                      const void* mem_p, 
00143                                      OSBOOL saved);
00144 EXTERNRTX void* rtxMemHeapRealloc (void** ppvMemHeap, 
00145                                    void* mem_p, size_t nbytes_);
00146 EXTERNRTX void  rtxMemHeapRelease (void** ppvMemHeap);
00147 EXTERNRTX void  rtxMemHeapReset (void** ppvMemHeap);
00148 EXTERNRTX void  rtxMemHeapSetProperty (void** ppvMemHeap, 
00149                                        OSUINT32 propId, void* pProp);
00150 
00151 
00152 
00168 EXTERNRTX void rtxMemSetAllocFuncs (OSMallocFunc malloc_func,
00169                                     OSReallocFunc realloc_func,
00170                                     OSFreeFunc free_func);
00171 
00172 #ifdef _RT_ALLOC_FUNC_WITH_CONTEXT
00173 
00174 EXTERNRTX void rtxMemSetAllocCtxtFuncs (OSMallocCtxtFunc malloc_func,
00175                                        OSReallocCtxtFunc realloc_func,
00176                                        OSFreeCtxtFunc free_func);
00177 
00178 #endif
00179 
00180 EXTERNRTX void rtxMemFreeOpenSeqExt 
00181 (OSCTXT* pctxt, struct OSRTDList *pElemList);
00182 
00183 /*
00184  * This function sets flags to a heap. May be used to control the heap's
00185  * behavior.
00186  *
00187  * @param pctxt        Pointer to a memory block structure that contains the
00188  *                       list of dynamic memory block maintained by these
00189  *                       functions.
00190  * @param flags        The flags.
00191  */
00192 EXTERNRTX void  rtxMemHeapSetFlags (OSCTXT* pctxt, OSUINT32 flags);
00193 
00194 /*
00195  * This function clears memory heap flags.
00196  *
00197  * @param pctxt        Pointer to a memory block structure that contains the
00198  *                       list of dynamic memory block maintained by these
00199  *                       functions.
00200  * @param flags        The flags
00201  */
00202 EXTERNRTX void  rtxMemHeapClearFlags (OSCTXT* pctxt, OSUINT32 flags);
00203 
00204 /*
00205  * This function sets the pointer to standard allocation functions. These
00206  * functions are used to allocate/reallocate/free the memory blocks. By
00207  * default, standard C functions - malloc, realloc, and free - are used. But if
00208  * some platforms do not support these functions or some other reasons exist)
00209  * they can be overloaded. The functions being overloaded should have the same
00210  * prototypes as standard ones.
00211  *
00212  * @param pctxt        Pointer to a context block.
00213  * @param blkSize      The currently used minimum size and the granularity of
00214  *                       memory blocks.
00215  */
00216 EXTERNRTX void  rtxMemHeapSetDefBlkSize (OSCTXT* pctxt, OSUINT32 blkSize);
00217 
00218 /*
00219  * This function returns the actual granularity of memory blocks.
00220  *
00221  * @param pctxt        Pointer to a context block.
00222  */
00223 EXTERNRTX OSUINT32 rtxMemHeapGetDefBlkSize (OSCTXT* pctxt);
00224 
00233 EXTERNRTX OSBOOL rtxMemIsZero (const void* pmem, size_t memsiz);
00234 
00235 #ifdef _STATIC_HEAP
00236 EXTERNRTX void rtxMemSetStaticBuf (void* memHeapBuf, OSUINT32 blkSize);
00237 #endif
00238 
00239 #if !defined (_BUILDCRTLIB)
00240 
00250 EXTERNRTX void* rtxMemAlloc (OSCTXT* pctxt, size_t nbytes);
00251 
00262 EXTERNRTX void* rtxMemAllocZ (OSCTXT* pctxt, size_t nbytes);
00263 
00272 EXTERNRTX void rtxMemFree (OSCTXT* pctxt);
00273 
00282 EXTERNRTX void rtxMemFreePtr (OSCTXT* pctxt, void* mem_p);
00283 
00299 EXTERNRTX void* rtxMemRealloc 
00300    (OSCTXT* pctxt, void* mem_p, size_t oldnbytes, size_t nbytes);
00301 
00302 /*
00303  * This function resets all memory held within a context.  This is all 
00304  * memory allocated using the OSMALLOC (and similar macros) and the rtxMem 
00305  * memory allocation functions using the given context variable.
00306  *
00307  * <p>The difference between this and the \c rtxMemFree function is that the 
00308  * memory blocks held within the context are not actually freed.  Internal 
00309  * pointers are reset so the existing blocks can be reused.  This can 
00310  * provide a performace improvement for repetitive tasks such as decoding 
00311  * messages in a loop.
00312  * 
00313  * @param pctxt - Pointer to a context block
00314  */
00315 EXTERNRTX void rtxMemReset (OSCTXT* pctxt);
00316 
00317 #else
00318 /*
00319  * Allocate memory.  This macro allocates the given number of bytes.  It is 
00320  * similar to the C \c malloc run-time function.
00321  * 
00322  * @param pctxt - Pointer to a context block
00323  * @param nbytes - Number of bytes of memory to allocate
00324  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00325  *   was available to fulfill the request.
00326  */
00327 #define rtxMemAlloc(pctxt,nbytes) \
00328 rtxMemHeapAlloc(&(pctxt)->pTypeMemHeap,nbytes)
00329 
00330 /*
00331  * Allocate and zero memory.  This macro allocates the given number of bytes
00332  * and then initializes the memory block to zero.
00333  * 
00334  * @param pctxt - Pointer to a context block
00335  * @param nbytes - Number of bytes of memory to allocate
00336  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00337  *   was available to fulfill the request.
00338  */
00339 #define rtxMemAllocZ(pctxt,nbytes) \
00340 rtxMemHeapAllocZ(&(pctxt)->pTypeMemHeap,nbytes)
00341 
00342 /*
00343  * Reallocate memory.  This macro reallocates a memory block (either 
00344  * expands or contracts) to the given number of bytes.  It is 
00345  * similar to the C \c realloc run-time function.
00346  * 
00347  * @param pctxt - Pointer to a context block
00348  * @param mem_p - Pointer to memory block to reallocate.  This must have been 
00349  *   allocated using the OSMALLOC macro or the rtxMemHeapAlloc function.
00350  * @param oldnbytes  The number of bytes already allocated.
00351  * @param nbytes - Number of bytes of memory to which the block is to be 
00352  *   resized.
00353  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00354  *   was available to fulfill the request.  This may be the same as the pmem 
00355  *   pointer that was passed in if the block did not need to be relocated.
00356  */
00357 #define rtxMemRealloc(pctxt,mem_p,oldnbytes,nbytes) \
00358 rtxMemHeapRealloc(&(pctxt)->pTypeMemHeap, (void*)mem_p, nbytes)
00359 
00360 /*
00361  * Free memory pointer.  This macro frees memory at the given pointer.  
00362  * The memory must have been allocated using the OSMALLOC (or similar) 
00363  * macros or the rtxMem memory allocation macros.  This macro is 
00364  * similar to the C \c free function.
00365  * 
00366  * @param pctxt - Pointer to a context block
00367  * @param mem_p - Pointer to memory block to free.  This must have 
00368  *   been allocated using the OSMALLOC or rtxMemAlloc macro or the 
00369  *   rtxMemHeapAlloc function.
00370  */
00371 #define rtxMemFreePtr(pctxt,mem_p) \
00372 if (rtxMemHeapCheckPtr (&(pctxt)->pTypeMemHeap, (void*)mem_p)) \
00373 rtxMemHeapFreePtr(&(pctxt)->pTypeMemHeap, (void*)mem_p)
00374 
00375 /*
00376  * Free memory associated with a context.  This macro frees all memory 
00377  * held within a context.  This is all memory allocated using the 
00378  * OSMALLOC (and similar macros) and the rtxMem memory allocation 
00379  * functions using the given context variable.
00380  * 
00381  * @param pctxt - Pointer to a context block
00382  */
00383 #define rtxMemFree(pctxt) \
00384 rtxMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
00385 
00386 /*
00387  * Reset memory associated with a context.  This macro resets all memory 
00388  * held within a context.  This is all memory allocated using the OSMALLOC 
00389  * (and similar macros) and the rtxMem memory allocation functions using the 
00390  * given context variable.
00391  *
00392  * <p>The difference between this and the OSMEMFREE macro is that the 
00393  * memory blocks held within the context are not actually freed.  Internal 
00394  * pointers are reset so the existing blocks can be reused.  This can 
00395  * provide a performace improvement for repetitive tasks such as decoding 
00396  * messages in a loop.
00397  * 
00398  * @param pctxt - Pointer to a context block
00399  */
00400 #define rtxMemReset(pctxt) \
00401 rtxMemHeapReset(&(pctxt)->pTypeMemHeap)
00402 #endif /* defined(_BUILDCRTLIB) */
00403 
00404 #define rtxMemAllocType(pctxt,type) \
00405 (type*) rtxMemAllocZ ((pctxt), sizeof(type))
00406 
00407 #ifdef __cplusplus
00408 }
00409 #endif
00410 
00413 #ifdef _STATIC_HEAP
00414 EXTERNRTX void rtxMemSetStaticBuf (void* memHeapBuf, OSUINT32 blkSize);
00415 #endif
00416 
00417 #endif /*__RTXMEMORY_H__*/

This file was last modified on 8 Jan 2007.
XBinder, Version 1.1.9