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

[FIXED PATCH 1/3] Use proper object allocators for unknown object nodes too

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Apr 17, 2007, 05:10 UTC
Message-ID
<Pine.LNX.4.64.0704162208290.5473@woody.linux-foundation.org>
In-Reply-To
<Pine.LNX.4.64.0704162200420.5473@woody.linux-foundation.org>

We used to use a different allocator scheme for when we didn't know the object type. That meant that objects that were created without any up-front knowledge of the type would not go through the same allocation paths as normal object allocations, and would miss out on the statistics.

But perhaps more importantly than the statistics (that are useful when looking at memory usage but not much else), if we want to make the object hash tables use a denser object pointer representation, we need to make sure that they all go through the same blocking allocator.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---

Gaah. So this should be the properly working version. Not a lot of code actually uses the "allocate unknown object", which is why nothing obvious broke with the previous version that effectively only allocated a "struct object", but it was still a bad bug.

Somebody should triple-check that I didn't screw up *again*.
 alloc.c  |   28 ++++++++++++++++++++--------
 cache.h  |    1 +
 object.c |   15 +++------------
 3 files changed, 24 insertions(+), 20 deletions(-)
diff --git a/alloc.c b/alloc.c
index 460db19..53eba37 100644
--- a/alloc.c
+++ b/alloc.c
@@ -18,26 +18,38 @@
 
 #define BLOCKING 1024
 
-#define DEFINE_ALLOCATOR(name)					\
+#define DEFINE_ALLOCATOR(name, type)				\
 static unsigned int name##_allocs;				\
 struct name *alloc_##name##_node(void)				\
 {								\
 	static int nr;						\
-	static struct name *block;				\
+	static type *block;					\
+	void *ret;						\
 								\
 	if (!nr) {						\
 		nr = BLOCKING;					\
-		block = xcalloc(BLOCKING, sizeof(struct name));	\
+		block = xmalloc(BLOCKING * sizeof(type));	\
 	}							\
 	nr--;							\
 	name##_allocs++;					\
-	return block++;						\
+	ret = block++;						\
+	memset(ret, 0, sizeof(type));				\
+	return ret;						\
 }
 
-DEFINE_ALLOCATOR(blob)
-DEFINE_ALLOCATOR(tree)
-DEFINE_ALLOCATOR(commit)
-DEFINE_ALLOCATOR(tag)
+union any_object {
+	struct object object;
+	struct blob blob;
+	struct tree tree;
+	struct commit commit;
+	struct tag tag;
+};
+
+DEFINE_ALLOCATOR(blob, struct blob)
+DEFINE_ALLOCATOR(tree, struct tree)
+DEFINE_ALLOCATOR(commit, struct commit)
+DEFINE_ALLOCATOR(tag, struct tag)
+DEFINE_ALLOCATOR(object, union any_object)
 
 #ifdef NO_C99_FORMAT
 #define SZ_FMT "%u"
diff --git a/cache.h b/cache.h
index b1bd9e4..aa72791 100644
--- a/cache.h
+++ b/cache.h
@@ -484,6 +484,7 @@ extern struct blob *alloc_blob_node(void);
 extern struct tree *alloc_tree_node(void);
 extern struct commit *alloc_commit_node(void);
 extern struct tag *alloc_tag_node(void);
+extern struct object *alloc_object_node(void);
 extern void alloc_report(void);
 
 /* trace.c */
diff --git a/object.c b/object.c
index 78a44a6..153ebac 100644
--- a/object.c
+++ b/object.c
@@ -120,22 +120,13 @@ void created_object(const unsigned char *sha1, struct object *obj)
 	nr_objs++;
 }
 
-union any_object {
-	struct object object;
-	struct commit commit;
-	struct tree tree;
-	struct blob blob;
-	struct tag tag;
-};
-
 struct object *lookup_unknown_object(const unsigned char *sha1)
 {
 	struct object *obj = lookup_object(sha1);
 	if (!obj) {
-		union any_object *ret = xcalloc(1, sizeof(*ret));
-		created_object(sha1, &ret->object);
-		ret->object.type = OBJ_NONE;
-		return &ret->object;
+		obj = alloc_object_node();
+		created_object(sha1, obj);
+		obj->type = OBJ_NONE;
 	}
 	return obj;
 }
-- 
1.5.1.1.107.g7a15
Previous: Linus TorvaldsNext: Linus Torvalds
Message 7 of 9 in “Use "object index" rather than pointers in the object hashing”
  1. 0/3 Use "object index" rather than pointers in the object hashingLinus Torvalds, Apr 17, 2007
  2. 1/3 Use proper object allocators for unknown object nodes tooLinus Torvalds, Apr 17, 2007
  3. 2/3 Clean up object creation to use more common codeLinus Torvalds, Apr 17, 2007
  4. 3/3 Make the object lookup hash use a "object index" instead of a pointerLinus Torvalds, Apr 17, 2007
  5. Junio C HamanoApr 17, 2007
  6. Linus TorvaldsApr 17, 2007
  7. 1/3 Use proper object allocators for unknown object nodes tooLinus Torvalds, Apr 17, 2007
  8. 2/3 Clean up object creation to use more common codeLinus Torvalds, Apr 17, 2007
  9. 3/3 Make the object lookup hash use a "object index" instead of a pointerLinus Torvalds, Apr 17, 2007

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.