int main(int argc, char* argv[]){ if(argc == 5){ request.siz = atoi(argv[3]); request.beg = atoi(argv[2]); if(argv[4][0] == 'b'){ isWorst = false; }else{ isWorst = true; } }else if(argc == 4){ request.siz = atoi(argv[2]); request.beg = -1; printf("!\n"); if(argv[3][0] == 'b'){ isWorst = false; }else{ isWorst = true; } }else{ printf("EX0: match.exe free 12 20 b(release 20 bytes, begin at 12, using best-matching).\nEX1: match.exe malloc 28 w(allocate 28 bytes, using worst-matching)\n"); exit(0); } int begin, size; Node* p = &head; FILE* in; if(!(in = fopen("stats.txt", "r"))){ printf("Fail to acquire memory States!\n"); } while(~fscanf(in, "%d %d", &begin, &size)){ printf("%d %d\n", begin, size); Node* newNode = new Node(); p->nxt = newNode; p->beg = begin; p->siz = size; newNode->beg = -1; newNode->siz = 0; newNode->nxt = NULL; p = p->nxt; blocks ++; } fclose(in); printf("We get:\n"); show_list(); if(request.beg != -1){ free_mem(); }else if(isWorst){ malloc_worst(); }else{ malloc_best(); } printf("Current Mem-states:\n"); show_list(); write_back(); return 0; }
/* number of bytes of memory to allocate */ void *malloc(size_t nbytes) { if (nbytes == 0) { return NULL; } #if STRATEGY == 1 return malloc_first(nbytes); #elif STRATEGY == 2 return malloc_best(nbytes); #elif STRATEGY == 3 return malloc_worst(nbytes); #elif STRATEGY == 4 return malloc_quick(nbytes); #else exit(1); #endif }
/* malloc * * malloc returns the start address of dynamically allocated memory. * This is simply a wrapper function that calls the different algorithm * implementations depending on the value of STRATEGY. * */ void *malloc( size_t nbytes) /* number of bytes of memory to allocate */ { if (nbytes == 0) { return NULL; } #if STRATEGY == 1 return malloc_first(nbytes); #elif STRATEGY == 2 return malloc_best(nbytes); #elif STRATEGY == 3 return malloc_worst(nbytes); #elif STRATEGY == 4 return malloc_quick(nbytes); #else exit(1); #endif }