doing some work on hidding symbols...

This commit is contained in:
Jack Halford 2017-02-17 23:38:50 +01:00
parent 982fc062f9
commit 28f582ad7f
11 changed files with 102 additions and 69 deletions

View file

@ -6,8 +6,10 @@ NAME = libft_malloc.so
ARCH_NAME = libft_malloc_$(HOSTTYPE).so
CC = gcc
FLAGS = -Wall -Wextra -Werror
D_FLAGS = -g
W_FLAGS = -Wall -Wextra -Werror
V_FLAGS = -fvisibility=hidden
D_FLAGS =
FLAGS = $(W_FLAGS) $(V_FLAGS) $(D_FLAGS)
DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -34,13 +36,14 @@ all :
@make -j $(NAME)
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
@$(CC) --shared $(FLAGS) $(D_FLAGS) \
@$(CC) --shared $(FLAGS)\
-I $(INC_DIR) \
-I $(LIBFT_INC) \
$(LIBS) \
$(LIBFT_LIB) $(OBJS) \
-o $(ARCH_NAME)
@ln -fs $(ARCH_NAME) $(NAME)
@strip -x $(NAME)
@printf "\r\e[48;5;15;38;5;25m✅ MAKE $(ARCH_NAME)\e[0m\e[K\n"
$(LIBFT_LIB):
@ -56,7 +59,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
@$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196))))
@$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB)))))
@printf "\r\e[38;5;11m⌛ MAKE %10.10s : %2d%% \e[48;5;%dm%*s\e[0m%*s\e[48;5;255m \e[0m \e[38;5;11m %*s\e[0m\e[K" $(NAME) $(PERCENT) $(COLOR) $(DONE) "" $(TO_DO) "" $(DELTA) "$@"
@$(CC) $(FLAGS) $(D_FLAGS) -MMD -c $< -o $@\
@$(CC) $(FLAGS) -MMD -c $< -o $@\
-I $(INC_DIR)\
-I $(LIBFT_INC)
@$(eval INDEX=$(shell echo $$(($(INDEX)+1))))
@ -80,7 +83,7 @@ re: fclean all
relib: fcleanlib $(LIBFT_LIB)
test:
gcc -lft_malloc -L. -Iincludes -o myprogram main.c
gcc -lft_malloc -L. -Iincludes -I$(LIBFT_INC) -o myprogram main.c
.PHONY : fclean clean re relib cleanlib fcleanlib

View file

@ -5,47 +5,19 @@
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:05 by jhalford #+# #+# */
/* Updated: 2017/02/17 13:17:57 by jhalford ### ########.fr */
/* Created: 2017/02/17 23:00:06 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:00:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MALLOC_H
# define MALLOC_H
# define malloc_n 64
# define malloc_m 1024
# define malloc_N (1 * getpagesize())
# define malloc_M (2 * getpagesize())
# define malloc_magic 1234567
# define HEADER_SIZE (sizeof(t_node))
# define TINY(x) (x < (malloc_n + 1))
# define SMALL(x) (!TINY(x) && !LARGE(x))
# define LARGE(x) (malloc_m < x)
#include "../libft/includes/libft.h"
#include <sys/mman.h>
typedef struct s_header {
int size;
int magic;
} t_header;
typedef struct s_node {
int size;
struct s_node *next;
} t_node;
extern t_node *tiny_zone;
extern t_node *small_zone;
extern t_node *tiny_alloc;
extern t_node *small_alloc;
#include "stdlib.h"
void free(void *ptr);
void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
void show_alloc_mem(void);
void show_free_mem(void);
void insert_node(t_node **head, t_node *new);
#endif

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* malloc_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 23:00:24 by jhalford #+# #+# */
/* Updated: 2017/02/17 23:25:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MALLOC_INTERNAL_H
# define MALLOC_INTERNAL_H
# define malloc_n 64
# define malloc_m 1024
# define malloc_N (1 * getpagesize())
# define malloc_M (2 * getpagesize())
# define malloc_magic 1234567
# define HEADER_SIZE (sizeof(t_node))
# define TINY(x) (x < (malloc_n + 1))
# define SMALL(x) (!TINY(x) && !LARGE(x))
# define LARGE(x) (malloc_m < x)
#include "libft.h"
#include <sys/mman.h>
typedef struct s_header {
int size;
int magic;
} t_header;
typedef struct s_node {
int size;
struct s_node *next;
} t_node;
extern t_node *tiny_zone;
extern t_node *small_zone;
extern t_node *tiny_alloc;
extern t_node *small_alloc;
# pragma GCC visibility push(default)
void free(void *ptr);
void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
void show_alloc_mem(void);
# pragma GCC visibility pop
void show_free_mem(void);
void insert_node(t_node **head, t_node *new);
#endif

@ -1 +1 @@
Subproject commit b9c0c4cbb1adf15f11e681951500f23ad1f34c60
Subproject commit a82ea94ef7f50f8396d7bf6f9c08ab4a7faa994d

Binary file not shown.

View file

@ -1,10 +1,8 @@
#include "malloc.h"
#include "includes/malloc.h"
#include <stdio.h>
int main(void)
{
printf("pagesize=[%i]\n", getpagesize());
printf("sizeof(long)=[%lu]\n", sizeof(long));
void *ptr0 = malloc(8150);
show_alloc_mem();
printf("\n");

5
malloc/run.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/sh
export DYLD_LIBRARY_PATH=.
export DYLD_INSERT_LIBRARIES="libft_malloc.so"
export DYLD_FORCE_FLAT_NAMESPACE=1
$@

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:03 by jhalford #+# #+# */
/* Updated: 2017/02/17 13:18:39 by jhalford ### ########.fr */
/* Updated: 2017/02/17 23:02:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc.h"
#include "malloc_internal.h"
int remove_node(t_node **head, t_node *node)
{

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:15 by jhalford #+# #+# */
/* Updated: 2017/02/17 12:28:15 by jhalford ### ########.fr */
/* Updated: 2017/02/17 23:02:17 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc.h"
#include "malloc_internal.h"
void insert_node(t_node **head, t_node *new)
{

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:02 by jhalford #+# #+# */
/* Updated: 2017/02/17 13:18:34 by jhalford ### ########.fr */
/* Updated: 2017/02/17 23:02:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc.h"
#include "malloc_internal.h"
t_node *tiny_zone = NULL;
t_node *small_zone = NULL;
@ -63,7 +63,7 @@ void *malloc(size_t size)
t_node **node_ref;
void *ptr;
printf("malloc(%zu) was called\n", size);
printf("malloc(%zu) called\n", size);
if (LARGE(size))
{
zone_ref = &large_zone;

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 12:28:20 by jhalford #+# #+# */
/* Updated: 2017/02/17 13:18:10 by jhalford ### ########.fr */
/* Updated: 2017/02/17 23:13:52 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "malloc.h"
#include "malloc_internal.h"
t_node *tiny_zone;
t_node *small_zone;
@ -41,28 +41,27 @@ void print_alloc_mem(t_node *node)
addr, (void*)addr + size, size, HEADER_SIZE, size > 1 ? 's' : 0);
}
int show_zone(t_node *node, int is_free, size_t (*total)[3])
{
while (node)
{
is_free ? print_free_mem(node) : print_alloc_mem(node);
(*total)[is_free ? 0 : 1] += node->size;
(*total)[2] += is_free ? 0 : HEADER_SIZE;
node = node->next;
}
return (0);
}
void show_alloc_zone(char *name, t_node *alloc, t_node *zone, size_t (*total)[3])
{
if (!alloc && !zone)
return ;
ft_putstr(name);
if (alloc || zone)
printf("%s", name);
/* printf("%s", FG_RED); */
ft_putstr(FG_RED);
show_zone(alloc, 0, total);
ft_putstr(FG_GREEN);
show_zone(zone, 1, total);
ft_putstr(FG_DEFAULT);
while (alloc)
{
print_alloc_mem(alloc);
(*total)[1] += alloc->size;
(*total)[2] += HEADER_SIZE;
alloc = alloc->next;
}
printf("%s", FG_GREEN);
while (zone)
{
print_free_mem(zone);
(*total)[0] += zone->size;
zone = zone->next;
}
printf("%s", FG_DEFAULT);
}
void show_alloc_mem(void)