zstd_cwksp.h 16 KB

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