+1
Trond
On Apr 6, 2009, at 8:10 PM, Dustin wrote:
Er, that was the one *before* it also handled realloc. This is the
final one:
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;
/* Special case: No buffer -- need to allocate fresh */
if (c->stats.buffer == NULL) {
nsize = 1024;
available = c->stats.size = c->stats.offset = 0;
}
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;
}
On Apr 6, 11:09 am, Dustin <[email protected]> wrote:
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
--
Trond Norbye
Application Platform Software Group E-mail: [email protected]
SUN Microsystems Phone: +47 73842100
Haakon VII's gt. 7B Fax: +47 73842101
7485 Trondheim, Norway