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.
18 * 3535 Factoria Blvd, Ste. 550
19 * Bellevue, WA 98006, USA
20 * Phone: +1 425 653 1200
21 * http://www.tecplot.com/
25 ******************************************************************
26 ******************************************************************
28 ****** (C) 1988-2009 Tecplot, Inc. *******
30 ******************************************************************
31 ******************************************************************
37 #if defined __cplusplus
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))
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)
50 #define FREE_ARRAY(X,str) free((void *)(X))
51 #define FREE_ITEM(X,str) free((void *)(X))
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
);
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
74 inline T
*nonExceptionNew(size_t numItems
,
78 REQUIRE(numItems
> 0);
79 REQUIRE(VALID_REF(fileName
));
80 REQUIRE(lineNumber
> 0);
87 #define USING_DEBUG_NEW
89 result
= new(fileName
, lineNumber
) T
[numItems
];
90 #ifdef USING_DEBUG_NEW
92 #undef USING_DEBUG_NEW
95 result
= new T
[numItems
];
98 catch (std::bad_alloc
&)
102 #ifdef TRACK_MEMORY_USAGE
106 trackMemoryAlloc(_msize(result
));
108 trackMemoryAlloc(malloc_usable_size(result
));
112 ENSURE(VALID_REF_OR_NULL(result
));
115 #define ALLOC_ARRAY(N,Type,str) nonExceptionNew<Type>((N),__FILE__,__LINE__)
117 template <typename T
>
118 inline T
*nonExceptionNew(size_t numItems
)
120 REQUIRE(numItems
> 0);
124 result
= new T
[numItems
];
126 catch (std::bad_alloc
&)
130 #ifdef TRACK_MEMORY_USAGE
134 trackMemoryAlloc(_msize(result
));
136 trackMemoryAlloc(malloc_usable_size(result
));
140 ENSURE(VALID_REF_OR_NULL(result
));
143 #define ALLOC_ARRAY(N,Type,str) nonExceptionNew<Type>((N))
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));
158 #if defined TRACK_MEMORY_USAGE
162 trackMemoryFree(_msize(ptr
));
164 trackMemoryFree(malloc_usable_size(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;
178 #define FREE_ARRAY(ptr,str) nonExceptionDelete((ptr))
179 #define FREE_ITEM(ptr,str) FREE_ARRAY(ptr,str)
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(),
198 void operator()(T
*& object
)