1 /* dfltcc_deflate.c - IBM Z DEFLATE CONVERSION CALL general support. */
2 
3 #include "zbuild.h"
4 #include "dfltcc_common.h"
5 #include "dfltcc_detail.h"
6 
7 /*
8    Memory management.
9 
10    DFLTCC requires parameter blocks and window to be aligned. zlib-ng allows
11    users to specify their own allocation functions, so using e.g.
12    `posix_memalign' is not an option. Thus, we overallocate and take the
13    aligned portion of the buffer.
14 */
15 
16 static const int PAGE_ALIGN = 0x1000;
17 
PREFIX(dfltcc_alloc_window)18 void Z_INTERNAL *PREFIX(dfltcc_alloc_window)(PREFIX3(streamp) strm, uInt items, uInt size) {
19     void *p;
20     void *w;
21 
22     /* To simplify freeing, we store the pointer to the allocated buffer right
23      * before the window.
24      */
25     p = ZALLOC(strm, sizeof(void *) + items * size + PAGE_ALIGN, sizeof(unsigned char));
26     if (p == NULL)
27         return NULL;
28     w = ALIGN_UP((char *)p + sizeof(void *), PAGE_ALIGN);
29     *(void **)((char *)w - sizeof(void *)) = p;
30     return w;
31 }
32 
PREFIX(dfltcc_free_window)33 void Z_INTERNAL PREFIX(dfltcc_free_window)(PREFIX3(streamp) strm, void *w) {
34     if (w)
35         ZFREE(strm, *(void **)((unsigned char *)w - sizeof(void *)));
36 }
37