From ec7a989cf45440e41d43e7fa2d2ce660fa499933 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 28 Jan 2025 13:21:34 +0100 Subject: [PATCH] Outsource init func to seperate file --- philo/Makefile | 2 +- philo/include/philo.h | 3 +- philo/src/init.c | 71 +++++++++++++++++++++++++++++++++++++++++++ philo/src/main.c | 57 +--------------------------------- 4 files changed, 75 insertions(+), 58 deletions(-) create mode 100644 philo/src/init.c diff --git a/philo/Makefile b/philo/Makefile index 0d55726..9cd7e26 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -9,7 +9,7 @@ CFLAGS = -Wall -Wextra -Werror HEADERS = -Iinclude VPATH := src -SRC := main.c ft_utils.c simulation.c philo_eat.c +SRC := main.c init.c ft_utils.c simulation.c philo_eat.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/philo/include/philo.h b/philo/include/philo.h index df1b451..07e4e96 100644 --- a/philo/include/philo.h +++ b/philo/include/philo.h @@ -6,7 +6,7 @@ /* By: dkaiser nbr_of_philos); + if (*philos == NULL) + return (ft_err(ERR_MALLOC)); + data->forks = (t_fork *)malloc(sizeof(t_fork) * data->nbr_of_philos); + if (data->forks == NULL) + { + free(*philos); + return (ft_err(ERR_MALLOC)); + } + result = pthread_mutex_init(&(data->pme_mutex), NULL); + if (result != 0) + { + free(*philos); + free(data->forks); + return (result); + } + result = init_philos(philos, data); + return (result); +} + +static int init_philos(t_philo **philos, t_phdata *data) +{ + int i; + int result; + + i = 0; + while (i < data->nbr_of_philos) + { + init_philo(&(*philos)[i], data, i + 1); + data->forks[i].owner = 0; + result = pthread_mutex_init(&(data->forks[i].mutex), NULL); + if (result != 0) + { + free(*philos); + free(data->forks); + return (result); + } + i++; + } + return (EXIT_SUCCESS); +} + +static void init_philo(t_philo *philo, t_phdata *data, int id) +{ + philo->id = id; + philo->is_alive = 1; + philo->times_must_eat = data->times_must_eat; + philo->data = data; + philo->last_time_eaten = ft_cur_time_in_ms(); +} diff --git a/philo/src/main.c b/philo/src/main.c index a43fe8d..ae0e54b 100644 --- a/philo/src/main.c +++ b/philo/src/main.c @@ -6,7 +6,7 @@ /* By: dkaiser id = id; - philo->is_alive = 1; - philo->times_must_eat = data->times_must_eat; - philo->data = data; - philo->last_time_eaten = ft_cur_time_in_ms(); -} - -int init_philos(t_philo **philos, t_phdata *data) -{ - int i; - int result; - - i = 0; - while (i < data->nbr_of_philos) - { - init_philo(&(*philos)[i], data, i + 1); - data->forks[i].owner = 0; - result = pthread_mutex_init(&(data->forks[i].mutex), NULL); - if (result != 0) - { - free(*philos); - free(data->forks); - return (result); - } - i++; - } - return (EXIT_SUCCESS); -} - -int init(t_philo **philos, t_phdata *data) -{ - int result; - - *philos = (t_philo *)malloc(sizeof(t_philo) * data->nbr_of_philos); - if (*philos == NULL) - return (ft_err(ERR_MALLOC)); - data->forks = (t_fork *)malloc(sizeof(t_fork) * data->nbr_of_philos); - if (data->forks == NULL) - { - free(*philos); - return (ft_err(ERR_MALLOC)); - } - result = pthread_mutex_init(&(data->pme_mutex), NULL); - if (result != 0) - { - free(*philos); - free(data->forks); - return (result); - } - result = init_philos(philos, data); - return (result); -} - int main(int argc, char *argv[]) { t_phdata data; -- 2.47.2