add show_alloc_mem
This commit is contained in:
7
Makefile
7
Makefile
@ -6,7 +6,7 @@
|
||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
||||
# Updated: 2024/11/29 17:03:52 by tomoron ### ########.fr #
|
||||
# Updated: 2024/11/30 18:07:44 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -16,7 +16,8 @@ NAME := libft_malloc_$(HOSTTYPE).so
|
||||
|
||||
CC = cc
|
||||
|
||||
SRCS_NAMES = malloc.c
|
||||
SRCS_NAMES = malloc.c \
|
||||
show_alloc_mem.c
|
||||
|
||||
SRCS_DIR = srcs
|
||||
|
||||
@ -35,7 +36,7 @@ LFT_DIR = libft/
|
||||
all: libft_malloc.so
|
||||
|
||||
exec: $(OBJS_DIR) $(OBJS) $(LFT)
|
||||
$(CC) -o a.out $(OBJS) $(LFT)
|
||||
$(CC) -o a.out $(FLAGS) main.c $(OBJS) $(LFT)
|
||||
|
||||
libft_malloc.so: $(NAME)
|
||||
ln -sf $(NAME) libft_malloc.so
|
||||
|
@ -6,13 +6,13 @@
|
||||
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
||||
# Updated: 2024/11/27 17:43:00 by tomoron ### ########.fr #
|
||||
# Updated: 2024/11/30 13:39:39 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = libftprintf.a
|
||||
|
||||
CC = gcc
|
||||
CC = cc
|
||||
|
||||
SRCS = ft_protected_atoi.c\
|
||||
ft_convert.c\
|
||||
|
20
srcs/free.c
Normal file
20
srcs/free.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/30 22:39:16 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "includes/malloc.h"
|
||||
|
||||
void ft_free(void *ptr)
|
||||
{
|
||||
t_alloc *alloc;
|
||||
|
||||
alloc = (t_alloc *)ptr - 1;
|
||||
}
|
58
srcs/includes/ft_printf.h
Executable file
58
srcs/includes/ft_printf.h
Executable file
@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/16 14:27:27 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_PRINTF_H
|
||||
# define FT_PRINTF_H
|
||||
# include <stdarg.h>
|
||||
# include <unistd.h>
|
||||
|
||||
typedef struct s_flags
|
||||
{
|
||||
int left_justify;
|
||||
int zero_padding;
|
||||
int always_sign_number;
|
||||
int blank_positive;
|
||||
int zero_x_prefix;
|
||||
int min_width;
|
||||
int precision;
|
||||
} t_flags;
|
||||
|
||||
int ft_printf(const char *str, ...);
|
||||
size_t ft_write_str_part(char **s);
|
||||
size_t ft_parse_arg(char **s, va_list args, int *err);
|
||||
size_t ft_convert(char *s, t_flags flags, va_list args, int err);
|
||||
size_t ft_putchar(char c);
|
||||
size_t ft_putstr(char *s);
|
||||
size_t ft_strlen(char const *s);
|
||||
int ft_protected_atoi(const char *s);
|
||||
int ft_isdigit(int c);
|
||||
size_t ft_print_unsigned_int(unsigned int nb, t_flags flags);
|
||||
size_t ft_print_signed_int(int nb, t_flags flags);
|
||||
size_t ft_fill_width(char c, int n);
|
||||
size_t ft_print_hex(unsigned int nb, t_flags flags, char mode);
|
||||
size_t ft_print_ptr(void *ptr, t_flags flags);
|
||||
size_t ft_print_str(char *s, t_flags flags);
|
||||
void ft_put_lu_nbr(long unsigned int nb);
|
||||
|
||||
//PRINTF_FD
|
||||
|
||||
int putchar_fd(int fd, const char c);
|
||||
int putstr_fd(int fd, const char *s);
|
||||
int putptr_fd(int fd, long unsigned int ptr);
|
||||
int putnbr_fd(int fd, long nbr);
|
||||
int puthexa_fd(int fd, unsigned int nbr, char c);
|
||||
int puthexa_ptr_fd(int fd, long unsigned int ptr);
|
||||
|
||||
int ft_printf_fd(int fd, const char *s, ...);
|
||||
int ft_print_arg(int fd, va_list args_lst, char c);
|
||||
|
||||
#endif
|
82
srcs/includes/libft.h
Executable file
82
srcs/includes/libft.h
Executable file
@ -0,0 +1,82 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libft.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/30 11:34:40 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFT_H
|
||||
# define LIBFT_H
|
||||
# include <stddef.h>
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include "./ft_printf.h"
|
||||
|
||||
typedef struct s_list
|
||||
{
|
||||
void *content;
|
||||
struct s_list *next;
|
||||
} t_list;
|
||||
|
||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
||||
char *ft_strnstr(const char *str, const char *to_find, size_t len);
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||
char *ft_strjoin_free(char *s1, char *s2, int free_flags);
|
||||
size_t ft_strlcpy(char *dest, const char *src, size_t size);
|
||||
size_t ft_strlcat(char *dest, const char *src, size_t size);
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
||||
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
||||
void *ft_memmove(void *dest, const void *src, size_t n);
|
||||
void *ft_memcpy(void *dest, const void *src, size_t n);
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void*));
|
||||
void ft_lstclear(t_list **lst, void (*del)(void*));
|
||||
char *ft_strtrim(char const *s1, char const *set);
|
||||
char *ft_strjoin(char const *s1, char const *s2);
|
||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
||||
void *ft_memchr(const void *s, int c, size_t n);
|
||||
void ft_lstadd_front(t_list **lst, t_list *new);
|
||||
char **ft_split(char const *str, char charset);
|
||||
void ft_lstadd_back(t_list **lst, t_list *new);
|
||||
char **ft_split_set(char *str, char *charset);
|
||||
int ft_str_is_only_char(char *str, char c);
|
||||
void *ft_memset(void *s, int c, size_t n);
|
||||
char *ft_get_color(int r, int g, int b);
|
||||
void *ft_calloc(size_t nb, size_t size);
|
||||
char *ft_strrchr(const char *s, int c);
|
||||
void ft_set_color(int r, int g, int b);
|
||||
char *ft_strchr(const char *s, int c);
|
||||
void ft_putendl_fd(char *s, int fd);
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
int ft_strcmp(char *s1, char *s2);
|
||||
char *ft_strdup(const char *src);
|
||||
void ft_bzero(void *s, size_t n);
|
||||
void ft_putnbr_fd(int n, int fd);
|
||||
void ft_free_str_arr(char **arr);
|
||||
int get_number_len(long int n);
|
||||
size_t ft_strlen(const char *str);
|
||||
void ft_swap(int *n1, int *n2);
|
||||
t_list *ft_lstnew(void *content);
|
||||
int ft_atoi(const char *str);
|
||||
t_list *ft_lstlast(t_list *lst);
|
||||
int ft_lstsize(t_list *lst);
|
||||
int ft_strisnbr(char *str);
|
||||
void ft_reset_color(void);
|
||||
int ft_isspace(char c);
|
||||
int ft_isalnum(int c);
|
||||
int ft_isalpha(int c);
|
||||
int ft_isascii(int c);
|
||||
int ft_isdigit(int c);
|
||||
int ft_isprint(int c);
|
||||
int ft_tolower(int c);
|
||||
int ft_toupper(int c);
|
||||
char *ft_itoa(int n);
|
||||
|
||||
#endif
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/29 19:10:52 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/11/30 13:53:11 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,8 +16,11 @@
|
||||
# include <stddef.h>
|
||||
# include <sys/mman.h>
|
||||
# include <unistd.h>
|
||||
# include "libft.h"
|
||||
|
||||
# define TINY_BLOC_SIZE 16384
|
||||
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
|
||||
|
||||
# define TINY_BLOC_SIZE (PAGE_SIZE * 4)
|
||||
# define SMALL_BLOC_SIZE 524288
|
||||
|
||||
# define TINY_MALLOC 1024
|
||||
@ -42,10 +45,9 @@ typedef struct s_allocations
|
||||
t_mem_bloc *large;
|
||||
} t_allocations;
|
||||
|
||||
t_allocations g_allocs;
|
||||
|
||||
extern t_allocations g_allocs;
|
||||
|
||||
void *ft_malloc(size_t size);
|
||||
void show_alloc_mem();
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/29 19:00:29 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/11/30 18:01:25 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
t_allocations g_allocs;
|
||||
|
||||
void *get_memory(size_t size)
|
||||
{
|
||||
void *ptr;
|
||||
@ -30,18 +32,6 @@ void *get_memory(size_t size)
|
||||
return (ptr);
|
||||
}
|
||||
|
||||
void *allocate_memory(size_t size, t_mem_bloc **bloc)
|
||||
{
|
||||
t_mem_bloc *new;
|
||||
|
||||
new = get_memory(size + sizeof(t_mem_bloc));
|
||||
if (!new)
|
||||
return (0);
|
||||
new->next = *bloc;
|
||||
*bloc = new;
|
||||
return ((char *)new + sizeof(t_mem_bloc));
|
||||
}
|
||||
|
||||
t_alloc *reserve_addr(t_alloc *addr, size_t size, t_alloc *prev,
|
||||
t_mem_bloc *bloc)
|
||||
{
|
||||
@ -72,12 +62,13 @@ t_alloc *get_suitable_addr_in_bloc(t_mem_bloc *bloc, size_t size)
|
||||
space_left = bloc->space_left;
|
||||
while (tmp->next)
|
||||
{
|
||||
free_space = (tmp->next - tmp) - (tmp->size + sizeof(t_alloc));
|
||||
free_space = ((unsigned long)tmp->next - (unsigned long)tmp) - (tmp->size + sizeof(t_alloc));
|
||||
if (free_space >= size + sizeof(t_alloc))
|
||||
return (reserve_addr(
|
||||
(void *)((char *)tmp->next + tmp->size + sizeof(t_alloc)),
|
||||
size, tmp, bloc));
|
||||
space_left -= free_space;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (space_left >= size + sizeof(t_alloc))
|
||||
return (reserve_addr(
|
||||
@ -133,21 +124,17 @@ void *pre_allocated(size_t size, t_mem_bloc **bloc, int is_small)
|
||||
|
||||
void *ft_malloc(size_t size)
|
||||
{
|
||||
t_mem_bloc *new;
|
||||
|
||||
if (size <= TINY_MALLOC)
|
||||
return (pre_allocated(size, &(g_allocs.tiny), 0));
|
||||
else if (size <= SMALL_MALLOC)
|
||||
return (pre_allocated(size, &(g_allocs.small), 1));
|
||||
else
|
||||
return (allocate_memory(size, &(g_allocs.large)));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
void *ptr;
|
||||
void *ptr2;
|
||||
|
||||
ptr = ft_malloc(10);
|
||||
ptr2 = ft_malloc(2000);
|
||||
printf("%p\n", ptr);
|
||||
printf("%p\n", ptr2);
|
||||
new = get_memory(size + sizeof(t_mem_bloc));
|
||||
if (!new)
|
||||
return (0);
|
||||
new->next = g_allocs.large;
|
||||
g_allocs.large = new;
|
||||
return ((char *)new + sizeof(t_mem_bloc));
|
||||
}
|
||||
|
102
srcs/show_alloc_mem.c
Normal file
102
srcs/show_alloc_mem.c
Normal file
@ -0,0 +1,102 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* show_alloc_mem.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/30 15:50:14 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "includes/malloc.h"
|
||||
|
||||
void put_ulnbr_base(unsigned long nbr, char *base)
|
||||
{
|
||||
unsigned long base_len;
|
||||
|
||||
base_len = ft_strlen(base);
|
||||
if(nbr >= base_len)
|
||||
{
|
||||
put_ulnbr_base(nbr / base_len, base);
|
||||
write(1, base + (nbr % base_len), 1);
|
||||
}
|
||||
else
|
||||
write(1, base + nbr, 1);
|
||||
}
|
||||
|
||||
size_t show_allocs(t_alloc *alloc)
|
||||
{
|
||||
size_t nb_bytes;
|
||||
|
||||
nb_bytes = 0;
|
||||
while(alloc)
|
||||
{
|
||||
nb_bytes += alloc->size;
|
||||
write(1, "0x", 2);
|
||||
put_ulnbr_base((unsigned long)alloc + sizeof(t_alloc), "0123456789ABCDEF");
|
||||
write(1, " - 0x", 5);
|
||||
put_ulnbr_base((unsigned long)alloc +sizeof(t_alloc) + alloc->size, "0123456789ABCDEF");
|
||||
write(1, " : ", 3);
|
||||
put_ulnbr_base(alloc->size, "0123456789");
|
||||
write(1, " bytes\n", 7);
|
||||
alloc = alloc->next;
|
||||
}
|
||||
return(nb_bytes);
|
||||
}
|
||||
|
||||
size_t show_pre_allocated(char *type, t_mem_bloc *bloc)
|
||||
{
|
||||
size_t nb_bytes;
|
||||
|
||||
nb_bytes = 0;
|
||||
while(bloc)
|
||||
{
|
||||
write(1, type, ft_strlen(type));
|
||||
write(1, " : 0x", 5);
|
||||
put_ulnbr_base((unsigned long)bloc, "0123456789ABCDEF");
|
||||
write(1, "\n", 1);
|
||||
nb_bytes += show_allocs((t_alloc *)(bloc + 1));
|
||||
bloc = bloc->next;
|
||||
}
|
||||
return(nb_bytes);
|
||||
}
|
||||
|
||||
size_t show_large()
|
||||
{
|
||||
t_mem_bloc *bloc;
|
||||
size_t total_size;
|
||||
|
||||
bloc = g_allocs.large;
|
||||
total_size = 0;
|
||||
while(bloc)
|
||||
{
|
||||
write(1, "LARGE : 0x", 10);
|
||||
put_ulnbr_base((unsigned long)bloc, "0123456789ABCDEF");
|
||||
write(1,"\n", 1);
|
||||
write(1, "0x", 2);
|
||||
put_ulnbr_base((unsigned long)(bloc + 1), "0123456789ABCDEF");
|
||||
write(1, " - 0x", 5);
|
||||
put_ulnbr_base((unsigned long)(bloc + 1) + bloc->space_left, "0123456789ABCDEF");
|
||||
write(1, " : ", 3);
|
||||
put_ulnbr_base(bloc->space_left, "0123456789");
|
||||
write(1, " bytes\n", 7);
|
||||
total_size += bloc->space_left;
|
||||
bloc = bloc->next;
|
||||
}
|
||||
return(total_size);
|
||||
}
|
||||
|
||||
void show_alloc_mem()
|
||||
{
|
||||
size_t total;
|
||||
|
||||
total = 0;
|
||||
total += show_pre_allocated("TINY", g_allocs.tiny);
|
||||
total += show_pre_allocated("SMALL", g_allocs.small);
|
||||
total += show_large();
|
||||
write(1, "Total : ", 8);
|
||||
put_ulnbr_base(total, "0123456789");
|
||||
write(1, " bytes\n", 6);
|
||||
}
|
Reference in New Issue
Block a user