Git development
 help / color / mirror / Atom feed
* [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function
@ 2014-07-10 23:58 Ramsay Jones
  2014-07-11  0:09 ` Jeff King
  0 siblings, 1 reply; 3+ messages in thread
From: Ramsay Jones @ 2014-07-10 23:58 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, GIT Mailing-list


In order to encapsulate the setting of the unique commit index, commit
969eba63 ("commit: push commit_index update into alloc_commit_node",
10-06-2014) introduced a (logically private) intermediary allocator
function. However, this function (alloc_raw_commit_node()) was declared
as a public function, which undermines its entire purpose.

Introduce an inline function, alloc_node(), which implements the main
logic of the allocator used by DEFINE_ALLOCATOR, and redefine the macro
in terms of the new function. In addition, use the new function in the
implementation of the alloc_commit_node() allocator, rather than the
intermediary allocator, which can now be removed.

Noticed by sparse ("symbol 'alloc_raw_commit_node' was not declared.
Should it be static?").

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
 alloc.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/alloc.c b/alloc.c
index eb22a45..d7c3605 100644
--- a/alloc.c
+++ b/alloc.c
@@ -19,22 +19,10 @@
 #define BLOCKING 1024
 
 #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));		\
 }
 
 union any_object {
@@ -45,16 +33,39 @@ union any_object {
 	struct tag tag;
 };
 
+struct alloc_state {
+	int count; /* total number of nodes allocated */
+	int nr;    /* number of nodes left in current allocation */
+	void *p;   /* first free node in current allocation */
+};
+
+static inline void *alloc_node(struct alloc_state *s, size_t node_size)
+{
+	void *ret;
+
+	if (!s->nr) {
+		s->nr = BLOCKING;
+		s->p = xmalloc(BLOCKING * node_size);
+	}
+	s->nr--;
+	s->count++;
+	ret = s->p;
+	s->p = (char *)s->p + node_size;
+	memset(ret, 0, node_size);
+	return ret;
+}
+
 DEFINE_ALLOCATOR(blob, struct blob)
 DEFINE_ALLOCATOR(tree, struct tree)
-DEFINE_ALLOCATOR(raw_commit, struct commit)
 DEFINE_ALLOCATOR(tag, struct tag)
 DEFINE_ALLOCATOR(object, union any_object)
 
+static struct alloc_state commit_state;
+
 void *alloc_commit_node(void)
 {
 	static int commit_count;
-	struct commit *c = alloc_raw_commit_node();
+	struct commit *c = alloc_node(&commit_state, sizeof(struct commit));
 	c->index = commit_count++;
 	return c;
 }
@@ -66,13 +77,13 @@ static void report(const char *name, unsigned int count, size_t size)
 }
 
 #define REPORT(name, type)	\
-    report(#name, name##_allocs, name##_allocs * sizeof(type) >> 10)
+    report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)
 
 void alloc_report(void)
 {
 	REPORT(blob, struct blob);
 	REPORT(tree, struct tree);
-	REPORT(raw_commit, struct commit);
+	REPORT(commit, struct commit);
 	REPORT(tag, struct tag);
 	REPORT(object, union any_object);
 }
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function
  2014-07-10 23:58 [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function Ramsay Jones
@ 2014-07-11  0:09 ` Jeff King
  2014-07-11  0:33   ` Ramsay Jones
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff King @ 2014-07-11  0:09 UTC (permalink / raw)
  To: Ramsay Jones; +Cc: Junio C Hamano, GIT Mailing-list

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.

-Peff

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function
  2014-07-11  0:09 ` Jeff King
@ 2014-07-11  0:33   ` Ramsay Jones
  0 siblings, 0 replies; 3+ messages in thread
From: Ramsay Jones @ 2014-07-11  0:33 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, GIT Mailing-list

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-07-11  0:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-10 23:58 [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function Ramsay Jones
2014-07-11  0:09 ` Jeff King
2014-07-11  0:33   ` Ramsay Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox