Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Sep 29, 2023
1 parent 5551e7c commit 3d78691
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ OBJS = $(SRCS:.c=.o)
DEPS = $(SRCS:.c=.d)

CC = cc
CFLAGS = -Wall -Wextra -Werror -MMD -fPIC -pthread
CFLAGS = -Wall -Wextra -Werror -MMD -fPIC -pthread -pedantic -fsanitize=address

HOSTNAME := $(shell hostname)

Expand Down Expand Up @@ -38,7 +38,7 @@ re: fclean all
rere: ffclean all

test: $(NAME)
$(CC) -g3 -Werror -Wall -Wextra -MMD -O0 -o test test.c -L. -lft_malloc -Wl,-rpath=.
$(CC) -g3 -Werror -Wall -Wextra -MMD -O0 -o test test.c ./$(NAME) $(LIBFT)

$(LIBFT):
make -C libft
Expand Down
5 changes: 3 additions & 2 deletions include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#define LOGGING false
#define LOGFILE "/tmp/malloc.log"

#define MMAP_SHIFT(mmap) ((void *)mmap + sizeof(t_mmap))
#define ALLOC_SHIFT(alloc) ((void *)alloc + sizeof(t_alloc))
#define MMAP_SHIFT(mmap) ((size_t)mmap + sizeof(t_mmap))
#define ALLOC_SHIFT(alloc) ((size_t)alloc + sizeof(t_alloc))

#define ALIGNMENT 16 // 8 on 32 bits, 16 on 64 bits
#define TINY_MMAP_SIZE (4 * getpagesize())
Expand Down Expand Up @@ -72,6 +72,7 @@ void *calloc(size_t nmemb, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size);

// alloc.c
void *alignp(size_t size);
t_alloc *find_alloc_ptr(void *ptr);
size_t get_alloc_size(size_t size);
t_alloc *find_alloc(size_t size);
Expand Down
29 changes: 15 additions & 14 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
// alloc
t_alloc *find_alloc_ptr(void *ptr) {
for (t_mmap *mmap = g_mmap; mmap; mmap = mmap->next) {
if (ptr < (void *)mmap || ptr > MMAP_SHIFT(mmap) + mmap->size)
if ((size_t)ptr < (size_t)mmap ||
(size_t)ptr > MMAP_SHIFT(mmap) + mmap->size)
continue;
for (t_alloc *alloc = mmap->alloc; alloc; alloc = alloc->next) {
if (ptr > ALLOC_SHIFT(alloc) + alloc->size)
if ((size_t)ptr > ALLOC_SHIFT(alloc) + alloc->size)
continue;
if (ptr < (void *)alloc)
if ((size_t)ptr < (size_t)alloc)
return NULL;
if (ptr < ALLOC_SHIFT(alloc) + alloc->size && ptr >= ALLOC_SHIFT(alloc))
if ((size_t)ptr < ALLOC_SHIFT(alloc) + alloc->size &&
(size_t)ptr >= ALLOC_SHIFT(alloc))
return alloc;
return NULL;
}
}
return NULL;
}

void *alignp(void *size) {
void *alignp(size_t size) {
intptr_t s = (intptr_t)size;
while (s % ALIGNMENT != 0)
s++;
Expand Down Expand Up @@ -61,27 +63,26 @@ t_alloc *find_alloc(size_t size) {
if (!g_mmap)
return NULL;
for (t_mmap *mmap = g_mmap; mmap; mmap = mmap->next) {
if (mmap->type != get_mmap_type(size))
if (mmap->type != get_mmap_type(size - ALIGNMENT))
continue;

// in case it's the first allocation of a mmap
if (!mmap->alloc)
return new_alloc(mmap, alignp(MMAP_SHIFT(mmap)), size);
return new_alloc(mmap, (void *)MMAP_SHIFT(mmap), size);

t_alloc *alloc = mmap->alloc;
void *ptr = alignp(MMAP_SHIFT(mmap));
// + ALIGNMENT because we need to have space to align the alloc
size_t needed_space = size + sizeof(t_alloc) + ALIGNMENT;
void *ptr = (void *)MMAP_SHIFT(mmap);
size_t needed_space = size + sizeof(t_alloc);
while (alloc) { // seach for space between allocated spaces
size_t space = (void *)alloc - ptr;
if (ptr < (void *)alloc && space >= needed_space)
size_t space = (size_t)alloc - (size_t)ptr;
if ((size_t)ptr < (size_t)alloc && space >= needed_space)
return new_alloc(mmap, ptr, size);

ptr = alignp(ALLOC_SHIFT(alloc) + alloc->size);
ptr = (void *)((size_t)ALLOC_SHIFT(alloc) + alloc->size);
alloc = alloc->next;
}
// allocated space after the last allocated space
if ((size_t)(MMAP_SHIFT(mmap) + mmap->size - ptr) >= needed_space)
if ((size_t)(MMAP_SHIFT(mmap) + mmap->size - (size_t)ptr) >= needed_space)
return new_alloc(mmap, ptr, size);
}
return NULL;
Expand Down
8 changes: 5 additions & 3 deletions src/calloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void *calloc(size_t nmemb, size_t size) {
return NULL;
}

if (nmemb * size == 0)
size = nmemb * size;
if (size == 0)
size = 1;
while (size % ALIGNMENT != 0)
size++;
Expand All @@ -19,8 +20,9 @@ void *calloc(size_t nmemb, size_t size) {
/* flog("calloc(): ", size * nmemb); */
}
lock_mutex();
void *res = _malloc(nmemb * size);
void *res = _malloc(size);
unlock_mutex();
ft_bzero(res, nmemb * size);
if (res)
ft_bzero(res, size);
return res;
}
11 changes: 8 additions & 3 deletions src/malloc.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "../include/malloc.h"

void *_malloc(size_t size) {
size += ALIGNMENT;
t_alloc *res = find_alloc(size);
if (res)
return ALLOC_SHIFT(res);
new_mmap(size);
return alignp(ALLOC_SHIFT(res));
new_mmap(size - ALIGNMENT);
res = find_alloc(size);
return ALLOC_SHIFT(res);
if (!res)
return NULL;
return alignp(ALLOC_SHIFT(res));
}

void *malloc(size_t size) {
Expand All @@ -21,5 +24,7 @@ void *malloc(size_t size) {
lock_mutex();
void *res = _malloc(size);
unlock_mutex();
if (!res)
ft_printf("malloc returned null\n");
return res;
}
4 changes: 2 additions & 2 deletions src/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

size_t get_mmap_size(size_t size) {
if (size <= TINY_ALLOC_SIZE)
return TINY_MMAP_SIZE + ALIGNMENT;
return TINY_MMAP_SIZE;
if (size <= SMALL_ALLOC_SIZE)
return SMALL_MMAP_SIZE + ALIGNMENT;
return SMALL_MMAP_SIZE;
return size + sizeof(t_alloc) + ALIGNMENT;
}

Expand Down
11 changes: 6 additions & 5 deletions src/realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
void *_realloc(void *ptr, size_t size) {
if (!ptr || !size)
return NULL;
size += ALIGNMENT;
/* t_alloc *alloc = ptr - sizeof(t_alloc); */
t_alloc *alloc = find_alloc_ptr(ptr);
if (!alloc) {
Expand All @@ -17,20 +18,20 @@ void *_realloc(void *ptr, size_t size) {
// reduce space
if (alloc->size >= size) {
alloc->size = size;
return ALLOC_SHIFT(alloc);
return ptr;
}

// find bigger space
if (alloc->next && (size_t)alloc->next - (size_t)ptr >= size) {
alloc->size = size;
return ALLOC_SHIFT(alloc);
return alignp(ALLOC_SHIFT(alloc));
} else if (!alloc->next &&
(size_t)MMAP_SHIFT(map) + map->size - (size_t)ptr >= size) {
alloc->size = size;
return ALLOC_SHIFT(alloc);
return alignp(ALLOC_SHIFT(alloc));
} else {
void *new = _malloc(size);
ft_memcpy(new, ALLOC_SHIFT(alloc), alloc->size);
void *new = _malloc(size - ALIGNMENT);
ft_memcpy(new, alignp(ALLOC_SHIFT(alloc)), alloc->size);
_free(ptr);
return new;
}
Expand Down
6 changes: 3 additions & 3 deletions src/show_alloc_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void asciidump(void *ptr, size_t size) {
b = *((unsigned char *)ptr + i);
if (ft_isprint(b))
ft_printf("%c", b);
else if (is_end(ptr + i + 1, size - i - 1)) {
else if (is_end((void *)((size_t)ptr + i + 1), size - i - 1)) {
ft_printf("\\0");
return;
} else
Expand Down Expand Up @@ -83,7 +83,7 @@ void show_alloc_mem_asciidump() {
continue;
ft_printf("%p - %p : %d bytes\n", ALLOC_SHIFT(alloc),
ALLOC_SHIFT(alloc) + alloc->size, alloc->size);
asciidump(ALLOC_SHIFT(alloc), alloc->size);
asciidump((void *)ALLOC_SHIFT(alloc), alloc->size);
ft_putstr("\n\n");
if (alloc->next == alloc) {
ft_printf("detected infinite loop on alloc at %p\n",
Expand Down Expand Up @@ -112,7 +112,7 @@ void show_alloc_mem_hexdump() {
continue;
ft_printf("%p - %p : %d bytes\n", ALLOC_SHIFT(alloc),
ALLOC_SHIFT(alloc) + alloc->size, alloc->size);
hexdump(ALLOC_SHIFT(alloc), alloc->size);
hexdump((void *)ALLOC_SHIFT(alloc), alloc->size);
ft_putstr("\n\n");
if (alloc->next == alloc) {
ft_printf("detected infinite loop on alloc at %p\n",
Expand Down
13 changes: 8 additions & 5 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,35 @@
#include <string.h>

int main() {
// 1. Basic functionality tests
char *str = (char *)malloc(20);
assert(str != NULL);
strcpy(str, "42helloworld");
ft_strlcpy(str, "42helloworld", 20);
assert(strcmp(str, "42helloworld") == 0);
ft_bzero(str, 20);
free(str);

int *arr = (int *)calloc(5, sizeof(int));
assert(arr != NULL);
for (int i = 0; i < 5; i++) {
assert(arr[i] == 0);
}
ft_bzero(arr, 5 * sizeof(int));
free(arr);

char *dynstr = (char *)malloc(5);
assert(dynstr != NULL);
strcpy(dynstr, "Test");
ft_strlcpy(dynstr, "Test", 5);
dynstr = realloc(dynstr, 10);
strcat(dynstr, "1234");
assert(strcmp(dynstr, "Test1234") == 0);
ft_strlcat(dynstr, "1234", 10);
assert(ft_strcmp(dynstr, "Test1234") == 0);
ft_bzero(dynstr, 10);
free(dynstr);

void *edge1 = malloc(0);
free(edge1);

char *edge2 = (char *)malloc(5);
ft_bzero(edge2, 5);
edge2 = realloc(edge2, 0);
assert(edge2 == NULL);

Expand Down

0 comments on commit 3d78691

Please sign in to comment.