-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmallocWatch.cpp
More file actions
139 lines (120 loc) · 3.72 KB
/
mallocWatch.cpp
File metadata and controls
139 lines (120 loc) · 3.72 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
#include "mallocWatch.hpp"
#if defined(__x86_64__) && defined(__APPLE__)
#include <mach/mach.h>
#include <mach/vm_map.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <malloc/malloc.h>
#include <stdarg.h>
extern void _simple_vdprintf(int, const char *, va_list);
inline void nomalloc_printf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
_simple_vdprintf(STDOUT_FILENO, format, ap);
va_end(ap);
}
void *(*system_malloc)(malloc_zone_t *zone, size_t size);
void *(*system_valloc)(malloc_zone_t *zone, size_t size);
void *(*system_calloc)(malloc_zone_t *zone, size_t size,size_t cnt);
void *(*system_realloc)(malloc_zone_t* zone,void* ptr,size_t size);
void (*system_free)(malloc_zone_t *zone, void *ptr);
void (*system_free_definite_size)(malloc_zone_t * zone, void *ptr , size_t sz);
static long nbBytes = 0;
static long peakBytes = 0;
void * my_malloc(malloc_zone_t *zone, size_t size)
{
void *ptr = system_malloc(zone, size);
size_t als = malloc_size(ptr);
nbBytes += als;
peakBytes = nbBytes > peakBytes ? nbBytes : peakBytes;
return ptr;
}
void * my_valloc(malloc_zone_t *zone, size_t size)
{
void *ptr = system_valloc(zone, size);
size_t als = malloc_size(ptr);
nbBytes += als;
peakBytes = nbBytes > peakBytes ? nbBytes : peakBytes;
return ptr;
}
void *my_realloc(malloc_zone_t* zone,void* ptr,size_t size)
{
size_t oldsz = malloc_size(ptr);
void* nPtr = system_realloc(zone,ptr,size);
size_t newsz = malloc_size(nPtr);
nbBytes += newsz - oldsz;
return nPtr;
}
void * my_calloc(malloc_zone_t *zone, size_t cnt,size_t size)
{
void* ptr = system_calloc(zone,cnt,size);
size_t als = malloc_size(ptr);
nbBytes += als;
peakBytes = nbBytes > peakBytes ? nbBytes : peakBytes;
return ptr;
}
void my_free(malloc_zone_t *zone, void *ptr)
{
size_t toFree = malloc_size(ptr);
nbBytes -= toFree;
system_free(zone, ptr);
}
void my_free_definite_size(malloc_zone_t * zone, void *ptr , size_t sz)
{
nbBytes -= sz;
system_free_definite_size(zone, ptr,sz);
}
void mallocWatch()
{
size_t protect_size = sizeof(malloc_zone_t);
malloc_zone_t *zone = malloc_default_zone();
if (zone->malloc == my_malloc) return ;
system_malloc = zone->malloc;
system_valloc = zone->valloc;
system_calloc = zone->calloc;
system_realloc = zone->realloc;
system_free = zone->free; // ignoring atomicity/caching
system_free_definite_size = zone->free_definite_size;
if(zone->version >= 8) {
vm_protect(mach_task_self(), (uintptr_t)zone, protect_size, 0, VM_PROT_READ | VM_PROT_WRITE);//remove the write protection
}
zone->malloc = my_malloc;
zone->valloc = my_valloc;
zone->calloc = my_calloc;
zone->realloc = my_realloc;
zone->free = my_free;
zone->free_definite_size = my_free_definite_size;
if(zone->version==8) {
vm_protect(mach_task_self(), (uintptr_t)zone, protect_size, 0, VM_PROT_READ);//put the write protection back
}
}
void mallocReport()
{
printf("malloc usage %ld, %ld\n",nbBytes,peakBytes);
}
#else
#include <stdlib.h>
#include <stdio.h>
void mallocWatch()
{}
void mallocReport()
{
printf("iOS no report\n");
}
#endif