xref: /aosp_15_r20/external/zstd/lib/compress/zstdmt_compress.h (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui  #ifndef ZSTDMT_COMPRESS_H
12*01826a49SYabin Cui  #define ZSTDMT_COMPRESS_H
13*01826a49SYabin Cui 
14*01826a49SYabin Cui  #if defined (__cplusplus)
15*01826a49SYabin Cui  extern "C" {
16*01826a49SYabin Cui  #endif
17*01826a49SYabin Cui 
18*01826a49SYabin Cui 
19*01826a49SYabin Cui /* Note : This is an internal API.
20*01826a49SYabin Cui  *        These APIs used to be exposed with ZSTDLIB_API,
21*01826a49SYabin Cui  *        because it used to be the only way to invoke MT compression.
22*01826a49SYabin Cui  *        Now, you must use ZSTD_compress2 and ZSTD_compressStream2() instead.
23*01826a49SYabin Cui  *
24*01826a49SYabin Cui  *        This API requires ZSTD_MULTITHREAD to be defined during compilation,
25*01826a49SYabin Cui  *        otherwise ZSTDMT_createCCtx*() will fail.
26*01826a49SYabin Cui  */
27*01826a49SYabin Cui 
28*01826a49SYabin Cui /* ===   Dependencies   === */
29*01826a49SYabin Cui #include "../common/zstd_deps.h"   /* size_t */
30*01826a49SYabin Cui #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_parameters */
31*01826a49SYabin Cui #include "../zstd.h"            /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTDLIB_API */
32*01826a49SYabin Cui 
33*01826a49SYabin Cui 
34*01826a49SYabin Cui /* ===   Constants   === */
35*01826a49SYabin Cui #ifndef ZSTDMT_NBWORKERS_MAX /* a different value can be selected at compile time */
36*01826a49SYabin Cui #  define ZSTDMT_NBWORKERS_MAX ((sizeof(void*)==4) /*32-bit*/ ? 64 : 256)
37*01826a49SYabin Cui #endif
38*01826a49SYabin Cui #ifndef ZSTDMT_JOBSIZE_MIN   /* a different value can be selected at compile time */
39*01826a49SYabin Cui #  define ZSTDMT_JOBSIZE_MIN (512 KB)
40*01826a49SYabin Cui #endif
41*01826a49SYabin Cui #define ZSTDMT_JOBLOG_MAX   (MEM_32bits() ? 29 : 30)
42*01826a49SYabin Cui #define ZSTDMT_JOBSIZE_MAX  (MEM_32bits() ? (512 MB) : (1024 MB))
43*01826a49SYabin Cui 
44*01826a49SYabin Cui 
45*01826a49SYabin Cui /* ========================================================
46*01826a49SYabin Cui  * ===  Private interface, for use by ZSTD_compress.c   ===
47*01826a49SYabin Cui  * ===  Not exposed in libzstd. Never invoke directly   ===
48*01826a49SYabin Cui  * ======================================================== */
49*01826a49SYabin Cui 
50*01826a49SYabin Cui /* ===   Memory management   === */
51*01826a49SYabin Cui typedef struct ZSTDMT_CCtx_s ZSTDMT_CCtx;
52*01826a49SYabin Cui /* Requires ZSTD_MULTITHREAD to be defined during compilation, otherwise it will return NULL. */
53*01826a49SYabin Cui ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers,
54*01826a49SYabin Cui                                         ZSTD_customMem cMem,
55*01826a49SYabin Cui 					ZSTD_threadPool *pool);
56*01826a49SYabin Cui size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx);
57*01826a49SYabin Cui 
58*01826a49SYabin Cui size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx);
59*01826a49SYabin Cui 
60*01826a49SYabin Cui /* ===   Streaming functions   === */
61*01826a49SYabin Cui 
62*01826a49SYabin Cui size_t ZSTDMT_nextInputSizeHint(const ZSTDMT_CCtx* mtctx);
63*01826a49SYabin Cui 
64*01826a49SYabin Cui /*! ZSTDMT_initCStream_internal() :
65*01826a49SYabin Cui  *  Private use only. Init streaming operation.
66*01826a49SYabin Cui  *  expects params to be valid.
67*01826a49SYabin Cui  *  must receive dict, or cdict, or none, but not both.
68*01826a49SYabin Cui  *  mtctx can be freshly constructed or reused from a prior compression.
69*01826a49SYabin Cui  *  If mtctx is reused, memory allocations from the prior compression may not be freed,
70*01826a49SYabin Cui  *  even if they are not needed for the current compression.
71*01826a49SYabin Cui  *  @return : 0, or an error code */
72*01826a49SYabin Cui size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* mtctx,
73*01826a49SYabin Cui                     const void* dict, size_t dictSize, ZSTD_dictContentType_e dictContentType,
74*01826a49SYabin Cui                     const ZSTD_CDict* cdict,
75*01826a49SYabin Cui                     ZSTD_CCtx_params params, unsigned long long pledgedSrcSize);
76*01826a49SYabin Cui 
77*01826a49SYabin Cui /*! ZSTDMT_compressStream_generic() :
78*01826a49SYabin Cui  *  Combines ZSTDMT_compressStream() with optional ZSTDMT_flushStream() or ZSTDMT_endStream()
79*01826a49SYabin Cui  *  depending on flush directive.
80*01826a49SYabin Cui  * @return : minimum amount of data still to be flushed
81*01826a49SYabin Cui  *           0 if fully flushed
82*01826a49SYabin Cui  *           or an error code
83*01826a49SYabin Cui  *  note : needs to be init using any ZSTD_initCStream*() variant */
84*01826a49SYabin Cui size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
85*01826a49SYabin Cui                                      ZSTD_outBuffer* output,
86*01826a49SYabin Cui                                      ZSTD_inBuffer* input,
87*01826a49SYabin Cui                                      ZSTD_EndDirective endOp);
88*01826a49SYabin Cui 
89*01826a49SYabin Cui  /*! ZSTDMT_toFlushNow()
90*01826a49SYabin Cui   *  Tell how many bytes are ready to be flushed immediately.
91*01826a49SYabin Cui   *  Probe the oldest active job (not yet entirely flushed) and check its output buffer.
92*01826a49SYabin Cui   *  If return 0, it means there is no active job,
93*01826a49SYabin Cui   *  or, it means oldest job is still active, but everything produced has been flushed so far,
94*01826a49SYabin Cui   *  therefore flushing is limited by speed of oldest job. */
95*01826a49SYabin Cui size_t ZSTDMT_toFlushNow(ZSTDMT_CCtx* mtctx);
96*01826a49SYabin Cui 
97*01826a49SYabin Cui /*! ZSTDMT_updateCParams_whileCompressing() :
98*01826a49SYabin Cui  *  Updates only a selected set of compression parameters, to remain compatible with current frame.
99*01826a49SYabin Cui  *  New parameters will be applied to next compression job. */
100*01826a49SYabin Cui void ZSTDMT_updateCParams_whileCompressing(ZSTDMT_CCtx* mtctx, const ZSTD_CCtx_params* cctxParams);
101*01826a49SYabin Cui 
102*01826a49SYabin Cui /*! ZSTDMT_getFrameProgression():
103*01826a49SYabin Cui  *  tells how much data has been consumed (input) and produced (output) for current frame.
104*01826a49SYabin Cui  *  able to count progression inside worker threads.
105*01826a49SYabin Cui  */
106*01826a49SYabin Cui ZSTD_frameProgression ZSTDMT_getFrameProgression(ZSTDMT_CCtx* mtctx);
107*01826a49SYabin Cui 
108*01826a49SYabin Cui 
109*01826a49SYabin Cui #if defined (__cplusplus)
110*01826a49SYabin Cui }
111*01826a49SYabin Cui #endif
112*01826a49SYabin Cui 
113*01826a49SYabin Cui #endif   /* ZSTDMT_COMPRESS_H */
114