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 /libbb | |
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
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/procps.c | 8 | ||||
-rw-r--r-- | libbb/xrealloc_vector.c | 14 |
2 files changed, 16 insertions, 6 deletions
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 | } |