From 7914f92f3ed79116141d1023f525b7236cf52bfd Mon Sep 17 00:00:00 2001
From: HongJiHo <wlghlplp@ajou.ac.kr>
Date: Fri, 15 Jun 2018 07:38:22 -0700
Subject: [PATCH] submission

---
 Makefile |   9 +++++
 alloc.c  | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 alloc.h  |  23 ++++++++++--
 main.c   |  61 +++++++++++++++++++++++++++++++
 4 files changed, 199 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index e69de29..c232842 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 e69de29..a54eaf9 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 24486ad..ce95422 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 e69de29..5097d0b 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;
+}
-- 
GitLab