diff --git a/src/free.c b/src/free.c index e6e4cd4..2372609 100644 --- a/src/free.c +++ b/src/free.c @@ -27,13 +27,25 @@ void _free(void *ptr) { map->prev->next = map->next; if (map->next) map->next->prev = map->prev; - if (map == g_mmap) - g_mmap = map->next; + // FIX: this cause page reclaims to skyrocket + /* if (map == g_mmap) */ + /* g_mmap = map->next; */ + t_mmap *gmmap = g_mmap; + (void)gmmap; + if (map == g_mmap) { + /* gmmap = map->next; */ + if (map->next) + g_mmap = map->next; + else + g_mmap = NULL; + } munmap(map, map->size + sizeof(t_mmap)); } } void free(void *ptr) { + (void)ptr; + /* return; */ if (LOGGING) { ft_dprintf(tmpfd(), "free(%p)\n", ptr); /* flog("free(): ", (size_t)ptr); */ diff --git a/src/mmap.c b/src/mmap.c index a9f1ea0..2e9137d 100644 --- a/src/mmap.c +++ b/src/mmap.c @@ -17,8 +17,9 @@ t_type get_mmap_type(size_t size) { } t_mmap *new_mmap(size_t size) { - t_mmap *map = mmap(NULL, get_mmap_size(size) + sizeof(t_mmap), - PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + t_mmap *map = + mmap(NULL, get_mmap_size(size) + sizeof(t_mmap), PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!map) { errno = ENOMEM; return NULL; diff --git a/test0 b/test0 new file mode 100755 index 0000000..7d3b667 Binary files /dev/null and b/test0 differ diff --git a/test0.c b/test0.c new file mode 100644 index 0000000..6a4739b --- /dev/null +++ b/test0.c @@ -0,0 +1,14 @@ +#include + +int main(void) +{ + int i; + char *addr; + + i = 0; + while (i < 1024) + { + i++; + } + return (0); +} diff --git a/test1 b/test1 new file mode 100755 index 0000000..27838c7 Binary files /dev/null and b/test1 differ diff --git a/test1.c b/test1.c new file mode 100644 index 0000000..0bb03c8 --- /dev/null +++ b/test1.c @@ -0,0 +1,28 @@ +#include +#include +#include + +void print(char *s) +{ + write(1, s, strlen(s)); +} + +int main(void) +{ + int i; + char *addr; + + i = 0; + while (i < 1024) + { + addr = (char*)malloc(1024); + if (addr == NULL) + { + print("Failed to allocate memory\n"); + return (1); + } + addr[0] = 42; + i++; + } + return (0); +} diff --git a/test2 b/test2 new file mode 100755 index 0000000..fd15d65 Binary files /dev/null and b/test2 differ diff --git a/test2.c b/test2.c new file mode 100644 index 0000000..114bfea --- /dev/null +++ b/test2.c @@ -0,0 +1,24 @@ +#include +#include +#include + +void print(char *s) { write(1, s, strlen(s)); } + +void test(void *p); +int main(void) { + int i; + char *addr; + + i = 0; + while (i < 1024) { + addr = (char *)malloc(1024); + if (addr == NULL) { + print("Failed to allocate memory\n"); + return (1); + } + addr[0] = 42; + free(addr); + i++; + } + return (0); +} diff --git a/test3 b/test3 new file mode 100755 index 0000000..8ba2e25 Binary files /dev/null and b/test3 differ diff --git a/test3.c b/test3.c new file mode 100644 index 0000000..129e213 --- /dev/null +++ b/test3.c @@ -0,0 +1,41 @@ +#include +#include +#include + +#define M (1024 * 1024) + +void print(char *s) +{ + write(1, s, strlen(s)); +} + +int main() +{ + char *addr1; + char *addr2; + char *addr3; + + addr1 = (char*)malloc(16*M); + if (addr1 == NULL) + { + print("Failed to allocate memory\n"); + exit(1); + } + strcpy(addr1, "Hello world!\n"); + print(addr1); + addr2 = (char*)malloc(16*M); + if (addr2 == NULL) + { + print("Failed to allocate memory\n"); + exit(1); + } + addr3 = (char*)realloc(addr1, 128*M); + if (addr3 == NULL) + { + print("Failed to reallocate memory\n"); + exit(1); + } + addr3[127*M] = 42; + print(addr3); + return (0); +} diff --git a/test4 b/test4 new file mode 100755 index 0000000..f92ac17 Binary files /dev/null and b/test4 differ diff --git a/test4.c b/test4.c new file mode 100644 index 0000000..f79da8c --- /dev/null +++ b/test4.c @@ -0,0 +1,26 @@ +#include + +#define M (1024 * 1024) + +int main() +{ + malloc(1); + malloc(2); + malloc(4); + malloc(8); + malloc(16); + malloc(32); + malloc(64); + malloc(128); + malloc(256); + malloc(512); + malloc(1024); + malloc(1024 * 2); + malloc(1024 * 4); + malloc(1024 * 32); + malloc(M); + malloc(16*M); + malloc(128*M); + show_alloc_mem(); + return (0); +} diff --git a/test5 b/test5 new file mode 100755 index 0000000..5f66d25 Binary files /dev/null and b/test5 differ diff --git a/test5.c b/test5.c new file mode 100644 index 0000000..01c2f63 --- /dev/null +++ b/test5.c @@ -0,0 +1,34 @@ +#include +#include +#include + +void print(char *s) +{ + write(1, s, strlen(s)); +} + +int main() +{ + int i; + int alignment; + char *addr; + + i = 1; + alignment = 2 * sizeof(size_t); + while (i <= 100) + { + addr = (char*)malloc(i); + if (addr == NULL) + { + print("Failed to allocate memory\n"); + exit(1); + } + if ((((unsigned long) (addr)) % alignment) != 0) + { + print("malloc returned a non aligned boundary\n"); + exit(1); + } + i++; + free(addr); + } +}