Debian Codegraph @

plink2_compress_stream.h

raw
1 #ifndef __PLINK2_COMPRESS_STREAM_H__
2 #define __PLINK2_COMPRESS_STREAM_H__
3
4 // This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell,
5 // Christopher Chang.
6 //
7 // This library is free software: you can redistribute it and/or modify it
8 // under the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this library. If not, see <http://www.gnu.org/licenses/>.
19
20
21 // Successor to plink 1.9 pigz.h. Provides a basic manually-buffered output
22 // stream interface for zstd compression. Not multithreaded yet, but the
23 // interface is identical to the old multithreaded gzipper so we'll be able to
24 // upgrade the backend later without making significant changes to other code.
25
26 #include "include/plink2_zstfile.h"
27 #include "plink2_cmdline.h"
28
29 #ifdef __cplusplus
30 namespace plink2 {
31 #endif
32
33 extern uint32_t g_zst_level;
34
35 // This should be at least as large as zstd's internal block size.
36 // todo: test different values, may want to increase on at least OS X...
37 CONSTI32(kCompressStreamBlock, 131072);
38
39 typedef struct CompressStreamStateStruct {
40 NONCOPYABLE(CompressStreamStateStruct);
41 // Usually compress text, so appropriate to define this as char*.
42 char* overflow_buf;
43
44 FILE* outfile;
45 ZSTD_CCtx* cctx;
46 ZSTD_outBuffer output;
47 } CompressStreamState;
48
49 HEADER_INLINE uint32_t IsUncompressedCstream(const CompressStreamState* css_ptr) {
50 return (css_ptr->cctx == nullptr);
51 }
52
53 HEADER_INLINE void PreinitCstream(CompressStreamState* css_ptr) {
54 css_ptr->overflow_buf = nullptr;
55 }
56
57 PglErr InitCstreamNoop(const char* out_fname, uint32_t do_append, char* overflow_buf, CompressStreamState* css_ptr);
58
59 HEADER_INLINE uintptr_t CstreamWkspaceReq(uintptr_t overflow_buf_size) {
60 return kCompressStreamBlock + MAXV(ZSTD_compressBound(overflow_buf_size), ZSTD_CStreamOutSize());
61 }
62
63 // overflow_buf must have space for at least kCompressStreamBlock + [max bytes
64 // added between Cswrite() calls] bytes.
65 // compress_wkspace can be nullptr in no-compression case; otherwise it must
66 // have space for cswrite_wkspace_req(overflow_buf size) bytes.
67 PglErr InitCstream(const char* out_fname, uint32_t do_append, uint32_t output_zst, uint32_t thread_ct, uintptr_t overflow_buf_size, char* overflow_buf, unsigned char* compress_wkspace, CompressStreamState* css_ptr);
68
69 // Convenience interface which allocates from the bottom of g_bigstack.
70 PglErr InitCstreamAlloc(const char* out_fname, uint32_t do_append, uint32_t output_zst, uint32_t thread_ct, uintptr_t overflow_buf_size, CompressStreamState* css_ptr, char** cswritepp);
71
72 BoolErr ForceUncompressedCswrite(CompressStreamState* css_ptr, char** writep_ptr);
73
74 // No longer guaranteed to consume entire input buffer, only reduces it to
75 // <128k.
76 BoolErr ForceCompressedCswrite(CompressStreamState* css_ptr, char** writep_ptr);
77
78 HEADER_INLINE BoolErr Cswrite(CompressStreamState* css_ptr, char** writep_ptr) {
79 if (S_CAST(uintptr_t, (*writep_ptr) - css_ptr->overflow_buf) >= kCompressStreamBlock + 1) {
80 if (IsUncompressedCstream(css_ptr)) {
81 return ForceUncompressedCswrite(css_ptr, writep_ptr);
82 } else {
83 return ForceCompressedCswrite(css_ptr, writep_ptr);
84 }
85 }
86 return 0;
87 }
88
89 // assumes overflow_buf has size >= 2 * kCompressStreamBlock.
90 BoolErr CsputsStd(const char* readp, uint32_t byte_ct, CompressStreamState* css_ptr, char** writep_ptr);
91
92 BoolErr UncompressedCswriteCloseNull(CompressStreamState* css_ptr, char* writep);
93
94 BoolErr CompressedCswriteCloseNull(CompressStreamState* css_ptr, char* writep);
95
96 BoolErr CswriteCloseNull(CompressStreamState* css_ptr, char* writep);
97
98 HEADER_INLINE void UncompressedCswriteCloseCond(CompressStreamState* css_ptr, char* writep) {
99 if (css_ptr->overflow_buf) {
100 UncompressedCswriteCloseNull(css_ptr, writep);
101 }
102 }
103
104 HEADER_INLINE void CompressedCswriteCloseCond(CompressStreamState* css_ptr, char* writep) {
105 if (css_ptr->overflow_buf) {
106 CompressedCswriteCloseNull(css_ptr, writep);
107 }
108 }
109
110 HEADER_INLINE void CswriteCloseCond(CompressStreamState* css_ptr, char* writep) {
111 if (css_ptr->overflow_buf) {
112 CswriteCloseNull(css_ptr, writep);
113 }
114 }
115
116
117 #ifdef __cplusplus
118 } // namespace plink2
119 #endif
120
121 #endif // __PLINK2_COMPRESS_STREAM_H__