1 /*
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
5 * package.
6 *
7 * Copyright (c) 2001-2021 The strace developers.
8 * All rights reserved.
9 *
10 * SPDX-License-Identifier: LGPL-2.1-or-later
11 */
12
13 #ifndef STRACE_XMALLOC_H
14 # define STRACE_XMALLOC_H
15
16 # include <stddef.h>
17 # include "gcc_compat.h"
18
19 # define xcalloc strace_calloc
20 # define xmalloc strace_malloc
21
22 /** Allocate memory, die if the allocation has failed. */
23 void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
24
25 /**
26 * Allocate an array and zero it out (similar to calloc), die if the allocation
27 * has failed or if the product of nmemb and size is too big.
28 */
29 void *xcalloc(size_t nmemb, size_t size)
30 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
31
32 /** Wrapper for xcalloc(1, size) with xmalloc-like interface. */
33 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1))
34 static inline void *
35 xzalloc(size_t size)
36 {
37 return xcalloc(1, size);
38 }
39
40 /**
41 * Allocate an array, but do not zero it out, die if the allocation
42 * has failed or if the product of nmemb and size is too big.
43 */
44 void *xallocarray(size_t nmemb, size_t size)
45 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
46
47 /**
48 * Reallocate memory for the array, die if the allocation has failed or
49 * if the product of nmemb and size is too big.
50 */
51 void *xreallocarray(void *ptr, size_t nmemb, size_t size)
52 ATTRIBUTE_ALLOC_SIZE((2, 3));
53
54 /**
55 * Utility function for the simplification of managing various dynamic arrays.
56 * Knows better how to resize arrays. Dies if there's no enough memory.
57 *
58 * @param[in] ptr Pointer to the array to be resized. If ptr is NULL,
59 * new array is allocated.
60 * @param[in, out] nmemb Pointer to the current member count. If ptr is
61 * NULL, it specifies number of members in the newly
62 * created array. If ptr is NULL and nmemb is 0,
63 * number of members in the new array is decided by
64 * the function. Member count is updated by the
65 * function to the new value.
66 * @param[in] memb_size Size of array member in bytes.
67 * @return Pointer to the (re)allocated array.
68 */
69 void *xgrowarray(void *ptr, size_t *nmemb, size_t memb_size);
70
71 /*
72 * Note that the following four functions return NULL when NULL is specified
73 * and not when allocation is failed, since, as the "x" prefix implies,
74 * the allocation failure leads to program termination, so we may re-purpose
75 * this return value and simplify the idiom "str ? xstrdup(str) : NULL".
76 */
77 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
78 char *xstrndup(const char *str, size_t n) ATTRIBUTE_MALLOC;
79
80 /** Implements xmalloc + memcpy idiom. */
81 void *xmemdup(const void *src, size_t size)
82 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
83 /** Implements xallocarray + memcpy idiom. */
84 void *xarraydup(const void *src, size_t nmemb, size_t memb_size)
85 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2, 3));
86
87 # define xobjdup(src_) xmemdup(src_, sizeof(*(src_)))
88
89 /**
90 * Analogous to asprintf, die in case of an error.
91 */
92 char *xasprintf(const char *fmt, ...)
93 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_MALLOC;
94
95 #endif /* !STRACE_XMALLOC_H */