outqueue.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file outqueue.c
  5. /// \brief Output queue handling in multithreaded coding
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "outqueue.h"
  11. /// Get the maximum number of buffers that may be allocated based
  12. /// on the number of threads. For now this is twice the number of threads.
  13. /// It's a compromise between RAM usage and keeping the worker threads busy
  14. /// when buffers finish out of order.
  15. #define GET_BUFS_LIMIT(threads) (2 * (threads))
  16. extern uint64_t
  17. lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads)
  18. {
  19. // This is to ease integer overflow checking: We may allocate up to
  20. // GET_BUFS_LIMIT(LZMA_THREADS_MAX) buffers and we need some extra
  21. // memory for other data structures too (that's the /2).
  22. //
  23. // lzma_outq_prealloc_buf() will still accept bigger buffers than this.
  24. const uint64_t limit
  25. = UINT64_MAX / GET_BUFS_LIMIT(LZMA_THREADS_MAX) / 2;
  26. if (threads > LZMA_THREADS_MAX || buf_size_max > limit)
  27. return UINT64_MAX;
  28. return GET_BUFS_LIMIT(threads)
  29. * lzma_outq_outbuf_memusage(buf_size_max);
  30. }
  31. static void
  32. move_head_to_cache(lzma_outq *outq, const lzma_allocator *allocator)
  33. {
  34. assert(outq->head != NULL);
  35. assert(outq->tail != NULL);
  36. assert(outq->bufs_in_use > 0);
  37. lzma_outbuf *buf = outq->head;
  38. outq->head = buf->next;
  39. if (outq->head == NULL)
  40. outq->tail = NULL;
  41. if (outq->cache != NULL && outq->cache->allocated != buf->allocated)
  42. lzma_outq_clear_cache(outq, allocator);
  43. buf->next = outq->cache;
  44. outq->cache = buf;
  45. --outq->bufs_in_use;
  46. outq->mem_in_use -= lzma_outq_outbuf_memusage(buf->allocated);
  47. return;
  48. }
  49. static void
  50. free_one_cached_buffer(lzma_outq *outq, const lzma_allocator *allocator)
  51. {
  52. assert(outq->cache != NULL);
  53. lzma_outbuf *buf = outq->cache;
  54. outq->cache = buf->next;
  55. --outq->bufs_allocated;
  56. outq->mem_allocated -= lzma_outq_outbuf_memusage(buf->allocated);
  57. lzma_free(buf, allocator);
  58. return;
  59. }
  60. extern void
  61. lzma_outq_clear_cache(lzma_outq *outq, const lzma_allocator *allocator)
  62. {
  63. while (outq->cache != NULL)
  64. free_one_cached_buffer(outq, allocator);
  65. return;
  66. }
  67. extern void
  68. lzma_outq_clear_cache2(lzma_outq *outq, const lzma_allocator *allocator,
  69. size_t keep_size)
  70. {
  71. if (outq->cache == NULL)
  72. return;
  73. // Free all but one.
  74. while (outq->cache->next != NULL)
  75. free_one_cached_buffer(outq, allocator);
  76. // Free the last one only if its size doesn't equal to keep_size.
  77. if (outq->cache->allocated != keep_size)
  78. free_one_cached_buffer(outq, allocator);
  79. return;
  80. }
  81. extern lzma_ret
  82. lzma_outq_init(lzma_outq *outq, const lzma_allocator *allocator,
  83. uint32_t threads)
  84. {
  85. if (threads > LZMA_THREADS_MAX)
  86. return LZMA_OPTIONS_ERROR;
  87. const uint32_t bufs_limit = GET_BUFS_LIMIT(threads);
  88. // Clear head/tail.
  89. while (outq->head != NULL)
  90. move_head_to_cache(outq, allocator);
  91. // If new buf_limit is lower than the old one, we may need to free
  92. // a few cached buffers.
  93. while (bufs_limit < outq->bufs_allocated)
  94. free_one_cached_buffer(outq, allocator);
  95. outq->bufs_limit = bufs_limit;
  96. outq->read_pos = 0;
  97. return LZMA_OK;
  98. }
  99. extern void
  100. lzma_outq_end(lzma_outq *outq, const lzma_allocator *allocator)
  101. {
  102. while (outq->head != NULL)
  103. move_head_to_cache(outq, allocator);
  104. lzma_outq_clear_cache(outq, allocator);
  105. return;
  106. }
  107. extern lzma_ret
  108. lzma_outq_prealloc_buf(lzma_outq *outq, const lzma_allocator *allocator,
  109. size_t size)
  110. {
  111. // Caller must have checked it with lzma_outq_has_buf().
  112. assert(outq->bufs_in_use < outq->bufs_limit);
  113. // If there already is appropriately-sized buffer in the cache,
  114. // we need to do nothing.
  115. if (outq->cache != NULL && outq->cache->allocated == size)
  116. return LZMA_OK;
  117. if (size > SIZE_MAX - sizeof(lzma_outbuf))
  118. return LZMA_MEM_ERROR;
  119. const size_t alloc_size = lzma_outq_outbuf_memusage(size);
  120. // The cache may have buffers but their size is wrong.
  121. lzma_outq_clear_cache(outq, allocator);
  122. outq->cache = lzma_alloc(alloc_size, allocator);
  123. if (outq->cache == NULL)
  124. return LZMA_MEM_ERROR;
  125. outq->cache->next = NULL;
  126. outq->cache->allocated = size;
  127. ++outq->bufs_allocated;
  128. outq->mem_allocated += alloc_size;
  129. return LZMA_OK;
  130. }
  131. extern lzma_outbuf *
  132. lzma_outq_get_buf(lzma_outq *outq, void *worker)
  133. {
  134. // Caller must have used lzma_outq_prealloc_buf() to ensure these.
  135. assert(outq->bufs_in_use < outq->bufs_limit);
  136. assert(outq->bufs_in_use < outq->bufs_allocated);
  137. assert(outq->cache != NULL);
  138. lzma_outbuf *buf = outq->cache;
  139. outq->cache = buf->next;
  140. buf->next = NULL;
  141. if (outq->tail != NULL) {
  142. assert(outq->head != NULL);
  143. outq->tail->next = buf;
  144. } else {
  145. assert(outq->head == NULL);
  146. outq->head = buf;
  147. }
  148. outq->tail = buf;
  149. buf->worker = worker;
  150. buf->finished = false;
  151. buf->finish_ret = LZMA_STREAM_END;
  152. buf->pos = 0;
  153. buf->decoder_in_pos = 0;
  154. buf->unpadded_size = 0;
  155. buf->uncompressed_size = 0;
  156. ++outq->bufs_in_use;
  157. outq->mem_in_use += lzma_outq_outbuf_memusage(buf->allocated);
  158. return buf;
  159. }
  160. extern bool
  161. lzma_outq_is_readable(const lzma_outq *outq)
  162. {
  163. if (outq->head == NULL)
  164. return false;
  165. return outq->read_pos < outq->head->pos || outq->head->finished;
  166. }
  167. extern lzma_ret
  168. lzma_outq_read(lzma_outq *restrict outq,
  169. const lzma_allocator *restrict allocator,
  170. uint8_t *restrict out, size_t *restrict out_pos,
  171. size_t out_size,
  172. lzma_vli *restrict unpadded_size,
  173. lzma_vli *restrict uncompressed_size)
  174. {
  175. // There must be at least one buffer from which to read.
  176. if (outq->bufs_in_use == 0)
  177. return LZMA_OK;
  178. // Get the buffer.
  179. lzma_outbuf *buf = outq->head;
  180. // Copy from the buffer to output.
  181. //
  182. // FIXME? In threaded decoder it may be bad to do this copy while
  183. // the mutex is being held.
  184. lzma_bufcpy(buf->buf, &outq->read_pos, buf->pos,
  185. out, out_pos, out_size);
  186. // Return if we didn't get all the data from the buffer.
  187. if (!buf->finished || outq->read_pos < buf->pos)
  188. return LZMA_OK;
  189. // The buffer was finished. Tell the caller its size information.
  190. if (unpadded_size != NULL)
  191. *unpadded_size = buf->unpadded_size;
  192. if (uncompressed_size != NULL)
  193. *uncompressed_size = buf->uncompressed_size;
  194. // Remember the return value.
  195. const lzma_ret finish_ret = buf->finish_ret;
  196. // Free this buffer for further use.
  197. move_head_to_cache(outq, allocator);
  198. outq->read_pos = 0;
  199. return finish_ret;
  200. }
  201. extern void
  202. lzma_outq_enable_partial_output(lzma_outq *outq,
  203. void (*enable_partial_output)(void *worker))
  204. {
  205. if (outq->head != NULL && !outq->head->finished
  206. && outq->head->worker != NULL) {
  207. enable_partial_output(outq->head->worker);
  208. // Set it to NULL since calling it twice is pointless.
  209. outq->head->worker = NULL;
  210. }
  211. return;
  212. }