OLD | NEW |
1 /* | 1 /* |
2 * | 2 * |
3 * ***** BEGIN GPL LICENSE BLOCK ***** | 3 * ***** BEGIN GPL LICENSE BLOCK ***** |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or | 5 * This program is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU General Public License | 6 * modify it under the terms of the GNU General Public License |
7 * as published by the Free Software Foundation; either version 2 | 7 * as published by the Free Software Foundation; either version 2 |
8 * of the License, or (at your option) any later version. | 8 * of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This program is distributed in the hope that it will be useful, | 10 * This program is distributed in the hope that it will be useful, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 typename std::list<MEM_CacheLimiterHandle<T> *,· | 119 typename std::list<MEM_CacheLimiterHandle<T> *,· |
120 MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator me; | 120 MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator me; |
121 MEM_CacheLimiter<T> * parent; | 121 MEM_CacheLimiter<T> * parent; |
122 }; | 122 }; |
123 | 123 |
124 template<class T> | 124 template<class T> |
125 class MEM_CacheLimiter { | 125 class MEM_CacheLimiter { |
126 public: | 126 public: |
127 typedef typename std::list<MEM_CacheLimiterHandle<T> *, | 127 typedef typename std::list<MEM_CacheLimiterHandle<T> *, |
128 MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator iterator; | 128 MEM_Allocator<MEM_CacheLimiterHandle<T> *> >::iterator iterator; |
| 129 typedef intptr_t (*MEM_CacheLimiter_DataSize_Func) (void *data); |
| 130 MEM_CacheLimiter(MEM_CacheLimiter_DataSize_Func getDataSize_) |
| 131 : getDataSize(getDataSize_) { |
| 132 } |
129 ~MEM_CacheLimiter() { | 133 ~MEM_CacheLimiter() { |
130 for (iterator it = queue.begin(); it != queue.end(); it++) { | 134 for (iterator it = queue.begin(); it != queue.end(); it++) { |
131 delete *it; | 135 delete *it; |
132 } | 136 } |
133 } | 137 } |
134 MEM_CacheLimiterHandle<T> * insert(T * elem) { | 138 MEM_CacheLimiterHandle<T> * insert(T * elem) { |
135 queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this)); | 139 queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this)); |
136 iterator it = queue.end(); | 140 iterator it = queue.end(); |
137 --it; | 141 --it; |
138 queue.back()->me = it; | 142 queue.back()->me = it; |
139 return queue.back(); | 143 return queue.back(); |
140 } | 144 } |
141 void unmanage(MEM_CacheLimiterHandle<T> * handle) { | 145 void unmanage(MEM_CacheLimiterHandle<T> * handle) { |
142 queue.erase(handle->me); | 146 queue.erase(handle->me); |
143 delete handle; | 147 delete handle; |
144 } | 148 } |
145 void enforce_limits() { | 149 void enforce_limits() { |
146 intptr_t max = MEM_CacheLimiter_get_maximum(); | 150 intptr_t max = MEM_CacheLimiter_get_maximum(); |
147 » » intptr_t mem_in_use= MEM_get_memory_in_use(); | 151 » » intptr_t mem_in_use, cur_size; |
148 » » intptr_t mmap_in_use= MEM_get_mapped_memory_in_use(); | |
149 | 152 |
150 if (max == 0) { | 153 if (max == 0) { |
151 return; | 154 return; |
152 } | 155 } |
| 156 |
| 157 if(getDataSize) { |
| 158 mem_in_use = total_size(); |
| 159 } else { |
| 160 mem_in_use = MEM_get_memory_in_use(); |
| 161 } |
| 162 |
153 for (iterator it = queue.begin();· | 163 for (iterator it = queue.begin();· |
154 » » it != queue.end() && mem_in_use + mmap_in_use > max;) { | 164 » » it != queue.end() && mem_in_use > max;) { |
155 iterator jt = it; | 165 iterator jt = it; |
156 ++it; | 166 ++it; |
| 167 |
| 168 if(getDataSize) { |
| 169 cur_size= getDataSize((*jt)->get()->get_data()); |
| 170 } else { |
| 171 cur_size= mem_in_use; |
| 172 } |
| 173 |
157 (*jt)->destroy_if_possible(); | 174 (*jt)->destroy_if_possible(); |
| 175 |
| 176 if(getDataSize) { |
| 177 mem_in_use-= cur_size; |
| 178 } else { |
| 179 mem_in_use-= cur_size - MEM_get_memory_in_use(); |
| 180 } |
158 } | 181 } |
159 } | 182 } |
160 void touch(MEM_CacheLimiterHandle<T> * handle) { | 183 void touch(MEM_CacheLimiterHandle<T> * handle) { |
161 queue.push_back(handle); | 184 queue.push_back(handle); |
162 queue.erase(handle->me); | 185 queue.erase(handle->me); |
163 iterator it = queue.end(); | 186 iterator it = queue.end(); |
164 --it; | 187 --it; |
165 handle->me = it; | 188 handle->me = it; |
166 } | 189 } |
167 private: | 190 private: |
| 191 intptr_t total_size() { |
| 192 intptr_t size = 0; |
| 193 for (iterator it = queue.begin(); it != queue.end(); it++) { |
| 194 size+= getDataSize((*it)->get()->get_data()); |
| 195 } |
| 196 return size; |
| 197 } |
| 198 |
168 std::list<MEM_CacheLimiterHandle<T>*, | 199 std::list<MEM_CacheLimiterHandle<T>*, |
169 MEM_Allocator<MEM_CacheLimiterHandle<T> *> > queue; | 200 MEM_Allocator<MEM_CacheLimiterHandle<T> *> > queue; |
| 201 MEM_CacheLimiter_DataSize_Func getDataSize; |
170 }; | 202 }; |
171 | 203 |
172 #endif // MEM_CACHELIMITER_H | 204 #endif // MEM_CACHELIMITER_H |
OLD | NEW |