This commit is contained in:
Guillem George 2025-11-06 22:14:56 +01:00
parent 5aba0ff0c3
commit 06fac933c2
3 changed files with 60 additions and 13 deletions

View file

@ -45,7 +45,7 @@ void *find_free_block(struct bucket *buck)
for (unsigned j = 0; j < 8; j <<= 1) for (unsigned j = 0; j < 8; j <<= 1)
{ {
if (map_byte & bit) // Test if free if (map_byte & bit) // Test if free
return buck->page + (buck->block_size * (i * 8 + j)); return get_data_begin(buck) + (buck->block_size * (i * 8 + j));
bit <<= 1; bit <<= 1;
} }
@ -67,14 +67,15 @@ struct bucket *create_bucket(size_t min_alloc_size)
// Init bucket // Init bucket
struct bucket *header = page; struct bucket *header = page;
header->page = page + sizeof(struct bucket);
header->block_size = alloc_size; header->block_size = alloc_size;
memset(header->free_list, 0, FREE_LIST_SIZE); memset(header->free_list, 0, FREE_LIST_SIZE);
return header; return header;
} }
struct bucket *get_free_bucket(struct bucket *head, size_t size) // Gets a bucket with at least a block_size of size
// Returns NULL if nothing was found
struct bucket *get_bucket(struct bucket *head, size_t size)
{ {
while (head != NULL) while (head != NULL)
{ {
@ -84,3 +85,32 @@ struct bucket *get_free_bucket(struct bucket *head, size_t size)
return NULL; return NULL;
} }
void *get_data_begin(struct bucket *biquette)
{
// TODO alignment
return biquette + sizeof(struct bucket);
}
void *get_page_begin(void *ptr)
{
size_t quelenfer = ((size_t)ptr) & ~(PAGE_SIZE - 1);
char *pointeur_alacon = NULL;
return pointeur_alacon + quelenfer;
}
void bucket_append(struct bucket **head, struct bucket *elt)
{
struct bucket *list_elt = *head;
if (list_elt == NULL)
{
*head = elt;
return;
}
while (list_elt->next != NULL)
{
list_elt = list_elt->next;
}
list_elt->next = elt;
}

View file

@ -15,16 +15,17 @@ struct bucket
unsigned block_size; unsigned block_size;
unsigned char free_list[FREE_LIST_SIZE]; // 4096 / MIN_BLOCK_SIZE / unsigned char free_list[FREE_LIST_SIZE]; // 4096 / MIN_BLOCK_SIZE /
// sizeof(unsigned char) // sizeof(unsigned char)
char *page;
struct bucket *next; struct bucket *next;
size_t checksum; size_t checksum;
}; };
size_t get_next_2power(size_t n); size_t s2p(size_t n);
void *find_free_block(struct bucket *buck); void *find_free_block(struct bucket *buck);
struct bucket *create_bucket(size_t min_alloc_size); struct bucket *create_bucket(size_t min_alloc_size);
struct bucket *get_free_bucket(struct bucket *head, size_t size); struct bucket *get_bucket(struct bucket *head, size_t size);
void *get_data_begin(struct bucket *biquette);
void *get_page_begin(void *ptr);
void bucket_append(struct bucket **head, struct bucket *elt);
#endif // ! HELPERS_H #endif // ! HELPERS_H

View file

@ -9,16 +9,32 @@ __attribute__((visibility("default"))) void *malloc(size_t size)
if (size == 0) if (size == 0)
return NULL; return NULL;
struct bucket *buck = get_free_bucket(head, size); // Find adequate bucket
if (buck == NULL) struct bucket *buck = get_bucket(head, size);
while (buck != NULL)
{ {
buck = create_bucket(size); // Find adequate block
return ...; void *block = find_free_block(buck);
if (block != NULL)
return block;
else
{
// No free space -> loop
buck = get_bucket(buck->next, size);
continue;
} }
}
buck = create_bucket(size);
bucket_append(&head, buck);
return get_data_begin(buck);
} }
__attribute__((visibility("default"))) void free(void *ptr) __attribute__((visibility("default"))) void free(void *ptr)
{} {
if (ptr == NULL)
return;
}
__attribute__((visibility("default"))) void *realloc(void *ptr, size_t size) __attribute__((visibility("default"))) void *realloc(void *ptr, size_t size)
{ {