aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-10-11 00:45:25 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-10-11 00:45:25 +0200
commite7670ff81d01d06f8f27ffb3b6e6d5e6f92c8f74 (patch)
treea6a13f9d753c590c06299e0c0770992b5faaba03
parent0e5e4eaf7bbfa5d71db1ea410f734c8458a2071e (diff)
downloadbusybox-w32-e7670ff81d01d06f8f27ffb3b6e6d5e6f92c8f74.tar.gz
busybox-w32-e7670ff81d01d06f8f27ffb3b6e6d5e6f92c8f74.tar.bz2
busybox-w32-e7670ff81d01d06f8f27ffb3b6e6d5e6f92c8f74.zip
ash: use bbox wrappers for malloc etc instead of homegrown ones
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@googlemail.com>
-rw-r--r--shell/ash.c78
1 files changed, 44 insertions, 34 deletions
diff --git a/shell/ash.c b/shell/ash.c
index b0b85358f..cc2677126 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -1155,6 +1155,49 @@ errmsg(int e, const char *em)
1155 1155
1156/* ============ Memory allocation */ 1156/* ============ Memory allocation */
1157 1157
1158#if 0
1159/* I consider these wrappers nearly useless:
1160 * ok, they return you to nearest exception handler, but
1161 * how much memory do you leak in the process, making
1162 * memory starvation worse?
1163 */
1164static void *
1165ckrealloc(void * p, size_t nbytes)
1166{
1167 p = realloc(p, nbytes);
1168 if (!p)
1169 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1170 return p;
1171}
1172
1173static void *
1174ckmalloc(size_t nbytes)
1175{
1176 return ckrealloc(NULL, nbytes);
1177}
1178
1179static void *
1180ckzalloc(size_t nbytes)
1181{
1182 return memset(ckmalloc(nbytes), 0, nbytes);
1183}
1184
1185static char *
1186ckstrdup(const char *s)
1187{
1188 char *p = strdup(s);
1189 if (!p)
1190 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1191 return p;
1192}
1193#else
1194/* Using bbox equivalents. They exit if out of memory */
1195# define ckrealloc xrealloc
1196# define ckmalloc xmalloc
1197# define ckzalloc xzalloc
1198# define ckstrdup xstrdup
1199#endif
1200
1158/* 1201/*
1159 * It appears that grabstackstr() will barf with such alignments 1202 * It appears that grabstackstr() will barf with such alignments
1160 * because stalloc() will return a string allocated in a new stackblock. 1203 * 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;
1210 herefd = -1; \ 1253 herefd = -1; \
1211} while (0) 1254} while (0)
1212 1255
1256
1213#define stackblock() ((void *)g_stacknxt) 1257#define stackblock() ((void *)g_stacknxt)
1214#define stackblocksize() g_stacknleft 1258#define stackblocksize() g_stacknleft
1215 1259
1216
1217static void *
1218ckrealloc(void * p, size_t nbytes)
1219{
1220 p = realloc(p, nbytes);
1221 if (!p)
1222 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1223 return p;
1224}
1225
1226static void *
1227ckmalloc(size_t nbytes)
1228{
1229 return ckrealloc(NULL, nbytes);
1230}
1231
1232static void *
1233ckzalloc(size_t nbytes)
1234{
1235 return memset(ckmalloc(nbytes), 0, nbytes);
1236}
1237
1238/*
1239 * Make a copy of a string in safe storage.
1240 */
1241static char *
1242ckstrdup(const char *s)
1243{
1244 char *p = strdup(s);
1245 if (!p)
1246 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1247 return p;
1248}
1249
1250/* 1260/*
1251 * Parse trees for commands are allocated in lifo order, so we use a stack 1261 * Parse trees for commands are allocated in lifo order, so we use a stack
1252 * to make this more efficient, and also to avoid all sorts of exception 1262 * to make this more efficient, and also to avoid all sorts of exception