zlib, deflate: How much memory to allocate?

前端 未结 2 1266
耶瑟儿~
耶瑟儿~ 2020-12-15 23:45

I am using zlib to compress a stream of text data. The text data comes in chunks, and for each chunk, deflate() is called, with flush set to Z_NO_FLUSH

2条回答
  •  忘掉有多难
    2020-12-16 00:05

    While looking at the sources for a hint, I fell over

    /* =========================================================================
     * Flush as much pending output as possible. All deflate() output goes
     * through this function so some applications may wish to modify it
     * to avoid allocating a large strm->next_out buffer and copying into it.
     * (See also read_buf()).
     */
    local void flush_pending(strm)
        z_streamp strm;
    {
        unsigned len = strm->state->pending;
    ...
    

    tracing the use of void flush_pending() throughout deflate() shows, that an upper bound on the needed output buffer in the middle of the stream is

    strm->state->pending + deflateBound(strm, strm->avail_in)
    

    the first part accounts for data still in the pipe from previous calls to deflate(), the second part accounts for the not-yet processed data of length avail_in.

提交回复
热议问题