cover.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <stdio.h> /* fprintf */
  2. #include <stdlib.h> /* malloc, free, qsort */
  3. #include <string.h> /* memset */
  4. #include <time.h> /* clock */
  5. #include "mem.h" /* read */
  6. #include "pool.h"
  7. #include "threading.h"
  8. #include "zstd_internal.h" /* includes zstd.h */
  9. #ifndef ZDICT_STATIC_LINKING_ONLY
  10. #define ZDICT_STATIC_LINKING_ONLY
  11. #endif
  12. #include "zdict.h"
  13. /**
  14. * COVER_best_t is used for two purposes:
  15. * 1. Synchronizing threads.
  16. * 2. Saving the best parameters and dictionary.
  17. *
  18. * All of the methods except COVER_best_init() are thread safe if zstd is
  19. * compiled with multithreaded support.
  20. */
  21. typedef struct COVER_best_s {
  22. ZSTD_pthread_mutex_t mutex;
  23. ZSTD_pthread_cond_t cond;
  24. size_t liveJobs;
  25. void *dict;
  26. size_t dictSize;
  27. ZDICT_cover_params_t parameters;
  28. size_t compressedSize;
  29. } COVER_best_t;
  30. /**
  31. * A segment is a range in the source as well as the score of the segment.
  32. */
  33. typedef struct {
  34. U32 begin;
  35. U32 end;
  36. U32 score;
  37. } COVER_segment_t;
  38. /**
  39. * Checks total compressed size of a dictionary
  40. */
  41. size_t COVER_checkTotalCompressedSize(const ZDICT_cover_params_t parameters,
  42. const size_t *samplesSizes, const BYTE *samples,
  43. size_t *offsets,
  44. size_t nbTrainSamples, size_t nbSamples,
  45. BYTE *const dict, size_t dictBufferCapacity);
  46. /**
  47. * Returns the sum of the sample sizes.
  48. */
  49. size_t COVER_sum(const size_t *samplesSizes, unsigned nbSamples) ;
  50. /**
  51. * Initialize the `COVER_best_t`.
  52. */
  53. void COVER_best_init(COVER_best_t *best);
  54. /**
  55. * Wait until liveJobs == 0.
  56. */
  57. void COVER_best_wait(COVER_best_t *best);
  58. /**
  59. * Call COVER_best_wait() and then destroy the COVER_best_t.
  60. */
  61. void COVER_best_destroy(COVER_best_t *best);
  62. /**
  63. * Called when a thread is about to be launched.
  64. * Increments liveJobs.
  65. */
  66. void COVER_best_start(COVER_best_t *best);
  67. /**
  68. * Called when a thread finishes executing, both on error or success.
  69. * Decrements liveJobs and signals any waiting threads if liveJobs == 0.
  70. * If this dictionary is the best so far save it and its parameters.
  71. */
  72. void COVER_best_finish(COVER_best_t *best, size_t compressedSize,
  73. ZDICT_cover_params_t parameters, void *dict,
  74. size_t dictSize);