2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version. The Blender
6 * Foundation also sells licenses for use in proprietary software under
7 * the Blender License. See http://www.blender.org/BL/ for information
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Contributor(s): Peter Schlaile <peter@schlaile.de> 2005
27 #include "MEM_CacheLimiter.h"
28 #include "MEM_CacheLimiterC-Api.h"
30 static intptr_t & get_max()
32 static intptr_t m = 32*1024*1024;
36 void MEM_CacheLimiter_set_maximum(intptr_t m)
41 intptr_t MEM_CacheLimiter_get_maximum()
46 class MEM_CacheLimiterHandleCClass;
47 class MEM_CacheLimiterCClass;
49 typedef MEM_CacheLimiterHandle<MEM_CacheLimiterHandleCClass> handle_t;
50 typedef MEM_CacheLimiter<MEM_CacheLimiterHandleCClass> cache_t;
51 typedef std::list<MEM_CacheLimiterHandleCClass*,
52 MEM_Allocator<MEM_CacheLimiterHandleCClass* > > list_t;
54 class MEM_CacheLimiterCClass {
56 MEM_CacheLimiterCClass(MEM_CacheLimiter_Destruct_Func data_destructor_)
57 : data_destructor(data_destructor_) {
59 ~MEM_CacheLimiterCClass();
61 handle_t * insert(void * data);
63 void destruct(void * data,
66 cache_t * get_cache() {
70 MEM_CacheLimiter_Destruct_Func data_destructor;
72 MEM_CacheLimiter<MEM_CacheLimiterHandleCClass> cache;
77 class MEM_CacheLimiterHandleCClass {
79 MEM_CacheLimiterHandleCClass(void * data_,
80 MEM_CacheLimiterCClass * parent_)
81 : data(data_), parent(parent_) { }
82 ~MEM_CacheLimiterHandleCClass();
83 void set_iter(list_t::iterator it_) {
86 void set_data(void * data_) {
89 void * get_data() const {
94 MEM_CacheLimiterCClass * parent;
98 handle_t * MEM_CacheLimiterCClass::insert(void * data)
100 cclass_list.push_back(new MEM_CacheLimiterHandleCClass(data, this));
101 list_t::iterator it = cclass_list.end();
103 cclass_list.back()->set_iter(it);
105 return cache.insert(cclass_list.back());
108 void MEM_CacheLimiterCClass::destruct(void * data, list_t::iterator it)
110 data_destructor(data);
111 cclass_list.erase(it);
114 MEM_CacheLimiterHandleCClass::~MEM_CacheLimiterHandleCClass()
117 parent->destruct(data, it);
121 MEM_CacheLimiterCClass::~MEM_CacheLimiterCClass()
123 // should not happen, but don't leak memory in this case...
124 for (list_t::iterator it = cclass_list.begin();
125 it != cclass_list.end(); it++) {
131 // ----------------------------------------------------------------------
133 static inline MEM_CacheLimiterCClass* cast(MEM_CacheLimiterC * l)
135 return (MEM_CacheLimiterCClass*) l;
138 static inline handle_t* cast(MEM_CacheLimiterHandleC * l)
140 return (handle_t*) l;
143 MEM_CacheLimiterC * new_MEM_CacheLimiter(
144 MEM_CacheLimiter_Destruct_Func data_destructor)
146 return (MEM_CacheLimiterC*) new MEM_CacheLimiterCClass(
150 void delete_MEM_CacheLimiter(MEM_CacheLimiterC * This)
155 MEM_CacheLimiterHandleC * MEM_CacheLimiter_insert(
156 MEM_CacheLimiterC * This, void * data)
158 return (MEM_CacheLimiterHandleC *) cast(This)->insert(data);
161 void MEM_CacheLimiter_enforce_limits(MEM_CacheLimiterC * This)
163 cast(This)->get_cache()->enforce_limits();
166 void MEM_CacheLimiter_unmanage(MEM_CacheLimiterHandleC * handle)
168 cast(handle)->unmanage();
171 void MEM_CacheLimiter_touch(MEM_CacheLimiterHandleC * handle)
173 cast(handle)->touch();
176 void MEM_CacheLimiter_ref(MEM_CacheLimiterHandleC * handle)
181 void MEM_CacheLimiter_unref(MEM_CacheLimiterHandleC * handle)
183 cast(handle)->unref();
186 int MEM_CacheLimiter_get_refcount(MEM_CacheLimiterHandleC * handle)
188 return cast(handle)->get_refcount();
192 void * MEM_CacheLimiter_get(MEM_CacheLimiterHandleC * handle)
194 return cast(handle)->get()->get_data();