escript  Revision_
mem.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16 
17 
18 #ifndef INC_ESYS_MEM
19 #define INC_ESYS_MEM
20 
21 /****************************************************************************/
22 /* Macros to deal with memory management */
23 /********************************************/
24 
25 
26 /****************************************************************************/
27 /* memory allocation: */
28 /* Wise to not use PASO_MALLOC/FREE/REALLOC and */
29 /* PASO_THREAD... directly. These are only for tailoring */
30 /* the main macros that follow */
31 /****************************************************************************/
32 
33 
34 #include <stdlib.h>
35 
36 #define PASO_MALLOC malloc
37 #define PASO_FREE free
38 #define PASO_REALLOC realloc
39 
40 
41 /* FIXME: This is not satisfactory. */
42 /* _ECC, __INTEL_COMPILER, and other */
43 /* intel compiler pre-defines need to be handled */
44 /* (__ICL, __ICC come to mind) */
45 /* Also, _WIN32 may take this branch one day... */
46 /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
47 
48 #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
49  #include <omp.h>
50  #define PASO_THREAD_MALLOC kmp_malloc
51  #define PASO_THREAD_FREE kmp_free
52 #else
53  #define PASO_THREAD_MALLOC PASO_MALLOC
54  #define PASO_THREAD_FREE PASO_FREE
55 #endif
56 
57 
58 /******************The main macros ************************************/
59 
60 #define MEMALLOC(_LENGTH_,_TYPE_) \
61  (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
62 
63 /* do {} while(0) - an old trick for bracketing a macro that */
64 /* makes sure a semi-colon does no harm. */
65 
66 #define MEMFREE(_PTR_) \
67 do \
68 { \
69  if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; } \
70 } while(0)
71 
72 #define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_) \
73 do \
74 { \
75  if( (_POINTER_)!=NULL ) \
76  { \
77  _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_), \
78  ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
79  } \
80  else \
81  { \
82  _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
83  } \
84 } while(0)
85 
86 #define TMPMEMALLOC MEMALLOC
87 #define TMPMEMFREE MEMFREE
88 #define TMPMEMREALLOC MEMREALLOC
89 
90 #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) \
91  (_TYPE_*) PASO_THREAD_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
92 
93 #define THREAD_MEMFREE(_PTR_) \
94 do \
95 { \
96  if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
97 } while(0)
98 
99 
100 #endif