]> git.ipfire.org Git - thirdparty/squid.git/blame - compat/xalloc.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / compat / xalloc.h
CommitLineData
37be9888 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
37be9888
AJ
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
25f98340
AJ
9#ifndef _SQUID_COMPAT_XALLOC_H
10#define _SQUID_COMPAT_XALLOC_H
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
f53969cc
SM
16/**
17 * xcalloc() - same as calloc(3). Used for portability.
f70cf39b 18 * Never returns nullptr; fatal on error.
f53969cc
SM
19 *
20 * Define failure_notify to receive error message.
21 * otherwise perror() is used to display it.
22 */
23void *xcalloc(size_t n, size_t sz);
25f98340 24
f53969cc
SM
25/**
26 * xmalloc() - same as malloc(3). Used for portability.
f70cf39b 27 * Never returns nullptr; fatal on error.
f53969cc
SM
28 *
29 * Define failure_notify to receive error message.
30 * otherwise perror() is used to display it.
31 */
32void *xmalloc(size_t sz);
25f98340 33
f53969cc
SM
34/**
35 * xrealloc() - same as realloc(3). Used for portability.
f70cf39b 36 * Never returns nullptr; fatal on error.
f53969cc
SM
37 */
38void *xrealloc(void *s, size_t sz);
25f98340 39
f53969cc
SM
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 */
47void free_const(const void *s);
25f98340 48
f53969cc
SM
49/**
50 * xfree() - same as free(3). Used for portability.
51 * Accepts pointers to dynamically allocated const data.
f70cf39b 52 * Will not call free(3) if the pointer is nullptr.
f53969cc
SM
53 *
54 * Pointer is left with a value on completion.
f70cf39b 55 * Use safe_free() if the pointer needs to be set to nullptr afterward.
f53969cc
SM
56 *
57 * Define failure_notify to receive error message.
58 * otherwise perror() is used to display it.
59 */
60static inline void xfree(const void *p) { if (p) free_const(p); }
25f98340 61
f53969cc
SM
62/**
63 * safe_free() - same as free(3). Used for portability.
64 * Accepts pointers to dynamically allocated const data.
f70cf39b
AJ
65 * Will not call free(3) if the pointer is nullptr.
66 * Sets the pointer to nullptr on completion.
f53969cc
SM
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 */
f70cf39b 73#define safe_free(x) while ((x)) { free_const((x)); (x) = nullptr; }
25f98340 74
25f98340
AJ
75#ifdef __cplusplus
76}
77#endif
78
0210ab23
AJ
79#if XMALLOC_STATISTICS
80extern void malloc_statistics(void (*func) (int, int, int, void *), void *data);
81#endif
82
25f98340 83#endif /* _SQUID_COMPAT_XALLOC_H */
f53969cc 84