1/* LzFindMt.h -- multithreaded Match finder for LZ algorithms
22013-01-18 : Igor Pavlov : Public domain */
3
4#ifndef __LZ_FIND_MT_H
5#define __LZ_FIND_MT_H
6
7#include "LzFind.h"
8#include "Threads.h"
9
10EXTERN_C_BEGIN
11
12#define kMtHashBlockSize (1 << 13)
13#define kMtHashNumBlocks (1 << 3)
14#define kMtHashNumBlocksMask (kMtHashNumBlocks - 1)
15
16#define kMtBtBlockSize (1 << 14)
17#define kMtBtNumBlocks (1 << 6)
18#define kMtBtNumBlocksMask (kMtBtNumBlocks - 1)
19
20typedef struct _CMtSync
21{
22  Bool wasCreated;
23  Bool needStart;
24  Bool exit;
25  Bool stopWriting;
26
27  CThread thread;
28  CAutoResetEvent canStart;
29  CAutoResetEvent wasStarted;
30  CAutoResetEvent wasStopped;
31  CSemaphore freeSemaphore;
32  CSemaphore filledSemaphore;
33  Bool csWasInitialized;
34  Bool csWasEntered;
35  CCriticalSection cs;
36  UInt32 numProcessedBlocks;
37} CMtSync;
38
39typedef UInt32 * (*Mf_Mix_Matches)(void *p, UInt32 matchMinPos, UInt32 *distances);
40
41/* kMtCacheLineDummy must be >= size_of_CPU_cache_line */
42#define kMtCacheLineDummy 128
43
44typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos,
45  UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc);
46
47typedef struct _CMatchFinderMt
48{
49  /* LZ */
50  const Byte *pointerToCurPos;
51  UInt32 *btBuf;
52  UInt32 btBufPos;
53  UInt32 btBufPosLimit;
54  UInt32 lzPos;
55  UInt32 btNumAvailBytes;
56
57  UInt32 *hash;
58  UInt32 fixedHashSize;
59  UInt32 historySize;
60  const UInt32 *crc;
61
62  Mf_Mix_Matches MixMatchesFunc;
63
64  /* LZ + BT */
65  CMtSync btSync;
66  Byte btDummy[kMtCacheLineDummy];
67
68  /* BT */
69  UInt32 *hashBuf;
70  UInt32 hashBufPos;
71  UInt32 hashBufPosLimit;
72  UInt32 hashNumAvail;
73
74  CLzRef *son;
75  UInt32 matchMaxLen;
76  UInt32 numHashBytes;
77  UInt32 pos;
78  Byte *buffer;
79  UInt32 cyclicBufferPos;
80  UInt32 cyclicBufferSize; /* it must be historySize + 1 */
81  UInt32 cutValue;
82
83  /* BT + Hash */
84  CMtSync hashSync;
85  /* Byte hashDummy[kMtCacheLineDummy]; */
86
87  /* Hash */
88  Mf_GetHeads GetHeadsFunc;
89  CMatchFinder *MatchFinder;
90} CMatchFinderMt;
91
92void MatchFinderMt_Construct(CMatchFinderMt *p);
93void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAlloc *alloc);
94SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore,
95    UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAlloc *alloc);
96void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable);
97void MatchFinderMt_ReleaseStream(CMatchFinderMt *p);
98
99EXTERN_C_END
100
101#endif
102