Created
June 20, 2014 16:05
-
-
Save lnicola/02e1aebd9a73b9ce4a16 to your computer and use it in GitHub Desktop.
simulator for simple memory allocation algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| #define TotalMemoryAmount 5000 ##UL | |
| #define MemRequestMin 40 ##UL | |
| #define MemRequestMax 1000 ##UL | |
| #define MemRequestSlice 20 ##UL | |
| #define MemThreshold 1 ##UL | |
| #define TotalTime 1000000 ##UL | |
| #define TAllocMin 5 ##UL | |
| #define TAllocMax 30 ##UL | |
| #define TRequestMin 1 ##UL | |
| #define TRequestMax 5 ##UL | |
| #define COUNTER | |
| #define rand_time(t_min,t_max) (unsigned long) ((rand() * 1.0 / RAND_MAX * (t_max - t_min) + t_min)) | |
| #define rand_size (unsigned long) ((rand() * 1.0 / RAND_MAX) * (MemRequestMax - MemRequestMin) + MemRequestMin)/MemRequestSlice * MemRequestSlice | |
| #ifdef COUNTER | |
| #define FORMAT "\rTime = %08lu, Reqs = %08lu, Fails = %08lu, Ratio = %05.2f%%" | |
| #endif /* COUNTER */ | |
| typedef struct tag_tblock { | |
| unsigned char free; | |
| unsigned long size, time; | |
| struct tag_tblock *next; | |
| } tblock; | |
| tblock *blocks, *head, *ptr; | |
| unsigned char method; | |
| const char *descs[6] = { "FIRST FIT", "BEST FIT", "WORST FIT", "LAST FIT", "BEST FIT REVERSED", "WORST FIT REVERSED" }; | |
| static int mem_alloc(const unsigned long size, const unsigned long time) | |
| { | |
| unsigned long dif = (method == 1 || method == 4) ? 0xFFFFFFFFUL : 0; | |
| ptr = NULL; | |
| blocks = head; | |
| do | |
| if ((blocks->free && blocks->size >= size) | |
| &&(!method && !ptr || method == 1 | |
| &&blocks->size - size < dif | |
| ||(method == 2 && blocks->size - size > dif) | |
| ||method == 3 || (method == 4 | |
| &&blocks->size - size <= dif) | |
| ||method == 5 && blocks->size - size >= dif)) { | |
| dif = blocks->size - size; | |
| ptr = blocks; | |
| } | |
| while ((blocks = blocks->next) != NULL); | |
| blocks = ptr; | |
| if (!blocks) | |
| return 1; | |
| if (dif < MemThreshold) { | |
| blocks->time = time; | |
| return blocks->free = 0; | |
| } else { | |
| ptr = (tblock *) malloc(sizeof(tblock)); | |
| ptr->size = dif; | |
| ptr->next = blocks->next; | |
| ptr->free = 1; | |
| blocks->size = size; | |
| blocks->next = ptr; | |
| blocks->time = time; | |
| return blocks->free = 0; | |
| } | |
| } | |
| int main() | |
| { | |
| unsigned int randseed = (unsigned int) time(NULL); | |
| unsigned long numfails, numreqs, time_sim, time_req; | |
| float ratio[6]; | |
| for (method = 0; method < 6; method++) { | |
| srand(randseed); | |
| printf("Using the following parameters:\n"); | |
| printf("TotalMemoryAmount = %lu\n", TotalMemoryAmount); | |
| printf("MemRequestMin = %lu\n", MemRequestMin); | |
| printf("MemRequestMax = %lu\n", MemRequestMax); | |
| printf("MemRequestSlice = %lu\n", MemRequestSlice); | |
| printf("MemThreshold = %lu\n", MemThreshold); | |
| printf("TotalTime = %lu\n", TotalTime); | |
| printf("TAllocMin = %lu\n", TAllocMin); | |
| printf("TAllocMax = %lu\n", TAllocMax); | |
| printf("TRequestMin = %lu\n", TRequestMin); | |
| printf("TRequestMax = %lu\n", TRequestMax); | |
| printf("Method = %s\n\n", descs[method]); | |
| head = (tblock *) calloc(1, sizeof(tblock)); | |
| if (!head) { | |
| printf("Error: not enough memory"); | |
| exit(1); | |
| } | |
| head->size = TotalMemoryAmount; | |
| head->free = 1; | |
| time_sim = numfails = numreqs = 0; | |
| while (time_sim < TotalTime) { | |
| time_req = rand_time(TRequestMin, TRequestMax); | |
| while (time_req--) { | |
| blocks = head; | |
| while (blocks->next) | |
| if (blocks->free && blocks->next->free) { | |
| blocks->size += blocks->next->size; | |
| ptr = blocks->next; | |
| blocks->next = blocks->next->next; | |
| free(ptr); | |
| } else | |
| blocks = blocks->next; | |
| blocks = head; | |
| do | |
| if (!blocks->free) | |
| if (!blocks->time--) | |
| blocks->free = 1; | |
| while ((blocks = blocks->next) != NULL); | |
| time_sim++; | |
| #ifdef COUNTER | |
| if (!(time_sim % 100)) | |
| printf(FORMAT, time_sim, numreqs, numfails, numfails * 100. / numreqs); | |
| #endif /* COUNTER */ | |
| if (time_sim > TotalTime) | |
| break; | |
| } | |
| numreqs++; | |
| numfails += mem_alloc(rand_size, rand_time(TAllocMin, TAllocMax)); | |
| } | |
| blocks = head; | |
| while (blocks) { | |
| ptr = blocks; | |
| blocks = blocks->next; | |
| free(ptr); | |
| } | |
| printf("\r%79s\rTotal number of requests = %lu", " ", numreqs); | |
| printf("\nTotal number of failures = %lu", numfails); | |
| printf("\nPercentage of failures = %05.2f%%\n", ratio[method] = numfails * 100.f / numreqs); | |
| } | |
| printf("Comparison between failure rates of the six methods:\n\n"); | |
| for (method = 0; method < 6; method++) | |
| printf("%-30s%05.2f%%\n", descs[method], ratio[method]); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment