diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/alloc.c b/alloc.c old mode 100644 new mode 100755 index 931b9fd59da6368f6a48cfa9f681133f918b162b..02527ff36741ad5fb17a87acd368368a4282b33d --- a/alloc.c +++ b/alloc.c @@ -1 +1,163 @@ #include "alloc.h" + +size_t align_4_unit(size_t size); + + +void *m_malloc(size_t size){ + + size = align_4_unit(size); + + if(head == NULL){ + meta* temp; + temp = sbrk(sizeof(struct meta_struct)+size); + temp->next = NULL; + temp->prev = NULL; + temp->size = size; + temp->free = 0; + + head = temp; + tail = temp; + return (void*)temp+sizeof(struct meta_struct); + } + + meta* ptr = head; + meta* ptr_assign; + + if(fit == ALLOC_FIRST_FIT){ + while(!ptr->free){ + if(ptr == NULL) break; + if(ptr->size>(size+sizeof(struct meta_struct))) break; + else ptr = ptr->next; + } + ptr_assign = ptr; + } + else if(fit == ALLOC_WORST_FIT){ + while(ptr){ + if(ptr->size > size+sizeof(struct meta_struct) && ptr->free == 1){ + if(ptr_assign == NULL) ptr_assign = ptr; + else if(ptr_assign->size < ptr->size) ptr_assign = ptr; + } + else ptr = ptr->next; + } + } + else if(fit == ALLOC_BEST_FIT){ + while(ptr){ + if(ptr->size > size+sizeof(struct meta_struct) && ptr->free == 1){ + if(ptr_assign == NULL) ptr_assign = ptr; + else if(ptr_assign->size > ptr->size) ptr_assign = ptr; + } + else ptr = ptr->next; + } + } + else { + sprintf(stderr,"wrong fit option"); + } + //printf("fit option done\n"); + + if(ptr_assign == NULL){ + meta* temp; + temp = sbrk(sizeof(struct meta_struct)+size); + temp->next = NULL; + temp->prev = tail; + temp->size = size; + + tail->next = temp; + tail = temp; + + return (void*)temp+sizeof(struct meta_struct); + } + else{ + meta* temp = (void*)(ptr_assign->next - (size+sizeof(struct meta_struct))); + temp->prev = ptr_assign->prev; + temp->prev->next = temp; + if(ptr_assign->next) ptr_assign->next->prev = temp; + temp->next = ptr_assign->next; + temp->size = size; + ptr_assign->size = ptr_assign->size - (sizeof(struct meta_struct)+size); + + return temp+sizeof(struct meta_struct); + + } + +} +void m_free (void*ptr){ + if(ptr == NULL) return; + meta* current = ptr; + current = (void*)(current - sizeof(struct meta_struct)); + current->free = 1; + + if(current == tail){ + if(current == head){ + brk(head); + } + else{ + tail = current->prev; + current->prev->next = NULL; + brk(current->prev); + } + } + if(current->prev && current->prev->free){ + current->prev->size += current->size; + + current->prev->next = current->next; + if(current->next) current->next->prev = current->prev; + } + if(current->next && current->next->free){ + current->size += current->next->size; + + current->next = current->next->next; + if(current->next->next) current->next->prev = current; + } + + +} + +void* m_realloc(void* ptr,size_t size){ + + meta *current = ptr; + current = (void * )(current - sizeof(struct meta_struct)); + if(size < current->size){ + meta *mark_empty = (void * )current + (sizeof(struct meta_struct) + size); + mark_empty->prev = current; + mark_empty->next = current->next; + mark_empty->size = current->size - size; + mark_empty->free = 1; + + current->next = mark_empty; + current->next->prev = mark_empty; + + memcpy(current,ptr,(sizeof(struct meta_struct)+size)); + + return (void*)(current+sizeof(struct meta_struct)); + + }else if(size>current->size){//size>current->size + + if(current == tail){ + sbrk(size - current->size); + current->size = size; + return (void*)(current+sizeof(struct meta_struct)); + } + else{ + if(current->size + current->next->size >= size){ + current->next = (void*)current +(sizeof(struct meta_struct)+ size); + current->next->size = (current->next->size -(size - current->size)); + current->size = size; + current->next->free = 1; + + return (void*)(current+sizeof(struct meta_struct)); + } + else{ + meta *temp = m_malloc(size); + memcpy(temp,current,size+sizeof(struct meta_struct)); + m_free(ptr); + return (void*)(temp+sizeof(struct meta_struct)); + } + } + } + +} + + +size_t align_4_unit(size_t size){ + return size+(4-(size%4)); +} \ No newline at end of file diff --git a/alloc.h b/alloc.h old mode 100644 new mode 100755 index 3245f55c3496bc75c70c04625ca3dcb7dd5d0afd..36a523307f1d3cff0764e88c5b5a53d7a2848fa3 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,31 @@ #ifndef _ALLOC_H_ #define _ALLOC_H_ +#include <stdint.h> +#include <sys/types.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> + +#define ALLOC_FIRST_FIT 0 +#define ALLOC_BEST_FIT 1 +#define ALLOC_WORST_FIT 2 + + typedef struct meta_struct { + struct meta_struct* next; + struct meta_struct* prev; + + uint32_t free; + uint32_t size; } meta; +meta* head; +meta* tail; +int fit; + +int num; +char type[10]; + #endif diff --git a/input.txt b/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..8a55a18eabb66cef3c24f8e64455ad3151029d2f --- /dev/null +++ b/input.txt @@ -0,0 +1,4 @@ +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 strengthened by use. +s Life is the art of drawing sufficient conclusions from insufficient premises. \ No newline at end of file diff --git a/main b/main new file mode 100755 index 0000000000000000000000000000000000000000..dad82d849c7cb88106be86de548bf4b7ede12089 Binary files /dev/null and b/main differ diff --git a/main.c b/main.c old mode 100644 new mode 100755 index b5fba0682b01a1005462aa24bf4ac2f21227e156..29d26365e84278caec54568fb0d0f6b61bbae479 --- a/main.c +++ b/main.c @@ -1,6 +1,145 @@ #include "alloc.h" +#include<unistd.h> +#include <string.h> -int main() -{ - return 0; +int main(int argc,char* argv[]){ + int count = 0; + int k = 0; + FILE* fp = fopen("input.txt","r"); + char buffer[512]; + char* token; + int length = strlen(buffer); + + fgets(buffer,512,fp); + buffer[length-1]='\0'; + //printf("%s",buffer); + + if(buffer[2] == 'F'){ + fit = ALLOC_FIRST_FIT; + } + else if(buffer[2] == 'B'){ + fit = ALLOC_BEST_FIT; + } + else if(buffer[2] == 'W'){ + fit = ALLOC_WORST_FIT; + } + else{ + printf("wrong flags\n"); + printf("%s",buffer); + return -1; + } + void * chunk_list[100]; + meta * meta_chunk[100]; + printf("while\n"); + + do{ + fgets(buffer,512,fp); + // printf("%s",buffer); + token = strtok(buffer," "); + //printf("%s\n",token); + if(!strcmp(token,"s")){ + // printf("token : %s\n",token); + //printf("%s\n",token); + token = strtok(NULL,"\n"); + printf("%s\n",token); + chunk_list[count] = m_malloc(strlen(token)+1); + strcpy(chunk_list[count],token); + count ++; + printf("s is done\n"); + + } + else if(!strcmp(token,"f")){ + // token = strtok(NULL,"\n"); + // int free = atoi(token); + // m_free(chunk_list[free]); + // k++; + } + else if(!strcmp(token,"e")){ + // token = strtok(NULL,"\n"); + // int b = atoi(token); + // chunk_list[count-k] = m_malloc(b); + // chunk_meta[count-k] = (meta *)((char*)chunk_list[count-k]-sizeof(struct meta_struct)); + } + else if (!strcmp(token,"r")){ + + } + }while(buffer); + + + /* + meta* met[num]; + void *chunk[num]; + + int n =0; + int i =0; + int dup=0; + + for ( ; i < num ; i++ ) + { + fgets(buf,256,fd); + tok = strtok(buf," "); + + if(!strcmp(tok,"s")) + { + tok = strtok(NULL,"\n"); + strcpy(data,tok); + + + chunk[i-dup] = m_malloc(strlen(data)+1); + strcpy((char *)chunk[i-dup],data); + met[i-dup] = (meta*)((char *)chunk[i-dup]-16); +// printf("%d - %d\n",i ,chunk[i]); + } + else if(!strcmp(tok,"f")) + { + tok = strtok(NULL,"\n"); + int target = atoi(tok); +// printf("%d target - %d\n",target,chunk[target]); + m_free(chunk[target]); + dup++; + } + else if(!strcmp(tok,"r")) + { + tok = strtok(NULL," "); + int target = atoi(tok); + + tok = strtok(NULL,"\n"); + int byte = atoi(tok); + + m_realloc(chunk[target],(size_t)byte); + dup++; + + } + else if(!strcmp(tok,"e")) + { + + tok = strtok(NULL,"\n"); + int byte = atoi(tok); + + chunk[i-dup] = m_malloc(byte); + + met[i-dup] = (meta*)((char *)chunk[i-dup]-16); + + } + } + int h=0; + while(head) + { + if((head->free) ==1) + {} + else{ + printf("%d %d-%s\n",head->free,head->size,(char *)head+16); + + } + head= head->next; + } + + printf("Finished\n");*/ + fclose(fp); } + +/* + + + +}*/ \ No newline at end of file diff --git a/temp.c b/temp.c new file mode 100644 index 0000000000000000000000000000000000000000..b33968baa18c7c0f64de99e8c37e4f0fbd06bb68 --- /dev/null +++ b/temp.c @@ -0,0 +1,303 @@ +#include "alloc.h" +// 1 meta size is 16 - point + 1 == 16 meta size +// count +// free - 0 (not free) & free - 1 (free) +// return new meta pointer +void *new_malloc(size_t chunk_size){ + meta* a; + + a = sbrk(sizeof(meta)+chunk_size) ; + a->next = NULL; + a->prev = NULL; + a->size = chunk_size; + a->free = 0; + return a; +} +void *m_malloc(size_t size) +{ + meta* ret; + meta* save; + // new head + if(head == NULL) + { + head = new_malloc(size); + return (head+1); + } + // head exist + else + { + save = head; + // for reculsive + meta* prev; + int count=0; + //first fit + if(fit == 0){ + //find split space + while( (head && (!(head->free) || head->size < (size+sizeof(meta)))) ) + { + prev = head; + head = head->next; + count++; + }}//first fit + //Best fit + else if(fit ==1){ +// printf("Best\n"); + int offset = 0; + int i = 0; + int sub = 50000; + int cmp = 0; + int flag = 0; + while(head) + { + + if(head->free && head->size >= (size + sizeof(meta)) ) + { + + cmp = head->size - (size + sizeof(meta)); + if(sub > cmp) + { + sub = cmp; + offset = i; + flag =1; + } + } + i++; + head =head -> next; + } + head = save; + + if(flag == 0) + { + while(head) + { + prev = head; + head = head->next; + } + } + + else{ + for(i=0;i<offset;i++) + { + prev = head; + head = head->next; + } + } + + } + + + //Worst fit + else{ + +// printf("Worst\n"); + int offset = 0; + int i = 0; + int sub = 0; + int cmp = 0; + int flag = 0; + while(head) + { + if(head->free && head -> size >= (size + sizeof(meta))) + { + cmp = head -> size - (size + sizeof(meta)); + if(sub < cmp) + { + sub = cmp; + offset = i; + flag = 1; + } + } + i++; + head = head -> next; + } + head = save; + if(flag == 0){ + + while(head) + { + prev = head; + head = head->next; + } + } + + else{ + for(i=0;i<offset;i++) + { + prev=head; + head = head->next; + } + } + } + // if no space + if(head==NULL){ + +// printf("if no space\n"); + + meta* new = (sbrk(sizeof(meta) + size)); + new->next = NULL; + new->prev = prev; + new->size = size; + prev->next = new; + new->free = 0; + ret = new; + } + // space exists + else{ + +// printf("space exists %d\n",size); + meta* new = (meta*)((char *)head->next - (sizeof(meta)+size) ); + new-> next = head->next; + if(head->next) + head->next->prev = new; + new->free = 0; + new -> prev = head; + head->next = new; + new -> size = size ; + head -> size = head->size - (sizeof(meta)+size); + + ret = new; + } + head = save; + }//else + return (ret+1) ; +} +void m_free(void *ptr) +{ + ptr = ptr-16; + // NULL + if(!ptr) + { + printf("NULL\n"); + return; + } + int flag=0; + meta *save = head; + while(head) + { + + if(head == ptr) + { + flag =1 ; + break; + } + + head = head->next; + } + if(flag == 0) + { + printf("Invalid pointer\n"); + return; + } + head = save; + meta* a = (meta*)ptr; + a->free = 1; +// printf("a->prev %d\n",a->prev); +//printf("123 123\n"); + if(a->next && a->next->free) + { + a->size = sizeof(meta) + a->size + a->next->size; + a->next = a->next->next; + if(a->next) + { + a->next->prev = a; + } + } +//printf("134 134\n"); + if(ptr==head) + {} + else if(a->prev->free) + { + a = a->prev; + a->size = sizeof(meta) + a->size + a->next->size; + a->next = a->next->next; + if(a->next) + { + a->next->prev = a; + } + } +//printf("146 146\n"); + if((a->next)==NULL) + { + if(a == head) + { + sbrk(-(a->size + sizeof(meta))); + head = NULL; + } + else{ + a->prev->next= NULL; + sbrk(- (a->size + sizeof(meta))); + } + } +//printf("???\n"); +} +void * m_realloc(void* ptr,size_t size){ + + char copy[1024]; + strcpy(copy,ptr); + ptr = ptr-16; + meta* a = (meta *)ptr; + + if((a->size == size)) + { + return ptr+16; + } + if( a->size > size ) + { + if( a->size - 16 < size ) + { + + return ptr+16; + } + + else + { + meta* split = (meta *)( (char*)a + sizeof(meta) + size ); + split -> size = a->size - sizeof(meta) - size; + split -> free = 1; + split -> next = a->next; + if(split->next) + { + split->next->prev = split; + } + split -> prev = a; + a->next = split; + a->size = size; + + a+=1; + strcpy((void *)a,copy); + return a; + } + } + if(!a->next) + { + sbrk(size - a->size); + a->size = size; + + return a+1; + } + else{ + if( (a->next->free ==1) && ( (a->size + a->next->size) >= size ) ) + { + meta *fusion = (meta *)((char *)a + sizeof(meta) + size ); + fusion->free = 1; + fusion->size = a->next->size - (size - a->size); + fusion->next = a->next->next; + if(fusion->next) + { + fusion->next->prev = fusion; + } + fusion ->prev = a; + a->size = size; + a->next = fusion; + a+=1; + strcpy((void *)a,copy); + return a; + } + } + ptr += 16; + m_free(ptr); + void *r = m_malloc(size); + + a = (meta *)((char *)r + sizeof(meta)); + strcpy((void *)a,copy); + return a; \ No newline at end of file