Skip to content

Commit

Permalink
staging: Add Snappy compression library (v3)
Browse files Browse the repository at this point in the history
Google's Snappy data compression library is a faster alternative to LZO,
optimized for x86-64. On compressible input it compresses ~2.5x faster than LZO
and decompresses ~1.5-2x faster than LZO. On incompressible input, it skips the
input at 100x faster than LZO and decompresses ~4x faster than LZO.
It is released under BSD license.
This is a kernel port from user space C++ code.
The current intended use is with zram (see next patch in series).

Signed-off-by: Zeev Tarantov <zeev.tarantov@gmail.com>
  • Loading branch information
zeevt authored and neobuddy89 committed Apr 24, 2013
1 parent fb312a0 commit 1c17b20
Show file tree
Hide file tree
Showing 8 changed files with 1,040 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/staging/Kconfig
Expand Up @@ -104,6 +104,8 @@ source "drivers/staging/iio/Kconfig"

source "drivers/staging/cs5535_gpio/Kconfig"

source "drivers/staging/snappy/Kconfig"

source "drivers/staging/zram/Kconfig"

source "drivers/staging/zcache/Kconfig"
Expand Down
2 changes: 2 additions & 0 deletions drivers/staging/Makefile
Expand Up @@ -43,6 +43,8 @@ obj-$(CONFIG_VME_BUS) += vme/
obj-$(CONFIG_DX_SEP) += sep/
obj-$(CONFIG_IIO) += iio/
obj-$(CONFIG_CS5535_GPIO) += cs5535_gpio/
obj-$(CONFIG_SNAPPY_COMPRESS) += snappy/
obj-$(CONFIG_SNAPPY_DECOMPRESS) += snappy/
obj-$(CONFIG_ZRAM) += zram/
obj-$(CONFIG_XVMALLOC) += zram/
obj-$(CONFIG_ZCACHE) += zcache/
Expand Down
5 changes: 5 additions & 0 deletions drivers/staging/snappy/Kconfig
@@ -0,0 +1,5 @@
config SNAPPY_COMPRESS
tristate "Google Snappy Compression"

config SNAPPY_DECOMPRESS
tristate "Google Snappy Decompression"
5 changes: 5 additions & 0 deletions drivers/staging/snappy/Makefile
@@ -0,0 +1,5 @@
snappy_compress-objs := csnappy_compress.o
snappy_decompress-objs := csnappy_decompress.o

obj-$(CONFIG_SNAPPY_COMPRESS) += csnappy_compress.o
obj-$(CONFIG_SNAPPY_DECOMPRESS) += csnappy_decompress.o
125 changes: 125 additions & 0 deletions drivers/staging/snappy/csnappy.h
@@ -0,0 +1,125 @@
#ifndef __CSNAPPY_H__
#define __CSNAPPY_H__
/*
File modified for the Linux Kernel by
Zeev Tarantov <zeev.tarantov <at> gmail.com>
*/
#ifdef __cplusplus
extern "C" {
#endif

#define CSNAPPY_VERSION 4

#define CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO 15
#define CSNAPPY_WORKMEM_BYTES (1 << CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO)

/*
* Returns the maximal size of the compressed representation of
* input data that is "source_len" bytes in length;
*/
uint32_t
csnappy_max_compressed_length(uint32_t source_len) __attribute__((const));

/*
* Flat array compression that does not emit the "uncompressed length"
* prefix. Compresses "input" array to the "output" array.
*
* REQUIRES: "input" is at most 32KiB long.
* REQUIRES: "output" points to an array of memory that is at least
* "csnappy_max_compressed_length(input_length)" in size.
* REQUIRES: working_memory has (1 << workmem_bytes_power_of_two) bytes.
* REQUIRES: 9 <= workmem_bytes_power_of_two <= 15.
*
* Returns an "end" pointer into "output" buffer.
* "end - output" is the compressed size of "input".
*/
char*
csnappy_compress_fragment(
const char *input,
const uint32_t input_length,
char *output,
void *working_memory,
const int workmem_bytes_power_of_two);

/*
* REQUIRES: "compressed" must point to an area of memory that is at
* least "csnappy_max_compressed_length(input_length)" bytes in length.
* REQUIRES: working_memory has (1 << workmem_bytes_power_of_two) bytes.
* REQUIRES: 9 <= workmem_bytes_power_of_two <= 15.
*
* Takes the data stored in "input[0..input_length]" and stores
* it in the array pointed to by "compressed".
*
* "*out_compressed_length" is set to the length of the compressed output.
*/
void
csnappy_compress(
const char *input,
uint32_t input_length,
char *compressed,
uint32_t *out_compressed_length,
void *working_memory,
const int workmem_bytes_power_of_two);

/*
* Reads header of compressed data to get stored length of uncompressed data.
* REQUIRES: start points to compressed data.
* REQUIRES: n is length of available compressed data.
*
* Returns SNAPPY_E_HEADER_BAD on error.
* Returns number of bytes read from input on success.
* Stores decoded length into *result.
*/
int
csnappy_get_uncompressed_length(
const char *start,
uint32_t n,
uint32_t *result);

/*
* Safely decompresses all data from array "src" of length "src_len" containing
* entire compressed stream (with header) into array "dst" of size "dst_len".
* REQUIRES: dst_len is at least csnappy_get_uncompressed_length(...).
*
* Iff sucessful, returns CSNAPPY_E_OK.
* If recorded length in header is greater than dst_len, returns
* CSNAPPY_E_OUTPUT_INSUF.
* If compressed data is malformed, does not write more than dst_len into dst.
*/
int
csnappy_decompress(
const char *src,
uint32_t src_len,
char *dst,
uint32_t dst_len);

/*
* Safely decompresses stream src_len bytes long read from src to dst.
* Amount of available space at dst must be provided in *dst_len by caller.
* If compressed stream needs more space, it will not overflow and return
* CSNAPPY_E_OUTPUT_OVERRUN.
* On success, sets *dst_len to actal number of bytes decompressed.
* Iff sucessful, returns CSNAPPY_E_OK.
*/
int
csnappy_decompress_noheader(
const char *src,
uint32_t src_len,
char *dst,
uint32_t *dst_len);

/*
* Return values (< 0 = Error)
*/
#define CSNAPPY_E_OK 0
#define CSNAPPY_E_HEADER_BAD (-1)
#define CSNAPPY_E_OUTPUT_INSUF (-2)
#define CSNAPPY_E_OUTPUT_OVERRUN (-3)
#define CSNAPPY_E_INPUT_NOT_CONSUMED (-4)
#define CSNAPPY_E_DATA_MALFORMED (-5)

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit 1c17b20

Please sign in to comment.