forked from CoolerVoid/Mosca
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_ops.c
62 lines (43 loc) · 780 Bytes
/
mem_ops.c
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
55
56
57
58
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "mem_ops.h"
static void *xmalloc_fatal(size_t size)
{
if ( size == 0 )
return NULL;
DEBUG("\n Memory FAILURE...\n");
exit(1);
}
void *xmalloc (size_t size)
{
void *ptr = malloc (size);
if (ptr == NULL)
return xmalloc_fatal(size);
return ptr;
}
void *xcalloc (size_t mem, size_t size)
{
void *ptr = calloc (mem, size);
if (ptr == NULL)
return xmalloc_fatal(mem*size);
return ptr;
}
void *xrealloc (void *ptr, size_t size)
{
void *p = realloc (ptr, size);
if (p == NULL)
return xmalloc_fatal(size);
return p;
}
void xfree(void **ptr)
{
assert(ptr);
if( ptr != NULL )
{
free(*ptr);
*ptr=NULL;
}
}