diff --git a/malloc/src/helpers/helpers.c b/malloc/src/helpers/helpers.c index 928234e..df0e7fa 100644 --- a/malloc/src/helpers/helpers.c +++ b/malloc/src/helpers/helpers.c @@ -45,7 +45,7 @@ void *find_free_block(struct bucket *buck) for (unsigned j = 0; j < 8; j <<= 1) { 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; } @@ -67,14 +67,15 @@ struct bucket *create_bucket(size_t min_alloc_size) // Init bucket struct bucket *header = page; - header->page = page + sizeof(struct bucket); header->block_size = alloc_size; memset(header->free_list, 0, FREE_LIST_SIZE); 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) { @@ -84,3 +85,32 @@ struct bucket *get_free_bucket(struct bucket *head, size_t size) 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; +} diff --git a/malloc/src/helpers/helpers.h b/malloc/src/helpers/helpers.h index c6c622c..217f7e9 100644 --- a/malloc/src/helpers/helpers.h +++ b/malloc/src/helpers/helpers.h @@ -14,17 +14,18 @@ struct bucket { unsigned block_size; unsigned char free_list[FREE_LIST_SIZE]; // 4096 / MIN_BLOCK_SIZE / - // sizeof(unsigned char) - char *page; - + // sizeof(unsigned char) struct bucket *next; size_t checksum; }; -size_t get_next_2power(size_t n); +size_t s2p(size_t n); void *find_free_block(struct bucket *buck); 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 diff --git a/malloc/src/malloc.c b/malloc/src/malloc.c index 4419fdb..3f19f56 100644 --- a/malloc/src/malloc.c +++ b/malloc/src/malloc.c @@ -9,16 +9,32 @@ __attribute__((visibility("default"))) void *malloc(size_t size) if (size == 0) return NULL; - struct bucket *buck = get_free_bucket(head, size); - if (buck == NULL) + // Find adequate bucket + struct bucket *buck = get_bucket(head, size); + while (buck != NULL) { - buck = create_bucket(size); - return ...; + // Find adequate block + 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) -{} +{ + if (ptr == NULL) + return; +} __attribute__((visibility("default"))) void *realloc(void *ptr, size_t size) {