Escript  Revision_4320
mem.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15 
16 
17 #ifndef INC_ESYS_MEM
18 #define INC_ESYS_MEM
19 
20 /************************************************************************************/
21 /* Macros to deal with memory management */
22 /********************************************/
23 
24 
25 /************************************************************************************/
26 /* memory allocation: */
27 /* Wise to not use PASO_MALLOC/FREE/REALLOC and */
28 /* PASO_THREAD... directly. These are only for tailoring */
29 /* the main macros that follow */
30 /************************************************************************************/
31 
32 
33 /*#if defined(_WIN32) */ /* Use python for memory management on windows. */
34 /*
35  #include <python.h>
36 
37  #define PASO_MALLOC PyMem_Malloc
38  #define PASO_FREE PyMem_Free
39  #define PASO_REALLOC PyMem_Realloc
40 
41 #else
42 */
43  #include <stdlib.h>
44 
45  #define PASO_MALLOC malloc
46  #define PASO_FREE free
47  #define PASO_REALLOC realloc
48 
49 /*#endif */
50 
51 /* FIXME: This is not satisfactory. */
52 /* _ECC, __INTEL_COMPILER, and other */
53 /* intel compiler pre-defines need to be handled */
54 /* (__ICL, __ICC come to mind) */
55 /* Also, _WIN32 may take this branch one day... */
56 /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
57 
58 #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
59  #include <omp.h>
60  #define PASO_THREAD_MALLOC kmp_malloc
61  #define PASO_THREAD_FREE kmp_free
62 #else
63  #define PASO_THREAD_MALLOC PASO_MALLOC
64  #define PASO_THREAD_FREE PASO_FREE
65 #endif
66 
67 
68 /* Prepare for the day that this becomes sharable. */
69 /* and we wish to do multi-file optimisations on windows */
70 
71 #define PASO_DLL_API
72 
73 #ifdef _WIN32
74 # ifndef PASO_STATIC_LIB
75 # undef PASO_DLL_API
76 # ifdef PASO_EXPORTS
77 # define PASO_DLL_API __declspec(dllexport)
78 # else
79 # define PASO_DLL_API __declspec(dllimport)
80 # endif
81 # endif
82 #endif
83 
84 
85 /******************The main macros ************************************/
86 
87 #define MEMALLOC(_LENGTH_,_TYPE_) \
88  (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
89 
90 /* do {} while(0) - an old trick for bracketing a macro that */
91 /* makes sure a semi-colon does no harm. */
92 
93 #define MEMFREE(_PTR_) \
94 do \
95 { \
96  if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; } \
97 } while(0)
98 
99 #define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_) \
100 do \
101 { \
102  if( (_POINTER_)!=NULL ) \
103  { \
104  _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_), \
105  ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
106  } \
107  else \
108  { \
109  _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
110  } \
111 } while(0)
112 
113 #define TMPMEMALLOC MEMALLOC
114 #define TMPMEMFREE MEMFREE
115 #define TMPMEMREALLOC MEMREALLOC
116 
117 #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) \
118  (_TYPE_*) PASO_THREAD_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
119 
120 #define THREAD_MEMFREE(_PTR_) \
121 do \
122 { \
123  if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
124 } while(0)
125 
126 
127 #endif