ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
Static Context API

Caller-allocated, fixed-footprint compression / decompression contexts. More...

Functions

size_t zxc_static_cctx_workspace_size (const size_t block_size, const int level)
 Exact size of a static compression workspace.
zxc_cctx * zxc_init_static_cctx (void *workspace, const size_t workspace_size, const size_t block_size, const int level)
 Initialises a compression context inside a caller-supplied workspace.
size_t zxc_static_dctx_workspace_size (const size_t block_size)
 Exact size of a static decompression workspace.
zxc_dctx * zxc_init_static_dctx (void *workspace, const size_t workspace_size, const size_t block_size)
 Initialises a decompression context inside a caller-supplied workspace.

Detailed Description

Caller-allocated, fixed-footprint compression / decompression contexts.

Mirrors the Reusable Context API, but the whole context (handle + persistent buffers) lives in one buffer the caller allocates and owns. Required wherever the library must not touch the host allocator on the hot path: Linux kernel filesystems (one workspace per mount, from vmalloc / kmalloc up front), heapless embedded targets (.bss or stack workspace), sandboxed runtimes on a fixed memory budget.

The trade-off: the workspace is pinned to one block_size and level at init time and cannot grow afterwards, so a workload mixing block sizes must size for the largest one up front.

Typical usage
void *ws = aligned_alloc(64, ws_sz); // or kmalloc, vmalloc, .bss
zxc_cctx *cctx = zxc_init_static_cctx(ws, ws_sz, 64 * 1024, ZXC_LEVEL_DEFAULT);
for (each block) zxc_compress_cctx(cctx, src, n, dst, cap);
// zxc_free_cctx is a no-op on a static cctx; the caller owns @c ws.
free(ws);
struct zxc_cctx_s zxc_cctx
Opaque reusable compression context (see zxc_create_cctx).
Definition zxc_buffer.h:370
int64_t zxc_compress_cctx(zxc_cctx *cctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
Compresses data using a reusable context.
@ ZXC_LEVEL_DEFAULT
Recommended default: better ratio and decode speed than LZ4.
size_t zxc_static_cctx_workspace_size(const size_t block_size, const int level)
Exact size of a static compression workspace.
zxc_cctx * zxc_init_static_cctx(void *workspace, const size_t workspace_size, const size_t block_size, const int level)
Initialises a compression context inside a caller-supplied workspace.

Function Documentation

◆ zxc_init_static_cctx()

zxc_cctx * zxc_init_static_cctx ( void * workspace,
const size_t workspace_size,
const size_t block_size,
const int level )

Initialises a compression context inside a caller-supplied workspace.

workspace_size must be at least zxc_static_cctx_workspace_size for the same block_size and level. The workspace must be cache-line (64-byte) aligned and must outlive the returned handle. The caller owns it; zxc_free_cctx is a no-op on this handle.

Pinned parameters
The workspace is carved for block_size and level, so zxc_cctx_set_param() refuses another ZXC_C_BLOCK_SIZE (ZXC_ERROR_BAD_BLOCK_SIZE) and a raise into ZXC_LEVEL_DENSITY when level was below it (ZXC_ERROR_BAD_LEVEL, as the optimal-parser scratch is absent). Lower levels, checksum and seek table are honoured without re-partitioning. Dictionaries are rejected by zxc_cctx_set_dict(): ZXC_ERROR_DICT_UNSUPPORTED.
Parameters
[in,out]workspaceCaller-allocated buffer, cache-line aligned.
[in]workspace_sizeCapacity of workspace in bytes.
[in]block_sizeBlock size the workspace was sized for.
[in]levelLevel the workspace was sized for, 1 to 7.
Returns
Handle pointing inside workspace, or NULL if the workspace is too small or a parameter is invalid.

Definition at line 2164 of file zxc_dispatch.c.

References zxc_cctx_compute_workspace_size(), zxc_cctx_init_in_workspace(), zxc_cparams_defaults(), ZXC_LEVEL_ULTRA, ZXC_OK, and zxc_validate_block_size().

◆ zxc_init_static_dctx()

zxc_dctx * zxc_init_static_dctx ( void * workspace,
const size_t workspace_size,
const size_t block_size )

Initialises a decompression context inside a caller-supplied workspace.

workspace_size must be at least zxc_static_dctx_workspace_size for the same block_size. The workspace must be cache-line aligned and must outlive the returned handle. The caller owns it; zxc_free_dctx is a no-op on this handle.

Locked block size
block_size is pinned at init time: an archive whose header declares a different block_size is rejected with ZXC_ERROR_BAD_BLOCK_SIZE; a block larger than it never decodes through the block API (see zxc_decompress_block() for the codes).
No dictionary
Any dictionary is rejected with ZXC_ERROR_DICT_UNSUPPORTED, since the workspace has no room for the prefix.
Parameters
[in,out]workspaceCaller-allocated buffer, cache-line aligned.
[in]workspace_sizeCapacity of workspace in bytes.
[in]block_sizeBlock size the decoder will accept.
Returns
Handle pointing inside workspace, or NULL if the workspace is too small or block_size is invalid.

Definition at line 2206 of file zxc_dispatch.c.

References zxc_cctx_compute_workspace_size(), zxc_cctx_init_in_workspace(), ZXC_OK, and zxc_validate_block_size().

◆ zxc_static_cctx_workspace_size()

size_t zxc_static_cctx_workspace_size ( const size_t block_size,
const int level )

Exact size of a static compression workspace.

Sums the opaque zxc_cctx wrapper and every persistent sub-buffer the library partitions out of it (hash tables, chain table, sequence buffers, literal scratch, plus the optimal-parser scratch at ZXC_LEVEL_DENSITY). The workspace must be at least cache-line aligned, so round up for posix_memalign / aligned_alloc.

Parameters
[in]block_sizeBlock size in bytes (power of two in [ZXC_BLOCK_SIZE_MIN, ZXC_BLOCK_SIZE_MAX]).
[in]levelCompression level (1..7); levels at or above ZXC_LEVEL_DENSITY add the optimal-parser scratch (~8.125 x block_size).
Returns
Workspace size in bytes, or 0 if either argument is invalid.

Exact size of a static compression workspace.

Public API; see zxc_buffer.h. Sum of the cache-line-aligned handle header and the persistent buffer that zxc_init_static_cctx carves for the given block_size / level. Performs no allocation.

Definition at line 2156 of file zxc_dispatch.c.

References zxc_cctx_compute_workspace_size(), ZXC_LEVEL_ULTRA, and zxc_validate_block_size().

◆ zxc_static_dctx_workspace_size()

size_t zxc_static_dctx_workspace_size ( const size_t block_size)

Exact size of a static decompression workspace.

Unlike the compression variant this is independent of the archive's level: lit_buffer is always provisioned worst-case, because the decoder cannot know a block's literal encoding until it reads that block's header.

Parameters
[in]block_sizeLargest block size the decoder will meet (same constraints as everywhere else).
Returns
Workspace size in bytes, or 0 if block_size is invalid.

Exact size of a static decompression workspace.

Public API; see zxc_buffer.h. Sum of the cache-line-aligned handle header and the persistent buffer that zxc_init_static_dctx carves for the given block_size. Performs no allocation.

Definition at line 2199 of file zxc_dispatch.c.

References zxc_cctx_compute_workspace_size(), and zxc_validate_block_size().