Re: [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function
- From
Jeff King <peff@peff.net>
- Date
- Jul 11, 2014, 00:09 UTC
- Message-ID
- <20140711000956.GA11360@sigill.intra.peff.net>
- In-Reply-To
- <53BF28A7.7000903@ramsay1.demon.co.uk>
On Fri, Jul 11, 2014 at 12:58:31AM +0100, Ramsay Jones wrote:
Show 20 quoted lines
> #define DEFINE_ALLOCATOR(name, type) \
> -static unsigned int name##_allocs; \
> +static struct alloc_state name##_state; \
> void *alloc_##name##_node(void) \
> { \
> - static int nr; \
> - static type *block; \
> - void *ret; \
> - \
> - if (!nr) { \
> - nr = BLOCKING; \
> - block = xmalloc(BLOCKING * sizeof(type)); \
> - } \
> - nr--; \
> - name##_allocs++; \
> - ret = block++; \
> - memset(ret, 0, sizeof(type)); \
> - return ret; \
> + return alloc_node(&name##_state, sizeof(type)); \
> }Yay. Not only does this solve the problem, but it gets rid of nasty multi-line macro. In fact, I kind of wonder if we should just do away with the macro entirely, and write out:
static struct alloc_state blob_state;
void alloc_blob_node(void)
{
return alloc_node(&blob_state, sizeof(struct blob));
}It's more lines, but it is probably less obfuscated to a reader.
-Peff