[PATCH v2 1/4] erofs-utils: lib: refactor erofs compressors init

Guo Xuenan guoxuenan at huawei.com
Thu Jun 15 20:17:24 AEST 2023


using struct erofs_compressors_cfg for erofs compressor
global configuration.

Signed-off-by: Guo Xuenan <guoxuenan at huawei.com>
---
 lib/compressor.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/compressor.h | 14 ++++++++++
 2 files changed, 82 insertions(+)

diff --git a/lib/compressor.c b/lib/compressor.c
index 52eb761..88a2fb0 100644
--- a/lib/compressor.c
+++ b/lib/compressor.c
@@ -22,6 +22,74 @@ static const struct erofs_compressor *compressors[] = {
 #endif
 };
 
+/* for compressors type configuration */
+static struct erofs_supported_algothrim {
+	int algtype;
+	const char *name;
+} erofs_supported_algothrims[] = {
+	{ Z_EROFS_COMPRESSION_LZ4, "lz4"},
+	{ Z_EROFS_COMPRESSION_LZ4, "lz4hc"},
+	{ Z_EROFS_COMPRESSION_LZMA, "lzma"},
+};
+
+/* global compressors configuration */
+static struct erofs_compressors_cfg erofs_ccfg;
+
+static void erofs_init_compressor(struct compressor *compressor,
+	const struct erofs_compressor *alg)
+{
+
+	compressor->handle.alg = alg;
+
+	/* should be written in "minimum compression ratio * 100" */
+	compressor->handle.compress_threshold = 100;
+
+	/* optimize for 4k size page */
+	compressor->handle.destsize_alignsize = erofs_blksiz();
+	compressor->handle.destsize_redzone_begin = erofs_blksiz() - 16;
+	compressor->handle.destsize_redzone_end = EROFS_CONFIG_COMPR_DEF_BOUNDARY;
+
+	if (alg && alg->init)
+		alg->init(&compressor->handle);
+}
+
+static void erofs_compressor_register(const char *name, const struct erofs_compressor *alg)
+{
+	int i;
+
+	for (i = 0; i < erofs_ccfg.erofs_ccfg_num; i++) {
+		if (!strcmp(erofs_ccfg.compressors[i].name, name)) {
+			erofs_init_compressor(&erofs_ccfg.compressors[i], alg);
+			return;
+		}
+	}
+
+	erofs_ccfg.compressors[i].name = name;
+	erofs_ccfg.compressors[i].algorithmtype = erofs_supported_algothrims[i].algtype;
+	erofs_init_compressor(&erofs_ccfg.compressors[i], alg);
+	erofs_ccfg.erofs_ccfg_num = ++i;
+}
+
+void erofs_register_compressors(void)
+{
+	int i;
+
+	erofs_ccfg.erofs_ccfg_num = 0;
+	for (i = 0; i < ARRAY_SIZE(erofs_supported_algothrims); i++) {
+		erofs_compressor_register(erofs_supported_algothrims[i].name, NULL);
+	}
+
+#if LZ4_ENABLED
+	erofs_compressor_register("lz4", &erofs_compressor_lz4);
+#if LZ4HC_ENABLED
+	erofs_compressor_register("lz4hc", &erofs_compressor_lz4hc);
+#endif
+#endif
+#if HAVE_LIBLZMA
+	erofs_compressor_register("lzma", &erofs_compressor_lzma);
+#endif
+}
+
 int erofs_compress_destsize(const struct erofs_compress *c,
 			    const void *src, unsigned int *srcsize,
 			    void *dst, unsigned int dstsize, bool inblocks)
diff --git a/lib/compressor.h b/lib/compressor.h
index cf063f1..1e760b6 100644
--- a/lib/compressor.h
+++ b/lib/compressor.h
@@ -8,6 +8,7 @@
 #define __EROFS_LIB_COMPRESSOR_H
 
 #include "erofs/defs.h"
+#include "erofs/config.h"
 
 struct erofs_compress;
 
@@ -40,6 +41,18 @@ struct erofs_compress {
 	void *private_data;
 };
 
+struct compressor {
+	const char *name;
+	struct erofs_compress handle;
+	unsigned int algorithmtype;
+	bool enable;
+};
+
+struct erofs_compressors_cfg {
+	struct compressor compressors[EROFS_MAX_COMPR_CFGS];
+	int erofs_ccfg_num;
+};
+
 /* list of compression algorithms */
 extern const struct erofs_compressor erofs_compressor_lz4;
 extern const struct erofs_compressor erofs_compressor_lz4hc;
@@ -52,5 +65,6 @@ int erofs_compress_destsize(const struct erofs_compress *c,
 int erofs_compressor_setlevel(struct erofs_compress *c, int compression_level);
 int erofs_compressor_init(struct erofs_compress *c, char *alg_name);
 int erofs_compressor_exit(struct erofs_compress *c);
+void erofs_register_compressors(void);
 
 #endif
-- 
2.31.1



More information about the Linux-erofs mailing list