Added Andi Kleen's Snappy compression C implementation

http://github.com/andikleen/snappy-c
This commit is contained in:
Magnus Edenhill 2013-09-05 23:44:23 +02:00
parent 657e2f78c2
commit 9100fcad6a
5 changed files with 1778 additions and 0 deletions

36
LICENSE.snappy Normal file
View File

@ -0,0 +1,36 @@
######################################################################
# LICENSE.snappy covers files: snappy.c, snappy.h, snappy_compat.h #
# originally retrieved from http://github.com/andikleen/snappy-c #
# git revision 711c52b7ef94c8e5c600571987fbe5769070b884 #
######################################################################
The snappy-c code is under the same license as the original snappy source
Copyright 2011 Intel Corporation All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -7,6 +7,7 @@ DESTDIR?=/usr/local
SRCS= rdkafka.c rdkafka_broker.c rdkafka_msg.c rdkafka_topic.c \
rdkafka_defaultconf.c
SRCS+= rdcrc32.c rdgz.c rdaddr.c rdrand.c rdthread.c rdqueue.c rdlog.c
SRCS+= snappy.c
HDRS= rdkafka.h
OBJS= $(SRCS:.c=.o)
@ -14,6 +15,8 @@ DEPS= ${OBJS:%.o=%.d}
CFLAGS+=-O2 -Wall -Werror -Wfloat-equal -Wpointer-arith -fPIC -I.
CFLAGS+=-g -rdynamic
# Enable iovecs in snappy
CFLAGS+=-DSG
# Profiling
#CFLAGS+=-O0

1608
snappy.c Normal file

File diff suppressed because it is too large Load Diff

39
snappy.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef _LINUX_SNAPPY_H
#define _LINUX_SNAPPY_H 1
#include <stdbool.h>
#include <stddef.h>
/* Only needed for compression. This preallocates the worst case */
struct snappy_env {
unsigned short *hash_table;
void *scratch;
void *scratch_output;
};
struct iovec;
int snappy_init_env(struct snappy_env *env);
int snappy_init_env_sg(struct snappy_env *env, bool sg);
void snappy_free_env(struct snappy_env *env);
int snappy_uncompress_iov(struct iovec *iov_in, int iov_in_len,
size_t input_len, char *uncompressed);
int snappy_uncompress(const char *compressed, size_t n, char *uncompressed);
int snappy_compress(struct snappy_env *env,
const char *input,
size_t input_length,
char *compressed,
size_t *compressed_length);
int snappy_compress_iov(struct snappy_env *env,
struct iovec *iov_in,
int iov_in_len,
size_t input_length,
struct iovec *iov_out,
int *iov_out_len,
size_t *compressed_length);
bool snappy_uncompressed_length(const char *buf, size_t len, size_t *result);
size_t snappy_max_compressed_length(size_t source_len);
#endif

92
snappy_compat.h Normal file
View File

@ -0,0 +1,92 @@
#ifdef __FreeBSD__
# include <sys/endian.h>
#elif defined(__APPLE_CC_) || defined(__MACH__) /* MacOS/X support */
# include <machine/endian.h>
#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
# define htole16(x) (x)
# define le32toh(x) (x)
#elif __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
# define htole16(x) __DARWIN_OSSwapInt16(x)
# define le32toh(x) __DARWIN_OSSwapInt32(x)
#else
# error "Endianness is undefined"
#endif
#else
# include <endian.h>
#endif
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/uio.h>
#if defined(__arm__) && \
!defined(__ARM_ARCH_4__) && \
!defined(__ARM_ARCH_4T__) && \
!defined(__ARM_ARCH_5__) && \
!defined(__ARM_ARCH_5T__) && \
!defined(__ARM_ARCH_5TE__) && \
!defined(__ARM_ARCH_5TEJ__) && \
!defined(__ARM_ARCH_6__) && \
!defined(__ARM_ARCH_6J__) && \
!defined(__ARM_ARCH_6K__) && \
!defined(__ARM_ARCH_6Z__) && \
!defined(__ARM_ARCH_6ZK__) && \
!defined(__ARM_ARCH_6T2__)
#define UNALIGNED64_REALLYS_SLOW 1
#endif
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned u32;
typedef unsigned long long u64;
#define BUG_ON(x) assert(!(x))
#define get_unaligned(x) (*(x))
#define get_unaligned_le32(x) (le32toh(*(u32 *)(x)))
#define put_unaligned(v,x) (*(x) = (v))
#define put_unaligned_le16(v,x) (*(u16 *)(x) = htole16(v))
/* You may want to define this on various ARM architectures */
#ifdef UNALIGNED64_REALLYS_SLOW
static inline u64 get_unaligned64(const void *p)
{
u64 t;
memcpy(&t, p, 8);
return t;
}
static inline u64 put_unaligned64(u64 t, void *p)
{
memcpy(p, &t, 8);
return t;
}
#else
#define get_unaligned64(x) get_unaligned(x)
#define put_unaligned64(x,p) put_unaligned(x,p)
#endif
#define vmalloc(x) malloc(x)
#define vfree(x) free(x)
#define EXPORT_SYMBOL(x)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#define min_t(t,x,y) ((x) < (y) ? (x) : (y))
#define max_t(t,x,y) ((x) > (y) ? (x) : (y))
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define __LITTLE_ENDIAN__ 1
#endif
#define BITS_PER_LONG (__SIZEOF_LONG__ * 8)