zstd_cwksp.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2016-2020, 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. #ifndef ZSTD_CWKSP_H
  11. #define ZSTD_CWKSP_H
  12. /*-*************************************
  13. * Dependencies
  14. ***************************************/
  15. #include "../common/zstd_internal.h"
  16. #if defined (__cplusplus)
  17. extern "C" {
  18. #endif
  19. /*-*************************************
  20. * Constants
  21. ***************************************/
  22. /* Since the workspace is effectively its own little malloc implementation /
  23. * arena, when we run under ASAN, we should similarly insert redzones between
  24. * each internal element of the workspace, so ASAN will catch overruns that
  25. * reach outside an object but that stay inside the workspace.
  26. *
  27. * This defines the size of that redzone.
  28. */
  29. #ifndef ZSTD_CWKSP_ASAN_REDZONE_SIZE
  30. #define ZSTD_CWKSP_ASAN_REDZONE_SIZE 128
  31. #endif
  32. /*-*************************************
  33. * Structures
  34. ***************************************/
  35. typedef enum {
  36. ZSTD_cwksp_alloc_objects,
  37. ZSTD_cwksp_alloc_buffers,
  38. ZSTD_cwksp_alloc_aligned
  39. } ZSTD_cwksp_alloc_phase_e;
  40. /**
  41. * Zstd fits all its internal datastructures into a single continuous buffer,
  42. * so that it only needs to perform a single OS allocation (or so that a buffer
  43. * can be provided to it and it can perform no allocations at all). This buffer
  44. * is called the workspace.
  45. *
  46. * Several optimizations complicate that process of allocating memory ranges
  47. * from this workspace for each internal datastructure:
  48. *
  49. * - These different internal datastructures have different setup requirements:
  50. *
  51. * - The static objects need to be cleared once and can then be trivially
  52. * reused for each compression.
  53. *
  54. * - Various buffers don't need to be initialized at all--they are always
  55. * written into before they're read.
  56. *
  57. * - The matchstate tables have a unique requirement that they don't need
  58. * their memory to be totally cleared, but they do need the memory to have
  59. * some bound, i.e., a guarantee that all values in the memory they've been
  60. * allocated is less than some maximum value (which is the starting value
  61. * for the indices that they will then use for compression). When this
  62. * guarantee is provided to them, they can use the memory without any setup
  63. * work. When it can't, they have to clear the area.
  64. *
  65. * - These buffers also have different alignment requirements.
  66. *
  67. * - We would like to reuse the objects in the workspace for multiple
  68. * compressions without having to perform any expensive reallocation or
  69. * reinitialization work.
  70. *
  71. * - We would like to be able to efficiently reuse the workspace across
  72. * multiple compressions **even when the compression parameters change** and
  73. * we need to resize some of the objects (where possible).
  74. *
  75. * To attempt to manage this buffer, given these constraints, the ZSTD_cwksp
  76. * abstraction was created. It works as follows:
  77. *
  78. * Workspace Layout:
  79. *
  80. * [ ... workspace ... ]
  81. * [objects][tables ... ->] free space [<- ... aligned][<- ... buffers]
  82. *
  83. * The various objects that live in the workspace are divided into the
  84. * following categories, and are allocated separately:
  85. *
  86. * - Static objects: this is optionally the enclosing ZSTD_CCtx or ZSTD_CDict,
  87. * so that literally everything fits in a single buffer. Note: if present,
  88. * this must be the first object in the workspace, since ZSTD_free{CCtx,
  89. * CDict}() rely on a pointer comparison to see whether one or two frees are
  90. * required.
  91. *
  92. * - Fixed size objects: these are fixed-size, fixed-count objects that are
  93. * nonetheless "dynamically" allocated in the workspace so that we can
  94. * control how they're initialized separately from the broader ZSTD_CCtx.
  95. * Examples:
  96. * - Entropy Workspace
  97. * - 2 x ZSTD_compressedBlockState_t
  98. * - CDict dictionary contents
  99. *
  100. * - Tables: these are any of several different datastructures (hash tables,
  101. * chain tables, binary trees) that all respect a common format: they are
  102. * uint32_t arrays, all of whose values are between 0 and (nextSrc - base).
  103. * Their sizes depend on the cparams.
  104. *
  105. * - Aligned: these buffers are used for various purposes that require 4 byte
  106. * alignment, but don't require any initialization before they're used.
  107. *
  108. * - Buffers: these buffers are used for various purposes that don't require
  109. * any alignment or initialization before they're used. This means they can
  110. * be moved around at no cost for a new compression.
  111. *
  112. * Allocating Memory:
  113. *
  114. * The various types of objects must be allocated in order, so they can be
  115. * correctly packed into the workspace buffer. That order is:
  116. *
  117. * 1. Objects
  118. * 2. Buffers
  119. * 3. Aligned
  120. * 4. Tables
  121. *
  122. * Attempts to reserve objects of different types out of order will fail.
  123. */
  124. typedef struct {
  125. void* workspace;
  126. void* workspaceEnd;
  127. void* objectEnd;
  128. void* tableEnd;
  129. void* tableValidEnd;
  130. void* allocStart;
  131. int allocFailed;
  132. int workspaceOversizedDuration;
  133. ZSTD_cwksp_alloc_phase_e phase;
  134. } ZSTD_cwksp;
  135. /*-*************************************
  136. * Functions
  137. ***************************************/
  138. MEM_STATIC size_t ZSTD_cwksp_available_space(ZSTD_cwksp* ws);
  139. MEM_STATIC void ZSTD_cwksp_assert_internal_consistency(ZSTD_cwksp* ws) {
  140. (void)ws;
  141. assert(ws->workspace <= ws->objectEnd);
  142. assert(ws->objectEnd <= ws->tableEnd);
  143. assert(ws->objectEnd <= ws->tableValidEnd);
  144. assert(ws->tableEnd <= ws->allocStart);
  145. assert(ws->tableValidEnd <= ws->allocStart);
  146. assert(ws->allocStart <= ws->workspaceEnd);
  147. }
  148. /**
  149. * Align must be a power of 2.
  150. */
  151. MEM_STATIC size_t ZSTD_cwksp_align(size_t size, size_t const align) {
  152. size_t const mask = align - 1;
  153. assert((align & mask) == 0);
  154. return (size + mask) & ~mask;
  155. }
  156. /**
  157. * Use this to determine how much space in the workspace we will consume to
  158. * allocate this object. (Normally it should be exactly the size of the object,
  159. * but under special conditions, like ASAN, where we pad each object, it might
  160. * be larger.)
  161. *
  162. * Since tables aren't currently redzoned, you don't need to call through this
  163. * to figure out how much space you need for the matchState tables. Everything
  164. * else is though.
  165. */
  166. MEM_STATIC size_t ZSTD_cwksp_alloc_size(size_t size) {
  167. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  168. return size + 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
  169. #else
  170. return size;
  171. #endif
  172. }
  173. MEM_STATIC void ZSTD_cwksp_internal_advance_phase(
  174. ZSTD_cwksp* ws, ZSTD_cwksp_alloc_phase_e phase) {
  175. assert(phase >= ws->phase);
  176. if (phase > ws->phase) {
  177. if (ws->phase < ZSTD_cwksp_alloc_buffers &&
  178. phase >= ZSTD_cwksp_alloc_buffers) {
  179. ws->tableValidEnd = ws->objectEnd;
  180. }
  181. if (ws->phase < ZSTD_cwksp_alloc_aligned &&
  182. phase >= ZSTD_cwksp_alloc_aligned) {
  183. /* If unaligned allocations down from a too-large top have left us
  184. * unaligned, we need to realign our alloc ptr. Technically, this
  185. * can consume space that is unaccounted for in the neededSpace
  186. * calculation. However, I believe this can only happen when the
  187. * workspace is too large, and specifically when it is too large
  188. * by a larger margin than the space that will be consumed. */
  189. /* TODO: cleaner, compiler warning friendly way to do this??? */
  190. ws->allocStart = (BYTE*)ws->allocStart - ((size_t)ws->allocStart & (sizeof(U32)-1));
  191. if (ws->allocStart < ws->tableValidEnd) {
  192. ws->tableValidEnd = ws->allocStart;
  193. }
  194. }
  195. ws->phase = phase;
  196. }
  197. }
  198. /**
  199. * Returns whether this object/buffer/etc was allocated in this workspace.
  200. */
  201. MEM_STATIC int ZSTD_cwksp_owns_buffer(const ZSTD_cwksp* ws, const void* ptr) {
  202. return (ptr != NULL) && (ws->workspace <= ptr) && (ptr <= ws->workspaceEnd);
  203. }
  204. /**
  205. * Internal function. Do not use directly.
  206. */
  207. MEM_STATIC void* ZSTD_cwksp_reserve_internal(
  208. ZSTD_cwksp* ws, size_t bytes, ZSTD_cwksp_alloc_phase_e phase) {
  209. void* alloc;
  210. void* bottom = ws->tableEnd;
  211. ZSTD_cwksp_internal_advance_phase(ws, phase);
  212. alloc = (BYTE *)ws->allocStart - bytes;
  213. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  214. /* over-reserve space */
  215. alloc = (BYTE *)alloc - 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
  216. #endif
  217. DEBUGLOG(5, "cwksp: reserving %p %zd bytes, %zd bytes remaining",
  218. alloc, bytes, ZSTD_cwksp_available_space(ws) - bytes);
  219. ZSTD_cwksp_assert_internal_consistency(ws);
  220. assert(alloc >= bottom);
  221. if (alloc < bottom) {
  222. DEBUGLOG(4, "cwksp: alloc failed!");
  223. ws->allocFailed = 1;
  224. return NULL;
  225. }
  226. if (alloc < ws->tableValidEnd) {
  227. ws->tableValidEnd = alloc;
  228. }
  229. ws->allocStart = alloc;
  230. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  231. /* Move alloc so there's ZSTD_CWKSP_ASAN_REDZONE_SIZE unused space on
  232. * either size. */
  233. alloc = (BYTE *)alloc + ZSTD_CWKSP_ASAN_REDZONE_SIZE;
  234. __asan_unpoison_memory_region(alloc, bytes);
  235. #endif
  236. return alloc;
  237. }
  238. /**
  239. * Reserves and returns unaligned memory.
  240. */
  241. MEM_STATIC BYTE* ZSTD_cwksp_reserve_buffer(ZSTD_cwksp* ws, size_t bytes) {
  242. return (BYTE*)ZSTD_cwksp_reserve_internal(ws, bytes, ZSTD_cwksp_alloc_buffers);
  243. }
  244. /**
  245. * Reserves and returns memory sized on and aligned on sizeof(unsigned).
  246. */
  247. MEM_STATIC void* ZSTD_cwksp_reserve_aligned(ZSTD_cwksp* ws, size_t bytes) {
  248. assert((bytes & (sizeof(U32)-1)) == 0);
  249. return ZSTD_cwksp_reserve_internal(ws, ZSTD_cwksp_align(bytes, sizeof(U32)), ZSTD_cwksp_alloc_aligned);
  250. }
  251. /**
  252. * Aligned on sizeof(unsigned). These buffers have the special property that
  253. * their values remain constrained, allowing us to re-use them without
  254. * memset()-ing them.
  255. */
  256. MEM_STATIC void* ZSTD_cwksp_reserve_table(ZSTD_cwksp* ws, size_t bytes) {
  257. const ZSTD_cwksp_alloc_phase_e phase = ZSTD_cwksp_alloc_aligned;
  258. void* alloc = ws->tableEnd;
  259. void* end = (BYTE *)alloc + bytes;
  260. void* top = ws->allocStart;
  261. DEBUGLOG(5, "cwksp: reserving %p table %zd bytes, %zd bytes remaining",
  262. alloc, bytes, ZSTD_cwksp_available_space(ws) - bytes);
  263. assert((bytes & (sizeof(U32)-1)) == 0);
  264. ZSTD_cwksp_internal_advance_phase(ws, phase);
  265. ZSTD_cwksp_assert_internal_consistency(ws);
  266. assert(end <= top);
  267. if (end > top) {
  268. DEBUGLOG(4, "cwksp: table alloc failed!");
  269. ws->allocFailed = 1;
  270. return NULL;
  271. }
  272. ws->tableEnd = end;
  273. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  274. __asan_unpoison_memory_region(alloc, bytes);
  275. #endif
  276. return alloc;
  277. }
  278. /**
  279. * Aligned on sizeof(void*).
  280. */
  281. MEM_STATIC void* ZSTD_cwksp_reserve_object(ZSTD_cwksp* ws, size_t bytes) {
  282. size_t roundedBytes = ZSTD_cwksp_align(bytes, sizeof(void*));
  283. void* alloc = ws->objectEnd;
  284. void* end = (BYTE*)alloc + roundedBytes;
  285. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  286. /* over-reserve space */
  287. end = (BYTE *)end + 2 * ZSTD_CWKSP_ASAN_REDZONE_SIZE;
  288. #endif
  289. DEBUGLOG(5,
  290. "cwksp: reserving %p object %zd bytes (rounded to %zd), %zd bytes remaining",
  291. alloc, bytes, roundedBytes, ZSTD_cwksp_available_space(ws) - roundedBytes);
  292. assert(((size_t)alloc & (sizeof(void*)-1)) == 0);
  293. assert((bytes & (sizeof(void*)-1)) == 0);
  294. ZSTD_cwksp_assert_internal_consistency(ws);
  295. /* we must be in the first phase, no advance is possible */
  296. if (ws->phase != ZSTD_cwksp_alloc_objects || end > ws->workspaceEnd) {
  297. DEBUGLOG(4, "cwksp: object alloc failed!");
  298. ws->allocFailed = 1;
  299. return NULL;
  300. }
  301. ws->objectEnd = end;
  302. ws->tableEnd = end;
  303. ws->tableValidEnd = end;
  304. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  305. /* Move alloc so there's ZSTD_CWKSP_ASAN_REDZONE_SIZE unused space on
  306. * either size. */
  307. alloc = (BYTE *)alloc + ZSTD_CWKSP_ASAN_REDZONE_SIZE;
  308. __asan_unpoison_memory_region(alloc, bytes);
  309. #endif
  310. return alloc;
  311. }
  312. MEM_STATIC void ZSTD_cwksp_mark_tables_dirty(ZSTD_cwksp* ws) {
  313. DEBUGLOG(4, "cwksp: ZSTD_cwksp_mark_tables_dirty");
  314. #if defined (MEMORY_SANITIZER) && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
  315. /* To validate that the table re-use logic is sound, and that we don't
  316. * access table space that we haven't cleaned, we re-"poison" the table
  317. * space every time we mark it dirty. */
  318. {
  319. size_t size = (BYTE*)ws->tableValidEnd - (BYTE*)ws->objectEnd;
  320. assert(__msan_test_shadow(ws->objectEnd, size) == -1);
  321. __msan_poison(ws->objectEnd, size);
  322. }
  323. #endif
  324. assert(ws->tableValidEnd >= ws->objectEnd);
  325. assert(ws->tableValidEnd <= ws->allocStart);
  326. ws->tableValidEnd = ws->objectEnd;
  327. ZSTD_cwksp_assert_internal_consistency(ws);
  328. }
  329. MEM_STATIC void ZSTD_cwksp_mark_tables_clean(ZSTD_cwksp* ws) {
  330. DEBUGLOG(4, "cwksp: ZSTD_cwksp_mark_tables_clean");
  331. assert(ws->tableValidEnd >= ws->objectEnd);
  332. assert(ws->tableValidEnd <= ws->allocStart);
  333. if (ws->tableValidEnd < ws->tableEnd) {
  334. ws->tableValidEnd = ws->tableEnd;
  335. }
  336. ZSTD_cwksp_assert_internal_consistency(ws);
  337. }
  338. /**
  339. * Zero the part of the allocated tables not already marked clean.
  340. */
  341. MEM_STATIC void ZSTD_cwksp_clean_tables(ZSTD_cwksp* ws) {
  342. DEBUGLOG(4, "cwksp: ZSTD_cwksp_clean_tables");
  343. assert(ws->tableValidEnd >= ws->objectEnd);
  344. assert(ws->tableValidEnd <= ws->allocStart);
  345. if (ws->tableValidEnd < ws->tableEnd) {
  346. memset(ws->tableValidEnd, 0, (BYTE*)ws->tableEnd - (BYTE*)ws->tableValidEnd);
  347. }
  348. ZSTD_cwksp_mark_tables_clean(ws);
  349. }
  350. /**
  351. * Invalidates table allocations.
  352. * All other allocations remain valid.
  353. */
  354. MEM_STATIC void ZSTD_cwksp_clear_tables(ZSTD_cwksp* ws) {
  355. DEBUGLOG(4, "cwksp: clearing tables!");
  356. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  357. {
  358. size_t size = (BYTE*)ws->tableValidEnd - (BYTE*)ws->objectEnd;
  359. __asan_poison_memory_region(ws->objectEnd, size);
  360. }
  361. #endif
  362. ws->tableEnd = ws->objectEnd;
  363. ZSTD_cwksp_assert_internal_consistency(ws);
  364. }
  365. /**
  366. * Invalidates all buffer, aligned, and table allocations.
  367. * Object allocations remain valid.
  368. */
  369. MEM_STATIC void ZSTD_cwksp_clear(ZSTD_cwksp* ws) {
  370. DEBUGLOG(4, "cwksp: clearing!");
  371. #if defined (MEMORY_SANITIZER) && !defined (ZSTD_MSAN_DONT_POISON_WORKSPACE)
  372. /* To validate that the context re-use logic is sound, and that we don't
  373. * access stuff that this compression hasn't initialized, we re-"poison"
  374. * the workspace (or at least the non-static, non-table parts of it)
  375. * every time we start a new compression. */
  376. {
  377. size_t size = (BYTE*)ws->workspaceEnd - (BYTE*)ws->tableValidEnd;
  378. __msan_poison(ws->tableValidEnd, size);
  379. }
  380. #endif
  381. #if defined (ADDRESS_SANITIZER) && !defined (ZSTD_ASAN_DONT_POISON_WORKSPACE)
  382. {
  383. size_t size = (BYTE*)ws->workspaceEnd - (BYTE*)ws->objectEnd;
  384. __asan_poison_memory_region(ws->objectEnd, size);
  385. }
  386. #endif
  387. ws->tableEnd = ws->objectEnd;
  388. ws->allocStart = ws->workspaceEnd;
  389. ws->allocFailed = 0;
  390. if (ws->phase > ZSTD_cwksp_alloc_buffers) {
  391. ws->phase = ZSTD_cwksp_alloc_buffers;
  392. }
  393. ZSTD_cwksp_assert_internal_consistency(ws);
  394. }
  395. /**
  396. * The provided workspace takes ownership of the buffer [start, start+size).
  397. * Any existing values in the workspace are ignored (the previously managed
  398. * buffer, if present, must be separately freed).
  399. */
  400. MEM_STATIC void ZSTD_cwksp_init(ZSTD_cwksp* ws, void* start, size_t size) {
  401. DEBUGLOG(4, "cwksp: init'ing workspace with %zd bytes", size);
  402. assert(((size_t)start & (sizeof(void*)-1)) == 0); /* ensure correct alignment */
  403. ws->workspace = start;
  404. ws->workspaceEnd = (BYTE*)start + size;
  405. ws->objectEnd = ws->workspace;
  406. ws->tableValidEnd = ws->objectEnd;
  407. ws->phase = ZSTD_cwksp_alloc_objects;
  408. ZSTD_cwksp_clear(ws);
  409. ws->workspaceOversizedDuration = 0;
  410. ZSTD_cwksp_assert_internal_consistency(ws);
  411. }
  412. MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp* ws, size_t size, ZSTD_customMem customMem) {
  413. void* workspace = ZSTD_malloc(size, customMem);
  414. DEBUGLOG(4, "cwksp: creating new workspace with %zd bytes", size);
  415. RETURN_ERROR_IF(workspace == NULL, memory_allocation, "NULL pointer!");
  416. ZSTD_cwksp_init(ws, workspace, size);
  417. return 0;
  418. }
  419. MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) {
  420. void *ptr = ws->workspace;
  421. DEBUGLOG(4, "cwksp: freeing workspace");
  422. memset(ws, 0, sizeof(ZSTD_cwksp));
  423. ZSTD_free(ptr, customMem);
  424. }
  425. /**
  426. * Moves the management of a workspace from one cwksp to another. The src cwksp
  427. * is left in an invalid state (src must be re-init()'ed before its used again).
  428. */
  429. MEM_STATIC void ZSTD_cwksp_move(ZSTD_cwksp* dst, ZSTD_cwksp* src) {
  430. *dst = *src;
  431. memset(src, 0, sizeof(ZSTD_cwksp));
  432. }
  433. MEM_STATIC size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp* ws) {
  434. return (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->workspace);
  435. }
  436. MEM_STATIC int ZSTD_cwksp_reserve_failed(const ZSTD_cwksp* ws) {
  437. return ws->allocFailed;
  438. }
  439. /*-*************************************
  440. * Functions Checking Free Space
  441. ***************************************/
  442. MEM_STATIC size_t ZSTD_cwksp_available_space(ZSTD_cwksp* ws) {
  443. return (size_t)((BYTE*)ws->allocStart - (BYTE*)ws->tableEnd);
  444. }
  445. MEM_STATIC int ZSTD_cwksp_check_available(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
  446. return ZSTD_cwksp_available_space(ws) >= additionalNeededSpace;
  447. }
  448. MEM_STATIC int ZSTD_cwksp_check_too_large(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
  449. return ZSTD_cwksp_check_available(
  450. ws, additionalNeededSpace * ZSTD_WORKSPACETOOLARGE_FACTOR);
  451. }
  452. MEM_STATIC int ZSTD_cwksp_check_wasteful(ZSTD_cwksp* ws, size_t additionalNeededSpace) {
  453. return ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)
  454. && ws->workspaceOversizedDuration > ZSTD_WORKSPACETOOLARGE_MAXDURATION;
  455. }
  456. MEM_STATIC void ZSTD_cwksp_bump_oversized_duration(
  457. ZSTD_cwksp* ws, size_t additionalNeededSpace) {
  458. if (ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)) {
  459. ws->workspaceOversizedDuration++;
  460. } else {
  461. ws->workspaceOversizedDuration = 0;
  462. }
  463. }
  464. #if defined (__cplusplus)
  465. }
  466. #endif
  467. #endif /* ZSTD_CWKSP_H */