Skip to content

Commit

Permalink
cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
remigastaldi committed Feb 10, 2017
1 parent a654edd commit 42dfdb9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Login <gastal_r>
##
## Started on Fri Jan 27 12:28:44 2017
## Last update Fri Feb 10 02:48:59 2017
## Last update Fri Feb 10 23:56:59 2017
##

CC = gcc
Expand All @@ -22,7 +22,7 @@ SRCS = src/malloc.c \
src/utils.c \
src/free.c \
src/add_to_free.c \
src/push_back_fct.c \
src/init_func.c \
src/fct_free.c \
src/realloc.c \
src/show_func.c
Expand Down
9 changes: 5 additions & 4 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 Fri Feb 10 02:53:58 2017 Full Name
** Last update Fri Feb 10 23:45:59 2017 Full Name
*/

#ifndef MALLOC_H_
Expand All @@ -17,9 +17,9 @@
#include "utils.h"

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

#define PAGESIZE sysconf (_SC_PAGESIZE)
Expand Down Expand Up @@ -71,8 +71,9 @@ 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 *init_malloc_head(size_t, size_t);
void free_delete_end(t_malloc *);
int get_multiple(int nb);
void init_mutex();

#endif /* !MALLOC_H_ */
18 changes: 0 additions & 18 deletions main.c

This file was deleted.

2 changes: 1 addition & 1 deletion src/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void *check_in_free_list(size_t size)
{
if (tmp->size >= size)
{
if ((int)((int) tmp->size - ((int) size + sizeof(t_free))) >= ALLIGN)
if ((int)((int) tmp->size - ((int) size + sizeof(t_free))) >= ALIGN)
fracturation(size, tmp);
removeFree(tmp);
addFreeToMalloc(tmp);
Expand Down
26 changes: 16 additions & 10 deletions src/push_back_fct.c → src/init_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
** Login <julian_r@epitech.net>
**
** Started on Mon Feb 6 11:58:49 2017 Juliani Renaud
** Last update Fri Feb 10 01:22:09 2017 Full Name
** Last update Fri Feb 10 23:53:56 2017 Full Name
*/

#include "malloc.h"

extern t_core *coreStruct;
extern pthread_mutex_t mutex_malloc;

void *push_if_null(size_t size, size_t currentPageSize)
void init_mutex()
{
static int init = 0;

if (init == 0)
{
mutex_malloc = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
init = 1;
}
}

void *init_malloc_head(size_t size, size_t currentPageSize)
{
if (coreStruct->fList)
coreStruct->mList = (void *) coreStruct->fEnd
Expand All @@ -33,18 +44,13 @@ int get_multiple(int nb)
{
int value;

value = nb % ALLIGN;
value = nb % ALIGN;
if (value == 0)
return nb;
return (nb + ALLIGN - value);
return (nb + ALIGN - value);
}

size_t allow_right(size_t needed)
{
size_t right;

right = PAGESIZE * 2;
while (right <= (needed + sizeof(t_malloc)))
right += PAGESIZE;
return (right);
return (((needed / PAGESIZE) + 1) * PAGESIZE);
}
19 changes: 3 additions & 16 deletions src/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,14 @@
** Login <gastal_r>
**
** Started on Fri Jan 27 12:45:07 2017
** Last update Fri Feb 10 01:44:37 2017 Full Name
** Last update Fri Feb 10 23:47:02 2017 Full Name
*/

#include "malloc.h"

extern t_core *coreStruct;
extern pthread_mutex_t mutex_malloc;

void lock_mutex_init()
{
static int init = 0;

if (init == 0)
{
mutex_malloc = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
init = 1;
}
}

t_malloc *getNextMalloc(t_free *tmpToMalloc)
{
t_malloc *ptr;
Expand All @@ -39,9 +28,7 @@ void *push_back_malloc_list(size_t size, size_t currentPageSize)
t_malloc *tmp;

if (coreStruct->mList == NULL)
{
return (push_if_null(size, currentPageSize));
}
return (init_malloc_head(size, currentPageSize));
else
{
tmp = coreStruct->mEnd;
Expand Down Expand Up @@ -91,7 +78,7 @@ void *malloc(size_t size)
{
void *ptrTestFree;

lock_mutex_init();
init_mutex();
pthread_mutex_lock(&mutex_malloc);
size = get_multiple(size);
if (size > (size_t) sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGE_SIZE))
Expand Down
15 changes: 5 additions & 10 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
** Login <gastal_r>
**
** Started on Tue Jan 24 14:19:22 2017
** Last update Tue Feb 07 13:37:19 2017 Full Name
** Last update Fri Feb 10 23:19:09 2017 Full Name
*/

#include "utils.h"

void print_pointer(void *nbs)
{
size_t result;
size_t div;
size_t length;
size_t nb;
char *base;

nb = (size_t)nbs;
base = "0123456789ABCDEF";
result = 0;
div = 1;
length = 16;
my_putstr("0x");
while ((nb / div) >= length)
div = div * length;
while ((nb / div) >= 16)
div = div * 16;
while (div > 0)
{
result = (nb / div) % length;
write (1, &base[result], 1);
div = div / length;
write (1, &base[(nb / div) % 16], 1);
div = div / 16;
}
}

Expand Down

0 comments on commit 42dfdb9

Please sign in to comment.