Compare commits

...
Sign in to create a new pull request.

5 commits

Author SHA1 Message Date
Steffen Vogel
ae1af7a053 fixed segmention fault
abort allocation after first malloc fail
2013-10-07 17:22:53 +02:00
Steffen Vogel
b3fa94b0e0 free chunks before terminate 2013-08-19 00:44:24 +02:00
Steffen Vogel
e766295d68 sequential allocation of multiple chunks 2013-08-19 00:43:34 +02:00
Steffen Vogel
264146a7e1 bugfix: invalid multp for single byte allocation 2013-08-19 00:37:05 +02:00
Steffen Vogel
030ba0d75f code cleanup 2013-08-19 00:35:30 +02:00

View file

@ -20,41 +20,64 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
int print_usage() {
printf("usage: [size mb/kb/b]");
printf("usage: size mb/kb/b [chunks]\n");
exit(0);
}
int main(int argc, char** argv)
{
int m = 0;
uint32_t size = 0;
if(argc <= 2)
print_usage();
if(argc == 3) {
if(!strcmp(argv[2], "mb"))
m = 1024*1024;
else if(!strcmp(argv[2], "kb"))
m = 1024;
else if(!strcmp(argv[2], "b"))
m = 0;
else
print_usage();
}
if(argc > 3)
print_usage();
size = atoi(argv[1]);
if(size <= 0)
int multp = 0;
int size = 0;
int chunks = 1;
void **test;
if (argc <= 2 || argc > 4)
print_usage();
size *= m;
uint8_t* test = malloc(size);
printf("malloc(%d) - START: %p END: %p \n", size, test, test + size);
size = atoi(argv[1]);
if (size <= 0)
print_usage();
if (!strcasecmp(argv[2], "mb"))
multp = 1024*1024;
else if (!strcasecmp(argv[2], "kb"))
multp = 1024;
else if (!strcasecmp(argv[2], "b"))
multp = 1;
else
print_usage();
size *= multp;
if (argc == 4)
chunks = atoi(argv[3]);
test = malloc(chunks * sizeof(void *));
if (!test)
printf("malloc(%d) - FAILED!\n", chunks * sizeof(void *));
// allocate...
int i;
for (i = 0; i < chunks; i++) {
test[i] = malloc(size);
if (test[i])
printf("malloc(%d)\tCHUNK: %d START: %p END: %p\n", size, i, test[i], test[i] + size);
else {
printf("malloc(%d)\tFAILED! Abort allocation, start with freeing memory\n", size);
break;
}
}
// and release again
for (i = 0; i < chunks; i++) {
if (test[i]) {
free(test[i]);
printf("free(%p)\tCHUNK: %d\n", test[i], i);
}
}
free(test);
return 0;
}