diff options
Diffstat (limited to 'intern/memutil/MEM_CacheLimiter.h')
-rw-r--r-- | intern/memutil/MEM_CacheLimiter.h | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/intern/memutil/MEM_CacheLimiter.h b/intern/memutil/MEM_CacheLimiter.h index 32f97a21815..88e06833b4a 100644 --- a/intern/memutil/MEM_CacheLimiter.h +++ b/intern/memutil/MEM_CacheLimiter.h @@ -137,6 +137,7 @@ 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) { @@ -230,11 +231,28 @@ public: 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::list<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; @@ -244,7 +262,7 @@ private: if (!item_priority_func) { for (iterator it = queue.begin(); it != queue.end(); it++) { MEM_CacheElementPtr elem = *it; - if (!elem->can_destroy()) + if (!can_destroy_element(elem)) continue; best_match_elem = elem; break; @@ -258,7 +276,7 @@ private: for (it = queue.begin(), i = 0; it != queue.end(); it++, i++) { MEM_CacheElementPtr elem = *it; - if (!elem->can_destroy()) + if (!can_destroy_element(elem)) continue; /* by default 0 means highest priority element */ @@ -280,6 +298,7 @@ private: 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__ |