ESYS13  Revision_
mem.h
Go to the documentation of this file.
00001 
00002 /*******************************************************
00003 *
00004 * Copyright (c) 2003-2012 by University of Queensland
00005 * Earth Systems Science Computational Center (ESSCC)
00006 * http://www.uq.edu.au/esscc
00007 *
00008 * Primary Business: Queensland, Australia
00009 * Licensed under the Open Software License version 3.0
00010 * http://www.opensource.org/licenses/osl-3.0.php
00011 *
00012 *******************************************************/
00013 
00014 
00015 #ifndef INC_ESYS_MEM
00016 #define INC_ESYS_MEM
00017 
00018 /**************************************************************/
00019 /*   Macros to deal with memory management */
00020 /********************************************/
00021 
00022 
00023 /**************************************************************/
00024 /*    memory allocation:                                      */
00025 /*    Wise to not use PASO_MALLOC/FREE/REALLOC and            */
00026 /*    PASO_THREAD... directly. These are only for tailoring   */
00027 /*    the main macros that follow                             */
00028 /**************************************************************/
00029 
00030 
00031 /*#if defined(_WIN32) */ /* Use python for memory management on windows. */
00032 /*
00033   #include <python.h>
00034 
00035   #define PASO_MALLOC PyMem_Malloc
00036   #define PASO_FREE PyMem_Free
00037   #define PASO_REALLOC PyMem_Realloc
00038 
00039 #else
00040 */
00041   #include <stdlib.h>
00042 
00043   #define PASO_MALLOC malloc
00044   #define PASO_FREE free
00045   #define PASO_REALLOC realloc
00046 
00047 /*#endif */
00048 
00049 /* FIXME: This is not satisfactory.                                */
00050 /* _ECC, __INTEL_COMPILER, and other                               */
00051 /* intel compiler pre-defines need to be handled                   */
00052 /* (__ICL, __ICC come to mind)                                     */
00053 /* Also, _WIN32 may take this branch one day...                    */
00054 /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
00055 
00056 #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
00057   #include <omp.h>
00058   #define PASO_THREAD_MALLOC kmp_malloc
00059   #define PASO_THREAD_FREE kmp_free
00060 #else
00061   #define PASO_THREAD_MALLOC PASO_MALLOC
00062   #define PASO_THREAD_FREE PASO_FREE
00063 #endif
00064 
00065 
00066 /* Prepare for the day that this becomes sharable. */
00067 /* and we wish to do multi-file optimisations on windows */
00068 
00069 #define PASO_DLL_API
00070 
00071 #ifdef _WIN32
00072 #   ifndef PASO_STATIC_LIB
00073 #      undef PASO_DLL_API
00074 #      ifdef PASO_EXPORTS
00075 #         define PASO_DLL_API __declspec(dllexport)
00076 #      else
00077 #         define PASO_DLL_API __declspec(dllimport)
00078 #      endif
00079 #   endif
00080 #endif
00081 
00082 
00083 /******************The main macros ************************************/ 
00084 
00085 #define MEMALLOC(_LENGTH_,_TYPE_)                                     \
00086   (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
00087 
00088 /* do {} while(0) -  an old trick for bracketing a macro that */
00089 /* makes sure a semi-colon does no harm.                      */
00090 
00091 #define MEMFREE(_PTR_)                                                  \
00092 do                                                                      \
00093 {                                                                       \
00094   if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; }   \
00095 } while(0)
00096 
00097 #define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_)                    \
00098 do                                                                        \
00099 {                                                                         \
00100    if( (_POINTER_)!=NULL )                                                \
00101    {                                                                      \
00102       _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_),               \
00103                                    ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
00104    }                                                                      \
00105    else                                                                   \
00106    {                                                                      \
00107       _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
00108    }                                                                      \
00109 } while(0)
00110 
00111 #define TMPMEMALLOC MEMALLOC
00112 #define TMPMEMFREE MEMFREE
00113 #define TMPMEMREALLOC MEMREALLOC
00114 
00115 #define THREAD_MEMALLOC(_LENGTH_,_TYPE_)                          \
00116    (_TYPE_*) PASO_THREAD_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
00117 
00118 #define THREAD_MEMFREE(_PTR_)                                                \
00119 do                                                                           \
00120 {                                                                            \
00121   if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
00122 } while(0)
00123 
00124 
00125 #endif