aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfshelper
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-23 14:41:38 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-23 14:41:38 +0200
commit8ee4a30a0f3545dac5b9a93b40c172f4fb4d44a1 (patch)
tree77baa5b3ef64609b8393eff5ad60abd14ece8f2b /lib/sqfshelper
parent57da296d0e31b87040510fa49eb75e950a4f0d67 (diff)
Move fragment table resizing into block processor callback
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfshelper')
-rw-r--r--lib/sqfshelper/data_writer.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c
index a545896..461cb55 100644
--- a/lib/sqfshelper/data_writer.c
+++ b/lib/sqfshelper/data_writer.c
@@ -111,6 +111,30 @@ static size_t deduplicate_blocks(data_writer_t *data, size_t count)
return i;
}
+static size_t grow_fragment_table(data_writer_t *data, size_t index)
+{
+ size_t newsz;
+ void *new;
+
+ if (index < data->max_fragments)
+ return 0;
+
+ do {
+ newsz = data->max_fragments ? data->max_fragments * 2 : 16;
+ } while (index >= newsz);
+
+ new = realloc(data->fragments, sizeof(data->fragments[0]) * newsz);
+
+ if (new == NULL) {
+ perror("appending to fragment table");
+ return -1;
+ }
+
+ data->max_fragments = newsz;
+ data->fragments = new;
+ return 0;
+}
+
static int block_callback(void *user, sqfs_block_t *blk)
{
data_writer_t *data = user;
@@ -134,6 +158,9 @@ static int block_callback(void *user, sqfs_block_t *blk)
offset = data->file->get_size(data->file);
if (blk->flags & SQFS_BLK_FRAGMENT_BLOCK) {
+ if (grow_fragment_table(data, blk->index))
+ return 0;
+
data->fragments[blk->index].start_offset = htole64(offset);
data->fragments[blk->index].pad0 = 0;
data->fragments[blk->index].size = htole32(out);
@@ -194,24 +221,8 @@ static int block_callback(void *user, sqfs_block_t *blk)
static int flush_fragment_block(data_writer_t *data)
{
- size_t newsz;
- void *new;
int ret;
- if (data->num_fragments == data->max_fragments) {
- newsz = data->max_fragments ? data->max_fragments * 2 : 16;
- new = realloc(data->fragments,
- sizeof(data->fragments[0]) * newsz);
-
- if (new == NULL) {
- perror("appending to fragment table");
- return -1;
- }
-
- data->max_fragments = newsz;
- data->fragments = new;
- }
-
data->frag_block->index = data->num_fragments++;
ret = sqfs_block_processor_enqueue(data->proc, data->frag_block);