2011-02-25 11:47:18 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2006-02-05 18:56:30 +00:00
|
|
|
*
|
|
|
|
* 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
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2006-02-05 18:56:30 +00:00
|
|
|
*
|
|
|
|
* 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,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-02-05 18:56:30 +00:00
|
|
|
*
|
|
|
|
* Contributor(s): Peter Schlaile <peter@schlaile.de> 2005
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2006-02-05 18:56:30 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-25 11:47:18 +00:00
|
|
|
/** \file memutil/MEM_CacheLimiter.h
|
|
|
|
* \ingroup memutil
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#ifndef __MEM_CACHELIMITER_H__
|
|
|
|
#define __MEM_CACHELIMITER_H__
|
2006-02-05 18:56:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @section MEM_CacheLimiter
|
|
|
|
* This class defines a generic memory cache management system
|
|
|
|
* to limit memory usage to a fixed global maximum.
|
2012-07-10 14:43:50 +00:00
|
|
|
*
|
2006-02-05 18:56:30 +00:00
|
|
|
* Please use the C-API in MEM_CacheLimiterC-Api.h for code written in C.
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
*
|
|
|
|
* class BigFatImage {
|
|
|
|
* public:
|
|
|
|
* ~BigFatImage() { tell_everyone_we_are_gone(this); }
|
|
|
|
* };
|
2012-07-10 14:43:50 +00:00
|
|
|
*
|
2006-02-05 18:56:30 +00:00
|
|
|
* void doit() {
|
|
|
|
* MEM_Cache<BigFatImage> BigFatImages;
|
|
|
|
*
|
|
|
|
* MEM_Cache_Handle<BigFatImage>* h = BigFatImages.insert(new BigFatImage);
|
2012-07-10 14:43:50 +00:00
|
|
|
*
|
2006-02-05 18:56:30 +00:00
|
|
|
* BigFatImages.enforce_limits();
|
|
|
|
* h->ref();
|
|
|
|
*
|
|
|
|
* work with image...
|
|
|
|
*
|
|
|
|
* h->unref();
|
|
|
|
*
|
|
|
|
* leave image in cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <list>
|
2012-07-10 14:43:50 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
2006-02-05 18:56:30 +00:00
|
|
|
#include "MEM_Allocator.h"
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class MEM_CacheLimiter;
|
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#ifndef __MEM_CACHELIMITERC_API_H__
|
2006-02-05 18:56:30 +00:00
|
|
|
extern "C" {
|
2012-07-10 14:43:50 +00:00
|
|
|
void MEM_CacheLimiter_set_maximum(size_t m);
|
|
|
|
size_t MEM_CacheLimiter_get_maximum();
|
2013-12-22 09:26:59 +00:00
|
|
|
void MEM_CacheLimiter_set_disabled(bool disabled);
|
|
|
|
bool MEM_CacheLimiter_is_disabled(void);
|
2006-02-05 18:56:30 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class MEM_CacheLimiterHandle {
|
|
|
|
public:
|
2012-07-10 14:43:50 +00:00
|
|
|
explicit MEM_CacheLimiterHandle(T * data_,MEM_CacheLimiter<T> *parent_) :
|
|
|
|
data(data_),
|
|
|
|
refcount(0),
|
|
|
|
parent(parent_)
|
|
|
|
{ }
|
2006-02-05 18:56:30 +00:00
|
|
|
|
2012-07-10 14:43:50 +00:00
|
|
|
void ref() {
|
|
|
|
refcount++;
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
void unref() {
|
|
|
|
refcount--;
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
T *get() {
|
|
|
|
return data;
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
const T *get() const {
|
|
|
|
return data;
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
int get_refcount() const {
|
|
|
|
return refcount;
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
bool can_destroy() const {
|
|
|
|
return !data || !refcount;
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
bool destroy_if_possible() {
|
|
|
|
if (can_destroy()) {
|
|
|
|
delete data;
|
2013-03-08 06:32:00 +00:00
|
|
|
data = NULL;
|
2006-02-05 18:56:30 +00:00
|
|
|
unmanage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
void unmanage() {
|
|
|
|
parent->unmanage(this);
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
void touch() {
|
|
|
|
parent->touch(this);
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
private:
|
|
|
|
friend class MEM_CacheLimiter<T>;
|
|
|
|
|
|
|
|
T * data;
|
|
|
|
int refcount;
|
2012-07-10 14:43:50 +00:00
|
|
|
typename std::list<MEM_CacheLimiterHandle<T> *, MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator me;
|
2006-02-05 18:56:30 +00:00
|
|
|
MEM_CacheLimiter<T> * parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class MEM_CacheLimiter {
|
|
|
|
public:
|
2012-01-31 11:11:56 +00:00
|
|
|
typedef size_t (*MEM_CacheLimiter_DataSize_Func) (void *data);
|
2012-07-10 14:43:50 +00:00
|
|
|
typedef int (*MEM_CacheLimiter_ItemPriority_Func) (void *item, int default_priority);
|
2013-12-10 08:40:09 +00:00
|
|
|
typedef bool (*MEM_CacheLimiter_ItemDestroyable_Func) (void *item);
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2013-12-10 09:07:10 +00:00
|
|
|
MEM_CacheLimiter(MEM_CacheLimiter_DataSize_Func data_size_func)
|
|
|
|
: data_size_func(data_size_func) {
|
2011-10-23 17:52:20 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
~MEM_CacheLimiter() {
|
|
|
|
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
MEM_CacheLimiterHandle<T> *insert(T * elem) {
|
2006-02-05 18:56:30 +00:00
|
|
|
queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
|
|
|
|
iterator it = queue.end();
|
|
|
|
--it;
|
|
|
|
queue.back()->me = it;
|
|
|
|
return queue.back();
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
void unmanage(MEM_CacheLimiterHandle<T> *handle) {
|
2006-02-05 18:56:30 +00:00
|
|
|
queue.erase(handle->me);
|
|
|
|
delete handle;
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2013-03-20 17:03:20 +00:00
|
|
|
size_t get_memory_in_use() {
|
2013-12-10 09:07:10 +00:00
|
|
|
size_t size = 0;
|
|
|
|
if (data_size_func) {
|
|
|
|
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
|
|
|
size += data_size_func((*it)->get()->get_data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = MEM_get_memory_in_use();
|
|
|
|
}
|
|
|
|
return size;
|
2013-03-20 17:03:20 +00:00
|
|
|
}
|
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
void enforce_limits() {
|
2012-01-31 11:11:56 +00:00
|
|
|
size_t max = MEM_CacheLimiter_get_maximum();
|
2013-12-22 09:26:59 +00:00
|
|
|
bool is_disabled = MEM_CacheLimiter_is_disabled();
|
2012-01-31 11:11:56 +00:00
|
|
|
size_t mem_in_use, cur_size;
|
Merge of first part of changes from the apricot branch, especially
the features that are needed to run the game. Compile tested with
scons, make, but not cmake, that seems to have an issue not related
to these changes. The changes include:
* GLSL support in the viewport and game engine, enable in the game
menu in textured draw mode.
* Synced and merged part of the duplicated blender and gameengine/
gameplayer drawing code.
* Further refactoring of game engine drawing code, especially mesh
storage changed a lot.
* Optimizations in game engine armatures to avoid recomputations.
* A python function to get the framerate estimate in game.
* An option take object color into account in materials.
* An option to restrict shadow casters to a lamp's layers.
* Increase from 10 to 18 texture slots for materials, lamps, word.
An extra texture slot shows up once the last slot is used.
* Memory limit for undo, not enabled by default yet because it
needs the .B.blend to be changed.
* Multiple undo for image painting.
* An offset for dupligroups, so not all objects in a group have to
be at the origin.
2008-09-04 20:51:28 +00:00
|
|
|
|
2013-12-22 09:26:59 +00:00
|
|
|
if (is_disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
if (max == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-23 17:52:20 +00:00
|
|
|
|
2013-03-20 17:03:20 +00:00
|
|
|
mem_in_use = get_memory_in_use();
|
2011-10-23 17:52:20 +00:00
|
|
|
|
2012-07-10 14:43:50 +00:00
|
|
|
if (mem_in_use <= max) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-23 17:52:20 +00:00
|
|
|
|
2012-09-20 12:59:16 +00:00
|
|
|
while (!queue.empty() && mem_in_use > max) {
|
|
|
|
MEM_CacheElementPtr elem = get_least_priority_destroyable_element();
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2012-09-20 12:59:16 +00:00
|
|
|
if (!elem)
|
|
|
|
break;
|
2011-10-23 17:52:20 +00:00
|
|
|
|
2013-12-10 09:07:10 +00:00
|
|
|
if (data_size_func) {
|
|
|
|
cur_size = data_size_func(elem->get()->get_data());
|
2012-09-17 22:34:42 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-10 14:43:50 +00:00
|
|
|
cur_size = mem_in_use;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elem->destroy_if_possible()) {
|
2013-12-10 09:07:10 +00:00
|
|
|
if (data_size_func) {
|
2012-07-10 14:43:50 +00:00
|
|
|
mem_in_use -= cur_size;
|
2012-09-17 22:34:42 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-10 14:43:50 +00:00
|
|
|
mem_in_use -= cur_size - MEM_get_memory_in_use();
|
|
|
|
}
|
2011-10-23 17:52:20 +00:00
|
|
|
}
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
void touch(MEM_CacheLimiterHandle<T> * handle) {
|
2013-12-10 09:07:10 +00:00
|
|
|
/* 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.push_back(handle);
|
|
|
|
queue.erase(handle->me);
|
|
|
|
iterator it = queue.end();
|
|
|
|
--it;
|
|
|
|
handle->me = it;
|
|
|
|
}
|
2006-02-05 18:56:30 +00:00
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
|
|
|
|
void set_item_priority_func(MEM_CacheLimiter_ItemPriority_Func item_priority_func) {
|
2013-12-10 09:07:10 +00:00
|
|
|
this->item_priority_func = item_priority_func;
|
2012-07-10 14:43:50 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 08:40:09 +00:00
|
|
|
void set_item_destroyable_func(MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func) {
|
|
|
|
this->item_destroyable_func = item_destroyable_func;
|
|
|
|
}
|
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
private:
|
2012-07-10 14:43:50 +00:00
|
|
|
typedef MEM_CacheLimiterHandle<T> *MEM_CacheElementPtr;
|
|
|
|
typedef std::list<MEM_CacheElementPtr, MEM_Allocator<MEM_CacheElementPtr> > MEM_CacheQueue;
|
|
|
|
typedef typename MEM_CacheQueue::iterator iterator;
|
|
|
|
|
2013-12-10 08:40:09 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2012-09-20 12:59:16 +00:00
|
|
|
MEM_CacheElementPtr get_least_priority_destroyable_element(void) {
|
|
|
|
if (queue.empty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
MEM_CacheElementPtr best_match_elem = NULL;
|
2012-07-10 14:43:50 +00:00
|
|
|
|
2013-12-10 09:07:10 +00:00
|
|
|
if (!item_priority_func) {
|
|
|
|
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
|
|
|
MEM_CacheElementPtr elem = *it;
|
2013-12-10 08:40:09 +00:00
|
|
|
if (!can_destroy_element(elem))
|
2013-12-10 09:07:10 +00:00
|
|
|
continue;
|
2012-09-20 12:59:16 +00:00
|
|
|
best_match_elem = elem;
|
2013-12-10 09:07:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int best_match_priority = 0;
|
|
|
|
iterator it;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (it = queue.begin(), i = 0; it != queue.end(); it++, i++) {
|
|
|
|
MEM_CacheElementPtr elem = *it;
|
|
|
|
|
2013-12-10 08:40:09 +00:00
|
|
|
if (!can_destroy_element(elem))
|
2013-12-10 09:07:10 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-07-10 14:43:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 12:59:16 +00:00
|
|
|
return best_match_elem;
|
2012-07-10 14:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MEM_CacheQueue queue;
|
2013-12-10 09:07:10 +00:00
|
|
|
MEM_CacheLimiter_DataSize_Func data_size_func;
|
|
|
|
MEM_CacheLimiter_ItemPriority_Func item_priority_func;
|
2013-12-10 08:40:09 +00:00
|
|
|
MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func;
|
2006-02-05 18:56:30 +00:00
|
|
|
};
|
|
|
|
|
2013-12-10 09:07:10 +00:00
|
|
|
#endif // __MEM_CACHELIMITER_H__
|