2011-02-25 11:47:18 +00:00
|
|
|
/*
|
2006-02-05 18:56:30 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-01-16 19:55:50 +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.
|
|
|
|
*
|
|
|
|
* 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); }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include "MEM_Allocator.h"
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class MEM_CacheLimiter;
|
|
|
|
|
|
|
|
#ifndef __MEM_cache_limiter_c_api_h_included__
|
|
|
|
extern "C" {
|
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
|
|
|
extern void MEM_CacheLimiter_set_maximum(intptr_t m);
|
|
|
|
extern intptr_t MEM_CacheLimiter_get_maximum();
|
2006-02-05 18:56:30 +00:00
|
|
|
};
|
|
|
|
#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 = 0;
|
|
|
|
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;
|
|
|
|
typename std::list<MEM_CacheLimiterHandle<T> *,
|
|
|
|
MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator me;
|
|
|
|
MEM_CacheLimiter<T> * parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class MEM_CacheLimiter {
|
|
|
|
public:
|
|
|
|
typedef typename std::list<MEM_CacheLimiterHandle<T> *,
|
|
|
|
MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator iterator;
|
|
|
|
~MEM_CacheLimiter() {
|
|
|
|
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MEM_CacheLimiterHandle<T> * insert(T * elem) {
|
|
|
|
queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
|
|
|
|
iterator it = queue.end();
|
|
|
|
--it;
|
|
|
|
queue.back()->me = it;
|
|
|
|
return queue.back();
|
|
|
|
}
|
|
|
|
void unmanage(MEM_CacheLimiterHandle<T> * handle) {
|
|
|
|
queue.erase(handle->me);
|
|
|
|
delete handle;
|
|
|
|
}
|
|
|
|
void enforce_limits() {
|
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
|
|
|
intptr_t max = MEM_CacheLimiter_get_maximum();
|
|
|
|
intptr_t mem_in_use= MEM_get_memory_in_use();
|
|
|
|
intptr_t mmap_in_use= MEM_get_mapped_memory_in_use();
|
|
|
|
|
2006-02-05 18:56:30 +00:00
|
|
|
if (max == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (iterator it = queue.begin();
|
2007-09-02 19:32:22 +00:00
|
|
|
it != queue.end() && mem_in_use + mmap_in_use > max;) {
|
2006-02-05 18:56:30 +00:00
|
|
|
iterator jt = it;
|
|
|
|
++it;
|
|
|
|
(*jt)->destroy_if_possible();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void touch(MEM_CacheLimiterHandle<T> * handle) {
|
|
|
|
queue.push_back(handle);
|
|
|
|
queue.erase(handle->me);
|
|
|
|
iterator it = queue.end();
|
|
|
|
--it;
|
|
|
|
handle->me = it;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::list<MEM_CacheLimiterHandle<T>*,
|
|
|
|
MEM_Allocator<MEM_CacheLimiterHandle<T> *> > queue;
|
|
|
|
};
|
|
|
|
|
2011-01-16 19:55:50 +00:00
|
|
|
#endif // MEM_CACHELIMITER_H
|