diff --git a/alloc.c b/alloc.c index 931b9fd59da6368f6a48cfa9f681133f918b162b..3c6006169566fc4af695a3d9051b3d71dca469ad 100644 --- a/alloc.c +++ b/alloc.c @@ -1 +1,157 @@ #include "alloc.h" + +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +void *m_malloc(uint32_t size){ + meta* temp; + if(size%4 != 0) + size += (4-size%4); + + if(head == NULL){ + temp = sbrk(size); + temp -> next_memory = NULL; + temp -> prev_memory = NULL; + temp -> size = size; + temp -> free = 0; + strcpy(temp -> comment,tmp_comment); + temp -> fit= fit; + + head = temp; + first = temp; + } + else{ + meta* prev = NULL; + meta* next = NULL; + meta* tmp_head = head; + while(1){ + if(tmp_head->next_memory == NULL){ + if(prev == NULL){ + prev = tmp_head; + } + break; + } + else if(tmp_head -> next_memory -> free == 1 && tmp_head -> next_memory -> size > size){ + if( prev == NULL){ + prev = tmp_head; + if(fit == "F") + break; + } + else{ + if(fit == "B") + prev = (prev -> size < tmp_head -> next_memory -> size) ? prev : tmp_head; + else if(fit == "W") + prev = (prev -> size > tmp_head -> next_memory -> size) ? prev : tmp_head; + } + } + tmp_head = tmp_head-> next_memory; + } + tmp_head = prev -> next_memory; + next = tmp_head -> next_memory; + if(tmp_head -> size == size){ + prev -> next_memory -> free = 0; + } + else{ + temp -> prev_memory = prev; + temp -> next_memory = tmp_head; + temp -> size = size; + temp -> free = 0; + strcpy(temp -> comment,tmp_comment); + temp -> fit = fit; + + tmp_head -> size -= size; + tmp_head -> prev_memory = temp; + + prev -> next_memory = temp; + } + } +} + +void m_realoc(void* p,uint32_t size){ + if(size%4 != 0) + size += (4-size%4); + + int index = *(int *)p; + int count = 0; + meta* temp = head; + + while(1){ + if(count == index) + break; + + temp = temp -> next_memory; + if(temp -> free == 0) + count++; + } + if(temp -> next_memory == NULL){ + temp -> size = size; + } + else if(size < (temp -> next_memory -> size) + temp -> size){ + temp -> next_memory -> size -= (size - temp->size); + temp -> size = size; + } + else if(size = (temp -> next_memory -> size) + temp -> size){ + temp -> size = size; + temp -> next_memory = temp -> next_memory -> next_memory; + } + else + m_malloc(size); +} + +void m_free(void *ptr){ + int index = *(int *)ptr; + int count = 0; + meta* temp = head; + int size = head -> size; + int break_point = 0; + + while(1){ + if(count == index) + break; + + temp = temp -> next_memory; + if(temp -> free == 0) + count++; + } + while(1){ + if(temp -> next_memory == NULL){ + if(temp -> next_memory -> free == 1){ + size += temp -> next_memory -> size; + temp -> next_memory = temp -> next_memory -> next_memory; + } + else + break; + } + else + break; + } + while(1){ + if(temp -> prev_memory == NULL){ + if(temp -> prev_memory -> free == 1){ + size += temp -> prev_memory -> size; + temp -> prev_memory = temp -> prev_memory -> prev_memory; + } + else + break; + } + else + break; + } + temp -> free = 1; + strcpy(temp -> comment," "); + if( index == 0) + head == temp -> next_memory; +} + + +void read(){ + meta* temp = first; + + while(1){ + if(temp -> next_memory == NULL) + break; + + printf("%d %d %s\n", temp -> free, temp -> size, temp -> comment); + } +} diff --git a/alloc.h b/alloc.h index 3245f55c3496bc75c70c04625ca3dcb7dd5d0afd..60f9bd5d0b7bfb00a40ed858af5d40c8eb6e742d 100644 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,22 @@ #ifndef _ALLOC_H_ #define _ALLOC_H_ -typedef struct meta_struct { +#include <stdint.h> +typedef struct meta_struct { + struct meta_struct* prev_memory; + struct meta_struct* next_memory; + uint32_t free; + uint32_t size; + char *comment; + char fit; } meta; +char fit; +char *tmp_comment; + +meta* head; +meta* first; + + #endif diff --git a/input.txt b/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..92308ca5672067c036689c5f77b8d11d30fbf608 --- /dev/null +++ b/input.txt @@ -0,0 +1,7 @@ +3 f +S THink like a man of action and act like man of thought. +s Courage is very important. LIke a muscle, it is strngthened by use. +s Life is the art if drqwing sufficient conclusions from insufficientpremises. +2 F +f 0 +r 1 diff --git a/main b/main new file mode 100755 index 0000000000000000000000000000000000000000..90eb02129370e40d37a4f7e2bd1d6f380ec0e167 Binary files /dev/null and b/main differ diff --git a/main.c b/main.c index b5fba0682b01a1005462aa24bf4ac2f21227e156..f526ffe72a3bf320ce8186e145ff24d7c1d6e8af 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,48 @@ #include "alloc.h" -int main() -{ +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char *argv[]){ + uint32_t size_index,size_data; + char type_fit, type_data; + char temp[256]; + int num, index; + FILE *fp = fopen("input.txt","rb"); + + if(fp == NULL){ + perror("Can't open the file"); + exit(1); + } + else{ + while(1){ + if(EOF == fscanf(fp,"%d %c",size_index,type_fit)) + break; + + for(int i = 0; i < size_index; i++){ + fscanf(fp,"%c",type_data); + if(type_data == "f"){ + fscanf(fp,"%d",index); + m_free(index); + } + else if(type_data == "r"){ + fscanf(fp,"%d %d",index,size_data); + m_realoc(index,size_data); + } + else if(type_data == "s"){ + fscanf(fp,"%s",temp); + if(temp != NULL){ + strcpy(tmp_comment,temp); + size_data = strlen(tmp_comment); + } + m_malloc(size_data); + } + } + read(); + } + } + fclose(fp); + return 0; }