Debian Codegraph @

plink2_decompress.h

raw
1 #ifndef __PLINK2_DECOMPRESS_H__
2 #define __PLINK2_DECOMPRESS_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 // This has been separated from plink2_cmdline due to the relatively
22 // heavyweight dependence on zstd.
23 #include "include/plink2_text.h"
24 #include "plink2_cmdline.h"
25
26 #ifdef __cplusplus
27 namespace plink2 {
28 #endif
29
30 HEADER_INLINE BoolErr CleanupTextFile2(const char* file_descrip, textFILE* txfp, PglErr* reterrp) {
31 if (unlikely(CleanupTextFile(txfp, reterrp))) {
32 logerrprintfww(kErrprintfFread, file_descrip, strerror(errno));
33 return 1;
34 }
35 return 0;
36 }
37
38 // max_line_blen must be >= kDecompressMinBlen.
39 PglErr InitTextStreamEx(const char* fname, uint32_t alloc_at_end, uint32_t enforced_max_line_blen, uint32_t max_line_blen, uint32_t decompress_thread_ct, TextStream* txsp);
40
41 HEADER_INLINE PglErr InitTextStream(const char* fname, uint32_t max_line_blen, uint32_t decompress_thread_ct, TextStream* txsp) {
42 return InitTextStreamEx(fname, 0, kMaxLongLine, max_line_blen, decompress_thread_ct, txsp);
43 }
44
45 // required_byte_ct can't be greater than kMaxLongLine.
46 // Now ok for byte_avail_ct to be bigstack_left(), since other allocations are
47 // made on the heap instead of the arena (to be more usable in non-plink2
48 // software).
49 // Note that the actual buffer size is max_line_blen + kDecompressChunkSize,
50 // not max_line_blen.
51 HEADER_INLINE BoolErr StandardizeMaxLineBlenEx(uintptr_t byte_avail_ct, uint32_t required_byte_ct, uint32_t* max_line_blenp) {
52 #ifdef __LP64__
53 if (byte_avail_ct >= S_CAST(uintptr_t, kMaxLongLine) + S_CAST(uintptr_t, kDecompressChunkSize)) {
54 *max_line_blenp = kMaxLongLine;
55 return 0;
56 }
57 #endif
58 if (unlikely(byte_avail_ct < kDecompressChunkSize + RoundUpPow2(MAXV(kDecompressMinBlen, required_byte_ct), kCacheline))) {
59 return 1;
60 }
61 *max_line_blenp = RoundDownPow2(byte_avail_ct, kCacheline) - kDecompressChunkSize;
62 return 0;
63 }
64
65 HEADER_INLINE BoolErr StandardizeMaxLineBlen(uintptr_t byte_avail_ct, uint32_t* max_line_blenp) {
66 return StandardizeMaxLineBlenEx(byte_avail_ct, kMaxMediumLine + 1, max_line_blenp);
67 }
68
69 HEADER_INLINE PglErr SizeAndInitTextStream(const char* fname, uintptr_t byte_avail_ct, uint32_t decompress_thread_ct, TextStream* txsp) {
70 // plink 1.9 immediately failed with an out-of-memory error if a "long line"
71 // buffer would be smaller than kMaxMediumLine + 1 bytes, so may as well make
72 // that the default lower bound. (The precise value is currently irrelevant
73 // since kTextStreamBlenLowerBound is larger and we take the maximum of the
74 // two at compile time, but it's useful to distinguish "minimum acceptable
75 // potentially-long-line buffer size" from "load/decompression block size
76 // which generally has good performance".)
77 uint32_t max_line_blen;
78 if (unlikely(StandardizeMaxLineBlen(byte_avail_ct, &max_line_blen))) {
79 return kPglRetNomem;
80 }
81 return InitTextStream(fname, max_line_blen, decompress_thread_ct, txsp);
82 }
83
84 HEADER_INLINE unsigned char* TextStreamMemStart(TextStream* txs_ptr) {
85 // placed here instead of plink2_text.h since it's pretty specific to arena
86 // memory-management
87 return R_CAST(unsigned char*, GET_PRIVATE(*txs_ptr, m).base.dst);
88 }
89
90 // TODO: logputs("\n") first when necessary
91 void TextErrPrint(const char* file_descrip, const char* errmsg, PglErr reterr);
92
93 HEADER_INLINE void TextFileErrPrint(const char* file_descrip, const textFILE* txfp) {
94 TextErrPrint(file_descrip, TextFileError(txfp), TextFileErrcode(txfp));
95 }
96
97 HEADER_INLINE void TextStreamErrPrint(const char* file_descrip, const TextStream* txsp) {
98 TextErrPrint(file_descrip, TextStreamError(txsp), TextStreamErrcode(txsp));
99 }
100
101 HEADER_INLINE void TextStreamErrPrintRewind(const char* file_descrip, const TextStream* txsp, PglErr* reterrp) {
102 if ((*reterrp == kPglRetOpenFail) || (*reterrp == kPglRetEof)) {
103 // attempting to rewind/reopen a pipe file descriptor should manifest as
104 // one of these two errors. (todo: verify that open-fail is possible.)
105 *reterrp = kPglRetRewindFail;
106 }
107 if (*reterrp == kPglRetRewindFail) {
108 logerrprintfww(kErrprintfRewind, file_descrip);
109 } else {
110 TextStreamErrPrint(file_descrip, txsp);
111 }
112 }
113
114 HEADER_INLINE BoolErr CleanupTextStream2(const char* file_descrip, TextStream* txsp, PglErr* reterrp) {
115 if (unlikely(CleanupTextStream(txsp, reterrp))) {
116 logerrprintfww(kErrprintfFread, file_descrip, strerror(errno));
117 return 1;
118 }
119 return 0;
120 }
121
122
123 HEADER_INLINE PglErr InitTokenStreamEx(const char* fname, uint32_t alloc_at_end, uint32_t decompress_thread_ct, TokenStream* tksp) {
124 return InitTextStreamEx(fname, alloc_at_end, 0, kTokenStreamBlen - kDecompressChunkSize, decompress_thread_ct, &(tksp->txs));
125 }
126
127 HEADER_INLINE PglErr InitTokenStream(const char* fname, uint32_t decompress_thread_ct, TokenStream* tksp) {
128 return InitTextStreamEx(fname, 0, 0, kTokenStreamBlen - kDecompressChunkSize, decompress_thread_ct, &(tksp->txs));
129 }
130
131 HEADER_INLINE void TokenStreamErrPrint(const char* file_descrip, const TokenStream* tksp) {
132 PglErr reterr = TokenStreamErrcode(tksp);
133 const char* errmsg = TokenStreamError(tksp);
134 if (reterr != kPglRetMalformedInput) {
135 TextErrPrint(file_descrip, errmsg, reterr);
136 } else {
137 logerrprintfww("Error: Pathologically long token in %s.\n", file_descrip);
138 }
139 }
140
141 HEADER_INLINE BoolErr CleanupTokenStream2(const char* file_descrip, TokenStream* tksp, PglErr* reterrp) {
142 if (unlikely(CleanupTokenStream(tksp, reterrp))) {
143 logerrprintfww(kErrprintfFread, file_descrip, strerror(errno));
144 return 1;
145 }
146 return 0;
147 }
148
149 HEADER_INLINE BoolErr CleanupTokenStream3(const char* file_descrip, TokenStream* tksp, PglErr* reterrp) {
150 *reterrp = kPglRetSuccess;
151 return CleanupTokenStream2(file_descrip, tksp, reterrp);
152 }
153
154 #ifdef __cplusplus
155 } // namespace plink2
156 #endif
157
158 #endif // __PLINK2_DECOMPRESS_H__