From e1e6511cd68b646234dcc8f5f86b25b7ea48a720 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Thu, 28 Mar 2019 15:17:34 +0106 Subject: schulung_tools: rename/modify malloc to avoid system() A more complex but cleaner implementation of retrieving/printing the stats has been implemented. The implementation is put into a separate stats.c file so that the main program (malloc.) can be easily reviewed to see what the program is doing. Signed-off-by: John Ogness --- schulung_tools/malloc/malloc.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) (limited to 'schulung_tools/malloc/malloc.c') diff --git a/schulung_tools/malloc/malloc.c b/schulung_tools/malloc/malloc.c index a636426..76defbe 100644 --- a/schulung_tools/malloc/malloc.c +++ b/schulung_tools/malloc/malloc.c @@ -1,57 +1,45 @@ #include #include -#include #include -#define NUM_OF_GBYTES 8 - -/* if verbose is enabled forks are done for printing additional informations */ -#define VERBOSE 0 - -#if VERBOSE -/* forking is not always possible if memory is low */ -#define sys(cmd) if(system(cmd) == -1) printf("%s failed\n", cmd); -#else -#define sys(cmd) ; -#endif +extern void print_stats(int); -#define MEMFREE "free -h" +#define NUM_OF_GBYTES 8 int main(void) { char *p[NUM_OF_GBYTES]; int i; - printf("memory usage:\n"); - sys(MEMFREE); - printf("\n\n"); - for (i = 0; i < NUM_OF_GBYTES; i++) { + printf("allocating 1GB\n"); + print_stats(0); p[i] = malloc(1024 * 1024 * 1024); - printf("allocated 1GB: %p\n", p[i]); - sys(MEMFREE); + print_stats(1); + printf(" malloc returned: %p\n", p[i]); } - printf("\n\n"); + printf("\n"); for (i = 0; i < NUM_OF_GBYTES; i++) { if (p[i]) { printf("memsetting 1GB: %p\n", p[i]); - sys("date +%s.%N"); + print_stats(0); memset(p[i], 1, 1024 * 1024 * 1024); - sys("date +%s.%N"); - sys(MEMFREE); + print_stats(1); } } - printf("\n\n"); + printf("\n"); for (i = 0; i < NUM_OF_GBYTES; i++) { if (p[i]) { printf("free 1GB: %p\n", p[i]); + print_stats(0); free(p[i]); - sys(MEMFREE); + print_stats(1); } } + return 0; } -- cgit v1.2.3