Pol  Revision:cb584c9
opnew.cpp
Go to the documentation of this file.
1 
14 #include "logfacility.h"
15 #include "opnew.h"
16 #include "pol_global_config.h"
17 
18 #ifdef _WIN32
19 #include <malloc.h>
20 #endif
21 
22 #ifdef MEMORYLEAK
23 #include <time.h>
24 
25 #define MEMORYLOGBLOCKS 0
26 #endif
27 
28 namespace Pol
29 {
30 namespace Clib
31 {
32 static unsigned int bytes_allocated = 0;
33 static unsigned int last_bytes_allocated = 0;
34 static unsigned int blocks_allocated = 0;
35 static unsigned int last_blocks_allocated = 0;
36 
37 #ifdef MEMORYLEAK
38 static unsigned int requested_8 = 0;
39 static unsigned int requested_16 = 0;
40 static unsigned int requested_32 = 0;
41 static unsigned int requested_64 = 0;
42 static unsigned int requested_128 = 0;
43 static unsigned int requested_512 = 0;
44 static unsigned int requested_1024 = 0;
45 static unsigned int requested_2048 = 0;
46 static unsigned int requested_4096 = 0;
47 static unsigned int requested_8192 = 0;
48 static unsigned int requested_16384 = 0;
49 static unsigned int requested_65536 = 0;
50 static unsigned int requested_131072 = 0;
51 static unsigned int requested_524288 = 0;
52 static unsigned int requested_1048576 = 0;
53 static unsigned int requested_16777216 = 0;
54 static unsigned int requested_huge = 0;
55 
56 static unsigned int used_8 = 0;
57 static unsigned int used_16 = 0;
58 static unsigned int used_32 = 0;
59 static unsigned int used_64 = 0;
60 static unsigned int used_128 = 0;
61 static unsigned int used_512 = 0;
62 static unsigned int used_1024 = 0;
63 static unsigned int used_2048 = 0;
64 static unsigned int used_4096 = 0;
65 static unsigned int used_8192 = 0;
66 static unsigned int used_16384 = 0;
67 static unsigned int used_65536 = 0;
68 static unsigned int used_131072 = 0;
69 static unsigned int used_524288 = 0;
70 static unsigned int used_1048576 = 0;
71 static unsigned int used_16777216 = 0;
72 static unsigned int used_huge = 0;
73 
74 static unsigned int allocated_8 = 0;
75 static unsigned int allocated_16 = 0;
76 static unsigned int allocated_32 = 0;
77 static unsigned int allocated_64 = 0;
78 static unsigned int allocated_128 = 0;
79 static unsigned int allocated_512 = 0;
80 static unsigned int allocated_1024 = 0;
81 static unsigned int allocated_2048 = 0;
82 static unsigned int allocated_4096 = 0;
83 static unsigned int allocated_8192 = 0;
84 static unsigned int allocated_16384 = 0;
85 static unsigned int allocated_65536 = 0;
86 static unsigned int allocated_131072 = 0;
87 static unsigned int allocated_524288 = 0;
88 static unsigned int allocated_1048576 = 0;
89 static unsigned int allocated_16777216 = 0;
90 static unsigned int allocated_huge = 0;
91 #endif
92 
93 #if 0
94 #ifndef NDEBUG
95 #ifdef _WIN32
96  static unsigned int n_allocs;
97  static unsigned int n_frees;
98  void* operator new( size_t len )
99  {
100  void *vp = malloc( len );
101  if (vp)
102  {
103 #ifdef _WIN32
104  bytes_allocated += _msize( vp );
105 #elif __linux__
106  bytes_allocated += malloc_usable_size( vp );
107 #endif
109  return vp;
110  }
111  else
112  {
113  throw std::runtime_error( "Out of memory" );
114  }
115  }
116 
117  void operator delete( void *ptr )
118  {
119  if (ptr)
120  {
121 #ifdef _WIN32
122  bytes_allocated -= _msize( ptr );
123 #elif __linux__
124  bytes_allocated -= malloc_usable_size( ptr );
125 #endif
127  free( ptr );
128  }
129  }
130 #endif
131 #endif
132 #endif
133 
134 #ifdef MEMORYLEAK
135 void* do_new( size_t len )
136 {
137  void* vp = malloc( len );
138  if ( vp )
139  {
140 #ifdef _WIN32
141  len = _msize( vp );
142 #elif __linux__
143  len = malloc_usable_size( vp );
144 #else
145  len = 0;
146 #endif
147 
148  if ( len < 8 )
149  {
150  requested_8++;
151  used_8++;
152  allocated_8 += len;
153  }
154  else if ( len < 16 )
155  {
156  requested_16++;
157  used_16++;
158  allocated_16 += len;
159  }
160  else if ( len < 32 )
161  {
162  requested_32++;
163  used_32++;
164  allocated_32 += len;
165  }
166  else if ( len < 64 )
167  {
168  requested_64++;
169  used_64++;
170  allocated_64 += len;
171  }
172  else if ( len < 128 )
173  {
174  requested_128++;
175  used_128++;
176  allocated_128 += len;
177  }
178  else if ( len < 512 )
179  {
180  requested_512++;
181  used_512++;
182  allocated_512 += len;
183  }
184  else if ( len < 1024 )
185  {
186  requested_1024++;
187  used_1024++;
188  allocated_1024 += len;
189  }
190  else if ( len < 2048 )
191  {
192  requested_2048++;
193  used_2048++;
194  allocated_2048 += len;
195  }
196  else if ( len < 4096 )
197  {
198  requested_4096++;
199  used_4096++;
200  allocated_4096 += len;
201  }
202  else if ( len < 8192 )
203  {
204  requested_8192++;
205  used_8192++;
206  allocated_8192 += len;
207  }
208  else if ( len < 16384 )
209  {
210  requested_16384++;
211  used_16384++;
212  allocated_16384 += len;
213  }
214  else if ( len < 65536 )
215  {
216  requested_65536++;
217  used_65536++;
218  allocated_65536 += len;
219 
220 #if MEMORYLOGBLOCKS == 1
221 #ifdef _WIN32
222  LEAKLOG.Format( "New 64 KB {:x} {}\n" ) << vp << len;
223 #else
224  LEAKLOG.Format( "New 64 KB {:x} {} {} {} {} {} {} {} {} {}\n" )
225  << vp << len << __builtin_return_address( 0 ) << __builtin_return_address( 1 )
226  << __builtin_return_address( 2 ) << __builtin_return_address( 3 )
227  << __builtin_return_address( 4 ) << __builtin_return_address( 5 )
228  << __builtin_return_address( 6 ) << __builtin_return_address( 7 );
229 #endif
230 #endif
231  }
232  else if ( len < 131072 )
233  {
234  requested_131072++;
235  used_131072++;
236  allocated_131072 += len;
237  }
238  else if ( len < 524288 )
239  {
240  requested_524288++;
241  used_524288++;
242  allocated_524288 += len;
243 
244 #if MEMORYLOGBLOCKS == 1
245 #ifdef _WIN32
246  LEAKLOG.Format( "New 0.5 MB {:x} {}\n" ) << vp << len;
247 #else
248  LEAKLOG.Format( "New 0.5 M {:x} {} {} {} {} {} {} {} {} {}\n" )
249  << vp << len << __builtin_return_address( 0 ) << __builtin_return_address( 1 )
250  << __builtin_return_address( 2 ) << __builtin_return_address( 3 )
251  << __builtin_return_address( 4 ) << __builtin_return_address( 5 )
252  << __builtin_return_address( 6 ) << __builtin_return_address( 7 );
253 #endif
254 #endif
255  }
256  else if ( len < 1048576 )
257  {
258  requested_1048576++;
259  used_1048576++;
260  allocated_1048576 += len;
261  }
262  else if ( len < 16777216 )
263  {
264  requested_16777216++;
265  used_16777216++;
266  allocated_16777216 += len;
267  }
268  else
269  {
270  requested_huge++;
271  used_huge++;
272  allocated_huge += len;
273 
274 #if MEMORYLOGBLOCKS == 1
275 #ifdef _WIN32
276  LEAKLOG.Format( "New huge {:x} {}\n" ) << vp << len;
277 #else
278  LEAKLOG.Format( "New huge {:x} {} {} {} {} {} {} {} {} {}\n" )
279  << vp << len << __builtin_return_address( 0 ) << __builtin_return_address( 1 )
280  << __builtin_return_address( 2 ) << __builtin_return_address( 3 )
281  << __builtin_return_address( 4 ) << __builtin_return_address( 5 )
282  << __builtin_return_address( 6 ) << __builtin_return_address( 7 );
283 #endif
284 #endif
285  }
286 
287  bytes_allocated += len;
289  return vp;
290  }
291  else
292  {
293  throw std::runtime_error( "Out of memory" );
294  }
295 }
296 
297 
298 void do_delete( void* ptr )
299 {
300  if ( ptr )
301  {
302 #ifdef _WIN32
303  size_t len = _msize( ptr );
304 #elif __linux__
305  size_t len = malloc_usable_size( ptr );
306 #else
307  size_t len = 0;
308 #endif
309 
310  if ( len < 8 )
311  {
312  used_8--;
313  allocated_8 -= len;
314  }
315  else if ( len < 16 )
316  {
317  used_16--;
318  allocated_16 -= len;
319  }
320  else if ( len < 32 )
321  {
322  used_32--;
323  allocated_32 -= len;
324  }
325  else if ( len < 64 )
326  {
327  used_64--;
328  allocated_64 -= len;
329  }
330  else if ( len < 128 )
331  {
332  used_128--;
333  allocated_128 -= len;
334  }
335  else if ( len < 512 )
336  {
337  used_512--;
338  allocated_512 -= len;
339  }
340  else if ( len < 1024 )
341  {
342  used_1024--;
343  allocated_1024 -= len;
344  }
345  else if ( len < 2048 )
346  {
347  used_2048--;
348  allocated_2048 -= len;
349  }
350  else if ( len < 4096 )
351  {
352  used_4096--;
353  allocated_4096 -= len;
354  }
355  else if ( len < 8192 )
356  {
357  used_8192--;
358  allocated_8192 -= len;
359  }
360  else if ( len < 16384 )
361  {
362  used_16384--;
363  allocated_16384 -= len;
364  }
365  else if ( len < 65536 )
366  {
367  used_65536--;
368  allocated_65536 -= len;
369 
370 #if MEMORYLOGBLOCKS == 1
371 #ifdef _WIN32
372  LEAKLOG.Format( "Delete 64 KB {:x} {}\n" ) << ptr << len;
373 #else
374  LEAKLOG.Format( "Delete 64 KB {:x} {} {} {} {} {} {} {} {} {}\n" )
375  << ptr << len << __builtin_return_address( 0 ) << __builtin_return_address( 1 )
376  << __builtin_return_address( 2 ) << __builtin_return_address( 3 )
377  << __builtin_return_address( 4 ) << __builtin_return_address( 5 )
378  << __builtin_return_address( 6 ) << __builtin_return_address( 7 );
379 #endif
380 #endif
381  }
382  else if ( len < 131072 )
383  {
384  used_131072--;
385  allocated_131072 -= len;
386  }
387  else if ( len < 524288 )
388  {
389  used_524288--;
390  allocated_524288 -= len;
391 
392 #if MEMORYLOGBLOCKS == 1
393 #ifdef _WIN32
394  LEAKLOG.Format( "Delete 0.5 MB {:x} {}\n" ) << ptr << len;
395 #else
396  LEAKLOG.Format( "Delete 0.5 MB {:x} {} {} {} {} {} {} {} {} {}\n" )
397  << ptr << len << __builtin_return_address( 0 ) << __builtin_return_address( 1 )
398  << __builtin_return_address( 2 ) << __builtin_return_address( 3 )
399  << __builtin_return_address( 4 ) << __builtin_return_address( 5 )
400  << __builtin_return_address( 6 ) << __builtin_return_address( 7 );
401 #endif
402 #endif
403  }
404  else if ( len < 1048576 )
405  {
406  used_1048576--;
407  allocated_1048576 -= len;
408  }
409  else if ( len < 16777216 )
410  {
411  used_16777216--;
412  allocated_16777216 -= len;
413  }
414  else
415  {
416  used_huge--;
417  allocated_huge -= len;
418 
419 #if MEMORYLOGBLOCKS == 1
420 #ifdef _WIN32
421  LEAKLOG.Format( "Delete huge {:x} {}\n" ) << ptr << len;
422 #else
423  LEAKLOG.Format( "Delete huge {:x} {} {} {} {} {} {} {} {} {}\n" )
424  << ptr << len << __builtin_return_address( 0 ) << __builtin_return_address( 1 )
425  << __builtin_return_address( 2 ) << __builtin_return_address( 3 )
426  << __builtin_return_address( 4 ) << __builtin_return_address( 5 )
427  << __builtin_return_address( 6 ) << __builtin_return_address( 7 );
428 #endif
429 #endif
430  }
431 
432  bytes_allocated -= len;
434  free( ptr );
435  }
436 }
437 
438 #endif
439 
441 {
442  int delta_bytes = bytes_allocated - last_bytes_allocated;
443  int delta_blocks = blocks_allocated - last_blocks_allocated;
444  last_bytes_allocated = bytes_allocated;
445  last_blocks_allocated = blocks_allocated;
446 
447 #ifndef MEMORYLEAK
448  INFO_PRINT << "OpNewHeap: allocated " << blocks_allocated << " blocks, " << bytes_allocated
449  << " bytes\n"
450  << "OpNewHeap: delta " << delta_blocks << " blocks, " << delta_bytes << " bytes\n";
451  ;
452 #else
453  DEBUGLOG << "Heap (whole): " << blocks_allocated << " blocks with " << bytes_allocated
454  << " Bytes\n"
455  << "Heap (delta): " << delta_blocks << " blocks with " << delta_bytes << " Bytes\n"
456  << "Heap (blocks): " << used_8 << " of " << requested_8 << " blocks of 8 Byte ("
457  << allocated_8 << ")\n"
458  << "Heap (blocks): " << used_16 << " of " << requested_16 << " blocks of 16 Byte ("
459  << allocated_16 << ")\n"
460  << "Heap (blocks): " << used_32 << " of " << requested_32 << " blocks of 32 Byte ("
461  << allocated_32 << ")\n"
462  << "Heap (blocks): " << used_64 << " of " << requested_64 << " blocks of 64 Byte ("
463  << allocated_64 << ")\n"
464  << "Heap (blocks): " << used_128 << " of " << requested_128 << " blocks of 128 Byte ("
465  << allocated_128 << ")\n"
466  << "Heap (blocks): " << used_512 << " of " << requested_512 << " blocks of 512 Byte ("
467  << allocated_512 << ")\n"
468  << "Heap (blocks): " << used_1024 << " of " << requested_1024 << " blocks of 1 KByte ("
469  << allocated_1024 << ")\n"
470  << "Heap (blocks): " << used_2048 << " of " << requested_2048 << " blocks of 2 KByte ("
471  << allocated_2048 << ")\n"
472  << "Heap (blocks): " << used_4096 << " of " << requested_4096 << " blocks of 4 KByte ("
473  << allocated_4096 << ")\n"
474  << "Heap (blocks): " << used_8192 << " of " << requested_8192 << " blocks of 8 KByte ("
475  << allocated_8192 << ")\n"
476  << "Heap (blocks): " << used_16384 << " of " << requested_16384
477  << " blocks of 16 KByte (" << allocated_16384 << ")\n"
478  << "Heap (blocks): " << used_65536 << " of " << requested_65536
479  << " blocks of 64 KByte (" << allocated_65536 << ")\n"
480  << "Heap (blocks): " << used_131072 << " of " << requested_131072
481  << " blocks of 128 KByte (" << allocated_131072 << ")\n"
482  << "Heap (blocks): " << used_524288 << " of " << requested_524288
483  << " blocks of 0.5 MByte (" << allocated_524288 << ")\n"
484  << "Heap (blocks): " << used_1048576 << " of " << requested_1048576
485  << " blocks of 1 MByte (" << allocated_1048576 << ")\n"
486  << "Heap (blocks): " << used_16777216 << " of " << requested_16777216
487  << " blocks of 16 MByte (" << allocated_16777216 << ")\n"
488  << "Heap (blocks): " << used_huge << " of " << requested_huge << " huge blocks ("
489  << allocated_huge << ")\n";
490 
491 
492  LEAKLOG << blocks_allocated << "; " << bytes_allocated << "; " << delta_blocks << ";"
493  << delta_bytes << ";" << used_8 << ";" << requested_8 << ";" << allocated_8 << ";"
494  << used_16 << ";" << requested_16 << ";" << allocated_16 << ";" << used_32 << ";"
495  << requested_32 << ";" << allocated_32 << ";" << used_64 << ";" << requested_64 << ";"
496  << allocated_64 << ";" << used_128 << ";" << requested_128 << ";" << allocated_128 << ";"
497  << used_512 << ";" << requested_512 << ";" << allocated_512 << ";" << used_1024 << ";"
498  << requested_1024 << ";" << allocated_1024 << ";" << used_2048 << ";" << requested_2048
499  << ";" << allocated_2048 << ";" << used_4096 << ";" << requested_4096 << ";"
500  << allocated_4096 << ";" << used_8192 << ";" << requested_8192 << ";" << allocated_8192
501  << ";" << used_16384 << ";" << requested_16384 << ";" << allocated_16384 << ";"
502  << used_65536 << ";" << requested_65536 << ";" << allocated_65536 << ";" << used_131072
503  << ";" << requested_131072 << ";" << allocated_131072 << ";" << used_524288 << ";"
504  << requested_524288 << ";" << allocated_524288 << ";" << used_1048576 << ";"
505  << requested_1048576 << ";" << allocated_1048576 << ";" << used_16777216 << ";"
506  << requested_16777216 << ";" << allocated_16777216 << ";" << used_huge << ";"
507  << requested_huge << ";" << allocated_huge << "\n";
508 #endif
509 }
510 }
511 }
512 
513 #ifdef MEMORYLEAK
514 void* operator new( size_t len )
515 {
516  return Pol::Clib::do_new( len );
517 }
518 
519 void* operator new[]( size_t len )
520 {
521  return Pol::Clib::do_new( len );
522 }
523 
524 void operator delete( void* ptr )
525 {
526  Pol::Clib::do_delete( ptr );
527 }
528 
529 void operator delete[]( void* ptr )
530 {
531  Pol::Clib::do_delete( ptr );
532 }
533 #endif
static unsigned int blocks_allocated
Definition: opnew.cpp:34
static unsigned int last_blocks_allocated
Definition: opnew.cpp:35
void PrintHeapData()
Definition: opnew.cpp:440
#define LEAKLOG
Definition: logfacility.h:241
#define DEBUGLOG
Definition: logfacility.h:237
#define INFO_PRINT
Definition: logfacility.h:223
Definition: berror.cpp:12
static unsigned int last_bytes_allocated
Definition: opnew.cpp:33
static unsigned int bytes_allocated
Definition: opnew.cpp:32