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) 1997-2004 by 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 #define OSRTXM_K_MEMBLKSIZ         (4*1024)
00043 
00044 struct OSRTDList;
00045 
00059 #define OSRTALLOCTYPE(pctxt,type) \
00060 (type*) rtxMemHeapAllocZ (&(pctxt)->pTypeMemHeap, sizeof(type))
00061 
00062 #define OSRTALLOCOCTETARRAY(pctxt,n) \
00063 (OSOCTET*) rtxMemHeapAllocZ (&(pctxt)->pTypeMemHeap, n)
00064 
00065 #define OSRTALLOCTYPEARRAY(pctxt,type,n) \
00066 (type*) rtxMemHeapAllocZ (&(pctxt)->pTypeMemHeap, n * sizeof(type))
00067 
00068 #define OSRTALLOCELEMDNODE(pctxt,type) \
00069 (type*) (((char*)rtxMemHeapAllocZ (&(pctxt)->pTypeMemHeap, sizeof(type) + \
00070 sizeof(OSRTDListNode))) + sizeof(OSRTDListNode))
00071 
00072 #define OSRTALLOCELEMSNODE(pctxt,type) \
00073 (type*) (((char*)rtxMemHeapAllocZ (&(pctxt)->pTypeMemHeap, sizeof(type) + \
00074 sizeof(OSRTSListNode))) + sizeof(OSRTSListNode))
00075 
00081 #define OSRTARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
00082 
00093 #define OSMALLOC(pctxt,nbytes) \
00094 rtxMemHeapAlloc(&(pctxt)->pTypeMemHeap, nbytes)
00095 
00096 #define OSMALLOCZ(pctxt,nbytes) \
00097 rtxMemHeapAllocZ(&(pctxt)->pTypeMemHeap, nbytes)
00098 
00116 #define OSREALLOC(pctxt,pmem,nbytes) \
00117 rtxMemHeapRealloc(&(pctxt)->pTypeMemHeap, pmem, nbytes)
00118 
00130 #define OSRTREALLOCARRAY(pctxt,pseqof,type) do {\
00131 if (sizeof(type)*(pseqof)->n < (pseqof)->n) return RTERR_NOMEM; \
00132 if (((pseqof)->elem = (type*) rtxMemHeapRealloc \
00133 (&(pctxt)->pTypeMemHeap, (pseqof)->elem, sizeof(type)*(pseqof)->n)) == 0) \
00134 return RTERR_NOMEM; \
00135 } while (0)
00136 
00145 #define OSMEMFREE(pctxt) \
00146 rtxMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
00147 
00159 #define OSFREEPTR(pctxt,pmem)  \
00160 rtxMemHeapFreePtr(&(pctxt)->pTypeMemHeap, (void*)pmem)
00161 
00175 #define OSMEMRESET(pctxt) \
00176 rtxMemHeapReset(&(pctxt)->pTypeMemHeap)
00177 
00178 #ifndef _NO_MALLOC
00179 #define OSCRTMALLOC0(nbytes)       malloc(nbytes)
00180 #define OSCRTFREE0(ptr)            free(ptr)
00181 #else
00182 
00183 #ifdef _NO_THREADS
00184 extern EXTERNRTX OSCTXT g_ctxt;
00185 
00186 #define OSCRTMALLOC0(nbytes)       rtxMemAlloc(&g_ctxt,(nbytes))
00187 #define OSCRTFREE0(ptr)            rtxMemFreePtr(&g_ctxt,(ptr))
00188 #else
00189 #define OSCRTMALLOC0(nbytes)       (void*)0
00190 #define OSCRTFREE0(ptr)            (void*)0
00191 
00192 #endif /* _NO_THREADS */
00193 #endif /* _NO_MALLOC */
00194 
00195 #define OSCRTMALLOC OSMALLOC
00196 #define OSCRTFREE   OSFREEPTR
00197 
00198 #ifdef __cplusplus
00199 extern "C" {
00200 #endif
00201 
00202 /* Alias for __cdecl modifier; if __cdecl keyword is not supported, 
00203  * redefine it as empty macro. */
00204 
00205 #if !defined(OSCDECL)
00206 #if defined(_MSC_VER) || defined(__BORLANDC__)
00207 #define OSCDECL __cdecl
00208 #else
00209 #define OSCDECL
00210 #endif
00211 #endif /* OSCDECL */
00212 
00213 /* Pointers to C Run-Time memory allocation functions *
00214  * (See rtxMemSetAllocFuncs)                           */
00215 
00216 typedef void *(OSCDECL *OSMallocFunc ) (size_t size);
00217 typedef void *(OSCDECL *OSReallocFunc) (void *ptr, size_t size);
00218 typedef void  (OSCDECL *OSFreeFunc   ) (void *ptr);
00219 
00220 EXTERNRTX void  rtxMemHeapAddRef (void** ppvMemHeap);
00221 EXTERNRTX void* rtxMemHeapAlloc (void** ppvMemHeap, size_t nbytes);
00222 EXTERNRTX void* rtxMemHeapAllocZ (void** ppvMemHeap, size_t nbytes);
00223 EXTERNRTX int   rtxMemHeapCheckPtr (void** ppvMemHeap, void* mem_p);
00224 EXTERNRTX int   rtxMemHeapCreate (void** ppvMemHeap);
00225 EXTERNRTX void  rtxMemHeapFreeAll (void** ppvMemHeap);
00226 EXTERNRTX void  rtxMemHeapFreePtr (void** ppvMemHeap, void* mem_p);
00227 EXTERNRTX void* rtxMemHeapMarkSaved (void** ppvMemHeap, 
00228                                      const void* mem_p, 
00229                                      OSBOOL saved);
00230 
00231 EXTERNRTX void* rtxMemHeapRealloc (void** ppvMemHeap, void* mem_p, size_t nbytes_);
00232 EXTERNRTX void  rtxMemHeapRelease (void** ppvMemHeap);
00233 EXTERNRTX void  rtxMemHeapReset (void** ppvMemHeap);
00234 EXTERNRTX void  rtxMemHeapSetProperty (void** ppvMemHeap, 
00235                                        OSUINT32 propId, void* pProp);
00236 
00237 
00238 
00254 EXTERNRTX void  rtxMemSetAllocFuncs (OSMallocFunc malloc_func,
00255                                      OSReallocFunc realloc_func,
00256                                      OSFreeFunc free_func);
00257 
00258 EXTERNRTX void  rtxMemFreeOpenSeqExt (OSCTXT* pctxt, struct OSRTDList *pElemList);
00259 
00260 /*
00261  * This function sets flags to a heap. May be used to control the heap's
00262  * behavior.
00263  *
00264  * @param pctxt        Pointer to a memory block structure that contains the
00265  *                       list of dynamic memory block maintained by these
00266  *                       functions.
00267  * @param flags        The flags.
00268  */
00269 EXTERNRTX void  rtxMemHeapSetFlags (OSCTXT* pctxt, OSUINT32 flags);
00270 
00271 /*
00272  * This function clears memory heap flags.
00273  *
00274  * @param pctxt        Pointer to a memory block structure that contains the
00275  *                       list of dynamic memory block maintained by these
00276  *                       functions.
00277  * @param flags        The flags
00278  */
00279 EXTERNRTX void  rtxMemHeapClearFlags (OSCTXT* pctxt, OSUINT32 flags);
00280 
00294 EXTERNRTX void  rtxMemHeapSetDefBlkSize (OSCTXT* pctxt, OSUINT32 blkSize);
00295 
00301 EXTERNRTX OSUINT32 rtxMemHeapGetDefBlkSize (OSCTXT* pctxt);
00302 
00311 EXTERNRTX OSBOOL rtxMemIsZero (const void* pmem, size_t memsiz);
00312 
00313 #ifdef _STATIC_HEAP
00314 EXTERNRTX void rtxMemSetStaticBuf (void* memHeapBuf, OSUINT32 blkSize);
00315 #endif
00316 
00317 
00318 #if !defined (_BUILDCRTLIB)
00319 /* C++ wrappers for new[] and delete[] */
00320 
00330 EXTERNRTX void* rtxMemAlloc (OSCTXT* pctxt, size_t nbytes);
00331 
00341 EXTERNRTX void* rtxMemAllocZ (OSCTXT* pctxt, size_t nbytes);
00342 
00350 EXTERNRTX void rtxMemFreePtr (OSCTXT* pctxt, void* mem_p);
00351 
00367 EXTERNRTX void* rtxMemRealloc 
00368    (OSCTXT* pctxt, void* mem_p, size_t oldnbytes, size_t nbytes);
00369 
00370 #else
00371 /*
00372  * Allocate memory.  This macro allocates the given number of bytes.  It is 
00373  * similar to the C \c malloc run-time function.
00374  * 
00375  * @param pctxt - Pointer to a context block
00376  * @param nbytes - Number of bytes of memory to allocate
00377  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00378  *   was available to fulfill the request.
00379  */
00380 #define rtxMemAlloc(pctxt,nbytes) \
00381 rtxMemHeapAlloc(&(pctxt)->pTypeMemHeap,nbytes)
00382 
00383 /*
00384  * Allocate and zero memory.  This macro allocates the given number of bytes
00385  * and then initializes the memory block to zero.
00386  * 
00387  * @param pctxt - Pointer to a context block
00388  * @param nbytes - Number of bytes of memory to allocate
00389  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00390  *   was available to fulfill the request.
00391  */
00392 #define rtxMemAllocZ(pctxt,nbytes) \
00393 rtxMemHeapAllocZ(&(pctxt)->pTypeMemHeap,nbytes)
00394 
00395 /*
00396  * Reallocate memory.  This macro reallocates a memory block (either 
00397  * expands or contracts) to the given number of bytes.  It is 
00398  * similar to the C \c realloc run-time function.
00399  * 
00400  * @param pctxt - Pointer to a context block
00401  * @param mem_p - Pointer to memory block to reallocate.  This must have been 
00402  *   allocated using the OSMALLOC macro or the rtxMemHeapAlloc function.
00403  * @param oldnbytes  The number of bytes already allocated.
00404  * @param nbytes - Number of bytes of memory to which the block is to be 
00405  *   resized.
00406  * @return - Void pointer to allocated memory or NULL if insufficient memory 
00407  *   was available to fulfill the request.  This may be the same as the pmem 
00408  *   pointer that was passed in if the block did not need to be relocated.
00409  */
00410 #define rtxMemRealloc(pctxt,mem_p,oldnbytes,nbytes) \
00411 rtxMemHeapRealloc(&(pctxt)->pTypeMemHeap, (void*)mem_p, nbytes)
00412 
00413 /*
00414  * Free memory pointer.  This macro frees memory at the given pointer.  
00415  * The memory must have been allocated using the OSMALLOC (or similar) 
00416  * macros or the rtxMem memory allocation macros.  This macro is 
00417  * similar to the C \c free function.
00418  * 
00419  * @param pctxt - Pointer to a context block
00420  * @param mem_p - Pointer to memory block to free.  This must have 
00421  *   been allocated using the OSMALLOC or rtxMemAlloc macro or the 
00422  *   rtxMemHeapAlloc function.
00423  */
00424 #define rtxMemFreePtr(pctxt,mem_p) \
00425 if (rtxMemHeapCheckPtr (&(pctxt)->pTypeMemHeap, (void*)mem_p)) \
00426 rtxMemHeapFreePtr(&(pctxt)->pTypeMemHeap, (void*)mem_p)
00427 
00428 /*
00429  * Free memory associated with a context.  This macro frees all memory 
00430  * held within a context.  This is all memory allocated using the 
00431  * OSMALLOC (and similar macros) and the rtxMem memory allocation 
00432  * functions using the given context variable.
00433  * 
00434  * @param pctxt - Pointer to a context block
00435  */
00436 #define rtxMemFree(pctxt) \
00437 rtxMemHeapFreeAll(&(pctxt)->pTypeMemHeap)
00438 
00439 /*
00440  * Reset memory associated with a context.  This macro resets all memory 
00441  * held within a context.  This is all memory allocated using the OSMALLOC 
00442  * (and similar macros) and the rtxMem memory allocation functions using the 
00443  * given context variable.
00444  *
00445  * <p>The difference between this and the OSMEMFREE macro is that the 
00446  * memory blocks held within the context are not actually freed.  Internal 
00447  * pointers are reset so the existing blocks can be reused.  This can 
00448  * provide a performace improvement for repetitive tasks such as decoding 
00449  * messages in a loop.
00450  * 
00451  * @param pctxt - Pointer to a context block
00452  */
00453 #define rtxMemReset(pctxt) \
00454 rtxMemHeapReset(&(pctxt)->pTypeMemHeap)
00455 #endif /* defined(_BUILDCRTLIB) */
00456 
00457 #define rtxMemAllocType(pctxt,type) \
00458 (type*) rtxMemAllocZ ((pctxt), sizeof(type))
00459 
00460 #ifdef __cplusplus
00461 }
00462 #endif
00463 
00466 #endif /*__RTXMEMORY_H__*/

This file was last modified on 1 Jun 2004.
XBinder, Version 1.0.0