Skip to content

Commit

Permalink
Add window alignment selection for s390x DFLTCC support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dead2 committed May 6, 2024
1 parent c7326ab commit 6d6120c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
21 changes: 14 additions & 7 deletions deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ const char PREFIX(deflate_copyright)[] = " deflate 1.3.1 Copyright 1995-2024 Jea
*/
#ifdef S390_DFLTCC_DEFLATE
# include "arch/s390/dfltcc_deflate.h"
/* DFLTCC instructions require window to be page-aligned */
# define PAD_WINDOW PAD_4096
# define WINDOW_PAD_SIZE 4096
# define HINT_ALIGNED_WINDOW HINT_ALIGNED_4096
#else
# define PAD_WINDOW PAD_64
# define WINDOW_PAD_SIZE 64
# define HINT_ALIGNED_WINDOW HINT_ALIGNED_64
/* Invoked at the beginning of deflateSetDictionary(). Useful for checking arch-specific window data. */
# define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
/* Invoked at the beginning of deflateGetDictionary(). Useful for adjusting arch-specific window data. */
Expand Down Expand Up @@ -202,8 +209,8 @@ 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_64(currsize), PADSZ(currsize,64));
int window_pos = PAD_64(currsize);
LOGSZP("window", window_size, PAD_WINDOW(currsize), PADSZ(currsize,WINDOW_PAD_SIZE));
int window_pos = PAD_WINDOW(currsize);
currsize = window_pos + window_size;

LOGSZP("prev", prev_size, PAD_64(currsize), PADSZ(currsize,64));
Expand All @@ -226,24 +233,24 @@ Z_INTERNAL deflate_allocs* alloc_deflate(PREFIX3(stream) *strm, int windowBits,
int alloc_pos = PAD_16(currsize);
currsize = alloc_pos + alloc_size;

/* Add 64 to allow our alignment, and round size of buffer up to multiple of 64 */
int totalsize = PAD_64(63 + currsize);
/* Add 64-1 or 4096-1 to allow window alignment, and round size of buffer up to multiple of 64 */
int totalsize = PAD_64(currsize + (WINDOW_PAD_SIZE - 1));

/* Allocate buffer, align to 64-byte cacheline, and zerofill the resulting buffer */
char *origbuff = strm->zalloc(strm->opaque, 1, totalsize);
if (origbuff == NULL)
return NULL;

char *buff = (char *)HINT_ALIGNED_64((char *)PAD_64(origbuff));
LOGSZPL("Buffer alloc", totalsize, PADSZ((uintptr_t)origbuff,64), PADSZ(currsize,64));
char *buff = (char *)HINT_ALIGNED_WINDOW((char *)PAD_WINDOW(origbuff));
LOGSZPL("Buffer alloc", totalsize, PADSZ((uintptr_t)origbuff,WINDOW_PAD_SIZE), PADSZ(currsize,WINDOW_PAD_SIZE));

/* Initialize alloc_bufs */
deflate_allocs *alloc_bufs = (struct deflate_allocs_s *)(buff + alloc_pos);
alloc_bufs->buf_start = (char *)origbuff;
alloc_bufs->zfree = strm->zfree;

/* Assign buffers */
alloc_bufs->window = (unsigned char *)HINT_ALIGNED_64(buff + window_pos);
alloc_bufs->window = (unsigned char *)HINT_ALIGNED_WINDOW(buff + window_pos);
alloc_bufs->prev = (Pos *)HINT_ALIGNED_64(buff + prev_pos);
alloc_bufs->head = (Pos *)HINT_ALIGNED_64(buff + head_pos);
alloc_bufs->pending_buf = (unsigned char *)HINT_ALIGNED_64(buff + pending_pos);
Expand Down
14 changes: 7 additions & 7 deletions inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm) {
int alloc_size = sizeof(inflate_allocs);

/* Calculate relative buffer positions and paddings */
LOGSZP("window", window_size, PAD_64(currsize), PADSZ(currsize,64));
int window_pos = PAD_64(currsize);
LOGSZP("window", window_size, PAD_WINDOW(currsize), PADSZ(currsize,WINDOW_PAD_SIZE));
int window_pos = PAD_WINDOW(currsize);
currsize = window_pos + window_size;

LOGSZP("state", state_size, PAD_64(currsize), PADSZ(currsize,64));
Expand All @@ -166,23 +166,23 @@ Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm) {
int alloc_pos = PAD_16(currsize);
currsize = alloc_pos + alloc_size;

/* Add 64 to allow our alignment, and round size of buffer up to multiple of 64 */
int totalsize = PAD_64(currsize + 63);
/* Add 64-1 or 4096-1 to allow window alignment, and round size of buffer up to multiple of 64 */
int totalsize = PAD_64(currsize + (WINDOW_PAD_SIZE - 1));

/* Allocate buffer, align to 64-byte cacheline, and zerofill the resulting buffer */
char *origbuff = strm->zalloc(strm->opaque, 1, totalsize);
if (origbuff == NULL)
return NULL;

char *buff = (char *)HINT_ALIGNED_64((char *)PAD_64(origbuff));
LOGSZPL("Buffer alloc", totalsize, PADSZ((uintptr_t)origbuff,64), PADSZ(currsize,64));
char *buff = (char *)HINT_ALIGNED_WINDOW((char *)PAD_WINDOW(origbuff));
LOGSZPL("Buffer alloc", totalsize, PADSZ((uintptr_t)origbuff,WINDOW_PAD_SIZE), PADSZ(currsize,WINDOW_PAD_SIZE));

/* Initialize alloc_bufs */
inflate_allocs *alloc_bufs = (struct inflate_allocs_s *)(buff + alloc_pos);
alloc_bufs->buf_start = (char *)origbuff;
alloc_bufs->zfree = strm->zfree;

alloc_bufs->window = (unsigned char *)HINT_ALIGNED_64((buff + window_pos));
alloc_bufs->window = (unsigned char *)HINT_ALIGNED_WINDOW((buff + window_pos));
alloc_bufs->state = (inflate_state *)HINT_ALIGNED_64((buff + state_pos));

#ifdef Z_MEMORY_SANITIZER
Expand Down
7 changes: 7 additions & 0 deletions inflate_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
/* Architecture-specific hooks. */
#ifdef S390_DFLTCC_INFLATE
# include "arch/s390/dfltcc_inflate.h"
/* DFLTCC instructions require window to be page-aligned */
# define PAD_WINDOW PAD_4096
# define WINDOW_PAD_SIZE 4096
# define HINT_ALIGNED_WINDOW HINT_ALIGNED_4096
#else
# 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. */
Expand Down
6 changes: 4 additions & 2 deletions zbuild.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,18 @@
#else
# define HINT_ALIGNED(p,n) (p)
#endif
#define HINT_ALIGNED_64(p) HINT_ALIGNED((p),64)
#define HINT_ALIGNED_16(p) HINT_ALIGNED((p),16)
#define HINT_ALIGNED_64(p) HINT_ALIGNED((p),64)
#define HINT_ALIGNED_4096(p) HINT_ALIGNED((p),4096)

/* PADSZ returns needed bytes to pad bpos to pad size
* PAD_NN calculates pad size and adds it to bpos, returning the result.
* All take an integer or a pointer as bpos input.
*/
#define PADSZ(bpos, pad) (((pad) - ((uintptr_t)(bpos) % (pad))) % (pad))
#define PAD_64(bpos) ((bpos) + PADSZ((bpos),64))
#define PAD_16(bpos) ((bpos) + PADSZ((bpos),16))
#define PAD_64(bpos) ((bpos) + PADSZ((bpos),64))
#define PAD_4096(bpos) ((bpos) + PADSZ((bpos),4096))

/* Diagnostic functions */
#ifdef ZLIB_DEBUG
Expand Down

0 comments on commit 6d6120c

Please sign in to comment.