## Hook macros
DFLTCC takes as arguments a parameter block, an input buffer, an output
-buffer and a window. `ZALLOC_STATE()`, `ZFREE_STATE()`, `ZCOPY_STATE()`,
+buffer and a window. `ZALLOC_DEFLATE_STATE()`, `ZALLOC_INFLATE_STATE()`,
+`ZFREE_STATE()`, `ZCOPY_DEFLATE_STATE()`, `ZCOPY_INFLATE_STATE()`,
`ZALLOC_WINDOW()` and `TRY_FREE_WINDOW()` macros encapsulate allocation
details for the parameter block (which is allocated alongside zlib-ng
state) and the window (which must be page-aligned).
`posix_memalign' is not an option. Thus, we overallocate and take the
aligned portion of the buffer.
*/
-static inline int is_dfltcc_enabled(void) {
- uint64_t facilities[(DFLTCC_FACILITY / 64) + 1];
- Z_REGISTER uint8_t r0 __asm__("r0");
-
- memset(facilities, 0, sizeof(facilities));
- r0 = sizeof(facilities) / sizeof(facilities[0]) - 1;
- /* STFLE is supported since z9-109 and only in z/Architecture mode. When
- * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
- * is 64-bit, it's always z/Architecture mode at runtime.
- */
- __asm__ volatile(
-#ifndef __clang__
- ".machinemode push\n"
- ".machinemode zarch\n"
-#endif
- "stfle %[facilities]\n"
-#ifndef __clang__
- ".machinemode pop\n"
-#endif
- : [facilities] "=Q" (facilities), [r0] "+r" (r0) :: "cc");
- return is_bit_set((const char *)facilities, DFLTCC_FACILITY);
-}
-
-void Z_INTERNAL PREFIX(dfltcc_reset)(PREFIX3(streamp) strm, uInt size) {
- struct dfltcc_state *dfltcc_state = (struct dfltcc_state *)((char *)strm->state + ALIGN_UP(size, 8));
- struct dfltcc_qaf_param *param = (struct dfltcc_qaf_param *)&dfltcc_state->param;
-
- /* Initialize available functions */
- if (is_dfltcc_enabled()) {
- dfltcc(DFLTCC_QAF, param, NULL, NULL, NULL, NULL, NULL);
- memmove(&dfltcc_state->af, param, sizeof(dfltcc_state->af));
- } else
- memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
-
- /* Initialize parameter block */
- memset(&dfltcc_state->param, 0, sizeof(dfltcc_state->param));
- dfltcc_state->param.nt = 1;
-
- /* Initialize tuning parameters */
- dfltcc_state->level_mask = DFLTCC_LEVEL_MASK;
- dfltcc_state->block_size = DFLTCC_BLOCK_SIZE;
- dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE;
- dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE;
- dfltcc_state->param.ribm = DFLTCC_RIBM;
-}
-
-void Z_INTERNAL *PREFIX(dfltcc_alloc_state)(PREFIX3(streamp) strm, uInt items, uInt size) {
- return ZALLOC(strm, ALIGN_UP(items * size, 8) + sizeof(struct dfltcc_state), sizeof(unsigned char));
-}
-
-void Z_INTERNAL PREFIX(dfltcc_copy_state)(void *dst, const void *src, uInt size) {
- memcpy(dst, src, ALIGN_UP(size, 8) + sizeof(struct dfltcc_state));
-}
static const int PAGE_ALIGN = 0x1000;
#include "zutil.h"
-void Z_INTERNAL *PREFIX(dfltcc_alloc_state)(PREFIX3(streamp) strm, uInt items, uInt size);
-void Z_INTERNAL PREFIX(dfltcc_copy_state)(void *dst, const void *src, uInt size);
-void Z_INTERNAL PREFIX(dfltcc_reset)(PREFIX3(streamp) strm, uInt size);
void Z_INTERNAL *PREFIX(dfltcc_alloc_window)(PREFIX3(streamp) strm, uInt items, uInt size);
void Z_INTERNAL PREFIX(dfltcc_free_window)(PREFIX3(streamp) strm, void *w);
-#define ZALLOC_STATE PREFIX(dfltcc_alloc_state)
-
#define ZFREE_STATE ZFREE
-#define ZCOPY_STATE PREFIX(dfltcc_copy_state)
-
#define ZALLOC_WINDOW PREFIX(dfltcc_alloc_window)
#define ZFREE_WINDOW PREFIX(dfltcc_free_window)
#include "dfltcc_deflate.h"
#include "dfltcc_detail.h"
+struct dfltcc_deflate_state {
+ struct dfltcc_state common;
+ uint16_t level_mask; /* Levels on which to use DFLTCC */
+ uint32_t block_size; /* New block each X bytes */
+ size_t block_threshold; /* New block after total_in > X */
+ uint32_t dht_threshold; /* New block only if avail_in >= X */
+};
+
+#define GET_DFLTCC_DEFLATE_STATE(state) ((struct dfltcc_deflate_state *)GET_DFLTCC_STATE(state))
+
+void Z_INTERNAL *PREFIX(dfltcc_alloc_deflate_state)(PREFIX3(streamp) strm) {
+ return dfltcc_alloc_state(strm, sizeof(deflate_state), sizeof(struct dfltcc_deflate_state));
+}
+
+void Z_INTERNAL PREFIX(dfltcc_reset_deflate_state)(PREFIX3(streamp) strm) {
+ deflate_state *state = (deflate_state *)strm->state;
+ struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state);
+
+ dfltcc_reset_state(&dfltcc_state->common);
+
+ /* Initialize tuning parameters */
+ dfltcc_state->level_mask = DFLTCC_LEVEL_MASK;
+ dfltcc_state->block_size = DFLTCC_BLOCK_SIZE;
+ dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE;
+ dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE;
+}
+
+void Z_INTERNAL PREFIX(dfltcc_copy_deflate_state)(void *dst, const void *src) {
+ dfltcc_copy_state(dst, src, sizeof(deflate_state), sizeof(struct dfltcc_deflate_state));
+}
+
static inline int dfltcc_can_deflate_with_params(PREFIX3(streamp) strm, int level, uInt window_bits, int strategy,
int reproducible) {
deflate_state *state = (deflate_state *)strm->state;
- struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
+ struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state);
/* Unsupported compression settings */
if ((dfltcc_state->level_mask & (1 << level)) == 0)
return 0;
/* Unsupported hardware */
- if (!is_bit_set(dfltcc_state->af.fns, DFLTCC_GDHT) ||
- !is_bit_set(dfltcc_state->af.fns, DFLTCC_CMPR) ||
- !is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0))
+ if (!is_bit_set(dfltcc_state->common.af.fns, DFLTCC_GDHT) ||
+ !is_bit_set(dfltcc_state->common.af.fns, DFLTCC_CMPR) ||
+ !is_bit_set(dfltcc_state->common.af.fmts, DFLTCC_FMT0))
return 0;
return 1;
int Z_INTERNAL PREFIX(dfltcc_deflate)(PREFIX3(streamp) strm, int flush, block_state *result) {
deflate_state *state = (deflate_state *)strm->state;
- struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
- struct dfltcc_param_v0 *param = &dfltcc_state->param;
+ struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state);
+ struct dfltcc_param_v0 *param = &dfltcc_state->common.param;
uInt masked_avail_in;
dfltcc_cc cc;
int need_empty_block;
} while (cc == DFLTCC_CC_AGAIN);
/* Translate parameter block to stream */
- strm->msg = oesc_msg(dfltcc_state->msg, param->oesc);
+ strm->msg = oesc_msg(dfltcc_state->common.msg, param->oesc);
state->bi_valid = param->sbb;
if (state->bi_valid == 0)
state->bi_buf = 0; /* Avoid accessing next_out */
#include "dfltcc_common.h"
+void Z_INTERNAL *PREFIX(dfltcc_alloc_deflate_state)(PREFIX3(streamp));
+void Z_INTERNAL PREFIX(dfltcc_reset_deflate_state)(PREFIX3(streamp));
+void Z_INTERNAL PREFIX(dfltcc_copy_deflate_state)(void *dst, const void *src);
int Z_INTERNAL PREFIX(dfltcc_can_deflate)(PREFIX3(streamp) strm);
int Z_INTERNAL PREFIX(dfltcc_deflate)(PREFIX3(streamp) strm, int flush, block_state *result);
int Z_INTERNAL PREFIX(dfltcc_deflate_params)(PREFIX3(streamp) strm, int level, int strategy, int *flush);
const unsigned char *dictionary, uInt dict_length);
int Z_INTERNAL PREFIX(dfltcc_deflate_get_dictionary)(PREFIX3(streamp) strm, unsigned char *dictionary, uInt* dict_length);
+#define ZALLOC_DEFLATE_STATE PREFIX(dfltcc_alloc_deflate_state)
+#define ZCOPY_DEFLATE_STATE PREFIX(dfltcc_copy_deflate_state)
+
#define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) \
do { \
if (PREFIX(dfltcc_can_deflate)((strm))) \
return PREFIX(dfltcc_deflate_get_dictionary)((strm), (dict), (dict_len)); \
} while (0)
-#define DEFLATE_RESET_KEEP_HOOK(strm) \
- PREFIX(dfltcc_reset)((strm), sizeof(deflate_state))
+#define DEFLATE_RESET_KEEP_HOOK PREFIX(dfltcc_reset_deflate_state)
#define DEFLATE_PARAMS_HOOK(strm, level, strategy, hook_flush) \
do { \
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <string.h>
#ifdef HAVE_SYS_SDT_H
#include <sys/sdt.h>
bits[n / 8] &= ~(1 << (7 - (n % 8)));
}
+static inline int is_dfltcc_enabled(void) {
+ uint64_t facilities[(DFLTCC_FACILITY / 64) + 1];
+ Z_REGISTER uint8_t r0 __asm__("r0");
+
+ memset(facilities, 0, sizeof(facilities));
+ r0 = sizeof(facilities) / sizeof(facilities[0]) - 1;
+ /* STFLE is supported since z9-109 and only in z/Architecture mode. When
+ * compiling with -m31, gcc defaults to ESA mode, however, since the kernel
+ * is 64-bit, it's always z/Architecture mode at runtime.
+ */
+ __asm__ volatile(
+#ifndef __clang__
+ ".machinemode push\n"
+ ".machinemode zarch\n"
+#endif
+ "stfle %[facilities]\n"
+#ifndef __clang__
+ ".machinemode pop\n"
+#endif
+ : [facilities] "=Q" (facilities), [r0] "+r" (r0) :: "cc");
+ return is_bit_set((const char *)facilities, DFLTCC_FACILITY);
+}
+
#define DFLTCC_FMT0 0
/*
struct dfltcc_state {
struct dfltcc_param_v0 param; /* Parameter block. */
struct dfltcc_qaf_param af; /* Available functions. */
- uint16_t level_mask; /* Levels on which to use DFLTCC */
- uint32_t block_size; /* New block each X bytes */
- size_t block_threshold; /* New block after total_in > X */
- uint32_t dht_threshold; /* New block only if avail_in >= X */
char msg[64]; /* Buffer for strm->msg */
};
#define ALIGN_UP(p, size) (__typeof__(p))(((uintptr_t)(p) + ((size) - 1)) & ~((size) - 1))
#define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((char *)(state) + ALIGN_UP(sizeof(*state), 8)))
+
+static inline void *dfltcc_alloc_state(PREFIX3(streamp) strm, uInt size, uInt extension_size) {
+ return ZALLOC(strm, 1, ALIGN_UP(size, 8) + extension_size);
+}
+
+static inline void dfltcc_reset_state(struct dfltcc_state *dfltcc_state) {
+ /* Initialize available functions */
+ if (is_dfltcc_enabled()) {
+ dfltcc(DFLTCC_QAF, &dfltcc_state->param, NULL, NULL, NULL, NULL, NULL);
+ memmove(&dfltcc_state->af, &dfltcc_state->param, sizeof(dfltcc_state->af));
+ } else
+ memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
+
+ /* Initialize parameter block */
+ memset(&dfltcc_state->param, 0, sizeof(dfltcc_state->param));
+ dfltcc_state->param.nt = 1;
+ dfltcc_state->param.ribm = DFLTCC_RIBM;
+}
+
+static inline void dfltcc_copy_state(void *dst, const void *src, uInt size, uInt extension_size) {
+ memcpy(dst, src, ALIGN_UP(size, 8) + extension_size);
+}
#include "dfltcc_inflate.h"
#include "dfltcc_detail.h"
+struct inflate_state Z_INTERNAL *PREFIX(dfltcc_alloc_inflate_state)(PREFIX3(streamp) strm) {
+ return (struct inflate_state *)dfltcc_alloc_state(strm, sizeof(struct inflate_state), sizeof(struct dfltcc_state));
+}
+
+void Z_INTERNAL PREFIX(dfltcc_reset_inflate_state)(PREFIX3(streamp) strm) {
+ struct inflate_state *state = (struct inflate_state *)strm->state;
+ struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
+
+ dfltcc_reset_state(dfltcc_state);
+}
+
+void Z_INTERNAL PREFIX(dfltcc_copy_inflate_state)(struct inflate_state *dst, const struct inflate_state *src) {
+ dfltcc_copy_state(dst, src, sizeof(struct inflate_state), sizeof(struct dfltcc_state));
+}
+
int Z_INTERNAL PREFIX(dfltcc_can_inflate)(PREFIX3(streamp) strm) {
struct inflate_state *state = (struct inflate_state *)strm->state;
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
#include "dfltcc_common.h"
+struct inflate_state Z_INTERNAL *PREFIX(dfltcc_alloc_inflate_state)(PREFIX3(streamp) strm);
+void Z_INTERNAL PREFIX(dfltcc_reset_inflate_state)(PREFIX3(streamp) strm);
+void Z_INTERNAL PREFIX(dfltcc_copy_inflate_state)(struct inflate_state *dst, const struct inflate_state *src);
int Z_INTERNAL PREFIX(dfltcc_can_inflate)(PREFIX3(streamp) strm);
typedef enum {
DFLTCC_INFLATE_CONTINUE,
int Z_INTERNAL PREFIX(dfltcc_was_inflate_used)(PREFIX3(streamp) strm);
int Z_INTERNAL PREFIX(dfltcc_inflate_disable)(PREFIX3(streamp) strm);
-#define INFLATE_RESET_KEEP_HOOK(strm) \
- PREFIX(dfltcc_reset)((strm), sizeof(struct inflate_state))
+#define ZALLOC_INFLATE_STATE PREFIX(dfltcc_alloc_inflate_state)
+#define ZCOPY_INFLATE_STATE PREFIX(dfltcc_copy_inflate_state)
+
+#define INFLATE_RESET_KEEP_HOOK PREFIX(dfltcc_reset_inflate_state)
#define INFLATE_PRIME_HOOK(strm, bits, value) \
do { if (PREFIX(dfltcc_inflate_disable)((strm))) return Z_STREAM_ERROR; } while (0)
# include "arch/s390/dfltcc_deflate.h"
#else
/* Memory management for the deflate state. Useful for allocating arch-specific extension blocks. */
-# define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
+# define ZALLOC_DEFLATE_STATE(strm) ((deflate_state *)ZALLOC(strm, 1, sizeof(deflate_state)))
# define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
-# define ZCOPY_STATE(dst, src, size) memcpy(dst, src, size)
+# define ZCOPY_DEFLATE_STATE(dst, src) memcpy(dst, src, sizeof(deflate_state))
/* Memory management for the window. Useful for allocation the aligned window. */
# define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
# define TRY_FREE_WINDOW(strm, addr) TRY_FREE(strm, addr)
if (windowBits == 8)
windowBits = 9; /* until 256-byte window bug fixed */
- s = (deflate_state *) ZALLOC_STATE(strm, 1, sizeof(deflate_state));
+ s = ZALLOC_DEFLATE_STATE(strm);
if (s == NULL)
return Z_MEM_ERROR;
strm->state = (struct internal_state *)s;
memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
- ds = (deflate_state *) ZALLOC_STATE(dest, 1, sizeof(deflate_state));
+ ds = ZALLOC_DEFLATE_STATE(dest);
if (ds == NULL)
return Z_MEM_ERROR;
dest->state = (struct internal_state *) ds;
- ZCOPY_STATE((void *)ds, (void *)ss, sizeof(deflate_state));
+ ZCOPY_DEFLATE_STATE(ds, ss);
ds->strm = dest;
#ifdef X86_PCLMULQDQ_CRC
}
if (strm->zfree == NULL)
strm->zfree = zng_cfree;
- state = (struct inflate_state *)ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
+ state = ZALLOC_INFLATE_STATE(strm);
if (state == NULL)
return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
}
if (strm->zfree == NULL)
strm->zfree = zng_cfree;
- state = (struct inflate_state *) ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
+ state = ZALLOC_INFLATE_STATE(strm);
if (state == NULL)
return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
state = (struct inflate_state *)source->state;
/* allocate space */
- copy = (struct inflate_state *)ZALLOC_STATE(source, 1, sizeof(struct inflate_state));
+ copy = ZALLOC_INFLATE_STATE(source);
if (copy == NULL)
return Z_MEM_ERROR;
window = NULL;
/* copy state */
memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
- ZCOPY_STATE((void *)copy, (void *)state, sizeof(struct inflate_state));
+ ZCOPY_INFLATE_STATE(copy, state);
copy->strm = dest;
if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) {
copy->lencode = copy->codes + (state->lencode - state->codes);
# include "arch/s390/dfltcc_inflate.h"
#else
/* Memory management for the inflate state. Useful for allocating arch-specific extension blocks. */
-# define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
+# define ZALLOC_INFLATE_STATE(strm) ((struct inflate_state *)ZALLOC(strm, 1, sizeof(struct inflate_state)))
# define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
-# define ZCOPY_STATE(dst, src, size) memcpy(dst, src, size)
+# define ZCOPY_INFLATE_STATE(dst, src) memcpy(dst, src, sizeof(struct inflate_state))
/* Memory management for the window. Useful for allocation the aligned window. */
# define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
# define ZFREE_WINDOW(strm, addr) ZFREE(strm, addr)