git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function

From
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Date
Jul 11, 2014, 00:33 UTC
Message-ID
<53BF30D4.7010105@ramsay1.demon.co.uk>
In-Reply-To
<20140711000956.GA11360@sigill.intra.peff.net>
On 11/07/14 01:09, Jeff King wrote:
Show 34 quoted lines
> 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

Previous: Jeff King
Message 3 of 3 in “alloc.c: remove the alloc_raw_commit_node() function”
  1. 1/2 alloc.c: remove the alloc_raw_commit_node() functionRamsay Jones, Jul 10, 2014
  2. Jeff KingJul 11, 2014
  3. Ramsay JonesJul 11, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.