tesseract  5.0.0-alpha-619-ge9db
object_cache.h
Go to the documentation of this file.
1 // File: object_cache.h
3 // Description: A string indexed object cache.
4 // Author: David Eger
5 //
6 // (C) Copyright 2012, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 
19 #ifndef TESSERACT_CCUTIL_OBJECT_CACHE_H_
20 #define TESSERACT_CCUTIL_OBJECT_CACHE_H_
21 
22 #include <mutex> // for std::mutex
23 #include <functional> // for std::function
24 #include "ccutil.h"
25 #include "errcode.h"
27 
28 namespace tesseract {
29 
30 // A simple object cache which maps a string to an object of type T.
31 // Usually, these are expensive objects that are loaded from disk.
32 // Reference counting is performed, so every Get() needs to be followed later
33 // by a Free(). Actual deletion is accomplished by DeleteUnusedObjects().
34 template<typename T>
35 class ObjectCache {
36  public:
37  ObjectCache() = default;
39  std::lock_guard<std::mutex> guard(mu_);
40  for (int i = 0; i < cache_.size(); i++) {
41  if (cache_[i].count > 0) {
42  tprintf("ObjectCache(%p)::~ObjectCache(): WARNING! LEAK! object %p "
43  "still has count %d (id %s)\n",
44  this, cache_[i].object, cache_[i].count,
45  cache_[i].id.c_str());
46  } else {
47  delete cache_[i].object;
48  cache_[i].object = nullptr;
49  }
50  }
51  }
52 
53  // Return a pointer to the object identified by id.
54  // If we haven't yet loaded the object, use loader to load it.
55  // If loader fails to load it, record a nullptr entry in the cache
56  // and return nullptr -- further attempts to load will fail (even
57  // with a different loader) until DeleteUnusedObjects() is called.
58  // We delete the given loader.
59  T* Get(STRING id, std::function<T*()> loader) {
60  T *retval = nullptr;
61  std::lock_guard<std::mutex> guard(mu_);
62  for (int i = 0; i < cache_.size(); i++) {
63  if (id == cache_[i].id) {
64  retval = cache_[i].object;
65  if (cache_[i].object != nullptr) {
66  cache_[i].count++;
67  }
68  return retval;
69  }
70  }
71  cache_.push_back(ReferenceCount());
72  ReferenceCount &rc = cache_.back();
73  rc.id = id;
74  retval = rc.object = loader();
75  rc.count = (retval != nullptr) ? 1 : 0;
76  return retval;
77  }
78 
79  // Decrement the count for t.
80  // Return whether we knew about the given pointer.
81  bool Free(T *t) {
82  if (t == nullptr) return false;
83  std::lock_guard<std::mutex> guard(mu_);
84  for (int i = 0; i < cache_.size(); i++) {
85  if (cache_[i].object == t) {
86  --cache_[i].count;
87  return true;
88  }
89  }
90  return false;
91  }
92 
94  std::lock_guard<std::mutex> guard(mu_);
95  for (int i = cache_.size() - 1; i >= 0; i--) {
96  if (cache_[i].count <= 0) {
97  delete cache_[i].object;
98  cache_.remove(i);
99  }
100  }
101  }
102 
103  private:
104  struct ReferenceCount {
105  STRING id; // A unique ID to identify the object (think path on disk)
106  T *object; // A copy of the object in memory. Can be delete'd.
107  int count; // A count of the number of active users of this object.
108  };
109 
110  std::mutex mu_;
112 };
113 
114 } // namespace tesseract
115 
116 
117 #endif // TESSERACT_CCUTIL_OBJECT_CACHE_H_
tesseract::ObjectCache::Get
T * Get(STRING id, std::function< T *()> loader)
Definition: object_cache.h:59
tesseract::ObjectCache::Free
bool Free(T *t)
Definition: object_cache.h:81
GenericVector::remove
void remove(int index)
Definition: genericvector.h:765
tesseract::ObjectCache::DeleteUnusedObjects
void DeleteUnusedObjects()
Definition: object_cache.h:93
STRING
Definition: strngs.h:45
GenericVector::back
T & back() const
Definition: genericvector.h:728
genericvector.h
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
tesseract::ObjectCache
Definition: object_cache.h:35
tesseract::ObjectCache::~ObjectCache
~ObjectCache()
Definition: object_cache.h:38
ccutil.h
tesseract
Definition: baseapi.h:65
tesseract::ObjectCache::ObjectCache
ObjectCache()=default
GenericVector< ReferenceCount >
count
int count(LIST var_list)
Definition: oldlist.cpp:79
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
errcode.h
GenericVector::size
int size() const
Definition: genericvector.h:71