summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/win/CPP/7zip/Common/InMemStream.h
blob: ec493977cadeed38ff3f944202e181d59a43aeb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// InMemStream.h

#ifndef __IN_MEM_STREAM_H
#define __IN_MEM_STREAM_H

#include <stdio.h>

#include "../../../C/Alloc.h"

#include "../../Common/MyCom.h"

#include "MemBlocks.h"

class CIntListCheck
{
protected:
  int *_data;
public:
  CIntListCheck(): _data(0) {}
  ~CIntListCheck() { FreeList(); }
  
  bool AllocateList(int numItems)
  {
    FreeList();
    if (numItems == 0)
      return true;
    _data = (int *)::MyAlloc(numItems * sizeof(int));
    return (_data != 0);
  }
  
  void FreeList()
  {
    ::MyFree(_data);
    _data = 0;
  }
};


class CResourceList : public CIntListCheck
{
  int _headFree;
public:
  CResourceList(): _headFree(-1) {}
  
  bool AllocateList(int numItems)
  {
    FreeList();
    if (numItems == 0)
      return true;
    if (!CIntListCheck::AllocateList(numItems))
      return false;
    for (int i = 0; i < numItems; i++)
      _data[i] = i + 1;
    _data[numItems - 1] = -1;
    _headFree = 0;
    return true;
  }
  
  void FreeList()
  {
    CIntListCheck::FreeList();
    _headFree = -1;
  }
  
  int AllocateItem()
  {
    int res = _headFree;
    if (res >= 0)
      _headFree = _data[res];
    return res;
  }
  
  void FreeItem(int index)
  {
    if (index < 0)
      return;
    _data[index] = _headFree;
    _headFree = index;
  }
};

class CResourceListMt: public CResourceList
{
  NWindows::NSynchronization::CCriticalSection _criticalSection;
public:
  NWindows::NSynchronization::CSemaphore Semaphore;

  HRes AllocateList(int numItems)
  {
    if (!CResourceList::AllocateList(numItems))
      return E_OUTOFMEMORY;
    Semaphore.Close();
    return Semaphore.Create(numItems, numItems);
  }

  int AllocateItem()
  {
    Semaphore.Lock();
    _criticalSection.Enter();
    int res = CResourceList::AllocateItem();
    _criticalSection.Leave();
    return res;
  }
  
  void FreeItem(int index)
  {
    if (index < 0)
      return;
    _criticalSection.Enter();
    CResourceList::FreeItem(index);
    _criticalSection.Leave();
    Semaphore.Release();
  }
};

class CIntQueueMt: public CIntListCheck
{
  int _numItems;
  int _head;
  int _cur;
public:
  CIntQueueMt(): _numItems(0), _head(0),  _cur(0) {}
  NWindows::NSynchronization::CSemaphore Semaphore;

  HRes AllocateList(int numItems)
  {
    FreeList();
    if (numItems == 0)
      return S_OK;
    if (!CIntListCheck::AllocateList(numItems))
      return E_OUTOFMEMORY;
    _numItems = numItems;
    return Semaphore.Create(0, numItems);
  }

  void FreeList()
  {
    CIntListCheck::FreeList();
    _numItems = 0;
    _head = 0;
    _cur = 0;
  }

  void AddItem(int value)
  {
    _data[_head++] = value;
    if (_head == _numItems)
      _head = 0;
    Semaphore.Release();
    // printf("\nRelease prev = %d\n", previousCount);
  }
  
  int GetItem()
  {
    // Semaphore.Lock();
    int res = _data[_cur++];
    if (_cur == _numItems)
      _cur = 0;
    return res;
  }
};

struct IInMemStreamMtCallback
{
  // must be same for all calls
  virtual size_t GetBlockSize() = 0;
  
  // Out:
  //  result != S_OK stops Reading
  //   if *p = 0, result must be != S_OK;
  // Locking is allowed
  virtual HRESULT AllocateBlock(void **p) = 0;

  virtual void FreeBlock(void *p) = 0;

  // It must allow to add at least numSubStreams + 1 ,
  // where numSubStreams is value from CInMemStreamMt::Create
  // value -1 means End of stream
  // Locking is not allowed
  virtual void AddStreamIndexToQueue(int index) = 0;
};

struct CStreamInfo
{
  CRecordVector<void *> Blocks;

  int LastBlockIndex;
  size_t LastBlockPos;
  bool StreamWasFinished;

  int CurBlockIndex;
  size_t CurBlockPos;

  NWindows::NSynchronization::CCriticalSection *Cs;
  NWindows::NSynchronization::CManualResetEvent *CanReadEvent;

  HRESULT ExitResult;

  CStreamInfo(): Cs(0), CanReadEvent(0), StreamWasFinished(false) { }
  ~CStreamInfo()
  {
    delete Cs;
    delete CanReadEvent;
    // Free();
  }
  void Create()
  {
    Cs = new NWindows::NSynchronization::CCriticalSection;
    CanReadEvent = new NWindows::NSynchronization::CManualResetEvent;
  }

  void Free(IInMemStreamMtCallback *callback);
  void Init()
  {
    LastBlockIndex = CurBlockIndex = 0;
    CurBlockPos = LastBlockPos = 0;
    StreamWasFinished = false;
    ExitResult = S_OK;
  }

  // res must be != S_OK
  void Exit(HRESULT res)
  {
    ExitResult = res;
    CanReadEvent->Set();
  }
};


class CInMemStreamMt
{
  CMyComPtr<ISequentialInStream> _stream;
  NWindows::NSynchronization::CCriticalSection CS;
  CObjectVector<CStreamInfo> _streams;
  int _nextFreeStreamIndex;
  int _currentStreamIndex;
  UInt64 _subStreamSize;

  CResourceListMt _streamIndexAllocator;

  // bool _stopReading;

public:
  HRESULT Read();
  HRESULT ReadResult;
  IInMemStreamMtCallback *Callback;
  void FreeSubStream(int subStreamIndex);
  HRESULT ReadSubStream(int subStreamIndex, void *data, UInt32 size, UInt32 *processedSize, bool keepData);

  // numSubStreams: min = 1, good min = numThreads
  bool Create(int numSubStreams, UInt64 subStreamSize);
  ~CInMemStreamMt() { Free(); }
  void SetStream(ISequentialInStream *stream) { _stream = stream; }
  
  // to stop reading you must implement
  // returning Error in IInMemStreamMtCallback::AllocateBlock
  // and then you must free at least one substream
  HRes StartReadThread();

  void Free();

  // you must free at least one substream after that function to unlock waiting.
  // void StopReading() { _stopReading = true; }
};

class CInMemStream:
  public ISequentialInStream,
  public CMyUnknownImp
{
  UInt64 _size;
  bool _keepData;
public:
  int Index;
  CInMemStreamMt *mtStream;
  void Init(bool keepData = false)
  {
    _size = 0; _keepData = keepData ;
  }
  MY_UNKNOWN_IMP
  STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
  UInt64 GetSize() const { return _size; }
};

#endif