Skip to content

Commit

Permalink
prepare for new version of libdeflate with thread-safe alloc function…
Browse files Browse the repository at this point in the history
… mechanism (#1425)

Removes all other calls to internal_exr_alloc if there is a context,
allowing full memory allocator overload without setting global
functions.

Also cleanup code formatting

Signed-off-by: Kimball Thurston <kdt3rd@gmail.com>
  • Loading branch information
kdt3rd committed May 21, 2023
1 parent a00bac5 commit 9e47dfd
Show file tree
Hide file tree
Showing 43 changed files with 633 additions and 578 deletions.
10 changes: 5 additions & 5 deletions cmake/OpenEXRSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ if(NOT TARGET PkgConfig::deflate AND NOT deflate_FOUND)
endif()
include(FetchContent)
FetchContent_Declare(Deflate
GIT_REPOSITORY ${OPENEXR_DEFLATE_REPO}
GIT_TAG ${OPENEXR_DEFLATE_TAG}
GIT_REPOSITORY "${OPENEXR_DEFLATE_REPO}"
GIT_TAG "${OPENEXR_DEFLATE_TAG}"
GIT_SHALLOW ON
)

Expand Down Expand Up @@ -206,7 +206,7 @@ if(NOT TARGET PkgConfig::deflate AND NOT deflate_FOUND)
# lib/gzip_compress.c
# lib/gzip_decompress.c
file(READ ${deflate_SOURCE_DIR}/lib/lib_common.h DEFLATE_HIDE)
string(REPLACE default hidden DEFLATE_HIDE "${DEFLATE_HIDE}")
string(REPLACE "visibility(\"default\")" "visibility(\"hidden\")" DEFLATE_HIDE "${DEFLATE_HIDE}")
string(REPLACE "__declspec(dllexport)" "/**/" DEFLATE_HIDE "${DEFLATE_HIDE}")
file(WRITE ${deflate_SOURCE_DIR}/lib/lib_common.h "${DEFLATE_HIDE}")

Expand Down Expand Up @@ -245,8 +245,8 @@ if(NOT TARGET Imath::Imath AND NOT Imath_FOUND)
endif()
include(FetchContent)
FetchContent_Declare(Imath
GIT_REPOSITORY ${IMATH_REPO}
GIT_TAG ${IMATH_TAG}
GIT_REPOSITORY "${IMATH_REPO}"
GIT_TAG "${IMATH_TAG}"
GIT_SHALLOW ON
)

Expand Down
6 changes: 6 additions & 0 deletions src/lib/OpenEXR/ImfDwaCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,7 @@ DwaCompressor::compress (
{
size_t outSize;
if (EXR_ERR_SUCCESS != exr_compress_buffer(
nullptr,
9, // TODO: use default??? the old call to zlib had 9 hardcoded
_planarUncBuffer[UNKNOWN],
*unknownUncompressedSize,
Expand Down Expand Up @@ -2116,6 +2117,7 @@ DwaCompressor::compress (
size_t sourceLen = *totalAcUncompressedCount * sizeof (unsigned short);
size_t destLen;
if (EXR_ERR_SUCCESS != exr_compress_buffer(
nullptr,
9, // TODO: use default??? the old call to zlib had 9 hardcoded
_packedAcBuffer,
sourceLen,
Expand Down Expand Up @@ -2166,6 +2168,7 @@ DwaCompressor::compress (

size_t dstLen;
if (EXR_ERR_SUCCESS != exr_compress_buffer(
nullptr,
9, // TODO: use default??? the old call to zlib had 9 hardcoded
_rleBuffer,
*rleUncompressedSize,
Expand Down Expand Up @@ -2403,6 +2406,7 @@ DwaCompressor::uncompress (
}

if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
nullptr,
compressedUnknownBuf,
unknownCompressedSize,
_planarUncBuffer[UNKNOWN],
Expand Down Expand Up @@ -2447,6 +2451,7 @@ DwaCompressor::uncompress (
size_t destLen;

if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
nullptr,
compressedAcBuf,
acCompressedSize,
_packedAcBuffer,
Expand Down Expand Up @@ -2518,6 +2523,7 @@ DwaCompressor::uncompress (
size_t dstLen;

if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
nullptr,
compressedRleBuf,
rleCompressedSize,
_rleBuffer,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/OpenEXR/ImfIDManifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ IDManifest::IDManifest (const CompressedIDManifest& compressed)
size_t outSize;
size_t inSize = static_cast<size_t> (compressed._compressedDataSize);
if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
nullptr,
compressed._data,
inSize,
uncomp.data(),
Expand Down Expand Up @@ -1072,6 +1073,7 @@ CompressedIDManifest::CompressedIDManifest (const IDManifest& manifest)
size_t compressedDataSize;
_data = (unsigned char*) malloc (compressedBufferSize);
if (EXR_ERR_SUCCESS != exr_compress_buffer (
nullptr,
-1,
serial.data (),
outputSize,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/OpenEXR/ImfPxr24Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Pxr24Compressor::compress (
size_t outSize = exr_compress_max_buffer_size (inBufferSize);

if (EXR_ERR_SUCCESS != exr_compress_buffer (
nullptr,
-1,
_tmpBuffer,
inBufferSize,
Expand Down Expand Up @@ -371,6 +372,7 @@ Pxr24Compressor::uncompress (
size_t tmpSize = static_cast<size_t> (_maxScanLineSize * _numScanLines);

if (EXR_ERR_SUCCESS != exr_uncompress_buffer(
nullptr,
inPtr,
inSize,
_tmpBuffer,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/OpenEXR/ImfZip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Zip::compress (const char* raw, int rawSize, char* compressed)
//
size_t outSize;
if (EXR_ERR_SUCCESS != exr_compress_buffer (
nullptr,
_zipLevel,
_tmpBuffer,
rawSize,
Expand Down Expand Up @@ -328,6 +329,7 @@ Zip::uncompress (const char* compressed, int compressedSize, char* raw)
{
size_t outSize = 0;
if (EXR_ERR_SUCCESS != exr_uncompress_buffer (
nullptr,
compressed,
(size_t)compressedSize,
_tmpBuffer,
Expand Down
11 changes: 4 additions & 7 deletions src/lib/OpenEXRCore/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ extract_chunk_leader (
rv = ctxt->do_read (
ctxt,
data,
(size_t)ntoread * sizeof (int32_t),
(size_t) ntoread * sizeof (int32_t),
&nextoffset,
NULL,
EXR_MUST_READ_ALL);
Expand Down Expand Up @@ -349,7 +349,7 @@ extract_chunk_leader (
return ctxt->print_error (
ctxt,
EXR_ERR_BAD_CHUNK_LEADER,
"Invalid chunk size reconstructing chunk table: found out of range %"PRId64,
"Invalid chunk size reconstructing chunk table: found out of range %" PRId64,
leaderdata->deep_data[1]);
}
leaderdata->packed_size = leaderdata->deep_packed_size;
Expand Down Expand Up @@ -484,10 +484,7 @@ reconstruct_chunk_table (
chunk_start = curctable[0];
for (int ci = 1; ci < curpart->chunk_count; ++ci)
{
if (curctable[ci] > chunk_start)
{
chunk_start = curctable[ci];
}
if (curctable[ci] > chunk_start) { chunk_start = curctable[ci]; }
}

rv = extract_chunk_size (
Expand Down Expand Up @@ -1266,7 +1263,7 @@ exr_read_tile_chunk_info (
else if (fsize > 0)
{
uint64_t finpos = dataoff + (uint64_t) tdata[4];
if (finpos > (uint64_t)fsize)
if (finpos > (uint64_t) fsize)
{
return pctxt->print_error (
pctxt,
Expand Down
115 changes: 71 additions & 44 deletions src/lib/OpenEXRCore/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
#include "openexr_compression.h"
#include "openexr_base.h"
#include "internal_memory.h"
#include "internal_structs.h"

#include <libdeflate.h>

#if ( \
LIBDEFLATE_VERSION_MAJOR > 1 || \
(LIBDEFLATE_VERSION_MAJOR == 1 && LIBDEFLATE_VERSION_MINOR > 18))
# define EXR_USE_CONFIG_DEFLATE_STRUCT 1
#endif

/* value Aras found to be better trade off of speed vs size */
#define EXR_DEFAULT_ZLIB_COMPRESS_LEVEL 4

/**************************************/

size_t exr_compress_max_buffer_size (size_t in_bytes)
size_t
exr_compress_max_buffer_size (size_t in_bytes)
{
size_t r, extra;

Expand All @@ -24,64 +32,71 @@ size_t exr_compress_max_buffer_size (size_t in_bytes)
* but is unclear if it actually adds that or not
* (see the comment on libdeflate_deflate_compress)
*/
if (r > (SIZE_MAX - 9))
return (size_t)(SIZE_MAX);
if (r > (SIZE_MAX - 9)) return (size_t) (SIZE_MAX);
r += 9;

/*
* old library had uiAdd( uiAdd( in, ceil(in * 0.01) ), 100 )
*/
extra = (in_bytes * (size_t)130);
if (extra < in_bytes)
return (size_t)(SIZE_MAX);
extra /= (size_t)128;
extra = (in_bytes * (size_t) 130);
if (extra < in_bytes) return (size_t) (SIZE_MAX);
extra /= (size_t) 128;

if (extra > (SIZE_MAX - 100))
return (size_t)(SIZE_MAX);
if (extra > (SIZE_MAX - 100)) return (size_t) (SIZE_MAX);

if (extra > r)
r = extra;
if (extra > r) r = extra;
return r;
}

/**************************************/

exr_result_t exr_compress_buffer (
int level,
const void *in,
size_t in_bytes,
void *out,
size_t out_bytes_avail,
size_t *actual_out )
exr_result_t
exr_compress_buffer (
exr_const_context_t ctxt,
int level,
const void* in,
size_t in_bytes,
void* out,
size_t out_bytes_avail,
size_t* actual_out)
{
struct libdeflate_compressor *comp;
struct libdeflate_compressor* comp;
const struct _internal_exr_context* pctxt = EXR_CCTXT (ctxt);
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
struct libdeflate_options opt = {
.sizeof_options = sizeof (struct libdeflate_options),
.malloc_func = pctxt ? pctxt->alloc_fn : internal_exr_alloc,
.free_func = pctxt ? pctxt->free_fn : internal_exr_free};

#else
libdeflate_set_memory_allocator (
pctxt ? pctxt->alloc_fn : internal_exr_alloc,
pctxt ? pctxt->free_fn : internal_exr_free);
#endif

if (level < 0)
{
exr_get_default_zip_compression_level (&level);
/* truly unset anywhere */
if (level < 0)
level = EXR_DEFAULT_ZLIB_COMPRESS_LEVEL;
if (level < 0) level = EXR_DEFAULT_ZLIB_COMPRESS_LEVEL;
}

libdeflate_set_memory_allocator (internal_exr_alloc, internal_exr_free);
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
comp = libdeflate_alloc_compressor_ex (level, &opt);
#else
comp = libdeflate_alloc_compressor (level);
#endif
if (comp)
{
size_t outsz;
outsz = libdeflate_zlib_compress (
comp,
in,
in_bytes,
out,
out_bytes_avail);
outsz =
libdeflate_zlib_compress (comp, in, in_bytes, out, out_bytes_avail);

libdeflate_free_compressor (comp);

if (outsz != 0)
{
if (actual_out)
*actual_out = outsz;
if (actual_out) *actual_out = outsz;
return EXR_ERR_SUCCESS;
}
return EXR_ERR_OUT_OF_MEMORY;
Expand All @@ -91,19 +106,33 @@ exr_result_t exr_compress_buffer (

/**************************************/

exr_result_t exr_uncompress_buffer (
const void *in,
size_t in_bytes,
void *out,
size_t out_bytes_avail,
size_t *actual_out )
exr_result_t
exr_uncompress_buffer (
exr_const_context_t ctxt,
const void* in,
size_t in_bytes,
void* out,
size_t out_bytes_avail,
size_t* actual_out)
{
struct libdeflate_decompressor *decomp;
enum libdeflate_result res;
size_t actual_in_bytes;

libdeflate_set_memory_allocator (internal_exr_alloc, internal_exr_free);
struct libdeflate_decompressor* decomp;
enum libdeflate_result res;
size_t actual_in_bytes;
const struct _internal_exr_context* pctxt = EXR_CCTXT (ctxt);
#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT
struct libdeflate_options opt = {
.sizeof_options = sizeof (struct libdeflate_options),
.malloc_func = pctxt ? pctxt->alloc_fn : internal_exr_alloc,
.free_func = pctxt ? pctxt->free_fn : internal_exr_free};

decomp = libdeflate_alloc_decompressor_ex (&opt);
#else
libdeflate_set_memory_allocator (
pctxt ? pctxt->alloc_fn : internal_exr_alloc,
pctxt ? pctxt->free_fn : internal_exr_free);
decomp = libdeflate_alloc_decompressor ();
#endif

if (decomp)
{
res = libdeflate_zlib_decompress_ex (
Expand All @@ -119,12 +148,10 @@ exr_result_t exr_uncompress_buffer (

if (res == LIBDEFLATE_SUCCESS)
{
if (in_bytes == actual_in_bytes)
return EXR_ERR_SUCCESS;
if (in_bytes == actual_in_bytes) return EXR_ERR_SUCCESS;
/* it's an error to not consume the full buffer, right? */
}
return EXR_ERR_CORRUPT_CHUNK;
}
return EXR_ERR_OUT_OF_MEMORY;
}

5 changes: 1 addition & 4 deletions src/lib/OpenEXRCore/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ process_query_size (
ctxt->file_size =
(inits->size_fn) ((exr_const_context_t) ctxt, ctxt->user_data);
}
else
{
ctxt->file_size = -1;
}
else { ctxt->file_size = -1; }
return EXR_ERR_SUCCESS;
}

Expand Down
Loading

0 comments on commit 9e47dfd

Please sign in to comment.