From: Eric Sunshine Date: Tue, 16 Feb 2016 01:47:30 GMT Subject: Re: [PATCH 04/18] add helpers for allocating flex-array structs Message-ID: In-Reply-To: <20160215215054.GD10287@sigill.intra.peff.net> On Mon, Feb 15, 2016 at 4:50 PM, Jeff King wrote: > 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 into a Grammo: "cases of into" > one-liner that properly checks for overflow. See the > embedded documentation for details. > [...] > Signed-off-by: Jeff King > --- > diff --git a/git-compat-util.h b/git-compat-util.h > @@ -782,6 +782,68 @@ extern FILE *fopen_for_writing(const char *path); > + * struct foo *f; > + * FLEX_ALLOC_STR(f, name, src); > + * > + * and "name" will point to a block of memory after the struct, which will be > + * freed along with the struct (but the pointer can be repoined anywhere). "repoined"? > + * The *_STR variants accept a string parameter rather than a ptr/len > + * combination.