[git commit master] ash: use bbox wrappers for malloc etc instead of homegrown ones

Denys Vlasenko vda.linux at googlemail.com
Sat Oct 10 22:45:25 UTC 2009


commit: http://git.busybox.net/busybox/commit/?id=e7670ff81d01d06f8f27ffb3b6e6d5e6f92c8f74
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
popstring                                            134     140      +6
ckmalloc                                               9       -      -9
ckstrdup                                              22       -     -22
ckrealloc                                             24       -     -24
ckzalloc                                              28       -     -28
------------------------------------------------------------------------------
(add/remove: 0/4 grow/shrink: 1/0 up/down: 6/-83)             Total: -77 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c |   78 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 44 insertions(+), 34 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index b0b8535..cc26771 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -1155,6 +1155,49 @@ errmsg(int e, const char *em)
 
 /* ============ Memory allocation */
 
+#if 0
+/* I consider these wrappers nearly useless:
+ * ok, they return you to nearest exception handler, but
+ * how much memory do you leak in the process, making
+ * memory starvation worse?
+ */
+static void *
+ckrealloc(void * p, size_t nbytes)
+{
+	p = realloc(p, nbytes);
+	if (!p)
+		ash_msg_and_raise_error(bb_msg_memory_exhausted);
+	return p;
+}
+
+static void *
+ckmalloc(size_t nbytes)
+{
+	return ckrealloc(NULL, nbytes);
+}
+
+static void *
+ckzalloc(size_t nbytes)
+{
+	return memset(ckmalloc(nbytes), 0, nbytes);
+}
+
+static char *
+ckstrdup(const char *s)
+{
+	char *p = strdup(s);
+	if (!p)
+		ash_msg_and_raise_error(bb_msg_memory_exhausted);
+	return p;
+}
+#else
+/* Using bbox equivalents. They exit if out of memory */
+# define ckrealloc xrealloc
+# define ckmalloc  xmalloc
+# define ckzalloc  xzalloc
+# define ckstrdup  xstrdup
+#endif
+
 /*
  * It appears that grabstackstr() will barf with such alignments
  * because stalloc() will return a string allocated in a new stackblock.
@@ -1210,43 +1253,10 @@ extern struct globals_memstack *const ash_ptr_to_globals_memstack;
 	herefd = -1; \
 } while (0)
 
+
 #define stackblock()     ((void *)g_stacknxt)
 #define stackblocksize() g_stacknleft
 
-
-static void *
-ckrealloc(void * p, size_t nbytes)
-{
-	p = realloc(p, nbytes);
-	if (!p)
-		ash_msg_and_raise_error(bb_msg_memory_exhausted);
-	return p;
-}
-
-static void *
-ckmalloc(size_t nbytes)
-{
-	return ckrealloc(NULL, nbytes);
-}
-
-static void *
-ckzalloc(size_t nbytes)
-{
-	return memset(ckmalloc(nbytes), 0, nbytes);
-}
-
-/*
- * Make a copy of a string in safe storage.
- */
-static char *
-ckstrdup(const char *s)
-{
-	char *p = strdup(s);
-	if (!p)
-		ash_msg_and_raise_error(bb_msg_memory_exhausted);
-	return p;
-}
-
 /*
  * Parse trees for commands are allocated in lifo order, so we use a stack
  * to make this more efficient, and also to avoid all sorts of exception
-- 
1.6.3.3



More information about the busybox-cvs mailing list