]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Lazily initialize functable members. (#108)
authorMika Lindqvist <postmaster@raasu.org>
Wed, 3 May 2017 17:14:57 +0000 (20:14 +0300)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 3 May 2017 17:14:57 +0000 (19:14 +0200)
- Split functableInit() function as separate functions for each functable member, so we don't need to initialize full functable in multiple places in the zlib-ng code, or to check for NULL on every invocation.
- Optimized function for each functable member is detected on first invocation and the functable item is updated for subsequent invocations.
- Remove NULL check in adler32() and adler32_z() as it is no longer needed.

adler32.c
deflate.c
functable.c
functable.h
inflate.c

index dcfeecdf29fbdaba6d4566165cf0ded641b45c40..f84c73cd1101ac398a83c5924a05e679a552f437 100644 (file)
--- a/adler32.c
+++ b/adler32.c
@@ -145,15 +145,11 @@ uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len) {
 }
 
 uint32_t ZEXPORT adler32_z(uint32_t adler, const unsigned char *buf, size_t len) {
-    if (functable.adler32 == NULL)
-        functableInit();
     return functable.adler32(adler, buf, len);
 }
 
 /* ========================================================================= */
 uint32_t ZEXPORT adler32(uint32_t adler, const unsigned char *buf, uint32_t len) {
-    if (functable.adler32 == NULL)
-        functableInit();
     return functable.adler32(adler, buf, len);
 }
 
index 7de68bbde0ede9b88037eb41f42f7ca0608fd5fe..ab5b69714218101704f51dba7d63330f185789bb 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -241,8 +241,6 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits,
     x86_check_features();
 #endif
 
-    functableInit();
-
     if (version == NULL || version[0] != my_version[0] || stream_size != sizeof(z_stream)) {
         return Z_VERSION_ERROR;
     }
index 7d34fad74fbf779e67441053896da0787526a9e9..5ebd3559d1f6d17215a5efd3a0e0d4aa12628d4f 100644 (file)
 #endif
 
 
+/* insert_string */
+ZLIB_INTERNAL Pos insert_string_stub(deflate_state *const s, const Pos str, unsigned int count);
 #ifdef X86_SSE4_2_CRC_HASH
 extern Pos insert_string_sse(deflate_state *const s, const Pos str, unsigned int count);
 #elif defined(ARM_ACLE_CRC_HASH)
 extern Pos insert_string_acle(deflate_state *const s, const Pos str, unsigned int count);
 #endif
 
+/* fill_window */
+ZLIB_INTERNAL void fill_window_stub(deflate_state *s);
 #ifdef X86_SSE2_FILL_WINDOW
 extern void fill_window_sse(deflate_state *s);
 #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
 extern void fill_window_arm(deflate_state *s);
 #endif
 
+/* adler32 */
+ZLIB_INTERNAL uint32_t adler32_stub(uint32_t adler, const unsigned char *buf, size_t len);
 #if (defined(__ARM_NEON__) || defined(__ARM_NEON))
 extern uint32_t adler32_neon(uint32_t adler, const unsigned char *buf, size_t len);
 #endif
 
 
-/* =========================================================================
- * Initialize functable
- */
-
-struct functable_s functable = {NULL,NULL,NULL};
+struct functable_s functable = {fill_window_stub,insert_string_stub,adler32_stub};
 
-
-ZLIB_INTERNAL void functableInit() {
-    // Initialize defaults
+/* stub functions */
+ZLIB_INTERNAL Pos insert_string_stub(deflate_state *const s, const Pos str, unsigned int count) {
+    // Initialize default
     functable.insert_string=&insert_string_c;
-    functable.fill_window=&fill_window_c;
-    functable.adler32=&adler32_c;
 
-    // insert_string
     #ifdef X86_SSE4_2_CRC_HASH
     if (x86_cpu_has_sse42)
         functable.insert_string=&insert_string_sse;
@@ -50,7 +49,13 @@ ZLIB_INTERNAL void functableInit() {
         functable.insert_string=&insert_string_acle;
     #endif
 
-    // fill_window
+    return functable.insert_string(s, str, count);
+}
+
+ZLIB_INTERNAL void fill_window_stub(deflate_state *s) {
+    // Initialize default
+    functable.fill_window=&fill_window_c;
+
     #ifdef X86_SSE2_FILL_WINDOW
     # ifndef X86_NOCHECK_SSE2
     if (x86_cpu_has_sse2)
@@ -60,8 +65,16 @@ ZLIB_INTERNAL void functableInit() {
         functable.fill_window=&fill_window_arm;
     #endif
 
-    // adler32
+    functable.fill_window(s);
+}
+
+ZLIB_INTERNAL uint32_t adler32_stub(uint32_t adler, const unsigned char *buf, size_t len) {
+    // Initialize default
+    functable.adler32=&adler32_c;
+
     #if (defined(__ARM_NEON__) || defined(__ARM_NEON))
         functable.adler32=&adler32_neon;
     #endif
+
+    return functable.adler32(adler, buf, len);
 }
index b7d026e5671c0888f86cf99b5dbfedaa71fa477f..3b01f7cb6257fe4348789e8b0a9bdd33e7640270 100644 (file)
@@ -10,8 +10,6 @@
 
 uint32_t adler32_c(uint32_t adler, const unsigned char *buf, size_t len);
 
-void functableInit();
-
 struct functable_s {
     void     (* fill_window)    (deflate_state *s);
     Pos      (* insert_string)  (deflate_state *const s, const Pos str, unsigned int count);
index 68a41acf460a1455e5fab92587a6d75252f4a274..1b5a554801ec31257e289bb0fb0a4450d67d7064 100644 (file)
--- a/inflate.c
+++ b/inflate.c
@@ -187,8 +187,6 @@ int ZEXPORT inflateInit2_(z_stream *strm, int windowBits, const char *version, i
     int ret;
     struct inflate_state *state;
 
-    functableInit();
-
     if (version == NULL || version[0] != ZLIB_VERSION[0] || stream_size != (int)(sizeof(z_stream)))
         return Z_VERSION_ERROR;
     if (strm == NULL)