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

Guo Xuenan guoxuenan at huawei.com
Fri Jun 9 18:50:38 AEST 2023


refactor compressor code using constructor.

Signed-off-by: Guo Xuenan <guoxuenan at huawei.com>
---
 lib/compressor.c         | 49 ++++++++++++++++++++++++++++++++++++++++
 lib/compressor.h         | 15 ++++++++++++
 lib/compressor_liblzma.c |  5 ++++
 lib/compressor_lz4.c     |  5 ++++
 lib/compressor_lz4hc.c   |  5 ++++
 5 files changed, 79 insertions(+)

diff --git a/lib/compressor.c b/lib/compressor.c
index 52eb761..0fa7105 100644
--- a/lib/compressor.c
+++ b/lib/compressor.c
@@ -22,6 +22,40 @@ static const struct erofs_compressor *compressors[] = {
 #endif
 };
 
+/* compressing configuration specified by users */
+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"},
+};
+
+static struct erofs_compressors_cfg erofs_ccfg;
+
+int erofs_compressor_num(void)
+{
+	return erofs_ccfg.erofs_ccfg_num;
+}
+
+void erofs_compressor_register(const char *name, const struct erofs_compressor *alg)
+{
+	int i;
+
+	for (i = 0; i < erofs_compressor_num(); i++) {
+		if (!strcmp(erofs_ccfg.compressors[i].name, name)) {
+			erofs_ccfg.compressors[i].handle.alg = alg;
+			return;
+		}
+	}
+
+	erofs_ccfg.compressors[i].name = name;
+	erofs_ccfg.compressors[i].handle.alg = alg;
+	erofs_ccfg.compressors[i].algorithmtype = erofs_supported_algothrims[i].algtype;
+	erofs_ccfg.erofs_ccfg_num = ++i;
+}
+
 int erofs_compress_destsize(const struct erofs_compress *c,
 			    const void *src, unsigned int *srcsize,
 			    void *dst, unsigned int dstsize, bool inblocks)
@@ -106,3 +140,18 @@ int erofs_compressor_exit(struct erofs_compress *c)
 		return c->alg->exit(c);
 	return 0;
 }
+
+void __attribute__((constructor(101))) __erofs_compressor_init(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);
+	}
+}
+
+void __attribute__((destructor)) __erofs_compressor_exit(void)
+{
+	erofs_err("__erofs_compressor_exit");
+}
diff --git a/lib/compressor.h b/lib/compressor.h
index cf063f1..3c1b328 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;
@@ -53,4 +66,6 @@ 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_compressor_register(const char *name, const struct erofs_compressor *alg);
+int erofs_compressor_num(void);
 #endif
diff --git a/lib/compressor_liblzma.c b/lib/compressor_liblzma.c
index f274dce..b24139c 100644
--- a/lib/compressor_liblzma.c
+++ b/lib/compressor_liblzma.c
@@ -108,4 +108,9 @@ const struct erofs_compressor erofs_compressor_lzma = {
 	.setlevel = erofs_compressor_liblzma_setlevel,
 	.compress_destsize = erofs_liblzma_compress_destsize,
 };
+
+static void __attribute__((constructor)) register_lzma_compressor(void)
+{
+	erofs_compressor_register("lzma", &erofs_compressor_lzma);
+}
 #endif
diff --git a/lib/compressor_lz4.c b/lib/compressor_lz4.c
index b6f6e7e..a8adffe 100644
--- a/lib/compressor_lz4.c
+++ b/lib/compressor_lz4.c
@@ -45,3 +45,8 @@ const struct erofs_compressor erofs_compressor_lz4 = {
 	.exit = compressor_lz4_exit,
 	.compress_destsize = lz4_compress_destsize,
 };
+
+static void __attribute__((constructor)) register_lz4_compressor(void)
+{
+	erofs_compressor_register("lz4", &erofs_compressor_lz4);
+}
diff --git a/lib/compressor_lz4hc.c b/lib/compressor_lz4hc.c
index eec1c84..2b7e900 100644
--- a/lib/compressor_lz4hc.c
+++ b/lib/compressor_lz4hc.c
@@ -68,3 +68,8 @@ const struct erofs_compressor erofs_compressor_lz4hc = {
 	.setlevel = compressor_lz4hc_setlevel,
 	.compress_destsize = lz4hc_compress_destsize,
 };
+
+static void __attribute__((constructor)) register_lz4hc_compressor(void)
+{
+	erofs_compressor_register("lz4hc", &erofs_compressor_lz4hc);
+}
-- 
2.31.1



More information about the Linux-erofs mailing list