From 7ca19d23cb4913b5dabfdf3469852ec9f2c0f8d7 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 13 Aug 2019 14:11:25 +0200 Subject: Add block processor data structure The interface is designed for parallel, asynchronuous processing of data blocks with an I/O callback that handles the serialized result. The underlying implementation is currently still synchronuous. Signed-off-by: David Oberhollenzer --- lib/comp/block_processor.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ lib/comp/create_block.c | 37 ++++++++++++++++++++++++ lib/comp/process_block.c | 37 ++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 lib/comp/block_processor.c create mode 100644 lib/comp/create_block.c create mode 100644 lib/comp/process_block.c (limited to 'lib/comp') diff --git a/lib/comp/block_processor.c b/lib/comp/block_processor.c new file mode 100644 index 0000000..5938b3a --- /dev/null +++ b/lib/comp/block_processor.c @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * block_processor.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "block_processor.h" +#include "util.h" + +#include +#include +#include + +struct block_processor_t { + size_t max_block_size; + compressor_t *cmp; + block_cb cb; + void *user; + int status; + + uint8_t scratch[]; +}; + +block_processor_t *block_processor_create(size_t max_block_size, + compressor_t *cmp, + unsigned int num_workers, + void *user, + block_cb callback) +{ + block_processor_t *proc = calloc(1, sizeof(*proc) + max_block_size); + (void)num_workers; + + if (proc == NULL) { + perror("Creating block processor"); + return NULL; + } + + proc->max_block_size = max_block_size; + proc->cmp = cmp; + proc->cb = callback; + proc->user = user; + return proc; +} + +void block_processor_destroy(block_processor_t *proc) +{ + free(proc); +} + +int block_processor_enqueue(block_processor_t *proc, block_t *block) +{ + if (process_block(block, proc->cmp, + proc->scratch, proc->max_block_size)) + goto fail; + + if (proc->cb(proc->user, block)) + goto fail; + + free(block); + return 0; +fail: + free(block); + proc->status = -1; + return -1; +} + +int block_processor_finish(block_processor_t *proc) +{ + return proc->status; +} diff --git a/lib/comp/create_block.c b/lib/comp/create_block.c new file mode 100644 index 0000000..e410091 --- /dev/null +++ b/lib/comp/create_block.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * create_block.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "block_processor.h" +#include "util.h" + +#include +#include +#include + +block_t *create_block(const char *filename, int fd, size_t size, + void *user, uint32_t flags) +{ + block_t *blk = calloc(1, sizeof(*blk) + size); + + if (blk == NULL) { + perror(filename); + return NULL; + } + + if (fd >= 0) { + if (read_data(filename, fd, blk->data, size)) { + free(blk); + return NULL; + } + } + + blk->size = size; + blk->user = user; + blk->flags = flags; + return blk; +} diff --git a/lib/comp/process_block.c b/lib/comp/process_block.c new file mode 100644 index 0000000..76cd07d --- /dev/null +++ b/lib/comp/process_block.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * process_block.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "block_processor.h" +#include "util.h" + +#include + +int process_block(block_t *block, compressor_t *cmp, + uint8_t *scratch, size_t scratch_size) +{ + ssize_t ret; + + if (!(block->flags & BLK_DONT_CHECKSUM)) + block->checksum = update_crc32(0, block->data, block->size); + + if (!(block->flags & BLK_DONT_COMPRESS)) { + ret = cmp->do_block(cmp, block->data, block->size, + scratch, scratch_size); + + if (ret < 0) + return -1; + + if (ret > 0) { + memcpy(block->data, scratch, ret); + block->size = ret; + block->flags |= BLK_IS_COMPRESSED; + } + } + + return 0; +} -- cgit v1.2.3