kodi
CircularCache.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "CacheStrategy.h"
12 #include "threads/CriticalSection.h"
13 #include "threads/Event.h"
14 
15 namespace XFILE {
16 
18 {
19 public:
20  CCircularCache(size_t front, size_t back);
21  ~CCircularCache() override;
22 
23  int Open() override;
24  void Close() override;
25 
26  size_t GetMaxWriteSize(const size_t& iRequestSize) override;
27  int WriteToCache(const char *buf, size_t len) override;
28  int ReadFromCache(char *buf, size_t len) override;
29  int64_t WaitForData(uint32_t minimum, std::chrono::milliseconds timeout) override;
30 
31  int64_t Seek(int64_t pos) override;
32  bool Reset(int64_t pos) override;
33 
34  int64_t CachedDataEndPosIfSeekTo(int64_t iFilePosition) override;
35  int64_t CachedDataStartPos() override;
36  int64_t CachedDataEndPos() override;
37  bool IsCachedPosition(int64_t iFilePosition) override;
38 
39  CCacheStrategy *CreateNew() override;
40 protected:
41  int64_t m_beg = 0;
42  int64_t m_end = 0;
43  int64_t m_cur = 0;
44  uint8_t *m_buf;
45  size_t m_size;
46  size_t m_size_back;
47  CCriticalSection m_sync;
48  CEvent m_written;
49 #ifdef TARGET_WINDOWS
50  HANDLE m_handle;
51 #endif
52 };
53 
54 } // namespace XFILE
55 
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
int64_t m_beg
index in file (not buffer) of beginning of valid data
Definition: CircularCache.h:41
Definition: XHandle.h:21
Definition: Scraper.h:41
int ReadFromCache(char *buf, size_t len) override
Reads data from cache.
Definition: CircularCache.cpp:145
uint8_t * m_buf
buffer holding data
Definition: CircularCache.h:44
int64_t m_end
index in file (not buffer) of end of valid data
Definition: CircularCache.h:42
Definition: CircularCache.h:17
bool Reset(int64_t pos) override
Reset cache position.
Definition: CircularCache.cpp:238
int64_t m_cur
current reading index in file
Definition: CircularCache.h:43
size_t m_size_back
guaranteed size of back buffer (actual size can be smaller, or larger if front buffer doesn't need it...
Definition: CircularCache.h:46
Definition: CacheStrategy.h:25
int WriteToCache(const char *buf, size_t len) override
Function will write to m_buf at m_end % m_size location it will write at maximum m_size, but it will only write as much it can without wrapping around in the buffer.
Definition: CircularCache.cpp:101
Definition: LibInputPointer.h:13
size_t m_size
size of data buffer used (m_buf)
Definition: CircularCache.h:45