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