Make add_single_dive() grow the divelist allocation if necessary

We only made sure the divelist allocation was big enough in
"record_dive_to_table()", but add_single_dive() can add entries too.

Now, in practice that never bit anybody, since

 (a) we allocate extra entries anyway, and it would be very unusual that
     the divelist table was exactly full

 (b) most "malloc()" implementations end up having their own slop on top
     of that

 (c) add_single_dive() was only used for merging dives, which actually
     ends up removing more divex than it adds (but it does add one first)

but when I'm starting to split dives, this will be a bigger issue in
practice.  And it was wrong.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2015-10-01 21:13:48 -04:00 committed by Dirk Hohndel
parent b80079c37f
commit b5b7bdda81
3 changed files with 20 additions and 10 deletions

View file

@ -67,17 +67,9 @@ void clear_table(struct dive_table *table)
void record_dive_to_table(struct dive *dive, struct dive_table *table)
{
assert(table != NULL);
int nr = table->nr, allocated = table->allocated;
struct dive **dives = table->dives;
struct dive **dives = grow_dive_table(table);
int nr = table->nr;
if (nr >= allocated) {
allocated = (nr + 32) * 3 / 2;
dives = realloc(dives, allocated * sizeof(struct dive *));
if (!dives)
exit(1);
table->dives = dives;
table->allocated = allocated;
}
dives[nr] = fixup_dive(dive);
table->nr = nr + 1;
}