From bd58a90f015548e118ad3b8661aa1d7fe9bfb46b Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 18 Apr 2021 17:18:30 +0300 Subject: [PATCH 1/3] Use constructors for functable --- functable.c | 84 ++++++++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 55 deletions(-) diff --git a/functable.c b/functable.c index c3c3707ee..faaf29ad9 100644 --- a/functable.c +++ b/functable.c @@ -134,10 +134,25 @@ extern uint32_t longest_match_unaligned_avx2(deflate_state *const s, Pos cur_mat #endif #endif -Z_INTERNAL Z_TLS struct functable_s functable; +/* functable init */ +Z_INTERNAL Z_TLS struct functable_s functable = { + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL +}; /* stub functions */ -Z_INTERNAL void insert_string_stub(deflate_state *const s, const uint32_t str, uint32_t count) { +static void __attribute__((constructor)) insert_string_stub() { // Initialize default functable.insert_string = &insert_string_c; @@ -149,11 +164,9 @@ Z_INTERNAL void insert_string_stub(deflate_state *const s, const uint32_t str, u if (arm_cpu_has_crc32) functable.insert_string = &insert_string_acle; #endif - - functable.insert_string(s, str, count); } -Z_INTERNAL Pos quick_insert_string_stub(deflate_state *const s, const uint32_t str) { +static Pos __attribute__((constructor)) quick_insert_string_stub_init() { functable.quick_insert_string = &quick_insert_string_c; #ifdef X86_SSE42_CRC_HASH @@ -163,11 +176,9 @@ Z_INTERNAL Pos quick_insert_string_stub(deflate_state *const s, const uint32_t s if (arm_cpu_has_crc32) functable.quick_insert_string = &quick_insert_string_acle; #endif - - return functable.quick_insert_string(s, str); } -Z_INTERNAL void slide_hash_stub(deflate_state *s) { +static void __attribute__((constructor)) slide_hash_stub_init() { functable.slide_hash = &slide_hash_c; @@ -190,11 +201,9 @@ Z_INTERNAL void slide_hash_stub(deflate_state *s) { if (power_cpu_has_arch_2_07) functable.slide_hash = &slide_hash_power8; #endif - - functable.slide_hash(s); } -Z_INTERNAL uint32_t adler32_stub(uint32_t adler, const unsigned char *buf, size_t len) { +static uint32_t __attribute__((constructor)) adler32_stub_init() { // Initialize default functable.adler32 = &adler32_c; @@ -216,11 +225,9 @@ Z_INTERNAL uint32_t adler32_stub(uint32_t adler, const unsigned char *buf, size_ if (power_cpu_has_arch_2_07) functable.adler32 = &adler32_power8; #endif - - return functable.adler32(adler, buf, len); } -Z_INTERNAL uint32_t chunksize_stub(void) { +static uint32_t __attribute__((constructor)) chunksize_stub_init(void) { // Initialize default functable.chunksize = &chunksize_c; @@ -242,7 +249,7 @@ Z_INTERNAL uint32_t chunksize_stub(void) { return functable.chunksize(); } -Z_INTERNAL uint8_t* chunkcopy_stub(uint8_t *out, uint8_t const *from, unsigned len) { +static uint8_t* __attribute__((constructor)) chunkcopy_stub_init() { // Initialize default functable.chunkcopy = &chunkcopy_c; @@ -260,11 +267,9 @@ Z_INTERNAL uint8_t* chunkcopy_stub(uint8_t *out, uint8_t const *from, unsigned l if (arm_cpu_has_neon) functable.chunkcopy = &chunkcopy_neon; #endif - - return functable.chunkcopy(out, from, len); } -Z_INTERNAL uint8_t* chunkcopy_safe_stub(uint8_t *out, uint8_t const *from, unsigned len, uint8_t *safe) { +static uint8_t* __attribute__((constructor)) chunkcopy_safe_stub_init() { // Initialize default functable.chunkcopy_safe = &chunkcopy_safe_c; @@ -282,11 +287,9 @@ Z_INTERNAL uint8_t* chunkcopy_safe_stub(uint8_t *out, uint8_t const *from, unsig if (arm_cpu_has_neon) functable.chunkcopy_safe = &chunkcopy_safe_neon; #endif - - return functable.chunkcopy_safe(out, from, len, safe); } -Z_INTERNAL uint8_t* chunkunroll_stub(uint8_t *out, unsigned *dist, unsigned *len) { +static uint8_t* __attribute__((constructor)) chunkunroll_stub_init() { // Initialize default functable.chunkunroll = &chunkunroll_c; @@ -305,11 +308,9 @@ Z_INTERNAL uint8_t* chunkunroll_stub(uint8_t *out, unsigned *dist, unsigned *len if (arm_cpu_has_neon) functable.chunkunroll = &chunkunroll_neon; #endif - - return functable.chunkunroll(out, dist, len); } -Z_INTERNAL uint8_t* chunkmemset_stub(uint8_t *out, unsigned dist, unsigned len) { +static uint8_t* __attribute__((constructor)) chunkmemset_stub_init() { // Initialize default functable.chunkmemset = &chunkmemset_c; @@ -327,11 +328,9 @@ Z_INTERNAL uint8_t* chunkmemset_stub(uint8_t *out, unsigned dist, unsigned len) if (arm_cpu_has_neon) functable.chunkmemset = &chunkmemset_neon; #endif - - return functable.chunkmemset(out, dist, len); } -Z_INTERNAL uint8_t* chunkmemset_safe_stub(uint8_t *out, unsigned dist, unsigned len, unsigned left) { +static uint8_t* __attribute__((constructor)) chunkmemset_safe_stub_init() { // Initialize default functable.chunkmemset_safe = &chunkmemset_safe_c; @@ -349,11 +348,9 @@ Z_INTERNAL uint8_t* chunkmemset_safe_stub(uint8_t *out, unsigned dist, unsigned if (arm_cpu_has_neon) functable.chunkmemset_safe = &chunkmemset_safe_neon; #endif - - return functable.chunkmemset_safe(out, dist, len, left); } -Z_INTERNAL uint32_t crc32_stub(uint32_t crc, const unsigned char *buf, uint64_t len) { +static uint32_t __attribute__((constructor)) crc32_stub_init() { int32_t use_byfour = sizeof(void *) == sizeof(ptrdiff_t); Assert(sizeof(uint64_t) >= sizeof(size_t), @@ -375,11 +372,9 @@ Z_INTERNAL uint32_t crc32_stub(uint32_t crc, const unsigned char *buf, uint64_t } else { functable.crc32 = crc32_generic; } - - return functable.crc32(crc, buf, len); } -Z_INTERNAL uint32_t compare258_stub(const unsigned char *src0, const unsigned char *src1) { +static uint32_t __attribute__((constructor)) compare258_stub_init() { functable.compare258 = &compare258_c; @@ -400,11 +395,9 @@ Z_INTERNAL uint32_t compare258_stub(const unsigned char *src0, const unsigned ch functable.compare258 = &compare258_unaligned_avx2; # endif #endif - - return functable.compare258(src0, src1); } -Z_INTERNAL uint32_t longest_match_stub(deflate_state *const s, Pos cur_match) { +static uint32_t __attribute__((constructor)) longest_match_stub_init() { functable.longest_match = &longest_match_c; @@ -425,23 +418,4 @@ Z_INTERNAL uint32_t longest_match_stub(deflate_state *const s, Pos cur_match) { functable.longest_match = &longest_match_unaligned_avx2; # endif #endif - - return functable.longest_match(s, cur_match); } - -/* functable init */ -Z_INTERNAL Z_TLS struct functable_s functable = { - insert_string_stub, - quick_insert_string_stub, - adler32_stub, - crc32_stub, - slide_hash_stub, - compare258_stub, - longest_match_stub, - chunksize_stub, - chunkcopy_stub, - chunkcopy_safe_stub, - chunkunroll_stub, - chunkmemset_stub, - chunkmemset_safe_stub -}; From c2be108ce15fc0ac1628cdf758e35e9acfe44e11 Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 18 Apr 2021 17:24:19 +0300 Subject: [PATCH 2/3] Fixed code review issues --- functable.c | 40 ++++++++++++---------------------------- functable.h | 2 +- zutil.h | 4 ---- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/functable.c b/functable.c index faaf29ad9..b1f0326f2 100644 --- a/functable.c +++ b/functable.c @@ -135,21 +135,7 @@ extern uint32_t longest_match_unaligned_avx2(deflate_state *const s, Pos cur_mat #endif /* functable init */ -Z_INTERNAL Z_TLS struct functable_s functable = { - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL -}; +Z_INTERNAL struct functable_s functable = {}; /* stub functions */ static void __attribute__((constructor)) insert_string_stub() { @@ -166,7 +152,7 @@ static void __attribute__((constructor)) insert_string_stub() { #endif } -static Pos __attribute__((constructor)) quick_insert_string_stub_init() { +static void __attribute__((constructor)) quick_insert_string_stub_init() { functable.quick_insert_string = &quick_insert_string_c; #ifdef X86_SSE42_CRC_HASH @@ -203,7 +189,7 @@ static void __attribute__((constructor)) slide_hash_stub_init() { #endif } -static uint32_t __attribute__((constructor)) adler32_stub_init() { +static void __attribute__((constructor)) adler32_stub_init() { // Initialize default functable.adler32 = &adler32_c; @@ -227,7 +213,7 @@ static uint32_t __attribute__((constructor)) adler32_stub_init() { #endif } -static uint32_t __attribute__((constructor)) chunksize_stub_init(void) { +static void __attribute__((constructor)) chunksize_stub_init(void) { // Initialize default functable.chunksize = &chunksize_c; @@ -245,11 +231,9 @@ static uint32_t __attribute__((constructor)) chunksize_stub_init(void) { if (arm_cpu_has_neon) functable.chunksize = &chunksize_neon; #endif - - return functable.chunksize(); } -static uint8_t* __attribute__((constructor)) chunkcopy_stub_init() { +static void __attribute__((constructor)) chunkcopy_stub_init() { // Initialize default functable.chunkcopy = &chunkcopy_c; @@ -269,7 +253,7 @@ static uint8_t* __attribute__((constructor)) chunkcopy_stub_init() { #endif } -static uint8_t* __attribute__((constructor)) chunkcopy_safe_stub_init() { +static void __attribute__((constructor)) chunkcopy_safe_stub_init() { // Initialize default functable.chunkcopy_safe = &chunkcopy_safe_c; @@ -289,7 +273,7 @@ static uint8_t* __attribute__((constructor)) chunkcopy_safe_stub_init() { #endif } -static uint8_t* __attribute__((constructor)) chunkunroll_stub_init() { +static void __attribute__((constructor)) chunkunroll_stub_init() { // Initialize default functable.chunkunroll = &chunkunroll_c; @@ -310,7 +294,7 @@ static uint8_t* __attribute__((constructor)) chunkunroll_stub_init() { #endif } -static uint8_t* __attribute__((constructor)) chunkmemset_stub_init() { +static void __attribute__((constructor)) chunkmemset_stub_init() { // Initialize default functable.chunkmemset = &chunkmemset_c; @@ -330,7 +314,7 @@ static uint8_t* __attribute__((constructor)) chunkmemset_stub_init() { #endif } -static uint8_t* __attribute__((constructor)) chunkmemset_safe_stub_init() { +static void __attribute__((constructor)) chunkmemset_safe_stub_init() { // Initialize default functable.chunkmemset_safe = &chunkmemset_safe_c; @@ -350,7 +334,7 @@ static uint8_t* __attribute__((constructor)) chunkmemset_safe_stub_init() { #endif } -static uint32_t __attribute__((constructor)) crc32_stub_init() { +static void __attribute__((constructor)) crc32_stub_init() { int32_t use_byfour = sizeof(void *) == sizeof(ptrdiff_t); Assert(sizeof(uint64_t) >= sizeof(size_t), @@ -374,7 +358,7 @@ static uint32_t __attribute__((constructor)) crc32_stub_init() { } } -static uint32_t __attribute__((constructor)) compare258_stub_init() { +static void __attribute__((constructor)) compare258_stub_init() { functable.compare258 = &compare258_c; @@ -397,7 +381,7 @@ static uint32_t __attribute__((constructor)) compare258_stub_init() { #endif } -static uint32_t __attribute__((constructor)) longest_match_stub_init() { +static void __attribute__((constructor)) longest_match_stub_init() { functable.longest_match = &longest_match_c; diff --git a/functable.h b/functable.h index 276c284a0..efcad1d57 100644 --- a/functable.h +++ b/functable.h @@ -24,6 +24,6 @@ struct functable_s { uint8_t* (* chunkmemset_safe) (uint8_t *out, unsigned dist, unsigned len, unsigned left); }; -Z_INTERNAL extern Z_TLS struct functable_s functable; +Z_INTERNAL extern struct functable_s functable; #endif diff --git a/zutil.h b/zutil.h index 497cb22b4..64a7e25a0 100644 --- a/zutil.h +++ b/zutil.h @@ -24,10 +24,6 @@ # define Z_REGISTER #endif -#ifndef Z_TLS -# define Z_TLS -#endif - #include #include #include From d03dce9e021996c3fec8866c4c4a136b05f5388a Mon Sep 17 00:00:00 2001 From: Maksim Kita Date: Sun, 18 Apr 2021 17:27:13 +0300 Subject: [PATCH 3/3] Remove explicit initialization --- functable.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functable.c b/functable.c index b1f0326f2..e068a5e55 100644 --- a/functable.c +++ b/functable.c @@ -135,7 +135,7 @@ extern uint32_t longest_match_unaligned_avx2(deflate_state *const s, Pos cur_mat #endif /* functable init */ -Z_INTERNAL struct functable_s functable = {}; +Z_INTERNAL struct functable_s functable; /* stub functions */ static void __attribute__((constructor)) insert_string_stub() {