Skip to content

Commit

Permalink
gamp
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Sep 15, 2023
1 parent f479dc3 commit 0ff755f
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
libft_malloc*
test
gpttest
a.out
tester
# Prerequisites
*.d

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ re: fclean all
rere: ffclean all

test: $(NAME)
$(CC) $(CFLAGS) -o test test.c -L. -lft_malloc -Wl,-rpath=.
$(CC) $(CFLAGS) -o gpttest gpttest.c -L. -lft_malloc -Wl,-rpath=.
$(CC) $(CFLAGS) -O0 -o test test.c -L. -lft_malloc -Wl,-rpath=.
$(CC) $(CFLAGS) -O0 -o gpttest gpttest.c -L. -lft_malloc -Wl,-rpath=.

$(LIBFT):
make -C libft
Expand Down
1 change: 1 addition & 0 deletions include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define MMAP_SHIFT(mmap) ((void *)mmap + sizeof(t_mmap))
#define ALLOC_SHIFT(alloc) ((void *)alloc + sizeof(t_alloc))

#define ALIGNMENT 16
#define TINY_MMAP_SIZE (4 * getpagesize())
#define TINY_ALLOC_SIZE (128)
#define SMALL_MMAP_SIZE (32 * getpagesize())
Expand Down
20 changes: 14 additions & 6 deletions src/alloc.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "../include/malloc.h"

void *alignp(void *size) {
intptr_t s = (intptr_t)size;
while (s % ALIGNMENT != 0)
s++;
/* ft_printf("before %p, after %p\n", size, (void *)s); */
return (void *)s;
}

t_alloc *new_alloc(t_mmap *mmap, void *ptr, size_t size) {
t_alloc *alloc = ptr;
alloc->size = size;
alloc->parent = mmap;
/* ft_printf("creating new alloc at %p. should return %p\n", ptr, */
/* ALLOC_SHIFT(ptr)); */
alloc->next = NULL;
alloc->prev = NULL;

Expand Down Expand Up @@ -34,24 +40,26 @@ t_alloc *new_alloc(t_mmap *mmap, void *ptr, size_t size) {
}

t_alloc *find_alloc(size_t size) {
size += ALIGNMENT; // add space to align the return ptr
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, MMAP_SHIFT(mmap), size);
return new_alloc(mmap, alignp(MMAP_SHIFT(mmap)), size);
/* return new_alloc(mmap, MMAP_SHIFT(mmap), size); */

t_alloc *alloc = mmap->alloc;
void *ptr = MMAP_SHIFT(mmap);
void *ptr = alignp(MMAP_SHIFT(mmap));
while (alloc) { // seach for space between allocated spaces
size_t space = (void *)alloc - ptr;
if (ptr < (void *)alloc && space >= (size_t)ALLOC_SHIFT(size))
return new_alloc(mmap, ptr, size);

ptr = ALLOC_SHIFT(alloc) + alloc->size;
ptr = alignp(ALLOC_SHIFT(alloc) + alloc->size);
alloc = alloc->next;
}
// allocated space after the last allocated space
Expand Down
23 changes: 10 additions & 13 deletions src/calloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

void *calloc(size_t nmemb, size_t size) {
ft_printf("calloc(%d)", size);
/* size_t overflow = nmemb * size; */
/* if (size && overflow / size != nmemb) { */
/* // TODO: rsyslog() detected overflow */
/* return NULL; */
/* } */
size_t overflow = nmemb * size;
if (size && overflow / size != nmemb) {
ft_printf("OVERFLOW\n");
// TODO: rsyslog() detected overflow
return NULL;
}

/* if (nmemb * size == 0) */
/* return NULL; */
if (nmemb * size == 0)
return NULL;

lock_mutex();
// TODO: rsyslog()
void *res;
if (nmemb * size == 0)
res = _malloc(1);
else
res = _malloc(nmemb * size);
lock_mutex();
void *res = _malloc(nmemb * size);
unlock_mutex();
ft_bzero(res, nmemb * size);
ft_printf(" -> %p\n", res);
Expand Down
9 changes: 4 additions & 5 deletions src/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ void _free(void *ptr) {
return;
if (!g_mmap)
return;
/* t_alloc *alloc = ptr - sizeof(t_alloc); */
t_alloc *alloc = find_alloc_ptr(ptr);
t_alloc *alloc = ptr - sizeof(t_alloc);
/* t_alloc *alloc = find_alloc_ptr(ptr); */
if (!alloc) {
ft_printf("tryed free(%p) but it's not an alloc\n", ptr);
ft_printf("tryed _free(%p) but it's not an alloc\n", ptr);
return;
}
ft_printf("free: (%p)->size %d\n", ptr, alloc->size);
if (alloc->prev)
alloc->prev->next = alloc->next;
if (alloc->next)
Expand All @@ -54,8 +53,8 @@ void free(void *ptr) {
ft_printf("free(%p)\n", ptr);
if (!ptr)
return;
lock_mutex();
// TODO: rsyslog()
lock_mutex();
_free(ptr);
unlock_mutex();
}
9 changes: 6 additions & 3 deletions src/global_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

t_mmap *g_mmap = NULL;

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t *get_mutex() {
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
return &mutex;
}

void lock_mutex() { pthread_mutex_lock(&g_mutex); }
void unlock_mutex() { pthread_mutex_unlock(&g_mutex); }
void lock_mutex() { pthread_mutex_lock(get_mutex()); }
void unlock_mutex() { pthread_mutex_unlock(get_mutex()); }
9 changes: 4 additions & 5 deletions src/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ void *_malloc(size_t size) {
return ALLOC_SHIFT(res);
new_mmap(size);
res = find_alloc(size);
if (!res)
ft_printf("NO SPACE FOUND ON NEW MMAP\n");
return ALLOC_SHIFT(res);
}

void *malloc(size_t size) {
ft_printf("malloc(%d)", size);
/* if (!size) */
/* size = 1; */
if (size == 0)
if (!size)
return NULL;
lock_mutex();
// TODO: rsyslog()
lock_mutex();
void *res = _malloc(size);
unlock_mutex();
ft_printf(" -> %p\n", res);
show_alloc_mem();
return res;
}
11 changes: 5 additions & 6 deletions src/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

t_type get_mmap_type(size_t size) {
Expand All @@ -17,9 +17,8 @@ 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, -1, 0);
t_mmap *map = mmap(NULL, get_mmap_size(size) + sizeof(t_mmap),
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (!map) {
errno = ENOMEM;
return NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ void *_realloc(void *ptr, size_t size) {

void *realloc(void *ptr, size_t size) {
ft_printf("realloc(%d) on %p", size, ptr);
lock_mutex();
// TODO: rsyslog()
lock_mutex();
void *res = NULL;
if (!ptr)
res = _malloc(size);
else if (!size && ptr) {
_free(ptr);
// TODO: should remove this is useless
res = NULL;
} else
res = _realloc(ptr, size);
Expand Down
2 changes: 1 addition & 1 deletion src/reallocarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size) {
}
if (nmemb * size == 0)
return NULL;
lock_mutex();
// TODO: rsyslog()
void *res;
lock_mutex();
if (!ptr)
res = _malloc(size * nmemb);
else if ((size * nmemb) == 0 && ptr) {
Expand Down
5 changes: 4 additions & 1 deletion src/show_alloc_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ char *get_type_string(t_type type) {

void show_alloc_mem() {
if (!g_mmap) {
ft_putstr("No memory allocated\n");
ft_putstr("show_alloc_mem(): No memory allocated\n");
return;
}
size_t total = 0;
for (t_mmap *mmap = g_mmap; mmap; mmap = mmap->next) {
ft_printf("%s : %p\n", get_type_string(mmap->type), mmap);
for (t_alloc *alloc = mmap->alloc; alloc; alloc = alloc->next) {
if (!alloc) {
ft_printf("alloc is NULL\n");
}
ft_printf("%p - %p : %d bytes\n", ALLOC_SHIFT(alloc),
ALLOC_SHIFT(alloc) + alloc->size, alloc->size);
if (alloc->next == alloc) {
Expand Down
68 changes: 39 additions & 29 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
#include "include/malloc.h"
#include "libft/libft.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
int main(void) {
int len = 100;
char *a = malloc(len);
ft_printf("a(%p)\n", a);
ft_bzero(a, len);
char *a1 = malloc(len);
ft_bzero(a1, len);
ft_printf("a(%p)\n", a1);
a = malloc(len);
ft_printf("a(%p)\n", a);
ft_bzero(a, len);
free(a1);
a = malloc(len);
ft_printf("a(%p)\n", a);
ft_bzero(a, len);
/* int len = 100; */
/* char *a = malloc(len); */
/* ft_printf("a(%p)\n", a); */
/* ft_bzero(a, len); */
/* char *a1 = malloc(len); */
/* ft_bzero(a1, len); */
/* ft_printf("a(%p)\n", a1); */
/* a = malloc(len); */
/* ft_printf("a(%p)\n", a); */
/* ft_bzero(a, len); */
/* free(a1); */
/* a = malloc(len); */
/* ft_printf("a(%p)\n", a); */
/* ft_bzero(a, len); */
/**/
/* len = 4000; */
/* char *b = malloc(len); */
/* ft_printf("b(%p)\n", b); */
/* ft_bzero(b, len); */
/**/
/* len = 500; */
/* char *c = malloc(len); */
/* ft_printf("c(%p)\n", c); */
/* ft_bzero(c, len); */

len = 4000;
char *b = malloc(len);
ft_printf("b(%p)\n", b);
ft_bzero(b, len);

len = 500;
char *c = malloc(len);
ft_printf("c(%p)\n", c);
ft_bzero(c, len);

/* ft_printf("allocating 1000 times\n"); */
/* for (int i = 0; i < 1000; i++) { */
/* char *d = malloc(i); */
/* ft_bzero(d, i); */
/* } */
ft_printf("allocating 5000 times\n");
for (int i = 1; i < 50000; i += 3) {
char *d = malloc(i);
/* d = realloc(d, i); */
/* d = realloc(d, i - 1); */
/* d = realloc(d, i + 1); */
((void)d);
ft_bzero(d, i);
bzero(d, i);
ft_memset(d, 'a', i);
memset(d, 'b', i);
free(d);
}

ft_putchar('\n');
show_alloc_mem();
Expand Down
40 changes: 40 additions & 0 deletions testreal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "libft/libft.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
int main(void) {
int len = 100;
char *a = malloc(len);
ft_printf("a(%p)\n", a);
ft_bzero(a, len);
char *a1 = malloc(len);
ft_bzero(a1, len);
ft_printf("a(%p)\n", a1);
a = malloc(len);
ft_printf("a(%p)\n", a);
ft_bzero(a, len);
free(a1);
a = malloc(len);
ft_printf("a(%p)\n", a);
ft_bzero(a, len);

len = 4000;
char *b = malloc(len);
ft_printf("b(%p)\n", b);
ft_bzero(b, len);

len = 500;
char *c = malloc(len);
ft_printf("c(%p)\n", c);
ft_bzero(c, len);

/* ft_printf("allocating 1000 times\n"); */
/* for (int i = 0; i < 1000; i++) { */
/* char *d = malloc(i); */
/* ft_bzero(d, i); */
/* } */

ft_putchar('\n');
return EXIT_SUCCESS;
}

0 comments on commit 0ff755f

Please sign in to comment.