forked from Linaro/uadk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wd_zlibwrapper.c
343 lines (262 loc) · 7.18 KB
/
wd_zlibwrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2022 Huawei Technologies Co.,Ltd. All rights reserved.
*/
/* === Dependencies === */
#define _GNU_SOURCE
#include <errno.h>
#include <math.h>
#include <numa.h>
#include <stdlib.h>
#include <stdio.h>
#include "wd.h"
#include "wd_comp.h"
#include "wd_sched.h"
#include "wd_util.h"
#include "wd_zlibwrapper.h"
#include "drv/wd_comp_drv.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
enum uadk_init_status {
WD_ZLIB_UNINIT,
WD_ZLIB_INIT,
};
struct wd_zlibwrapper_config {
int count;
int status;
};
static pthread_mutex_t wd_zlib_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct wd_zlibwrapper_config zlib_config = {0};
static void wd_zlib_unlock(void)
{
pthread_mutex_unlock(&wd_zlib_mutex);
zlib_config.status = WD_ZLIB_UNINIT;
}
static int wd_zlib_init(void)
{
struct wd_ctx_nums *ctx_set_num;
struct wd_ctx_params cparams;
int ret, i;
if (zlib_config.status == WD_ZLIB_INIT)
return 0;
ctx_set_num = calloc(WD_DIR_MAX, sizeof(*ctx_set_num));
if (!ctx_set_num) {
WD_ERR("failed to alloc ctx_set_size!\n");
return -WD_ENOMEM;
}
cparams.op_type_num = WD_DIR_MAX;
cparams.ctx_set_num = ctx_set_num;
cparams.bmp = numa_allocate_nodemask();
if (!cparams.bmp) {
WD_ERR("failed to create nodemask!\n");
ret = -WD_ENOMEM;
goto out_freectx;
}
numa_bitmask_setall(cparams.bmp);
for (i = 0; i < WD_DIR_MAX; i++)
ctx_set_num[i].sync_ctx_num = 2;
ret = wd_comp_init2_("zlib", 0, 0, &cparams);
if (ret)
goto out_freebmp;
zlib_config.status = WD_ZLIB_INIT;
out_freebmp:
numa_free_nodemask(cparams.bmp);
out_freectx:
free(ctx_set_num);
return ret;
}
static void wd_zlib_uninit(void)
{
wd_comp_uninit2();
zlib_config.status = WD_ZLIB_UNINIT;
}
static int wd_zlib_analy_alg(int windowbits, int *alg, int *windowsize)
{
static const int ZLIB_MAX_WBITS = 15;
static const int ZLIB_MIN_WBITS = 8;
static const int GZIP_MAX_WBITS = 31;
static const int GZIP_MIN_WBITS = 24;
static const int DEFLATE_MAX_WBITS = -8;
static const int DEFLATE_MIN_WBITS = -15;
static const int WBINS_ZLIB_4K = 12;
static const int WBINS_GZIP_4K = 27;
static const int WBINS_DEFLATE_4K = -12;
int ret = Z_STREAM_ERROR;
if ((windowbits >= ZLIB_MIN_WBITS) && (windowbits <= ZLIB_MAX_WBITS)) {
*alg = WD_ZLIB;
*windowsize = max(windowbits - WBINS_ZLIB_4K, WD_COMP_WS_4K);
ret = Z_OK;
} else if ((windowbits >= GZIP_MIN_WBITS) && (windowbits <= GZIP_MAX_WBITS)) {
*alg = WD_GZIP;
*windowsize = max(windowbits - WBINS_GZIP_4K, WD_COMP_WS_4K);
ret = Z_OK;
} else if ((windowbits >= DEFLATE_MIN_WBITS) && (windowbits <= DEFLATE_MAX_WBITS)) {
*alg = WD_DEFLATE;
*windowsize = max(windowbits - WBINS_DEFLATE_4K, WD_COMP_WS_4K);
ret = Z_OK;
}
return ret;
}
static int wd_zlib_alloc_sess(z_streamp strm, int level, int windowbits, enum wd_comp_op_type type)
{
struct wd_comp_sess_setup setup = {0};
struct sched_params sparams = {0};
int windowsize, alg, ret;
handle_t h_sess;
ret = wd_zlib_analy_alg(windowbits, &alg, &windowsize);
if (ret < 0) {
WD_ERR("invalid: windowbits is %d!\n", windowbits);
return ret;
}
setup.comp_lv = level;
setup.alg_type = alg;
setup.win_sz = windowsize;
setup.op_type = type;
sparams.type = type;
setup.sched_param = &sparams;
h_sess = wd_comp_alloc_sess(&setup);
if (!h_sess) {
WD_ERR("failed to alloc comp sess!\n");
return Z_STREAM_ERROR;
}
strm->reserved = (__u64)h_sess;
return Z_OK;
}
static void wd_zlib_free_sess(z_streamp strm)
{
wd_comp_free_sess((handle_t)strm->reserved);
}
static int wd_zlib_do_request(z_streamp strm, int flush, enum wd_comp_op_type type)
{
handle_t h_sess = strm->reserved;
struct wd_comp_req req = {0};
int src_len = strm->avail_in;
int dst_len = strm->avail_out;
int ret;
if (unlikely(flush != Z_SYNC_FLUSH && flush != Z_FINISH)) {
WD_ERR("invalid: flush is %d!\n", flush);
return Z_STREAM_ERROR;
}
req.src = (void *)strm->next_in;
req.src_len = strm->avail_in;
req.dst = (void *)strm->next_out;
req.dst_len = strm->avail_out;
req.op_type = type;
req.data_fmt = WD_FLAT_BUF;
req.last = (flush == Z_FINISH) ? 1 : 0;
ret = wd_do_comp_strm(h_sess, &req);
if (unlikely(ret)) {
WD_ERR("failed to do compress(%d)!\n", ret);
return Z_STREAM_ERROR;
}
strm->avail_in = src_len - req.src_len;
strm->avail_out = dst_len - req.dst_len;
strm->total_in += req.src_len;
strm->total_out += req.dst_len;
if (flush == Z_FINISH && req.src_len == src_len)
ret = Z_STREAM_END;
return ret;
}
/* === Compression === */
int wd_deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
{
return wd_deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
Z_DEFAULT_STRATEGY, version, stream_size);
}
int wd_deflateInit2_(z_streamp strm, int level, int method, int windowBits,
int memLevel, int strategy, const char *version, int stream_size)
{
int ret;
pthread_atfork(NULL, NULL, wd_zlib_unlock);
pthread_mutex_lock(&wd_zlib_mutex);
ret = wd_zlib_init();
if (unlikely(ret < 0))
goto out_unlock;
strm->total_in = 0;
strm->total_out = 0;
ret = wd_zlib_alloc_sess(strm, level, windowBits, WD_DIR_COMPRESS);
if (unlikely(ret < 0))
goto out_uninit;
__atomic_add_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED);
pthread_mutex_unlock(&wd_zlib_mutex);
return 0;
out_uninit:
wd_zlib_uninit();
out_unlock:
pthread_mutex_unlock(&wd_zlib_mutex);
return ret;
}
int wd_deflate(z_streamp strm, int flush)
{
return wd_zlib_do_request(strm, flush, WD_DIR_COMPRESS);
}
int wd_deflateReset(z_streamp strm)
{
wd_comp_reset_sess((handle_t)strm->reserved);
strm->total_in = 0;
strm->total_out = 0;
return Z_OK;
}
int wd_deflateEnd(z_streamp strm)
{
int ret;
wd_zlib_free_sess(strm);
pthread_mutex_lock(&wd_zlib_mutex);
ret = __atomic_sub_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED);
if (ret != 0)
goto out_unlock;
wd_zlib_uninit();
out_unlock:
pthread_mutex_unlock(&wd_zlib_mutex);
return Z_OK;
}
/* === Decompression === */
int wd_inflateInit_(z_streamp strm, const char *version, int stream_size)
{
return wd_inflateInit2_(strm, MAX_WBITS, version, stream_size);
}
int wd_inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
{
int ret;
pthread_atfork(NULL, NULL, wd_zlib_unlock);
pthread_mutex_lock(&wd_zlib_mutex);
ret = wd_zlib_init();
if (unlikely(ret < 0))
goto out_unlock;
strm->total_in = 0;
strm->total_out = 0;
ret = wd_zlib_alloc_sess(strm, 0, windowBits, WD_DIR_DECOMPRESS);
if (unlikely(ret < 0))
goto out_uninit;
__atomic_add_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED);
pthread_mutex_unlock(&wd_zlib_mutex);
return 0;
out_uninit:
wd_zlib_uninit();
out_unlock:
pthread_mutex_unlock(&wd_zlib_mutex);
return ret;
}
int wd_inflate(z_streamp strm, int flush)
{
return wd_zlib_do_request(strm, flush, WD_DIR_DECOMPRESS);
}
int wd_inflateReset(z_streamp strm)
{
wd_comp_reset_sess((handle_t)strm->reserved);
strm->total_in = 0;
strm->total_out = 0;
return Z_OK;
}
int wd_inflateEnd(z_streamp strm)
{
int ret;
wd_zlib_free_sess(strm);
pthread_mutex_lock(&wd_zlib_mutex);
ret = __atomic_sub_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED);
if (ret != 0)
goto out_unlock;
wd_zlib_uninit();
out_unlock:
pthread_mutex_unlock(&wd_zlib_mutex);
return Z_OK;
}