Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / postProcessing / dataConversion / foamToTecplot360 / tecio / tecsrc / alloc.cpp
blob19ac3e0a0685cafa59af24cd2c5f78e26228a23f
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/
24 #include "stdafx.h"
25 #include "MASTER.h"
27 #define TECPLOTENGINEMODULE
30 ******************************************************************
31 ******************************************************************
32 ******* ********
33 ****** (C) 1988-2008 Tecplot, Inc. *******
34 ******* ********
35 ******************************************************************
36 ******************************************************************
39 #define ALLOCMODULE
40 #include "GLOBAL.h"
41 #include "ALLOC.h"
42 #include "TASSERT.h"
43 #if defined TECPLOTKERNEL
44 /* CORE SOURCE CODE REMOVED */
45 #endif
47 #if defined TRACK_MEMORY_USAGE
48 static size_t memInUse = 0;
49 static size_t memTotalHighMark = 0;
50 static size_t memCurrentHighMark = 0;
51 static size_t memSavedHighMark = 0;
52 Mutex_pa memMutex;
54 void initMemoryUsageTracking(void)
56 REQUIRE(!Thread_ThreadingIsInitialized());
57 Thread_InitMutex(&memMutex);
60 void cleanUpMemoryUsageTracking(void)
62 REQUIRE(!Thread_ThreadingIsInitialized());
63 Thread_FreeMutex(&memMutex);
66 void trackMemoryClearHighMark(void)
68 memCurrentHighMark = memInUse;
71 void trackMemorySaveHighMark(void)
73 memSavedHighMark = memCurrentHighMark;
76 void trackMemoryAlloc(size_t size)
78 REQUIRE(memInUse >= 0);
80 if (Thread_ThreadingIsInitialized())
81 Thread_LockMutex(memMutex);
83 memInUse += size;
84 if (memInUse > memTotalHighMark)
85 memTotalHighMark = memInUse;
86 if (memInUse > memCurrentHighMark)
87 memCurrentHighMark = memInUse;
89 if (Thread_ThreadingIsInitialized())
90 Thread_UnlockMutex(memMutex);
93 void trackMemoryFree(size_t size)
95 if (Thread_ThreadingIsInitialized())
96 Thread_LockMutex(memMutex);
98 memInUse -= size;
100 if (Thread_ThreadingIsInitialized())
101 Thread_UnlockMutex(memMutex);
103 ENSURE(memInUse >= 0);
106 void getMemoryUsage(size_t* memoryInUse,
107 size_t* memoryCurrentHighMark,
108 size_t* memorySavedHighMark,
109 size_t* memoryTotalHighMark)
111 REQUIRE(VALID_REF_OR_NULL(memoryInUse));
112 REQUIRE(VALID_REF_OR_NULL(memoryCurrentHighMark));
113 REQUIRE(VALID_REF_OR_NULL(memorySavedHighMark));
114 REQUIRE(VALID_REF_OR_NULL(memoryTotalHighMark));
115 if (memoryInUse != NULL)
116 *memoryInUse = memInUse;
117 if (memoryCurrentHighMark != NULL)
118 *memoryCurrentHighMark = memCurrentHighMark;
119 if (memorySavedHighMark != NULL)
120 *memorySavedHighMark = memSavedHighMark;
121 if (memoryTotalHighMark != NULL)
122 *memoryTotalHighMark = memTotalHighMark;
124 #endif
126 #if defined MSWIN && defined ALLOC_HEAP
127 #define HEAPMIN 512
128 #endif
130 #if defined MSWIN && defined ALLOC_HEAP
133 void *MSWinAlloc(DWORD nSize)
135 long *pMem = NULL;
136 if (nSize < HEAPMIN)
137 pMem = (long *)malloc(sizeof(long) + nSize);
138 else
139 pMem = (long *)HeapAlloc(GetProcessHeap(), NULL, sizeof(long) + nSize);
140 if (pMem)
141 pMem[0] = nSize;
142 return (void *)&(pMem[1]);
144 #endif
146 #if defined MSWIN && defined ALLOC_HEAP
149 void MSWinFree(void *pMem)
151 REQUIRE(VALID_REF(pMem));
152 if (pMem)
154 long *pMemLong = &(((long *)pMem)[-1]);
155 if (pMemLong[0] < HEAPMIN)
156 free((void *)pMemLong);
157 else
158 HeapFree(GetProcessHeap(), NULL, (void *)pMemLong);
161 #endif