diff --git a/Makefile b/Makefile index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c232842cd29b4d04ca6ff8422bbff60759645e66 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,9 @@ + +main: main.c alloc.c + gcc -w -g O2 $^ -o $@ + +.PHONY: clean +clean: + rm -rf *~ main + + diff --git a/alloc.c b/alloc.c index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a54eaf9a9ba33693d4a4dc9c0243cc0b8266b14b 100644 --- a/alloc.c +++ b/alloc.c @@ -0,0 +1,108 @@ +#include "alloc.h" + +void *m_malloc(size_t size){ + meta* ptr = head; + meta* chunk; + + + //align the requested size in 4bytes units + size = align_size(size); + + //no memory + if(head == NULL){ + meta* tmp; + tmp = sbrk(sizeof(meta) + size); + tmp->next = NULL; + tmp->prev = NULL; + tmp->size = size; + tmp->free = 0; //chunk used + head = tmp; + tail = tmp; + + return (tmp + 1); + } + //heap empty + else if(fit == 0){ //first fit + while(ptr-> free == 0){ + if(ptr == NULL) + break; + if(ptr->size > (sizeof(meta) + size)) + break; + ptr = ptr->next; + } + chunk = ptr; + } + else if(fit == 1){ //Best fit + while(ptr){ + if(ptr->size > size*sizeof(meta) && ptr->free == 1){ //if there is fit chunk + if(chunk == NULL) + chunk = ptr; + else if(chunk->size > ptr->size) //we want smallest chunk size + chunk = ptr; + } + ptr = ptr->next; + } + } + else if(fit == 2){ //Worst fit + while(ptr){ + if(ptr->size > size*sizeof(meta) && ptr->free == 1){ + if(chunk == NULL) + chunk = ptr; + else if(chunk->size < ptr->size) //we want biggest chunk size + chunk = ptr; + } + ptr = ptr->next; + } + } + else + return -1; + + //memory allocation + if(chunk == NULL){ //new memory space + meta* tmp; + tmp = sbrk(sizeof(meta) + size); + tmp->next = NULL; + tmp->prev = tail; + tmp->size = size; + + //change tail + tail->next = tmp; + tail = tmp; + } + else{ + meta* tmp; + tmp = (meta*)(chunk->next - (sizeof(meta) + size)); + tmp->prev = chunk->prev; + tmp->prev->next = tmp; + + tmp->next = chunk->next; + if(chunk->next) + chunk->next->prev = tmp; + + tmp->size = size; + } + return (tmp + sizeof(meta)); +} + + + +void m_free(void *ptr){ + meta* ptr = head; + + if(ptr == NULL) //if memory space free + return; + + + +} + +void* m_realloc(void* ptr, size_t size){ + + +} + +size_t align_size(size_t size){ + uint32_t tmp = 0; + tmp = size % 4; + return (size + (4-tmp)); +} diff --git a/alloc.h b/alloc.h index 24486ad45da459088ee22dac0260b81142be9249..ce954222e6dab8f255c3a8d5e9a8c403f628b0a1 100644 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,27 @@ #ifndef _ALLOC_H_ #define _ALLOC_H_ -typedef struct meta_struct { +#include<stdint.h> + +static meta* head = NULL; +static meta* tail = NULL; + +int fit = 0; //0:first fit, 1:Best fit, 2:Worst fit +typedef struct meta_struct { + struct meta_struct* prev; + struct meta_struct* next; + uint32_t free; //if 0: chunk used, 1: chunk is free + uint32_t size; } meta; -#endif \ No newline at end of file +//return : pointer beginning of the alloceted space +void *m_malloc(size_t size); + +void m_free(void *ptr); + +void* m_realloc(void* ptr, size_t size); + + + +#endif diff --git a/main.c b/main.c index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..5097d0bcaba0c39be3cf110739670cb75a42b4bc 100644 --- a/main.c +++ b/main.c @@ -0,0 +1,61 @@ +#include "alloc.h" + + +int main(int argc, char* argv[]) +{ + char buf[1024]; + char string[1024]; + char *ptr; + int commandNum; + + FILE *fd = fopen(argv[1], "r"); + fgets(buf, 1024, fd); + buf[strle(buf) - 1] = "\0"; + + ptr = strtok(buf, " "); + commandNum = atoi(ptr); + + //get fit type + ptr = strtok(NULL, " "); + if(strcmp(ptr, "F") == 0) //First fit + fit = 0; + else if(strcmp(ptr, "B") == 0) //Best fit + fit = 1; + else if(strcmp(ptr, "w") == 0) //Worst fit + fit = 2; + else{ + printf("invalid fit type"); + return -1; + } + + + //read command + + for(int i = 0; i < commandNum; i++){ + fgets(buf, 1024, fd); + ptr = strtok(buf, " "); + if(strcmp(ptr, "s") == 0){ //string command + ptr = strtok(NULL, "\n"); + strcpy(string, ptr); + + + } + else if(strcmp(ptr, "f") == 0){ //nth area free + + + } + else if(strcmp(ptr, "r") == 0){ //n, m : realloc m byte in nth area + + + } + else if(strcmp(ptr, "e") == 0){ //alloc n byte without space + + + } + + + } + + + return 0; +}