libft
ft_lstnew.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* ft_lstnew.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2019/09/05 16:34:37 by unite #+# #+# */
10 /* Updated: 2020/07/16 03:00:51 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "libft.h"
15 #include <stdlib.h>
16 
31 t_list *ft_lstnew(void const *content, size_t content_size)
32 {
33  t_list *link;
34 
35  if (!(link = (t_list *)malloc(sizeof(t_list))))
36  return (NULL);
37  if (content)
38  {
39  if (!(link->content = ft_memalloc(content_size)))
40  {
41  ft_memdel((void **)&link);
42  return (NULL);
43  }
45  link->content_size = content_size;
46  }
47  else
48  {
49  link->content = NULL;
50  link->content_size = 0;
51  }
52  link->next = NULL;
53  return (link);
54 }
ft_memcpy
void * ft_memcpy(void *dst, const void *src, size_t n)
Replicates behaviour of memcpy from libc.
Definition: ft_memcpy.c:20
s_list::content_size
size_t content_size
The size of the data stored in bytes.
Definition: libft.h:37
s_list
A link in a multi-purpose linked list.
Definition: libft.h:34
s_list::content
void * content
The data contained in the link.
Definition: libft.h:36
s_list::next
struct s_list * next
The next link’s address or NULL if it’s the last link.
Definition: libft.h:38
libft.h
ft_lstnew
t_list * ft_lstnew(void const *content, size_t content_size)
Allocates a new list.
Definition: ft_lstnew.c:31
ft_memalloc
void * ft_memalloc(size_t size)
Allocates memory of a given size and initializes it to 0.
Definition: ft_memalloc.c:24
ft_memdel
void ft_memdel(void **ap)
Frees memory where a pointer points to and sets the pointer to NULL.
Definition: ft_memdel.c:22