diff --git a/alloc.c b/alloc.c index 931b9fd59da6368f6a48cfa9f681133f918b162b..0f0e0df96e78fef9a61b8b314a810a792e826daf 100644 --- a/alloc.c +++ b/alloc.c @@ -1 +1,157 @@ #include "alloc.h" +//start +#include <stdio.h> +#define _SYS/TYPES_H_ +#include <sys/types.h> + +meta *search_position(size_t size); +extern int FIT = F; +void *init = 0; +void *last = 0; + +void *m_malloc(size_t size){ + meta *search; + if (init == 0){ + init = sbrk(0); + last = init; + } + size_t alloc_size = size + METASIZE; + if (last == init) search = -1; + else search = search_position(size); + + if (search == -1 || (!search->next && (!search->free || search->free && search->size < size))){ + meta *new = last; + last += alloc_size; + if (sbrk(alloc_size) == -1) return 0; + new->free = 0; + new->next = 0; + new->prev = search; + new->size = size; + + if (search != -1) search->next = new; + search = new; + } + else m_realloc(search->data, size); + + return search->data; +} + +void m_free(void *point){ + meta *curr = point - METASIZE; + curr->free = 1; + + if (curr->next && curr->next->free == 1){ + curr->size += curr->next->size + METASIZE; + curr->next = curr->next->next; + } + if (curr->prev != -1){ + if(curr->prev->free){ + curr = curr->prev; + curr->size += curr->next->size + METASIZE; + curr->next = curr->next->next; + } + if (!curr->next){ + last -= curr->size + METASIZE; + curr->prev->next = 0; + } + } + else if (!curr->next && !curr->prev) last = brk(init); + point = 0; +} + +void *m_realloc(void *pointer, size_t size){ + meta *curr = pointer - METASIZE; + if (curr->size == size) return pointer; + else if (curr->size < size){ + if(curr->next && curr->next->free && curr->size + curr->next->size + METASIZE >= size){ + curr->size += curr->next->size + METASIZE; + curr->next = curr->next->next; + curr->next->prev = curr; + if ((curr->size - size) < METASIZE) return pointer; + else { + meta *new = (int)curr + size + METASIZE; + new->prev = curr; + new->next = curr->next; + new->size = curr->size - size - METASIZE; + curr->next = new; + curr->size = size; + curr->free = 0; + m_free(new->data); + return curr->data; + } + } + else { + m_free(curr->data); + void *tmp = m_malloc(size); + strcpy(tmp, pointer); + return tmp; + } + } + else if ((curr->size - size) < METASIZE) return pointer; + else { + meta *new = (int)curr + size + METASIZE; + new->prev = curr; + new->next = curr->next; + new->size = curr->size - size - METASIZE; + curr->next = new; + curr->size = size; + curr->free = 0; + m_free(new->data); + return curr->data; + } +} + +meta *search_position(size_t size){ + meta *fix = init; + meta *position = -1; + + switch(FIT){ + case F: + if (fix == NULL) break; + while(1){ + if (fix->free && fix->size >= size){ + position = fix; + break; + } + if (fix->next == NULL){ + position = fix; + break; + } + fix = fix->next; + } + break; + case B: + if (fix == NULL) break; + while(1){ + if (fix->size = size && fix->free){ + if (position == -1) position = fix; + else if (position->size > fix->size) position = fix; + } + if (position == -1 && !fix->next) position = fix; + fix = fix->next; + } + break; + case W: + if (fix == NULL) break; + while(1){ + if (fix->size >= size && fix->free){ + if (position == -1) position = fix; + else if (position->size < fix->size) position = fix; + } + if (position == -1 && !fix->next) position = fix; + fix = fix->next; + } + break; + } + return position; +} + +void print_alloc(){ + meta *curr = init; + while(curr){ + printf("free : %d / size : %d ",curr->free,curr->size); + if(!curr->free) printf("%s\n",curr->data); + else printf("\n"); + curr = curr->next; + } +} diff --git a/alloc.h b/alloc.h index 3245f55c3496bc75c70c04625ca3dcb7dd5d0afd..c723808ad82cf740e9ed77965513bf7853828507 100644 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,32 @@ #ifndef _ALLOC_H_ #define _ALLOC_H_ +//start +#ifndef _STDINT_H_ +#define _STDINT_H_ +#include <stdint.h> +#endif -typedef struct meta_struct { +#ifndef _SYS/TYPES_H_ +#define _SYS/TYPES_H_ +#include <sys/types.h> +#endif +#define F 1 +#define B 2 +#define W 4 +#define METASIZE 16 + +typedef struct meta_struct { + uint32_t free; + uint32_t size; + struct meta_struct *prev; + struct meta_struct *next; + char data[1]; } meta; +extern int type; +void *m_malloc(size_t size); +void m_free(void *point); +void *m_realloc(void *point, size_t size); + #endif diff --git a/input.txt b/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..1a4ec36b420249faa2ccf8f80467e1593071b991 --- /dev/null +++ b/input.txt @@ -0,0 +1,5 @@ +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. + diff --git a/main b/main new file mode 100755 index 0000000000000000000000000000000000000000..8ac88050a5d3f3c19d54904729c26a1d3bcc18ae Binary files /dev/null and b/main differ diff --git a/main.c b/main.c index b5fba0682b01a1005462aa24bf4ac2f21227e156..ede9f4aa7d79f787a36ece21df15ec6189889685 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,74 @@ #include "alloc.h" +#include <string.h> +#include <stdio.h> -int main() -{ +extern int FIT; +int main(int argc, char *argv[]){ + if (argc < 2){ + printf("Please input 2 argument\n"); + return 0; + } + FILE *fp = fopen(argv[1],"r"); + if (fp == NULL) return 0; + + char buf[4096]; + char *token; + fgets(buf,sizeof(buf),fp); + token = strtok(buf," "); + + int index = atoi(token); + if (index == 0) return 0; + + token = strtok(NULL," "); + switch(token[0]){ + case 'F': + FIT = F; + break; + case 'B': + FIT = B; + break; + case 'W': + FIT = W; + break; + default: + printf("FIT is in F,B,W\n"); + return 0; + } + + char **command = (char**)malloc(sizeof(char*)*index); + int tmp, init=0, resize; + + for (int i=0;i<index;i++){ + memset(buf,'\0',sizeof(buf)); + fgets(buf,sizeof(buf),fp); + buf[strlen(buf)-1]='\0'; + + switch(buf[0]){ + case 'f': + tmp = atoi(buf+2); + m_free(command[tmp]); + break; + case 'e': + tmp = atoi(buf+2); + command[init++]=m_malloc(tmp); + break; + case 'r': + token = strtok(buf+2," "); + tmp = atoi(token); + token = strtok(NULL," "); + resize = atoi(token); + m_realloc(command[tmp],resize); + break; + case 'm': + case 's': + command[init] = m_malloc(strlen(buf+2)+1); + strcpy(command[init++],buf+2); + break; + default: + printf("command error. input f, e, r, m, s\n"); + return 0; + } + } + print_alloc(); return 0; }