​codexion /
src / sim / free.c
60 commits
07-07-2026
55 lines 1.6 kB
 1/* ************************************************************************** */
 2/*                                                                            */
 3/*                                                        :::      ::::::::   */
 4/*   free.c                                             :+:      :+:    :+:   */
 5/*                                                    +:+ +:+         +:+     */
 6/*   By: mleitao <marvin@42.fr>                     +#+  +:+       +#+        */
 7/*                                                +#+#+#+#+#+   +#+           */
 8/*   Created: 2026/07/02 23:56:25 by mleitao           #+#    #+#             */
 9/*   Updated: 2026/07/07 00:52:18 by mleitao          ###   ########.fr       */
10/*                                                                            */
11/* ************************************************************************** */
12
13#include "codexion.h"
14#include "sim.h"
15#include "heap.h"
16#include <stdlib.h>
17
18void	destroy_dongles(t_dongle *d, unsigned int count)
19{
20	unsigned int	i;
21
22	i = 0;
23	while (i < count)
24	{
25		pthread_mutex_destroy(&d[i].lock);
26		pthread_cond_destroy(&d[i].wait);
27		heap_free(&d[i].queue);
28		i++;
29	}
30}
31
32void	destroy_coders(t_coder *c, unsigned int count)
33{
34	unsigned int	i;
35
36	i = 0;
37	while (i < count)
38	{
39		pthread_mutex_destroy(&c[i].state_lock);
40		i++;
41	}
42}
43
44void	free_sim(t_sim *sim)
45{
46	unsigned int	n;
47
48	n = sim->args.v[ARG_CODERS];
49	destroy_dongles(sim->dongles, n);
50	destroy_coders(sim->coders, n);
51	pthread_mutex_destroy(&sim->log_lock);
52	pthread_mutex_destroy(&sim->stop_lock);
53	free(sim->coders);
54	free(sim->dongles);
55}