diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2015-10-01 21:13:48 -0400 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-10-01 22:28:04 -0400 |
commit | b5b7bdda81acd0ee8c14a081f8bfd38d9809cec8 (patch) | |
tree | 6918ffb14e88a9fd2c7aea4948568e39dc202753 /divelist.c | |
parent | b80079c37f7bad67cf91d96ef7c79e87ac0801cb (diff) | |
download | subsurface-b5b7bdda81acd0ee8c14a081f8bfd38d9809cec8.tar.gz |
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>
Diffstat (limited to 'divelist.c')
-rw-r--r-- | divelist.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/divelist.c b/divelist.c index 3cbff84ef..3cc0d2c8d 100644 --- a/divelist.c +++ b/divelist.c @@ -767,9 +767,26 @@ void delete_single_dive(int idx) free(dive); } +struct dive **grow_dive_table(struct dive_table *table) +{ + int nr = table->nr, allocated = table->allocated; + struct dive **dives = table->dives; + + 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; + } + return dives; +} + void add_single_dive(int idx, struct dive *dive) { int i; + grow_dive_table(&dive_table); dive_table.nr++; if (dive->selected) amount_selected++; |