metalsvm/newlib/examples/memtest.c
Steffen Vogel 16c65de934 fixed segmention fault
abort allocation after first malloc fail
2013-10-07 17:22:53 +02:00

83 lines
1.8 KiB
C

/*
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
* RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of MetalSVM.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int print_usage() {
printf("usage: size mb/kb/b [chunks]\n");
exit(0);
}
int main(int argc, char** argv)
{
int multp = 0;
int size = 0;
int chunks = 1;
void **test;
if (argc <= 2 || argc > 4)
print_usage();
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;
}