int main(int argc, char *argv[]) { int i, maxMem=0; void *start, *end; struct timeval starttv; struct timeval endtv; if (argc > 1) { printf("Using seed: %s", argv[1]); srand(atoi(argv[1])); } else { srand((unsigned int)time(NULL)); } start = (void *)sbrk(0); gettimeofday(&starttv, NULL); /*Alloc MAXPOSTS blocks of memory*/ for(i=0;i<MAXPOSTS;i++) { memPosts[i].size = rand()%(MAXSIZE/2); #ifndef NO memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double)); #endif } gettimeofday(&endtv, NULL); end = (void *) sbrk(0); printf("Time for malloc: %f\n", ((float)(endtv.tv_sec-starttv.tv_sec) + (float)(endtv.tv_usec-starttv.tv_usec)/1000000)); calcMemUsage(&maxMem); printf("Maxmem after malloc: %d\n", maxMem); printf("Memory consumed after malloc %ld\n\n", (unsigned long)(end-start)); gettimeofday(&starttv, NULL); /*Realloc MAXITERS blocks of memory*/ for(i=0;i<MAXITERS;i++) { int index; index = rand()%MAXPOSTS; if(ALLOCATED(index)) { if(rand()%5 < 3) { memPosts[index].size = rand()%MAXSIZE; #ifndef NO memPosts[index].ptr = (double*) realloc(memPosts[index].ptr, memPosts[index].size*sizeof(double)); #endif } } } gettimeofday(&endtv, NULL); end = (void *) sbrk(0); printf("Time for realloc: %f\n", ((float)(endtv.tv_sec-starttv.tv_sec) + (float)(endtv.tv_usec-starttv.tv_usec)/1000000)); calcMemUsage(&maxMem); printf("Maxmem after realloc: %d\n", maxMem); printf("Memory consumed after realloc %ld\n\n", (unsigned long)(end-start)); gettimeofday(&starttv, NULL); /*Free all blocks*/ for(i=0;i<MAXPOSTS;i++) { #ifndef NO memPosts[i].size = 0; free(memPosts[i].ptr); #endif } gettimeofday(&endtv, NULL); printf("Time for free: %f\n", ((float)(endtv.tv_sec-starttv.tv_sec) + (float)(endtv.tv_usec-starttv.tv_usec)/1000000)); return 0; }
int main(int argc, char *argv[]) { int i, maxMem=0; void *start, *end; char *progname; if (argc > 0) progname = argv[0]; else progname = ""; MESSAGE("-- This test checks malloc(), free() and realloc()\n"); srand((unsigned int)time(NULL)); #ifdef CUSTOM_MALLOC MESSAGE("Using custom malloc\n"); #else MESSAGE("Using system malloc\n"); #endif #ifdef MMAP start = endHeap(); #else start = (void *)sbrk(0); #endif for(i=0;i<MAXPOSTS;i++) { memPosts[i].size = rand()%(MAXSIZE/2); startMeasure(); memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double)); stopMeasure(); if ( memPosts[i].size == 0 && memPosts[i].ptr!= NULL ) /* MESSAGE("* ERROR: malloc doesn't return NULL pointer on zero size\n");*/ ; else if( memPosts[i].size && memPosts[i].ptr == NULL ) { MESSAGE("* ERROR: malloc returned NULL on non-zero size request\n"); } else if( memPosts[i].ptr != NULL ) memPosts[i].ptr[0] = (double)3.14; } calcMemUsage(&maxMem); for(i=0;i<MAXITERS;i++) { int index; index = rand()%MAXPOSTS; if(ALLOCATED(index)) { if(rand()%5 < 3) { if(memPosts[index].ptr[0] != (double)3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); memPosts[index].size = rand()%MAXSIZE; startMeasure(); memPosts[index].ptr = (double*) realloc(memPosts[index].ptr, memPosts[index].size*sizeof(double)); stopMeasure(); if(memPosts[index].size && memPosts[index].ptr[0] != (double)3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); if(memPosts[index].size) memPosts[index].ptr[0] = (double)3.14; } else { if(memPosts[index].ptr[0] != (double)3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); startMeasure(); free(memPosts[index].ptr); stopMeasure(); memPosts[index].size = 0; } } else { memPosts[index].size = rand()%MAXSIZE; startMeasure(); memPosts[index].ptr = (double*) malloc(memPosts[index].size*sizeof(double)); stopMeasure(); if(memPosts[index].size) memPosts[index].ptr[0] = (double)3.14; } calcMemUsage(&maxMem); } #ifdef MMAP end = endHeap(); #else end = (void *) sbrk(0); #endif fprintf(stderr, "%s: Max memory allocated %d\n%s: Memory consumed %ld\n%s: Time in free/malloc/realloc: %f\n", progname, maxMem, progname, (unsigned long)(end-start), progname, getMeasuredTime()); return 0; }
int main(int argc, char *argv[]) { int i, maxMem=0; char *start, *end; char *progname; if (argc > 0) progname = argv[0]; else progname = ""; MESSAGE("-- This test checks malloc(), free() and realloc()\n"); srand((unsigned int)time(NULL)); start = (char *)sbrk(0); for(i=0;i<MAXPOSTS;i++) { memPosts[i].size = rand()%(MAXSIZE/2); memPosts[i].ptr = (double*) malloc(memPosts[i].size*sizeof(double)); if ( memPosts[i].size == 0 && memPosts[i].ptr!= NULL ) /* MESSAGE("* ERROR: malloc doesn't return NULL pointer on zero size\n");*/ ; else if( memPosts[i].size && memPosts[i].ptr == NULL ) { MESSAGE("* ERROR: malloc returned NULL on non-zero size request\n"); } else if( memPosts[i].ptr != NULL ) memPosts[i].ptr[0] = 3.14; } calcMemUsage(&maxMem); for(i=0;i<MAXITERS;i++) { int index; index = rand()%MAXPOSTS; if(ALLOCATED(index)) { if(rand()%5 < 3) { if(memPosts[index].ptr[0] != 3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); memPosts[index].size = rand()%MAXSIZE; memPosts[index].ptr = (double*) realloc(memPosts[index].ptr, memPosts[index].size*sizeof(double)); if(memPosts[index].size && memPosts[index].ptr[0] != 3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); if(memPosts[index].size) memPosts[index].ptr[0] = 3.14; } else { if(memPosts[index].ptr[0] != 3.14) MESSAGE("* ERROR: Corrupt memory handling\n"); free(memPosts[index].ptr); memPosts[index].size = 0; } } else { memPosts[index].size = rand()%MAXSIZE; memPosts[index].ptr = (double*) malloc(memPosts[index].size*sizeof(double)); if(memPosts[index].size) memPosts[index].ptr[0] = 3.14; } calcMemUsage(&maxMem); } end = (char*) sbrk(0); fprintf(stderr, "%s: Max memory allocated %d\n%s: Memory consumed %ld\n", progname, maxMem, progname, (unsigned long)(end-start)); return 0; }