diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-02-12 02:22:31 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-02-12 02:26:24 +0100 |
commit | 303680ebcd5adaac2934b63a0edc2d9d1a36d7fb (patch) | |
tree | bd2012dc6fa56f7259dbe2e5edd7ab3042f8e0a0 /lib | |
parent | ec7a522a520017327dd73b4d8e3787016ee1a31e (diff) |
Implement a more explicit object system
Make every dynamically allocated, opaque data structure inherit from
a common sqfs_object_t structure with common entry points (e.g. destroy).
This removes tons of public API functions and replaces them with a
simple sqfs_destroy instead. If semantics of the (until now implicit)
object system need to be extended, it can be much more conveniantely
done this way.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/common/comp_lzo.c | 4 | ||||
-rw-r--r-- | lib/common/io_stdin.c | 4 | ||||
-rw-r--r-- | lib/common/writer.c | 42 | ||||
-rw-r--r-- | lib/sqfs/block_processor/internal.h | 2 | ||||
-rw-r--r-- | lib/sqfs/block_processor/serial.c | 11 | ||||
-rw-r--r-- | lib/sqfs/block_processor/winpthread.c | 93 | ||||
-rw-r--r-- | lib/sqfs/block_writer.c | 14 | ||||
-rw-r--r-- | lib/sqfs/comp/gzip.c | 4 | ||||
-rw-r--r-- | lib/sqfs/comp/lz4.c | 4 | ||||
-rw-r--r-- | lib/sqfs/comp/lzma.c | 4 | ||||
-rw-r--r-- | lib/sqfs/comp/xz.c | 4 | ||||
-rw-r--r-- | lib/sqfs/comp/zstd.c | 4 | ||||
-rw-r--r-- | lib/sqfs/data_reader.c | 21 | ||||
-rw-r--r-- | lib/sqfs/dir_reader.c | 21 | ||||
-rw-r--r-- | lib/sqfs/dir_writer.c | 19 | ||||
-rw-r--r-- | lib/sqfs/frag_table.c | 19 | ||||
-rw-r--r-- | lib/sqfs/id_table.c | 24 | ||||
-rw-r--r-- | lib/sqfs/meta_reader.c | 13 | ||||
-rw-r--r-- | lib/sqfs/meta_writer.c | 30 | ||||
-rw-r--r-- | lib/sqfs/read_table.c | 4 | ||||
-rw-r--r-- | lib/sqfs/unix/io_file.c | 4 | ||||
-rw-r--r-- | lib/sqfs/win32/io_file.c | 4 | ||||
-rw-r--r-- | lib/sqfs/write_table.c | 2 | ||||
-rw-r--r-- | lib/sqfs/xattr_reader.c | 31 | ||||
-rw-r--r-- | lib/sqfs/xattr_writer.c | 38 |
25 files changed, 238 insertions, 182 deletions
diff --git a/lib/common/comp_lzo.c b/lib/common/comp_lzo.c index f371fb6..a0c838a 100644 --- a/lib/common/comp_lzo.c +++ b/lib/common/comp_lzo.c @@ -195,7 +195,7 @@ static sqfs_compressor_t *lzo_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)lzo; } -static void lzo_destroy(sqfs_compressor_t *base) +static void lzo_destroy(sqfs_object_t *base) { free(base); } @@ -247,11 +247,11 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) lzo->buf_size = scratch_size; lzo->work_size = lzo_algs[cfg->opt.lzo.algorithm].bufsize; - base->destroy = lzo_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? lzo_uncomp_block : lzo_comp_block; base->write_options = lzo_write_options; base->read_options = lzo_read_options; base->create_copy = lzo_create_copy; + ((sqfs_object_t *)base)->destroy = lzo_destroy; return base; } diff --git a/lib/common/io_stdin.c b/lib/common/io_stdin.c index ca6bf49..469e300 100644 --- a/lib/common/io_stdin.c +++ b/lib/common/io_stdin.c @@ -21,7 +21,7 @@ typedef struct { } sqfs_file_stdinout_t; -static void stdinout_destroy(sqfs_file_t *base) +static void stdinout_destroy(sqfs_object_t *base) { free(base); } @@ -159,7 +159,7 @@ sqfs_file_t *sqfs_get_stdin_file(FILE *fp, const sparse_map_t *map, file->map = map; file->fp = fp; - base->destroy = stdinout_destroy; + ((sqfs_object_t *)base)->destroy = stdinout_destroy; base->write_at = stdin_write_at; base->get_size = stdinout_get_size; base->truncate = stdinout_truncate; diff --git a/lib/common/writer.c b/lib/common/writer.c index 84a40d2..4762b82 100644 --- a/lib/common/writer.c +++ b/lib/common/writer.c @@ -90,7 +90,7 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) #ifdef WITH_LZO if (cfg.id == SQFS_COMP_LZO) { if (sqfs->cmp != NULL) - sqfs->cmp->destroy(sqfs->cmp); + sqfs_destroy(sqfs->cmp); sqfs->cmp = lzo_compressor_create(&cfg); } @@ -187,26 +187,26 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) return 0; fail_dm: - sqfs_meta_writer_destroy(sqfs->dm); + sqfs_destroy(sqfs->dm); fail_im: - sqfs_meta_writer_destroy(sqfs->im); + sqfs_destroy(sqfs->im); fail_xwr: if (sqfs->xwr != NULL) - sqfs_xattr_writer_destroy(sqfs->xwr); + sqfs_destroy(sqfs->xwr); fail_id: - sqfs_id_table_destroy(sqfs->idtbl); + sqfs_destroy(sqfs->idtbl); fail_data: - sqfs_block_processor_destroy(sqfs->data); + sqfs_destroy(sqfs->data); fail_fragtbl: - sqfs_frag_table_destroy(sqfs->fragtbl); + sqfs_destroy(sqfs->fragtbl); fail_blkwr: - sqfs_block_writer_destroy(sqfs->blkwr); + sqfs_destroy(sqfs->blkwr); fail_cmp: - sqfs->cmp->destroy(sqfs->cmp); + sqfs_destroy(sqfs->cmp); fail_fs: fstree_cleanup(&sqfs->fs); fail_file: - sqfs->outfile->destroy(sqfs->outfile); + sqfs_destroy(sqfs->outfile); return -1; } @@ -299,16 +299,16 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg) void sqfs_writer_cleanup(sqfs_writer_t *sqfs) { if (sqfs->xwr != NULL) - sqfs_xattr_writer_destroy(sqfs->xwr); - - sqfs_dir_writer_destroy(sqfs->dirwr); - sqfs_meta_writer_destroy(sqfs->dm); - sqfs_meta_writer_destroy(sqfs->im); - sqfs_id_table_destroy(sqfs->idtbl); - sqfs_block_processor_destroy(sqfs->data); - sqfs_block_writer_destroy(sqfs->blkwr); - sqfs_frag_table_destroy(sqfs->fragtbl); - sqfs->cmp->destroy(sqfs->cmp); + sqfs_destroy(sqfs->xwr); + + sqfs_destroy(sqfs->dirwr); + sqfs_destroy(sqfs->dm); + sqfs_destroy(sqfs->im); + sqfs_destroy(sqfs->idtbl); + sqfs_destroy(sqfs->data); + sqfs_destroy(sqfs->blkwr); + sqfs_destroy(sqfs->fragtbl); + sqfs_destroy(sqfs->cmp); fstree_cleanup(&sqfs->fs); - sqfs->outfile->destroy(sqfs->outfile); + sqfs_destroy(sqfs->outfile); } diff --git a/lib/sqfs/block_processor/internal.h b/lib/sqfs/block_processor/internal.h index b3d76ba..71f8fab 100644 --- a/lib/sqfs/block_processor/internal.h +++ b/lib/sqfs/block_processor/internal.h @@ -50,6 +50,8 @@ typedef struct sqfs_block_t { } sqfs_block_t; struct sqfs_block_processor_t { + sqfs_object_t obj; + /* synchronization primitives */ #ifdef WITH_PTHREAD pthread_mutex_t mtx; diff --git a/lib/sqfs/block_processor/serial.c b/lib/sqfs/block_processor/serial.c index e18b67e..469388c 100644 --- a/lib/sqfs/block_processor/serial.c +++ b/lib/sqfs/block_processor/serial.c @@ -7,6 +7,11 @@ #define SQFS_BUILDING_DLL #include "internal.h" +static void block_processor_destroy(sqfs_object_t *obj) +{ + block_processor_cleanup((sqfs_block_processor_t *)obj); +} + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -27,14 +32,10 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, return NULL; } + ((sqfs_object_t *)proc)->destroy = block_processor_destroy; return proc; } -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) -{ - block_processor_cleanup(proc); -} - int test_and_set_status(sqfs_block_processor_t *proc, int status) { if (proc->status == 0) diff --git a/lib/sqfs/block_processor/winpthread.c b/lib/sqfs/block_processor/winpthread.c index 85416ab..a91f001 100644 --- a/lib/sqfs/block_processor/winpthread.c +++ b/lib/sqfs/block_processor/winpthread.c @@ -94,6 +94,35 @@ static THREAD_TYPE worker_proc(THREAD_ARG arg) } #if defined(_WIN32) || defined(__WINDOWS__) +static void block_processor_destroy(sqfs_object_t *obj) +{ + sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj; + unsigned int i; + + EnterCriticalSection(&proc->mtx); + proc->status = -1; + WakeAllConditionVariable(&proc->queue_cond); + LeaveCriticalSection(&proc->mtx); + + for (i = 0; i < proc->num_workers; ++i) { + if (proc->workers[i] == NULL) + continue; + + if (proc->workers[i]->thread != NULL) { + WaitForSingleObject(proc->workers[i]->thread, INFINITE); + CloseHandle(proc->workers[i]->thread); + } + + if (proc->workers[i]->cmp != NULL) + sqfs_destroy(proc->workers[i]->cmp); + + free(proc->workers[i]); + } + + DeleteCriticalSection(&proc->mtx); + block_processor_cleanup(proc); +} + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -112,6 +141,8 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, if (proc == NULL) return NULL; + ((sqfs_object_t *)obj)->destroy = block_processor_destroy; + InitializeCriticalSection(&proc->mtx); InitializeConditionVariable(&proc->queue_cond); InitializeConditionVariable(&proc->done_cond); @@ -142,38 +173,34 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, return proc; fail: - sqfs_block_processor_destroy(proc); + block_processor_destroy(proc); return NULL; } - -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) +#else +static void block_processor_destroy(sqfs_object_t *obj) { + sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj; unsigned int i; - EnterCriticalSection(&proc->mtx); + pthread_mutex_lock(&proc->mtx); proc->status = -1; - WakeAllConditionVariable(&proc->queue_cond); - LeaveCriticalSection(&proc->mtx); + pthread_cond_broadcast(&proc->queue_cond); + pthread_mutex_unlock(&proc->mtx); for (i = 0; i < proc->num_workers; ++i) { - if (proc->workers[i] == NULL) - continue; - - if (proc->workers[i]->thread != NULL) { - WaitForSingleObject(proc->workers[i]->thread, INFINITE); - CloseHandle(proc->workers[i]->thread); - } - - if (proc->workers[i]->cmp != NULL) - proc->workers[i]->cmp->destroy(proc->workers[i]->cmp); + pthread_join(proc->workers[i]->thread, NULL); + sqfs_destroy(proc->workers[i]->cmp); free(proc->workers[i]); } - DeleteCriticalSection(&proc->mtx); + pthread_cond_destroy(&proc->done_cond); + pthread_cond_destroy(&proc->queue_cond); + pthread_mutex_destroy(&proc->mtx); + block_processor_cleanup(proc); } -#else + sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, sqfs_compressor_t *cmp, unsigned int num_workers, @@ -194,6 +221,7 @@ sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size, if (proc == NULL) return NULL; + ((sqfs_object_t *)proc)->destroy = block_processor_destroy; proc->mtx = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; proc->queue_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; proc->done_cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; @@ -246,10 +274,8 @@ fail_thread: fail_init: for (i = 0; i < num_workers; ++i) { if (proc->workers[i] != NULL) { - if (proc->workers[i]->cmp != NULL) { - proc->workers[i]->cmp-> - destroy(proc->workers[i]->cmp); - } + if (proc->workers[i]->cmp != NULL) + sqfs_destroy(proc->workers[i]->cmp); free(proc->workers[i]); } @@ -260,29 +286,6 @@ fail_init: block_processor_cleanup(proc); return NULL; } - -void sqfs_block_processor_destroy(sqfs_block_processor_t *proc) -{ - unsigned int i; - - pthread_mutex_lock(&proc->mtx); - proc->status = -1; - pthread_cond_broadcast(&proc->queue_cond); - pthread_mutex_unlock(&proc->mtx); - - for (i = 0; i < proc->num_workers; ++i) { - pthread_join(proc->workers[i]->thread, NULL); - - proc->workers[i]->cmp->destroy(proc->workers[i]->cmp); - free(proc->workers[i]); - } - - pthread_cond_destroy(&proc->done_cond); - pthread_cond_destroy(&proc->queue_cond); - pthread_mutex_destroy(&proc->mtx); - - block_processor_cleanup(proc); -} #endif int append_to_work_queue(sqfs_block_processor_t *proc, sqfs_block_t *block, diff --git a/lib/sqfs/block_writer.c b/lib/sqfs/block_writer.c index f77fcf8..63f8c61 100644 --- a/lib/sqfs/block_writer.c +++ b/lib/sqfs/block_writer.c @@ -26,6 +26,7 @@ typedef struct { } blk_info_t; struct sqfs_block_writer_t { + sqfs_object_t *base; sqfs_file_t *file; size_t num_blocks; @@ -115,6 +116,12 @@ static int align_file(sqfs_block_writer_t *wr) return store_block_location(wr, size, 0, 0); } +static void block_writer_destroy(sqfs_object_t *wr) +{ + free(((sqfs_block_writer_t *)wr)->blocks); + free(wr); +} + sqfs_block_writer_t *sqfs_block_writer_create(sqfs_file_t *file, size_t devblksz, sqfs_u32 flags) { @@ -127,6 +134,7 @@ sqfs_block_writer_t *sqfs_block_writer_create(sqfs_file_t *file, if (wr == NULL) return NULL; + ((sqfs_object_t *)wr)->destroy = block_writer_destroy; wr->file = file; wr->devblksz = devblksz; wr->max_blocks = INIT_BLOCK_COUNT; @@ -153,12 +161,6 @@ int sqfs_block_writer_set_hooks(sqfs_block_writer_t *wr, void *user_ptr, return 0; } -void sqfs_block_writer_destroy(sqfs_block_writer_t *wr) -{ - free(wr->blocks); - free(wr); -} - int sqfs_block_writer_write(sqfs_block_writer_t *wr, sqfs_u32 size, sqfs_u32 checksum, sqfs_u32 flags, const sqfs_u8 *data, sqfs_u64 *location) diff --git a/lib/sqfs/comp/gzip.c b/lib/sqfs/comp/gzip.c index 7183e5a..37491bb 100644 --- a/lib/sqfs/comp/gzip.c +++ b/lib/sqfs/comp/gzip.c @@ -31,7 +31,7 @@ typedef struct { gzip_options_t opt; } gzip_compressor_t; -static void gzip_destroy(sqfs_compressor_t *base) +static void gzip_destroy(sqfs_object_t *base) { gzip_compressor_t *gzip = (gzip_compressor_t *)base; @@ -264,10 +264,10 @@ sqfs_compressor_t *gzip_compressor_create(const sqfs_compressor_config_t *cfg) gzip->compress = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) == 0; gzip->block_size = cfg->block_size; base->do_block = gzip_do_block; - base->destroy = gzip_destroy; base->write_options = gzip_write_options; base->read_options = gzip_read_options; base->create_copy = gzip_create_copy; + ((sqfs_object_t *)base)->destroy = gzip_destroy; if (gzip->compress) { ret = deflateInit2(&gzip->strm, cfg->opt.gzip.level, diff --git a/lib/sqfs/comp/lz4.c b/lib/sqfs/comp/lz4.c index dd9b6ac..7656d67 100644 --- a/lib/sqfs/comp/lz4.c +++ b/lib/sqfs/comp/lz4.c @@ -110,7 +110,7 @@ static sqfs_compressor_t *lz4_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)lz4; } -static void lz4_destroy(sqfs_compressor_t *base) +static void lz4_destroy(sqfs_object_t *base) { free(base); } @@ -132,11 +132,11 @@ sqfs_compressor_t *lz4_compressor_create(const sqfs_compressor_config_t *cfg) lz4->high_compression = (cfg->flags & SQFS_COMP_FLAG_LZ4_HC) != 0; - base->destroy = lz4_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? lz4_uncomp_block : lz4_comp_block; base->write_options = lz4_write_options; base->read_options = lz4_read_options; base->create_copy = lz4_create_copy; + ((sqfs_object_t *)base)->destroy = lz4_destroy; return base; } diff --git a/lib/sqfs/comp/lzma.c b/lib/sqfs/comp/lzma.c index f7ab428..900cc14 100644 --- a/lib/sqfs/comp/lzma.c +++ b/lib/sqfs/comp/lzma.c @@ -153,7 +153,7 @@ static sqfs_compressor_t *lzma_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)copy; } -static void lzma_destroy(sqfs_compressor_t *base) +static void lzma_destroy(sqfs_object_t *base) { free(base); } @@ -176,11 +176,11 @@ sqfs_compressor_t *lzma_compressor_create(const sqfs_compressor_config_t *cfg) if (lzma->block_size < SQFS_META_BLOCK_SIZE) lzma->block_size = SQFS_META_BLOCK_SIZE; - base->destroy = lzma_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? lzma_uncomp_block : lzma_comp_block; base->write_options = lzma_write_options; base->read_options = lzma_read_options; base->create_copy = lzma_create_copy; + ((sqfs_object_t *)base)->destroy = lzma_destroy; return base; } diff --git a/lib/sqfs/comp/xz.c b/lib/sqfs/comp/xz.c index 91d48da..d612729 100644 --- a/lib/sqfs/comp/xz.c +++ b/lib/sqfs/comp/xz.c @@ -207,7 +207,7 @@ static sqfs_compressor_t *xz_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)xz; } -static void xz_destroy(sqfs_compressor_t *base) +static void xz_destroy(sqfs_object_t *base) { free(base); } @@ -233,11 +233,11 @@ sqfs_compressor_t *xz_compressor_create(const sqfs_compressor_config_t *cfg) xz->flags = cfg->flags; xz->dict_size = cfg->opt.xz.dict_size; xz->block_size = cfg->block_size; - base->destroy = xz_destroy; base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ? xz_uncomp_block : xz_comp_block; base->write_options = xz_write_options; base->read_options = xz_read_options; base->create_copy = xz_create_copy; + ((sqfs_object_t *)base)->destroy = xz_destroy; return base; } diff --git a/lib/sqfs/comp/zstd.c b/lib/sqfs/comp/zstd.c index a850033..4bd9456 100644 --- a/lib/sqfs/comp/zstd.c +++ b/lib/sqfs/comp/zstd.c @@ -110,7 +110,7 @@ static sqfs_compressor_t *zstd_create_copy(sqfs_compressor_t *cmp) return (sqfs_compressor_t *)zstd; } -static void zstd_destroy(sqfs_compressor_t *base) +static void zstd_destroy(sqfs_object_t *base) { zstd_compressor_t *zstd = (zstd_compressor_t *)base; @@ -142,11 +142,11 @@ sqfs_compressor_t *zstd_compressor_create(const sqfs_compressor_config_t *cfg) return NULL; } - base->destroy = zstd_destroy; base->do_block = cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS ? zstd_uncomp_block : zstd_comp_block; base->write_options = zstd_write_options; base->read_options = zstd_read_options; base->create_copy = zstd_create_copy; + ((sqfs_object_t *)base)->destroy = zstd_destroy; return base; } diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c index c8886c5..8c9bb2e 100644 --- a/lib/sqfs/data_reader.c +++ b/lib/sqfs/data_reader.c @@ -21,6 +21,8 @@ #include <string.h> struct sqfs_data_reader_t { + sqfs_object_t obj; + sqfs_frag_table_t *frag_tbl; sqfs_compressor_t *cmp; sqfs_file_t *file; @@ -125,6 +127,16 @@ static int precache_fragment_block(sqfs_data_reader_t *data, size_t idx) &data->frag_blk_size, &data->frag_block); } +static void data_reader_destroy(sqfs_object_t *obj) +{ + sqfs_data_reader_t *data = (sqfs_data_reader_t *)obj; + + sqfs_destroy(data->frag_tbl); + free(data->data_block); + free(data->frag_block); + free(data); +} + sqfs_data_reader_t *sqfs_data_reader_create(sqfs_file_t *file, size_t block_size, sqfs_compressor_t *cmp) @@ -140,6 +152,7 @@ sqfs_data_reader_t *sqfs_data_reader_create(sqfs_file_t *file, return NULL; } + ((sqfs_object_t *)data)->destroy = data_reader_destroy; data->file = file; data->block_size = block_size; data->cmp = cmp; @@ -164,14 +177,6 @@ int sqfs_data_reader_load_fragment_table(sqfs_data_reader_t *data, return 0; } -void sqfs_data_reader_destroy(sqfs_data_reader_t *data) -{ - sqfs_frag_table_destroy(data->frag_tbl); - free(data->data_block); - free(data->frag_block); - free(data); -} - int sqfs_data_reader_get_block(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, size_t index, size_t *size, sqfs_u8 **out) diff --git a/lib/sqfs/dir_reader.c b/lib/sqfs/dir_reader.c index 8eae5bd..39e3e57 100644 --- a/lib/sqfs/dir_reader.c +++ b/lib/sqfs/dir_reader.c @@ -20,6 +20,8 @@ #include <stdlib.h> struct sqfs_dir_reader_t { + sqfs_object_t base; + sqfs_meta_reader_t *meta_dir; sqfs_meta_reader_t *meta_inode; const sqfs_super_t *super; @@ -34,6 +36,15 @@ struct sqfs_dir_reader_t { sqfs_u16 inode_offset; }; +static void dir_reader_destroy(sqfs_object_t *obj) +{ + sqfs_dir_reader_t *rd = (sqfs_dir_reader_t *)obj; + + sqfs_destroy(rd->meta_inode); + sqfs_destroy(rd->meta_dir); + free(rd); +} + sqfs_dir_reader_t *sqfs_dir_reader_create(const sqfs_super_t *super, sqfs_compressor_t *cmp, sqfs_file_t *file) @@ -66,22 +77,16 @@ sqfs_dir_reader_t *sqfs_dir_reader_create(const sqfs_super_t *super, rd->meta_dir = sqfs_meta_reader_create(file, cmp, start, limit); if (rd->meta_dir == NULL) { - sqfs_meta_reader_destroy(rd->meta_inode); + sqfs_destroy(rd->meta_inode); free(rd); return NULL; } + ((sqfs_object_t *)rd)->destroy = dir_reader_destroy; rd->super = super; return rd; } -void sqfs_dir_reader_destroy(sqfs_dir_reader_t *rd) -{ - sqfs_meta_reader_destroy(rd->meta_inode); - sqfs_meta_reader_destroy(rd->meta_dir); - free(rd); -} - int sqfs_dir_reader_open_dir(sqfs_dir_reader_t *rd, const sqfs_inode_generic_t *inode) { diff --git a/lib/sqfs/dir_writer.c b/lib/sqfs/dir_writer.c index 53e5800..9dd7ad2 100644 --- a/lib/sqfs/dir_writer.c +++ b/lib/sqfs/dir_writer.c @@ -37,6 +37,8 @@ typedef struct index_ent_t { } index_ent_t; struct sqfs_dir_writer_t { + sqfs_object_t base; + dir_entry_t *list; dir_entry_t *list_end; @@ -136,6 +138,15 @@ static int add_export_table_entry(sqfs_dir_writer_t *writer, return 0; } +static void dir_writer_destroy(sqfs_object_t *obj) +{ + sqfs_dir_writer_t *writer = (sqfs_dir_writer_t *)obj; + + writer_reset(writer); + free(writer->export_tbl); + free(writer); +} + sqfs_dir_writer_t *sqfs_dir_writer_create(sqfs_meta_writer_t *dm, sqfs_u32 flags) { @@ -162,17 +173,11 @@ sqfs_dir_writer_t *sqfs_dir_writer_create(sqfs_meta_writer_t *dm, sizeof(writer->export_tbl[0]) * writer->export_tbl_max); } + ((sqfs_object_t *)writer)->destroy = dir_writer_destroy; writer->dm = dm; return writer; } -void sqfs_dir_writer_destroy(sqfs_dir_writer_t *writer) -{ - writer_reset(writer); - free(writer->export_tbl); - free(writer); -} - int sqfs_dir_writer_begin(sqfs_dir_writer_t *writer, sqfs_u32 flags) { sqfs_u32 offset; diff --git a/lib/sqfs/frag_table.c b/lib/sqfs/frag_table.c index 200f3d5..58851c5 100644 --- a/lib/sqfs/frag_table.c +++ b/lib/sqfs/frag_table.c @@ -27,6 +27,8 @@ typedef struct { struct sqfs_frag_table_t { + sqfs_object_t base; + size_t capacity; size_t used; sqfs_fragment_t *table; @@ -37,6 +39,15 @@ struct sqfs_frag_table_t { chunk_info_t *chunk_list; }; +static void frag_table_destroy(sqfs_object_t *obj) +{ + sqfs_frag_table_t *tbl = (sqfs_frag_table_t *)obj; + + free(tbl->chunk_list); + free(tbl->table); + free(tbl); +} + sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags) { sqfs_frag_table_t *tbl; @@ -48,16 +59,10 @@ sqfs_frag_table_t *sqfs_frag_table_create(sqfs_u32 flags) if (tbl == NULL) return NULL; + ((sqfs_object_t *)tbl)->destroy = frag_table_destroy; return tbl; } -void sqfs_frag_table_destroy(sqfs_frag_table_t *tbl) -{ - free(tbl->chunk_list); - free(tbl->table); - free(tbl); -} - int sqfs_frag_table_read(sqfs_frag_table_t *tbl, sqfs_file_t *file, const sqfs_super_t *super, sqfs_compressor_t *cmp) { diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c index 8c30964..86f2809 100644 --- a/lib/sqfs/id_table.c +++ b/lib/sqfs/id_table.c @@ -17,23 +17,35 @@ #include <string.h> struct sqfs_id_table_t { + sqfs_object_t base; + sqfs_u32 *ids; size_t num_ids; size_t max_ids; }; +static void id_table_destroy(sqfs_object_t *obj) +{ + sqfs_id_table_t *tbl = (sqfs_id_table_t *)obj; + + free(tbl->ids); + free(tbl); +} + sqfs_id_table_t *sqfs_id_table_create(sqfs_u32 flags) { + sqfs_id_table_t *tbl; + if (flags != 0) return NULL; - return calloc(1, sizeof(sqfs_id_table_t)); -} + tbl = calloc(1, sizeof(sqfs_id_table_t)); -void sqfs_id_table_destroy(sqfs_id_table_t *tbl) -{ - free(tbl->ids); - free(tbl); + if (tbl != NULL) { + ((sqfs_object_t *)tbl)->destroy = id_table_destroy; + } + + return tbl; } int sqfs_id_table_id_to_index(sqfs_id_table_t *tbl, sqfs_u32 id, sqfs_u16 *out) diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c index 19e856a..8fd7f21 100644 --- a/lib/sqfs/meta_reader.c +++ b/lib/sqfs/meta_reader.c @@ -18,6 +18,8 @@ #include <string.h> struct sqfs_meta_reader_t { + sqfs_object_t base; + sqfs_u64 start; sqfs_u64 limit; size_t data_used; @@ -44,6 +46,11 @@ struct sqfs_meta_reader_t { sqfs_u8 scratch[SQFS_META_BLOCK_SIZE]; }; +static void meta_reader_destroy(sqfs_object_t *m) +{ + free(m); +} + sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, sqfs_compressor_t *cmp, sqfs_u64 start, sqfs_u64 limit) @@ -53,6 +60,7 @@ sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, if (m == NULL) return NULL; + ((sqfs_object_t *)m)->destroy = meta_reader_destroy; m->start = start; m->limit = limit; m->file = file; @@ -60,11 +68,6 @@ sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, return m; } -void sqfs_meta_reader_destroy(sqfs_meta_reader_t *m) -{ - free(m); -} - int sqfs_meta_reader_seek(sqfs_meta_reader_t *m, sqfs_u64 block_start, size_t offset) { diff --git a/lib/sqfs/meta_writer.c b/lib/sqfs/meta_writer.c index acb2235..46a67cd 100644 --- a/lib/sqfs/meta_writer.c +++ b/lib/sqfs/meta_writer.c @@ -25,6 +25,8 @@ typedef struct meta_block_t { } meta_block_t; struct sqfs_meta_writer_t { + sqfs_object_t base; + /* A byte offset into the uncompressed data of the current block */ size_t offset; @@ -53,6 +55,20 @@ static int write_block(sqfs_file_t *file, meta_block_t *outblk) return file->write_at(file, off, outblk->data, count + 2); } +static void meta_writer_destroy(sqfs_object_t *obj) +{ + sqfs_meta_writer_t *m = (sqfs_meta_writer_t *)obj; + meta_block_t *blk; + + while (m->list != NULL) { + blk = m->list; + m->list = blk->next; + free(blk); + } + + free(m); +} + sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file, sqfs_compressor_t *cmp, sqfs_u32 flags) @@ -66,25 +82,13 @@ sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file, if (m == NULL) return NULL; + ((sqfs_object_t *)m)->destroy = meta_writer_destroy; m->cmp = cmp; m->file = file; m->flags = flags; return m; } -void sqfs_meta_writer_destroy(sqfs_meta_writer_t *m) -{ - meta_block_t *blk; - - while (m->list != NULL) { - blk = m->list; - m->list = blk->next; - free(blk); - } - - free(m); -} - int sqfs_meta_writer_flush(sqfs_meta_writer_t *m) { meta_block_t *outblk; diff --git a/lib/sqfs/read_table.c b/lib/sqfs/read_table.c index 096ac1a..1c9c8ad 100644 --- a/lib/sqfs/read_table.c +++ b/lib/sqfs/read_table.c @@ -76,12 +76,12 @@ int sqfs_read_table(sqfs_file_t *file, sqfs_compressor_t *cmp, table_size -= diff; } - sqfs_meta_reader_destroy(m); + sqfs_destroy(m); free(locations); *out = data; return 0; fail: - sqfs_meta_reader_destroy(m); + sqfs_destroy(m); fail_idx: free(locations); fail_data: diff --git a/lib/sqfs/unix/io_file.c b/lib/sqfs/unix/io_file.c index f93af4d..1c181ab 100644 --- a/lib/sqfs/unix/io_file.c +++ b/lib/sqfs/unix/io_file.c @@ -25,7 +25,7 @@ typedef struct { } sqfs_file_stdio_t; -static void stdio_destroy(sqfs_file_t *base) +static void stdio_destroy(sqfs_object_t *base) { sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; @@ -154,10 +154,10 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) file->size = sb.st_size; - base->destroy = stdio_destroy; base->read_at = stdio_read_at; base->write_at = stdio_write_at; base->get_size = stdio_get_size; base->truncate = stdio_truncate; + ((sqfs_object_t *)base)->destroy = stdio_destroy; return base; } diff --git a/lib/sqfs/win32/io_file.c b/lib/sqfs/win32/io_file.c index 951a0fe..c1849a8 100644 --- a/lib/sqfs/win32/io_file.c +++ b/lib/sqfs/win32/io_file.c @@ -24,7 +24,7 @@ typedef struct { } sqfs_file_stdio_t; -static void stdio_destroy(sqfs_file_t *base) +static void stdio_destroy(sqfs_object_t *base) { sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; @@ -161,10 +161,10 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) } file->size = size.QuadPart; - base->destroy = stdio_destroy; base->read_at = stdio_read_at; base->write_at = stdio_write_at; base->get_size = stdio_get_size; base->truncate = stdio_truncate; + ((sqfs_object_t *)base)->destroy = stdio_destroy; return base; } diff --git a/lib/sqfs/write_table.c b/lib/sqfs/write_table.c index 1931b83..b198941 100644 --- a/lib/sqfs/write_table.c +++ b/lib/sqfs/write_table.c @@ -74,7 +74,7 @@ int sqfs_write_table(sqfs_file_t *file, sqfs_compressor_t *cmp, /* cleanup */ ret = 0; out: - sqfs_meta_writer_destroy(m); + sqfs_destroy(m); out_idx: free(locations); return ret; diff --git a/lib/sqfs/xattr_reader.c b/lib/sqfs/xattr_reader.c index d5b003b..97cf821 100644 --- a/lib/sqfs/xattr_reader.c +++ b/lib/sqfs/xattr_reader.c @@ -21,6 +21,8 @@ #include <errno.h> struct sqfs_xattr_reader_t { + sqfs_object_t base; + sqfs_u64 xattr_start; size_t num_id_blocks; @@ -34,6 +36,20 @@ struct sqfs_xattr_reader_t { sqfs_file_t *file; }; +static void xattr_reader_destroy(sqfs_object_t *obj) +{ + sqfs_xattr_reader_t *xr = (sqfs_xattr_reader_t *)obj; + + if (xr->kvrd != NULL) + sqfs_destroy(xr->kvrd); + + if (xr->idrd != NULL) + sqfs_destroy(xr->idrd); + + free(xr->id_block_starts); + free(xr); +} + int sqfs_xattr_reader_load_locations(sqfs_xattr_reader_t *xr) { sqfs_xattr_id_table_t idtbl; @@ -242,18 +258,6 @@ int sqfs_xattr_reader_get_desc(sqfs_xattr_reader_t *xr, sqfs_u32 idx, return 0; } -void sqfs_xattr_reader_destroy(sqfs_xattr_reader_t *xr) -{ - if (xr->kvrd != NULL) - sqfs_meta_reader_destroy(xr->kvrd); - - if (xr->idrd != NULL) - sqfs_meta_reader_destroy(xr->idrd); - - free(xr->id_block_starts); - free(xr); -} - sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp) @@ -263,6 +267,7 @@ sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, if (xr == NULL) return NULL; + ((sqfs_object_t *)xr)->destroy = xattr_reader_destroy; xr->file = file; xr->super = super; @@ -286,6 +291,6 @@ sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, return xr; fail: - sqfs_xattr_reader_destroy(xr); + sqfs_destroy(xr); return NULL; } diff --git a/lib/sqfs/xattr_writer.c b/lib/sqfs/xattr_writer.c index 047e411..827d0c5 100644 --- a/lib/sqfs/xattr_writer.c +++ b/lib/sqfs/xattr_writer.c @@ -99,6 +99,8 @@ typedef struct kv_block_desc_t { } kv_block_desc_t; struct sqfs_xattr_writer_t { + sqfs_object_t base; + str_table_t keys; str_table_t values; @@ -113,6 +115,23 @@ struct sqfs_xattr_writer_t { }; +static void xattr_writer_destroy(sqfs_object_t *obj) +{ + sqfs_xattr_writer_t *xwr = (sqfs_xattr_writer_t *)obj; + kv_block_desc_t *blk; + + while (xwr->kv_blocks != NULL) { + blk = xwr->kv_blocks; + xwr->kv_blocks = xwr->kv_blocks->next; + free(blk); + } + + free(xwr->kv_pairs); + str_table_cleanup(&xwr->values); + str_table_cleanup(&xwr->keys); + free(xwr); +} + sqfs_xattr_writer_t *sqfs_xattr_writer_create(void) { sqfs_xattr_writer_t *xwr = calloc(1, sizeof(*xwr)); @@ -129,6 +148,7 @@ sqfs_xattr_writer_t *sqfs_xattr_writer_create(void) if (xwr->kv_pairs == NULL) goto fail_pairs; + ((sqfs_object_t *)xwr)->destroy = xattr_writer_destroy; return xwr; fail_pairs: str_table_cleanup(&xwr->values); @@ -139,22 +159,6 @@ fail_keys: return NULL; } -void sqfs_xattr_writer_destroy(sqfs_xattr_writer_t *xwr) -{ - kv_block_desc_t *blk; - - while (xwr->kv_blocks != NULL) { - blk = xwr->kv_blocks; - xwr->kv_blocks = xwr->kv_blocks->next; - free(blk); - } - - free(xwr->kv_pairs); - str_table_cleanup(&xwr->values); - str_table_cleanup(&xwr->keys); - free(xwr); -} - int sqfs_xattr_writer_begin(sqfs_xattr_writer_t *xwr) { xwr->kv_start = xwr->num_pairs; @@ -603,6 +607,6 @@ int sqfs_xattr_writer_flush(sqfs_xattr_writer_t *xwr, sqfs_file_t *file, locations, count); out: free(locations); - sqfs_meta_writer_destroy(mw); + sqfs_destroy(mw); return err; } |