Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToTecplot360 / tecio / tecsrc / ALLOC.h
blob8ea1db45f0a62b416a70502740796d44b0efa268
1 /*
2 * NOTICE and LICENSE for Tecplot Input/Output Library (TecIO) - OpenFOAM
4 * Copyright (C) 1988-2009 Tecplot, Inc. All rights reserved worldwide.
6 * Tecplot hereby grants OpenCFD limited authority to distribute without
7 * alteration the source code to the Tecplot Input/Output library, known
8 * as TecIO, as part of its distribution of OpenFOAM and the
9 * OpenFOAM_to_Tecplot converter. Users of this converter are also hereby
10 * granted access to the TecIO source code, and may redistribute it for the
11 * purpose of maintaining the converter. However, no authority is granted
12 * to alter the TecIO source code in any form or manner.
14 * This limited grant of distribution does not supersede Tecplot, Inc.'s
15 * copyright in TecIO. Contact Tecplot, Inc. for further information.
17 * Tecplot, Inc.
18 * 3535 Factoria Blvd, Ste. 550
19 * Bellevue, WA 98006, USA
20 * Phone: +1 425 653 1200
21 * http://www.tecplot.com/
25 ******************************************************************
26 ******************************************************************
27 ******* ********
28 ****** (C) 1988-2009 Tecplot, Inc. *******
29 ******* ********
30 ******************************************************************
31 ******************************************************************
33 #ifndef ALLOC_H
34 #define ALLOC_H
36 #include "TASSERT.h"
37 #if defined __cplusplus
38 #include <new>
39 #endif
41 #if !defined __cplusplus
42 #define ALLOC_ARRAY(N,Type,str) (Type *)malloc((N)*sizeof(Type))
43 #define ALLOC_ITEM(Type,str) (Type *)malloc(sizeof(Type))
44 #ifdef _DEBUG
45 /* NOTE: the pointer is set to 0xFFFF after the free for debug */
46 /* versions in the hopes of catching invalid pointer usage */
47 #define FREE_ARRAY(X,str) do { free((void *)(X)); *((void **)&(X)) = (void *)0xFFFF; } while (0)
48 #define FREE_ITEM(X,str) do { free((void *)(X)); *((void **)&(X)) = (void *)0xFFFF; } while (0)
49 #else
50 #define FREE_ARRAY(X,str) free((void *)(X))
51 #define FREE_ITEM(X,str) free((void *)(X))
52 #endif
53 #else
54 #ifdef TRACK_MEMORY_USAGE
55 extern void initMemoryUsageTracking(void);
56 extern void cleanUpMemoryUsageTracking(void);
57 extern void trackMemoryAlloc(size_t size);
58 extern void trackMemoryFree(size_t size);
59 extern void trackMemoryClearHighMark(void);
60 extern void trackMemorySaveHighMark(void);
61 extern void getMemoryUsage(size_t* memoryInUse,
62 size_t* memoryCurrentHighMark,
63 size_t* memorySavedHighMark,
64 size_t* memoryTotalHighMark);
65 #endif
67 * Create a version of new that returns NULL instead
68 * of throwing std::bad_alloc. A lot of code is written using
69 * ALLOC_ITEM and ALLOC_ARRAY that expect a return value of
70 * NULL on failure instead of the exception. 2008-05-08 CAM
72 #if defined MSWIN && defined _DEBUG
73 template <typename T>
74 inline T *nonExceptionNew(size_t numItems,
75 const char* fileName,
76 int lineNumber)
78 REQUIRE(numItems > 0);
79 REQUIRE(VALID_REF(fileName));
80 REQUIRE(lineNumber > 0);
81 T* result;
82 try
84 #ifdef DEBUG_NEW
85 #ifdef new
86 #undef new
87 #define USING_DEBUG_NEW
88 #endif
89 result = new(fileName, lineNumber) T[numItems];
90 #ifdef USING_DEBUG_NEW
91 #define new DEBUG_NEW
92 #undef USING_DEBUG_NEW
93 #endif
94 #else
95 result = new T[numItems];
96 #endif
98 catch (std::bad_alloc&)
100 result = NULL;
102 #ifdef TRACK_MEMORY_USAGE
103 if (result != NULL)
105 #ifdef MSWIN
106 trackMemoryAlloc(_msize(result));
107 #else
108 trackMemoryAlloc(malloc_usable_size(result));
109 #endif
111 #endif
112 ENSURE(VALID_REF_OR_NULL(result));
113 return result;
115 #define ALLOC_ARRAY(N,Type,str) nonExceptionNew<Type>((N),__FILE__,__LINE__)
116 #else
117 template <typename T>
118 inline T *nonExceptionNew(size_t numItems)
120 REQUIRE(numItems > 0);
121 T *result;
124 result = new T[numItems];
126 catch (std::bad_alloc&)
128 result = NULL;
130 #ifdef TRACK_MEMORY_USAGE
131 if (result != NULL)
133 #ifdef MSWIN
134 trackMemoryAlloc(_msize(result));
135 #else
136 trackMemoryAlloc(malloc_usable_size(result));
137 #endif
139 #endif
140 ENSURE(VALID_REF_OR_NULL(result));
141 return result;
143 #define ALLOC_ARRAY(N,Type,str) nonExceptionNew<Type>((N))
144 #endif
145 #define ALLOC_ITEM(Type,str) ALLOC_ARRAY(1,Type,str)
148 * Although delete doesn't throw exceptions, this function matches
149 * nonExceptionNew, and also reports the size of the block if we
150 * are tracking memory.
152 template <typename T>
153 inline void nonExceptionDelete(T* &ptr)
155 #if defined MSWIN && !defined NO_ASSERTS
156 CHECK(!IsBadReadPtr((void*)ptr, 1));
157 #endif
158 #if defined TRACK_MEMORY_USAGE
159 if (ptr != NULL)
161 #ifdef MSWIN
162 trackMemoryFree(_msize(ptr));
163 #else
164 trackMemoryFree(malloc_usable_size(ptr));
165 #endif
167 #endif
169 delete [] ptr;
170 #if !defined NO_ASSERTS
172 * NOTE: the pointer is set to 0xFFFF after the free for asserted
173 * builds in the hopes of catching invalid pointer usage
175 ptr = (T*)(void*)0xFFFF;
176 #endif
178 #define FREE_ARRAY(ptr,str) nonExceptionDelete((ptr))
179 #define FREE_ITEM(ptr,str) FREE_ARRAY(ptr,str)
180 #endif
183 * The following functor can be used to easily deallocate memory from containers
184 * that hold pointers to allocated objects. For example:
186 * vector<MyObject*> container;
187 * for (int ii = 0; ii < 10; ii++
188 * container.push_back(new MyObject);
189 * ... do something with the objects ...
190 * ... now we need to clean up ...
191 * for_each(container.begin(),
192 * container.end(),
193 * DeleteItem());
195 struct DeleteItem
197 template<typename T>
198 void operator()(T*& object)
200 delete object;
201 object = NULL;
205 #endif /* ALLOC_H */