From 0d25185671ddbd23d91b814cfd55dd2bc3d9e8d6 Mon Sep 17 00:00:00 2001 From: KyleJHarper Date: Wed, 28 Oct 2015 20:54:51 -0500 Subject: Finished the two example files. Decided to avoid adding anything to lz4.c/h to expose LZ4_compress_generic(). --- examples/basics.c | 8 +-- examples/compressFunctions | Bin 0 -> 68555 bytes examples/compress_functions.c | 117 +++++++++++++++++++++++------------------- lib/lz4.c | 5 -- lib/lz4.h | 7 --- 5 files changed, 69 insertions(+), 68 deletions(-) create mode 100755 examples/compressFunctions diff --git a/examples/basics.c b/examples/basics.c index c8c674a..7c26473 100644 --- a/examples/basics.c +++ b/examples/basics.c @@ -3,8 +3,8 @@ * Copyright : Kyle Harper * License : Follows same licensing as the lz4.c/lz4.h program at any given time. Currently, BSD 2. * Description: Example program to demonstrate the basic usage of the compress/decompress functions within lz4.c/lz4.h. - * The functions you'll likely want are LZ4_compress_default and LZ4_compress_fast. Both of these are documented in - * the lz4.h header file; I guess reading them. + * The functions you'll likely want are LZ4_compress_default and LZ4_decompress_fast. Both of these are documented in + * the lz4.h header file; I recommend reading them. */ /* Includes, for Power! */ @@ -32,7 +32,7 @@ int main(void) { const char *src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; // The compression function needs to know how many bytes of data we're sending. The string above has 57 characters == 57 bytes. const int src_size = 57; - // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound. + // LZ4 provides a function that will tell you the maximum size of compressed output based on input data via LZ4_compressBound(). const int max_dst_size = LZ4_compressBound(src_size); // We will use that size for our destination boundary when allocating space. char *compressed_data = malloc(max_dst_size); @@ -54,7 +54,7 @@ int main(void) { const int compressed_data_size = return_value; compressed_data = (char *)realloc(compressed_data, compressed_data_size); if (compressed_data == NULL) - run_screaming("Failed to re-alloc memeory for compressed_data. Sad :(", 1); + run_screaming("Failed to re-alloc memory for compressed_data. Sad :(", 1); /* Decompression */ // Now that we've successfully compressed the information from *src to *compressed_data, let's do the opposite! We'll create a diff --git a/examples/compressFunctions b/examples/compressFunctions new file mode 100755 index 0000000..1b86f1f Binary files /dev/null and b/examples/compressFunctions differ diff --git a/examples/compress_functions.c b/examples/compress_functions.c index 302f03f..08913ef 100644 --- a/examples/compress_functions.c +++ b/examples/compress_functions.c @@ -5,6 +5,7 @@ * Description: A program to demonstrate the various compression functions involved in when using LZ4_compress_default(). The idea * is to show how each step in the call stack can be used directly, if desired. There is also some benchmarking for * each function to demonstrate the (probably lack of) performance difference when jumping the stack. + * (If you're new to lz4, please read basics.c to understand the fundamentals) * * The call stack (before theoretical compiler optimizations) for LZ4_compress_default is as follows: * LZ4_compress_default @@ -25,11 +26,11 @@ * LZ4_compress_generic() * As the name suggests, this is the generic function that ultimately does most of the heavy lifting. Calling this * directly can help avoid some test cases and branching which might be useful in some implementation-specific - * situations. - * + * situations, but you really need to know what you're doing AND what you're asking lz4 to do! You also need a + * wrapper function because this function isn't exposed with lz4.h. */ -/* Since lz4 compiles with c99 and not gnu/std99 we need to enable posix linking for time.h structs and functions. */ +/* Since lz4 compiles with c99 and not gnu/std99 we need to enable POSIX linking for time.h structs and functions. */ #if __STDC_VERSION__ >= 199901L #define _XOPEN_SOURCE 600 #else @@ -39,29 +40,24 @@ /* Includes, for Power! */ #include "lz4.h" -#include -#include -#include /* for atoi() */ +#include /* for printf() */ +#include /* for exit() */ +#include /* for atoi() memcmp() */ #include /* for uint_types */ #include /* for PRIu64 */ #include /* for clock_gettime() */ +#include /* for setlocale() */ /* We need to know what one billion is for clock timing. */ #define BILLION 1000000000L -/* Create a crude set of test IDs so we can switch on them later (Can't switch() on a char[] or char*. */ +/* Create a crude set of test IDs so we can switch on them later (Can't switch() on a char[] or char*). */ #define ID__LZ4_COMPRESS_DEFAULT 1 #define ID__LZ4_COMPRESS_FAST 2 #define ID__LZ4_COMPRESS_FAST_EXTSTATE 3 #define ID__LZ4_COMPRESS_GENERIC 4 #define ID__LZ4_DECOMPRESS_FAST 5 -/* Copy these to be syntactically accurate to lz4.c */ -typedef enum { notLimited = 0, limitedOutput = 1 } limitedOutput_directive; -typedef enum { byPtr, byU32, byU16 } tableType_t; -typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive; -typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; - /* @@ -96,9 +92,8 @@ uint64_t bench(const char *known_good_dst, const int function_id, const int iter const int acceleration = 1; LZ4_stream_t state; - // Select the right function to perform the benchmark on. For simplicity, start the timer here. We perform 5000 initial loops - // to warm the cache and ensure that dst remains matching to known_good_dst between successive calls. - clock_gettime(CLOCK_MONOTONIC, &start); + // Select the right function to perform the benchmark on. We perform 5000 initial loops to warm the cache and ensure that dst + // remains matching to known_good_dst between successive calls. switch(function_id) { case ID__LZ4_COMPRESS_DEFAULT: printf("Starting benchmark for function: LZ4_compress_default()\n"); @@ -108,6 +103,7 @@ uint64_t bench(const char *known_good_dst, const int function_id, const int iter run_screaming("Couldn't run LZ4_compress_default()... error code received is in exit code.", rv); if (memcmp(known_good_dst, dst, max_dst_size) != 0) run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1); + clock_gettime(CLOCK_MONOTONIC, &start); for (int i=1; i<=iterations; i++) LZ4_compress_default(src, dst, src_size, max_dst_size); break; @@ -120,6 +116,7 @@ uint64_t bench(const char *known_good_dst, const int function_id, const int iter run_screaming("Couldn't run LZ4_compress_fast()... error code received is in exit code.", rv); if (memcmp(known_good_dst, dst, max_dst_size) != 0) run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1); + clock_gettime(CLOCK_MONOTONIC, &start); for (int i=1; i<=iterations; i++) LZ4_compress_fast(src, dst, src_size, max_dst_size, acceleration); break; @@ -132,26 +129,30 @@ uint64_t bench(const char *known_good_dst, const int function_id, const int iter run_screaming("Couldn't run LZ4_compress_fast_extState()... error code received is in exit code.", rv); if (memcmp(known_good_dst, dst, max_dst_size) != 0) run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1); + clock_gettime(CLOCK_MONOTONIC, &start); for (int i=1; i<=iterations; i++) LZ4_compress_fast_extState(&state, src, dst, src_size, max_dst_size, acceleration); break; - case ID__LZ4_COMPRESS_GENERIC: - printf("Starting benchmark for function: LZ4_compress_generic()\n"); - LZ4_resetStream((LZ4_stream_t*)&state); - for(int junk=0; junk