-
Notifications
You must be signed in to change notification settings - Fork 30
/
memleak.c
156 lines (127 loc) · 3.67 KB
/
memleak.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2010,2011 ulatencyd developers
This file is part of ulatencyd.
ulatencyd is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
ulatencyd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ulatencyd. If not, see http://www.gnu.org/licenses/.
*/
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
int
main (argc, argv)
int argc;
char **argv;
{
int c = 0;
int j, i = 0;
int stress = 0;
int chunk = 1024;
int nums = 10;
int delay = 50000;
char *tmp;
while (1)
{
int option_index = 0;
static struct option long_options[] =
{
{"chunk", 1, 0, 'c'},
{"nums", 1, 0, 'n'},
{"delay", 1, 0, 'd'},
{"stress", 0, 0, 's'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "n:c:hd:",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'h':
printf ("memleak:\n");
printf ("-c size size of chunk in kb\n");
printf ("-n num number of memory chunks\n");
printf ("-d delay delay in usecs between alloc \n");
printf ("-s stress test allocated memory \n");
exit(0);
break;
case 'b':
printf ("option b\n");
break;
case 'c':
//printf ("option c with value '%s'\n", optarg);
chunk = atoi(optarg);
break;
case 'n':
nums = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 's':
stress = 1;
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
printf(
"!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!\n"
"!!!!! THIS PROGRAM WILL LIKELY KILL YOUR COMPUTER !!!!!\n"
"!!!!! ulatency may rescue you ;-) !!!!!\n"
"!!!!! press ctrl+c to stop !!!!!\n"
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
);
sleep(3);
void **ptr = malloc(sizeof(void *) * nums);
void *ttmp;
char cmp[10] = "iutare29i3";
printf ("malloc %d chunks of %d kb (delay %d us):\n", nums, chunk, delay);
while(1) {
if(i < nums) {
tmp = malloc(chunk*1024);
ptr[i] = memset(tmp, 0, chunk*1024);
printf(".");
fflush(stdout);
i++;
}
if(stress) {
for(j = 0; i/3 < j; j++) {
ttmp = ptr[rand()%i];
if(rand()%6 == 1)
memset(ttmp + (rand()%chunk) - sizeof(int) , rand(), sizeof(int));
else
memcmp(ttmp + (rand()%chunk) - MIN(10, chunk), cmp, MIN(10, chunk));
}
}
usleep(delay);
}
exit (0);
}