Configure ZLib to use bucket allocator in deflate buckets.

* buckets/deflate_buckets.c
  (zalloc_func, zalloc_free): New.
  (serf_bucket_deflate_create,
   serf_bucket_deflate_compress_create): Configure zlib's alloc/free callbacks
   to allocate memory from bucket allocator.


git-svn-id: https://svn.apache.org/repos/asf/serf/trunk@1761053 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/buckets/deflate_buckets.c b/buckets/deflate_buckets.c
index 59d4050..cd41c0c 100644
--- a/buckets/deflate_buckets.c
+++ b/buckets/deflate_buckets.c
@@ -93,6 +93,23 @@
           | (((unsigned long)string[3]) << 24);
 }
 
+/* zlib alloc function. opaque is the bucket allocator. */
+static voidpf zalloc_func(voidpf opaque, uInt items, uInt size)
+{
+    serf_bucket_alloc_t *allocator = opaque;
+    apr_size_t alloc_size = items * size;
+    return serf_bucket_mem_alloc(allocator, alloc_size);
+}
+
+/* zlib free function */
+static void zfree_func(voidpf opaque, voidpf address)
+{
+    if (address) {
+        serf_bucket_alloc_t *allocator = opaque;
+        serf_bucket_mem_free(allocator, address);
+    }
+}
+
 serf_bucket_t *serf_bucket_deflate_create(
     serf_bucket_t *stream,
     serf_bucket_alloc_t *allocator,
@@ -110,6 +127,12 @@
     /* zstream must be NULL'd out. */
     memset(&ctx->zstream, 0, sizeof(ctx->zstream));
 
+    /* Configure alloc/free callbacks to allocate memory from bucket
+     * allocator. */
+    ctx->zstream.zalloc = zalloc_func;
+    ctx->zstream.zfree = zfree_func;
+    ctx->zstream.opaque = allocator;
+
     switch (ctx->format) {
         case SERF_DEFLATE_GZIP:
             ctx->state = STATE_READING_HEADER;
@@ -151,6 +174,12 @@
     /* zstream must be NULL'd out. */
     memset(&ctx->zstream, 0, sizeof(ctx->zstream));
 
+    /* Configure alloc/free callbacks to allocate memory from bucket
+     * allocator. */
+    ctx->zstream.zalloc = zalloc_func;
+    ctx->zstream.zfree = zfree_func;
+    ctx->zstream.opaque = allocator;
+
     switch (ctx->format) {
         case SERF_DEFLATE_GZIP:
             ctx->state = STATE_WRITING_HEADER;