]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/xalloc.h
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_COMPAT_XALLOC_H
10 #define SQUID_COMPAT_XALLOC_H
17 * xcalloc() - same as calloc(3). Used for portability.
18 * Never returns nullptr; fatal on error.
20 * Define failure_notify to receive error message.
21 * otherwise perror() is used to display it.
23 void *xcalloc(size_t n
, size_t sz
);
26 * xmalloc() - same as malloc(3). Used for portability.
27 * Never returns nullptr; fatal on error.
29 * Define failure_notify to receive error message.
30 * otherwise perror() is used to display it.
32 void *xmalloc(size_t sz
);
35 * xrealloc() - same as realloc(3). Used for portability.
36 * Never returns nullptr; fatal on error.
38 void *xrealloc(void *s
, size_t sz
);
41 * free_const() - Same as free(3). Used for portability.
42 * Accepts pointers to dynamically allocated const data.
44 * Define failure_notify to receive error message.
45 * otherwise perror() is used to display it.
47 void free_const(const void *s
);
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 nullptr.
54 * Pointer is left with a value on completion.
55 * Use safe_free() if the pointer needs to be set to nullptr afterward.
57 * Define failure_notify to receive error message.
58 * otherwise perror() is used to display it.
60 static inline void xfree(const void *p
) { if (p
) free_const(p
); }
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 nullptr.
66 * Sets the pointer to nullptr on completion.
68 * Use xfree() if the pointer does not need to be set afterward.
70 * Define failure_notify to receive error message.
71 * otherwise perror() is used to display it.
73 #define safe_free(x) while ((x)) { free_const((x)); (x) = nullptr; }
79 #if XMALLOC_STATISTICS
80 extern void malloc_statistics(void (*func
) (int, int, int, void *), void *data
);
83 #endif /* SQUID_COMPAT_XALLOC_H */