/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   free_safe.c                                        :+:    :+:            */
/*                                                     +:+                    */
/*   By: whaffman <whaffman@student.codam.nl>         +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/25 15:09:34 by whaffman      #+#    #+#                 */
/*   Updated: 2025/02/26 16:20:46 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

void	free_safe(t_minishell *msh, void **ptr)
{
	t_list	*prev;
	t_list	*current;

	prev = NULL;
	current = msh->freelist;
	if (*ptr)
	{
		while (current)
		{
			if (current->content == *ptr)
			{
				if (prev)
					prev->next = current->next;
				else
					msh->freelist = current->next;
				free(*ptr);
				free(current);
				return ;
			}
			prev = current;
			current = current->next;
		}
		error_msg("free_safe", "pointer not found in freelist");
	}
	*ptr = NULL;
}
