1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 01:57:52 +00:00

in bencode.c's makeroom(), make the containers' sizes scale in powers of 2 to avoid excess reallocs.

This commit is contained in:
Jordan Lee 2012-12-11 05:23:48 +00:00
parent 959a8c2f8b
commit 84f2cf8fa2

View file

@ -158,27 +158,29 @@ tr_bencParseStr (const uint8_t * buf,
return 0;
}
/* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
#define LIST_SIZE 4 /* number of items to increment list/dict buffer by */
static int
makeroom (tr_benc * val,
size_t count)
makeroom (tr_benc * container, size_t count)
{
assert (isContainer (val));
const size_t needed = container->val.l.count + count;
if (val->val.l.count + count > val->val.l.alloc)
assert (isContainer (container));
if (needed > container->val.l.alloc)
{
/* We need a bigger boat */
const int len = val->val.l.alloc + count +
(count % LIST_SIZE ? LIST_SIZE -
(count % LIST_SIZE) : 0);
void * tmp = realloc (val->val.l.vals, len * sizeof (tr_benc));
if (!tmp)
size_t n;
void * tmp;
/* scale the alloc size in powers-of-2 */
n = container->val.l.alloc ? container->val.l.alloc : 8;
while (n < needed)
n *= 2u;
tmp = realloc (container->val.l.vals, n * sizeof (tr_benc));
if (tmp == NULL)
return 1;
val->val.l.alloc = len;
val->val.l.vals = tmp;
container->val.l.alloc = n;
container->val.l.vals = tmp;
}
return 0;
@ -676,7 +678,7 @@ tr_bencListAdd (tr_benc * list)
assert (tr_bencIsList (list));
if (list->val.l.count == list->val.l.alloc)
tr_bencListReserve (list, LIST_SIZE);
tr_bencListReserve (list, 4);
assert (list->val.l.count < list->val.l.alloc);