xref: /freebsd/sys/contrib/zstd/lib/dictBuilder/cover.h (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
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 /**
15  * COVER_best_t is used for two purposes:
16  * 1. Synchronizing threads.
17  * 2. Saving the best parameters and dictionary.
18  *
19  * All of the methods except COVER_best_init() are thread safe if zstd is
20  * compiled with multithreaded support.
21  */
22 typedef struct COVER_best_s {
23   ZSTD_pthread_mutex_t mutex;
24   ZSTD_pthread_cond_t cond;
25   size_t liveJobs;
26   void *dict;
27   size_t dictSize;
28   ZDICT_cover_params_t parameters;
29   size_t compressedSize;
30 } COVER_best_t;
31 
32 /**
33  * A segment is a range in the source as well as the score of the segment.
34  */
35 typedef struct {
36   U32 begin;
37   U32 end;
38   U32 score;
39 } COVER_segment_t;
40 
41 /**
42  *  Checks total compressed size of a dictionary
43  */
44 size_t COVER_checkTotalCompressedSize(const ZDICT_cover_params_t parameters,
45                                       const size_t *samplesSizes, const BYTE *samples,
46                                       size_t *offsets,
47                                       size_t nbTrainSamples, size_t nbSamples,
48                                       BYTE *const dict, size_t dictBufferCapacity);
49 
50 /**
51  * Returns the sum of the sample sizes.
52  */
53 size_t COVER_sum(const size_t *samplesSizes, unsigned nbSamples) ;
54 
55 /**
56  * Initialize the `COVER_best_t`.
57  */
58 void COVER_best_init(COVER_best_t *best);
59 
60 /**
61  * Wait until liveJobs == 0.
62  */
63 void COVER_best_wait(COVER_best_t *best);
64 
65 /**
66  * Call COVER_best_wait() and then destroy the COVER_best_t.
67  */
68 void COVER_best_destroy(COVER_best_t *best);
69 
70 /**
71  * Called when a thread is about to be launched.
72  * Increments liveJobs.
73  */
74 void COVER_best_start(COVER_best_t *best);
75 
76 /**
77  * Called when a thread finishes executing, both on error or success.
78  * Decrements liveJobs and signals any waiting threads if liveJobs == 0.
79  * If this dictionary is the best so far save it and its parameters.
80  */
81 void COVER_best_finish(COVER_best_t *best, size_t compressedSize,
82                        ZDICT_cover_params_t parameters, void *dict,
83                        size_t dictSize);
84