Grok  9.5.0
SequentialCache.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 Grok Image Compression Inc.
3  *
4  * This source code is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License, version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This source code is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #pragma once
17 #include "grk_includes.h"
18 
19 #include <vector>
20 
21 namespace grk
22 {
23 template<typename T>
25 {
26  public:
28  SequentialCache(uint64_t maxChunkSize)
29  : m_chunkSize(std::min<uint64_t>(maxChunkSize, kSequentialChunkSize)), m_currChunk(nullptr),
30  m_index(0)
31  {}
32  virtual ~SequentialCache(void)
33  {
34  for(auto& ch : chunks)
35  {
36  for(size_t i = 0; i < m_chunkSize; ++i)
37  delete ch[i];
38  delete[] ch;
39  }
40  }
41  void rewind(void)
42  {
43  if(chunks.empty())
44  return;
45  m_index = 0;
46  m_currChunk = chunks[0];
47  }
48  T* get()
49  {
50  uint64_t itemIndex = m_index % m_chunkSize;
51  uint64_t chunkIndex = m_index / m_chunkSize;
52  bool initialized = (m_currChunk != nullptr);
53  bool lastChunk = (chunkIndex == chunks.size() - 1);
54  bool endOfChunk = (itemIndex == m_chunkSize - 1);
55  bool createNew = !initialized || (lastChunk && endOfChunk);
56  itemIndex++;
57  if(createNew || endOfChunk)
58  {
59  itemIndex = 0;
60  chunkIndex++;
61  if(createNew)
62  {
63  m_currChunk = new T*[m_chunkSize];
64  memset(m_currChunk, 0, m_chunkSize * sizeof(T*));
65  chunks.push_back(m_currChunk);
66  }
67  else
68  {
69  m_currChunk = chunks[chunkIndex];
70  }
71  }
72  auto item = m_currChunk[itemIndex];
73  if(!item)
74  {
75  item = create();
76  m_currChunk[itemIndex] = item;
77  }
78  if(initialized)
79  m_index++;
80  return item;
81  }
82 
83  protected:
84  virtual T* create(void)
85  {
86  auto item = new T();
87  return item;
88  }
89 
90  private:
91  std::vector<T**> chunks;
92  uint64_t m_chunkSize;
94  uint64_t m_index;
95  static constexpr uint64_t kSequentialChunkSize = 1024;
96 };
97 
98 } // namespace grk
Definition: SequentialCache.h:25
void rewind(void)
Definition: SequentialCache.h:41
SequentialCache(uint64_t maxChunkSize)
Definition: SequentialCache.h:28
static constexpr uint64_t kSequentialChunkSize
Definition: SequentialCache.h:95
virtual T * create(void)
Definition: SequentialCache.h:84
uint64_t m_chunkSize
Definition: SequentialCache.h:92
virtual ~SequentialCache(void)
Definition: SequentialCache.h:32
std::vector< T ** > chunks
Definition: SequentialCache.h:91
uint64_t m_index
Definition: SequentialCache.h:94
SequentialCache(void)
Definition: SequentialCache.h:27
T * get()
Definition: SequentialCache.h:48
T ** m_currChunk
Definition: SequentialCache.h:93
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: ICacheable.h:20