Changeset View
Changeset View
Standalone View
Standalone View
sys/contrib/openzfs/module/zstd/zfs_zstd.c
Show First 20 Lines • Show All 176 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* ZSTD memory handlers | * ZSTD memory handlers | ||||
* | * | ||||
* For decompression we use a different handler which also provides fallback | * For decompression we use a different handler which also provides fallback | ||||
* memory allocation in case memory runs out. | * memory allocation in case memory runs out. | ||||
* | * | ||||
* The ZSTD handlers were split up for the most simplified implementation. | * The ZSTD handlers were split up for the most simplified implementation. | ||||
*/ | */ | ||||
#ifndef IN_LIBSA | |||||
static void *zstd_alloc(void *opaque, size_t size); | static void *zstd_alloc(void *opaque, size_t size); | ||||
#endif | |||||
static void *zstd_dctx_alloc(void *opaque, size_t size); | static void *zstd_dctx_alloc(void *opaque, size_t size); | ||||
static void zstd_free(void *opaque, void *ptr); | static void zstd_free(void *opaque, void *ptr); | ||||
#ifndef IN_LIBSA | |||||
/* Compression memory handler */ | /* Compression memory handler */ | ||||
static const ZSTD_customMem zstd_malloc = { | static const ZSTD_customMem zstd_malloc = { | ||||
zstd_alloc, | zstd_alloc, | ||||
zstd_free, | zstd_free, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
#endif | |||||
/* Decompression memory handler */ | /* Decompression memory handler */ | ||||
static const ZSTD_customMem zstd_dctx_malloc = { | static const ZSTD_customMem zstd_dctx_malloc = { | ||||
zstd_dctx_alloc, | zstd_dctx_alloc, | ||||
zstd_free, | zstd_free, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 281 Lines • ▼ Show 20 Lines | if (zstd_earlyabort_pass > 0 && zstd_level >= zstd_cutoff_level && | ||||
if (s_len < actual_abort_size) { | if (s_len < actual_abort_size) { | ||||
ZSTDSTAT_BUMP(zstd_stat_passignored_size); | ZSTDSTAT_BUMP(zstd_stat_passignored_size); | ||||
} | } | ||||
} | } | ||||
keep_trying: | keep_trying: | ||||
return (zfs_zstd_compress(s_start, d_start, s_len, d_len, level)); | return (zfs_zstd_compress(s_start, d_start, s_len, d_len, level)); | ||||
} | } | ||||
#endif | |||||
/* Compress block using zstd */ | /* Compress block using zstd */ | ||||
size_t | size_t | ||||
zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, | zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, | ||||
int level) | int level) | ||||
{ | { | ||||
size_t c_len; | size_t c_len; | ||||
int16_t zstd_level; | int16_t zstd_level; | ||||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, | ||||
* added, differentiating between the versions. | * added, differentiating between the versions. | ||||
*/ | */ | ||||
zfs_set_hdrversion(hdr, ZSTD_VERSION_NUMBER); | zfs_set_hdrversion(hdr, ZSTD_VERSION_NUMBER); | ||||
zfs_set_hdrlevel(hdr, level); | zfs_set_hdrlevel(hdr, level); | ||||
hdr->raw_version_level = BE_32(hdr->raw_version_level); | hdr->raw_version_level = BE_32(hdr->raw_version_level); | ||||
return (c_len + sizeof (*hdr)); | return (c_len + sizeof (*hdr)); | ||||
} | } | ||||
#endif | |||||
/* Decompress block using zstd and return its stored level */ | /* Decompress block using zstd and return its stored level */ | ||||
int | int | ||||
zfs_zstd_decompress_level(void *s_start, void *d_start, size_t s_len, | zfs_zstd_decompress_level(void *s_start, void *d_start, size_t s_len, | ||||
size_t d_len, uint8_t *level) | size_t d_len, uint8_t *level) | ||||
{ | { | ||||
ZSTD_DCtx *dctx; | ZSTD_DCtx *dctx; | ||||
size_t result; | size_t result; | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | |||||
zfs_zstd_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, | zfs_zstd_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, | ||||
int level __maybe_unused) | int level __maybe_unused) | ||||
{ | { | ||||
return (zfs_zstd_decompress_level(s_start, d_start, s_len, d_len, | return (zfs_zstd_decompress_level(s_start, d_start, s_len, d_len, | ||||
NULL)); | NULL)); | ||||
} | } | ||||
#ifndef IN_LIBSA | |||||
/* Allocator for zstd compression context using mempool_allocator */ | /* Allocator for zstd compression context using mempool_allocator */ | ||||
static void * | static void * | ||||
zstd_alloc(void *opaque __maybe_unused, size_t size) | zstd_alloc(void *opaque __maybe_unused, size_t size) | ||||
{ | { | ||||
size_t nbytes = sizeof (struct zstd_kmem) + size; | size_t nbytes = sizeof (struct zstd_kmem) + size; | ||||
struct zstd_kmem *z = NULL; | struct zstd_kmem *z = NULL; | ||||
z = (struct zstd_kmem *)zstd_mempool_alloc(zstd_mempool_cctx, nbytes); | z = (struct zstd_kmem *)zstd_mempool_alloc(zstd_mempool_cctx, nbytes); | ||||
if (!z) { | if (!z) { | ||||
ZSTDSTAT_BUMP(zstd_stat_alloc_fail); | ZSTDSTAT_BUMP(zstd_stat_alloc_fail); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
return ((void*)z + (sizeof (struct zstd_kmem))); | return ((void*)z + (sizeof (struct zstd_kmem))); | ||||
} | } | ||||
#endif | |||||
/* | /* | ||||
* Allocator for zstd decompression context using mempool_allocator with | * Allocator for zstd decompression context using mempool_allocator with | ||||
* fallback to reserved memory if allocation fails | * fallback to reserved memory if allocation fails | ||||
*/ | */ | ||||
static void * | static void * | ||||
zstd_dctx_alloc(void *opaque __maybe_unused, size_t size) | zstd_dctx_alloc(void *opaque __maybe_unused, size_t size) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 205 Lines • Show Last 20 Lines |