{"thread":{"id":"37104","subject":"[PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function","startedAt":"2014-07-10T23:58:31Z","lastAt":"2014-07-11T00:33:24Z","messageCount":3,"participants":["Ramsay Jones","Jeff King"],"isPatch":true,"patchVersion":3,"patchTotal":2},"messages":[{"id":"245742","messageId":"53BF28A7.7000903@ramsay1.demon.co.uk","threadId":"37104","inReplyTo":null,"subject":"[PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsay1.demon.co.uk","sentAt":"2014-07-10T23:58:31Z","receivedAt":"2014-07-10T23:58:31Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"\nIn order to encapsulate the setting of the unique commit index, commit\n969eba63 (\"commit: push commit_index update into alloc_commit_node\",\n10-06-2014) introduced a (logically private) intermediary allocator\nfunction. However, this function (alloc_raw_commit_node()) was declared\nas a public function, which undermines its entire purpose.\n\nIntroduce an inline function, alloc_node(), which implements the main\nlogic of the allocator used by DEFINE_ALLOCATOR, and redefine the macro\nin terms of the new function. In addition, use the new function in the\nimplementation of the alloc_commit_node() allocator, rather than the\nintermediary allocator, which can now be removed.\n\nNoticed by sparse (\"symbol 'alloc_raw_commit_node' was not declared.\nShould it be static?\").\n\nSigned-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n---\n alloc.c | 47 +++++++++++++++++++++++++++++------------------\n 1 file changed, 29 insertions(+), 18 deletions(-)\n\ndiff --git a/alloc.c b/alloc.c\nindex eb22a45..d7c3605 100644\n--- a/alloc.c\n+++ b/alloc.c\n@@ -19,22 +19,10 @@\n #define BLOCKING 1024\n \n #define DEFINE_ALLOCATOR(name, type)\t\t\t\t\\\n-static unsigned int name##_allocs;\t\t\t\t\\\n+static struct alloc_state name##_state;\t\t\t\t\\\n void *alloc_##name##_node(void)\t\t\t\t\t\\\n {\t\t\t\t\t\t\t\t\\\n-\tstatic int nr;\t\t\t\t\t\t\\\n-\tstatic type *block;\t\t\t\t\t\\\n-\tvoid *ret;\t\t\t\t\t\t\\\n-\t\t\t\t\t\t\t\t\\\n-\tif (!nr) {\t\t\t\t\t\t\\\n-\t\tnr = BLOCKING;\t\t\t\t\t\\\n-\t\tblock = xmalloc(BLOCKING * sizeof(type));\t\\\n-\t}\t\t\t\t\t\t\t\\\n-\tnr--;\t\t\t\t\t\t\t\\\n-\tname##_allocs++;\t\t\t\t\t\\\n-\tret = block++;\t\t\t\t\t\t\\\n-\tmemset(ret, 0, sizeof(type));\t\t\t\t\\\n-\treturn ret;\t\t\t\t\t\t\\\n+\treturn alloc_node(&name##_state, sizeof(type));\t\t\\\n }\n \n union any_object {\n@@ -45,16 +33,39 @@ union any_object {\n \tstruct tag tag;\n };\n \n+struct alloc_state {\n+\tint count; /* total number of nodes allocated */\n+\tint nr;    /* number of nodes left in current allocation */\n+\tvoid *p;   /* first free node in current allocation */\n+};\n+\n+static inline void *alloc_node(struct alloc_state *s, size_t node_size)\n+{\n+\tvoid *ret;\n+\n+\tif (!s->nr) {\n+\t\ts->nr = BLOCKING;\n+\t\ts->p = xmalloc(BLOCKING * node_size);\n+\t}\n+\ts->nr--;\n+\ts->count++;\n+\tret = s->p;\n+\ts->p = (char *)s->p + node_size;\n+\tmemset(ret, 0, node_size);\n+\treturn ret;\n+}\n+\n DEFINE_ALLOCATOR(blob, struct blob)\n DEFINE_ALLOCATOR(tree, struct tree)\n-DEFINE_ALLOCATOR(raw_commit, struct commit)\n DEFINE_ALLOCATOR(tag, struct tag)\n DEFINE_ALLOCATOR(object, union any_object)\n \n+static struct alloc_state commit_state;\n+\n void *alloc_commit_node(void)\n {\n \tstatic int commit_count;\n-\tstruct commit *c = alloc_raw_commit_node();\n+\tstruct commit *c = alloc_node(&commit_state, sizeof(struct commit));\n \tc->index = commit_count++;\n \treturn c;\n }\n@@ -66,13 +77,13 @@ static void report(const char *name, unsigned int count, size_t size)\n }\n \n #define REPORT(name, type)\t\\\n-    report(#name, name##_allocs, name##_allocs * sizeof(type) >> 10)\n+    report(#name, name##_state.count, name##_state.count * sizeof(type) >> 10)\n \n void alloc_report(void)\n {\n \tREPORT(blob, struct blob);\n \tREPORT(tree, struct tree);\n-\tREPORT(raw_commit, struct commit);\n+\tREPORT(commit, struct commit);\n \tREPORT(tag, struct tag);\n \tREPORT(object, union any_object);\n }\n-- \n2.0.0\n"},{"id":"245744","messageId":"20140711000956.GA11360@sigill.intra.peff.net","threadId":"37104","inReplyTo":"53BF28A7.7000903@ramsay1.demon.co.uk","subject":"Re: [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-07-11T00:09:56Z","receivedAt":"2014-07-11T00:09:56Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Jul 11, 2014 at 12:58:31AM +0100, Ramsay Jones wrote:\n\n>  #define DEFINE_ALLOCATOR(name, type)\t\t\t\t\\\n> -static unsigned int name##_allocs;\t\t\t\t\\\n> +static struct alloc_state name##_state;\t\t\t\t\\\n>  void *alloc_##name##_node(void)\t\t\t\t\t\\\n>  {\t\t\t\t\t\t\t\t\\\n> -\tstatic int nr;\t\t\t\t\t\t\\\n> -\tstatic type *block;\t\t\t\t\t\\\n> -\tvoid *ret;\t\t\t\t\t\t\\\n> -\t\t\t\t\t\t\t\t\\\n> -\tif (!nr) {\t\t\t\t\t\t\\\n> -\t\tnr = BLOCKING;\t\t\t\t\t\\\n> -\t\tblock = xmalloc(BLOCKING * sizeof(type));\t\\\n> -\t}\t\t\t\t\t\t\t\\\n> -\tnr--;\t\t\t\t\t\t\t\\\n> -\tname##_allocs++;\t\t\t\t\t\\\n> -\tret = block++;\t\t\t\t\t\t\\\n> -\tmemset(ret, 0, sizeof(type));\t\t\t\t\\\n> -\treturn ret;\t\t\t\t\t\t\\\n> +\treturn alloc_node(&name##_state, sizeof(type));\t\t\\\n>  }\n\nYay. Not only does this solve the problem, but it gets rid of nasty\nmulti-line macro. In fact, I kind of wonder if we should just do away\nwith the macro entirely, and write out:\n\n  static struct alloc_state blob_state;\n  void alloc_blob_node(void)\n  {\n\treturn alloc_node(&blob_state, sizeof(struct blob));\n  }\n\nIt's more lines, but it is probably less obfuscated to a reader.\n\n-Peff\n"},{"id":"245750","messageId":"53BF30D4.7010105@ramsay1.demon.co.uk","threadId":"37104","inReplyTo":"20140711000956.GA11360@sigill.intra.peff.net","subject":"Re: [PATCH v3 1/2] alloc.c: remove the alloc_raw_commit_node() function","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsay1.demon.co.uk","sentAt":"2014-07-11T00:33:24Z","receivedAt":"2014-07-11T00:33:24Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"On 11/07/14 01:09, Jeff King wrote:\n> On Fri, Jul 11, 2014 at 12:58:31AM +0100, Ramsay Jones wrote:\n> \n>>  #define DEFINE_ALLOCATOR(name, type)\t\t\t\t\\\n>> -static unsigned int name##_allocs;\t\t\t\t\\\n>> +static struct alloc_state name##_state;\t\t\t\t\\\n>>  void *alloc_##name##_node(void)\t\t\t\t\t\\\n>>  {\t\t\t\t\t\t\t\t\\\n>> -\tstatic int nr;\t\t\t\t\t\t\\\n>> -\tstatic type *block;\t\t\t\t\t\\\n>> -\tvoid *ret;\t\t\t\t\t\t\\\n>> -\t\t\t\t\t\t\t\t\\\n>> -\tif (!nr) {\t\t\t\t\t\t\\\n>> -\t\tnr = BLOCKING;\t\t\t\t\t\\\n>> -\t\tblock = xmalloc(BLOCKING * sizeof(type));\t\\\n>> -\t}\t\t\t\t\t\t\t\\\n>> -\tnr--;\t\t\t\t\t\t\t\\\n>> -\tname##_allocs++;\t\t\t\t\t\\\n>> -\tret = block++;\t\t\t\t\t\t\\\n>> -\tmemset(ret, 0, sizeof(type));\t\t\t\t\\\n>> -\treturn ret;\t\t\t\t\t\t\\\n>> +\treturn alloc_node(&name##_state, sizeof(type));\t\t\\\n>>  }\n> \n> Yay. Not only does this solve the problem, but it gets rid of nasty\n> multi-line macro. In fact, I kind of wonder if we should just do away\n> with the macro entirely, and write out:\n> \n>   static struct alloc_state blob_state;\n>   void alloc_blob_node(void)\n>   {\n> \treturn alloc_node(&blob_state, sizeof(struct blob));\n>   }\n> \n> It's more lines, but it is probably less obfuscated to a reader.\n\nYeah, I'm not a fan of _large_ multi-line macros myself.\n\nNow that DEFINE_ALLOCATOR has slimmed down, I don't mind it so much.\nHowever, I agree that doing away with the macro leads to easier to\nread code. (I also don't mind the extra lines).\n\nATB,\nRamsay Jones\n"}]}