diff --git a/malloc/.gitignore b/malloc/.gitignore new file mode 100644 index 00000000..0b2c88d2 --- /dev/null +++ b/malloc/.gitignore @@ -0,0 +1,2 @@ +myprogram +*.o diff --git a/malloc/includes/malloc.h b/malloc/includes/malloc.h index b9725805..71aa48e8 100644 --- a/malloc/includes/malloc.h +++ b/malloc/includes/malloc.h @@ -2,7 +2,7 @@ # define MALLOC_H # define malloc_n 64 -# define malloc_m 512 +# define malloc_m 1024 # define malloc_N (1 * getpagesize()) # define malloc_M (2 * getpagesize()) # define malloc_magic 1234567 diff --git a/malloc/libft_malloc_x86_64_Darwin.so b/malloc/libft_malloc_x86_64_Darwin.so index 4ef82e3d..0242ce53 100755 Binary files a/malloc/libft_malloc_x86_64_Darwin.so and b/malloc/libft_malloc_x86_64_Darwin.so differ diff --git a/malloc/main.c b/malloc/main.c index 28cbc7bc..7f8ed0de 100644 --- a/malloc/main.c +++ b/malloc/main.c @@ -6,11 +6,11 @@ int main(void) printf("sizeof(long)=[%lu]\n", sizeof(long)); printf("sizeof(t_node)=[%lu]\n", sizeof(t_node)); - void *ptr0 = malloc(16 * sizeof(long)); + void *ptr0 = malloc(8190 * sizeof(long)); show_alloc_mem(); printf("\n"); - void *ptr1 = malloc(32 * sizeof(long)); + void *ptr1 = malloc(300 * sizeof(long)); show_alloc_mem(); printf("\n"); @@ -22,9 +22,9 @@ int main(void) /* show_alloc_mem(); */ /* printf("\n"); */ - free(ptr0); - show_alloc_mem(); - printf("\n"); + /* free(ptr0); */ + /* show_alloc_mem(); */ + /* printf("\n"); */ free(ptr1); show_alloc_mem(); diff --git a/malloc/myprogram b/malloc/myprogram deleted file mode 100755 index 23304706..00000000 Binary files a/malloc/myprogram and /dev/null differ diff --git a/malloc/src/malloc.c b/malloc/src/malloc.c index 58311c28..b89c8b73 100644 --- a/malloc/src/malloc.c +++ b/malloc/src/malloc.c @@ -7,18 +7,22 @@ t_node *small_alloc = NULL; t_node **find_node(t_node **node, size_t size) { - while (*node && (size_t)(*node)->size < sizeof(t_node) + size) + while (*node && (size_t)(*node)->size < size + HEADER_SIZE) node = &(*node)->next; return (node); } void add_chunk(t_node **node_ref, size_t size) { + size_t chunk_size; + while (*node_ref) node_ref = &(*node_ref)->next; - *node_ref = mmap(NULL, malloc_N, PROT_READ|PROT_WRITE, + chunk_size = TINY(size) ? malloc_N : malloc_M; + printf("chunk_size=[%zu]\n", chunk_size); + *node_ref = mmap(NULL, (chunk_size +1) * 8, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); - (*node_ref)->size = TINY(size) ? malloc_N : malloc_M; + (*node_ref)->size = chunk_size; (*node_ref)->next = NULL; }