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

Re: [PATCH 2/4] short circuit out of a few places where we would allocate zero bytes

From
Junio C Hamano <junkio@cox.net>
Date
Dec 28, 2005, 05:07 UTC
Message-ID
<7vpsnh24j3.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0512272036380.14098@g5.osdl.org>
Linus Torvalds <torvalds@osdl.org> writes:
> That said, I think that would be preferable to changing the source code to 
> unnecessarily avoid zero-sized allocations.

Yes, that has essentially been the plan (according to the discussion lead to 7e4a2a848377241b8fb4f624d1151bbf2f8d5814 commit on the list).

After eradicating zero-sized allocations where that change makes the overall code cleaner (which Johannes and Eric did most of the heavylifting and I think mostly done), we would apply something like this, instead of doing x*alloc(size ? size : 1) at the calling site.

About die(), I think the current code structure is fine. If we were doing a library, propagating NULL from C library *alloc() back to our caller and having the caller deal with it is the right thing, but most of the callers of x*alloc() are our main programs and there aren't much they can do when we run out of memory.

---
diff --git a/git-compat-util.h b/git-compat-util.h
index 0c98c99..a71728e 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -63,6 +63,8 @@ extern char *gitstrcasestr(const char *h
 static inline void *xmalloc(size_t size)
 {
 	void *ret = malloc(size);
+	if (!ret && !size)
+		ret = malloc(1); /* funny c library */
 	if (!ret)
 		die("Out of memory, malloc failed");
 	return ret;
@@ -71,6 +73,8 @@ static inline void *xmalloc(size_t size)
 static inline void *xrealloc(void *ptr, size_t size)
 {
 	void *ret = realloc(ptr, size);
+	if (!ret && !size)
+		ret = realloc(ptr, 1); /* funny c library */
 	if (!ret)
 		die("Out of memory, realloc failed");
 	return ret;
@@ -79,6 +83,8 @@ static inline void *xrealloc(void *ptr, 
 static inline void *xcalloc(size_t nmemb, size_t size)
 {
 	void *ret = calloc(nmemb, size);
+	if (!ret && (!nmemb || !size))
+		ret = calloc(1, 1); /* funny c library */
 	if (!ret)
 		die("Out of memory, calloc failed");
 	return ret;
Previous: Linus TorvaldsNext: H. Peter Anvin
Message 8 of 24 in “dietlibc compatibility”
  1. 0/4 dietlibc compatibilityEric Wong, Dec 24, 2005
  2. 1/4 git.c: extra #include for dietlibc (and possibly other C libraries)Eric Wong, Dec 24, 2005
  3. Junio C HamanoDec 26, 2005
  4. 2/4 short circuit out of a few places where we would allocate zero bytesEric Wong, Dec 24, 2005
  5. Johannes SchindelinDec 24, 2005
  6. H. Peter AnvinDec 28, 2005
  7. Linus TorvaldsDec 28, 2005
  8. Junio C HamanoDec 28, 2005
  9. H. Peter AnvinDec 28, 2005
  10. 3/4 add xmktime() function that always accounts for the TZ envEric Wong, Dec 24, 2005
  11. Johannes SchindelinDec 24, 2005
  12. Junio C HamanoDec 24, 2005
  13. Eric WongDec 24, 2005
  14. Junio C HamanoDec 24, 2005
  15. 4/4 git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}Eric Wong, Dec 24, 2005
  16. Junio C HamanoDec 24, 2005
  17. Eric WongDec 24, 2005
  18. Avoid allocating 0 bytes, was Re: [PATCH 4/4] git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}Johannes Schindelin, Dec 26, 2005
  19. Junio C HamanoDec 26, 2005
  20. Johannes SchindelinDec 26, 2005
  21. avoid asking ?alloc() for zero bytes.Junio C Hamano, Dec 26, 2005
  22. Johannes SchindelinDec 26, 2005
  23. Johannes SchindelinDec 28, 2005
  24. Eric WongDec 30, 2005

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.