-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalloc.h
More file actions
65 lines (43 loc) · 1.18 KB
/
alloc.h
File metadata and controls
65 lines (43 loc) · 1.18 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
55
56
57
58
59
60
61
62
63
64
#ifndef _ALLOC_H
#define _ALLOC_H
#include <stdlib.h> // calloc, malloc, free, realloc
#include <string.h> // strdup
#include "tools.h" // exitOnErrSyst
// fonctions qui permettent de gérer les erreurs pour les fonctions d'allocations dynamique
static inline void *xcalloc(size_t nmemb, size_t size) {
void *ret=calloc(nmemb, size);
if(nmemb && size && !ret)
exitOnErrSyst("calloc", NULL);
return ret;
}
static inline void *xmalloc(size_t size) {
void *ret=malloc(size);
if(size && !ret)
exitOnErrSyst("malloc", NULL);
return ret;
}
static inline void xfree(void *ptr) {
if(!ptr)
exitOnErrSyst("free", NULL);
free(ptr);
}
static inline void *xrealloc(void *ptr, size_t size) {
void *ret=realloc(ptr, size);
if(size && !ret)
exitOnErrSyst("realloc", NULL);
return ret;
}
static inline char *xstrdup(const char *s) {
char *ret=strdup(s);
if(s && !ret)
exitOnErrSyst("strdup", (char *)s);
return ret;
}
static inline void *xmemdup(const void *s, size_t n) {
void *ret=malloc(n);
if(n && !ret)
exitOnErrSyst("memdup", NULL);
memcpy(ret, s, n);
return ret;
}
#endif