diff --git a/.input.txt.swp b/.input.txt.swp new file mode 100644 index 0000000000000000000000000000000000000000..9a97ec4070cea56a053d24c7005020531f48f100 Binary files /dev/null and b/.input.txt.swp differ diff --git a/alloc.c b/alloc.c index 931b9fd59da6368f6a48cfa9f681133f918b162b..71854fbdc0bd6dcab32b3289927a9df33bd530fd 100644 --- a/alloc.c +++ b/alloc.c @@ -1 +1,66 @@ #include "alloc.h" + +#include <stdio.h> +#include <sys/types.h> + +void* start = 0; +void* end = 0; +extern int fit_type = 1; + + +void* m_malloc(size_t size) +{ + meta* block; + + if(start == 0) + { + start = sbrk(0); + end = start; + } + + + if(end == start) + { + block = -1; + } + + + + meta* nextblk = end; + end += size + sizeof(meta); + + if(sbrk(size+sizeof(meta)) == -1) + { + return 0; + } + nextblk->free=0; + nextblk->next=0; + nextblk->prev = block; + nextblk->size = size; + + block = nextblk; + + + printf("%d %d ",nextblk->free,nextblk->size); + + + return block->data; +} + +void m_free(void *ptr) +{ + + ptr = ptr-sizeof(meta); + + if(!ptr) + { + printf("NULL\n"); + return; + } +} + + +void* m_realloc(void* ptr,size_t size){ + +//GG +} \ No newline at end of file diff --git a/alloc.h b/alloc.h index 3245f55c3496bc75c70c04625ca3dcb7dd5d0afd..b53aed17eda01e09cf898154440fd0e510de1f86 100644 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,26 @@ -#ifndef _ALLOC_H_ -#define _ALLOC_H_ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdbool.h> +#include <stdint.h> typedef struct meta_struct { + uint32_t size; + uint32_t free; + + struct meta_struct* next; + struct meta_struct* prev; + + char data[1]; + } meta; -#endif + +void* m_malloc(size_t size); + +void m_free(void* ptr); + +void* m_realloc(void* ptr,size_t size); + + 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..47dd08ec469c02b29654aecca7f6a59ad652d804 Binary files /dev/null and b/main differ diff --git a/main.c b/main.c index b5fba0682b01a1005462aa24bf4ac2f21227e156..57dcb86a968a381885cc3e74b3eaa2247e5640cb 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,66 @@ #include "alloc.h" -int main() +#include <string.h> +#include <stdio.h> + + +extern int fit_type; + +int main(int argc,char* argv[]) { - return 0; -} + char buf[1024]; + char* tok; + int line_num; // 1,2,3,4,5 + int fit_type=0; + + + FILE* fp = fopen(argv[1],"r"); + + fgets(buf,sizeof(buf),fp); + tok = strtok(buf, " "); + + line_num = atoi(tok); + if(line_num==0) + { + printf("line number is zero\n"); + return 0; + } + + tok = strtok(NULL, " "); + if(tok[0]=='F') + fit_type = 1; + else if(tok[0]=='B') + fit_type = 2; + else if(tok[0]=='W') + fit_type = 3; + else + { + printf("fit type is not correct\n"); + return 0; + } + + char** lines = (char**)malloc(line_num * sizeof(char*)); + + for(int i=0;i<line_num;i++) + { + memset(buf,0,sizeof(buf)); + fgets(buf,sizeof(buf),fp); + buf[strlen(buf)-1]=0; + + if(buf[0]=='s') + { + lines[i] = (char*)m_malloc(sizeof(char)*strlen(buf+2)+1); + strcpy(lines[i], buf+2); + printf("%s\n", lines[i]); + continue; + } + } + //for(int i=0; i<line_num;i++) + //{ + // printf("%s\n", lines[i]); + //} + + + return 0; + +} \ No newline at end of file