]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/xalloc.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / compat / xalloc.h
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef _SQUID_COMPAT_XALLOC_H
10 #define _SQUID_COMPAT_XALLOC_H
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 /**
17 * xcalloc() - same as calloc(3). Used for portability.
18 * Never returns NULL; fatal on error.
19 *
20 * Define failure_notify to receive error message.
21 * otherwise perror() is used to display it.
22 */
23 void *xcalloc(size_t n, size_t sz);
24
25 /**
26 * xmalloc() - same as malloc(3). Used for portability.
27 * Never returns NULL; fatal on error.
28 *
29 * Define failure_notify to receive error message.
30 * otherwise perror() is used to display it.
31 */
32 void *xmalloc(size_t sz);
33
34 /**
35 * xrealloc() - same as realloc(3). Used for portability.
36 * Never returns NULL; fatal on error.
37 */
38 void *xrealloc(void *s, size_t sz);
39
40 /**
41 * free_const() - Same as free(3). Used for portability.
42 * Accepts pointers to dynamically allocated const data.
43 *
44 * Define failure_notify to receive error message.
45 * otherwise perror() is used to display it.
46 */
47 void free_const(const void *s);
48
49 /**
50 * xfree() - same as free(3). Used for portability.
51 * Accepts pointers to dynamically allocated const data.
52 * Will not call free(3) if the pointer is NULL.
53 *
54 * Pointer is left with a value on completion.
55 * Use safe_free() if the pointer needs to be set to NULL afterward.
56 *
57 * Define failure_notify to receive error message.
58 * otherwise perror() is used to display it.
59 */
60 static inline void xfree(const void *p) { if (p) free_const(p); }
61
62 /**
63 * safe_free() - same as free(3). Used for portability.
64 * Accepts pointers to dynamically allocated const data.
65 * Will not call free(3) if the pointer is NULL.
66 * Sets the pointer to NULL on completion.
67 *
68 * Use xfree() if the pointer does not need to be set afterward.
69 *
70 * Define failure_notify to receive error message.
71 * otherwise perror() is used to display it.
72 */
73 #define safe_free(x) while ((x)) { free_const((x)); (x) = NULL; }
74
75 #ifdef __cplusplus
76 }
77 #endif
78
79 #if XMALLOC_STATISTICS
80 extern void malloc_statistics(void (*func) (int, int, int, void *), void *data);
81 #endif
82
83 #endif /* _SQUID_COMPAT_XALLOC_H */
84