Skip to content

Commit 87f9b08

Browse files
author
gpouyat
committed
[MASTER]CHORE: init project
1 parent 09323a1 commit 87f9b08

17 files changed

+649
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libft"]
2+
path = libft
3+
url = https://github.com/Guiforge/libft.git

.vscode/c_cpp_properties.json

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"${workspaceFolder}/includes",
7+
"${workspaceFolder}",
8+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
9+
"/usr/local/include",
10+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include",
11+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
12+
"/usr/include"
13+
],
14+
"defines": [],
15+
"intelliSenseMode": "clang-x64",
16+
"browse": {
17+
"path": [
18+
"${workspaceFolder}",
19+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
20+
"/usr/local/include",
21+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include",
22+
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
23+
"/usr/include"
24+
],
25+
"limitSymbolsToIncludedHeaders": true,
26+
"databaseFilename": ""
27+
},
28+
"macFrameworkPath": [
29+
"/System/Library/Frameworks",
30+
"/Library/Frameworks"
31+
],
32+
"compilerPath": "/usr/bin/clang",
33+
"cStandard": "c11",
34+
"cppStandard": "c++17"
35+
},
36+
{
37+
"name": "Linux",
38+
"includePath": [
39+
"${workspaceFolder}/includes",
40+
"/usr/include",
41+
"/usr/local/include",
42+
"${workspaceFolder}"
43+
],
44+
"defines": [],
45+
"intelliSenseMode": "clang-x64",
46+
"browse": {
47+
"path": [
48+
"/usr/include",
49+
"/usr/local/include",
50+
"${workspaceFolder}"
51+
],
52+
"limitSymbolsToIncludedHeaders": true,
53+
"databaseFilename": ""
54+
}
55+
},
56+
{
57+
"name": "Win32",
58+
"includePath": [
59+
"${workspaceFolder}/includes",
60+
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
61+
"${workspaceFolder}"
62+
],
63+
"defines": [
64+
"_DEBUG",
65+
"UNICODE",
66+
"_UNICODE"
67+
],
68+
"intelliSenseMode": "msvc-x64",
69+
"browse": {
70+
"path": [
71+
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
72+
"${workspaceFolder}"
73+
],
74+
"limitSymbolsToIncludedHeaders": true,
75+
"databaseFilename": ""
76+
}
77+
}
78+
],
79+
"version": 4
80+
}

.vscode/launch.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
3+
// Pointez pour afficher la description des attributs existants.
4+
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(lldb) Launch",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/a.out",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": true,
17+
"MIMode": "lldb"
18+
}
19+
]
20+
}

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"intern_malloc.h": "c"
4+
}
5+
}

Makefile

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# **************************************************************************** #
2+
# #
3+
# ::: :::::::: #
4+
# Makefile :+: :+: :+: #
5+
# +:+ +:+ +:+ #
6+
# By: gpouyat <[email protected]> +#+ +:+ +#+ #
7+
# +#+#+#+#+#+ +#+ #
8+
# Created: 2017/02/05 12:29:27 by gpouyat #+# #+# #
9+
# Updated: 2018/09/26 15:32:36 by gpouyat ### ########.fr #
10+
# #
11+
# **************************************************************************** #
12+
13+
.NOTPARALLEL:
14+
15+
.PHONY: all clean fclean re
16+
17+
C_NO = \033[0m
18+
C_G = \033[0;32m
19+
C_Y = \033[1;33m
20+
C_B = \033[1;34m
21+
C_C = \033[1;36m
22+
C_R = \033[1;31m
23+
24+
SRCS_NAME += intern/create_zone.c intern/error.c intern/init_zones.c \
25+
intern/find_block.c intern/get.c intern/check_align.c \
26+
intern/split_block.c
27+
SRCS_NAME +=
28+
29+
30+
###############################################################################
31+
32+
# Compiler
33+
34+
ifeq ($(HOSTTYPE),)
35+
HOSTTYPE := $(shell uname -m)_$(shell uname -s)
36+
endif
37+
38+
NAME = libft_malloc_$(HOSTTYPE).so
39+
NAME_TEST = libmalloc_test.a
40+
CC = clang
41+
CFLAGS = -Wall -Wextra -Werror
42+
43+
ifeq ($(DEV),yes)
44+
CFLAGS += -g3
45+
endif
46+
47+
ifeq ($(SAN),yes)
48+
CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls -g3
49+
endif
50+
51+
#The Directories, Source, Includes, Objects and Libraries
52+
INC = -I includes -I libft/includes
53+
SRCS_DIR = sources
54+
SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAME))
55+
56+
#Objects
57+
OBJ_NAME = $(SRCS_NAME:.c=.o)
58+
OBJS_DIR = objs
59+
OBJS = $(addprefix $(OBJS_DIR)/,$(OBJ_NAME))
60+
61+
62+
LIB_PATH = ./libft/
63+
LIB_NAME = -lft
64+
LIB = $(addprefix -L,$(LIB_PATH))
65+
LFLAGS = $(LIB) $(LIB_NAME)
66+
67+
#Utils
68+
RM = rm -rf
69+
MKDIR = mkdir -p
70+
LINK = libft_malloc.so
71+
72+
COUNT = 0
73+
TOTAL = 0
74+
PERCENT = 0
75+
$(eval TOTAL=$(shell echo $$(printf "%s" "$(SRCS)" | wc -w)))
76+
77+
#tests
78+
COUNT_TESTS = 0
79+
TOTAL_TESTS = 0
80+
PERCENT_TESTS = 0
81+
$(eval TOTAL_TESTS=$(shell echo $$(printf "%s" "$(SRCS_TESTS)" | wc -w)))
82+
###############################################################################
83+
84+
85+
all: lib $(NAME)
86+
87+
lib:
88+
@make -C $(LIB_PATH) CFLAGS="$(CFLAGS)"
89+
90+
$(NAME): $(OBJS)
91+
@$(CC) $(OBJS) $(CFLAGS) $(INC) -shared -o $(NAME) $(LFLAGS)
92+
@/bin/rm -f $(LINK)
93+
@ln -s $(NAME) $(LINK)
94+
@echo
95+
@echo compiling with flag: $(CFLAGS)
96+
@echo "[\033[35m------------------------------------------------\033[0m]"
97+
@echo "[\033[36m----------- MALLOC compilation Done! -----------\033[0m]"
98+
@echo "[\033[35m------------------------------------------------\033[0m]"
99+
100+
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c
101+
@mkdir -p $(OBJS_DIR) $(OBJS_DIR)/intern
102+
@$(CC) $(CFLAGS) $(INC) -o $@ -c $<
103+
$(eval COUNT=$(shell echo $$(($(COUNT)+1))))
104+
$(eval PERCENT=$(shell echo $$((($(COUNT) * 100 )/$(TOTAL)))))
105+
@printf "$(C_B)%-8s $(C_C) $<$(C_NO) \n" "[$(PERCENT)%]"
106+
107+
$(OBJS_DIR_TESTS)/%.o: $(SRCS_DIR_TESTS)/%.c
108+
@mkdir -p $(OBJS_DIR_TESTS)
109+
@$(CC) $(CFLAGS) $(INC) $(INC_TESTS) -o $@ -c $<
110+
$(eval COUNT_TESTS=$(shell echo $$(($(COUNT_TESTS)+1))))
111+
$(eval PERCENT_TESTS=$(shell echo $$((($(COUNT_TESTS) * 100 )/$(TOTAL_TESTS)))))
112+
@printf "$(C_B)%-8s $(C_G) $<$(C_NO) \n" "[$(PERCENT_TESTS)%]"
113+
114+
clean:
115+
@echo "\033[35m$(NAME) :\033[0m [\033[31mSuppression des .o\033[0m]"
116+
@$(RM) $(OBJS_DIR)
117+
@$(RM) $(OBJS_DIR_TESTS)
118+
@make clean -C $(LIB_PATH)
119+
120+
fclean: clean
121+
@echo "\033[35m$(NAME) :\033[0m [\033[31mSuppression de $(NAME) et $(LINK)\033[0m]"
122+
@$(RM) $(NAME)
123+
@$(RM) $(LINK)
124+
@$(RM) test
125+
@$(RM) $(NAME_TEST)
126+
@make fclean -C $(LIB_PATH)
127+
128+
re: fclean all
129+
130+
miniclean:
131+
@echo "\033[35m$(NAME) :\033[0m [\033[31mSuppression des .o\033[0m]"
132+
@$(RM) $(OBJS_DIR)
133+
@$(RM) $(OBJS_DIR_TESTS)
134+
135+
miniflcean: miniclean
136+
@echo "\033[35m$(NAME) :\033[0m [\033[31mSuppression de $(NAME) et $(LINK)\033[0m]"
137+
@$(RM) $(NAME)
138+
@$(RM) $(LINK)
139+
@$(RM) test
140+
@$(RM) $(NAME_TEST)
141+
142+
minire: miniflcean all
143+
144+
.PHONY: re clean fclean all lib doc dev minire miniflcean miniclean
145+
.SUFFIXES: .c .h .o .d

includes/intern_malloc.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* intern_malloc.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: gpouyat <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2018/09/26 12:26:26 by gpouyat #+# #+# */
9+
/* Updated: 2018/09/26 16:54:16 by gpouyat ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef INTERN_MALLOC_H
14+
#define INTERN_MALLOC_H
15+
16+
#include "../libft/includes/libft.h"
17+
#include <sys/mman.h>
18+
19+
typedef struct s_block
20+
{
21+
size_t size;
22+
struct s_block *next;
23+
struct s_block *previous;
24+
int free;
25+
} t_block;
26+
27+
typedef struct s_malloc_global
28+
{
29+
t_block *tiny;
30+
void *tiny_end;
31+
t_block *medium;
32+
void *medium_end;
33+
t_block *large;
34+
int error;
35+
} t_malloc_global;
36+
37+
# define MALLOC_FLAG_MMAP_RW PROT_READ | PROT_WRITE
38+
# define MALLOC_FLAG_MMAP_MAP MAP_PRIVATE | MAP_ANON
39+
# define TINY_MAX 1024
40+
# define MEDIUM_MAX TINY_MAX * 3
41+
# define TINY_SIZE TINY_MAX * 1000
42+
# define MEDIUM_SIZE MEDIUM_MAX * 1000
43+
44+
# define BLOCK_SIZE sizeof(struct s_block)
45+
46+
t_block *create_zone(size_t size);
47+
void init_zones(void);
48+
t_block *find_block_free(t_block *zone, size_t size);
49+
t_block *find_block(t_block *zone, t_block *block, int free);
50+
void check_align(size_t size);
51+
void *get_data(t_block *block);
52+
t_block *get_block(void *data);
53+
t_block *split_block(t_block *block, size_t size);
54+
void defrag(t_block *zone);
55+
56+
#endif

includes/malloc_error.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* malloc_error.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: gpouyat <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2017/12/25 21:18:48 by gpouyat #+# #+# */
9+
/* Updated: 2018/09/26 13:47:45 by gpouyat ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef INTERN_MALLOC_ERROR_H
14+
# define INTERN_MALLOC_ERROR_H
15+
16+
typedef enum e_intern_malloc_error t_intern_malloc_error;
17+
18+
enum e_intern_malloc_error
19+
{
20+
E_INTERN_MALLOC_INIT_FAIL = 0,
21+
E_INTERN_MALLOC_CANT_ALLOC = 1,
22+
E_INTERN_MALLOC_INVALID_FREE = 2,
23+
E_INTERN_MALLOC_MUNMAP_FAIL = 3,
24+
E_INTERN_MALLOC_SEARCH_FAIL = 4,
25+
E_INTERN_MALLOC_INVALID_SIZE_MALLOC = 5,
26+
E_INTERN_MALLOC_PTHREAD_LOCK = 6,
27+
E_INTERN_MALLOC_PTHREAD_UNLOCK = 7,
28+
E_INTERN_MALLOC_INTERNAL_ERROR = 8,
29+
E_INTERN_MALLOC_ALLIGN = 9
30+
};
31+
32+
void malloc_error(t_intern_malloc_error error);
33+
34+
#endif

libft

Submodule libft added at d3b48e8

main.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "./includes/intern_malloc.h"
2+
3+
extern t_malloc_global g_malloc;
4+
5+
void print_one(t_block *block)
6+
{
7+
if (block)
8+
printf("size = %lu, next = %lx, free = %d\n", block->size, (long)block->next, block->free);
9+
else
10+
printf("NO blocks\n");
11+
}
12+
#include <assert.h>
13+
int main()
14+
{
15+
printf("%lu\n", BLOCK_SIZE);
16+
t_block *toto = create_zone(32);
17+
printf("%lu, %d\n", toto->size + BLOCK_SIZE, getpagesize());
18+
// printf("%lu, %lu\n", g_malloc.tiny->size, g_malloc.medium->size);
19+
init_zones();
20+
printf("%lu, %lu\n", g_malloc.tiny->size, g_malloc.medium->size);
21+
t_block *fit = find_block_free(g_malloc.tiny, 48);
22+
printf("BEFORE\n");
23+
printf("%lx = %lx\n", (long)g_malloc.tiny, (long)fit);
24+
print_one(fit);
25+
26+
void *new = split_block(fit, 16);
27+
split_block(fit, 16);
28+
fit = find_block_free(g_malloc.tiny, 16);
29+
printf("AFTER\n");
30+
print_one(fit);
31+
printf("%lx = %lx\n", (long)g_malloc.tiny, (long)fit);
32+
}

0 commit comments

Comments
 (0)