zstdmt_compress.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2016-present, 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 ZSTDMT_COMPRESS_H
  11. #define ZSTDMT_COMPRESS_H
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /* Note : This is an internal API.
  16. * Some methods are still exposed (ZSTDLIB_API),
  17. * because it used to be the only way to invoke MT compression.
  18. * Now, it's recommended to use ZSTD_compress_generic() instead.
  19. * These methods will stop being exposed in a future version */
  20. /* === Dependencies === */
  21. #include <stddef.h> /* size_t */
  22. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters */
  23. #include "zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTDLIB_API */
  24. /* === Memory management === */
  25. typedef struct ZSTDMT_CCtx_s ZSTDMT_CCtx;
  26. ZSTDLIB_API ZSTDMT_CCtx* ZSTDMT_createCCtx(unsigned nbWorkers);
  27. ZSTDLIB_API ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers,
  28. ZSTD_customMem cMem);
  29. ZSTDLIB_API size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx);
  30. ZSTDLIB_API size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx);
  31. /* === Simple one-pass compression function === */
  32. ZSTDLIB_API size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx,
  33. void* dst, size_t dstCapacity,
  34. const void* src, size_t srcSize,
  35. int compressionLevel);
  36. /* === Streaming functions === */
  37. ZSTDLIB_API size_t ZSTDMT_initCStream(ZSTDMT_CCtx* mtctx, int compressionLevel);
  38. ZSTDLIB_API size_t ZSTDMT_resetCStream(ZSTDMT_CCtx* mtctx, unsigned long long pledgedSrcSize); /**< if srcSize is not known at reset time, use ZSTD_CONTENTSIZE_UNKNOWN. Note: for compatibility with older programs, 0 means the same as ZSTD_CONTENTSIZE_UNKNOWN, but it will change in the future to mean "empty" */
  39. ZSTDLIB_API size_t ZSTDMT_compressStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
  40. ZSTDLIB_API size_t ZSTDMT_flushStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output); /**< @return : 0 == all flushed; >0 : still some data to be flushed; or an error code (ZSTD_isError()) */
  41. ZSTDLIB_API size_t ZSTDMT_endStream(ZSTDMT_CCtx* mtctx, ZSTD_outBuffer* output); /**< @return : 0 == all flushed; >0 : still some data to be flushed; or an error code (ZSTD_isError()) */
  42. /* === Advanced functions and parameters === */
  43. #ifndef ZSTDMT_JOBSIZE_MIN
  44. # define ZSTDMT_JOBSIZE_MIN (1U << 20) /* 1 MB - Minimum size of each compression job */
  45. #endif
  46. ZSTDLIB_API size_t ZSTDMT_compress_advanced(ZSTDMT_CCtx* mtctx,
  47. void* dst, size_t dstCapacity,
  48. const void* src, size_t srcSize,
  49. const ZSTD_CDict* cdict,
  50. ZSTD_parameters params,
  51. unsigned overlapLog);
  52. ZSTDLIB_API size_t ZSTDMT_initCStream_advanced(ZSTDMT_CCtx* mtctx,
  53. const void* dict, size_t dictSize, /* dict can be released after init, a local copy is preserved within zcs */
  54. ZSTD_parameters params,
  55. unsigned long long pledgedSrcSize); /* pledgedSrcSize is optional and can be zero == unknown */
  56. ZSTDLIB_API size_t ZSTDMT_initCStream_usingCDict(ZSTDMT_CCtx* mtctx,
  57. const ZSTD_CDict* cdict,
  58. ZSTD_frameParameters fparams,
  59. unsigned long long pledgedSrcSize); /* note : zero means empty */
  60. /* ZSTDMT_parameter :
  61. * List of parameters that can be set using ZSTDMT_setMTCtxParameter() */
  62. typedef enum {
  63. ZSTDMT_p_jobSize, /* Each job is compressed in parallel. By default, this value is dynamically determined depending on compression parameters. Can be set explicitly here. */
  64. ZSTDMT_p_overlapSectionLog /* Each job may reload a part of previous job to enhance compressionr ratio; 0 == no overlap, 6(default) == use 1/8th of window, >=9 == use full window. This is a "sticky" parameter : its value will be re-used on next compression job */
  65. } ZSTDMT_parameter;
  66. /* ZSTDMT_setMTCtxParameter() :
  67. * allow setting individual parameters, one at a time, among a list of enums defined in ZSTDMT_parameter.
  68. * The function must be called typically after ZSTD_createCCtx() but __before ZSTDMT_init*() !__
  69. * Parameters not explicitly reset by ZSTDMT_init*() remain the same in consecutive compression sessions.
  70. * @return : 0, or an error code (which can be tested using ZSTD_isError()) */
  71. ZSTDLIB_API size_t ZSTDMT_setMTCtxParameter(ZSTDMT_CCtx* mtctx, ZSTDMT_parameter parameter, unsigned value);
  72. /*! ZSTDMT_compressStream_generic() :
  73. * Combines ZSTDMT_compressStream() with optional ZSTDMT_flushStream() or ZSTDMT_endStream()
  74. * depending on flush directive.
  75. * @return : minimum amount of data still to be flushed
  76. * 0 if fully flushed
  77. * or an error code
  78. * note : needs to be init using any ZSTD_initCStream*() variant */
  79. ZSTDLIB_API size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
  80. ZSTD_outBuffer* output,
  81. ZSTD_inBuffer* input,
  82. ZSTD_EndDirective endOp);
  83. /* ========================================================
  84. * === Private interface, for use by ZSTD_compress.c ===
  85. * === Not exposed in libzstd. Never invoke directly ===
  86. * ======================================================== */
  87. size_t ZSTDMT_CCtxParam_setMTCtxParameter(ZSTD_CCtx_params* params, ZSTDMT_parameter parameter, unsigned value);
  88. /* ZSTDMT_CCtxParam_setNbWorkers()
  89. * Set nbWorkers, and clamp it.
  90. * Also reset jobSize and overlapLog */
  91. size_t ZSTDMT_CCtxParam_setNbWorkers(ZSTD_CCtx_params* params, unsigned nbWorkers);
  92. /*! ZSTDMT_updateCParams_whileCompressing() :
  93. * Updates only a selected set of compression parameters, to remain compatible with current frame.
  94. * New parameters will be applied to next compression job. */
  95. void ZSTDMT_updateCParams_whileCompressing(ZSTDMT_CCtx* mtctx, const ZSTD_CCtx_params* cctxParams);
  96. /* ZSTDMT_getNbWorkers():
  97. * @return nb threads currently active in mtctx.
  98. * mtctx must be valid */
  99. unsigned ZSTDMT_getNbWorkers(const ZSTDMT_CCtx* mtctx);
  100. /* ZSTDMT_getFrameProgression():
  101. * tells how much data has been consumed (input) and produced (output) for current frame.
  102. * able to count progression inside worker threads.
  103. */
  104. ZSTD_frameProgression ZSTDMT_getFrameProgression(ZSTDMT_CCtx* mtctx);
  105. /*! ZSTDMT_initCStream_internal() :
  106. * Private use only. Init streaming operation.
  107. * expects params to be valid.
  108. * must receive dict, or cdict, or none, but not both.
  109. * @return : 0, or an error code */
  110. size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs,
  111. const void* dict, size_t dictSize, ZSTD_dictContentType_e dictContentType,
  112. const ZSTD_CDict* cdict,
  113. ZSTD_CCtx_params params, unsigned long long pledgedSrcSize);
  114. #if defined (__cplusplus)
  115. }
  116. #endif
  117. #endif /* ZSTDMT_COMPRESS_H */