zstd_cwksp.h 20 KB

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