aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.clang-format1
-rw-r--r--.gitignore6
-rw-r--r--.gitmodules3
-rw-r--r--Makefile59
m---------deps/Libft0
-rw-r--r--docs/malloc.en.subject.pdfbin0 -> 1345014 bytes
-rw-r--r--inc/malloc.h33
-rw-r--r--src/free.c14
-rw-r--r--src/malloc.c15
-rw-r--r--src/realloc.c16
-rw-r--r--tests/Makefile40
-rw-r--r--tests/src/main.c46
12 files changed, 233 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..a6cc54a
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1 @@
+BasedOnStyle: GNU
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..aa76930
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+obj/
+lib/
+compile_commands.json
+.cache/
+tests/obj/
+tests/bin/
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..be519d8
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "deps/Libft"]
+ path = deps/Libft
+ url = https://git.tvcloud.fr/Libft
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..fd8fe46
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,59 @@
+ifeq ($(HOSTTYPE),)
+HOSTTYPE := $(shell uname -m)_$(shell uname -s)
+endif
+
+CC = cc
+CFLAGS = -Wall -Wextra -Werror -fPIC $(EXTRA)
+
+SRCDIR = src
+INCDIR = inc
+OBJDIR = obj
+LIBDIR = lib
+
+LIBFT_DIR = deps/Libft
+LIBFT = $(LIBFT_DIR)/lib/libft.a
+
+NAME = $(LIBDIR)/libft_malloc_$(HOSTTYPE).so
+LINK = $(LIBDIR)/libft_malloc.so
+
+SRCS = malloc.c free.c realloc.c
+
+OBJS = $(SRCS:%.c=$(OBJDIR)/%.o)
+
+$(NAME): $(LIBFT) $(OBJS) | $(LIBDIR)
+ $(CC) -shared -o $@ $(OBJS) -L $(LIBFT_DIR)/lib -lft
+ ln -sf libft_malloc_$(HOSTTYPE).so $(LINK)
+
+all: $(NAME)
+
+$(LIBFT):
+ $(MAKE) -C $(LIBFT_DIR) NOMALLOC=1
+
+$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
+ $(CC) $(CFLAGS) -I $(INCDIR) -I $(LIBFT_DIR)/inc -c $< -o $@
+
+$(OBJDIR):
+ mkdir -p $(OBJDIR)
+
+$(LIBDIR):
+ mkdir -p $(LIBDIR)
+
+clean:
+ rm -rf $(OBJDIR)
+ rm -f compile_commands.json
+
+fclean: clean
+ rm -rf $(LIBDIR)
+ $(MAKE) -C $(LIBFT_DIR) fclean
+ $(MAKE) -C tests clean
+
+re: fclean all
+
+test: $(NAME)
+ $(MAKE) -C tests test
+
+compile_commands.json: $(SRCS:%=$(SRCDIR)/%)
+ @echo '$(foreach src,$(SRCS),{"directory":"$(CURDIR)","command":"$(CC) $(CFLAGS) -I $(INCDIR) -I $(LIBFT_DIR)/inc -c $(SRCDIR)/$(src)","file":"$(SRCDIR)/$(src)"})' \
+ | jq -s '.' > $@
+
+.PHONY: all clean fclean re test
diff --git a/deps/Libft b/deps/Libft
new file mode 160000
+Subproject 2d515c104c9e8bc3d2cc9d27c44fd3feb894569
diff --git a/docs/malloc.en.subject.pdf b/docs/malloc.en.subject.pdf
new file mode 100644
index 0000000..de250f4
--- /dev/null
+++ b/docs/malloc.en.subject.pdf
Binary files differ
diff --git a/inc/malloc.h b/inc/malloc.h
new file mode 100644
index 0000000..75ea6e9
--- /dev/null
+++ b/inc/malloc.h
@@ -0,0 +1,33 @@
+/**
+ * @file malloc.h
+ * @brief Public interface for the ft_malloc allocator.
+ *
+ * Declares malloc, free, realloc, and show_alloc_mem with the same
+ * prototypes as their libc counterparts so the library can be used
+ * as a drop-in replacement via LD_PRELOAD.
+ */
+
+#ifndef MALLOC_H
+#define MALLOC_H
+
+#include <stddef.h>
+
+/** @brief Release the memory block at @p ptr. */
+void free (void *ptr);
+
+/** @brief Allocate @p size bytes and return a pointer to them. */
+void *malloc (size_t size);
+
+/**
+ * @brief Resize the block at @p ptr to @p size bytes.
+ * @return Pointer to the (possibly moved) block, or NULL on failure.
+ */
+void *realloc (void *ptr, size_t size);
+
+/**
+ * @brief Print every allocated zone and block to stdout, sorted by
+ * ascending address.
+ */
+void show_alloc_mem (void);
+
+#endif
diff --git a/src/free.c b/src/free.c
new file mode 100644
index 0000000..22c3faa
--- /dev/null
+++ b/src/free.c
@@ -0,0 +1,14 @@
+/**
+ * @file free.c
+ * @brief Stub implementation of free.
+ */
+
+#include "libft.h"
+#include "malloc.h"
+
+void
+free (void *ptr)
+{
+ (void)ptr;
+ ft_putendl_fd ("FREE", 1);
+}
diff --git a/src/malloc.c b/src/malloc.c
new file mode 100644
index 0000000..8982de0
--- /dev/null
+++ b/src/malloc.c
@@ -0,0 +1,15 @@
+/**
+ * @file malloc.c
+ * @brief Stub implementation of malloc.
+ */
+
+#include "malloc.h"
+#include "libft.h"
+
+void *
+malloc (size_t size)
+{
+ (void)size;
+ ft_putendl_fd ("MALLOC", 1);
+ return (NULL);
+}
diff --git a/src/realloc.c b/src/realloc.c
new file mode 100644
index 0000000..c0bbfed
--- /dev/null
+++ b/src/realloc.c
@@ -0,0 +1,16 @@
+/**
+ * @file realloc.c
+ * @brief Stub implementation of realloc.
+ */
+
+#include "libft.h"
+#include "malloc.h"
+
+void *
+realloc (void *ptr, size_t size)
+{
+ (void)ptr;
+ (void)size;
+ ft_putendl_fd ("REALLOC", 1);
+ return (NULL);
+}
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 0000000..04d7c1f
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,40 @@
+CC = cc
+CFLAGS = -Wall -Wextra -Werror $(EXTRA)
+
+SRCDIR = src
+OBJDIR = obj
+BINDIR = bin
+
+NAME = $(BINDIR)/test
+
+SRCS = main.c
+
+OBJS = $(SRCS:%.c=$(OBJDIR)/%.o)
+
+MALLOC_LIB = ../lib/libft_malloc.so
+
+$(NAME): $(OBJS) | $(BINDIR)
+ $(CC) $(CFLAGS) -o $@ $(OBJS)
+
+all: $(NAME)
+
+$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+$(OBJDIR):
+ mkdir -p $(OBJDIR)
+
+$(BINDIR):
+ mkdir -p $(BINDIR)
+
+test: $(NAME)
+ @echo "=== system malloc ==="
+ @$(NAME)
+ @echo ""
+ @echo "=== ft_malloc ==="
+ @LD_PRELOAD=$(MALLOC_LIB) $(NAME)
+
+clean:
+ rm -rf $(OBJDIR) $(BINDIR)
+
+.PHONY: all test clean
diff --git a/tests/src/main.c b/tests/src/main.c
new file mode 100644
index 0000000..0ded605
--- /dev/null
+++ b/tests/src/main.c
@@ -0,0 +1,46 @@
+/**
+ * @file main.c
+ * @brief Smoke test — call malloc once and print the returned pointer.
+ *
+ * Uses write(2) instead of printf to avoid stdio calling malloc
+ * internally, which would pollute output under LD_PRELOAD.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+
+/** @brief Write the hex representation of @p ptr to stdout. */
+static void
+_s_put_ptr (void *ptr)
+{
+ const char hex[] = "0123456789abcdef";
+ char buf[20];
+ unsigned long v = (unsigned long)ptr;
+ int i;
+
+ if (!ptr)
+ {
+ write (1, "(nil)", 5);
+ return;
+ }
+ i = (int)sizeof (buf);
+ while (v)
+ {
+ buf[--i] = hex[v % 16];
+ v /= 16;
+ }
+ buf[--i] = 'x';
+ buf[--i] = '0';
+ write (1, buf + i, (size_t)(sizeof (buf) - i));
+}
+
+int
+main (void)
+{
+ void *p = malloc (42);
+ write (1, "malloc(42) = ", 13);
+ _s_put_ptr (p);
+ write (1, "\n", 1);
+ free (p);
+ return (0);
+}