diff --git a/include/malloc.h b/include/malloc.h index 4eecd75..2451f36 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -4,4 +4,5 @@ void ft_free(void *ptr); void *ft_malloc(size_t size); -void *ft_realloc(void *ptr, size_t size); \ No newline at end of file +void *ft_realloc(void *ptr, size_t size); +void show_alloc_mem(); \ No newline at end of file diff --git a/src/chunk.c b/src/chunk.c index 8ad34e2..a80ef4d 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -1,8 +1,10 @@ #include #include +#include #include "./chunk.h" #include "./align.h" +#include "print.h" void chunk_write(const chunk_t *chunk) { @@ -48,4 +50,14 @@ void init_chunk(chunk_t *chunk, size_t size, long block_id, void *prev, void *cu size_t get_physical_size(const chunk_t *chunk) { return chunk->data_start + chunk->size - chunk->current; +} + +void chunk_print(const chunk_t *chunk) +{ + putnbr_base((size_t) chunk->data_start, HEXA); + putstr(" - "); + putnbr_base((size_t) chunk->data_start + chunk->size, HEXA); + putstr(" : "); + putnbr_base(chunk->size, DECIMAL); + putstr("bytes\n"); } \ No newline at end of file diff --git a/src/chunk.h b/src/chunk.h index 88b1db9..ee61d91 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -34,4 +34,5 @@ enum { void chunk_read(void * const *block, chunk_t *chunk); void chunk_write(const chunk_t *chunk); void init_chunk(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used); -size_t get_physical_size(const chunk_t *chunk); \ No newline at end of file +size_t get_physical_size(const chunk_t *chunk); +void chunk_print(const chunk_t *chunk); \ No newline at end of file diff --git a/src/chunk_manager.c b/src/chunk_manager.c index 475cfa2..dae1357 100644 --- a/src/chunk_manager.c +++ b/src/chunk_manager.c @@ -41,6 +41,19 @@ int chunk_split(chunk_t *chunk, chunk_t *new_chunk, size_t new_size) return 0; } +void chunk_iter(chunk_t *root, void (*f)(chunk_t*)) +{ + chunk_t *current = root; + + while (current) + { + f(current); + if (current->next == NULL) + break; + chunk_read(current->next, current); + } +} + void destroy_chunk(chunk_t *chunk) { chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk; diff --git a/src/chunk_manager.h b/src/chunk_manager.h index 7ba5790..86d1dd7 100644 --- a/src/chunk_manager.h +++ b/src/chunk_manager.h @@ -2,4 +2,5 @@ #include "chunk.h" -void destroy_chunk(chunk_t *chunk); \ No newline at end of file +void destroy_chunk(chunk_t *chunk); +void chunk_iter(chunk_t *root, void (*f)(chunk_t*)); \ No newline at end of file diff --git a/src/malloc.c b/src/malloc.c index d34ef1d..a620441 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -4,12 +4,14 @@ #include #include -#include "./size.h" +#include "size.h" #include "block.h" #include "raw_chunk_manager.h" #include "chunk_manager.h" #include "malloc.h" +void* allocs_tree[3] = { 0, 0, 0}; + void *alloc_tiny(size_t size) { (void)size; diff --git a/src/malloc.h b/src/malloc.h index 1f88c81..0b528a5 100644 --- a/src/malloc.h +++ b/src/malloc.h @@ -1,3 +1,3 @@ #pragma once -void* allocs_tree[3] = { 0, 0, 0}; \ No newline at end of file +extern void* allocs_tree[3]; \ No newline at end of file diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..da3830a --- /dev/null +++ b/src/print.c @@ -0,0 +1,23 @@ +#include + +static size_t ft_strlen(const char *str) +{ + size_t i = 0; + + for (; str[i]; i++); + return i; +} + +void putstr(const char* str) +{ + write(1, str, ft_strlen(str)); +} + +void putnbr_base(size_t nbr, const char *base) +{ + size_t base_size = ft_strlen(base); + + if (nbr > base_size -1) + putnbr_base(nbr / base_size, base); + write(1, base + (nbr % base_size), 1); +} \ No newline at end of file diff --git a/src/print.h b/src/print.h new file mode 100644 index 0000000..7ed83e4 --- /dev/null +++ b/src/print.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +#define DECIMAL "0123456789" +#define HEXA "0123456789ABCDEF" + +void putnbr_base(size_t nbr, const char *base); +void putstr(const char *str); \ No newline at end of file diff --git a/src/raw_chunk_manager.c b/src/raw_chunk_manager.c index 21bb197..0a8f8b7 100644 --- a/src/raw_chunk_manager.c +++ b/src/raw_chunk_manager.c @@ -1,4 +1,5 @@ #include "chunk.h" +#include "./malloc.h" void *raw_get_last_chunk(void **root) { diff --git a/src/show_alloc_mem.c b/src/show_alloc_mem.c new file mode 100644 index 0000000..5a23375 --- /dev/null +++ b/src/show_alloc_mem.c @@ -0,0 +1,23 @@ +#include "chunk.h" +#include "chunk_manager.h" +#include "./malloc.h" +#include "print.h" + +void show_alloc_mem() +{ + static const char *ZONE_NAME[3] = {"TINY", "SMALL", "LARGE"}; + chunk_t current; + + for (size_t i = TINY; i <= LARGE; i++) + { + if (allocs_tree[i] == NULL) + continue; + putstr(ZONE_NAME[i]); + putstr(" : "); + putnbr_base((size_t) allocs_tree[i], HEXA); + putstr("\n"); + chunk_read(allocs_tree[i], ¤t); + chunk_iter(¤t, (void (*)(chunk_t *)) &chunk_print); + } + +} \ No newline at end of file