Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-02-04 |
2 | // Created by: Mikhail SAZONOV | |
3 | // Copyright (c) 2011-2012 OPEN CASCADE SAS | |
4 | // | |
5 | // The content of this file is subject to the Open CASCADE Technology Public | |
6 | // License Version 6.5 (the "License"). You may not use the content of this file | |
7 | // except in compliance with the License. Please obtain a copy of the License | |
8 | // at http://www.opencascade.org and read it completely before using this file. | |
9 | // | |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its | |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. | |
12 | // | |
13 | // The Original Code and all software distributed under the License is | |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the | |
15 | // Initial Developer hereby disclaims all such warranties, including without | |
16 | // limitation, any warranties of merchantability, fitness for a particular | |
17 | // purpose or non-infringement. Please see the License for the specific terms | |
18 | // and conditions governing the rights and limitations under the License. | |
19 | ||
7af17f1e MA |
20 | #include <OSD_MAllocHook.hxx> |
21 | ||
22 | #ifndef WNT | |
23 | #if !defined __STDC_LIMIT_MACROS | |
24 | #define __STDC_LIMIT_MACROS | |
25 | #endif | |
26 | #include <stdint.h> | |
27 | #endif | |
28 | ||
29 | #include <set> | |
30 | #include <map> | |
e2f8b392 | 31 | #include <cstdlib> |
64531d9c | 32 | #include <iomanip> |
7af17f1e | 33 | |
213cb888 M |
34 | #ifndef SIZE_MAX |
35 | #define SIZE_MAX UINT_MAX | |
36 | #endif | |
37 | ||
7af17f1e MA |
38 | #define MAX_STR 80 |
39 | ||
40 | static OSD_MAllocHook::Callback* MypCurrentCallback = NULL; | |
41 | ||
302f96fb | 42 | namespace { |
43 | // dummy function to call at place where break point might be needed | |
44 | inline void place_for_breakpoint () {} | |
45 | }; | |
46 | ||
7af17f1e MA |
47 | //======================================================================= |
48 | //function : GetCallback | |
49 | //purpose : | |
50 | //======================================================================= | |
51 | ||
52 | OSD_MAllocHook::Callback* OSD_MAllocHook::GetCallback() | |
53 | { | |
54 | return MypCurrentCallback; | |
55 | } | |
56 | ||
57 | //======================================================================= | |
58 | //function : GetLogFileHandler | |
59 | //purpose : | |
60 | //======================================================================= | |
61 | ||
62 | OSD_MAllocHook::LogFileHandler* OSD_MAllocHook::GetLogFileHandler() | |
63 | { | |
64 | static LogFileHandler MyHandler; | |
65 | return &MyHandler; | |
66 | } | |
67 | ||
68 | //======================================================================= | |
69 | //function : GetCollectBySize | |
70 | //purpose : | |
71 | //======================================================================= | |
72 | ||
73 | OSD_MAllocHook::CollectBySize* OSD_MAllocHook::GetCollectBySize() | |
74 | { | |
75 | static CollectBySize MyHandler; | |
76 | return &MyHandler; | |
77 | } | |
78 | ||
79 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
80 | // Platform-dependent methods | |
81 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
82 | ||
83 | #ifdef WNT | |
84 | #include <crtdbg.h> | |
85 | ||
86 | static long getRequestNum(void* pvData, long lRequest, size_t& theSize) | |
87 | { | |
88 | if (_CrtIsValidHeapPointer(pvData)) | |
89 | { | |
90 | #if _MSC_VER == 1500 // VS 2008 | |
91 | #define nNoMansLandSize 4 | |
92 | // the header struct is taken from crt/src/dbgint.h | |
93 | struct _CrtMemBlockHeader | |
94 | { | |
95 | #ifdef _WIN64 | |
96 | int nBlockUse; | |
97 | size_t nDataSize; | |
98 | #else | |
99 | size_t nDataSize; | |
100 | int nBlockUse; | |
101 | #endif | |
102 | long lRequest; | |
103 | unsigned char gap[nNoMansLandSize]; | |
104 | }; | |
105 | _CrtMemBlockHeader* aHeader = ((_CrtMemBlockHeader*)pvData)-1; | |
106 | theSize = aHeader->nDataSize; | |
107 | return aHeader->lRequest; | |
108 | #endif | |
109 | } | |
110 | return lRequest; | |
111 | } | |
112 | ||
113 | int __cdecl MyAllocHook(int nAllocType, | |
114 | void * pvData, | |
115 | size_t nSize, | |
116 | int nBlockUse, | |
117 | long lRequest, | |
118 | const unsigned char * /*szFileName*/, | |
119 | int /*nLine*/) | |
120 | { | |
121 | if (nBlockUse == _CRT_BLOCK || // Ignore internal C runtime library allocations | |
122 | MypCurrentCallback == NULL) | |
123 | return(1); | |
124 | ||
125 | if (nAllocType == _HOOK_ALLOC) | |
126 | MypCurrentCallback->AllocEvent(nSize, lRequest); | |
127 | else if (nAllocType == _HOOK_FREE) | |
128 | { | |
129 | // for free hook, lRequest is not defined, | |
130 | // but we can take it from the CRT mem block header | |
131 | size_t aSize = 0; | |
132 | lRequest = getRequestNum(pvData, lRequest, aSize); | |
133 | MypCurrentCallback->FreeEvent(pvData, aSize, lRequest); | |
134 | } | |
135 | else // _HOOK_REALLOC | |
136 | { | |
137 | // for realloc hook, lRequest shows the new request, | |
138 | // and we should get request number for old block | |
139 | size_t anOldSize = 0; | |
140 | long anOldRequest = getRequestNum(pvData, 0, anOldSize); | |
141 | MypCurrentCallback->FreeEvent(pvData, anOldSize, anOldRequest); | |
142 | MypCurrentCallback->AllocEvent(nSize, lRequest); | |
143 | } | |
144 | ||
145 | return(1); // Allow the memory operation to proceed | |
146 | } | |
147 | ||
148 | //======================================================================= | |
149 | //function : SetCallback | |
150 | //purpose : | |
151 | //======================================================================= | |
152 | ||
153 | void OSD_MAllocHook::SetCallback(Callback* theCB) | |
154 | { | |
155 | MypCurrentCallback = theCB; | |
156 | if (theCB == NULL) | |
157 | _CrtSetAllocHook(NULL); | |
158 | else | |
159 | _CrtSetAllocHook(MyAllocHook); | |
160 | } | |
161 | ||
162 | #else // ! WNT | |
163 | ||
164 | // Not yet implemented for non-WNT platform | |
165 | ||
166 | void OSD_MAllocHook::SetCallback(Callback* theCB) | |
167 | { | |
168 | MypCurrentCallback = theCB; | |
169 | } | |
170 | ||
171 | #endif // WNT | |
172 | ||
173 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
174 | // LogFileHandler handler methods | |
175 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
176 | ||
177 | //======================================================================= | |
178 | //function : LogFileHandler::LogFileHandler | |
179 | //purpose : | |
180 | //======================================================================= | |
181 | ||
182 | OSD_MAllocHook::LogFileHandler::LogFileHandler() | |
64531d9c | 183 | : myBreakSize(0) |
7af17f1e | 184 | { |
64531d9c | 185 | myLogFile.imbue (std::locale ("C")); |
7af17f1e MA |
186 | } |
187 | ||
188 | //======================================================================= | |
189 | //function : LogFileHandler::~LogFileHandler | |
190 | //purpose : | |
191 | //======================================================================= | |
192 | ||
193 | OSD_MAllocHook::LogFileHandler::~LogFileHandler() | |
194 | { | |
195 | Close(); | |
196 | } | |
197 | ||
198 | //======================================================================= | |
199 | //function : LogFileHandler::Open | |
200 | //purpose : | |
201 | //======================================================================= | |
202 | ||
203 | Standard_Boolean OSD_MAllocHook::LogFileHandler::Open(const char* theFileName) | |
204 | { | |
205 | Close(); | |
64531d9c | 206 | myLogFile.open (theFileName); |
207 | if (!myLogFile.is_open()) | |
7af17f1e | 208 | { |
64531d9c | 209 | return Standard_False; |
7af17f1e | 210 | } |
64531d9c | 211 | |
212 | myLogFile << "Operation type; Request Number; Block Size\n" | |
213 | "------------------------------------------\n"; | |
214 | return Standard_True; | |
7af17f1e MA |
215 | } |
216 | ||
217 | //======================================================================= | |
218 | //function : LogFileHandler::Close | |
219 | //purpose : | |
220 | //======================================================================= | |
221 | ||
222 | void OSD_MAllocHook::LogFileHandler::Close() | |
223 | { | |
64531d9c | 224 | if (myLogFile.is_open()) |
7af17f1e | 225 | { |
64531d9c | 226 | myLogFile.close(); |
7af17f1e MA |
227 | } |
228 | } | |
229 | ||
230 | //======================================================================= | |
231 | //function : LogFileHandler::MakeReport | |
232 | //purpose : | |
233 | //======================================================================= | |
234 | ||
235 | struct StorageInfo | |
236 | { | |
237 | Standard_Size size; | |
238 | int nbAlloc; | |
239 | int nbFree; | |
240 | int nbLeftPeak; | |
241 | std::set<unsigned long>* alive; | |
242 | StorageInfo() | |
243 | : size(0), nbAlloc(0), nbFree(0), nbLeftPeak(0), alive(NULL) {} | |
244 | StorageInfo(Standard_Size theSize) | |
245 | : size(theSize), nbAlloc(0), nbFree(0), nbLeftPeak(0), alive(NULL) {} | |
246 | ~StorageInfo() | |
247 | { | |
248 | if (alive) | |
249 | delete alive; | |
250 | } | |
251 | std::set<unsigned long>& Alive() | |
252 | { | |
253 | if (!alive) | |
254 | alive = new std::set<unsigned long>; | |
255 | return *alive; | |
256 | } | |
257 | const std::set<unsigned long>& Alive() const | |
258 | { | |
259 | return *alive; | |
260 | } | |
261 | }; | |
262 | ||
263 | inline bool operator < (const StorageInfo& one, const StorageInfo& two) | |
264 | { | |
265 | return one.size < two.size; | |
266 | } | |
267 | ||
268 | Standard_Boolean OSD_MAllocHook::LogFileHandler::MakeReport | |
269 | (const char* theLogFile, | |
270 | const char* theOutFile, | |
271 | const Standard_Boolean theIncludeAlive) | |
272 | { | |
273 | // open log file | |
274 | FILE* aLogFile = fopen(theLogFile, "r"); | |
275 | if (aLogFile == NULL) | |
276 | return Standard_False; | |
277 | ||
278 | // skip 2 header lines | |
279 | char aStr[MAX_STR]; | |
280 | if (fgets(aStr, MAX_STR-1, aLogFile) == NULL) | |
281 | { | |
282 | fclose(aLogFile); | |
283 | return Standard_False; | |
284 | } | |
285 | if (fgets(aStr, MAX_STR-1, aLogFile) == NULL) | |
286 | { | |
287 | fclose(aLogFile); | |
288 | return Standard_False; | |
289 | } | |
290 | ||
291 | // scan the log file | |
292 | size_t aTotalLeftSize = 0; | |
293 | size_t aTotalPeakSize = 0; | |
294 | std::set<StorageInfo> aStMap; | |
295 | while (fgets(aStr, MAX_STR-1, aLogFile) != NULL) | |
296 | { | |
297 | // detect operation type, request number and block size | |
298 | unsigned long aReqNum, aSize; | |
299 | char* aType = aStr; | |
300 | char* pStr = aStr; | |
301 | //sscanf(aStr, "%5s %lu %lu", aType, &aReqNum, &aSize); | |
302 | while (*pStr != ' ' && *pStr) pStr++; | |
303 | *pStr++ = '\0'; | |
304 | while (*pStr == ' ' && *pStr) pStr++; | |
305 | aReqNum = atol(pStr); | |
306 | while (*pStr != ' ' && *pStr) pStr++; | |
307 | while (*pStr == ' ' && *pStr) pStr++; | |
308 | aSize = atol(pStr); | |
309 | Standard_Boolean isAlloc = Standard_False; | |
310 | if (strcmp(aType, "alloc") == 0) | |
311 | { | |
312 | isAlloc = Standard_True; | |
313 | } | |
314 | else if (strcmp(aType, "free") != 0) | |
315 | continue; | |
316 | ||
317 | // collect statistics by storage size | |
318 | StorageInfo aSuchInfo(aSize); | |
319 | std::set<StorageInfo>::iterator aFound = aStMap.find(aSuchInfo); | |
320 | if (aFound == aStMap.end()) | |
321 | aFound = aStMap.insert(aSuchInfo).first; | |
322 | StorageInfo& aInfo = const_cast<StorageInfo&>(*aFound); | |
323 | if (isAlloc) | |
324 | { | |
325 | if (aInfo.nbAlloc + 1 > 0) | |
326 | aInfo.nbAlloc++; | |
327 | aTotalLeftSize += aSize; | |
328 | if (aTotalLeftSize > aTotalPeakSize) | |
329 | aTotalPeakSize = aTotalLeftSize; | |
330 | int nbLeft = aInfo.nbAlloc - aInfo.nbFree; | |
331 | if (nbLeft > aInfo.nbLeftPeak) | |
332 | aInfo.nbLeftPeak = nbLeft; | |
333 | aInfo.Alive().insert(aReqNum); | |
334 | } | |
335 | else | |
336 | { | |
337 | std::set<unsigned long>::iterator aFoundReqNum = | |
338 | aInfo.Alive().find(aReqNum); | |
339 | if (aFoundReqNum == aInfo.Alive().end()) | |
340 | // freeing non-registered block, skip it | |
341 | continue; | |
342 | aTotalLeftSize -= aSize; | |
343 | aInfo.Alive().erase(aFoundReqNum); | |
344 | if (aInfo.nbAlloc + 1 > 0) | |
345 | aInfo.nbFree++; | |
346 | } | |
347 | } | |
348 | fclose(aLogFile); | |
349 | ||
350 | // print the report | |
64531d9c | 351 | std::ofstream aRepFile (theOutFile); |
352 | if(!aRepFile.is_open()) | |
353 | { | |
7af17f1e | 354 | return Standard_False; |
64531d9c | 355 | } |
356 | aRepFile.imbue (std::locale ("C")); | |
357 | ||
358 | aRepFile << std::setw(20) << "BlockSize " | |
359 | << std::setw(10) << "NbAlloc " | |
360 | << std::setw(10) << "NbLeft " | |
361 | << std::setw(10) << "NbLeftPeak " | |
362 | << std::setw(20) << "AllocSize " | |
363 | << std::setw(20) << "LeftSize " | |
364 | << std::setw(20) << "PeakSize " << std::endl; | |
365 | ||
7af17f1e MA |
366 | Standard_Size aTotAlloc = 0; |
367 | for (std::set<StorageInfo>::const_iterator it = aStMap.begin(); | |
368 | it != aStMap.end(); ++it) | |
369 | { | |
370 | const StorageInfo& aInfo = *it; | |
371 | Standard_Integer nbLeft = aInfo.nbAlloc - aInfo.nbFree; | |
372 | Standard_Size aSizeAlloc = aInfo.nbAlloc * aInfo.size; | |
373 | Standard_Size aSizeLeft = nbLeft * aInfo.size; | |
374 | Standard_Size aSizePeak = aInfo.nbLeftPeak * aInfo.size; | |
64531d9c | 375 | |
376 | aRepFile << std::setw(20) << aInfo.size << ' ' | |
377 | << std::setw(10) << aInfo.nbAlloc << ' ' | |
378 | << std::setw(10) << nbLeft << ' ' | |
379 | << std::setw(10) << aInfo.nbLeftPeak << ' ' | |
380 | << std::setw(20) << aSizeAlloc << ' ' | |
381 | << std::setw(20) << aSizeLeft << ' ' | |
382 | << std::setw(20) << aSizePeak << std::endl; | |
383 | ||
7af17f1e MA |
384 | if (aTotAlloc + aSizeAlloc < aTotAlloc) // overflow ? |
385 | aTotAlloc = SIZE_MAX; | |
386 | else | |
387 | aTotAlloc += aSizeAlloc; | |
388 | if (theIncludeAlive && !aInfo.Alive().empty()) | |
389 | { | |
390 | for (std::set<unsigned long>::const_iterator it1 = aInfo.alive->begin(); | |
391 | it1 != aInfo.alive->end(); ++it1) | |
64531d9c | 392 | aRepFile << std::setw(10) << *it1; |
7af17f1e MA |
393 | } |
394 | } | |
64531d9c | 395 | aRepFile << std::setw(20) << "Total:" |
396 | << std::setw(10) << "" << ' ' | |
397 | << std::setw(10) << "" << ' ' | |
398 | << std::setw(10) << "" << ' ' | |
399 | << (aTotAlloc == SIZE_MAX ? '>' : ' ') | |
400 | << std::setw(20) << aTotAlloc << ' ' | |
401 | << std::setw(20) << aTotalLeftSize << ' ' | |
402 | << std::setw(20) << aTotalPeakSize << std::endl; | |
403 | ||
404 | aRepFile.close(); | |
7af17f1e MA |
405 | return Standard_True; |
406 | } | |
407 | ||
408 | //======================================================================= | |
409 | //function : LogFileHandler::AllocEvent | |
410 | //purpose : | |
411 | //======================================================================= | |
412 | ||
413 | void OSD_MAllocHook::LogFileHandler::AllocEvent | |
414 | (size_t theSize, | |
415 | long theRequestNum) | |
416 | { | |
64531d9c | 417 | if (myLogFile.is_open()) |
7af17f1e MA |
418 | { |
419 | myMutex.Lock(); | |
64531d9c | 420 | myLogFile << "alloc "<< std::setw(10) << theRequestNum |
421 | << std::setw(20) << theSize << std::endl; | |
7af17f1e | 422 | if (myBreakSize == theSize) |
302f96fb | 423 | place_for_breakpoint(); |
424 | myMutex.Unlock(); | |
7af17f1e MA |
425 | } |
426 | } | |
427 | ||
428 | //======================================================================= | |
429 | //function : LogFileHandler::FreeEvent | |
430 | //purpose : | |
431 | //======================================================================= | |
432 | ||
433 | void OSD_MAllocHook::LogFileHandler::FreeEvent | |
434 | (void* /*theData*/, | |
435 | size_t theSize, | |
436 | long theRequestNum) | |
437 | { | |
64531d9c | 438 | if (myLogFile.is_open()) |
7af17f1e MA |
439 | { |
440 | myMutex.Lock(); | |
64531d9c | 441 | myLogFile << "free " << std::setw(20) << theRequestNum |
442 | << std::setw(20) << theSize << std::endl; | |
7af17f1e MA |
443 | myMutex.Unlock(); |
444 | } | |
445 | } | |
446 | ||
447 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
448 | // CollectBySize handler methods | |
449 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
450 | ||
451 | //======================================================================= | |
452 | //function : CollectBySize::CollectBySize | |
453 | //purpose : | |
454 | //======================================================================= | |
455 | ||
456 | OSD_MAllocHook::CollectBySize::CollectBySize() | |
457 | : myArray(NULL), | |
458 | myTotalLeftSize(0), | |
459 | myTotalPeakSize(0), | |
460 | myBreakSize(0) | |
461 | { | |
462 | Reset(); | |
463 | } | |
464 | ||
465 | //======================================================================= | |
466 | //function : CollectBySize::~CollectBySize | |
467 | //purpose : | |
468 | //======================================================================= | |
469 | ||
470 | OSD_MAllocHook::CollectBySize::~CollectBySize() | |
471 | { | |
472 | if (myArray != NULL) | |
473 | delete [] myArray; | |
474 | } | |
475 | ||
476 | //======================================================================= | |
477 | //function : CollectBySize::Reset | |
478 | //purpose : | |
479 | //======================================================================= | |
480 | ||
481 | #define MAX_ALLOC_SIZE 2000000u | |
1cc1abe1 | 482 | const size_t OSD_MAllocHook::CollectBySize::myMaxAllocSize = MAX_ALLOC_SIZE; |
7af17f1e MA |
483 | |
484 | void OSD_MAllocHook::CollectBySize::Reset() | |
485 | { | |
486 | myMutex.Lock(); | |
487 | if (myArray == NULL) | |
488 | myArray = new Numbers[MAX_ALLOC_SIZE]; | |
489 | else | |
490 | { | |
491 | for (int i = 0; i < MAX_ALLOC_SIZE; i++) | |
492 | myArray[i] = Numbers(); | |
493 | } | |
494 | myTotalLeftSize = 0; | |
495 | myTotalPeakSize = 0; | |
496 | myMutex.Unlock(); | |
497 | } | |
498 | ||
499 | //======================================================================= | |
500 | //function : CollectBySize::MakeReport | |
501 | //purpose : | |
502 | //======================================================================= | |
503 | ||
504 | Standard_Boolean OSD_MAllocHook::CollectBySize::MakeReport(const char* theOutFile) | |
505 | { | |
506 | // print the report | |
64531d9c | 507 | std::ofstream aRepFile(theOutFile); |
508 | if (!aRepFile.is_open()) | |
7af17f1e | 509 | return Standard_False; |
64531d9c | 510 | std::locale aCLoc("C"); |
511 | aRepFile.imbue(aCLoc); | |
512 | ||
513 | aRepFile << std::setw(10) << "BlockSize " | |
514 | << std::setw(10) << "NbAlloc " | |
515 | << std::setw(10) << "NbLeft " | |
516 | << std::setw(10) << "NbLeftPeak " | |
517 | << std::setw(20) << "AllocSize " | |
518 | << std::setw(20) << "LeftSize " | |
519 | << std::setw(20) << "PeakSize " << std::endl; | |
520 | ||
7af17f1e MA |
521 | Standard_Size aTotAlloc = 0; |
522 | for (int i = 0; i < MAX_ALLOC_SIZE; i++) | |
523 | { | |
13b4230b | 524 | if (myArray[i].nbAlloc > 0 || myArray[i].nbFree > 0) |
7af17f1e MA |
525 | { |
526 | Standard_Integer nbLeft = myArray[i].nbAlloc - myArray[i].nbFree; | |
7af17f1e MA |
527 | int aSize = i + 1; |
528 | Standard_Size aSizeAlloc = myArray[i].nbAlloc * aSize; | |
13b4230b | 529 | ptrdiff_t aSizeLeft = nbLeft * aSize; |
7af17f1e | 530 | Standard_Size aSizePeak = myArray[i].nbLeftPeak * aSize; |
64531d9c | 531 | |
532 | aRepFile << std::setw(10) << aSize << ' ' | |
533 | << std::setw(10) << myArray[i].nbAlloc << ' ' | |
534 | << std::setw(10) << nbLeft << ' ' | |
535 | << std::setw(10) << myArray[i].nbLeftPeak << ' ' | |
536 | << std::setw(20) << aSizeAlloc << ' ' | |
537 | << std::setw(20) << aSizeLeft << ' ' | |
538 | << std::setw(20) << aSizePeak << std::endl; | |
539 | ||
7af17f1e MA |
540 | if (aTotAlloc + aSizeAlloc < aTotAlloc) // overflow ? |
541 | aTotAlloc = SIZE_MAX; | |
542 | else | |
543 | aTotAlloc += aSizeAlloc; | |
544 | } | |
545 | } | |
64531d9c | 546 | aRepFile << std::setw(10) << "Total:" << ' ' |
547 | << std::setw(10) << "" << ' ' | |
548 | << std::setw(10) << "" << ' ' | |
549 | << std::setw(10) << "" << ' ' | |
550 | << (aTotAlloc == SIZE_MAX ? '>' : ' ') | |
551 | << std::setw(20) << aTotAlloc << ' ' | |
552 | << std::setw(20) << myTotalLeftSize << ' ' | |
553 | << std::setw(20) << myTotalPeakSize << std::endl; | |
554 | aRepFile.close(); | |
7af17f1e MA |
555 | return Standard_True; |
556 | } | |
557 | ||
558 | //======================================================================= | |
559 | //function : CollectBySize::AllocEvent | |
560 | //purpose : | |
561 | //======================================================================= | |
562 | ||
563 | void OSD_MAllocHook::CollectBySize::AllocEvent | |
564 | (size_t theSize, | |
565 | long /*theRequestNum*/) | |
566 | { | |
567 | if (myBreakSize == theSize) | |
302f96fb | 568 | place_for_breakpoint(); |
7af17f1e MA |
569 | if (theSize > 0) |
570 | { | |
571 | myMutex.Lock(); | |
572 | int ind = (theSize > MAX_ALLOC_SIZE ? MAX_ALLOC_SIZE-1 : (int)(theSize-1)); | |
13b4230b | 573 | myArray[ind].nbAlloc++; |
7af17f1e MA |
574 | myTotalLeftSize += theSize; |
575 | int nbLeft = myArray[ind].nbAlloc - myArray[ind].nbFree; | |
576 | if (nbLeft > myArray[ind].nbLeftPeak) | |
577 | myArray[ind].nbLeftPeak = nbLeft; | |
13b4230b | 578 | if (myTotalLeftSize > (ptrdiff_t)myTotalPeakSize) |
7af17f1e MA |
579 | myTotalPeakSize = myTotalLeftSize; |
580 | myMutex.Unlock(); | |
581 | } | |
582 | } | |
583 | ||
584 | //======================================================================= | |
585 | //function : CollectBySize::FreeEvent | |
586 | //purpose : | |
587 | //======================================================================= | |
588 | ||
589 | void OSD_MAllocHook::CollectBySize::FreeEvent | |
590 | (void* /*theData*/, | |
591 | size_t theSize, | |
592 | long /*theRequestNum*/) | |
593 | { | |
13b4230b | 594 | if (theSize > 0) |
7af17f1e MA |
595 | { |
596 | myMutex.Lock(); | |
597 | int ind = (theSize > MAX_ALLOC_SIZE ? MAX_ALLOC_SIZE-1 : (int)(theSize-1)); | |
13b4230b | 598 | myArray[ind].nbFree++; |
7af17f1e MA |
599 | myTotalLeftSize -= theSize; |
600 | myMutex.Unlock(); | |
601 | } | |
602 | } |