From d174f975d218c8d2c4df79da3b80a11c50c59e5a Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 11 Jul 2022 05:04:41 -0700 Subject: clarify static sizes of states for static allocation --- lib/lz4.c | 11 +++++------ lib/lz4.h | 55 ++++++++++--------------------------------------------- lib/lz4hc.c | 4 +--- lib/lz4hc.h | 35 +++++++++++++++++------------------ 4 files changed, 33 insertions(+), 72 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index 7374f6e..3f468d7 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -686,7 +686,7 @@ typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; } int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } -int LZ4_sizeofState(void) { return LZ4_STREAMSIZE; } +int LZ4_sizeofState(void) { return sizeof(LZ4_stream_t); } /*-**************************************** @@ -1443,7 +1443,7 @@ int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targe LZ4_stream_t* LZ4_createStream(void) { LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); - LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ + LZ4_STATIC_ASSERT(sizeof(LZ4_stream_t) >= sizeof(LZ4_stream_t_internal)); DEBUGLOG(4, "LZ4_createStream %p", lz4s); if (lz4s == NULL) return NULL; LZ4_initStream(lz4s, sizeof(*lz4s)); @@ -2323,9 +2323,8 @@ int LZ4_decompress_fast_doubleDict(const char* source, char* dest, int originalS LZ4_streamDecode_t* LZ4_createStreamDecode(void) { - LZ4_streamDecode_t* lz4s = (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); - LZ4_STATIC_ASSERT(LZ4_STREAMDECODESIZE >= sizeof(LZ4_streamDecode_t_internal)); /* A compilation error here means LZ4_STREAMDECODESIZE is not large enough */ - return lz4s; + LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal)); + return (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); } int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) @@ -2550,7 +2549,7 @@ int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, /* Obsolete Streaming functions */ -int LZ4_sizeofStreamState(void) { return LZ4_STREAMSIZE; } +int LZ4_sizeofStreamState(void) { return sizeof(LZ4_stream_t); } int LZ4_resetStreamState(void* state, char* inputBuffer) { diff --git a/lib/lz4.h b/lib/lz4.h index 1bd7d26..9a896d2 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -623,34 +623,14 @@ typedef struct { /*! LZ4_stream_t : - * Do not use below internal definitions directly ! - * Declare or allocate an LZ4_stream_t instead. - * LZ4_stream_t can also be created using LZ4_createStream(), which is recommended. - * The structure definition can be convenient for static allocation - * (on stack, or as part of larger structure). - * Init this structure with LZ4_initStream() before first use. - * note : only use this definition in association with static linking ! - * this definition is not API/ABI safe, and may change in future versions. - * Note : OS400 pointers are 16 bytes and the compiler adds 8 bytes of padding after - * tableType and 12 bytes after dictSize to ensure the structure is word aligned: - * |========================================================= - * | Offset | Length | Member Name - * |========================================================= - * | 0 | 16384 | hashTable[4096] - * | 16384 | 4 | currentOffset - * | 16388 | 4 | tableType - * | 16392 | 8 | ***PADDING*** - * | 16400 | 16 | dictionary - * | 16416 | 16 | dictCtx - * | 16432 | 4 | dictSize - * | 16436 | 12 | ***PADDING*** - * ========================================================== + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_stream_t object. */ -#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + ((sizeof(void*)==16) ? 64 : 32)) /* static size, for inter-version compatibility */ -#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*)) +#define LZ4_STREAMSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */ union LZ4_stream_u { - void* table[LZ4_STREAMSIZE_VOIDP]; LZ4_stream_t_internal internal_donotuse; + char minStateSize[LZ4_STREAMSIZE]; }; /* previously typedef'd to LZ4_stream_t */ @@ -672,29 +652,14 @@ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size); /*! LZ4_streamDecode_t : - * information structure to track an LZ4 stream during decompression. - * init this structure using LZ4_setStreamDecode() before first use. - * note : only use in association with static linking ! - * this definition is not API/ABI safe, - * and may change in a future version ! - * Note : Same story as LZ4_STREAMSIZE for OS400 in terms of additional padding to - * ensure pointers start on and structures finish on 16 byte boundaries - * |========================================================= - * | Offset | Length | Member Name - * |========================================================= - * | 0 | 16 | externalDict - * | 16 | 4 | extDictSize - * | 20 | 12 | ***PADDING*** - * | 32 | 16 | prefixEnd - * | 48 | 4 | prefixSize - * | 52 | 12 | ***PADDING*** - * ========================================================== + * Never ever use below internal definitions directly ! + * These definitions are not API/ABI safe, and may change in future versions. + * If you need static allocation, declare or allocate an LZ4_streamDecode_t object. */ -#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 4 : 0)) -#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) +#define LZ4_STREAMDECODESIZE 32 union LZ4_streamDecode_u { - unsigned long long table[LZ4_STREAMDECODESIZE_U64]; LZ4_streamDecode_t_internal internal_donotuse; + char minStateSize[LZ4_STREAMDECODESIZE]; } ; /* previously typedef'd to LZ4_streamDecode_t */ diff --git a/lib/lz4hc.c b/lib/lz4hc.c index 99650a6..77b4767 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -1005,8 +1005,6 @@ int LZ4_freeStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr) LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size) { LZ4_streamHC_t* const LZ4_streamHCPtr = (LZ4_streamHC_t*)buffer; - /* if compilation fails here, LZ4_STREAMHCSIZE must be increased */ - LZ4_STATIC_ASSERT(sizeof(LZ4HC_CCtx_internal) <= LZ4_STREAMHCSIZE); DEBUGLOG(4, "LZ4_initStreamHC(%p, %u)", buffer, (unsigned)size); /* check conditions */ if (buffer == NULL) return NULL; @@ -1205,7 +1203,7 @@ int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* ctx, const char* src, /* Deprecated streaming functions */ -int LZ4_sizeofStreamStateHC(void) { return LZ4_STREAMHCSIZE; } +int LZ4_sizeofStreamStateHC(void) { return sizeof(LZ4_streamHC_t); } /* state is presumed correctly sized, aka >= sizeof(LZ4_streamHC_t) * @return : 0 on success, !=0 if error */ diff --git a/lib/lz4hc.h b/lib/lz4hc.h index 6176457..bead076 100644 --- a/lib/lz4hc.h +++ b/lib/lz4hc.h @@ -217,37 +217,36 @@ struct LZ4HC_CCtx_internal }; -/* Do not use these definitions directly ! +/* Never ever use these definitions directly ! * Declare or allocate an LZ4_streamHC_t instead. * Note : OS400 uses 16 byte pointers and so the structure size is larger than other * platforms * |=========================================================== - * | Offset | Length | Member Name + * | Offset | Length | Member Name * |=========================================================== - * | 0 | 131072 | hashTable[32768] + * | 0 | 131072 | hashTable[32768] * | 131072 | 131072 | chainTable[65536] - * | 262144 | 16 | end - * | 262160 | 16 | base - * | 262176 | 16 | dictBase - * | 262192 | 4 | dictLimit - * | 262196 | 4 | lowLimit - * | 262200 | 4 | nextToUpdate - * | 262204 | 2 | compressionLevel - * | 262206 | 1 | favorDecSpeed - * | 262207 | 1 | dirty - * | 262208 | 16 | dictCtx + * | 262144 | 16 | end + * | 262160 | 16 | base + * | 262176 | 16 | dictBase + * | 262192 | 4 | dictLimit + * | 262196 | 4 | lowLimit + * | 262200 | 4 | nextToUpdate + * | 262204 | 2 | compressionLevel + * | 262206 | 1 | favorDecSpeed + * | 262207 | 1 | dirty + * | 262208 | 16 | dictCtx * ============================================================ */ -#define LZ4_STREAMHCSIZE (262200 + ((sizeof(void*)==16) ? 24 : 0)) /* static size, for inter-version compatibility */ -#define LZ4_STREAMHCSIZE_VOIDP (LZ4_STREAMHCSIZE / sizeof(void*)) +#define LZ4_STREAMHCSIZE 262200 /* static size, for inter-version compatibility */ union LZ4_streamHC_u { - void* table[LZ4_STREAMHCSIZE_VOIDP]; LZ4HC_CCtx_internal internal_donotuse; + char minStateSize[LZ4_STREAMHCSIZE]; }; /* previously typedef'd to LZ4_streamHC_t */ /* LZ4_streamHC_t : * This structure allows static allocation of LZ4 HC streaming state. - * This can be used to allocate statically, on state, or as part of a larger structure. + * This can be used to allocate statically on stack, or as part of a larger structure. * * Such state **must** be initialized using LZ4_initStreamHC() before first use. * @@ -262,7 +261,7 @@ union LZ4_streamHC_u { * Required before first use of a statically allocated LZ4_streamHC_t. * Before v1.9.0 : use LZ4_resetStreamHC() instead */ -LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC (void* buffer, size_t size); +LZ4LIB_API LZ4_streamHC_t* LZ4_initStreamHC(void* buffer, size_t size); /*-************************************ -- cgit v0.12