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
This commit is contained in:
Denis Vlasenko
2008-08-04 13:20:36 +00:00
parent 2b576b8e76
commit 27842288b3
9 changed files with 30 additions and 20 deletions

View File

@@ -14,7 +14,6 @@
* #define magic packed two parameters into one:
* sizeof = sizeof_and_shift >> 8
* shift = (sizeof_and_shift) & 0xff
* (TODO: encode "I want it zeroed" in lowest bit?)
*
* Lets say shift = 4. 1 << 4 == 0x10.
* If idx == 0, 0x10, 0x20 etc, vector[] is resized to next higher
@@ -23,14 +22,25 @@
*
* In other words: after xrealloc_vector(v, 4, idx) it's ok to use
* at least v[idx] and v[idx+1], for all idx values.
*
* New elements are zeroed out, but only if realloc was done
* (not on every call). You can depend on v[idx] and v[idx+1] being
* zeroed out if you use it like this:
* v = xrealloc_vector(v, 4, idx);
* v[idx].some_fields = ...; - the rest stays 0/NULL
* idx++;
* If you do not advance idx like above, you should be more careful.
* Next call to xrealloc_vector(v, 4, idx) may or may not zero out v[idx].
*/
void* FAST_FUNC xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx)
{
int mask = 1 << (uint8_t)sizeof_and_shift;
if (!(idx & (mask - 1))) {
sizeof_and_shift >>= 8;
sizeof_and_shift >>= 8; /* sizeof(vector[0]) */
vector = xrealloc(vector, sizeof_and_shift * (idx + mask + 1));
vector += idx;
memset(vector, 0, sizeof_and_shift * (mask + 1));
}
return vector;
}