2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2006-2022 Blender Authors
|
2023-06-15 05:09:04 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2006-02-05 19:56:30 +01:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
2021-12-14 08:35:31 +01:00
|
|
|
* \ingroup intern_memutil
|
2011-02-25 12:47:18 +01:00
|
|
|
*/
|
|
|
|
|
2012-02-17 19:59:41 +01:00
|
|
|
#ifndef __MEM_CACHELIMITER_H__
|
|
|
|
#define __MEM_CACHELIMITER_H__
|
2006-02-05 19:56:30 +01:00
|
|
|
|
|
|
|
/**
|
2015-05-20 04:54:45 +02:00
|
|
|
* \section MEM_CacheLimiter
|
2006-02-05 19:56:30 +01:00
|
|
|
* This class defines a generic memory cache management system
|
|
|
|
* to limit memory usage to a fixed global maximum.
|
2012-07-10 16:43:50 +02:00
|
|
|
*
|
2015-05-20 04:54:45 +02:00
|
|
|
* \note Please use the C-API in MEM_CacheLimiterC-Api.h for code written in C.
|
2006-02-05 19:56:30 +01:00
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
*
|
2015-05-20 04:54:45 +02:00
|
|
|
* \code{.cpp}
|
2006-02-05 19:56:30 +01:00
|
|
|
* class BigFatImage {
|
|
|
|
* public:
|
|
|
|
* ~BigFatImage() { tell_everyone_we_are_gone(this); }
|
|
|
|
* };
|
2012-07-10 16:43:50 +02:00
|
|
|
*
|
2015-05-20 04:54:45 +02:00
|
|
|
* void doit()
|
|
|
|
* {
|
2006-02-05 19:56:30 +01:00
|
|
|
* MEM_Cache<BigFatImage> BigFatImages;
|
|
|
|
*
|
|
|
|
* MEM_Cache_Handle<BigFatImage>* h = BigFatImages.insert(new BigFatImage);
|
2012-07-10 16:43:50 +02:00
|
|
|
*
|
2006-02-05 19:56:30 +01:00
|
|
|
* BigFatImages.enforce_limits();
|
|
|
|
* h->ref();
|
|
|
|
*
|
2015-05-20 04:54:45 +02:00
|
|
|
* // work with image...
|
2006-02-05 19:56:30 +01:00
|
|
|
*
|
|
|
|
* h->unref();
|
|
|
|
*
|
2015-05-20 04:54:45 +02:00
|
|
|
* // leave image in cache.
|
|
|
|
* \endcode
|
2006-02-05 19:56:30 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "MEM_Allocator.h"
|
2006-02-05 19:56:30 +01:00
|
|
|
#include <list>
|
2012-07-10 16:43:50 +02:00
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
2006-02-05 19:56:30 +01:00
|
|
|
|
|
|
|
template<class T> class MEM_CacheLimiter;
|
|
|
|
|
2012-02-17 19:59:41 +01:00
|
|
|
#ifndef __MEM_CACHELIMITERC_API_H__
|
2006-02-05 19:56:30 +01:00
|
|
|
extern "C" {
|
2012-07-10 16:43:50 +02:00
|
|
|
void MEM_CacheLimiter_set_maximum(size_t m);
|
|
|
|
size_t MEM_CacheLimiter_get_maximum();
|
2013-12-22 10:26:59 +01:00
|
|
|
void MEM_CacheLimiter_set_disabled(bool disabled);
|
|
|
|
bool MEM_CacheLimiter_is_disabled(void);
|
2006-02-05 19:56:30 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<class T> class MEM_CacheLimiterHandle {
|
|
|
|
public:
|
2012-07-10 16:43:50 +02:00
|
|
|
explicit MEM_CacheLimiterHandle(T *data_, MEM_CacheLimiter<T> *parent_)
|
|
|
|
: data(data_), refcount(0), parent(parent_)
|
|
|
|
{
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
void ref()
|
|
|
|
{
|
|
|
|
refcount++;
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
void unref()
|
|
|
|
{
|
|
|
|
refcount--;
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
T *get()
|
|
|
|
{
|
|
|
|
return data;
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
const T *get() const
|
|
|
|
{
|
|
|
|
return data;
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
int get_refcount() const
|
|
|
|
{
|
|
|
|
return refcount;
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
bool can_destroy() const
|
|
|
|
{
|
|
|
|
return !data || !refcount;
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
bool destroy_if_possible()
|
|
|
|
{
|
|
|
|
if (can_destroy()) {
|
|
|
|
delete data;
|
2013-03-08 07:32:00 +01:00
|
|
|
data = NULL;
|
2006-02-05 19:56:30 +01:00
|
|
|
unmanage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
void unmanage()
|
|
|
|
{
|
|
|
|
parent->unmanage(this);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
void touch()
|
|
|
|
{
|
|
|
|
parent->touch(this);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
private:
|
|
|
|
friend class MEM_CacheLimiter<T>;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
T *data;
|
|
|
|
int refcount;
|
2015-02-05 15:06:13 +01:00
|
|
|
int pos;
|
2006-02-05 19:56:30 +01:00
|
|
|
MEM_CacheLimiter<T> *parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T> class MEM_CacheLimiter {
|
|
|
|
public:
|
2012-01-31 12:11:56 +01:00
|
|
|
typedef size_t (*MEM_CacheLimiter_DataSize_Func)(void *data);
|
2012-07-10 16:43:50 +02:00
|
|
|
typedef int (*MEM_CacheLimiter_ItemPriority_Func)(void *item, int default_priority);
|
2013-12-10 09:40:09 +01:00
|
|
|
typedef bool (*MEM_CacheLimiter_ItemDestroyable_Func)(void *item);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 10:07:10 +01:00
|
|
|
MEM_CacheLimiter(MEM_CacheLimiter_DataSize_Func data_size_func) : data_size_func(data_size_func)
|
|
|
|
{
|
2011-10-23 19:52:20 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
~MEM_CacheLimiter()
|
|
|
|
{
|
2015-02-05 15:06:13 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < queue.size(); i++) {
|
|
|
|
delete queue[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
MEM_CacheLimiterHandle<T> *insert(T *elem)
|
|
|
|
{
|
2006-02-05 19:56:30 +01:00
|
|
|
queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
|
2015-02-05 15:06:13 +01:00
|
|
|
queue.back()->pos = queue.size() - 1;
|
2006-02-05 19:56:30 +01:00
|
|
|
return queue.back();
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
void unmanage(MEM_CacheLimiterHandle<T> *handle)
|
|
|
|
{
|
2015-02-05 15:06:13 +01:00
|
|
|
int pos = handle->pos;
|
|
|
|
queue[pos] = queue.back();
|
|
|
|
queue[pos]->pos = pos;
|
|
|
|
queue.pop_back();
|
2006-02-05 19:56:30 +01:00
|
|
|
delete handle;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-20 18:03:20 +01:00
|
|
|
size_t get_memory_in_use()
|
|
|
|
{
|
2013-12-10 10:07:10 +01:00
|
|
|
size_t size = 0;
|
|
|
|
if (data_size_func) {
|
2015-02-05 15:06:13 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < queue.size(); i++) {
|
|
|
|
size += data_size_func(queue[i]->get()->get_data());
|
2013-12-10 10:07:10 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-12-10 10:07:10 +01:00
|
|
|
else {
|
|
|
|
size = MEM_get_memory_in_use();
|
|
|
|
}
|
|
|
|
return size;
|
2013-03-20 18:03:20 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
void enforce_limits()
|
|
|
|
{
|
2012-01-31 12:11:56 +01:00
|
|
|
size_t max = MEM_CacheLimiter_get_maximum();
|
2013-12-22 10:26:59 +01:00
|
|
|
bool is_disabled = MEM_CacheLimiter_is_disabled();
|
2012-01-31 12:11:56 +01:00
|
|
|
size_t mem_in_use, cur_size;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-22 10:26:59 +01:00
|
|
|
if (is_disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
if (max == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-20 18:03:20 +01:00
|
|
|
mem_in_use = get_memory_in_use();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
if (mem_in_use <= max) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-20 14:59:16 +02:00
|
|
|
while (!queue.empty() && mem_in_use > max) {
|
|
|
|
MEM_CacheElementPtr elem = get_least_priority_destroyable_element();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-20 14:59:16 +02:00
|
|
|
if (!elem)
|
|
|
|
break;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 10:07:10 +01:00
|
|
|
if (data_size_func) {
|
|
|
|
cur_size = data_size_func(elem->get()->get_data());
|
2012-09-18 00:34:42 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-07-10 16:43:50 +02:00
|
|
|
cur_size = mem_in_use;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
if (elem->destroy_if_possible()) {
|
2013-12-10 10:07:10 +01:00
|
|
|
if (data_size_func) {
|
2012-07-10 16:43:50 +02:00
|
|
|
mem_in_use -= cur_size;
|
2012-09-18 00:34:42 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-07-10 16:43:50 +02:00
|
|
|
mem_in_use -= cur_size - MEM_get_memory_in_use();
|
|
|
|
}
|
2011-10-23 19:52:20 +02:00
|
|
|
}
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
void touch(MEM_CacheLimiterHandle<T> *handle)
|
|
|
|
{
|
2013-12-10 10:07:10 +01: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) {
|
2015-02-05 15:06:13 +01:00
|
|
|
queue[handle->pos] = queue.back();
|
|
|
|
queue[handle->pos]->pos = handle->pos;
|
|
|
|
queue.pop_back();
|
2013-12-10 10:07:10 +01:00
|
|
|
queue.push_back(handle);
|
2015-02-05 15:06:13 +01:00
|
|
|
handle->pos = queue.size() - 1;
|
2013-12-10 10:07:10 +01:00
|
|
|
}
|
2006-02-05 19:56:30 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
void set_item_priority_func(MEM_CacheLimiter_ItemPriority_Func item_priority_func)
|
|
|
|
{
|
2013-12-10 10:07:10 +01:00
|
|
|
this->item_priority_func = item_priority_func;
|
2012-07-10 16:43:50 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 09:40:09 +01:00
|
|
|
void set_item_destroyable_func(MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func)
|
|
|
|
{
|
|
|
|
this->item_destroyable_func = item_destroyable_func;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-02-05 19:56:30 +01:00
|
|
|
private:
|
2012-07-10 16:43:50 +02:00
|
|
|
typedef MEM_CacheLimiterHandle<T> *MEM_CacheElementPtr;
|
2015-02-05 15:06:13 +01:00
|
|
|
typedef std::vector<MEM_CacheElementPtr, MEM_Allocator<MEM_CacheElementPtr>> MEM_CacheQueue;
|
2012-07-10 16:43:50 +02:00
|
|
|
typedef typename MEM_CacheQueue::iterator iterator;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 09:40:09 +01: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;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-20 14:59:16 +02:00
|
|
|
MEM_CacheElementPtr get_least_priority_destroyable_element(void)
|
|
|
|
{
|
|
|
|
if (queue.empty())
|
|
|
|
return NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-20 14:59:16 +02:00
|
|
|
MEM_CacheElementPtr best_match_elem = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 10:07:10 +01:00
|
|
|
if (!item_priority_func) {
|
|
|
|
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
|
|
|
MEM_CacheElementPtr elem = *it;
|
2013-12-10 09:40:09 +01:00
|
|
|
if (!can_destroy_element(elem))
|
2013-12-10 10:07:10 +01:00
|
|
|
continue;
|
2012-09-20 14:59:16 +02:00
|
|
|
best_match_elem = elem;
|
2013-12-10 10:07:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-12-10 10:07:10 +01:00
|
|
|
else {
|
|
|
|
int best_match_priority = 0;
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 15:06:13 +01:00
|
|
|
for (i = 0; i < queue.size(); i++) {
|
|
|
|
MEM_CacheElementPtr elem = queue[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 09:40:09 +01:00
|
|
|
if (!can_destroy_element(elem))
|
2013-12-10 10:07:10 +01:00
|
|
|
continue;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-30 14:20:13 +02:00
|
|
|
/* By default 0 means highest priority element. */
|
|
|
|
/* Casting a size type to int is questionable,
|
|
|
|
* but unlikely to cause problems. */
|
2013-12-10 10:07:10 +01:00
|
|
|
int priority = -((int)(queue.size()) - i - 1);
|
|
|
|
priority = item_priority_func(elem->get()->get_data(), priority);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-10 10:07:10 +01:00
|
|
|
if (priority < best_match_priority || best_match_elem == NULL) {
|
|
|
|
best_match_priority = priority;
|
|
|
|
best_match_elem = elem;
|
2012-07-10 16:43:50 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-07-10 16:43:50 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-20 14:59:16 +02:00
|
|
|
return best_match_elem;
|
2012-07-10 16:43:50 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-10 16:43:50 +02:00
|
|
|
MEM_CacheQueue queue;
|
2013-12-10 10:07:10 +01:00
|
|
|
MEM_CacheLimiter_DataSize_Func data_size_func;
|
|
|
|
MEM_CacheLimiter_ItemPriority_Func item_priority_func;
|
2013-12-10 09:40:09 +01:00
|
|
|
MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func;
|
2006-02-05 19:56:30 +01:00
|
|
|
};
|
|
|
|
|
2013-12-10 10:07:10 +01:00
|
|
|
#endif // __MEM_CACHELIMITER_H__
|