summaryrefslogtreecommitdiff
path: root/xdiff/xmacros.h
diff options
context:
space:
mode:
authorPhillip Wood <phillip.wood@dunelm.org.uk>2022-07-08 16:25:19 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-07-08 16:34:30 (GMT)
commitf7b587bf656574103a0a2ad9c2337b0d15c0e92d (patch)
tree00bc34a5d6773022acd2a3a9434d50b7a9dc073a /xdiff/xmacros.h
parent848fd5ae5b6506df50ae872302d94b794dc57d51 (diff)
downloadgit-f7b587bf656574103a0a2ad9c2337b0d15c0e92d.zip
git-f7b587bf656574103a0a2ad9c2337b0d15c0e92d.tar.gz
git-f7b587bf656574103a0a2ad9c2337b0d15c0e92d.tar.bz2
xdiff: introduce XDL_ALLOC_GROW()
Add a helper to grow an array. This is analogous to ALLOC_GROW() in the rest of the codebase but returns −1 on allocation failure to accommodate other users of libxdiff such as libgit2. It will also return a error if the multiplication overflows while calculating the new allocation size. Note that this keeps doubling on reallocation like the code it is replacing rather than increasing the existing size by half like ALLOC_GROW(). It does however copy ALLOC_GROW()'s trick of adding a small amount to the new allocation to avoid a lot of reallocations at small sizes. Note that xdl_alloc_grow_helper() uses long rather than size_t for `nr` and `alloc` to match the existing code. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'xdiff/xmacros.h')
-rw-r--r--xdiff/xmacros.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/xdiff/xmacros.h b/xdiff/xmacros.h
index 0977d16..8487bb3 100644
--- a/xdiff/xmacros.h
+++ b/xdiff/xmacros.h
@@ -58,4 +58,14 @@ do { \
/* Allocate an array of nr zeroed out elements, returns NULL on failure */
#define XDL_CALLOC_ARRAY(p, nr) ((p) = xdl_calloc(nr, sizeof(*(p))))
+/*
+ * Ensure array p can accommodate at least nr elements, growing the
+ * array and updating alloc (which is the number of allocated
+ * elements) as necessary. Frees p and returns -1 on failure, returns
+ * 0 on success
+ */
+#define XDL_ALLOC_GROW(p, nr, alloc) \
+ (-!((nr) <= (alloc) || \
+ ((p) = xdl_alloc_grow_helper((p), (nr), &(alloc), sizeof(*(p))))))
+
#endif /* #if !defined(XMACROS_H) */