Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/tls-earlyentropy]: src LZF in the kernel. As an entropy estimator for n...



details:   https://anonhg.NetBSD.org/src/rev/230433403adf
branches:  tls-earlyentropy
changeset: 795262:230433403adf
user:      tls <tls%NetBSD.org@localhost>
date:      Mon Apr 07 01:10:55 2014 +0000

description:
LZF in the kernel.  As an entropy estimator for now but it's very small, and
we could use it for ipcomp, for hibernation, for paging, for core dumps, etc.

diffstat:

 external/bsd/liblzf/dist/lzfP.h  |  40 ++++++++++++++++------------------------
 external/bsd/liblzf/dist/lzf_c.c |  17 +++++++++++------
 external/bsd/liblzf/dist/lzf_d.c |  18 ++++++++++++++----
 sys/lib/libkern/Makefile.libkern |   5 ++++-
 sys/lib/libkern/libkern.h        |  20 +++++++++++++++++++-
 5 files changed, 64 insertions(+), 36 deletions(-)

diffs (205 lines):

diff -r ba36265fa7b2 -r 230433403adf external/bsd/liblzf/dist/lzfP.h
--- a/external/bsd/liblzf/dist/lzfP.h   Sun Apr 06 15:59:42 2014 +0000
+++ b/external/bsd/liblzf/dist/lzfP.h   Mon Apr 07 01:10:55 2014 +0000
@@ -37,22 +37,21 @@
 #ifndef LZFP_h
 #define LZFP_h
 
-#define STANDALONE 1 /* at the moment, this is ok. */
-
-#ifndef STANDALONE
-# include "lzf.h"
+#if !defined(_KERNEL) && !defined(_STANDALONE)
+#include <sys/types.h>
+#include <inttypes.h>
 #endif
 
 /*
- * Size of hashtable is (1 << HLOG) * sizeof (char *)
+ * Size of hashtable is (1 << LZF_HLOG) * sizeof (char *)
  * decompression is independent of the hash table size
  * the difference between 15 and 14 is very small
  * for small blocks (and 14 is usually a bit faster).
- * For a low-memory/faster configuration, use HLOG == 13;
+ * For a low-memory/faster configuration, use LZF_HLOG == 13;
  * For best compression, use 15 or 16 (or more, up to 23).
  */
-#ifndef HLOG
-# define HLOG 16
+#ifndef LZF_HLOG
+# define LZF_HLOG 16
 #endif
 
 /*
@@ -77,9 +76,12 @@
 
 /*
  * Unconditionally aligning does not cost very much, so do it if unsure
+ *
+ * In fact, on modern x86 processors, strict alignment is faster whether
+ * in 32 or 64 bit mode.
  */
-#ifndef STRICT_ALIGN
-# define STRICT_ALIGN !(defined(__i386) || defined (__amd64))
+#ifndef STRICT_ALIGN   
+# define STRICT_ALIGN 1 /* !(defined(__i386) || defined (__amd64)) */
 #endif
 
 /*
@@ -124,21 +126,11 @@
 /*****************************************************************************/
 /* nothing should be changed below */
 
-typedef unsigned char u8;
-
-typedef const u8 *LZF_STATE[1 << (HLOG)];
+typedef uint8_t u8;
+typedef uint16_t u16;
 
-#if !STRICT_ALIGN
-/* for unaligned accesses we need a 16 bit datatype. */
-# include <limits.h>
-# if USHRT_MAX == 65535
-    typedef unsigned short u16;
-# elif UINT_MAX == 65535
-    typedef unsigned int u16;
-# else
-#  undef STRICT_ALIGN
-#  define STRICT_ALIGN 1
-# endif
+#if !defined(_KERNEL) && !defined(STANDALONE)
+typedef const u8 *LZF_STATE[1 << (LZF_HLOG)];
 #endif
 
 #if ULTRA_FAST
diff -r ba36265fa7b2 -r 230433403adf external/bsd/liblzf/dist/lzf_c.c
--- a/external/bsd/liblzf/dist/lzf_c.c  Sun Apr 06 15:59:42 2014 +0000
+++ b/external/bsd/liblzf/dist/lzf_c.c  Mon Apr 07 01:10:55 2014 +0000
@@ -34,9 +34,14 @@
  * either the BSD or the GPL.
  */
 
+#if defined(_KERNEL) || defined (_STANDALONE)
+#include <lib/libkern/libkern.h>
+#include "lzfP.h"
+#else
 #include "lzf.h"
+#endif
 
-#define HSIZE (1 << (HLOG))
+#define HSIZE (1 << (LZF_HLOG))
 
 /*
  * don't play with this unless you benchmark!
@@ -48,16 +53,16 @@
 # define FRST(p) (((p[0]) << 8) | p[1])
 # define NEXT(v,p) (((v) << 8) | p[2])
 # if ULTRA_FAST
-#  define IDX(h) ((( h             >> (3*8 - HLOG)) - h  ) & (HSIZE - 1))
+#  define IDX(h) ((( h             >> (3*8 - LZF_HLOG)) - h  ) & (HSIZE - 1))
 # elif VERY_FAST
-#  define IDX(h) ((( h             >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
+#  define IDX(h) ((( h             >> (3*8 - LZF_HLOG)) - h*5) & (HSIZE - 1))
 # else
-#  define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
+#  define IDX(h) ((((h ^ (h << 5)) >> (3*8 - LZF_HLOG)) - h*5) & (HSIZE - 1))
 # endif
 #endif
 /*
  * IDX works because it is very similar to a multiplicative hash, e.g.
- * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
+ * ((h * 57321 >> (3*8 - LZF_HLOG)) & (HSIZE - 1))
  * the latter is also quite fast on newer CPUs, and compresses similarly.
  *
  * the next one is also quite good, albeit slow ;)
@@ -147,7 +152,7 @@
 #endif
           && (off = ip - ref - 1) < MAX_OFF
           && ip + 4 < in_end
-          && ref > (u8 *)in_data
+          && ref > (const u8 *)in_data
 #if STRICT_ALIGN
           && ref[0] == ip[0]
           && ref[1] == ip[1]
diff -r ba36265fa7b2 -r 230433403adf external/bsd/liblzf/dist/lzf_d.c
--- a/external/bsd/liblzf/dist/lzf_d.c  Sun Apr 06 15:59:42 2014 +0000
+++ b/external/bsd/liblzf/dist/lzf_d.c  Mon Apr 07 01:10:55 2014 +0000
@@ -34,13 +34,23 @@
  * either the BSD or the GPL.
  */
 
+#if defined(_KERNEL) || defined (_STANDALONE)
+#include <lib/libkern/libkern.h>
+#include <sys/systm.h>
+#include "lzfP.h"
+#else
 #include "lzf.h"
+#endif
 
-#if AVOID_ERRNO
-# define SET_ERRNO(n)
+#ifdef _KERNEL
+# define SET_ERRNO(n) panic("lzf decompression failure: %s", #n)
 #else
-# include <errno.h>
-# define SET_ERRNO(n) errno = (n)
+# ifdef AVOID_ERRNO
+#  define SET_ERRNO(n)
+# else
+#  include <errno.h>
+#  define SET_ERRNO(n) errno = (n)
+# endif
 #endif
 
 #if (__i386 || __amd64) && __GNUC__ >= 3
diff -r ba36265fa7b2 -r 230433403adf sys/lib/libkern/Makefile.libkern
--- a/sys/lib/libkern/Makefile.libkern  Sun Apr 06 15:59:42 2014 +0000
+++ b/sys/lib/libkern/Makefile.libkern  Mon Apr 07 01:10:55 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.libkern,v 1.32 2014/03/12 00:22:53 pooka Exp $
+#      $NetBSD: Makefile.libkern,v 1.32.2.1 2014/04/07 01:10:55 tls Exp $
 
 # 
 # Variable definitions for libkern.  
@@ -94,6 +94,9 @@
 SRCS+= cdbr.c
 SRCS+= mi_vector_hash.c
 
+.PATH: ${NETBSDSRCDIR}/external/bsd/liblzf/dist
+SRCS+= lzf_c.c lzf_d.c
+
 # Files to clean up
 CLEANFILES+= lib${LIB}.o lib${LIB}.po
 
diff -r ba36265fa7b2 -r 230433403adf sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Sun Apr 06 15:59:42 2014 +0000
+++ b/sys/lib/libkern/libkern.h Mon Apr 07 01:10:55 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: libkern.h,v 1.113 2014/02/27 18:05:07 joerg Exp $      */
+/*     $NetBSD: libkern.h,v 1.113.2.1 2014/04/07 01:10:55 tls Exp $    */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -379,4 +379,22 @@
 
 void   *explicit_memset(void *, int, size_t);
 int    consttime_memequal(const void *, const void *, size_t);
+
+/*
+ * LZF hashtable/state size: on uncompressible data and on a system with
+ * a sufficiently large d-cache, a larger table produces a considerable
+ * speed benefit.  On systems with small memory and caches, however...
+ */
+#if defined(__vax__) || defined(__m68k__)
+#define LZF_HLOG 14
+#else
+#define LZF_HLOG 15
+#endif
+typedef const uint8_t *LZF_STATE[1 << LZF_HLOG];
+
+unsigned int lzf_compress_r (const void *const, unsigned int, void *,
+                            unsigned int, LZF_STATE);
+unsigned int lzf_decompress (const void *const, unsigned int, void *,
+                            unsigned int);
+
 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */



Home | Main Index | Thread Index | Old Index