There's been some confusion around grow_stats_buf, so I thought I'd
rewrite it a bit for clarity to make it clear that its goal is to
figure out how much space it has, how much space it needs and get
there.
I also thought it'd be good to be able to special case a null buffer
and reset itself to 0 so that allocation and reallocation occur in the
same place. This is where it ends up:
static bool grow_stats_buf(conn *c, size_t needed) {
size_t nsize = c->stats.size;
size_t available = nsize - c->stats.offset;
bool rv = true;
while (needed > available) {
assert(nsize > 0);
nsize = nsize << 1;
available = nsize - c->stats.offset;
}
if (nsize != c->stats.size) {
char *ptr = realloc(c->stats.buffer, nsize);
if (ptr) {
c->stats.buffer = ptr;
c->stats.size = nsize;
} else {
rv = false;
}
}
return rv;
}
http://github.com/dustin/memcached/commits/growth-refactor