diff --git a/alloc.c b/alloc.c index 931b9fd59da6368f6a48cfa9f681133f918b162b..8a2d385f8f9f16e3d7d2064e02b89bdce89578f9 100644 --- a/alloc.c +++ b/alloc.c @@ -1 +1,136 @@ #include "alloc.h" +#include <unistd.h> +#include <memory.h> +#include <stdio.h> + +void *m_malloc(size_t size); +void m_free(void *ptr); +void *m_realloc(void *ptr, size_t size); + +meta *meta1 = NULL; +meta *meta2 = NULL; + +int mode = -1; + +void select_mode(char cr){ + if(cr == 'F'){ + mode = 1; + } + else if(cr == 'B'){ + mode = 2; + } + else if(cr == 'W'){ + mode = 3; + } +} + +meta *find_block(size_t size2){ + meta *temp = meta1; + meta *block = NULL; + while(temp != 0){ + if(temp->free != 0 && temp->size >= size2){ + if(mode == 1){ + return temp; + } + else if(mode == 2){ + if(block == 0 || (block->size - size2 > temp->size - size2)){ + block = temp; + } + } + else if(mode == 3){ + if(block == 0 || block->size < temp->size){ + block = temp; + } + } + } + temp = temp->next; + } + return block; +} + +void *m_malloc(size_t size){ + if(mode ==-1||size <=0){ + return NULL; + } + if(meta1 == NULL){ + meta1 = sbrk(size + sizeof(meta)); + meta1->prev = NULL; + meta1->next = NULL; + meta1->free = 0; + meta1->size = size; + meta2 = meta1; + return meta1 + sizeof(meta); + } + meta *block = find_block(size); + if(block == 0){ + block = sbrk(size + sizeof(meta)); + block->prev = meta2; + block->next = NULL; + block->free = 0; + block->size = size; + meta2->next = block; + meta2 = block; + return block + sizeof(meta); + } + block->free = 0; + if(block->size - size > sizeof(meta)){ + block->size = size; + meta *block2 = block + sizeof(meta) + size; + block2->prev = block; + block2->next = block->next; + block2->free = 1; + block2->size = block->size - (size + sizeof(meta)); + block->next = block2; + } + return block + sizeof(meta); +} + +void m_free(void *ptr){ + meta *temp = (meta*)ptr - sizeof(meta); + temp->free = 1; + if(temp->prev !=0 && temp->prev->free !=0){ + temp->next->prev = temp->prev; + temp->prev->next = temp->next; + temp->prev->size += temp->size; + temp = temp->prev; + } + + if(temp->next !=0 && temp->next->free !=0){ + temp->size += temp->next->size; + if(temp->next == meta2){ + meta2 = temp; + } + temp->next = temp->next->next; + if(temp->next->next != 0){ + temp->next->next->prev = temp; + } + } + + while(meta2 !=0 && meta2->free !=0){ + meta2 = meta2->prev; + if(meta2 !=0){ + meta2->next = NULL; + } + else{ + meta1 = NULL; + } + + brk(meta2); + } +} + +void *m_realloc(void *ptr, size_t size){ + meta *temp = (meta*)ptr - sizeof(meta); + if(temp->size >= size){ + return ptr; + } + void *new_ptr = m_malloc(size); + memcpy(new_ptr, ptr, temp->size); + m_free(ptr); + return new_ptr; +} + + + + + diff --git a/alloc.h b/alloc.h index 3245f55c3496bc75c70c04625ca3dcb7dd5d0afd..628ebb2b2a6e31994a12d9a0371a65198dbd60ff 100644 --- a/alloc.h +++ b/alloc.h @@ -1,8 +1,16 @@ #ifndef _ALLOC_H_ #define _ALLOC_H_ +#include <unistd.h> typedef struct meta_struct { + struct meta_struct *prev; + struct meta_struct *next; + int free; + size_t size; + +}meta; -} meta; +meta *meta1; +meta *meta2; #endif diff --git a/main b/main index 46ac1c4e7ddb9537dcab3abadc4e6507ba893967..ea9c6baf6268190d1cc43e8ebbddbc30d50bc301 100755 Binary files a/main and b/main differ diff --git a/main.c b/main.c index b5fba0682b01a1005462aa24bf4ac2f21227e156..9c2887960dacd719ccf49e82ac8db013e9e255c6 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,58 @@ #include "alloc.h" +#include <string.h> +#include <stdio.h> +#include <memory.h> + +void *arr1[512]; +int temp; + int main() { - return 0; + int num1; + int cr; + scanf("%d %c", &num1, &cr); + select_mode(cr); + while(num1 != -1){ + char class; + scanf("%c", &class); + if(class == 's'){ + char arr2[512] = {}; + int num2 = 0; + while(1){ + char cr2 = getchar(); + if(cr2 == '\n'){ + break; + } + arr2[num2] = cr2; + num2++; + } + arr1[temp] = m_malloc(num2); + memcpy(arr1[temp], arr2,strlen(arr2)); + temp++; + } + else if(class == 'f'){ + int num2; + int num3; + scanf("%d %d", &num2, &num3); + arr1[num2] = m_realloc(arr1[num2], num3); + } + else if(class == 'r'){ + int num3; + scanf("%d" , &num3); + arr1[temp] = m_malloc(num3); + temp++; + } + num1--; + } + + meta *temp2 = meta1; + while(temp2 !=0){ + printf("%d %d" , temp2->free, temp2->size); + if(temp2->free == 0){ + printf("%s\n", (char*)(temp2 + sizeof(meta))); + } + temp2 = temp2->next; + } + return 0; }