2 * This file contains wrapper functions working with memory allocations,
3 * they just terminate the program in case of memory allocation failure.
4 * These functions can be used by various binaries included in the strace
7 * Copyright (c) 2001-2017 The strace developers.
10 * SPDX-License-Identifier: LGPL-2.1-or-later
13 #ifndef STRACE_XMALLOC_H
14 #define STRACE_XMALLOC_H
17 #include "gcc_compat.h"
19 #define xcalloc strace_calloc
20 #define xmalloc strace_malloc
22 void *xcalloc(size_t nmemb, size_t size)
23 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
24 void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
25 void *xreallocarray(void *ptr, size_t nmemb, size_t size)
26 ATTRIBUTE_ALLOC_SIZE((2, 3));
29 * Utility function for the simplification of managing various dynamic arrays.
30 * Knows better how to resize arrays. Dies if there's no enough memory.
32 * @param[in] ptr Pointer to the array to be resized. If ptr is NULL,
33 * new array is allocated.
34 * @param[in, out] nmemb Pointer to the current member count. If ptr is
35 * NULL, it specifies number of members in the newly
36 * created array. If ptr is NULL and nmemb is 0,
37 * number of members in the new array is decided by
38 * the function. Member count is updated by the
39 * function to the new value.
40 * @param[in] memb_size Size of array member in bytes.
41 * @return Pointer to the (re)allocated array.
43 void *xgrowarray(void *ptr, size_t *nmemb, size_t memb_size);
46 * Note that the following two functions return NULL when NULL is specified
47 * and not when allocation is failed, since, as the "x" prefix implies,
48 * the allocation failure leads to program termination, so we may re-purpose
49 * this return value and simplify the idiom "str ? xstrdup(str) : NULL".
51 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
52 char *xstrndup(const char *str, size_t n) ATTRIBUTE_MALLOC;
54 #endif /* !STRACE_XMALLOC_H */