3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Contributor(s): Peter Schlaile <peter@schlaile.de> 2005
21 * ***** END GPL LICENSE BLOCK *****
24 #ifndef __MEM_cache_limiter_h_included__
25 #define __MEM_cache_limiter_h_included__ 1
28 * @section MEM_CacheLimiter
29 * This class defines a generic memory cache management system
30 * to limit memory usage to a fixed global maximum.
32 * Please use the C-API in MEM_CacheLimiterC-Api.h for code written in C.
38 * ~BigFatImage() { tell_everyone_we_are_gone(this); }
42 * MEM_Cache<BigFatImage> BigFatImages;
44 * MEM_Cache_Handle<BigFatImage>* h = BigFatImages.insert(new BigFatImage);
46 * BigFatImages.enforce_limits();
53 * leave image in cache.
57 #include "MEM_Allocator.h"
60 class MEM_CacheLimiter;
62 #ifndef __MEM_cache_limiter_c_api_h_included__
64 extern void MEM_CacheLimiter_set_maximum(intptr_t m);
65 extern intptr_t MEM_CacheLimiter_get_maximum();
70 class MEM_CacheLimiterHandle {
72 explicit MEM_CacheLimiterHandle(T * data_,
73 MEM_CacheLimiter<T> * parent_)
74 : data(data_), refcount(0), parent(parent_) { }
85 const T * get() const {
88 int get_refcount() const {
91 bool can_destroy() const {
92 return !data || !refcount;
94 bool destroy_if_possible() {
104 parent->unmanage(this);
110 friend class MEM_CacheLimiter<T>;
114 typename std::list<MEM_CacheLimiterHandle<T> *,
115 MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator me;
116 MEM_CacheLimiter<T> * parent;
120 class MEM_CacheLimiter {
122 typedef typename std::list<MEM_CacheLimiterHandle<T> *,
123 MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator iterator;
124 ~MEM_CacheLimiter() {
125 for (iterator it = queue.begin(); it != queue.end(); it++) {
129 MEM_CacheLimiterHandle<T> * insert(T * elem) {
130 queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
131 iterator it = queue.end();
133 queue.back()->me = it;
136 void unmanage(MEM_CacheLimiterHandle<T> * handle) {
137 queue.erase(handle->me);
140 void enforce_limits() {
141 intptr_t max = MEM_CacheLimiter_get_maximum();
142 intptr_t mem_in_use= MEM_get_memory_in_use();
143 intptr_t mmap_in_use= MEM_get_mapped_memory_in_use();
148 for (iterator it = queue.begin();
149 it != queue.end() && mem_in_use + mmap_in_use > max;) {
152 (*jt)->destroy_if_possible();
155 void touch(MEM_CacheLimiterHandle<T> * handle) {
156 queue.push_back(handle);
157 queue.erase(handle->me);
158 iterator it = queue.end();
163 std::list<MEM_CacheLimiterHandle<T>*,
164 MEM_Allocator<MEM_CacheLimiterHandle<T> *> > queue;