add helpers for allocating flex-array structs
commit3689539127f786f32ff04da374d37cc1f72fb918
authorJeff King <peff@peff.net>
Mon, 22 Feb 2016 22:43:25 +0000 (22 17:43 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Feb 2016 22:50:32 +0000 (22 14:50 -0800)
treeb3e860b9335b096490a581e6b2fdeb3ac9e6e741
parente7792a74bcf7fcd554b4546fc91122b8c2af7d75
add helpers for allocating flex-array structs

Allocating a struct with a flex array is pretty simple in
practice: you over-allocate the struct, then copy some data
into the over-allocation. But it can be a slight pain to
make sure you're allocating and copying the right amounts.

This patch adds a few helpers to turn simple cases of
flex-array struct allocation into a one-liner that properly
checks for overflow. See the embedded documentation for
details.

Ideally we could provide a more flexible version that could
handle multiple strings, like:

  FLEX_ALLOC_FMT(ref, name, "%s%s", prefix, name);

But we have to implement this as a macro (because of the
offset calculation of the flex member), which means we would
need all compilers to support variadic macros.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h