public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
From: Joakim Tjernlund <Joakim.Tjernlund@lumentis.se>
To: linux-mtd@lists.infradead.org
Subject: compr_zlib.c
Date: Mon, 11 Mar 2002 09:56:30 +0100	[thread overview]
Message-ID: <200203110856.JAA08992@mail.utfors.se> (raw)
In-Reply-To: <01121714081700.06319@jocke.lumentis.se>

Hi all

I noticed that zlib.c by default adds a small header and a adler32 checksum to the compressed
data. This is not needed since JFFS2 adds it's own CRC32. There is an 'undocumented' way to 
avoid the header and adler32 checksum, specify a negative windowBits to deflateInit2()/inflateInit2().
Also it is possible to trim the memory usage by adjusting  windowBits and memLevel accordinly. I have
left these at their default values since I don't know zlib very well. Perhaps someone else can comment?

Also I wonder about STREAM_END_SPACE, which is defined to 12. Is it useful to try and compress
data as small as 13 bytes with zlib? Maybe the simpler rtime should be used for small amounts of
data?

Below is a patch against the stable 2.4 branch, which is backwards compatible.

        Jocke

PS.
     There has been a few improvements to JFFS2 that I would like to see in the stable 2.4 branch, we
     have been using these for about 2 weeks now and I think they are stable. 


Index: fs/jffs2/compr_zlib.c
===================================================================
RCS file: /home/cvs/mtd/fs/jffs2/compr_zlib.c,v
retrieving revision 1.8
diff -u -r1.8 compr_zlib.c
--- fs/jffs2/compr_zlib.c       2001/09/20 15:28:31     1.8
+++ fs/jffs2/compr_zlib.c       2002/03/11 08:31:35
@@ -31,7 +31,7 @@
  * provisions above, a recipient may use your version of this file
  * under either the RHEPL or the GPL.
  *
- * $Id: compr_zlib.c,v 1.8 2001/09/20 15:28:31 dwmw2 Exp $
+ * $Id: compr_zlib.c,v 1.8 2002/01/31 13:56:11 jocke Exp $
  *
  */

@@ -92,9 +92,21 @@
        strm.zalloc = (void *)0;
        strm.zfree = (void *)0;
 #endif
-
-       if (Z_OK != deflateInit(&strm, 3)) {
-               printk(KERN_WARNING "deflateInit failed\n");
+       /* The memory requirements for deflate are (in bytes):
+          1 << (windowBits+2)   +  1 << (memLevel+9)
+          that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
+          plus a few kilobytes for small objects. For example, if you want to reduce
+          the default memory requirements from 256K to 128K, compile with
+              make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
+          Of course this will generally degrade compression (there's no free lunch).
+
+          The memory requirements for inflate are (in bytes) 1 << windowBits
+          that is, 32K for windowBits=15 (default value) plus a few kilobytes
+          for small objects.
+       */
+       /* -MAX_WBITS impiles -> suppress zlib header and adler32 */
+       if (Z_OK != deflateInit2(&strm, 3, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY)) {
+               printk(KERN_WARNING "deflateInit2 failed\n");
                return -1;
        }
        strm.next_in = data_in;
@@ -142,7 +154,7 @@
                      __u32 srclen, __u32 destlen)
 {
        z_stream strm;
-       int ret;
+       int ret, wbits, i;

 #ifdef __KERNEL__
        strm.zalloc = zalloc;
@@ -151,23 +163,34 @@
        strm.zalloc = (void *)0;
        strm.zfree = (void *)0;
 #endif
-
-       if (Z_OK != inflateInit(&strm)) {
-               printk(KERN_WARNING "inflateInit failed\n");
-               return;
-       }
-       strm.next_in = data_in;
-       strm.avail_in = srclen;
-       strm.total_in = 0;
-
-       strm.next_out = cpage_out;
-       strm.avail_out = destlen;
-       strm.total_out = 0;
+       /* -MAX_WBITS impiles -> suppress zlib header and adler32.
+          try first with -MAX_WBITS, if that fails, try MAX_WBITS to be
+          backwards compatible */
+       wbits = -MAX_WBITS;
+       for(i = 0; i < 2; i++){
+               if (Z_OK != inflateInit2(&strm, wbits)) {
+                       printk(KERN_WARNING "inflateInit2 failed\n");
+                       return;
+               }
+               strm.next_in = data_in;
+               strm.avail_in = srclen;
+               strm.total_in = 0;
+
+               strm.next_out = cpage_out;
+               strm.avail_out = destlen;
+               strm.total_out = 0;
+
+               while((ret = inflate(&strm, Z_FINISH)) == Z_OK)
+                       ;
+               inflateEnd(&strm);
+               if (ret == Z_DATA_ERROR){
+                       wbits = -wbits;
+                       continue; /* try again with next wbits */
+               }

-       while((ret = inflate(&strm, Z_FINISH)) == Z_OK)
-               ;
-       if (ret != Z_STREAM_END) {
-               printk(KERN_NOTICE "inflate returned %d\n", ret);
+               if (ret != Z_STREAM_END) {
+                       printk(KERN_NOTICE "inflate returned %d, wbits: %d\n", ret, wbits);
+               }
+               break; /* all went well */
        }
-       inflateEnd(&strm);
 }

  reply	other threads:[~2002-03-11  9:33 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-12 12:14 Cache mappings and invalidate Joakim Tjernlund
2001-11-12 17:44 ` Joakim Tjernlund
2001-12-17 13:08 ` Burst read and other improvements Joakim Tjernlund
2002-03-11  8:56   ` Joakim Tjernlund [this message]
2002-03-19 12:03   ` compr_zlib.c Joakim Tjernlund
2002-03-19 12:24     ` compr_zlib.c David Woodhouse
2002-01-04  8:59 ` CLEANMARKER question Joakim Tjernlund
2002-01-04  9:42   ` David Woodhouse
2002-01-04 10:33     ` Joakim Tjernlund
2002-01-04 10:41       ` David Woodhouse
2002-01-29 10:33   ` MTD/CFI probe broken? Joakim Tjernlund
2002-01-29 18:09     ` Joakim Tjernlund
2002-02-14 14:05     ` cfi_cmdset0001.c: bug fixes and new features Joakim Tjernlund
2002-02-26 13:42     ` scan.c & ACCURATE Joakim Tjernlund
2002-02-26 15:52       ` David Woodhouse
2002-02-27  7:34         ` Joakim Tjernlund
2002-02-27  8:17           ` David Woodhouse
2002-02-27 12:29             ` Joakim Tjernlund
2002-02-26 15:53       ` David Woodhouse
2002-02-27  7:43         ` Joakim Tjernlund
2002-06-17  9:24       ` point()/unpoint() questions + small cfi_cmdset_0001.c patch Joakim Tjernlund
2002-06-17  9:56         ` David Woodhouse
2002-06-17 13:37           ` David Woodhouse
2002-06-17 15:41           ` Joakim Tjernlund
2002-06-17 15:56             ` David Woodhouse
2002-06-17 16:22               ` Joakim Tjernlund
2002-06-18 14:11                 ` David Woodhouse
2002-06-17  9:48       ` [PATCH] scan.c Joakim Tjernlund
2002-06-17  9:54       ` Joakim Tjernlund
2002-06-17 10:15         ` David Woodhouse
2002-06-17 12:16           ` Joakim Tjernlund
2002-06-17 12:45             ` David Woodhouse
2002-06-17 15:12               ` Joakim Tjernlund
2002-06-17 16:00                 ` David Woodhouse
2002-06-17 16:51                   ` Joakim Tjernlund
2002-06-17 22:59                     ` David Woodhouse
  -- strict thread matches above, loose matches on Subject: below --
2002-03-06 15:54 Lost DOC2000 after installation of Grub Chris Fowler
2002-03-19 18:55 ` compr_zlib.c Joakim Tjernlund
2002-03-20 17:36   ` compr_zlib.c David Woodhouse
2002-03-20 18:58     ` compr_zlib.c Joakim Tjernlund

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200203110856.JAA08992@mail.utfors.se \
    --to=joakim.tjernlund@lumentis.se \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox