add: show_alloc_mem

This commit is contained in:
starnakin 2024-07-30 13:17:36 +02:00
parent 0c3ba7ca43
commit 02f2caf9e1
11 changed files with 91 additions and 5 deletions

View File

@ -4,4 +4,5 @@
void ft_free(void *ptr); void ft_free(void *ptr);
void *ft_malloc(size_t size); void *ft_malloc(size_t size);
void *ft_realloc(void *ptr, size_t size); void *ft_realloc(void *ptr, size_t size);
void show_alloc_mem();

View File

@ -1,8 +1,10 @@
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h>
#include "./chunk.h" #include "./chunk.h"
#include "./align.h" #include "./align.h"
#include "print.h"
void chunk_write(const chunk_t *chunk) 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) size_t get_physical_size(const chunk_t *chunk)
{ {
return chunk->data_start + chunk->size - chunk->current; 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");
} }

View File

@ -34,4 +34,5 @@ enum {
void chunk_read(void * const *block, chunk_t *chunk); void chunk_read(void * const *block, chunk_t *chunk);
void chunk_write(const 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); 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); size_t get_physical_size(const chunk_t *chunk);
void chunk_print(const chunk_t *chunk);

View File

@ -41,6 +41,19 @@ int chunk_split(chunk_t *chunk, chunk_t *new_chunk, size_t new_size)
return 0; 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) void destroy_chunk(chunk_t *chunk)
{ {
chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk; chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk;

View File

@ -2,4 +2,5 @@
#include "chunk.h" #include "chunk.h"
void destroy_chunk(chunk_t *chunk); void destroy_chunk(chunk_t *chunk);
void chunk_iter(chunk_t *root, void (*f)(chunk_t*));

View File

@ -4,12 +4,14 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include "./size.h" #include "size.h"
#include "block.h" #include "block.h"
#include "raw_chunk_manager.h" #include "raw_chunk_manager.h"
#include "chunk_manager.h" #include "chunk_manager.h"
#include "malloc.h" #include "malloc.h"
void* allocs_tree[3] = { 0, 0, 0};
void *alloc_tiny(size_t size) void *alloc_tiny(size_t size)
{ {
(void)size; (void)size;

View File

@ -1,3 +1,3 @@
#pragma once #pragma once
void* allocs_tree[3] = { 0, 0, 0}; extern void* allocs_tree[3];

23
src/print.c Normal file
View File

@ -0,0 +1,23 @@
#include <unistd.h>
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);
}

9
src/print.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <stddef.h>
#define DECIMAL "0123456789"
#define HEXA "0123456789ABCDEF"
void putnbr_base(size_t nbr, const char *base);
void putstr(const char *str);

View File

@ -1,4 +1,5 @@
#include "chunk.h" #include "chunk.h"
#include "./malloc.h"
void *raw_get_last_chunk(void **root) void *raw_get_last_chunk(void **root)
{ {

23
src/show_alloc_mem.c Normal file
View File

@ -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], &current);
chunk_iter(&current, (void (*)(chunk_t *)) &chunk_print);
}
}