feat: reuse free blocks and defrag
This commit is contained in:
parent
9be740f2c0
commit
cf91169543
|
@ -7,9 +7,6 @@
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
# include <stdarg.h>
|
# include <stdarg.h>
|
||||||
|
|
||||||
// TODO Remove me (printf)
|
|
||||||
# include <stdio.h>
|
|
||||||
|
|
||||||
#define MAX_ALIGNMENT 16
|
#define MAX_ALIGNMENT 16
|
||||||
|
|
||||||
typedef struct s_block t_block;
|
typedef struct s_block t_block;
|
||||||
|
|
3
main.c
3
main.c
|
@ -12,11 +12,12 @@ int main(int argc, char **argv) {
|
||||||
//free(arr);
|
//free(arr);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < atoi(argv[2]); i++) {
|
for (int i = 0; i < atoi(argv[2]); i++) {
|
||||||
|
// if (rand & 11 == 11)
|
||||||
free(arr[i]);
|
free(arr[i]);
|
||||||
}
|
}
|
||||||
ft_printf("second round\n");
|
ft_printf("second round\n");
|
||||||
for (int i = 0; i < atoi(argv[2]); i++) {
|
for (int i = 0; i < atoi(argv[2]); i++) {
|
||||||
size = (rand() & 0x7ff);// | 0xf00000;
|
size = (rand() & 0x7ff) | 0xf000;
|
||||||
arr[i] = (int *)malloc(size * sizeof(int));
|
arr[i] = (int *)malloc(size * sizeof(int));
|
||||||
ft_printf ("arr %d addr: %p arr size: %d\n", i, arr[i], size);
|
ft_printf ("arr %d addr: %p arr size: %d\n", i, arr[i], size);
|
||||||
//free(arr);
|
//free(arr);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "malloc.h"
|
#include "malloc.h"
|
||||||
|
|
||||||
void free(void *ptr) {
|
void free(void *ptr) {
|
||||||
ft_printf("free %p\n", ptr);
|
// ft_printf("free %p\n", ptr);
|
||||||
pthread_mutex_lock(&g_malloc_mutex);
|
pthread_mutex_lock(&g_malloc_mutex);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
ft_printf("free null\n");
|
ft_printf("free null\n");
|
||||||
|
|
27
src/malloc.c
27
src/malloc.c
|
@ -14,6 +14,17 @@ t_zone *create_new_zone(size_t size) {
|
||||||
return zone;
|
return zone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void defrag(t_block **block) {
|
||||||
|
ft_printf("defragging !!! sizeof(t_block) == %d\n", sizeof(t_block));
|
||||||
|
t_block *curr = *block;
|
||||||
|
t_block *to_defrag = curr->next;
|
||||||
|
|
||||||
|
curr->next = to_defrag->next;
|
||||||
|
// TODO pointer arithmetic good practices
|
||||||
|
curr->size = to_defrag->size + (size_t)(char *)(to_defrag - curr);
|
||||||
|
ft_printf("done\n");
|
||||||
|
}
|
||||||
|
|
||||||
void *malloc_block(size_t size, size_t zone_size, t_zone *zone) {
|
void *malloc_block(size_t size, size_t zone_size, t_zone *zone) {
|
||||||
while (1) {
|
while (1) {
|
||||||
t_block *curr = zone->head;
|
t_block *curr = zone->head;
|
||||||
|
@ -33,8 +44,20 @@ void *malloc_block(size_t size, size_t zone_size, t_zone *zone) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
while (curr->next) {
|
while (curr->next) {
|
||||||
// todo check if free and defrag
|
if (curr->free == 1 && curr->next->free == 1) {
|
||||||
curr = curr->next;
|
defrag(&curr);
|
||||||
|
ft_printf("curr %p curr->next %p\n", curr, curr->next);
|
||||||
|
// alloc if size is large enough
|
||||||
|
//if (curr->size >= size)
|
||||||
|
} else {
|
||||||
|
curr = curr->next;
|
||||||
|
}
|
||||||
|
if (curr->free == 1 && curr->size >= size) {
|
||||||
|
write(1, "good\n", 5);
|
||||||
|
curr->size = size;
|
||||||
|
curr->free = 0;
|
||||||
|
return (void *)((char *)curr+ sizeof(t_block));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// TODO size_t should be replaced by char * for pointer arithmetic
|
// TODO size_t should be replaced by char * for pointer arithmetic
|
||||||
size_t addr = (size_t)curr + sizeof(t_block) + curr->size;
|
size_t addr = (size_t)curr + sizeof(t_block) + curr->size;
|
||||||
|
|
Loading…
Reference in New Issue