int main(int argc, char* argv[]) { int i; int n_alloc=0, n_dealloc=0; kma_page_stat_t* stat; srand(SEED); for (i = 0; i < NUM_OPS; i++) { if ((first != NULL) && (first->next != NULL) && (frand() > PROB_ALLOC)) { // always one page remaining deallocate(); n_dealloc++; } else { allocate(); n_alloc++; } } stat = page_stats(); printf("Allocation/Deallocation: %5d/%5d\n", n_alloc, n_dealloc); printf("Page Requested/Freed/In_Use: %5d/%5d/%5d\n", stat->num_requested, stat->num_freed, stat->num_in_use); printf("Freeing all memory now\n"); // free all allocated memory while (first) { deallocate(); } stat = page_stats(); printf("Page Requested/Freed/In Use: %5d/%5d/%5d\n", stat->num_requested, stat->num_freed, stat->num_in_use); if ((stat->num_requested == stat->num_freed) && stat->num_in_use == 0) { printf("Test: PASS\n"); } else { printf("Test: FAILED\n"); } exit(0); }
int main(int argc, char* argv[]) { name = argv[0]; #ifdef COMPETITION printf("%s: Running in competition mode\n", name); #endif #ifndef COMPETITION printf("%s: Running in correctness mode\n", name); #endif int n_req = 0, n_alloc=0, n_dealloc=0; kpage_stat_t* stat; #ifdef COMPETITION double ratioSum = 0.0; int ratioCount = 0; #endif #ifndef COMPETITION FILE* allocTrace = fopen("kma_output.dat", "w"); if (allocTrace == NULL) { error("unable to open allocation output file", "kma_output.dat"); } fprintf(allocTrace, "0 0 0\n"); #endif if (argc != 2) { usage(); } FILE* f_test = fopen(argv[1], "r"); if (f_test == NULL) { error("unable to open input test file", argv[1]); } // Get the number of requests in the trace file // Allocate some memory... int status = fscanf(f_test, "%d\n", &n_req); if(status != 1) error("Couldn't read number of requests at head of file", ""); mem_t* requests = malloc((n_req + 1)*sizeof(mem_t)); memset(requests, 0, (n_req + 1)*sizeof(mem_t)); char command[16]; int req_id, req_size, index = 1; // Parse the lines in the file, and call allocate or // deallocate accordingly. while (fscanf(f_test, "%10s", command) == 1) { if (strcmp(command, "REQUEST") == 0) { if (fscanf(f_test, "%d %d", &req_id, &req_size) != 2) error("Not enough arguments to REQUEST", ""); assert(req_id >= 0 && req_id < n_req); allocate(requests, req_id, req_size); n_alloc++; } else if (strcmp(command, "FREE") == 0) { if (fscanf(f_test, "%d", &req_id) != 1) error("Not enough arguments to FREE", ""); assert(req_id >= 0 && req_id < n_req); deallocate(requests, req_id); n_dealloc++; } else { error("unknown command type:", command); } stat = page_stats(); int totalBytes = stat->num_in_use * stat->page_size; #ifdef COMPETITION if(req_id < n_req && n_alloc != n_dealloc) { // We can calculate the ratio of wasted to used memory here. int wastedBytes = totalBytes - currentAllocBytes; ratioSum += ((double) wastedBytes) / currentAllocBytes; ratioCount += 1; } #endif #ifndef COMPETITION fprintf(allocTrace, "%d %d %d\n", index, currentAllocBytes, totalBytes); #endif index += 1; } #ifndef COMPETITION fclose(allocTrace); #endif stat = page_stats(); printf("Page Requested/Freed/In Use: %5d/%5d/%5d\n", stat->num_requested, stat->num_freed, stat->num_in_use); if (stat->num_requested != stat->num_freed || stat->num_in_use != 0) { error("not all pages freed", ""); } if(anyMismatches) { error("there were memory mismatches", ""); } #ifdef COMPETITION printf("Competition average ratio: %f\n", ratioSum / ratioCount); #endif pass(); return 0; }