diff --git a/alloc.c b/alloc.c index 931b9fd59da6368f6a48cfa9f681133f918b162b..13c0061c2873cf2389f0c623359b6f6baf6cb3b4 100644 --- a/alloc.c +++ b/alloc.c @@ -1 +1,413 @@ #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; + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/alloc.h b/alloc.h index 3245f55c3496bc75c70c04625ca3dcb7dd5d0afd..969adb2e38401ecebb2302dc658197408d805387 100644 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,31 @@ #ifndef _ALLOC_H_ #define _ALLOC_H_ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdbool.h> + typedef struct meta_struct { + size_t size; + bool free; + + struct meta_struct* next; + struct meta_struct* prev; + } meta; +meta *head; + +void* m_malloc(size_t size); +void m_free(void* ptr); + +char type[10]; +int num; + +// F - 0 , B - 1 , W - 2 +int fit; + + #endif diff --git a/data b/data new file mode 100644 index 0000000000000000000000000000000000000000..3dc6d9e8db27618a3fd14067f72225de8b9f4895 --- /dev/null +++ b/data @@ -0,0 +1,7 @@ +6 B +s Think like a man of action and act like man of thought. +s Keita is set to sign for Liverpool. +s Fekir will be set to sign for Liverpool. +s Alison will be join Liverpool. +e 10 +s Salah scores. diff --git a/main.c b/main.c index b5fba0682b01a1005462aa24bf4ac2f21227e156..187a99f0317826707e8531666564bb7a44c97a3d 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,199 @@ #include "alloc.h" -int main() -{ - return 0; +int main(int argc,char* argv[]){ + + sleep(1); + + + char name[256]; + strcpy(name,argv[1]); + char data[256]; + char buf[256]; + char *tok; + + + FILE * fd = fopen(name,"r"); + + fgets(buf,256,fd); + buf[strlen(buf)-1] = '\0'; + tok = strtok(buf," "); + + num = atoi(tok); + + tok = strtok(NULL," "); + + strcpy(type,tok); + + if(!strcmp(type,"F")) + { + fit = 0; + } + else if (!strcmp(type,"B")) + { + fit = 1; + } + else if(!strcmp(type,"W")) + { + fit = 2; + } + + + printf("%d %s\n",num,type); + + 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(fd); + + /* + void *a = m_malloc(24); + void *b = m_malloc(20); + void *c = m_malloc(60); + void *d = m_malloc(24); + + m_free(c); + + m_realloc(b,40); + printf("head-%d chunk-%d size-%d\n",head,a,head->size); + printf("next-%d chunk-%d size-%d\n",head->next,b,head->next->size); + printf("next-%d chunk-%d size-%d\n",head->next->next,c,head->next->next->size); + printf("next-%d chunk-%d size-%d\n",head->next->next->next,d,head->next->next->next->size); +*/ + + /* + size_t size = 24; // 24 + 16 = 40 + printf("Start memory size %d\n",size); + + void *a=m_malloc(size); + void *b=m_malloc(size); + void *c=m_malloc(size); + void *d=m_malloc(size); + +m_free(b); + + +printf("head-%d chunk-%d\n",head,a); +printf("next-%d chunk-%d\n",head->next,b); +printf("next-%d chunk-%d\n",head->next->next,c); +printf("next-%d chunk-%d\n",head->next->next->next,d); + + +printf("\n\n"); + +void *r = m_malloc(4); + +printf("head-%d chunk-%d\n",head,a); +printf("next-%d chunk-%d\n",head->next,b); +printf("next-%d chunk-%d\n",head->next->next,r); +printf("next-%d chunk-%d\n",head->next->next->next,c); +printf("next-%d chunk-%d\n",head->next->next->next->next,d); + +//m_free(d); +m_free(r); +m_free(c); + +printf("\n\n"); + +printf("b-size-%d\nb-free-%d\n",head->next->size,head->next->free); + +printf("head-%d\n",head); +printf("next-%d\n",head->next); +printf("next-%d\n",head->next->next); + +printf("\n\n"); +m_realloc(a,4); +printf("head->size-%d\n",head->size); +printf("head-> -%d\n",head); +printf("head->next-%d\n",head->next); + +printf("\n\n"); +*/ + return 0; + } + +