Skip to content

Commit

Permalink
struct changes
Browse files Browse the repository at this point in the history
  • Loading branch information
remigastaldi committed Feb 10, 2017
1 parent bf2f823 commit acac687
Show file tree
Hide file tree
Showing 20 changed files with 874 additions and 97 deletions.
15 changes: 10 additions & 5 deletions include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
** Login <gastal_r>
**
** Started on Wed Jan 25 18:35:39 2017
** Last update Mon Feb 06 23:34:40 2017 Full Name
** Last update Fri Feb 10 01:42:14 2017 Full Name
*/

#ifndef MALLOC_H_
Expand All @@ -30,7 +30,6 @@ typedef struct s_malloc
{
struct s_malloc *next;
struct s_malloc *prev;
struct s_malloc *end;
size_t size;
char flag;
} t_malloc;
Expand All @@ -39,13 +38,19 @@ typedef struct s_free
{
struct s_free *next;
struct s_free *prev;
struct s_free *end;
size_t size;
char flag;
} t_free;

t_malloc *mallocStruct;
t_free *freeStruct;
typedef struct s_core
{
struct s_malloc *mEnd;
struct s_free *fEnd;
struct s_malloc *mList;
struct s_free *fList;
} t_core;

t_core *coreStruct;

pthread_mutex_t mutex_malloc;

Expand Down
18 changes: 18 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "malloc.h"

int roundUp(int numToRound)
{
if (ALLIGN == 0)
return numToRound;

int remainder = numToRound % ALLIGN;
if (remainder == 0)
return numToRound;

return numToRound + ALLIGN - remainder;
}

int main()
{
printf("%d\n", sizeof(t_malloc));
}
45 changes: 45 additions & 0 deletions save/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
##
## Makefile for Makefile in /home/gastal_r/rendu/PSU_2016_malloc
##
## Made by
## Login <gastal_r>
##
## Started on Fri Jan 27 12:28:44 2017
## Last update Tue Feb 7 15:02:06 2017
##

CC = gcc

RM = rm -rf

CFLAGS += -Wall -Wextra -W -Werror -fPIC -lpthread
CFLAGS += -I./include
LDFLAGS += -shared

NAME = libmy_malloc.so

SRCS = src/malloc.c \
src/utils.c \
src/free.c \
src/add_to_free.c \
src/push_back_fct.c \
src/fct_free.c \
src/realloc.c \
src/show_func.c

OBJS = $(SRCS:.c=.o)

all : $(NAME)

$(NAME) : $(OBJS)
$(CC) -o $(NAME) $(OBJS) $(CFLAGS) $(LDFLAGS)

clean :
$(RM) $(OBJS)

fclean : clean
$(RM) $(NAME)

re : fclean all

.PHONNY : all clean fclean re
73 changes: 73 additions & 0 deletions save/include/malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
** malloc.h for malloc in /home/gastal_r/rendu/PSU_2016_malloc
**
** Made by
** Login <gastal_r>
**
** Started on Wed Jan 25 18:35:39 2017
** Last update Mon Feb 06 23:34:40 2017 Full Name
*/

#ifndef MALLOC_H_
#define MALLOC_H_

#include <pthread.h>
#include <string.h>

#include "utils.h"

#if __x86_64__
#define ALLIGN 8
#else
#define ALLIGN 4
#endif

#define PAGESIZE sysconf (_SC_PAGESIZE)
#define MALLOC_FLAG 'M'
#define FREE_FLAG 'F'

typedef struct s_malloc
{
struct s_malloc *next;
struct s_malloc *prev;
struct s_malloc *end;
size_t size;
char flag;
} t_malloc;

typedef struct s_free
{
struct s_free *next;
struct s_free *prev;
struct s_free *end;
size_t size;
char flag;
} t_free;

t_malloc *mallocStruct;
t_free *freeStruct;

pthread_mutex_t mutex_malloc;

void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
void *calloc(size_t nmemb, size_t size);
void free(void *);
void show_alloc_mem();
void show_free_list();
size_t allow_right(size_t);
void *check_in_free_list(size_t size);
t_malloc *getNextMalloc(t_free *tmpToMalloc);
t_free *getNextFree(t_free *);
t_free *fracturation(size_t, t_free *);
void removeFree(t_free *);
void addFreeToMalloc(t_free *);
void add_end(t_free*);
void add_middle(t_free *);
void merging_block();
void free_malloc_head();
void *push_if_null(size_t, size_t);
void free_delete_end(t_malloc *);
int get_multiple(int nb);

#endif /* !MALLOC_H_ */
21 changes: 21 additions & 0 deletions save/include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
** utils.h for utils in /home/gastal_r/rendu/PSU_2016_malloc
**
** Made by
** Login <gastal_r>
**
** Started on Tue Jan 24 14:52:14 2017
** Last update Tue Feb 07 13:37:30 2017 Full Name
*/

#ifndef UTILS_H_
#define UTILS_H_

#include <unistd.h>

int my_strlen(char *str);
void my_putstr(char *str);
void my_putnbr_unsigned(size_t nb);
void print_pointer(void *nbs);

#endif
Binary file added save/moul_malloc_stud
Binary file not shown.
81 changes: 81 additions & 0 deletions save/src/add_to_free.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
** add_to_free.c for free in /home/julian_r/rendu/systeme_unix/PSU_2016_malloc
**
** Made by Juliani Renaud
** Login <julian_r@epitech.net>
**
** Started on Mon Feb 6 11:42:28 2017 Juliani Renaud
** Last update Mon Feb 06 20:47:30 2017 Full Name
*/

#include "malloc.h"

extern t_malloc *mallocStruct;
extern t_free *freeStruct;

void merging_block(t_free *tmp, t_free *ptr)
{
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));
}
memset(tmp, 0, sizeof(t_free));
}

void add_middle(t_free *ptr)
{
t_free *tmp;

tmp = getNextFree(ptr);
if (tmp == (void *) ptr + ptr->size + sizeof(t_free))
merging_block (tmp, ptr);
else if (tmp->prev && tmp->prev == (void*) ptr
- (tmp->prev->size + sizeof(t_free)))
{
tmp->prev->size += ptr->size + sizeof(t_free);
memset(ptr, 0, sizeof(t_free));
}
else
{
ptr->next = tmp;
ptr->prev = tmp->prev;
tmp->prev->next = ptr;
tmp->prev = ptr;
}
}

void add_end(t_free *ptr)
{
if ((void *) freeStruct->end + freeStruct->end->size
+ sizeof(t_free) == (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;
}
}

void free_malloc_head()
{
mallocStruct->next->end = mallocStruct->end;
mallocStruct = mallocStruct->next;
mallocStruct->prev = NULL;
}
104 changes: 104 additions & 0 deletions save/src/fct_free.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
** fct_free.c for malloc in /home/julian_r/rendu/systeme_unix/PSU_2016_malloc
**
** Made by Juliani Renaud
** Login <julian_r@epitech.net>
**
** Started on Mon Feb 6 10:58:07 2017 Juliani Renaud
** Last update Mon Feb 06 19:18:52 2017 Full Name
*/

#include "malloc.h"

extern t_malloc *mallocStruct;
extern t_free *freeStruct;

t_free *fracturation(size_t size, t_free *tmpToMalloc)
{
t_free *tmpNext;

tmpNext = (void *) tmpToMalloc + size + sizeof(t_free);
tmpNext->size = tmpToMalloc->size - (size + sizeof(t_free));
tmpToMalloc->size = size;
tmpNext->prev = tmpToMalloc;
tmpNext->next = tmpToMalloc->next;
if (tmpToMalloc->next)
tmpToMalloc->next->prev = tmpNext;
else
freeStruct->end = tmpNext;
tmpToMalloc->next = tmpNext;
tmpNext->flag = FREE_FLAG;
return (tmpNext);
}

void removeFree(t_free *tmpToMalloc)
{
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;
}
return ;
}

void addFreeToMalloc2(t_free *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;
mallocStruct->prev = NULL;
}
else
{
tmpToMalloc->prev = (t_free *) ptrNextMalloc->prev;
tmpToMalloc->next = (t_free *) ptrNextMalloc;
ptrNextMalloc->prev->next = (t_malloc *) tmpToMalloc;
ptrNextMalloc->prev = (t_malloc *) tmpToMalloc;
}
}

void addFreeToMalloc(t_free *tmpToMalloc)
{
if (mallocStruct == NULL)
{
mallocStruct = (t_malloc *) tmpToMalloc;
mallocStruct->end = mallocStruct;
mallocStruct->next = NULL;
mallocStruct->prev = NULL;
}
else
{
if (mallocStruct && mallocStruct->end > (t_malloc *) tmpToMalloc)
addFreeToMalloc2(tmpToMalloc);
else
{
mallocStruct->end->next = (t_malloc *) tmpToMalloc;
mallocStruct->end->next->prev = mallocStruct->end;
mallocStruct->end = (t_malloc *) tmpToMalloc;
mallocStruct->end->next = NULL;
}
}
tmpToMalloc->flag = MALLOC_FLAG;
}
Loading

0 comments on commit acac687

Please sign in to comment.