diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-04 13:20:36 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-08-04 13:20:36 +0000 |
commit | 27842288b393e532e5693f2a2bab94fee73a326d (patch) | |
tree | 98535c0fd140c89aa6b83179b11d160e6ed59c28 | |
parent | 2b576b8e76ee0dc548f46489e2546b7ed70d080d (diff) | |
download | busybox-w32-27842288b393e532e5693f2a2bab94fee73a326d.tar.gz busybox-w32-27842288b393e532e5693f2a2bab94fee73a326d.tar.bz2 busybox-w32-27842288b393e532e5693f2a2bab94fee73a326d.zip |
libbb: make xrealloc_vector zero out the realloc'ed tail
function old new delta
xrealloc_vector_helper 51 76 +25
man_main 712 705 -7
act 250 234 -16
create_list 91 70 -21
getopt_main 695 664 -31
load_dep_bb 281 248 -33
fileAction 744 709 -35
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/6 up/down: 25/-143) Total: -118 bytes
-rw-r--r-- | archival/dpkg.c | 3 | ||||
-rw-r--r-- | debianutils/run_parts.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/procps.c | 8 | ||||
-rw-r--r-- | libbb/xrealloc_vector.c | 14 | ||||
-rw-r--r-- | miscutils/man.c | 2 | ||||
-rw-r--r-- | modutils/modprobe-small.c | 8 | ||||
-rw-r--r-- | procps/top.c | 4 | ||||
-rw-r--r-- | util-linux/getopt.c | 5 |
9 files changed, 30 insertions, 20 deletions
diff --git a/archival/dpkg.c b/archival/dpkg.c index 9ea308703..54e963233 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c | |||
@@ -1169,10 +1169,9 @@ static char **create_list(const char *filename) | |||
1169 | file_list = NULL; | 1169 | file_list = NULL; |
1170 | count = 0; | 1170 | count = 0; |
1171 | while ((line = xmalloc_fgetline(list_stream)) != NULL) { | 1171 | while ((line = xmalloc_fgetline(list_stream)) != NULL) { |
1172 | //TODO: zeroing xrealloc_vector? | ||
1173 | file_list = xrealloc_vector(file_list, 2, count); | 1172 | file_list = xrealloc_vector(file_list, 2, count); |
1174 | file_list[count++] = line; | 1173 | file_list[count++] = line; |
1175 | file_list[count] = NULL; | 1174 | /*file_list[count] = NULL; - xrealloc_vector did it */ |
1176 | } | 1175 | } |
1177 | fclose(list_stream); | 1176 | fclose(list_stream); |
1178 | 1177 | ||
diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index cb5f48fdd..a58fab3a5 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c | |||
@@ -92,7 +92,7 @@ static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUS | |||
92 | 92 | ||
93 | names = xrealloc_vector(names, 4, cur); | 93 | names = xrealloc_vector(names, 4, cur); |
94 | names[cur++] = xstrdup(file); | 94 | names[cur++] = xstrdup(file); |
95 | names[cur] = NULL; | 95 | /*names[cur] = NULL; - xrealloc_vector did it */ |
96 | 96 | ||
97 | return TRUE; | 97 | return TRUE; |
98 | } | 98 | } |
diff --git a/include/libbb.h b/include/libbb.h index 2dcdeacb1..f7a68492c 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -570,6 +570,10 @@ void *malloc_or_warn(size_t size) FAST_FUNC; | |||
570 | void *xmalloc(size_t size) FAST_FUNC; | 570 | void *xmalloc(size_t size) FAST_FUNC; |
571 | void *xzalloc(size_t size) FAST_FUNC; | 571 | void *xzalloc(size_t size) FAST_FUNC; |
572 | void *xrealloc(void *old, size_t size) FAST_FUNC; | 572 | void *xrealloc(void *old, size_t size) FAST_FUNC; |
573 | /* After xrealloc_vector(v, 4, idx) it's ok to use | ||
574 | * at least v[idx] and v[idx+1], for all idx values. | ||
575 | * shift specifies how many new elements are added (1: 2, 2: 4... 8: 256...) | ||
576 | * when all elements are used up. New elements are zeroed out. */ | ||
573 | #define xrealloc_vector(vector, shift, idx) \ | 577 | #define xrealloc_vector(vector, shift, idx) \ |
574 | xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx)) | 578 | xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx)) |
575 | void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC; | 579 | void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC; |
diff --git a/libbb/procps.c b/libbb/procps.c index ba3d25050..fd19621db 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -45,8 +45,8 @@ static int get_cached(cache_t *cp, unsigned id) | |||
45 | for (i = 0; i < cp->size; i++) | 45 | for (i = 0; i < cp->size; i++) |
46 | if (cp->cache[i].id == id) | 46 | if (cp->cache[i].id == id) |
47 | return i; | 47 | return i; |
48 | i = cp->size; | 48 | i = cp->size++; |
49 | cp->cache = xrealloc_vector(cp->cache, 2, cp->size++); | 49 | cp->cache = xrealloc_vector(cp->cache, 2, i); |
50 | cp->cache[i++].id = id; | 50 | cp->cache[i++].id = id; |
51 | return -i; | 51 | return -i; |
52 | } | 52 | } |
@@ -59,8 +59,8 @@ static char* get_cached(cache_t *cp, unsigned id, ug_func* fp) | |||
59 | for (i = 0; i < cp->size; i++) | 59 | for (i = 0; i < cp->size; i++) |
60 | if (cp->cache[i].id == id) | 60 | if (cp->cache[i].id == id) |
61 | return cp->cache[i].name; | 61 | return cp->cache[i].name; |
62 | i = cp->size; | 62 | i = cp->size++; |
63 | cp->cache = xrealloc_vector(cp->cache, 2, cp->size++); | 63 | cp->cache = xrealloc_vector(cp->cache, 2, i); |
64 | cp->cache[i].id = id; | 64 | cp->cache[i].id = id; |
65 | /* Never fails. Generates numeric string if name isn't found */ | 65 | /* Never fails. Generates numeric string if name isn't found */ |
66 | fp(cp->cache[i].name, sizeof(cp->cache[i].name), id); | 66 | fp(cp->cache[i].name, sizeof(cp->cache[i].name), id); |
diff --git a/libbb/xrealloc_vector.c b/libbb/xrealloc_vector.c index ccc961b7b..342ae536e 100644 --- a/libbb/xrealloc_vector.c +++ b/libbb/xrealloc_vector.c | |||
@@ -14,7 +14,6 @@ | |||
14 | * #define magic packed two parameters into one: | 14 | * #define magic packed two parameters into one: |
15 | * sizeof = sizeof_and_shift >> 8 | 15 | * sizeof = sizeof_and_shift >> 8 |
16 | * shift = (sizeof_and_shift) & 0xff | 16 | * shift = (sizeof_and_shift) & 0xff |
17 | * (TODO: encode "I want it zeroed" in lowest bit?) | ||
18 | * | 17 | * |
19 | * Lets say shift = 4. 1 << 4 == 0x10. | 18 | * Lets say shift = 4. 1 << 4 == 0x10. |
20 | * If idx == 0, 0x10, 0x20 etc, vector[] is resized to next higher | 19 | * If idx == 0, 0x10, 0x20 etc, vector[] is resized to next higher |
@@ -23,14 +22,25 @@ | |||
23 | * | 22 | * |
24 | * In other words: after xrealloc_vector(v, 4, idx) it's ok to use | 23 | * In other words: after xrealloc_vector(v, 4, idx) it's ok to use |
25 | * at least v[idx] and v[idx+1], for all idx values. | 24 | * at least v[idx] and v[idx+1], for all idx values. |
25 | * | ||
26 | * New elements are zeroed out, but only if realloc was done | ||
27 | * (not on every call). You can depend on v[idx] and v[idx+1] being | ||
28 | * zeroed out if you use it like this: | ||
29 | * v = xrealloc_vector(v, 4, idx); | ||
30 | * v[idx].some_fields = ...; - the rest stays 0/NULL | ||
31 | * idx++; | ||
32 | * If you do not advance idx like above, you should be more careful. | ||
33 | * Next call to xrealloc_vector(v, 4, idx) may or may not zero out v[idx]. | ||
26 | */ | 34 | */ |
27 | void* FAST_FUNC xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) | 35 | void* FAST_FUNC xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) |
28 | { | 36 | { |
29 | int mask = 1 << (uint8_t)sizeof_and_shift; | 37 | int mask = 1 << (uint8_t)sizeof_and_shift; |
30 | 38 | ||
31 | if (!(idx & (mask - 1))) { | 39 | if (!(idx & (mask - 1))) { |
32 | sizeof_and_shift >>= 8; | 40 | sizeof_and_shift >>= 8; /* sizeof(vector[0]) */ |
33 | vector = xrealloc(vector, sizeof_and_shift * (idx + mask + 1)); | 41 | vector = xrealloc(vector, sizeof_and_shift * (idx + mask + 1)); |
42 | vector += idx; | ||
43 | memset(vector, 0, sizeof_and_shift * (mask + 1)); | ||
34 | } | 44 | } |
35 | return vector; | 45 | return vector; |
36 | } | 46 | } |
diff --git a/miscutils/man.c b/miscutils/man.c index edeffc776..f499fef62 100644 --- a/miscutils/man.c +++ b/miscutils/man.c | |||
@@ -112,7 +112,7 @@ int man_main(int argc UNUSED_PARAM, char **argv) | |||
112 | man_path_list[count_mp] = xstrdup(token[1]); | 112 | man_path_list[count_mp] = xstrdup(token[1]); |
113 | count_mp++; | 113 | count_mp++; |
114 | /* man_path_list is NULL terminated */ | 114 | /* man_path_list is NULL terminated */ |
115 | man_path_list[count_mp] = NULL; | 115 | /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */ |
116 | } | 116 | } |
117 | if (strcmp("MANSECT", token[0]) == 0) { | 117 | if (strcmp("MANSECT", token[0]) == 0) { |
118 | free(sec_list); | 118 | free(sec_list); |
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c index 63be9aadb..4aa2b5862 100644 --- a/modutils/modprobe-small.c +++ b/modutils/modprobe-small.c | |||
@@ -270,10 +270,9 @@ static FAST_FUNC int fileAction(const char *pathname, | |||
270 | 270 | ||
271 | cur = module_count++; | 271 | cur = module_count++; |
272 | modinfo = xrealloc_vector(modinfo, 12, cur); | 272 | modinfo = xrealloc_vector(modinfo, 12, cur); |
273 | //TODO: use zeroing version of xrealloc_vector? | ||
274 | modinfo[cur].pathname = xstrdup(pathname); | 273 | modinfo[cur].pathname = xstrdup(pathname); |
275 | modinfo[cur].aliases = NULL; | 274 | /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */ |
276 | modinfo[cur+1].pathname = NULL; | 275 | /*modinfo[cur+1].pathname = NULL;*/ |
277 | 276 | ||
278 | if (!pathname_matches_modname(fname, modname_to_match)) { | 277 | if (!pathname_matches_modname(fname, modname_to_match)) { |
279 | dbg1_error_msg("'%s' module name doesn't match", pathname); | 278 | dbg1_error_msg("'%s' module name doesn't match", pathname); |
@@ -330,8 +329,7 @@ static int load_dep_bb(void) | |||
330 | space = strchrnul(line, ' '); | 329 | space = strchrnul(line, ' '); |
331 | cur = module_count++; | 330 | cur = module_count++; |
332 | modinfo = xrealloc_vector(modinfo, 12, cur); | 331 | modinfo = xrealloc_vector(modinfo, 12, cur); |
333 | //TODO: use zeroing version of xrealloc_vector? | 332 | /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */ |
334 | modinfo[cur+1].pathname = NULL; | ||
335 | modinfo[cur].pathname = line; /* we take ownership of malloced block here */ | 333 | modinfo[cur].pathname = line; /* we take ownership of malloced block here */ |
336 | if (*space) | 334 | if (*space) |
337 | *space++ = '\0'; | 335 | *space++ = '\0'; |
diff --git a/procps/top.c b/procps/top.c index 1a6b8abb2..1f1415f83 100644 --- a/procps/top.c +++ b/procps/top.c | |||
@@ -814,7 +814,7 @@ int top_main(int argc UNUSED_PARAM, char **argv) | |||
814 | int n; | 814 | int n; |
815 | if (scan_mask == TOP_MASK) { | 815 | if (scan_mask == TOP_MASK) { |
816 | n = ntop; | 816 | n = ntop; |
817 | top = xrealloc_vector(top, 2, ntop++); | 817 | top = xrealloc_vector(top, 6, ntop++); |
818 | top[n].pid = p->pid; | 818 | top[n].pid = p->pid; |
819 | top[n].ppid = p->ppid; | 819 | top[n].ppid = p->ppid; |
820 | top[n].vsz = p->vsz; | 820 | top[n].vsz = p->vsz; |
@@ -830,7 +830,7 @@ int top_main(int argc UNUSED_PARAM, char **argv) | |||
830 | continue; /* kernel threads are ignored */ | 830 | continue; /* kernel threads are ignored */ |
831 | n = ntop; | 831 | n = ntop; |
832 | /* No bug here - top and topmem are the same */ | 832 | /* No bug here - top and topmem are the same */ |
833 | top = xrealloc_vector(topmem, 2, ntop++); | 833 | top = xrealloc_vector(topmem, 6, ntop++); |
834 | strcpy(topmem[n].comm, p->comm); | 834 | strcpy(topmem[n].comm, p->comm); |
835 | topmem[n].pid = p->pid; | 835 | topmem[n].pid = p->pid; |
836 | topmem[n].vsz = p->mapped_rw + p->mapped_ro; | 836 | topmem[n].vsz = p->mapped_rw + p->mapped_ro; |
diff --git a/util-linux/getopt.c b/util-linux/getopt.c index 25aa4069a..402630385 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c | |||
@@ -238,14 +238,13 @@ static struct option *add_long_options(struct option *long_options, char *option | |||
238 | if (tlen == 0) | 238 | if (tlen == 0) |
239 | bb_error_msg_and_die("empty long option specified"); | 239 | bb_error_msg_and_die("empty long option specified"); |
240 | } | 240 | } |
241 | //TODO: zeroing version of xrealloc_vector! | ||
242 | long_options = xrealloc_vector(long_options, 4, long_nr); | 241 | long_options = xrealloc_vector(long_options, 4, long_nr); |
243 | long_options[long_nr].has_arg = arg_opt; | 242 | long_options[long_nr].has_arg = arg_opt; |
244 | long_options[long_nr].flag = NULL; | 243 | /*long_options[long_nr].flag = NULL; - xrealloc_vector did it */ |
245 | long_options[long_nr].val = LONG_OPT; | 244 | long_options[long_nr].val = LONG_OPT; |
246 | long_options[long_nr].name = xstrdup(tokptr); | 245 | long_options[long_nr].name = xstrdup(tokptr); |
247 | long_nr++; | 246 | long_nr++; |
248 | memset(&long_options[long_nr], 0, sizeof(long_options[0])); | 247 | /*memset(&long_options[long_nr], 0, sizeof(long_options[0])); - xrealloc_vector did it */ |
249 | } | 248 | } |
250 | tokptr = strtok(NULL, ", \t\n"); | 249 | tokptr = strtok(NULL, ", \t\n"); |
251 | } | 250 | } |