| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "filter_encoder.h" |
| 13 |
#include "easy_preset.h" |
| 14 |
#include "block_encoder.h" |
| 15 |
#include "block_buffer_encoder.h" |
| 16 |
#include "index_encoder.h" |
| 17 |
#include "outqueue.h" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#define BLOCK_SIZE_MAX (UINT64_MAX / LZMA_THREADS_MAX) |
| 23 |
|
| 24 |
|
| 25 |
typedef enum { |
| 26 |
|
| 27 |
THR_IDLE, |
| 28 |
|
| 29 |
|
| 30 |
THR_RUN, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
THR_FINISH, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
THR_STOP, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
THR_EXIT, |
| 43 |
|
| 44 |
} worker_state; |
| 45 |
|
| 46 |
typedef struct lzma_stream_coder_s lzma_stream_coder; |
| 47 |
|
| 48 |
typedef struct worker_thread_s worker_thread; |
| 49 |
struct worker_thread_s { |
| 50 |
worker_state state; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
uint8_t *in; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
size_t in_size; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
lzma_outbuf *outbuf; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
lzma_stream_coder *coder; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
const lzma_allocator *allocator; |
| 74 |
|
| 75 |
|
| 76 |
uint64_t progress_in; |
| 77 |
|
| 78 |
|
| 79 |
uint64_t progress_out; |
| 80 |
|
| 81 |
|
| 82 |
lzma_next_coder block_encoder; |
| 83 |
|
| 84 |
|
| 85 |
lzma_block block_options; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 91 |
|
| 92 |
|
| 93 |
worker_thread *next; |
| 94 |
|
| 95 |
mythread_mutex mutex; |
| 96 |
mythread_cond cond; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
mythread thread_id; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
struct lzma_stream_coder_s { |
| 105 |
enum { |
| 106 |
SEQ_STREAM_HEADER, |
| 107 |
SEQ_BLOCK, |
| 108 |
SEQ_INDEX, |
| 109 |
SEQ_STREAM_FOOTER, |
| 110 |
} sequence; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
size_t block_size; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
lzma_filter filters_cache[LZMA_FILTERS_MAX + 1]; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
lzma_index *index; |
| 135 |
|
| 136 |
|
| 137 |
lzma_next_coder index_encoder; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
lzma_stream_flags stream_flags; |
| 142 |
|
| 143 |
|
| 144 |
uint8_t header[LZMA_STREAM_HEADER_SIZE]; |
| 145 |
|
| 146 |
|
| 147 |
size_t header_pos; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
lzma_outq outq; |
| 152 |
|
| 153 |
|
| 154 |
size_t outbuf_alloc_size; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
uint32_t timeout; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
lzma_ret thread_error; |
| 164 |
|
| 165 |
|
| 166 |
worker_thread *threads; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
uint32_t threads_max; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
uint32_t threads_initialized; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
worker_thread *threads_free; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
worker_thread *thr; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
uint64_t progress_in; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
uint64_t progress_out; |
| 193 |
|
| 194 |
|
| 195 |
mythread_mutex mutex; |
| 196 |
mythread_cond cond; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
static void |
| 202 |
worker_error(worker_thread *thr, lzma_ret ret) |
| 203 |
{ |
| 204 |
assert(ret != LZMA_OK); |
| 205 |
assert(ret != LZMA_STREAM_END); |
| 206 |
|
| 207 |
mythread_sync(thr->coder->mutex) { |
| 208 |
if (thr->coder->thread_error == LZMA_OK) |
| 209 |
thr->coder->thread_error = ret; |
| 210 |
|
| 211 |
mythread_cond_signal(&thr->coder->cond); |
| 212 |
} |
| 213 |
|
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
static worker_state |
| 219 |
worker_encode(worker_thread *thr, size_t *out_pos, worker_state state) |
| 220 |
{ |
| 221 |
assert(thr->progress_in == 0); |
| 222 |
assert(thr->progress_out == 0); |
| 223 |
|
| 224 |
|
| 225 |
thr->block_options = (lzma_block){ |
| 226 |
.version = 0, |
| 227 |
.check = thr->coder->stream_flags.check, |
| 228 |
.compressed_size = thr->outbuf->allocated, |
| 229 |
.uncompressed_size = thr->coder->block_size, |
| 230 |
.filters = thr->filters, |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
lzma_ret ret = lzma_block_header_size(&thr->block_options); |
| 238 |
if (ret != LZMA_OK) { |
| 239 |
worker_error(thr, ret); |
| 240 |
return THR_STOP; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
ret = lzma_block_encoder_init(&thr->block_encoder, |
| 245 |
thr->allocator, &thr->block_options); |
| 246 |
if (ret != LZMA_OK) { |
| 247 |
worker_error(thr, ret); |
| 248 |
return THR_STOP; |
| 249 |
} |
| 250 |
|
| 251 |
size_t in_pos = 0; |
| 252 |
size_t in_size = 0; |
| 253 |
|
| 254 |
*out_pos = thr->block_options.header_size; |
| 255 |
const size_t out_size = thr->outbuf->allocated; |
| 256 |
|
| 257 |
do { |
| 258 |
mythread_sync(thr->mutex) { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
thr->progress_in = in_pos; |
| 267 |
thr->progress_out = *out_pos; |
| 268 |
|
| 269 |
while (in_size == thr->in_size |
| 270 |
&& thr->state == THR_RUN) |
| 271 |
mythread_cond_wait(&thr->cond, &thr->mutex); |
| 272 |
|
| 273 |
state = thr->state; |
| 274 |
in_size = thr->in_size; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
if (state >= THR_STOP) |
| 279 |
return state; |
| 280 |
|
| 281 |
lzma_action action = state == THR_FINISH |
| 282 |
? LZMA_FINISH : LZMA_RUN; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
static const size_t in_chunk_max = 16384; |
| 288 |
size_t in_limit = in_size; |
| 289 |
if (in_size - in_pos > in_chunk_max) { |
| 290 |
in_limit = in_pos + in_chunk_max; |
| 291 |
action = LZMA_RUN; |
| 292 |
} |
| 293 |
|
| 294 |
ret = thr->block_encoder.code( |
| 295 |
thr->block_encoder.coder, thr->allocator, |
| 296 |
thr->in, &in_pos, in_limit, thr->outbuf->buf, |
| 297 |
out_pos, out_size, action); |
| 298 |
} while (ret == LZMA_OK && *out_pos < out_size); |
| 299 |
|
| 300 |
switch (ret) { |
| 301 |
case LZMA_STREAM_END: |
| 302 |
assert(state == THR_FINISH); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
ret = lzma_block_header_encode(&thr->block_options, |
| 308 |
thr->outbuf->buf); |
| 309 |
if (ret != LZMA_OK) { |
| 310 |
worker_error(thr, ret); |
| 311 |
return THR_STOP; |
| 312 |
} |
| 313 |
|
| 314 |
break; |
| 315 |
|
| 316 |
case LZMA_OK: |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
mythread_sync(thr->mutex) { |
| 322 |
while (thr->state == THR_RUN) |
| 323 |
mythread_cond_wait(&thr->cond, &thr->mutex); |
| 324 |
|
| 325 |
state = thr->state; |
| 326 |
in_size = thr->in_size; |
| 327 |
} |
| 328 |
|
| 329 |
if (state >= THR_STOP) |
| 330 |
return state; |
| 331 |
|
| 332 |
|
| 333 |
*out_pos = 0; |
| 334 |
ret = lzma_block_uncomp_encode(&thr->block_options, |
| 335 |
thr->in, in_size, thr->outbuf->buf, |
| 336 |
out_pos, out_size); |
| 337 |
|
| 338 |
|
| 339 |
if (ret != LZMA_OK) { |
| 340 |
worker_error(thr, LZMA_PROG_ERROR); |
| 341 |
return THR_STOP; |
| 342 |
} |
| 343 |
|
| 344 |
break; |
| 345 |
|
| 346 |
default: |
| 347 |
worker_error(thr, ret); |
| 348 |
return THR_STOP; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
thr->outbuf->unpadded_size |
| 354 |
= lzma_block_unpadded_size(&thr->block_options); |
| 355 |
assert(thr->outbuf->unpadded_size != 0); |
| 356 |
thr->outbuf->uncompressed_size = thr->block_options.uncompressed_size; |
| 357 |
|
| 358 |
return THR_FINISH; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
static MYTHREAD_RET_TYPE |
| 363 |
worker_start(void *thr_ptr) |
| 364 |
{ |
| 365 |
worker_thread *thr = thr_ptr; |
| 366 |
worker_state state = THR_IDLE; |
| 367 |
|
| 368 |
while (true) { |
| 369 |
|
| 370 |
mythread_sync(thr->mutex) { |
| 371 |
while (true) { |
| 372 |
|
| 373 |
|
| 374 |
if (thr->state == THR_STOP) { |
| 375 |
thr->state = THR_IDLE; |
| 376 |
mythread_cond_signal(&thr->cond); |
| 377 |
} |
| 378 |
|
| 379 |
state = thr->state; |
| 380 |
if (state != THR_IDLE) |
| 381 |
break; |
| 382 |
|
| 383 |
mythread_cond_wait(&thr->cond, &thr->mutex); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
size_t out_pos = 0; |
| 388 |
|
| 389 |
assert(state != THR_IDLE); |
| 390 |
assert(state != THR_STOP); |
| 391 |
|
| 392 |
if (state <= THR_FINISH) |
| 393 |
state = worker_encode(thr, &out_pos, state); |
| 394 |
|
| 395 |
if (state == THR_EXIT) |
| 396 |
break; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
mythread_sync(thr->mutex) { |
| 402 |
if (thr->state != THR_EXIT) { |
| 403 |
thr->state = THR_IDLE; |
| 404 |
mythread_cond_signal(&thr->cond); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
mythread_sync(thr->coder->mutex) { |
| 409 |
|
| 410 |
|
| 411 |
if (state == THR_FINISH) { |
| 412 |
thr->outbuf->pos = out_pos; |
| 413 |
thr->outbuf->finished = true; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
thr->coder->progress_in |
| 418 |
+= thr->outbuf->uncompressed_size; |
| 419 |
thr->coder->progress_out += out_pos; |
| 420 |
thr->progress_in = 0; |
| 421 |
thr->progress_out = 0; |
| 422 |
|
| 423 |
|
| 424 |
thr->next = thr->coder->threads_free; |
| 425 |
thr->coder->threads_free = thr; |
| 426 |
|
| 427 |
mythread_cond_signal(&thr->coder->cond); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
lzma_filters_free(thr->filters, thr->allocator); |
| 433 |
|
| 434 |
mythread_mutex_destroy(&thr->mutex); |
| 435 |
mythread_cond_destroy(&thr->cond); |
| 436 |
|
| 437 |
lzma_next_end(&thr->block_encoder, thr->allocator); |
| 438 |
lzma_free(thr->in, thr->allocator); |
| 439 |
return MYTHREAD_RET_VALUE; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
static void |
| 445 |
threads_stop(lzma_stream_coder *coder, bool wait_for_threads) |
| 446 |
{ |
| 447 |
|
| 448 |
for (uint32_t i = 0; i < coder->threads_initialized; ++i) { |
| 449 |
mythread_sync(coder->threads[i].mutex) { |
| 450 |
coder->threads[i].state = THR_STOP; |
| 451 |
mythread_cond_signal(&coder->threads[i].cond); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
if (!wait_for_threads) |
| 456 |
return; |
| 457 |
|
| 458 |
|
| 459 |
for (uint32_t i = 0; i < coder->threads_initialized; ++i) { |
| 460 |
mythread_sync(coder->threads[i].mutex) { |
| 461 |
while (coder->threads[i].state != THR_IDLE) |
| 462 |
mythread_cond_wait(&coder->threads[i].cond, |
| 463 |
&coder->threads[i].mutex); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
return; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
static void |
| 474 |
threads_end(lzma_stream_coder *coder, const lzma_allocator *allocator) |
| 475 |
{ |
| 476 |
for (uint32_t i = 0; i < coder->threads_initialized; ++i) { |
| 477 |
mythread_sync(coder->threads[i].mutex) { |
| 478 |
coder->threads[i].state = THR_EXIT; |
| 479 |
mythread_cond_signal(&coder->threads[i].cond); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
for (uint32_t i = 0; i < coder->threads_initialized; ++i) { |
| 484 |
int ret = mythread_join(coder->threads[i].thread_id); |
| 485 |
assert(ret == 0); |
| 486 |
(void)ret; |
| 487 |
} |
| 488 |
|
| 489 |
lzma_free(coder->threads, allocator); |
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
static lzma_ret |
| 496 |
initialize_new_thread(lzma_stream_coder *coder, |
| 497 |
const lzma_allocator *allocator) |
| 498 |
{ |
| 499 |
worker_thread *thr = &coder->threads[coder->threads_initialized]; |
| 500 |
|
| 501 |
thr->in = lzma_alloc(coder->block_size, allocator); |
| 502 |
if (thr->in == NULL) |
| 503 |
return LZMA_MEM_ERROR; |
| 504 |
|
| 505 |
if (mythread_mutex_init(&thr->mutex)) |
| 506 |
goto error_mutex; |
| 507 |
|
| 508 |
if (mythread_cond_init(&thr->cond)) |
| 509 |
goto error_cond; |
| 510 |
|
| 511 |
thr->state = THR_IDLE; |
| 512 |
thr->allocator = allocator; |
| 513 |
thr->coder = coder; |
| 514 |
thr->progress_in = 0; |
| 515 |
thr->progress_out = 0; |
| 516 |
thr->block_encoder = LZMA_NEXT_CODER_INIT; |
| 517 |
thr->filters[0].id = LZMA_VLI_UNKNOWN; |
| 518 |
|
| 519 |
if (mythread_create(&thr->thread_id, &worker_start, thr)) |
| 520 |
goto error_thread; |
| 521 |
|
| 522 |
++coder->threads_initialized; |
| 523 |
coder->thr = thr; |
| 524 |
|
| 525 |
return LZMA_OK; |
| 526 |
|
| 527 |
error_thread: |
| 528 |
mythread_cond_destroy(&thr->cond); |
| 529 |
|
| 530 |
error_cond: |
| 531 |
mythread_mutex_destroy(&thr->mutex); |
| 532 |
|
| 533 |
error_mutex: |
| 534 |
lzma_free(thr->in, allocator); |
| 535 |
return LZMA_MEM_ERROR; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
static lzma_ret |
| 540 |
get_thread(lzma_stream_coder *coder, const lzma_allocator *allocator) |
| 541 |
{ |
| 542 |
|
| 543 |
|
| 544 |
if (!lzma_outq_has_buf(&coder->outq)) |
| 545 |
return LZMA_OK; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
return_if_error(lzma_outq_prealloc_buf(&coder->outq, allocator, |
| 550 |
coder->outbuf_alloc_size)); |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
if (coder->filters_cache[0].id == LZMA_VLI_UNKNOWN) |
| 556 |
return_if_error(lzma_filters_copy( |
| 557 |
coder->filters, coder->filters_cache, allocator)); |
| 558 |
|
| 559 |
|
| 560 |
mythread_sync(coder->mutex) { |
| 561 |
if (coder->threads_free != NULL) { |
| 562 |
coder->thr = coder->threads_free; |
| 563 |
coder->threads_free = coder->threads_free->next; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
if (coder->thr == NULL) { |
| 568 |
|
| 569 |
if (coder->threads_initialized == coder->threads_max) |
| 570 |
return LZMA_OK; |
| 571 |
|
| 572 |
|
| 573 |
return_if_error(initialize_new_thread(coder, allocator)); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
mythread_sync(coder->thr->mutex) { |
| 579 |
coder->thr->state = THR_RUN; |
| 580 |
coder->thr->in_size = 0; |
| 581 |
coder->thr->outbuf = lzma_outq_get_buf(&coder->outq, NULL); |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
lzma_filters_free(coder->thr->filters, allocator); |
| 587 |
memcpy(coder->thr->filters, coder->filters_cache, |
| 588 |
sizeof(coder->filters_cache)); |
| 589 |
coder->filters_cache[0].id = LZMA_VLI_UNKNOWN; |
| 590 |
|
| 591 |
mythread_cond_signal(&coder->thr->cond); |
| 592 |
} |
| 593 |
|
| 594 |
return LZMA_OK; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
static lzma_ret |
| 599 |
stream_encode_in(lzma_stream_coder *coder, const lzma_allocator *allocator, |
| 600 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 601 |
size_t in_size, lzma_action action) |
| 602 |
{ |
| 603 |
while (*in_pos < in_size |
| 604 |
|| (coder->thr != NULL && action != LZMA_RUN)) { |
| 605 |
if (coder->thr == NULL) { |
| 606 |
|
| 607 |
const lzma_ret ret = get_thread(coder, allocator); |
| 608 |
if (coder->thr == NULL) |
| 609 |
return ret; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
size_t thr_in_size = coder->thr->in_size; |
| 614 |
lzma_bufcpy(in, in_pos, in_size, coder->thr->in, |
| 615 |
&thr_in_size, coder->block_size); |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
const bool finish = thr_in_size == coder->block_size |
| 624 |
|| (*in_pos == in_size && action != LZMA_RUN); |
| 625 |
|
| 626 |
bool block_error = false; |
| 627 |
|
| 628 |
mythread_sync(coder->thr->mutex) { |
| 629 |
if (coder->thr->state == THR_IDLE) { |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
block_error = true; |
| 634 |
} else { |
| 635 |
|
| 636 |
|
| 637 |
coder->thr->in_size = thr_in_size; |
| 638 |
|
| 639 |
if (finish) |
| 640 |
coder->thr->state = THR_FINISH; |
| 641 |
|
| 642 |
mythread_cond_signal(&coder->thr->cond); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
if (block_error) { |
| 647 |
lzma_ret ret = LZMA_OK; |
| 648 |
|
| 649 |
mythread_sync(coder->mutex) { |
| 650 |
ret = coder->thread_error; |
| 651 |
} |
| 652 |
|
| 653 |
return ret; |
| 654 |
} |
| 655 |
|
| 656 |
if (finish) |
| 657 |
coder->thr = NULL; |
| 658 |
} |
| 659 |
|
| 660 |
return LZMA_OK; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
static bool |
| 667 |
wait_for_work(lzma_stream_coder *coder, mythread_condtime *wait_abs, |
| 668 |
bool *has_blocked, bool has_input) |
| 669 |
{ |
| 670 |
if (coder->timeout != 0 && !*has_blocked) { |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
*has_blocked = true; |
| 684 |
mythread_condtime_set(wait_abs, &coder->cond, coder->timeout); |
| 685 |
} |
| 686 |
|
| 687 |
bool timed_out = false; |
| 688 |
|
| 689 |
mythread_sync(coder->mutex) { |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
while ((!has_input || coder->threads_free == NULL |
| 698 |
|| !lzma_outq_has_buf(&coder->outq)) |
| 699 |
&& !lzma_outq_is_readable(&coder->outq) |
| 700 |
&& coder->thread_error == LZMA_OK |
| 701 |
&& !timed_out) { |
| 702 |
if (coder->timeout != 0) |
| 703 |
timed_out = mythread_cond_timedwait( |
| 704 |
&coder->cond, &coder->mutex, |
| 705 |
wait_abs) != 0; |
| 706 |
else |
| 707 |
mythread_cond_wait(&coder->cond, |
| 708 |
&coder->mutex); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
return timed_out; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
static lzma_ret |
| 717 |
stream_encode_mt(void *coder_ptr, const lzma_allocator *allocator, |
| 718 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 719 |
size_t in_size, uint8_t *restrict out, |
| 720 |
size_t *restrict out_pos, size_t out_size, lzma_action action) |
| 721 |
{ |
| 722 |
lzma_stream_coder *coder = coder_ptr; |
| 723 |
|
| 724 |
switch (coder->sequence) { |
| 725 |
case SEQ_STREAM_HEADER: |
| 726 |
lzma_bufcpy(coder->header, &coder->header_pos, |
| 727 |
sizeof(coder->header), |
| 728 |
out, out_pos, out_size); |
| 729 |
if (coder->header_pos < sizeof(coder->header)) |
| 730 |
return LZMA_OK; |
| 731 |
|
| 732 |
coder->header_pos = 0; |
| 733 |
coder->sequence = SEQ_BLOCK; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
case SEQ_BLOCK: { |
| 738 |
|
| 739 |
lzma_vli unpadded_size = 0; |
| 740 |
lzma_vli uncompressed_size = 0; |
| 741 |
lzma_ret ret = LZMA_OK; |
| 742 |
|
| 743 |
|
| 744 |
bool has_blocked = false; |
| 745 |
mythread_condtime wait_abs = { 0 }; |
| 746 |
|
| 747 |
while (true) { |
| 748 |
mythread_sync(coder->mutex) { |
| 749 |
|
| 750 |
ret = coder->thread_error; |
| 751 |
if (ret != LZMA_OK) { |
| 752 |
assert(ret != LZMA_STREAM_END); |
| 753 |
break; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
ret = lzma_outq_read(&coder->outq, allocator, |
| 758 |
out, out_pos, out_size, |
| 759 |
&unpadded_size, |
| 760 |
&uncompressed_size); |
| 761 |
} |
| 762 |
|
| 763 |
if (ret == LZMA_STREAM_END) { |
| 764 |
|
| 765 |
ret = lzma_index_append(coder->index, |
| 766 |
allocator, unpadded_size, |
| 767 |
uncompressed_size); |
| 768 |
if (ret != LZMA_OK) { |
| 769 |
threads_stop(coder, false); |
| 770 |
return ret; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
if (*out_pos < out_size) |
| 777 |
continue; |
| 778 |
} |
| 779 |
|
| 780 |
if (ret != LZMA_OK) { |
| 781 |
|
| 782 |
threads_stop(coder, false); |
| 783 |
return ret; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
ret = stream_encode_in(coder, allocator, |
| 788 |
in, in_pos, in_size, action); |
| 789 |
if (ret != LZMA_OK) { |
| 790 |
threads_stop(coder, false); |
| 791 |
return ret; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
if (*in_pos == in_size) { |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
if (action == LZMA_RUN) |
| 802 |
return LZMA_OK; |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
if (action == LZMA_FULL_BARRIER) |
| 808 |
return LZMA_STREAM_END; |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
if (lzma_outq_is_empty(&coder->outq)) { |
| 814 |
|
| 815 |
|
| 816 |
if (action == LZMA_FINISH) |
| 817 |
break; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
if (action == LZMA_FULL_FLUSH) |
| 823 |
return LZMA_STREAM_END; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if (*out_pos == out_size) |
| 832 |
return LZMA_OK; |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if (wait_for_work(coder, &wait_abs, &has_blocked, |
| 837 |
*in_pos < in_size)) |
| 838 |
return LZMA_TIMED_OUT; |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
return_if_error(lzma_index_encoder_init( |
| 844 |
&coder->index_encoder, allocator, |
| 845 |
coder->index)); |
| 846 |
coder->sequence = SEQ_INDEX; |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
coder->progress_out += lzma_index_size(coder->index) |
| 853 |
+ LZMA_STREAM_HEADER_SIZE; |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
case SEQ_INDEX: { |
| 859 |
|
| 860 |
|
| 861 |
const lzma_ret ret = coder->index_encoder.code( |
| 862 |
coder->index_encoder.coder, allocator, |
| 863 |
NULL, NULL, 0, |
| 864 |
out, out_pos, out_size, LZMA_RUN); |
| 865 |
if (ret != LZMA_STREAM_END) |
| 866 |
return ret; |
| 867 |
|
| 868 |
|
| 869 |
coder->stream_flags.backward_size |
| 870 |
= lzma_index_size(coder->index); |
| 871 |
if (lzma_stream_footer_encode(&coder->stream_flags, |
| 872 |
coder->header) != LZMA_OK) |
| 873 |
return LZMA_PROG_ERROR; |
| 874 |
|
| 875 |
coder->sequence = SEQ_STREAM_FOOTER; |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
case SEQ_STREAM_FOOTER: |
| 881 |
lzma_bufcpy(coder->header, &coder->header_pos, |
| 882 |
sizeof(coder->header), |
| 883 |
out, out_pos, out_size); |
| 884 |
return coder->header_pos < sizeof(coder->header) |
| 885 |
? LZMA_OK : LZMA_STREAM_END; |
| 886 |
} |
| 887 |
|
| 888 |
assert(0); |
| 889 |
return LZMA_PROG_ERROR; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
static void |
| 894 |
stream_encoder_mt_end(void *coder_ptr, const lzma_allocator *allocator) |
| 895 |
{ |
| 896 |
lzma_stream_coder *coder = coder_ptr; |
| 897 |
|
| 898 |
|
| 899 |
threads_end(coder, allocator); |
| 900 |
lzma_outq_end(&coder->outq, allocator); |
| 901 |
|
| 902 |
lzma_filters_free(coder->filters, allocator); |
| 903 |
lzma_filters_free(coder->filters_cache, allocator); |
| 904 |
|
| 905 |
lzma_next_end(&coder->index_encoder, allocator); |
| 906 |
lzma_index_end(coder->index, allocator); |
| 907 |
|
| 908 |
mythread_cond_destroy(&coder->cond); |
| 909 |
mythread_mutex_destroy(&coder->mutex); |
| 910 |
|
| 911 |
lzma_free(coder, allocator); |
| 912 |
return; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
static lzma_ret |
| 917 |
stream_encoder_mt_update(void *coder_ptr, const lzma_allocator *allocator, |
| 918 |
const lzma_filter *filters, |
| 919 |
const lzma_filter *reversed_filters |
| 920 |
lzma_attribute((__unused__))) |
| 921 |
{ |
| 922 |
lzma_stream_coder *coder = coder_ptr; |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
if (coder->sequence > SEQ_BLOCK) |
| 927 |
return LZMA_PROG_ERROR; |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
if (coder->thr != NULL) |
| 932 |
return LZMA_PROG_ERROR; |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
if (lzma_raw_encoder_memusage(filters) == UINT64_MAX) |
| 937 |
return LZMA_OPTIONS_ERROR; |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
lzma_filter temp[LZMA_FILTERS_MAX + 1]; |
| 942 |
return_if_error(lzma_filters_copy(filters, temp, allocator)); |
| 943 |
|
| 944 |
|
| 945 |
lzma_filters_free(coder->filters, allocator); |
| 946 |
lzma_filters_free(coder->filters_cache, allocator); |
| 947 |
|
| 948 |
|
| 949 |
memcpy(coder->filters, temp, sizeof(temp)); |
| 950 |
|
| 951 |
return LZMA_OK; |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
static lzma_ret |
| 958 |
get_options(const lzma_mt *options, lzma_options_easy *opt_easy, |
| 959 |
const lzma_filter **filters, uint64_t *block_size, |
| 960 |
uint64_t *outbuf_size_max) |
| 961 |
{ |
| 962 |
|
| 963 |
if (options == NULL) |
| 964 |
return LZMA_PROG_ERROR; |
| 965 |
|
| 966 |
if (options->flags != 0 || options->threads == 0 |
| 967 |
|| options->threads > LZMA_THREADS_MAX) |
| 968 |
return LZMA_OPTIONS_ERROR; |
| 969 |
|
| 970 |
if (options->filters != NULL) { |
| 971 |
|
| 972 |
*filters = options->filters; |
| 973 |
} else { |
| 974 |
|
| 975 |
if (lzma_easy_preset(opt_easy, options->preset)) |
| 976 |
return LZMA_OPTIONS_ERROR; |
| 977 |
|
| 978 |
*filters = opt_easy->filters; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
if (options->block_size > 0) |
| 983 |
*block_size = options->block_size; |
| 984 |
else |
| 985 |
*block_size = lzma_mt_block_size(*filters); |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
if (*block_size > BLOCK_SIZE_MAX || *block_size == UINT64_MAX) |
| 992 |
return LZMA_OPTIONS_ERROR; |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
*outbuf_size_max = lzma_block_buffer_bound64(*block_size); |
| 998 |
if (*outbuf_size_max == 0) |
| 999 |
return LZMA_MEM_ERROR; |
| 1000 |
|
| 1001 |
return LZMA_OK; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
static void |
| 1006 |
get_progress(void *coder_ptr, uint64_t *progress_in, uint64_t *progress_out) |
| 1007 |
{ |
| 1008 |
lzma_stream_coder *coder = coder_ptr; |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
mythread_sync(coder->mutex) { |
| 1013 |
*progress_in = coder->progress_in; |
| 1014 |
*progress_out = coder->progress_out; |
| 1015 |
|
| 1016 |
for (size_t i = 0; i < coder->threads_initialized; ++i) { |
| 1017 |
mythread_sync(coder->threads[i].mutex) { |
| 1018 |
*progress_in += coder->threads[i].progress_in; |
| 1019 |
*progress_out += coder->threads[i] |
| 1020 |
.progress_out; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
return; |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
static lzma_ret |
| 1030 |
stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator, |
| 1031 |
const lzma_mt *options) |
| 1032 |
{ |
| 1033 |
lzma_next_coder_init(&stream_encoder_mt_init, next, allocator); |
| 1034 |
|
| 1035 |
|
| 1036 |
lzma_options_easy easy; |
| 1037 |
const lzma_filter *filters; |
| 1038 |
uint64_t block_size; |
| 1039 |
uint64_t outbuf_size_max; |
| 1040 |
return_if_error(get_options(options, &easy, &filters, |
| 1041 |
&block_size, &outbuf_size_max)); |
| 1042 |
|
| 1043 |
#if SIZE_MAX < UINT64_MAX |
| 1044 |
if (block_size > SIZE_MAX || outbuf_size_max > SIZE_MAX) |
| 1045 |
return LZMA_MEM_ERROR; |
| 1046 |
#endif |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
if (lzma_raw_encoder_memusage(filters) == UINT64_MAX) |
| 1055 |
return LZMA_OPTIONS_ERROR; |
| 1056 |
|
| 1057 |
|
| 1058 |
if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX) |
| 1059 |
return LZMA_PROG_ERROR; |
| 1060 |
|
| 1061 |
if (!lzma_check_is_supported(options->check)) |
| 1062 |
return LZMA_UNSUPPORTED_CHECK; |
| 1063 |
|
| 1064 |
|
| 1065 |
lzma_stream_coder *coder = next->coder; |
| 1066 |
if (coder == NULL) { |
| 1067 |
coder = lzma_alloc(sizeof(lzma_stream_coder), allocator); |
| 1068 |
if (coder == NULL) |
| 1069 |
return LZMA_MEM_ERROR; |
| 1070 |
|
| 1071 |
next->coder = coder; |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
if (mythread_mutex_init(&coder->mutex)) { |
| 1078 |
lzma_free(coder, allocator); |
| 1079 |
next->coder = NULL; |
| 1080 |
return LZMA_MEM_ERROR; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if (mythread_cond_init(&coder->cond)) { |
| 1084 |
mythread_mutex_destroy(&coder->mutex); |
| 1085 |
lzma_free(coder, allocator); |
| 1086 |
next->coder = NULL; |
| 1087 |
return LZMA_MEM_ERROR; |
| 1088 |
} |
| 1089 |
|
| 1090 |
next->code = &stream_encode_mt; |
| 1091 |
next->end = &stream_encoder_mt_end; |
| 1092 |
next->get_progress = &get_progress; |
| 1093 |
next->update = &stream_encoder_mt_update; |
| 1094 |
|
| 1095 |
coder->filters[0].id = LZMA_VLI_UNKNOWN; |
| 1096 |
coder->filters_cache[0].id = LZMA_VLI_UNKNOWN; |
| 1097 |
coder->index_encoder = LZMA_NEXT_CODER_INIT; |
| 1098 |
coder->index = NULL; |
| 1099 |
memzero(&coder->outq, sizeof(coder->outq)); |
| 1100 |
coder->threads = NULL; |
| 1101 |
coder->threads_max = 0; |
| 1102 |
coder->threads_initialized = 0; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
coder->sequence = SEQ_STREAM_HEADER; |
| 1107 |
coder->block_size = (size_t)(block_size); |
| 1108 |
coder->outbuf_alloc_size = (size_t)(outbuf_size_max); |
| 1109 |
coder->thread_error = LZMA_OK; |
| 1110 |
coder->thr = NULL; |
| 1111 |
|
| 1112 |
|
| 1113 |
assert(options->threads > 0); |
| 1114 |
if (coder->threads_max != options->threads) { |
| 1115 |
threads_end(coder, allocator); |
| 1116 |
|
| 1117 |
coder->threads = NULL; |
| 1118 |
coder->threads_max = 0; |
| 1119 |
|
| 1120 |
coder->threads_initialized = 0; |
| 1121 |
coder->threads_free = NULL; |
| 1122 |
|
| 1123 |
coder->threads = lzma_alloc( |
| 1124 |
options->threads * sizeof(worker_thread), |
| 1125 |
allocator); |
| 1126 |
if (coder->threads == NULL) |
| 1127 |
return LZMA_MEM_ERROR; |
| 1128 |
|
| 1129 |
coder->threads_max = options->threads; |
| 1130 |
} else { |
| 1131 |
|
| 1132 |
|
| 1133 |
threads_stop(coder, true); |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
return_if_error(lzma_outq_init(&coder->outq, allocator, |
| 1138 |
options->threads)); |
| 1139 |
|
| 1140 |
|
| 1141 |
coder->timeout = options->timeout; |
| 1142 |
|
| 1143 |
|
| 1144 |
lzma_filters_free(coder->filters, allocator); |
| 1145 |
lzma_filters_free(coder->filters_cache, allocator); |
| 1146 |
|
| 1147 |
|
| 1148 |
return_if_error(lzma_filters_copy( |
| 1149 |
filters, coder->filters, allocator)); |
| 1150 |
|
| 1151 |
|
| 1152 |
lzma_index_end(coder->index, allocator); |
| 1153 |
coder->index = lzma_index_init(allocator); |
| 1154 |
if (coder->index == NULL) |
| 1155 |
return LZMA_MEM_ERROR; |
| 1156 |
|
| 1157 |
|
| 1158 |
coder->stream_flags.version = 0; |
| 1159 |
coder->stream_flags.check = options->check; |
| 1160 |
return_if_error(lzma_stream_header_encode( |
| 1161 |
&coder->stream_flags, coder->header)); |
| 1162 |
|
| 1163 |
coder->header_pos = 0; |
| 1164 |
|
| 1165 |
|
| 1166 |
coder->progress_in = 0; |
| 1167 |
coder->progress_out = LZMA_STREAM_HEADER_SIZE; |
| 1168 |
|
| 1169 |
return LZMA_OK; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
#ifdef HAVE_SYMBOL_VERSIONS_LINUX |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
LZMA_SYMVER_API("lzma_stream_encoder_mt@XZ_5.1.2alpha", |
| 1180 |
lzma_ret, lzma_stream_encoder_mt_512a)( |
| 1181 |
lzma_stream *strm, const lzma_mt *options) |
| 1182 |
lzma_nothrow lzma_attr_warn_unused_result |
| 1183 |
__attribute__((__alias__("lzma_stream_encoder_mt_52"))); |
| 1184 |
|
| 1185 |
LZMA_SYMVER_API("lzma_stream_encoder_mt@XZ_5.2.2", |
| 1186 |
lzma_ret, lzma_stream_encoder_mt_522)( |
| 1187 |
lzma_stream *strm, const lzma_mt *options) |
| 1188 |
lzma_nothrow lzma_attr_warn_unused_result |
| 1189 |
__attribute__((__alias__("lzma_stream_encoder_mt_52"))); |
| 1190 |
|
| 1191 |
LZMA_SYMVER_API("lzma_stream_encoder_mt@@XZ_5.2", |
| 1192 |
lzma_ret, lzma_stream_encoder_mt_52)( |
| 1193 |
lzma_stream *strm, const lzma_mt *options) |
| 1194 |
lzma_nothrow lzma_attr_warn_unused_result; |
| 1195 |
|
| 1196 |
#define lzma_stream_encoder_mt lzma_stream_encoder_mt_52 |
| 1197 |
#endif |
| 1198 |
extern LZMA_API(lzma_ret) |
| 1199 |
lzma_stream_encoder_mt(lzma_stream *strm, const lzma_mt *options) |
| 1200 |
{ |
| 1201 |
lzma_next_strm_init(stream_encoder_mt_init, strm, options); |
| 1202 |
|
| 1203 |
strm->internal->supported_actions[LZMA_RUN] = true; |
| 1204 |
|
| 1205 |
strm->internal->supported_actions[LZMA_FULL_FLUSH] = true; |
| 1206 |
strm->internal->supported_actions[LZMA_FULL_BARRIER] = true; |
| 1207 |
strm->internal->supported_actions[LZMA_FINISH] = true; |
| 1208 |
|
| 1209 |
return LZMA_OK; |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
#ifdef HAVE_SYMBOL_VERSIONS_LINUX |
| 1214 |
LZMA_SYMVER_API("lzma_stream_encoder_mt_memusage@XZ_5.1.2alpha", |
| 1215 |
uint64_t, lzma_stream_encoder_mt_memusage_512a)( |
| 1216 |
const lzma_mt *options) lzma_nothrow lzma_attr_pure |
| 1217 |
__attribute__((__alias__("lzma_stream_encoder_mt_memusage_52"))); |
| 1218 |
|
| 1219 |
LZMA_SYMVER_API("lzma_stream_encoder_mt_memusage@XZ_5.2.2", |
| 1220 |
uint64_t, lzma_stream_encoder_mt_memusage_522)( |
| 1221 |
const lzma_mt *options) lzma_nothrow lzma_attr_pure |
| 1222 |
__attribute__((__alias__("lzma_stream_encoder_mt_memusage_52"))); |
| 1223 |
|
| 1224 |
LZMA_SYMVER_API("lzma_stream_encoder_mt_memusage@@XZ_5.2", |
| 1225 |
uint64_t, lzma_stream_encoder_mt_memusage_52)( |
| 1226 |
const lzma_mt *options) lzma_nothrow lzma_attr_pure; |
| 1227 |
|
| 1228 |
#define lzma_stream_encoder_mt_memusage lzma_stream_encoder_mt_memusage_52 |
| 1229 |
#endif |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
extern LZMA_API(uint64_t) |
| 1234 |
lzma_stream_encoder_mt_memusage(const lzma_mt *options) |
| 1235 |
{ |
| 1236 |
lzma_options_easy easy; |
| 1237 |
const lzma_filter *filters; |
| 1238 |
uint64_t block_size; |
| 1239 |
uint64_t outbuf_size_max; |
| 1240 |
|
| 1241 |
if (get_options(options, &easy, &filters, &block_size, |
| 1242 |
&outbuf_size_max) != LZMA_OK) |
| 1243 |
return UINT64_MAX; |
| 1244 |
|
| 1245 |
|
| 1246 |
const uint64_t inbuf_memusage = options->threads * block_size; |
| 1247 |
|
| 1248 |
|
| 1249 |
uint64_t filters_memusage = lzma_raw_encoder_memusage(filters); |
| 1250 |
if (filters_memusage == UINT64_MAX) |
| 1251 |
return UINT64_MAX; |
| 1252 |
|
| 1253 |
filters_memusage *= options->threads; |
| 1254 |
|
| 1255 |
|
| 1256 |
const uint64_t outq_memusage = lzma_outq_memusage( |
| 1257 |
outbuf_size_max, options->threads); |
| 1258 |
if (outq_memusage == UINT64_MAX) |
| 1259 |
return UINT64_MAX; |
| 1260 |
|
| 1261 |
|
| 1262 |
uint64_t total_memusage = LZMA_MEMUSAGE_BASE |
| 1263 |
+ sizeof(lzma_stream_coder) |
| 1264 |
+ options->threads * sizeof(worker_thread); |
| 1265 |
|
| 1266 |
if (UINT64_MAX - total_memusage < inbuf_memusage) |
| 1267 |
return UINT64_MAX; |
| 1268 |
|
| 1269 |
total_memusage += inbuf_memusage; |
| 1270 |
|
| 1271 |
if (UINT64_MAX - total_memusage < filters_memusage) |
| 1272 |
return UINT64_MAX; |
| 1273 |
|
| 1274 |
total_memusage += filters_memusage; |
| 1275 |
|
| 1276 |
if (UINT64_MAX - total_memusage < outq_memusage) |
| 1277 |
return UINT64_MAX; |
| 1278 |
|
| 1279 |
return total_memusage + outq_memusage; |
| 1280 |
} |