-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsafe_mem.c
More file actions
54 lines (46 loc) · 1.56 KB
/
Copy pathsafe_mem.c
File metadata and controls
54 lines (46 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* safe_mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <jkong@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/30 21:25:54 by jkong #+# #+# */
/* Updated: 2022/06/23 22:48:50 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "safe_mem.h"
static void *_required_non_null(void *ptr)
{
if (!ptr)
exit(EXIT_FAILURE);
return (ptr);
}
static void *_fill_zero(void *b, size_t len)
{
const size_t long_len = len / sizeof(long);
const unsigned char c = 0;
const long l = 0L;
size_t i;
i = 0;
while (i < long_len)
((long *)b)[i++] = l;
i *= sizeof(long);
while (i < len)
((unsigned char *)b)[i++] = c;
return (b);
}
void *malloc_safe(size_t size)
{
return (_required_non_null(malloc(size)));
}
void *calloc_safe(size_t count, size_t size)
{
const size_t final_size = count * size;
return (_fill_zero(malloc_safe(final_size), final_size));
}
int free_safe(void *ptr)
{
free(ptr);
return (0);
}