Skip to content

Commit

Permalink
Stable version
Browse files Browse the repository at this point in the history
BIG CHANGE  --- Stable version
  • Loading branch information
remigastaldi committed Feb 4, 2017
1 parent a769375 commit 52c3a79
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 325 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ CC = gcc -g -g3

RM = rm -rf

CFLAGS = -Wall -Wextra -W -fPIC
CFLAGS = -Wall -Wextra -W -fPIC -lpthread

LFLAGS = -shared

NAME = libmy_malloc.so

SRCS = malloc.c \
utils.c \
free.c \
printpointer.c

OBJS = $(SRCS:.c=.o)
Expand Down
Binary file removed a.out
Binary file not shown.
232 changes: 232 additions & 0 deletions free.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
** free.c for free in /home/gastal_r/rendu/PSU_2016_malloc
**
** Made by
** Login <gastal_r>
**
** Started on Sat Feb 4 02:37:06 2017
** Last update Sat Feb 04 03:01:29 2017 Full Name
*/

#include "malloc.h"

extern t_malloc *mallocStruct;
extern t_free *freeStruct;

void show_free_list()
{
t_free *tmp;

tmp = freeStruct;
my_putstr("freeList : ");
my_putstr("\n");
while (tmp)
{
void *ptrTmp = (void *) tmp + sizeof(t_free);
printpointer((void*) tmp);
my_putstr(" - ");
printpointer(ptrTmp + tmp->size);
my_putstr(" : ");
my_putnbr(tmp->size);
my_putstr(" bytes\n");
tmp = tmp->next;
}
}

t_free *getNextFree(t_free *tmpToMalloc)
{
t_free *tmp;

tmp = freeStruct->end;
if (tmp->prev < tmpToMalloc)
return (tmp);
while (tmp > (t_free *) tmpToMalloc)
{
if (tmp->prev == NULL)
return (tmp);
tmp = tmp->prev;
}
return (tmp->next);
}

void *check_in_free_list(size_t size)
{
t_free *tmp;

tmp = freeStruct;
while (tmp)
{
if (tmp->size >= size)
{
t_free *tmpToMalloc = tmp;
if (tmpToMalloc->prev)
{
if (tmpToMalloc->next == NULL)
{
freeStruct->end = freeStruct->end->prev;
freeStruct->end->next = NULL;
}
else
{
tmpToMalloc->next->prev = tmpToMalloc->prev;
tmpToMalloc->prev->next = tmpToMalloc->next;
}
}
else if (freeStruct->next == NULL)
freeStruct = NULL;
else
{
freeStruct->next->end = freeStruct->end;
freeStruct = freeStruct->next;
freeStruct->prev = NULL;
}
if (mallocStruct == NULL)
{
mallocStruct = (t_malloc *) tmpToMalloc;
mallocStruct->end = mallocStruct;
mallocStruct->next = NULL;
mallocStruct->prev = NULL;
}
else
{
if (mallocStruct && mallocStruct->end > (t_malloc *) tmpToMalloc)
{
t_malloc *ptrNextMalloc;
ptrNextMalloc = getNextMalloc(tmpToMalloc);
if (ptrNextMalloc->prev == NULL)
{
ptrNextMalloc->prev = (t_malloc *) tmpToMalloc;
mallocStruct = (t_malloc *) tmpToMalloc;
mallocStruct->next = ptrNextMalloc;
mallocStruct->end = ptrNextMalloc->end;
}
else
{
tmpToMalloc->prev = (t_free *) ptrNextMalloc->prev;
tmpToMalloc->next = (t_free *) ptrNextMalloc;
ptrNextMalloc->prev->next = (t_malloc *) tmpToMalloc;
ptrNextMalloc->prev = (t_malloc *) tmpToMalloc;
}
}
else
{
mallocStruct->end->next = (t_malloc *) tmpToMalloc;
mallocStruct->end->next->prev = mallocStruct->end;
mallocStruct->end = (t_malloc *) tmpToMalloc;
mallocStruct->end->next = NULL;
}
}
void *returnPtr = (void *) tmpToMalloc + sizeof(t_malloc);
return (returnPtr);
}
tmp = tmp->next;
}
return (NULL);
}

void add_to_free_list(t_free *ptr)
{
if (freeStruct == NULL)
{
freeStruct = ptr;
freeStruct->end = freeStruct;
freeStruct->next = NULL;
freeStruct->prev = NULL;
}
else
{
if (ptr < freeStruct->end && ptr > freeStruct)
{

t_free *tmp;
tmp = getNextFree(ptr);
if (tmp == (void *) ptr + ptr->size + sizeof(t_free))
{
ptr->size += tmp->size + sizeof(t_free);
tmp->prev->next = ptr;
ptr->prev = tmp->prev;
ptr->next = tmp->next;
if (tmp->next)
tmp->next->prev = ptr;
else
freeStruct->end = ptr;
if (ptr->prev == (void*) ptr - (ptr->prev->size + sizeof(t_free)))
{
ptr->prev->size += ptr->size + sizeof(t_free);
ptr->prev->next = ptr->next;
(ptr->next ? ptr->next->prev = ptr->prev : (freeStruct->end = ptr->prev));
}
}
else if (tmp->prev && tmp->prev == (void*) ptr - (tmp->prev->size + sizeof(t_free)))
tmp->prev->size += ptr->size + sizeof(t_free);
else
{
ptr->next = tmp;
ptr->prev = tmp->prev;
tmp->prev->next = ptr;
tmp->prev = ptr;
}
}
else if (ptr > freeStruct->end)
{
if ((void *) freeStruct->end + freeStruct->end->size + sizeof(t_malloc) == (void *) ptr)
{
freeStruct->end->size += ptr->size + sizeof(t_malloc);
ptr->prev = NULL;
ptr->next = NULL;
}
else
{
freeStruct->end->next = ptr;
freeStruct->end->next->prev = freeStruct->end;
freeStruct->end = ptr;
freeStruct->end->next = NULL;
}
}
else
{
ptr->end = freeStruct->end;
ptr->prev = NULL;
ptr->next = freeStruct;
freeStruct->prev = ptr;
freeStruct = ptr;
}
}
}

void free(void *ptr)
{
t_malloc *tmp;

if (ptr == NULL)
return;
//pthread_mutex_lock(&lock_mutex);
/* my_putstr("free : ");
printpointer(ptr - sizeof(t_malloc));
my_putstr("\n"); */
tmp = ptr - sizeof(t_malloc);
if (tmp->prev)
{
if (tmp->next == NULL)
{
mallocStruct->end = mallocStruct->end->prev;
mallocStruct->end->next = NULL;
}
else
{
tmp->next->prev = tmp->prev;
tmp->prev->next = tmp->next;
}
}
else if (mallocStruct->next == NULL)
mallocStruct = NULL;
else
{
mallocStruct->next->end = mallocStruct->end;
mallocStruct = mallocStruct->next;
mallocStruct->prev = NULL;
}
memset(ptr, 0, tmp->size);
add_to_free_list((t_free *) tmp);
//pthread_mutex_unlock(&lock_mutex);
}
Binary file removed libmy_malloc.so
Binary file not shown.
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
** Login <gastal_r>
**
** Started on Tue Jan 24 14:47:37 2017
** Last update Thu Feb 02 11:33:56 2017 Full Name
** Last update Fri Feb 03 11:52:30 2017 Full Name
*/

#include <string.h>
Expand All @@ -18,7 +18,7 @@ int main()
char *bonsoir;
char *oui;

for (int i = 0; i < 3; ++i)
for (int i = 0; i < 1; ++i)
{
test = malloc(100);
test = memset(test, 't', 100);
Expand All @@ -45,9 +45,9 @@ for (int i = 0; i < 3; ++i)

show_alloc_mem();
show_free_list();
free(test);
// free(test);
//free(bonsoir);
free(oui);
// free(oui);
my_putstr(" --------------PRINT OF END------------- \n");
show_alloc_mem();
show_free_list();
Expand Down
Loading

0 comments on commit 52c3a79

Please sign in to comment.