Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
zstd_cwksp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11#ifndef ZSTD_CWKSP_H
12#define ZSTD_CWKSP_H
13
14/*-*************************************
15* Dependencies
16***************************************/
18
19#if defined (__cplusplus)
20extern "C" {
21#endif
22
23/*-*************************************
24* Constants
25***************************************/
26
27/* Since the workspace is effectively its own little malloc implementation /
28 * arena, when we run under ASAN, we should similarly insert redzones between
29 * each internal element of the workspace, so ASAN will catch overruns that
30 * reach outside an object but that stay inside the workspace.
31 *
32 * This defines the size of that redzone.
33 */
34#ifndef ZSTD_CWKSP_ASAN_REDZONE_SIZE
35#define ZSTD_CWKSP_ASAN_REDZONE_SIZE 128
36#endif
37
38
39/* Set our tables and aligneds to align by 64 bytes */
40#define ZSTD_CWKSP_ALIGNMENT_BYTES 64
41
42/*-*************************************
43* Structures
44***************************************/
50
60
159
160/*-*************************************
161* Functions
162***************************************/
163
165
167 (void)ws;
168 assert(ws->workspace <= ws->objectEnd);
169 assert(ws->objectEnd <= ws->tableEnd);
170 assert(ws->objectEnd <= ws->tableValidEnd);
171 assert(ws->tableEnd <= ws->allocStart);
172 assert(ws->tableValidEnd <= ws->allocStart);
173 assert(ws->allocStart <= ws->workspaceEnd);
174}
175
179MEM_STATIC size_t ZSTD_cwksp_align(size_t size, size_t const align) {
180 size_t const mask = align - 1;
181 assert((align & mask) == 0);
182 return (size + mask) & ~mask;
183}
184
198 if (size == 0)
199 return 0;
200#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
202#else
203 return size;
204#endif
205}
206
214
220 /* For alignment, the wksp will always allocate an additional n_1=[1, 64] bytes
221 * to align the beginning of tables section, as well as another n_2=[0, 63] bytes
222 * to align the beginning of the aligned section.
223 *
224 * n_1 + n_2 == 64 bytes if the cwksp is freshly allocated, due to tables and
225 * aligneds being sized in multiples of 64 bytes.
226 */
227 size_t const slackSpace = ZSTD_CWKSP_ALIGNMENT_BYTES;
228 return slackSpace;
229}
230
231
236MEM_STATIC size_t ZSTD_cwksp_bytes_to_align_ptr(void* ptr, const size_t alignBytes) {
237 size_t const alignBytesMask = alignBytes - 1;
238 size_t const bytes = (alignBytes - ((size_t)ptr & (alignBytesMask))) & alignBytesMask;
239 assert((alignBytes & alignBytesMask) == 0);
241 return bytes;
242}
243
251MEM_STATIC void*
253{
254 void* const alloc = (BYTE*)ws->allocStart - bytes;
255 void* const bottom = ws->tableEnd;
256 DEBUGLOG(5, "cwksp: reserving %p %zd bytes, %zd bytes remaining",
259 assert(alloc >= bottom);
260 if (alloc < bottom) {
261 DEBUGLOG(4, "cwksp: alloc failed!");
262 ws->allocFailed = 1;
263 return NULL;
264 }
265 /* the area is reserved from the end of wksp.
266 * If it overlaps with tableValidEnd, it voids guarantees on values' range */
267 if (alloc < ws->tableValidEnd) {
268 ws->tableValidEnd = alloc;
269 }
270 ws->allocStart = alloc;
271 return alloc;
272}
273
279MEM_STATIC size_t
281{
282 assert(phase >= ws->phase);
283 if (phase > ws->phase) {
284 /* Going from allocating objects to allocating buffers */
286 phase >= ZSTD_cwksp_alloc_buffers) {
287 ws->tableValidEnd = ws->objectEnd;
288 }
289
290 /* Going from allocating buffers to allocating aligneds/tables */
292 phase >= ZSTD_cwksp_alloc_aligned) {
293 { /* Align the start of the "aligned" to 64 bytes. Use [1, 64] bytes. */
294 size_t const bytesToAlign =
296 DEBUGLOG(5, "reserving aligned alignment addtl space: %zu", bytesToAlign);
299 memory_allocation, "aligned phase - alignment initial allocation failed!");
300 }
301 { /* Align the start of the tables to 64 bytes. Use [0, 63] bytes */
302 void* const alloc = ws->objectEnd;
303 size_t const bytesToAlign = ZSTD_cwksp_bytes_to_align_ptr(alloc, ZSTD_CWKSP_ALIGNMENT_BYTES);
304 void* const objectEnd = (BYTE*)alloc + bytesToAlign;
305 DEBUGLOG(5, "reserving table alignment addtl space: %zu", bytesToAlign);
306 RETURN_ERROR_IF(objectEnd > ws->workspaceEnd, memory_allocation,
307 "table phase - alignment initial allocation failed!");
308 ws->objectEnd = objectEnd;
309 ws->tableEnd = objectEnd; /* table area starts being empty */
310 if (ws->tableValidEnd < ws->tableEnd) {
311 ws->tableValidEnd = ws->tableEnd;
312 } } }
313 ws->phase = phase;
315 }
316 return 0;
317}
318
322MEM_STATIC int ZSTD_cwksp_owns_buffer(const ZSTD_cwksp* ws, const void* ptr)
323{
324 return (ptr != NULL) && (ws->workspace <= ptr) && (ptr <= ws->workspaceEnd);
325}
326
330MEM_STATIC void*
332{
333 void* alloc;
334 if (ZSTD_isError(ZSTD_cwksp_internal_advance_phase(ws, phase)) || bytes == 0) {
335 return NULL;
336 }
337
338#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
339 /* over-reserve space */
341#endif
342
344
345#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
346 /* Move alloc so there's ZSTD_CWKSP_ASAN_REDZONE_SIZE unused space on
347 * either size. */
348 if (alloc) {
349 alloc = (BYTE *)alloc + ZSTD_CWKSP_ASAN_REDZONE_SIZE;
351 __asan_unpoison_memory_region(alloc, bytes);
352 }
353 }
354#endif
355
356 return alloc;
357}
358
366
377
384{
386 void* alloc;
387 void* end;
388 void* top;
389
391 return NULL;
392 }
393 alloc = ws->tableEnd;
394 end = (BYTE *)alloc + bytes;
395 top = ws->allocStart;
396
397 DEBUGLOG(5, "cwksp: reserving %p table %zd bytes, %zd bytes remaining",
399 assert((bytes & (sizeof(U32)-1)) == 0);
401 assert(end <= top);
402 if (end > top) {
403 DEBUGLOG(4, "cwksp: table alloc failed!");
404 ws->allocFailed = 1;
405 return NULL;
406 }
407 ws->tableEnd = end;
408
409#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
411 __asan_unpoison_memory_region(alloc, bytes);
412 }
413#endif
414
416 assert(((size_t)alloc & (ZSTD_CWKSP_ALIGNMENT_BYTES-1))== 0);
417 return alloc;
418}
419
425{
426 size_t const roundedBytes = ZSTD_cwksp_align(bytes, sizeof(void*));
427 void* alloc = ws->objectEnd;
428 void* end = (BYTE*)alloc + roundedBytes;
429
430#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
431 /* over-reserve space */
432 end = (BYTE *)end + 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
433#endif
434
435 DEBUGLOG(4,
436 "cwksp: reserving %p object %zd bytes (rounded to %zd), %zd bytes remaining",
437 alloc, bytes, roundedBytes, ZSTD_cwksp_available_space(ws) - roundedBytes);
438 assert((size_t)alloc % ZSTD_ALIGNOF(void*) == 0);
439 assert(bytes % ZSTD_ALIGNOF(void*) == 0);
441 /* we must be in the first phase, no advance is possible */
442 if (ws->phase != ZSTD_cwksp_alloc_objects || end > ws->workspaceEnd) {
443 DEBUGLOG(3, "cwksp: object alloc failed!");
444 ws->allocFailed = 1;
445 return NULL;
446 }
447 ws->objectEnd = end;
448 ws->tableEnd = end;
449 ws->tableValidEnd = end;
450
451#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
452 /* Move alloc so there's ZSTD_CWKSP_ASAN_REDZONE_SIZE unused space on
453 * either size. */
454 alloc = (BYTE*)alloc + ZSTD_CWKSP_ASAN_REDZONE_SIZE;
456 __asan_unpoison_memory_region(alloc, bytes);
457 }
458#endif
459
460 return alloc;
461}
462
464{
465 DEBUGLOG(4, "cwksp: ZSTD_cwksp_mark_tables_dirty");
466
467#if ZSTD_MEMORY_SANITIZER && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
468 /* To validate that the table re-use logic is sound, and that we don't
469 * access table space that we haven't cleaned, we re-"poison" the table
470 * space every time we mark it dirty. */
471 {
472 size_t size = (BYTE*)ws->tableValidEnd - (BYTE*)ws->objectEnd;
473 assert(__msan_test_shadow(ws->objectEnd, size) == -1);
474 __msan_poison(ws->objectEnd, size);
475 }
476#endif
477
478 assert(ws->tableValidEnd >= ws->objectEnd);
479 assert(ws->tableValidEnd <= ws->allocStart);
480 ws->tableValidEnd = ws->objectEnd;
482}
483
485 DEBUGLOG(4, "cwksp: ZSTD_cwksp_mark_tables_clean");
486 assert(ws->tableValidEnd >= ws->objectEnd);
487 assert(ws->tableValidEnd <= ws->allocStart);
488 if (ws->tableValidEnd < ws->tableEnd) {
489 ws->tableValidEnd = ws->tableEnd;
490 }
492}
493
498 DEBUGLOG(4, "cwksp: ZSTD_cwksp_clean_tables");
499 assert(ws->tableValidEnd >= ws->objectEnd);
500 assert(ws->tableValidEnd <= ws->allocStart);
501 if (ws->tableValidEnd < ws->tableEnd) {
503 }
505}
506
512 DEBUGLOG(4, "cwksp: clearing tables!");
513
514#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
515 /* We don't do this when the workspace is statically allocated, because
516 * when that is the case, we have no capability to hook into the end of the
517 * workspace's lifecycle to unpoison the memory.
518 */
520 size_t size = (BYTE*)ws->tableValidEnd - (BYTE*)ws->objectEnd;
521 __asan_poison_memory_region(ws->objectEnd, size);
522 }
523#endif
524
525 ws->tableEnd = ws->objectEnd;
527}
528
534 DEBUGLOG(4, "cwksp: clearing!");
535
536#if ZSTD_MEMORY_SANITIZER && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
537 /* To validate that the context re-use logic is sound, and that we don't
538 * access stuff that this compression hasn't initialized, we re-"poison"
539 * the workspace (or at least the non-static, non-table parts of it)
540 * every time we start a new compression. */
541 {
542 size_t size = (BYTE*)ws->workspaceEnd - (BYTE*)ws->tableValidEnd;
543 __msan_poison(ws->tableValidEnd, size);
544 }
545#endif
546
547#if ZSTD_ADDRESS_SANITIZER && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
548 /* We don't do this when the workspace is statically allocated, because
549 * when that is the case, we have no capability to hook into the end of the
550 * workspace's lifecycle to unpoison the memory.
551 */
553 size_t size = (BYTE*)ws->workspaceEnd - (BYTE*)ws->objectEnd;
554 __asan_poison_memory_region(ws->objectEnd, size);
555 }
556#endif
557
558 ws->tableEnd = ws->objectEnd;
559 ws->allocStart = ws->workspaceEnd;
560 ws->allocFailed = 0;
563 }
565}
566
573 DEBUGLOG(4, "cwksp: init'ing workspace with %zd bytes", size);
574 assert(((size_t)start & (sizeof(void*)-1)) == 0); /* ensure correct alignment */
575 ws->workspace = start;
576 ws->workspaceEnd = (BYTE*)start + size;
577 ws->objectEnd = ws->workspace;
578 ws->tableValidEnd = ws->objectEnd;
580 ws->isStatic = isStatic;
584}
585
586MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp* ws, size_t size, ZSTD_customMem customMem) {
587 void* workspace = ZSTD_customMalloc(size, customMem);
588 DEBUGLOG(4, "cwksp: creating new workspace with %zd bytes", size);
589 RETURN_ERROR_IF(workspace == NULL, memory_allocation, "NULL pointer!");
591 return 0;
592}
593
594MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) {
595 void *ptr = ws->workspace;
596 DEBUGLOG(4, "cwksp: freeing workspace");
597 ZSTD_memset(ws, 0, sizeof(ZSTD_cwksp));
598 ZSTD_customFree(ptr, customMem);
599}
600
609
611 return (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->workspace);
612}
613
615 return (size_t)((BYTE*)ws->tableEnd - (BYTE*)ws->workspace)
616 + (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->allocStart);
617}
618
620 return ws->allocFailed;
621}
622
623/*-*************************************
624* Functions Checking Free Space
625***************************************/
626
627/* ZSTD_alignmentSpaceWithinBounds() :
628 * Returns if the estimated space needed for a wksp is within an acceptable limit of the
629 * actual amount of space used.
630 */
632 size_t const estimatedSpace, int resizedWorkspace) {
633 if (resizedWorkspace) {
634 /* Resized/newly allocated wksp should have exact bounds */
635 return ZSTD_cwksp_used(ws) == estimatedSpace;
636 } else {
637 /* Due to alignment, when reusing a workspace, we can actually consume 63 fewer or more bytes
638 * than estimatedSpace. See the comments in zstd_cwksp.h for details.
639 */
640 return (ZSTD_cwksp_used(ws) >= estimatedSpace - 63) && (ZSTD_cwksp_used(ws) <= estimatedSpace + 63);
641 }
642}
643
644
646 return (size_t)((BYTE*)ws->allocStart - (BYTE*)ws->tableEnd);
647}
648
649MEM_STATIC int ZSTD_cwksp_check_available(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
650 return ZSTD_cwksp_available_space(ws) >= additionalNeededSpace;
651}
652
653MEM_STATIC int ZSTD_cwksp_check_too_large(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
655 ws, additionalNeededSpace * ZSTD_WORKSPACETOOLARGE_FACTOR);
656}
657
658MEM_STATIC int ZSTD_cwksp_check_wasteful(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
659 return ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)
661}
662
664 ZSTD_cwksp* ws, size_t additionalNeededSpace) {
665 if (ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)) {
667 } else {
669 }
670}
671
672#if defined (__cplusplus)
673}
674#endif
675
676#endif /* ZSTD_CWKSP_H */
int start()
unsigned char BYTE
Definition lz4.c:314
#define DEBUGLOG(l,...)
Definition lz4.c:289
unsigned int U32
Definition lz4.c:316
#define assert(condition)
Definition lz4.c:273
char * dst
Definition lz4.h:833
const char * src
Definition lz4.h:866
#define RETURN_ERROR_IF(c, e)
Definition lz4frame.c:320
Definition zstd_cwksp.h:145
BYTE allocFailed
Definition zstd_cwksp.h:154
void * workspaceEnd
Definition zstd_cwksp.h:147
void * tableValidEnd
Definition zstd_cwksp.h:151
void * tableEnd
Definition zstd_cwksp.h:150
ZSTD_cwksp_alloc_phase_e phase
Definition zstd_cwksp.h:156
int workspaceOversizedDuration
Definition zstd_cwksp.h:155
void * objectEnd
Definition zstd_cwksp.h:149
void * workspace
Definition zstd_cwksp.h:146
void * allocStart
Definition zstd_cwksp.h:152
ZSTD_cwksp_static_alloc_e isStatic
Definition zstd_cwksp.h:157
Definition compress42.c:551
#define ZSTD_ALIGNOF(T)
Definition compiler.h:267
size_t size
Definition platform.h:559
#define NULL
Definition getopt1.c:37
#define MEM_STATIC
Definition mem.h:27
#define ZSTD_memset(d, s, n)
Definition zstd_deps.h:34
void * ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
Definition zstd_common.c:56
void ZSTD_customFree(void *ptr, ZSTD_customMem customMem)
Definition zstd_common.c:75
#define ZSTD_WORKSPACETOOLARGE_MAXDURATION
Definition zstd_internal.h:274
#define ZSTD_isError
Definition zstd_internal.h:49
#define ZSTD_STATIC_ASSERT(c)
Definition zstd_internal.h:48
#define ZSTD_WORKSPACETOOLARGE_FACTOR
Definition zstd_internal.h:267
MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp *ws, size_t size, ZSTD_customMem customMem)
Definition zstd_cwksp.h:586
MEM_STATIC size_t ZSTD_cwksp_align(size_t size, size_t const align)
Definition zstd_cwksp.h:179
MEM_STATIC void ZSTD_cwksp_mark_tables_clean(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:484
MEM_STATIC int ZSTD_cwksp_reserve_failed(const ZSTD_cwksp *ws)
Definition zstd_cwksp.h:619
ZSTD_cwksp_static_alloc_e
Definition zstd_cwksp.h:56
@ ZSTD_cwksp_dynamic_alloc
Definition zstd_cwksp.h:57
@ ZSTD_cwksp_static_alloc
Definition zstd_cwksp.h:58
MEM_STATIC void * ZSTD_cwksp_reserve_internal_buffer_space(ZSTD_cwksp *ws, size_t const bytes)
Definition zstd_cwksp.h:252
MEM_STATIC void ZSTD_cwksp_move(ZSTD_cwksp *dst, ZSTD_cwksp *src)
Definition zstd_cwksp.h:605
MEM_STATIC size_t ZSTD_cwksp_internal_advance_phase(ZSTD_cwksp *ws, ZSTD_cwksp_alloc_phase_e phase)
Definition zstd_cwksp.h:280
MEM_STATIC size_t ZSTD_cwksp_used(const ZSTD_cwksp *ws)
Definition zstd_cwksp.h:614
#define ZSTD_CWKSP_ALIGNMENT_BYTES
Definition zstd_cwksp.h:40
MEM_STATIC void ZSTD_cwksp_clean_tables(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:497
MEM_STATIC int ZSTD_cwksp_check_too_large(ZSTD_cwksp *ws, size_t additionalNeededSpace)
Definition zstd_cwksp.h:653
MEM_STATIC size_t ZSTD_cwksp_bytes_to_align_ptr(void *ptr, const size_t alignBytes)
Definition zstd_cwksp.h:236
MEM_STATIC void ZSTD_cwksp_clear_tables(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:511
MEM_STATIC void ZSTD_cwksp_assert_internal_consistency(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:166
MEM_STATIC int ZSTD_cwksp_owns_buffer(const ZSTD_cwksp *ws, const void *ptr)
Definition zstd_cwksp.h:322
MEM_STATIC int ZSTD_cwksp_check_wasteful(ZSTD_cwksp *ws, size_t additionalNeededSpace)
Definition zstd_cwksp.h:658
MEM_STATIC int ZSTD_cwksp_estimated_space_within_bounds(const ZSTD_cwksp *const ws, size_t const estimatedSpace, int resizedWorkspace)
Definition zstd_cwksp.h:631
MEM_STATIC size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp *ws)
Definition zstd_cwksp.h:610
MEM_STATIC void ZSTD_cwksp_bump_oversized_duration(ZSTD_cwksp *ws, size_t additionalNeededSpace)
Definition zstd_cwksp.h:663
MEM_STATIC size_t ZSTD_cwksp_slack_space_required(void)
Definition zstd_cwksp.h:219
MEM_STATIC void ZSTD_cwksp_mark_tables_dirty(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:463
ZSTD_cwksp_alloc_phase_e
Definition zstd_cwksp.h:45
@ ZSTD_cwksp_alloc_objects
Definition zstd_cwksp.h:46
@ ZSTD_cwksp_alloc_buffers
Definition zstd_cwksp.h:47
@ ZSTD_cwksp_alloc_aligned
Definition zstd_cwksp.h:48
MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp *ws, ZSTD_customMem customMem)
Definition zstd_cwksp.h:594
MEM_STATIC void * ZSTD_cwksp_reserve_internal(ZSTD_cwksp *ws, size_t bytes, ZSTD_cwksp_alloc_phase_e phase)
Definition zstd_cwksp.h:331
MEM_STATIC BYTE * ZSTD_cwksp_reserve_buffer(ZSTD_cwksp *ws, size_t bytes)
Definition zstd_cwksp.h:362
MEM_STATIC void ZSTD_cwksp_clear(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:533
MEM_STATIC void * ZSTD_cwksp_reserve_object(ZSTD_cwksp *ws, size_t bytes)
Definition zstd_cwksp.h:424
MEM_STATIC void ZSTD_cwksp_init(ZSTD_cwksp *ws, void *start, size_t size, ZSTD_cwksp_static_alloc_e isStatic)
Definition zstd_cwksp.h:572
MEM_STATIC size_t ZSTD_cwksp_aligned_alloc_size(size_t size)
Definition zstd_cwksp.h:211
MEM_STATIC size_t ZSTD_cwksp_alloc_size(size_t size)
Definition zstd_cwksp.h:197
MEM_STATIC int ZSTD_cwksp_check_available(ZSTD_cwksp *ws, size_t additionalNeededSpace)
Definition zstd_cwksp.h:649
MEM_STATIC void * ZSTD_cwksp_reserve_table(ZSTD_cwksp *ws, size_t bytes)
Definition zstd_cwksp.h:383
#define ZSTD_CWKSP_ASAN_REDZONE_SIZE
Definition zstd_cwksp.h:35
MEM_STATIC void * ZSTD_cwksp_reserve_aligned(ZSTD_cwksp *ws, size_t bytes)
Definition zstd_cwksp.h:370
MEM_STATIC size_t ZSTD_cwksp_available_space(ZSTD_cwksp *ws)
Definition zstd_cwksp.h:645
ZSTD_cwksp_static_alloc_e
Definition zstd_cwksp.h:59
ZSTD_cwksp_alloc_phase_e
Definition zstd_cwksp.h:47