Skip to content

Add Functions that Report LZ4F Contexts' Memory Footprints #1595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Functions that Return the Memory Footprint for LZ4F Contexts
  • Loading branch information
felixhandte committed Jun 2, 2025
commit 92a17e9e0d9d171d13bd166106965fa9858d8fa9
16 changes: 16 additions & 0 deletions lib/lz4frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ static int ctxTypeID_to_size(int ctxTypeID) {
}
}

size_t LZ4F_cctx_size(const LZ4F_cctx* cctx) {
if (cctx == NULL) {
return 0;
}
return sizeof(*cctx) + cctx->maxBufferSize + ctxTypeID_to_size(cctx->lz4CtxAlloc);
}

/* LZ4F_compressBegin_internal()
* Note: only accepts @cdict _or_ @dictBuffer as non NULL.
*/
Expand Down Expand Up @@ -1331,6 +1338,15 @@ LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx)
return result;
}

size_t LZ4F_dctx_size(const LZ4F_dctx* dctx) {
if (dctx == NULL) {
return 0;
}
return sizeof(*dctx)
+ (dctx->tmpIn != NULL ? dctx->maxBlockSize + BFSize : 0)
+ (dctx->tmpOutBuffer != NULL ? dctx->maxBufferSize : 0);
}


/*==--- Streaming Decompression operations ---==*/
void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx)
Expand Down
5 changes: 5 additions & 0 deletions lib/lz4frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,11 @@ LZ4FLIB_STATIC_API LZ4F_cctx* LZ4F_createCompressionContext_advanced(LZ4F_Custom
LZ4FLIB_STATIC_API LZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version);
LZ4FLIB_STATIC_API LZ4F_CDict* LZ4F_createCDict_advanced(LZ4F_CustomMem customMem, const void* dictBuffer, size_t dictSize);

/*! Context size inspection : v1.10.1+
* These functions return the total memory footprint of the provided context.
*/
LZ4FLIB_STATIC_API size_t LZ4F_cctx_size(const LZ4F_cctx* cctx);
LZ4FLIB_STATIC_API size_t LZ4F_dctx_size(const LZ4F_dctx* dctx);

#if defined (__cplusplus)
}
Expand Down