minishell
ft_calloc.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* ft_calloc.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: kfujita <kfujita@student.42tokyo.jp> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2022/04/18 03:15:08 by kfujita #+# #+# */
9 /* Updated: 2023/02/06 23:14:48 by kfujita ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "ft_mem.h"
14 #include "../ft_math/ft_math.h"
15 #include <stdlib.h>
16 
17 void *ft_calloc_nofill(size_t count, size_t size)
18 {
19  size_t bytes_to_allocate;
20 
21  if (!can_mulp(count, size))
22  return (NULL);
23  bytes_to_allocate = count * size;
24  if (bytes_to_allocate <= 0)
25  bytes_to_allocate = 1;
26  return (malloc(bytes_to_allocate));
27 }
28 
29 void *ft_calloc(size_t count, size_t size)
30 {
31  void *p_ret;
32 
33  p_ret = ft_calloc_nofill(count, size);
34  if (p_ret != NULL)
35  ft_bzero(p_ret, count * size);
36  return (p_ret);
37 }
bool can_mulp(size_t a, size_t b)
Definition: can_mul.c:50
void ft_bzero(void *s, size_t n)
Definition: ft_bzero.c:15
void * ft_calloc_nofill(size_t count, size_t size)
Definition: ft_calloc.c:17
void * ft_calloc(size_t count, size_t size)
Definition: ft_calloc.c:29