-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathALLOCS.C
82 lines (67 loc) · 1.67 KB
/
ALLOCS.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#define ALLOCS_IMPL
unsigned long malloc_count = 0;
unsigned long calloc_count = 0;
unsigned long realloc_count = 0;
unsigned long alloc_bytes = 0;
unsigned long alloc_max_bytes = 0;
unsigned long free_count = 0;
static void update_stats( size_t size )
{
alloc_bytes += size;
if ( alloc_bytes > alloc_max_bytes ) {
alloc_max_bytes = alloc_bytes;
}
}
void *calloc_stat( size_t n, size_t size )
{
void *res = calloc( n, size );
calloc_count++;
if ( !res ) {
return res;
}
update_stats( _msize( res ) );
return res;
}
void *malloc_stat( size_t size )
{
void *res = malloc( size );
malloc_count++;
if ( !res ) {
return res;
}
update_stats( _msize( res ) );
return res;
}
void *realloc_stat( void *old_blk, size_t size )
{
size_t old_size;
void *res;
old_size = old_blk ? _msize( old_blk ) : 0;
realloc_count++;
alloc_bytes -= old_size;
res = realloc( old_blk, size );
if ( !res ) {
return res;
}
update_stats( _msize( res ) );
return res;
}
void free_stat( void *p )
{
free_count++;
alloc_bytes -= _msize( p );
free( p );
}
void print_mem_stat()
{
printf( "--- MALLOC STATISTICS -----------\n" );
printf( "malloc calls : %lu\n", malloc_count );
printf( "calloc calls : %lu\n", calloc_count );
printf( "realloc calls : %lu\n", realloc_count );
printf( "free calls : %lu\n", free_count );
printf( "allocated mem now: %lu bytes\n", alloc_bytes );
printf( "max allocated mem: %lu bytes\n", alloc_max_bytes );
}