]> granicus.if.org Git - strace/blobdiff - xmalloc.c
xlat: add ability to specify a default string to print_xlat_ex
[strace] / xmalloc.c
index c2c50f8c618f69c026cc70233869cfea0b63032f..4a318b6154f3254f803b5d20fbad7b7676449994 100644 (file)
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
+ * Copyright (c) 2015-2017 The strace developers.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -88,6 +89,28 @@ xreallocarray(void *ptr, size_t nmemb, size_t size)
        return p;
 }
 
+void *
+xgrowarray(void *const ptr, size_t *const nmemb, const size_t memb_size)
+{
+       /* this is the same value as glibc DEFAULT_MXFAST */
+       enum { DEFAULT_ALLOC_SIZE = 64 * SIZEOF_LONG / 4 };
+
+       size_t grow_memb;
+
+       if (ptr == NULL)
+               grow_memb = *nmemb ? 0 :
+                       (DEFAULT_ALLOC_SIZE + memb_size - 1) / memb_size;
+       else
+               grow_memb = (*nmemb >> 1) + 1;
+
+       if ((*nmemb + grow_memb) < *nmemb)
+               die_out_of_memory();
+
+       *nmemb += grow_memb;
+
+       return xreallocarray(ptr, *nmemb, memb_size);
+}
+
 char *
 xstrdup(const char *str)
 {