summaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2021-03-03 15:15:56 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2021-03-03 15:15:56 +0900
commit9fc73853a0480851a94e182525a26f0741c597d8 (patch)
treee640c7a45596cf700f06020fc303b10605b2e36b /git-compat-util.h
parent5fcb68584ed20e8097c8c0c7495351741675f40f (diff)
downloadgit-9fc73853a0480851a94e182525a26f0741c597d8.tar.gz
git-9fc73853a0480851a94e182525a26f0741c597d8.tar.bz2
git-9fc73853a0480851a94e182525a26f0741c597d8.zip
Imported Upstream version 2.10.2upstream/2.10.2
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h25
1 files changed, 14 insertions, 11 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index 2c949983..b4d9c2aa 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -798,6 +798,14 @@ extern FILE *fopen_for_writing(const char *path);
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
+#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
+ BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
+static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
+{
+ if (n)
+ memcpy(dst, src, st_mult(size, n));
+}
+
/*
* These functions help you allocate structs with flex arrays, and copy
* the data directly into the array. For example, if you had:
@@ -840,11 +848,14 @@ extern FILE *fopen_for_writing(const char *path);
* times, and it must be assignable as an lvalue.
*/
#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
- (x) = NULL; /* silence -Wuninitialized for offset calculation */ \
- (x) = xalloc_flex(sizeof(*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
} while (0)
#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
- (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((x) + 1, (buf), flex_array_len_); \
(x)->ptrname = (void *)((x)+1); \
} while(0)
#define FLEX_ALLOC_STR(x, flexname, str) \
@@ -852,14 +863,6 @@ extern FILE *fopen_for_writing(const char *path);
#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
-static inline void *xalloc_flex(size_t base_len, size_t offset,
- const void *src, size_t src_len)
-{
- unsigned char *ret = xcalloc(1, st_add3(base_len, src_len, 1));
- memcpy(ret + offset, src, src_len);
- return ret;
-}
-
static inline char *xstrdup_or_null(const char *str)
{
return str ? xstrdup(str) : NULL;