From 05784150b15f9903b4ffaa1a7d9243a717785943 Mon Sep 17 00:00:00 2001 From: Ilya Leoshkevich Date: Tue, 21 May 2024 11:48:36 +0200 Subject: [PATCH] Adapt DFLTCC code to the new memory management --- CMakeLists.txt | 3 --- arch/s390/Makefile.in | 6 ------ arch/s390/README.md | 7 ++++--- arch/s390/dfltcc_common.c | 40 -------------------------------------- arch/s390/dfltcc_common.h | 19 ++++++++---------- arch/s390/dfltcc_deflate.h | 2 ++ arch/s390/dfltcc_detail.h | 2 -- arch/s390/dfltcc_inflate.h | 2 ++ configure | 5 ----- deflate.c | 8 +++++--- inflate.c | 6 +++--- inflate_p.h | 4 ++-- 12 files changed, 26 insertions(+), 78 deletions(-) delete mode 100644 arch/s390/dfltcc_common.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f236ce860c..b08e05861a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -851,9 +851,6 @@ if(WITH_OPTIM) list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/s390_features.c) endif() endif() - if(WITH_DFLTCC_DEFLATE OR WITH_DFLTCC_INFLATE) - list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_common.c) - endif() if(WITH_DFLTCC_DEFLATE) add_definitions(-DS390_DFLTCC_DEFLATE) list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_deflate.c) diff --git a/arch/s390/Makefile.in b/arch/s390/Makefile.in index 6b4fba7775..e994157df2 100644 --- a/arch/s390/Makefile.in +++ b/arch/s390/Makefile.in @@ -20,12 +20,6 @@ s390_features.o: s390_features.lo: $(CC) $(SFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/s390_features.c -dfltcc_common.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/dfltcc_common.c - -dfltcc_common.lo: - $(CC) $(SFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/dfltcc_common.c - dfltcc_deflate.o: $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/dfltcc_deflate.c diff --git a/arch/s390/README.md b/arch/s390/README.md index 0f3329108d..57e9ee8f14 100644 --- a/arch/s390/README.md +++ b/arch/s390/README.md @@ -62,9 +62,10 @@ integrated with the rest of zlib-ng using hook macros. DFLTCC takes as arguments a parameter block, an input buffer, an output buffer, and a window. Parameter blocks are stored alongside zlib states; -buffers are forwarded from the caller; and window (which must be page-aligned) -is managed using `ZALLOC_WINDOW()`, `ZCOPY_WINDOW()` and `TRY_FREE_WINDOW()` -macros. +buffers are forwarded from the caller; and window - which must be +4k-aligned and is always 64k large, is managed using the `PAD_WINDOW()`, +`WINDOW_PAD_SIZE`, `HINT_ALIGNED_WINDOW` and `DEFLATE_ADJUST_WINDOW_SIZE()` +and `INFLATE_ADJUST_WINDOW_SIZE()` hooks. Software and hardware window formats do not match, therefore, `deflateSetDictionary()`, `deflateGetDictionary()`, `inflateSetDictionary()` diff --git a/arch/s390/dfltcc_common.c b/arch/s390/dfltcc_common.c deleted file mode 100644 index 78be718114..0000000000 --- a/arch/s390/dfltcc_common.c +++ /dev/null @@ -1,40 +0,0 @@ -/* dfltcc_deflate.c - IBM Z DEFLATE CONVERSION CALL general support. */ - -#include "zbuild.h" -#include "dfltcc_common.h" -#include "dfltcc_detail.h" - -/* - Memory management. - - DFLTCC requires parameter blocks and window to be aligned. zlib-ng allows - users to specify their own allocation functions, so using e.g. - `posix_memalign' is not an option. Thus, we overallocate and take the - aligned portion of the buffer. -*/ - -static const int PAGE_ALIGN = 0x1000; - -void Z_INTERNAL *PREFIX(dfltcc_alloc_window)(PREFIX3(streamp) strm, uInt items, uInt size) { - void *p; - void *w; - - /* To simplify freeing, we store the pointer to the allocated buffer right - * before the window. Note that DFLTCC always uses HB_SIZE bytes. - */ - p = ZALLOC(strm, sizeof(void *) + MAX(items * size, HB_SIZE) + PAGE_ALIGN, sizeof(unsigned char)); - if (p == NULL) - return NULL; - w = ALIGN_UP((char *)p + sizeof(void *), PAGE_ALIGN); - *(void **)((char *)w - sizeof(void *)) = p; - return w; -} - -void Z_INTERNAL PREFIX(dfltcc_copy_window)(void *dest, const void *src, size_t n) { - memcpy(dest, src, MAX(n, HB_SIZE)); -} - -void Z_INTERNAL PREFIX(dfltcc_free_window)(PREFIX3(streamp) strm, void *w) { - if (w) - ZFREE(strm, *(void **)((unsigned char *)w - sizeof(void *))); -} diff --git a/arch/s390/dfltcc_common.h b/arch/s390/dfltcc_common.h index 5acef291f8..a6527ab5df 100644 --- a/arch/s390/dfltcc_common.h +++ b/arch/s390/dfltcc_common.h @@ -83,18 +83,15 @@ typedef struct { struct dfltcc_state common; } arch_inflate_state; -void Z_INTERNAL *PREFIX(dfltcc_alloc_window)(PREFIX3(streamp) strm, uInt items, uInt size); -void Z_INTERNAL PREFIX(dfltcc_copy_window)(void *dest, const void *src, size_t n); -void Z_INTERNAL PREFIX(dfltcc_free_window)(PREFIX3(streamp) strm, void *w); - -#define ZALLOC_WINDOW PREFIX(dfltcc_alloc_window) - -#define ZCOPY_WINDOW PREFIX(dfltcc_copy_window) - -#define ZFREE_WINDOW PREFIX(dfltcc_free_window) - -#define TRY_FREE_WINDOW PREFIX(dfltcc_free_window) +/* + History buffer size. + */ +#define HB_BITS 15 +#define HB_SIZE (1 << HB_BITS) +/* + Sizes of deflate block parts. + */ #define DFLTCC_BLOCK_HEADER_BITS 3 #define DFLTCC_HLITS_COUNT_BITS 5 #define DFLTCC_HDISTS_COUNT_BITS 5 diff --git a/arch/s390/dfltcc_deflate.h b/arch/s390/dfltcc_deflate.h index 02c6714203..35e2fd3f62 100644 --- a/arch/s390/dfltcc_deflate.h +++ b/arch/s390/dfltcc_deflate.h @@ -53,4 +53,6 @@ int Z_INTERNAL PREFIX(dfltcc_deflate_get_dictionary)(PREFIX3(streamp) strm, unsi #define DEFLATE_CAN_SET_REPRODUCIBLE PREFIX(dfltcc_can_set_reproducible) +#define DEFLATE_ADJUST_WINDOW_SIZE(n) MAX(n, HB_SIZE) + #endif diff --git a/arch/s390/dfltcc_detail.h b/arch/s390/dfltcc_detail.h index 4977530699..ae6001ba38 100644 --- a/arch/s390/dfltcc_detail.h +++ b/arch/s390/dfltcc_detail.h @@ -100,8 +100,6 @@ typedef enum { #define DFLTCC_XPND 4 #define HBT_CIRCULAR (1 << 7) #define DFLTCC_FN_MASK ((1 << 7) - 1) -#define HB_BITS 15 -#define HB_SIZE (1 << HB_BITS) /* Return lengths of high (starting at param->ho) and low (starting at 0) fragments of the circular history buffer. */ static inline void get_history_lengths(struct dfltcc_param_v0 *param, size_t *hl_high, size_t *hl_low) { diff --git a/arch/s390/dfltcc_inflate.h b/arch/s390/dfltcc_inflate.h index 8fcab1d77c..3623f8ed7f 100644 --- a/arch/s390/dfltcc_inflate.h +++ b/arch/s390/dfltcc_inflate.h @@ -62,4 +62,6 @@ int Z_INTERNAL PREFIX(dfltcc_inflate_get_dictionary)(PREFIX3(streamp) strm, return PREFIX(dfltcc_inflate_get_dictionary)((strm), (dict), (dict_len)); \ } while (0) +#define INFLATE_ADJUST_WINDOW_SIZE(n) MAX(n, HB_SIZE) + #endif diff --git a/configure b/configure index 4efe9cbd87..6cf61da20f 100755 --- a/configure +++ b/configure @@ -1832,11 +1832,6 @@ EOF ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} s390_features.lo" fi - if test $builddfltccdeflate -eq 1 -o $builddfltccinflate -eq 1; then - ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} dfltcc_common.o" - ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} dfltcc_common.lo" - fi - if test $builddfltccdeflate -eq 1; then CFLAGS="${CFLAGS} -DS390_DFLTCC_DEFLATE" SFLAGS="${SFLAGS} -DS390_DFLTCC_DEFLATE" diff --git a/deflate.c b/deflate.c index 57a042a557..93786419b5 100644 --- a/deflate.c +++ b/deflate.c @@ -101,6 +101,8 @@ const char PREFIX(deflate_copyright)[] = " deflate 1.3.1 Copyright 1995-2024 Jea # define DEFLATE_NEED_CHECKSUM(strm) 1 /* Returns whether reproducibility parameter can be set to a given value. */ # define DEFLATE_CAN_SET_REPRODUCIBLE(strm, reproducible) 1 +/* Adjust the window size for the arch-specific deflate code. */ +# define DEFLATE_ADJUST_WINDOW_SIZE(n) (n) #endif /* =========================================================================== @@ -201,7 +203,7 @@ Z_INTERNAL deflate_allocs* alloc_deflate(PREFIX3(stream) *strm, int windowBits, int curr_size = 0; /* Define sizes */ - int window_size = (1 << windowBits) * 2; + int window_size = DEFLATE_ADJUST_WINDOW_SIZE((1 << windowBits) * 2); int prev_size = (1 << windowBits) * sizeof(Pos); int head_size = HASH_SIZE * sizeof(Pos); int pending_size = lit_bufsize * LIT_BUFS; @@ -209,7 +211,7 @@ Z_INTERNAL deflate_allocs* alloc_deflate(PREFIX3(stream) *strm, int windowBits, int alloc_size = sizeof(deflate_allocs); /* Calculate relative buffer positions and paddings */ - LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size,WINDOW_PAD_SIZE)); + LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size, WINDOW_PAD_SIZE)); int window_pos = PAD_WINDOW(curr_size); curr_size = window_pos + window_size; @@ -1158,7 +1160,7 @@ int32_t Z_EXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *sou return Z_MEM_ERROR; } - memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(unsigned char)); + memcpy(ds->window, ss->window, DEFLATE_ADJUST_WINDOW_SIZE(ds->w_size * 2 * sizeof(unsigned char))); memcpy((void *)ds->prev, (void *)ss->prev, ds->w_size * sizeof(Pos)); memcpy((void *)ds->head, (void *)ss->head, HASH_SIZE * sizeof(Pos)); memcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS); diff --git a/inflate.c b/inflate.c index 2d1a42d5be..5eb0bbf217 100644 --- a/inflate.c +++ b/inflate.c @@ -149,12 +149,12 @@ Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm) { int curr_size = 0; /* Define sizes */ - int window_size = (1 << MAX_WBITS) + 64; /* 64B padding for chunksize */ + int window_size = INFLATE_ADJUST_WINDOW_SIZE((1 << MAX_WBITS) + 64); /* 64B padding for chunksize */ int state_size = sizeof(inflate_state); int alloc_size = sizeof(inflate_allocs); /* Calculate relative buffer positions and paddings */ - LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size,WINDOW_PAD_SIZE)); + LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size, WINDOW_PAD_SIZE)); int window_pos = PAD_WINDOW(curr_size); curr_size = window_pos + window_size; @@ -1418,7 +1418,7 @@ int32_t Z_EXPORT PREFIX(inflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *sou copy->window = alloc_bufs->window; /* window */ - ZCOPY_WINDOW(copy->window, state->window, (size_t)state->wsize); + memcpy(copy->window, state->window, INFLATE_ADJUST_WINDOW_SIZE((size_t)state->wsize)); dest->state = (struct internal_state *)copy; return Z_OK; diff --git a/inflate_p.h b/inflate_p.h index 2b5a2698fe..6d2f916490 100644 --- a/inflate_p.h +++ b/inflate_p.h @@ -18,8 +18,6 @@ # define PAD_WINDOW PAD_64 # define WINDOW_PAD_SIZE 64 # define HINT_ALIGNED_WINDOW HINT_ALIGNED_64 -/* Memory management for the window. Useful for allocation the aligned window. */ -# define ZCOPY_WINDOW(dest, src, n) memcpy(dest, src, n) /* Invoked at the end of inflateResetKeep(). Useful for initializing arch-specific extension blocks. */ # define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0) /* Invoked at the beginning of inflatePrime(). Useful for updating arch-specific buffers. */ @@ -38,6 +36,8 @@ # define INFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0) /* Invoked at the beginning of inflateGetDictionary(). Useful for adjusting arch-specific window data. */ # define INFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0) +/* Adjust the window size for the arch-specific inflate code. */ +# define INFLATE_ADJUST_WINDOW_SIZE(n) (n) #endif /*