metalsvm/newlib/examples/memtest.c

107 lines
2.2 KiB
C
Raw Permalink Normal View History

2013-01-30 16:29:59 +01:00
/*
* 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.
*/
2014-05-14 17:43:51 +02:00
/**
* @author Steffen Vogel <steffen.vogel@rwth-aachen.de>
*/
2013-01-30 16:29:59 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2014-01-09 14:08:33 +01:00
#include <time.h>
#include <sys/times.h>
void sleep(int sec) {
struct tms tms;
clock_t t, s = times(&tms);
do {
t = times(&tms);
}
while (t - s <= 1000 * sec);
}
2013-01-30 16:29:59 +01:00
int print_usage() {
printf("usage: size mb/kb/b [chunks]\n");
2014-01-09 14:08:33 +01:00
exit(-1);
}
2013-01-30 16:29:59 +01:00
int main(int argc, char** argv)
{
2013-08-19 00:35:30 +02:00
int multp = 0;
int size = 0;
int chunks = 1;
2013-08-19 00:35:30 +02:00
void **test;
2013-08-19 00:35:30 +02:00
if (argc <= 2 || argc > 4)
print_usage();
2013-08-19 00:35:30 +02:00
size = atoi(argv[1]);
2013-08-19 00:35:30 +02:00
if (size <= 0)
print_usage();
if (!strcasecmp(argv[2], "mb"))
2014-01-09 14:08:33 +01:00
multp = (1 << 20);
else if (!strcasecmp(argv[2], "kb"))
2014-01-09 14:08:33 +01:00
multp = (1 << 10);
else if (!strcasecmp(argv[2], "b"))
2014-01-09 14:08:33 +01:00
multp = (1 << 0);
else
print_usage();
2013-08-19 00:35:30 +02:00
size *= multp;
if (argc == 4)
chunks = atoi(argv[3]);
test = malloc(chunks * sizeof(void *));
2014-01-09 14:08:33 +01:00
printf("malloc(%lu)\n", chunks * sizeof(void *));
if (!test) {
printf("malloc(%lu) - FAILED!\n", chunks * sizeof(void *));
exit(-1);
}
// 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);
2014-01-09 14:08:33 +01:00
else
printf("malloc(%d)\tFAILED! Abort allocation, start with freeing memory\n", size);
2014-01-09 14:08:33 +01:00
sleep(1);
}
2013-08-19 00:35:30 +02:00
2013-08-19 00:44:24 +02:00
// and release again
for (i = 0; i < chunks; i++) {
if (test[i]) {
free(test[i]);
printf("free(%p)\tCHUNK: %d\n", test[i], i);
}
2014-01-09 14:08:33 +01:00
sleep(1);
2013-08-19 00:44:24 +02:00
}
free(test);
2014-01-09 14:08:33 +01:00
printf("free(%p)\n", test);
return 0;
2013-01-30 16:29:59 +01:00
}