17#include "../common/allocations.h"
18#include "../common/zstd_internal.h"
19#include "../common/portability_macros.h"
21#if defined (__cplusplus)
36#ifndef ZSTD_CWKSP_ASAN_REDZONE_SIZE
37#define ZSTD_CWKSP_ASAN_REDZONE_SIZE 128
42#define ZSTD_CWKSP_ALIGNMENT_BYTES 64
191#if ZSTD_MEMORY_SANITIZER
193 intptr_t
const offset = __msan_test_shadow(ws->
initOnceStart,
196#if defined(ZSTD_MSAN_PRINT)
210 size_t const mask = align - 1;
211 assert((align & mask) == 0);
212 return (size + mask) & ~mask;
230#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
263 size_t const alignBytesMask = alignBytes - 1;
264 size_t const bytes = (alignBytes - ((size_t)ptr & (alignBytesMask))) & alignBytesMask;
265 assert((alignBytes & alignBytesMask) == 0);
266 assert(bytes < alignBytes);
290 DEBUGLOG(5,
"cwksp: reserving %p %zd bytes, %zd bytes remaining",
294 if (alloc < bottom) {
295 DEBUGLOG(4,
"cwksp: alloc failed!");
301 if (alloc < ws->tableValidEnd) {
317 if (phase > ws->
phase) {
327 void *
const objectEnd = (
BYTE *) alloc + bytesToAlign;
328 DEBUGLOG(5,
"reserving table alignment addtl space: %zu", bytesToAlign);
330 "table phase - alignment initial allocation failed!");
349 return (ptr != NULL) && (ws->
workspace <= ptr) && (ptr < ws->workspaceEnd);
363#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
370#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
408 if(ptr && ptr < ws->initOnceStart) {
418#if ZSTD_MEMORY_SANITIZER
419 assert(__msan_test_shadow(ptr, bytes) == -1);
449 if(ws->
phase < phase) {
455 end = (
BYTE *)alloc + bytes;
458 DEBUGLOG(5,
"cwksp: reserving %p table %zd bytes, %zd bytes remaining",
464 DEBUGLOG(4,
"cwksp: table alloc failed!");
470#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
472 __asan_unpoison_memory_region(alloc, bytes);
489 void* end = (
BYTE*)alloc + roundedBytes;
491#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
497 "cwksp: reserving %p object %zd bytes (rounded to %zd), %zd bytes remaining",
504 DEBUGLOG(3,
"cwksp: object alloc failed!");
512#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
517 __asan_unpoison_memory_region(alloc, bytes);
526 DEBUGLOG(4,
"cwksp: ZSTD_cwksp_mark_tables_dirty");
528#if ZSTD_MEMORY_SANITIZER && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
554 DEBUGLOG(4,
"cwksp: ZSTD_cwksp_mark_tables_clean");
567 DEBUGLOG(4,
"cwksp: ZSTD_cwksp_clean_tables");
581 DEBUGLOG(4,
"cwksp: clearing tables!");
583#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
590 __asan_poison_memory_region(ws->
objectEnd, size);
605#if ZSTD_MEMORY_SANITIZER && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
619#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
626 __asan_poison_memory_region(ws->
objectEnd, size);
654 DEBUGLOG(4,
"cwksp: init'ing workspace with %zd bytes", size);
655 assert(((
size_t)start & (
sizeof(
void*)-1)) == 0);
670 DEBUGLOG(4,
"cwksp: creating new workspace with %zd bytes", size);
671 RETURN_ERROR_IF(workspace == NULL, memory_allocation,
"NULL pointer!");
678 DEBUGLOG(4,
"cwksp: freeing workspace");
679#if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
680 if (ptr != NULL && customMem.customFree != NULL) {
736 ZSTD_cwksp* ws,
size_t additionalNeededSpace) {
744#if defined (__cplusplus)
MEM_STATIC void ZSTD_customFree(void *ptr, ZSTD_customMem customMem)
MEM_STATIC void * ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
#define assert(condition)
#define RETURN_ERROR_IF(cond, err,...)
int workspaceOversizedDuration
ZSTD_cwksp_alloc_phase_e phase
ZSTD_cwksp_static_alloc_e isStatic
MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp *ws, size_t size, ZSTD_customMem customMem)
MEM_STATIC size_t ZSTD_cwksp_align(size_t size, size_t const align)
MEM_STATIC void ZSTD_cwksp_mark_tables_clean(ZSTD_cwksp *ws)
MEM_STATIC int ZSTD_cwksp_reserve_failed(const ZSTD_cwksp *ws)
ZSTD_cwksp_static_alloc_e
@ ZSTD_cwksp_dynamic_alloc
@ ZSTD_cwksp_static_alloc
MEM_STATIC void * ZSTD_cwksp_reserve_internal_buffer_space(ZSTD_cwksp *ws, size_t const bytes)
MEM_STATIC void ZSTD_cwksp_move(ZSTD_cwksp *dst, ZSTD_cwksp *src)
MEM_STATIC size_t ZSTD_cwksp_internal_advance_phase(ZSTD_cwksp *ws, ZSTD_cwksp_alloc_phase_e phase)
MEM_STATIC size_t ZSTD_cwksp_used(const ZSTD_cwksp *ws)
#define ZSTD_CWKSP_ALIGNMENT_BYTES
MEM_STATIC void ZSTD_cwksp_clean_tables(ZSTD_cwksp *ws)
MEM_STATIC int ZSTD_cwksp_check_too_large(ZSTD_cwksp *ws, size_t additionalNeededSpace)
MEM_STATIC size_t ZSTD_cwksp_bytes_to_align_ptr(void *ptr, const size_t alignBytes)
MEM_STATIC void ZSTD_cwksp_clear_tables(ZSTD_cwksp *ws)
MEM_STATIC void ZSTD_cwksp_assert_internal_consistency(ZSTD_cwksp *ws)
MEM_STATIC int ZSTD_cwksp_owns_buffer(const ZSTD_cwksp *ws, const void *ptr)
MEM_STATIC int ZSTD_cwksp_check_wasteful(ZSTD_cwksp *ws, size_t additionalNeededSpace)
MEM_STATIC void * ZSTD_cwksp_reserve_aligned_init_once(ZSTD_cwksp *ws, size_t bytes)
MEM_STATIC size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp *ws)
MEM_STATIC void ZSTD_cwksp_bump_oversized_duration(ZSTD_cwksp *ws, size_t additionalNeededSpace)
MEM_STATIC size_t ZSTD_cwksp_slack_space_required(void)
MEM_STATIC void ZSTD_cwksp_mark_tables_dirty(ZSTD_cwksp *ws)
@ ZSTD_cwksp_alloc_objects
@ ZSTD_cwksp_alloc_buffers
@ ZSTD_cwksp_alloc_aligned
@ ZSTD_cwksp_alloc_aligned_init_once
MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp *ws, ZSTD_customMem customMem)
MEM_STATIC void * ZSTD_cwksp_reserve_internal(ZSTD_cwksp *ws, size_t bytes, ZSTD_cwksp_alloc_phase_e phase)
MEM_STATIC BYTE * ZSTD_cwksp_reserve_buffer(ZSTD_cwksp *ws, size_t bytes)
MEM_STATIC int ZSTD_cwksp_estimated_space_within_bounds(const ZSTD_cwksp *const ws, size_t const estimatedSpace)
MEM_STATIC void ZSTD_cwksp_clear(ZSTD_cwksp *ws)
MEM_STATIC void * ZSTD_cwksp_reserve_object(ZSTD_cwksp *ws, size_t bytes)
MEM_STATIC void ZSTD_cwksp_init(ZSTD_cwksp *ws, void *start, size_t size, ZSTD_cwksp_static_alloc_e isStatic)
MEM_STATIC size_t ZSTD_cwksp_aligned_alloc_size(size_t size)
MEM_STATIC size_t ZSTD_cwksp_alloc_size(size_t size)
MEM_STATIC int ZSTD_cwksp_check_available(ZSTD_cwksp *ws, size_t additionalNeededSpace)
MEM_STATIC void * ZSTD_cwksp_initialAllocStart(ZSTD_cwksp *ws)
MEM_STATIC void * ZSTD_cwksp_reserve_table(ZSTD_cwksp *ws, size_t bytes)
#define ZSTD_CWKSP_ASAN_REDZONE_SIZE
MEM_STATIC void * ZSTD_cwksp_reserve_aligned(ZSTD_cwksp *ws, size_t bytes)
MEM_STATIC size_t ZSTD_cwksp_available_space(ZSTD_cwksp *ws)
#define ZSTD_memset(p, v, l)
#define ZSTD_WORKSPACETOOLARGE_MAXDURATION
#define ZSTD_WORKSPACETOOLARGE_FACTOR