1 #ifndef MY_ALLOC_H
2 #define MY_ALLOC_H
3 
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <string.h>
7 
8 static inline void *
my_calloc(size_t nmemb,size_t size)9 my_calloc(size_t nmemb, size_t size)
10 {
11 	void *ptr = calloc(nmemb, size);
12 	assert(ptr != NULL);
13 	return (ptr);
14 }
15 
16 static inline void *
my_malloc(size_t size)17 my_malloc(size_t size)
18 {
19 	void *ptr = malloc(size);
20 	assert(ptr != NULL);
21 	return (ptr);
22 }
23 
24 static inline void *
my_realloc(void * ptr,size_t size)25 my_realloc(void *ptr, size_t size)
26 {
27 	ptr = realloc(ptr, size);
28 	assert(ptr != NULL);
29 	return (ptr);
30 }
31 
32 static inline char *
my_strdup(const char * s)33 my_strdup(const char *s)
34 {
35 	char *ptr = strdup(s);
36 	assert(ptr != NULL);
37 	return (ptr);
38 }
39 
40 #define my_free(ptr) do { free(ptr); (ptr) = NULL; } while (0)
41 
42 #if defined(MY_ALLOC_WARN_DEPRECATED)
43 
44 static inline void *my_calloc_deprecated(size_t, size_t)
45 	__attribute__ ((deprecated("use my_calloc, not calloc")));
46 
47 static inline void *my_malloc_deprecated(size_t)
48 	__attribute__ ((deprecated("use my_malloc, not malloc")));
49 
50 static inline void *my_realloc_deprecated(void *, size_t)
51 	__attribute__ ((deprecated("use my_realloc, not realloc")));
52 
53 static inline void *
my_calloc_deprecated(size_t nmemb,size_t size)54 my_calloc_deprecated(size_t nmemb, size_t size)
55 {
56 	return calloc(nmemb, size);
57 }
58 
59 static inline void *
my_malloc_deprecated(size_t size)60 my_malloc_deprecated(size_t size)
61 {
62 	return malloc(size);
63 }
64 
65 static inline void *
my_realloc_deprecated(void * ptr,size_t size)66 my_realloc_deprecated(void *ptr, size_t size)
67 {
68 	return realloc(ptr, size);
69 }
70 
71 #define calloc	my_calloc_deprecated
72 #define malloc	my_malloc_deprecated
73 #define realloc	my_realloc_deprecated
74 
75 #endif /* MY_ALLOC_WARN_DEPRECATED */
76 
77 #endif /* MY_ALLOC_H */
78