On 11/07/14 01:09, Jeff King wrote: > On Fri, Jul 11, 2014 at 12:58:31AM +0100, Ramsay Jones wrote: > >> #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. Yeah, I'm not a fan of _large_ multi-line macros myself. Now that DEFINE_ALLOCATOR has slimmed down, I don't mind it so much. However, I agree that doing away with the macro leads to easier to read code. (I also don't mind the extra lines). ATB, Ramsay Jones -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html