2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2015-2019 The strace developers.
6 * SPDX-License-Identifier: LGPL-2.1-or-later
16 #include "error_prints.h"
20 die_out_of_memory(void)
28 error_msg_and_die("Out of memory");
34 void *p = malloc(size);
43 xcalloc(size_t nmemb, size_t size)
45 void *p = calloc(nmemb, size);
53 #define HALF_SIZE_T (((size_t) 1) << (sizeof(size_t) * 4))
56 xreallocarray(void *ptr, size_t nmemb, size_t size)
58 size_t bytes = nmemb * size;
60 if ((nmemb | size) >= HALF_SIZE_T &&
61 size && bytes / size != nmemb)
64 void *p = realloc(ptr, bytes);
73 xgrowarray(void *const ptr, size_t *const nmemb, const size_t memb_size)
75 /* this is the same value as glibc DEFAULT_MXFAST */
76 enum { DEFAULT_ALLOC_SIZE = 64 * SIZEOF_LONG / 4 };
81 grow_memb = *nmemb ? 0 :
82 (DEFAULT_ALLOC_SIZE + memb_size - 1) / memb_size;
84 grow_memb = (*nmemb >> 1) + 1;
86 if ((*nmemb + grow_memb) < *nmemb)
91 return xreallocarray(ptr, *nmemb, memb_size);
95 xstrdup(const char *str)
100 char *p = strdup(str);
109 xstrndup(const char *str, size_t n)