public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
From: Richard Purdie <rpurdie@openedhand.com>
To: linux-mtd <linux-mtd@lists.infradead.org>, Josh Boyer <jdub@us.ibm.com>
Subject: [mtd-utils patch 2/2] Add favourlzo compression mode
Date: Tue, 10 Jul 2007 16:03:47 +0100	[thread overview]
Message-ID: <1184079827.6435.56.camel@localhost.localdomain> (raw)

Add a favourlzo compression mode to mtd-utils

This allows lzo compression to be used in the cases where the
compression ratio isn't quite as good zlib. This can make sense in
certain use cases because LZO decompression is much faster than zlib.

Signed-off-by: Richard Purdie <rpurdie@openedhand.com>

---
 compr.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 compr.h |    1 +
 2 files changed, 50 insertions(+), 3 deletions(-)

Index: git/compr.c
===================================================================
--- git.orig/compr.c	2007-03-01 11:58:01.000000000 +0000
+++ git/compr.c	2007-03-01 11:58:09.000000000 +0000
@@ -16,6 +16,8 @@
 #include <stdlib.h>
 #include <linux/jffs2.h>
 
+#define FAVOUR_LZO_PERCENT 80
+
 extern int page_size;
 
 /* LIST IMPLEMENTATION (from linux/list.h) */
@@ -166,6 +168,33 @@ static void jffs2_decompression_test(str
 	}
 }
 
+/*
+ * Return 1 to use this compression
+ */
+static int jffs2_is_best_compression(struct jffs2_compressor *this,
+		struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
+{
+	switch (jffs2_compression_mode) {
+	case JFFS2_COMPR_MODE_SIZE:
+		if (bestsize > size)
+			return 1;
+		return 0;
+	case JFFS2_COMPR_MODE_FAVOURLZO:
+		if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
+			return 1;
+		if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
+			return 1;
+		if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
+			return 1;
+		if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
+			return 1;
+
+		return 0;
+	}
+	/* Shouldn't happen */
+	return 0;
+}
+
 /* jffs2_compress:
  * @data: Pointer to uncompressed data
  * @cdata: Pointer to returned pointer to buffer for compressed data
@@ -231,21 +260,29 @@ uint16_t jffs2_compress( unsigned char *
 			}
 			if (ret == JFFS2_COMPR_NONE) free(output_buf);
 			break;
+		case JFFS2_COMPR_MODE_FAVOURLZO:
 		case JFFS2_COMPR_MODE_SIZE:
 			orig_slen = *datalen;
 			orig_dlen = *cdatalen;
 			list_for_each_entry(this, &jffs2_compressor_list, list) {
+				uint32_t needed_buf_size;
+
+				if (jffs2_compression_mode == JFFS2_COMPR_MODE_FAVOURLZO)
+					needed_buf_size = orig_slen + jffs2_compression_check;
+				else
+					needed_buf_size = orig_dlen + jffs2_compression_check;
+
 				/* Skip decompress-only backwards-compatibility and disabled modules */
 				if ((!this->compress)||(this->disabled))
 					continue;
 				/* Allocating memory for output buffer if necessary */
-				if ((this->compr_buf_size<orig_dlen+jffs2_compression_check)&&(this->compr_buf)) {
+				if ((this->compr_buf_size < needed_buf_size) && (this->compr_buf)) {
 					free(this->compr_buf);
 					this->compr_buf_size=0;
 					this->compr_buf=NULL;
 				}
 				if (!this->compr_buf) {
-					tmp_buf = malloc(orig_dlen+jffs2_compression_check);
+					tmp_buf = malloc(needed_buf_size);
 					if (!tmp_buf) {
 						fprintf(stderr,"mkfs.jffs2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
 						continue;
@@ -265,7 +302,8 @@ uint16_t jffs2_compress( unsigned char *
 				if (!compr_ret) {
 					if (jffs2_compression_check)
 						jffs2_decompression_test(this, data_in, this->compr_buf, *cdatalen, *datalen, this->compr_buf_size);
-					if ((!best_dlen)||(best_dlen>*cdatalen)) {
+					if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
+								&& (*cdatalen < *datalen)) {
 						best_dlen = *cdatalen;
 						best_slen = *datalen;
 						best = this;
@@ -377,6 +415,9 @@ char *jffs2_stats(void)
 		case JFFS2_COMPR_MODE_SIZE:
 			act_buf += sprintf(act_buf,"size");
 			break;
+		case JFFS2_COMPR_MODE_FAVOURLZO:
+			act_buf += sprintf(act_buf, "favourlzo");
+			break;
 		default:
 			act_buf += sprintf(act_buf,"unkown");
 			break;
@@ -413,6 +454,11 @@ int jffs2_set_compression_mode_name(cons
 		jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
 		return 0;
 	}
+	if (!strcmp("favourlzo", name)) {
+		jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
+		return 0;
+	}
+
 	return 1;
 }
 
Index: git/compr.h
===================================================================
--- git.orig/compr.h	2007-03-01 11:58:01.000000000 +0000
+++ git/compr.h	2007-03-01 11:58:09.000000000 +0000
@@ -32,6 +32,7 @@
 #define JFFS2_COMPR_MODE_NONE       0
 #define JFFS2_COMPR_MODE_PRIORITY   1
 #define JFFS2_COMPR_MODE_SIZE       2
+#define JFFS2_COMPR_MODE_FAVOURLZO  3
 
 #define kmalloc(a,b)                malloc(a)
 #define kfree(a)                    free(a)

             reply	other threads:[~2007-07-10 15:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-10 15:03 Richard Purdie [this message]
2007-07-23 14:23 ` [mtd-utils patch 2/2] Add favourlzo compression mode Josh Boyer
  -- strict thread matches above, loose matches on Subject: below --
2007-03-01 12:08 Richard Purdie

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=1184079827.6435.56.camel@localhost.localdomain \
    --to=rpurdie@openedhand.com \
    --cc=jdub@us.ibm.com \
    --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