Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MEM_CacheLimiter.h « memutil « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5a44375885c9b481ebc8a5a63acaf1ec7ffbbee (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Peter Schlaile <peter@schlaile.de> 2005
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file memutil/MEM_CacheLimiter.h
 *  \ingroup memutil
 */


#ifndef __MEM_CACHELIMITER_H__
#define __MEM_CACHELIMITER_H__

/**
 * \section MEM_CacheLimiter
 * This class defines a generic memory cache management system
 * to limit memory usage to a fixed global maximum.
 *
 * \note Please use the C-API in MEM_CacheLimiterC-Api.h for code written in C.
 *
 * Usage example:
 *
 * \code{.cpp}
 * class BigFatImage {
 * public:
 *       ~BigFatImage() { tell_everyone_we_are_gone(this); }
 * };
 *
 * void doit()
 * {
 *     MEM_Cache<BigFatImage> BigFatImages;
 *
 *     MEM_Cache_Handle<BigFatImage>* h = BigFatImages.insert(new BigFatImage);
 *
 *     BigFatImages.enforce_limits();
 *     h->ref();
 *
 *     // work with image...
 *
 *     h->unref();
 *
 *     // leave image in cache.
 * \endcode
 */

#include <list>
#include <queue>
#include <vector>
#include "MEM_Allocator.h"

template<class T>
class MEM_CacheLimiter;

#ifndef __MEM_CACHELIMITERC_API_H__
extern "C" {
	void MEM_CacheLimiter_set_maximum(size_t m);
	size_t MEM_CacheLimiter_get_maximum();
	void MEM_CacheLimiter_set_disabled(bool disabled);
	bool MEM_CacheLimiter_is_disabled(void);
};
#endif

template<class T>
class MEM_CacheLimiterHandle {
public:
	explicit MEM_CacheLimiterHandle(T * data_,MEM_CacheLimiter<T> *parent_) :
		data(data_),
		refcount(0),
		parent(parent_)
	{ }

	void ref() {
		refcount++;
	}

	void unref() {
		refcount--;
	}

	T *get() {
		return data;
	}

	const T *get() const {
		return data;
	}

	int get_refcount() const {
		return refcount;
	}

	bool can_destroy() const {
		return !data || !refcount;
	}

	bool destroy_if_possible() {
		if (can_destroy()) {
			delete data;
			data = NULL;
			unmanage();
			return true;
		}
		return false;
	}

	void unmanage() {
		parent->unmanage(this);
	}

	void touch() {
		parent->touch(this);
	}

private:
	friend class MEM_CacheLimiter<T>;

	T * data;
	int refcount;
	int pos;
	MEM_CacheLimiter<T> * parent;
};

template<class T>
class MEM_CacheLimiter {
public:
	typedef size_t (*MEM_CacheLimiter_DataSize_Func) (void *data);
	typedef int    (*MEM_CacheLimiter_ItemPriority_Func) (void *item, int default_priority);
	typedef bool   (*MEM_CacheLimiter_ItemDestroyable_Func) (void *item);

	MEM_CacheLimiter(MEM_CacheLimiter_DataSize_Func data_size_func)
		: data_size_func(data_size_func) {
	}

	~MEM_CacheLimiter() {
		int i;
		for (i = 0; i < queue.size(); i++) {
			delete queue[i];
		}
	}

	MEM_CacheLimiterHandle<T> *insert(T * elem) {
		queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
		queue.back()->pos = queue.size() - 1;
		return queue.back();
	}

	void unmanage(MEM_CacheLimiterHandle<T> *handle) {
		int pos = handle->pos;
		queue[pos] = queue.back();
		queue[pos]->pos = pos;
		queue.pop_back();
		delete handle;
	}

	size_t get_memory_in_use() {
		size_t size = 0;
		if (data_size_func) {
			int i;
			for (i = 0; i < queue.size(); i++) {
				size += data_size_func(queue[i]->get()->get_data());
			}
		}
		else {
			size = MEM_get_memory_in_use();
		}
		return size;
	}

	void enforce_limits() {
		size_t max = MEM_CacheLimiter_get_maximum();
		bool is_disabled = MEM_CacheLimiter_is_disabled();
		size_t mem_in_use, cur_size;

		if (is_disabled) {
			return;
		}

		if (max == 0) {
			return;
		}

		mem_in_use = get_memory_in_use();

		if (mem_in_use <= max) {
			return;
		}

		while (!queue.empty() && mem_in_use > max) {
			MEM_CacheElementPtr elem = get_least_priority_destroyable_element();

			if (!elem)
				break;

			if (data_size_func) {
				cur_size = data_size_func(elem->get()->get_data());
			}
			else {
				cur_size = mem_in_use;
			}

			if (elem->destroy_if_possible()) {
				if (data_size_func) {
					mem_in_use -= cur_size;
				}
				else {
					mem_in_use -= cur_size - MEM_get_memory_in_use();
				}
			}
		}
	}

	void touch(MEM_CacheLimiterHandle<T> * handle) {
		/* If we're using custom priority callback re-arranging the queue
		 * doesn't make much sense because we'll iterate it all to get
		 * least priority element anyway.
		 */
		if (item_priority_func == NULL) {
			queue[handle->pos] = queue.back();
			queue[handle->pos]->pos = handle->pos;
			queue.pop_back();
			queue.push_back(handle);
			handle->pos = queue.size() - 1;
		}
	}

	void set_item_priority_func(MEM_CacheLimiter_ItemPriority_Func item_priority_func) {
		this->item_priority_func = item_priority_func;
	}

	void set_item_destroyable_func(MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func) {
		this->item_destroyable_func = item_destroyable_func;
	}

private:
	typedef MEM_CacheLimiterHandle<T> *MEM_CacheElementPtr;
	typedef std::vector<MEM_CacheElementPtr, MEM_Allocator<MEM_CacheElementPtr> > MEM_CacheQueue;
	typedef typename MEM_CacheQueue::iterator iterator;

	/* Check whether element can be destroyed when enforcing cache limits */
	bool can_destroy_element(MEM_CacheElementPtr &elem) {
		if (!elem->can_destroy()) {
			/* Element is referenced */
			return false;
		}
		if (item_destroyable_func) {
			if (!item_destroyable_func(elem->get()->get_data()))
				return false;
		}
		return true;
	}

	MEM_CacheElementPtr get_least_priority_destroyable_element(void) {
		if (queue.empty())
			return NULL;

		MEM_CacheElementPtr best_match_elem = NULL;

		if (!item_priority_func) {
			for (iterator it = queue.begin(); it != queue.end(); it++) {
				MEM_CacheElementPtr elem = *it;
				if (!can_destroy_element(elem))
					continue;
				best_match_elem = elem;
				break;
			}
		}
		else {
			int best_match_priority = 0;
			int i;

			for (i = 0; i < queue.size(); i++) {
				MEM_CacheElementPtr elem = queue[i];

				if (!can_destroy_element(elem))
					continue;

				/* by default 0 means highest priority element */
				/* casting a size type to int is questionable,
				   but unlikely to cause problems */
				int priority = -((int)(queue.size()) - i - 1);
				priority = item_priority_func(elem->get()->get_data(), priority);

				if (priority < best_match_priority || best_match_elem == NULL) {
					best_match_priority = priority;
					best_match_elem = elem;
				}
			}
		}

		return best_match_elem;
	}

	MEM_CacheQueue queue;
	MEM_CacheLimiter_DataSize_Func data_size_func;
	MEM_CacheLimiter_ItemPriority_Func item_priority_func;
	MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func;
};

#endif  // __MEM_CACHELIMITER_H__