diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0d603d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +# ---------------------------------------------------------------------------- # +# VARIABLES # +# ---------------------------------------------------------------------------- # + +NAME_BASE := libft_malloc +HOSTTYPE ?= $(shell uname -m)_$(shell uname -s) +NAME := $(NAME_BASE)_$(HOSTTYPE).so +SYMLINK := $(NAME_BASE).so + +CC := cc +CFLAGS := -Wall -Wextra -Werror -fPIC +LDFLAGS := -shared + +SRC_DIR := src +INC_DIR := inc +OBJ_DIR := obj + +SRCS := $(wildcard $(SRC_DIR)/*.c) +OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS)) + +INCLUDES := -I$(INC_DIR) + +# ---------------------------------------------------------------------------- # +# RULES # +# ---------------------------------------------------------------------------- # + +all: $(NAME) symlink + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +$(NAME): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $^ + +symlink: $(NAME) + @ln -sf $(NAME) $(SYMLINK) + +clean: + @rm -rf $(OBJ_DIR) + +fclean: clean + @rm -f $(NAME) $(SYMLINK) + +re: fclean all + +.PHONY: all clean fclean re symlink + diff --git a/inc/malloc.h b/inc/malloc.h new file mode 100644 index 0000000..628ab1a --- /dev/null +++ b/inc/malloc.h @@ -0,0 +1,11 @@ +#ifndef MALLOC_H +# define MALLOC_H + +#include +#include + +#define PROUT "42\n" + +void *malloc(size_t size); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..a199598 --- /dev/null +++ b/main.c @@ -0,0 +1,6 @@ +#include + +int main(void) { + malloc(42); + return 0; +} diff --git a/src/malloc.c b/src/malloc.c new file mode 100644 index 0000000..bc3d02a --- /dev/null +++ b/src/malloc.c @@ -0,0 +1,7 @@ +#include "malloc.h" + +void * malloc(size_t size) { + (void)size; + write(1, PROUT, 3); + return 0; +} diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..fb172f0 --- /dev/null +++ b/test.sh @@ -0,0 +1,4 @@ +#!/bin/sh +make +gcc main.c -o test_malloc.out +LD_PRELOAD=./libft_malloc.so ./test_malloc.out