ClangFormat: apply to source, most of intern
Apply clang format as proposed in T53211. For details on usage and instructions for migrating branches without conflicts, see: https://wiki.blender.org/wiki/Tools/ClangFormat
This commit is contained in:
@@ -19,8 +19,8 @@
|
||||
# ***** END GPL LICENSE BLOCK *****
|
||||
|
||||
set(INC
|
||||
.
|
||||
..
|
||||
.
|
||||
..
|
||||
)
|
||||
|
||||
set(INC_SYS
|
||||
@@ -28,14 +28,14 @@ set(INC_SYS
|
||||
)
|
||||
|
||||
set(SRC
|
||||
intern/MEM_CacheLimiterC-Api.cpp
|
||||
intern/MEM_RefCountedC-Api.cpp
|
||||
intern/MEM_CacheLimiterC-Api.cpp
|
||||
intern/MEM_RefCountedC-Api.cpp
|
||||
|
||||
MEM_Allocator.h
|
||||
MEM_CacheLimiter.h
|
||||
MEM_CacheLimiterC-Api.h
|
||||
MEM_RefCounted.h
|
||||
MEM_RefCountedC-Api.h
|
||||
MEM_Allocator.h
|
||||
MEM_CacheLimiter.h
|
||||
MEM_CacheLimiterC-Api.h
|
||||
MEM_RefCounted.h
|
||||
MEM_RefCountedC-Api.h
|
||||
)
|
||||
|
||||
set(LIB
|
||||
|
||||
@@ -18,68 +18,80 @@
|
||||
* \ingroup memutil
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __MEM_ALLOCATOR_H__
|
||||
#define __MEM_ALLOCATOR_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include "guardedalloc/MEM_guardedalloc.h"
|
||||
|
||||
template<typename _Tp>
|
||||
struct MEM_Allocator
|
||||
{
|
||||
typedef size_t size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef _Tp* pointer;
|
||||
typedef const _Tp* const_pointer;
|
||||
typedef _Tp& reference;
|
||||
typedef const _Tp& const_reference;
|
||||
typedef _Tp value_type;
|
||||
template<typename _Tp> struct MEM_Allocator {
|
||||
typedef size_t size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef _Tp *pointer;
|
||||
typedef const _Tp *const_pointer;
|
||||
typedef _Tp &reference;
|
||||
typedef const _Tp &const_reference;
|
||||
typedef _Tp value_type;
|
||||
|
||||
template<typename _Tp1>
|
||||
struct rebind {
|
||||
typedef MEM_Allocator<_Tp1> other;
|
||||
};
|
||||
template<typename _Tp1> struct rebind {
|
||||
typedef MEM_Allocator<_Tp1> other;
|
||||
};
|
||||
|
||||
MEM_Allocator() throw() {}
|
||||
MEM_Allocator(const MEM_Allocator&) throw() {}
|
||||
MEM_Allocator() throw()
|
||||
{
|
||||
}
|
||||
MEM_Allocator(const MEM_Allocator &) throw()
|
||||
{
|
||||
}
|
||||
|
||||
template<typename _Tp1>
|
||||
MEM_Allocator(const MEM_Allocator<_Tp1>) throw() { }
|
||||
template<typename _Tp1> MEM_Allocator(const MEM_Allocator<_Tp1>) throw()
|
||||
{
|
||||
}
|
||||
|
||||
~MEM_Allocator() throw() {}
|
||||
~MEM_Allocator() throw()
|
||||
{
|
||||
}
|
||||
|
||||
pointer address(reference __x) const { return &__x; }
|
||||
pointer address(reference __x) const
|
||||
{
|
||||
return &__x;
|
||||
}
|
||||
|
||||
const_pointer address(const_reference __x) const { return &__x; }
|
||||
const_pointer address(const_reference __x) const
|
||||
{
|
||||
return &__x;
|
||||
}
|
||||
|
||||
// NB: __n is permitted to be 0. The C++ standard says nothing
|
||||
// about what the return value is when __n == 0.
|
||||
_Tp* allocate(size_type __n, const void* = 0) {
|
||||
_Tp* __ret = NULL;
|
||||
if (__n)
|
||||
__ret = static_cast<_Tp*>(
|
||||
MEM_mallocN(__n * sizeof(_Tp),
|
||||
"STL MEM_Allocator"));
|
||||
return __ret;
|
||||
}
|
||||
// NB: __n is permitted to be 0. The C++ standard says nothing
|
||||
// about what the return value is when __n == 0.
|
||||
_Tp *allocate(size_type __n, const void * = 0)
|
||||
{
|
||||
_Tp *__ret = NULL;
|
||||
if (__n)
|
||||
__ret = static_cast<_Tp *>(MEM_mallocN(__n * sizeof(_Tp), "STL MEM_Allocator"));
|
||||
return __ret;
|
||||
}
|
||||
|
||||
// __p is not permitted to be a null pointer.
|
||||
void deallocate(pointer __p, size_type) {
|
||||
MEM_freeN(__p);
|
||||
}
|
||||
// __p is not permitted to be a null pointer.
|
||||
void deallocate(pointer __p, size_type)
|
||||
{
|
||||
MEM_freeN(__p);
|
||||
}
|
||||
|
||||
size_type max_size() const throw() {
|
||||
return size_t(-1) / sizeof(_Tp);
|
||||
}
|
||||
size_type max_size() const throw()
|
||||
{
|
||||
return size_t(-1) / sizeof(_Tp);
|
||||
}
|
||||
|
||||
void construct(pointer __p, const _Tp& __val) {
|
||||
new(__p) _Tp(__val);
|
||||
}
|
||||
void construct(pointer __p, const _Tp &__val)
|
||||
{
|
||||
new (__p) _Tp(__val);
|
||||
}
|
||||
|
||||
void destroy(pointer __p) {
|
||||
__p->~_Tp();
|
||||
}
|
||||
void destroy(pointer __p)
|
||||
{
|
||||
__p->~_Tp();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __MEM_ALLOCATOR_H__
|
||||
#endif // __MEM_ALLOCATOR_H__
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
* \ingroup memutil
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __MEM_CACHELIMITER_H__
|
||||
#define __MEM_CACHELIMITER_H__
|
||||
|
||||
@@ -59,252 +58,267 @@
|
||||
#include <vector>
|
||||
#include "MEM_Allocator.h"
|
||||
|
||||
template<class T>
|
||||
class MEM_CacheLimiter;
|
||||
template<class T> class MEM_CacheLimiter;
|
||||
|
||||
#ifndef __MEM_CACHELIMITERC_API_H__
|
||||
extern "C" {
|
||||
void MEM_CacheLimiter_set_maximum(size_t m);
|
||||
size_t MEM_CacheLimiter_get_maximum();
|
||||
void MEM_CacheLimiter_set_disabled(bool disabled);
|
||||
bool MEM_CacheLimiter_is_disabled(void);
|
||||
void MEM_CacheLimiter_set_maximum(size_t m);
|
||||
size_t MEM_CacheLimiter_get_maximum();
|
||||
void MEM_CacheLimiter_set_disabled(bool disabled);
|
||||
bool MEM_CacheLimiter_is_disabled(void);
|
||||
};
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
class MEM_CacheLimiterHandle {
|
||||
public:
|
||||
explicit MEM_CacheLimiterHandle(T * data_,MEM_CacheLimiter<T> *parent_) :
|
||||
data(data_),
|
||||
refcount(0),
|
||||
parent(parent_)
|
||||
{ }
|
||||
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 ref()
|
||||
{
|
||||
refcount++;
|
||||
}
|
||||
|
||||
void unref() {
|
||||
refcount--;
|
||||
}
|
||||
void unref()
|
||||
{
|
||||
refcount--;
|
||||
}
|
||||
|
||||
T *get() {
|
||||
return data;
|
||||
}
|
||||
T *get()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
const T *get() const {
|
||||
return data;
|
||||
}
|
||||
const T *get() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
int get_refcount() const {
|
||||
return refcount;
|
||||
}
|
||||
int get_refcount() const
|
||||
{
|
||||
return refcount;
|
||||
}
|
||||
|
||||
bool can_destroy() const {
|
||||
return !data || !refcount;
|
||||
}
|
||||
bool can_destroy() const
|
||||
{
|
||||
return !data || !refcount;
|
||||
}
|
||||
|
||||
bool destroy_if_possible() {
|
||||
if (can_destroy()) {
|
||||
delete data;
|
||||
data = NULL;
|
||||
unmanage();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool destroy_if_possible()
|
||||
{
|
||||
if (can_destroy()) {
|
||||
delete data;
|
||||
data = NULL;
|
||||
unmanage();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void unmanage() {
|
||||
parent->unmanage(this);
|
||||
}
|
||||
void unmanage()
|
||||
{
|
||||
parent->unmanage(this);
|
||||
}
|
||||
|
||||
void touch() {
|
||||
parent->touch(this);
|
||||
}
|
||||
void touch()
|
||||
{
|
||||
parent->touch(this);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class MEM_CacheLimiter<T>;
|
||||
private:
|
||||
friend class MEM_CacheLimiter<T>;
|
||||
|
||||
T * data;
|
||||
int refcount;
|
||||
int pos;
|
||||
MEM_CacheLimiter<T> * parent;
|
||||
T *data;
|
||||
int refcount;
|
||||
int pos;
|
||||
MEM_CacheLimiter<T> *parent;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
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);
|
||||
template<class T> 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) {
|
||||
}
|
||||
MEM_CacheLimiter(MEM_CacheLimiter_DataSize_Func data_size_func) : data_size_func(data_size_func)
|
||||
{
|
||||
}
|
||||
|
||||
~MEM_CacheLimiter() {
|
||||
int i;
|
||||
for (i = 0; i < queue.size(); i++) {
|
||||
delete queue[i];
|
||||
}
|
||||
}
|
||||
~MEM_CacheLimiter()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < queue.size(); i++) {
|
||||
delete queue[i];
|
||||
}
|
||||
}
|
||||
|
||||
MEM_CacheLimiterHandle<T> *insert(T * elem) {
|
||||
queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
|
||||
queue.back()->pos = queue.size() - 1;
|
||||
return queue.back();
|
||||
}
|
||||
MEM_CacheLimiterHandle<T> *insert(T *elem)
|
||||
{
|
||||
queue.push_back(new MEM_CacheLimiterHandle<T>(elem, this));
|
||||
queue.back()->pos = queue.size() - 1;
|
||||
return queue.back();
|
||||
}
|
||||
|
||||
void unmanage(MEM_CacheLimiterHandle<T> *handle) {
|
||||
int pos = handle->pos;
|
||||
queue[pos] = queue.back();
|
||||
queue[pos]->pos = pos;
|
||||
queue.pop_back();
|
||||
delete handle;
|
||||
}
|
||||
void unmanage(MEM_CacheLimiterHandle<T> *handle)
|
||||
{
|
||||
int pos = handle->pos;
|
||||
queue[pos] = queue.back();
|
||||
queue[pos]->pos = pos;
|
||||
queue.pop_back();
|
||||
delete handle;
|
||||
}
|
||||
|
||||
size_t get_memory_in_use() {
|
||||
size_t size = 0;
|
||||
if (data_size_func) {
|
||||
int i;
|
||||
for (i = 0; i < queue.size(); i++) {
|
||||
size += data_size_func(queue[i]->get()->get_data());
|
||||
}
|
||||
}
|
||||
else {
|
||||
size = MEM_get_memory_in_use();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
size_t get_memory_in_use()
|
||||
{
|
||||
size_t size = 0;
|
||||
if (data_size_func) {
|
||||
int i;
|
||||
for (i = 0; i < queue.size(); i++) {
|
||||
size += data_size_func(queue[i]->get()->get_data());
|
||||
}
|
||||
}
|
||||
else {
|
||||
size = MEM_get_memory_in_use();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void enforce_limits() {
|
||||
size_t max = MEM_CacheLimiter_get_maximum();
|
||||
bool is_disabled = MEM_CacheLimiter_is_disabled();
|
||||
size_t mem_in_use, cur_size;
|
||||
void enforce_limits()
|
||||
{
|
||||
size_t max = MEM_CacheLimiter_get_maximum();
|
||||
bool is_disabled = MEM_CacheLimiter_is_disabled();
|
||||
size_t mem_in_use, cur_size;
|
||||
|
||||
if (is_disabled) {
|
||||
return;
|
||||
}
|
||||
if (is_disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (max == 0) {
|
||||
return;
|
||||
}
|
||||
if (max == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
mem_in_use = get_memory_in_use();
|
||||
mem_in_use = get_memory_in_use();
|
||||
|
||||
if (mem_in_use <= max) {
|
||||
return;
|
||||
}
|
||||
if (mem_in_use <= max) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (!queue.empty() && mem_in_use > max) {
|
||||
MEM_CacheElementPtr elem = get_least_priority_destroyable_element();
|
||||
while (!queue.empty() && mem_in_use > max) {
|
||||
MEM_CacheElementPtr elem = get_least_priority_destroyable_element();
|
||||
|
||||
if (!elem)
|
||||
break;
|
||||
if (!elem)
|
||||
break;
|
||||
|
||||
if (data_size_func) {
|
||||
cur_size = data_size_func(elem->get()->get_data());
|
||||
}
|
||||
else {
|
||||
cur_size = mem_in_use;
|
||||
}
|
||||
if (data_size_func) {
|
||||
cur_size = data_size_func(elem->get()->get_data());
|
||||
}
|
||||
else {
|
||||
cur_size = mem_in_use;
|
||||
}
|
||||
|
||||
if (elem->destroy_if_possible()) {
|
||||
if (data_size_func) {
|
||||
mem_in_use -= cur_size;
|
||||
}
|
||||
else {
|
||||
mem_in_use -= cur_size - MEM_get_memory_in_use();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (elem->destroy_if_possible()) {
|
||||
if (data_size_func) {
|
||||
mem_in_use -= cur_size;
|
||||
}
|
||||
else {
|
||||
mem_in_use -= cur_size - MEM_get_memory_in_use();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void touch(MEM_CacheLimiterHandle<T> * handle) {
|
||||
/* 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) {
|
||||
queue[handle->pos] = queue.back();
|
||||
queue[handle->pos]->pos = handle->pos;
|
||||
queue.pop_back();
|
||||
queue.push_back(handle);
|
||||
handle->pos = queue.size() - 1;
|
||||
}
|
||||
}
|
||||
void touch(MEM_CacheLimiterHandle<T> *handle)
|
||||
{
|
||||
/* 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) {
|
||||
queue[handle->pos] = queue.back();
|
||||
queue[handle->pos]->pos = handle->pos;
|
||||
queue.pop_back();
|
||||
queue.push_back(handle);
|
||||
handle->pos = queue.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
void set_item_priority_func(MEM_CacheLimiter_ItemPriority_Func item_priority_func) {
|
||||
this->item_priority_func = item_priority_func;
|
||||
}
|
||||
void set_item_priority_func(MEM_CacheLimiter_ItemPriority_Func item_priority_func)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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::vector<MEM_CacheElementPtr, MEM_Allocator<MEM_CacheElementPtr> > MEM_CacheQueue;
|
||||
typedef typename MEM_CacheQueue::iterator iterator;
|
||||
private:
|
||||
typedef MEM_CacheLimiterHandle<T> *MEM_CacheElementPtr;
|
||||
typedef std::vector<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;
|
||||
}
|
||||
/* 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;
|
||||
MEM_CacheElementPtr get_least_priority_destroyable_element(void)
|
||||
{
|
||||
if (queue.empty())
|
||||
return NULL;
|
||||
|
||||
MEM_CacheElementPtr best_match_elem = NULL;
|
||||
MEM_CacheElementPtr best_match_elem = NULL;
|
||||
|
||||
if (!item_priority_func) {
|
||||
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
||||
MEM_CacheElementPtr elem = *it;
|
||||
if (!can_destroy_element(elem))
|
||||
continue;
|
||||
best_match_elem = elem;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int best_match_priority = 0;
|
||||
int i;
|
||||
if (!item_priority_func) {
|
||||
for (iterator it = queue.begin(); it != queue.end(); it++) {
|
||||
MEM_CacheElementPtr elem = *it;
|
||||
if (!can_destroy_element(elem))
|
||||
continue;
|
||||
best_match_elem = elem;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int best_match_priority = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < queue.size(); i++) {
|
||||
MEM_CacheElementPtr elem = queue[i];
|
||||
for (i = 0; i < queue.size(); i++) {
|
||||
MEM_CacheElementPtr elem = queue[i];
|
||||
|
||||
if (!can_destroy_element(elem))
|
||||
continue;
|
||||
if (!can_destroy_element(elem))
|
||||
continue;
|
||||
|
||||
/* by default 0 means highest priority element */
|
||||
/* casting a size type to int is questionable,
|
||||
but unlikely to cause problems */
|
||||
int priority = -((int)(queue.size()) - i - 1);
|
||||
priority = item_priority_func(elem->get()->get_data(), priority);
|
||||
/* by default 0 means highest priority element */
|
||||
/* casting a size type to int is questionable,
|
||||
but unlikely to cause problems */
|
||||
int priority = -((int)(queue.size()) - i - 1);
|
||||
priority = item_priority_func(elem->get()->get_data(), priority);
|
||||
|
||||
if (priority < best_match_priority || best_match_elem == NULL) {
|
||||
best_match_priority = priority;
|
||||
best_match_elem = elem;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (priority < best_match_priority || best_match_elem == NULL) {
|
||||
best_match_priority = priority;
|
||||
best_match_elem = elem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best_match_elem;
|
||||
}
|
||||
return best_match_elem;
|
||||
}
|
||||
|
||||
MEM_CacheQueue queue;
|
||||
MEM_CacheLimiter_DataSize_Func data_size_func;
|
||||
MEM_CacheLimiter_ItemPriority_Func item_priority_func;
|
||||
MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func;
|
||||
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__
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
* \ingroup memutil
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __MEM_CACHELIMITERC_API_H__
|
||||
#define __MEM_CACHELIMITERC_API_H__
|
||||
|
||||
@@ -33,16 +32,16 @@ typedef struct MEM_CacheLimiter_s MEM_CacheLimiterC;
|
||||
typedef struct MEM_CacheLimiterHandle_s MEM_CacheLimiterHandleC;
|
||||
|
||||
/* function used to remove data from memory */
|
||||
typedef void (*MEM_CacheLimiter_Destruct_Func)(void*);
|
||||
typedef void (*MEM_CacheLimiter_Destruct_Func)(void *);
|
||||
|
||||
/* function used to measure stored data element size */
|
||||
typedef size_t (*MEM_CacheLimiter_DataSize_Func) (void*);
|
||||
typedef size_t (*MEM_CacheLimiter_DataSize_Func)(void *);
|
||||
|
||||
/* function used to measure priority of item when freeing memory */
|
||||
typedef int (*MEM_CacheLimiter_ItemPriority_Func) (void*, int);
|
||||
typedef int (*MEM_CacheLimiter_ItemPriority_Func)(void *, int);
|
||||
|
||||
/* function to check whether item could be destroyed */
|
||||
typedef bool (*MEM_CacheLimiter_ItemDestroyable_Func) (void*);
|
||||
typedef bool (*MEM_CacheLimiter_ItemDestroyable_Func)(void *);
|
||||
|
||||
#ifndef __MEM_CACHELIMITER_H__
|
||||
void MEM_CacheLimiter_set_maximum(size_t m);
|
||||
@@ -60,7 +59,7 @@ bool MEM_CacheLimiter_is_disabled(void);
|
||||
*/
|
||||
|
||||
MEM_CacheLimiterC *new_MEM_CacheLimiter(MEM_CacheLimiter_Destruct_Func data_destructor,
|
||||
MEM_CacheLimiter_DataSize_Func data_size);
|
||||
MEM_CacheLimiter_DataSize_Func data_size);
|
||||
|
||||
/**
|
||||
* Delete MEM_CacheLimiter
|
||||
@@ -98,7 +97,6 @@ void MEM_CacheLimiter_enforce_limits(MEM_CacheLimiterC *This);
|
||||
|
||||
void MEM_CacheLimiter_unmanage(MEM_CacheLimiterHandleC *handle);
|
||||
|
||||
|
||||
/**
|
||||
* Raise priority of object (put it at the tail of the deletion chain)
|
||||
*
|
||||
@@ -144,8 +142,8 @@ void *MEM_CacheLimiter_get(MEM_CacheLimiterHandleC *handle);
|
||||
void MEM_CacheLimiter_ItemPriority_Func_set(MEM_CacheLimiterC *This,
|
||||
MEM_CacheLimiter_ItemPriority_Func item_priority_func);
|
||||
|
||||
void MEM_CacheLimiter_ItemDestroyable_Func_set(MEM_CacheLimiterC *This,
|
||||
MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func);
|
||||
void MEM_CacheLimiter_ItemDestroyable_Func_set(
|
||||
MEM_CacheLimiterC *This, MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func);
|
||||
|
||||
size_t MEM_CacheLimiter_get_memory_in_use(MEM_CacheLimiterC *This);
|
||||
|
||||
@@ -153,5 +151,4 @@ size_t MEM_CacheLimiter_get_memory_in_use(MEM_CacheLimiterC *This);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif // __MEM_CACHELIMITERC_API_H__
|
||||
#endif // __MEM_CACHELIMITERC_API_H__
|
||||
|
||||
@@ -34,71 +34,70 @@
|
||||
* The default destructor of this object has been made protected on purpose.
|
||||
* This disables the creation of shared objects on the stack.
|
||||
*
|
||||
* @author Maarten Gribnau
|
||||
* @date March 31, 2001
|
||||
* @author Maarten Gribnau
|
||||
* @date March 31, 2001
|
||||
*/
|
||||
|
||||
class MEM_RefCounted {
|
||||
public:
|
||||
/**
|
||||
* Constructs a a shared object.
|
||||
*/
|
||||
MEM_RefCounted() : m_refCount(1)
|
||||
{
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* Constructs a a shared object.
|
||||
*/
|
||||
MEM_RefCounted() : m_refCount(1)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reference count of this object.
|
||||
* @return the reference count.
|
||||
*/
|
||||
inline virtual int getRef() const;
|
||||
/**
|
||||
* Returns the reference count of this object.
|
||||
* @return the reference count.
|
||||
*/
|
||||
inline virtual int getRef() const;
|
||||
|
||||
/**
|
||||
* Increases the reference count of this object.
|
||||
* @return the new reference count.
|
||||
*/
|
||||
inline virtual int incRef();
|
||||
/**
|
||||
* Increases the reference count of this object.
|
||||
* @return the new reference count.
|
||||
*/
|
||||
inline virtual int incRef();
|
||||
|
||||
/**
|
||||
* Decreases the reference count of this object.
|
||||
* If the reference count reaches zero, the object self-destructs.
|
||||
* @return the new reference count.
|
||||
*/
|
||||
inline virtual int decRef();
|
||||
/**
|
||||
* Decreases the reference count of this object.
|
||||
* If the reference count reaches zero, the object self-destructs.
|
||||
* @return the new reference count.
|
||||
*/
|
||||
inline virtual int decRef();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Destructs a shared object.
|
||||
* The destructor is protected to force the use of incRef and decRef.
|
||||
*/
|
||||
virtual ~MEM_RefCounted()
|
||||
{
|
||||
}
|
||||
protected:
|
||||
/**
|
||||
* Destructs a shared object.
|
||||
* The destructor is protected to force the use of incRef and decRef.
|
||||
*/
|
||||
virtual ~MEM_RefCounted()
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
/// The reference count.
|
||||
int m_refCount;
|
||||
protected:
|
||||
/// The reference count.
|
||||
int m_refCount;
|
||||
};
|
||||
|
||||
|
||||
inline int MEM_RefCounted::getRef() const
|
||||
{
|
||||
return m_refCount;
|
||||
return m_refCount;
|
||||
}
|
||||
|
||||
inline int MEM_RefCounted::incRef()
|
||||
{
|
||||
return ++m_refCount;
|
||||
return ++m_refCount;
|
||||
}
|
||||
|
||||
inline int MEM_RefCounted::decRef()
|
||||
{
|
||||
m_refCount--;
|
||||
if (m_refCount == 0) {
|
||||
delete this;
|
||||
return 0;
|
||||
}
|
||||
return m_refCount;
|
||||
m_refCount--;
|
||||
if (m_refCount == 0) {
|
||||
delete this;
|
||||
return 0;
|
||||
}
|
||||
return m_refCount;
|
||||
}
|
||||
|
||||
#endif // __MEM_REFCOUNTED_H__
|
||||
#endif // __MEM_REFCOUNTED_H__
|
||||
|
||||
@@ -27,29 +27,27 @@
|
||||
#define __MEM_REFCOUNTEDC_API_H__
|
||||
|
||||
/** A pointer to a private object. */
|
||||
typedef struct MEM_TOpaqueObject* MEM_TObjectPtr;
|
||||
typedef struct MEM_TOpaqueObject *MEM_TObjectPtr;
|
||||
/** A pointer to a shared object. */
|
||||
typedef MEM_TObjectPtr MEM_TRefCountedObjectPtr;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Returns the reference count of this object.
|
||||
* @param shared The object to query.
|
||||
* @return The current reference count.
|
||||
*/
|
||||
extern int MEM_RefCountedGetRef(MEM_TRefCountedObjectPtr shared);
|
||||
extern int MEM_RefCountedGetRef(MEM_TRefCountedObjectPtr shared);
|
||||
|
||||
/**
|
||||
* Increases the reference count of this object.
|
||||
* @param shared The object to query.
|
||||
* @return The new reference count.
|
||||
*/
|
||||
extern int MEM_RefCountedIncRef(MEM_TRefCountedObjectPtr shared);
|
||||
extern int MEM_RefCountedIncRef(MEM_TRefCountedObjectPtr shared);
|
||||
|
||||
/**
|
||||
* Decreases the reference count of this object.
|
||||
@@ -57,11 +55,10 @@ extern int MEM_RefCountedIncRef(MEM_TRefCountedObjectPtr shared);
|
||||
* @param shared The object to query.
|
||||
* @return The new reference count.
|
||||
*/
|
||||
extern int MEM_RefCountedDecRef(MEM_TRefCountedObjectPtr shared);
|
||||
|
||||
extern int MEM_RefCountedDecRef(MEM_TRefCountedObjectPtr shared);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // __MEM_REFCOUNTEDC_API_H__
|
||||
#endif // __MEM_REFCOUNTEDC_API_H__
|
||||
|
||||
@@ -25,30 +25,30 @@
|
||||
|
||||
static bool is_disabled = false;
|
||||
|
||||
static size_t & get_max()
|
||||
static size_t &get_max()
|
||||
{
|
||||
static size_t m = 32 * 1024 * 1024;
|
||||
return m;
|
||||
static size_t m = 32 * 1024 * 1024;
|
||||
return m;
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_set_maximum(size_t m)
|
||||
{
|
||||
get_max() = m;
|
||||
get_max() = m;
|
||||
}
|
||||
|
||||
size_t MEM_CacheLimiter_get_maximum()
|
||||
{
|
||||
return get_max();
|
||||
return get_max();
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_set_disabled(bool disabled)
|
||||
{
|
||||
is_disabled = disabled;
|
||||
is_disabled = disabled;
|
||||
}
|
||||
|
||||
bool MEM_CacheLimiter_is_disabled(void)
|
||||
{
|
||||
return is_disabled;
|
||||
return is_disabled;
|
||||
}
|
||||
|
||||
class MEM_CacheLimiterHandleCClass;
|
||||
@@ -56,166 +56,174 @@ class MEM_CacheLimiterCClass;
|
||||
|
||||
typedef MEM_CacheLimiterHandle<MEM_CacheLimiterHandleCClass> handle_t;
|
||||
typedef MEM_CacheLimiter<MEM_CacheLimiterHandleCClass> cache_t;
|
||||
typedef std::list<MEM_CacheLimiterHandleCClass*, MEM_Allocator<MEM_CacheLimiterHandleCClass* > > list_t;
|
||||
typedef std::list<MEM_CacheLimiterHandleCClass *, MEM_Allocator<MEM_CacheLimiterHandleCClass *>>
|
||||
list_t;
|
||||
|
||||
class MEM_CacheLimiterCClass {
|
||||
public:
|
||||
MEM_CacheLimiterCClass(MEM_CacheLimiter_Destruct_Func data_destructor_, MEM_CacheLimiter_DataSize_Func data_size)
|
||||
: data_destructor(data_destructor_), cache(data_size) {
|
||||
}
|
||||
~MEM_CacheLimiterCClass();
|
||||
public:
|
||||
MEM_CacheLimiterCClass(MEM_CacheLimiter_Destruct_Func data_destructor_,
|
||||
MEM_CacheLimiter_DataSize_Func data_size)
|
||||
: data_destructor(data_destructor_), cache(data_size)
|
||||
{
|
||||
}
|
||||
~MEM_CacheLimiterCClass();
|
||||
|
||||
handle_t * insert(void *data);
|
||||
handle_t *insert(void *data);
|
||||
|
||||
void destruct(void *data, list_t::iterator it);
|
||||
void destruct(void *data, list_t::iterator it);
|
||||
|
||||
cache_t * get_cache() {
|
||||
return &cache;
|
||||
}
|
||||
private:
|
||||
MEM_CacheLimiter_Destruct_Func data_destructor;
|
||||
cache_t *get_cache()
|
||||
{
|
||||
return &cache;
|
||||
}
|
||||
|
||||
MEM_CacheLimiter<MEM_CacheLimiterHandleCClass> cache;
|
||||
private:
|
||||
MEM_CacheLimiter_Destruct_Func data_destructor;
|
||||
|
||||
list_t cclass_list;
|
||||
MEM_CacheLimiter<MEM_CacheLimiterHandleCClass> cache;
|
||||
|
||||
list_t cclass_list;
|
||||
};
|
||||
|
||||
class MEM_CacheLimiterHandleCClass {
|
||||
public:
|
||||
MEM_CacheLimiterHandleCClass(void *data_, MEM_CacheLimiterCClass *parent_) :
|
||||
data(data_),
|
||||
parent(parent_)
|
||||
{ }
|
||||
public:
|
||||
MEM_CacheLimiterHandleCClass(void *data_, MEM_CacheLimiterCClass *parent_)
|
||||
: data(data_), parent(parent_)
|
||||
{
|
||||
}
|
||||
|
||||
~MEM_CacheLimiterHandleCClass();
|
||||
~MEM_CacheLimiterHandleCClass();
|
||||
|
||||
void set_iter(list_t::iterator it_) {
|
||||
it = it_;
|
||||
}
|
||||
void set_iter(list_t::iterator it_)
|
||||
{
|
||||
it = it_;
|
||||
}
|
||||
|
||||
void set_data(void *data_) {
|
||||
data = data_;
|
||||
}
|
||||
void set_data(void *data_)
|
||||
{
|
||||
data = data_;
|
||||
}
|
||||
|
||||
void *get_data() const {
|
||||
return data;
|
||||
}
|
||||
void *get_data() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
private:
|
||||
void *data;
|
||||
MEM_CacheLimiterCClass *parent;
|
||||
list_t::iterator it;
|
||||
private:
|
||||
void *data;
|
||||
MEM_CacheLimiterCClass *parent;
|
||||
list_t::iterator it;
|
||||
};
|
||||
|
||||
handle_t *MEM_CacheLimiterCClass::insert(void *data)
|
||||
{
|
||||
cclass_list.push_back(new MEM_CacheLimiterHandleCClass(data, this));
|
||||
list_t::iterator it = cclass_list.end();
|
||||
--it;
|
||||
cclass_list.back()->set_iter(it);
|
||||
cclass_list.push_back(new MEM_CacheLimiterHandleCClass(data, this));
|
||||
list_t::iterator it = cclass_list.end();
|
||||
--it;
|
||||
cclass_list.back()->set_iter(it);
|
||||
|
||||
return cache.insert(cclass_list.back());
|
||||
return cache.insert(cclass_list.back());
|
||||
}
|
||||
|
||||
void MEM_CacheLimiterCClass::destruct(void *data, list_t::iterator it)
|
||||
{
|
||||
data_destructor(data);
|
||||
cclass_list.erase(it);
|
||||
data_destructor(data);
|
||||
cclass_list.erase(it);
|
||||
}
|
||||
|
||||
MEM_CacheLimiterHandleCClass::~MEM_CacheLimiterHandleCClass()
|
||||
{
|
||||
if (data) {
|
||||
parent->destruct(data, it);
|
||||
}
|
||||
if (data) {
|
||||
parent->destruct(data, it);
|
||||
}
|
||||
}
|
||||
|
||||
MEM_CacheLimiterCClass::~MEM_CacheLimiterCClass()
|
||||
{
|
||||
// should not happen, but don't leak memory in this case...
|
||||
for (list_t::iterator it = cclass_list.begin(); it != cclass_list.end(); it++) {
|
||||
(*it)->set_data(NULL);
|
||||
// should not happen, but don't leak memory in this case...
|
||||
for (list_t::iterator it = cclass_list.begin(); it != cclass_list.end(); it++) {
|
||||
(*it)->set_data(NULL);
|
||||
|
||||
delete *it;
|
||||
}
|
||||
delete *it;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
static inline MEM_CacheLimiterCClass *cast(MEM_CacheLimiterC *l)
|
||||
{
|
||||
return (MEM_CacheLimiterCClass *) l;
|
||||
return (MEM_CacheLimiterCClass *)l;
|
||||
}
|
||||
|
||||
static inline handle_t *cast(MEM_CacheLimiterHandleC *l)
|
||||
{
|
||||
return (handle_t *) l;
|
||||
return (handle_t *)l;
|
||||
}
|
||||
|
||||
MEM_CacheLimiterC *new_MEM_CacheLimiter(MEM_CacheLimiter_Destruct_Func data_destructor,
|
||||
MEM_CacheLimiter_DataSize_Func data_size)
|
||||
{
|
||||
return (MEM_CacheLimiterC *) new MEM_CacheLimiterCClass(data_destructor, data_size);
|
||||
return (MEM_CacheLimiterC *)new MEM_CacheLimiterCClass(data_destructor, data_size);
|
||||
}
|
||||
|
||||
void delete_MEM_CacheLimiter(MEM_CacheLimiterC *This)
|
||||
{
|
||||
delete cast(This);
|
||||
delete cast(This);
|
||||
}
|
||||
|
||||
MEM_CacheLimiterHandleC *MEM_CacheLimiter_insert(MEM_CacheLimiterC *This, void *data)
|
||||
{
|
||||
return (MEM_CacheLimiterHandleC *) cast(This)->insert(data);
|
||||
return (MEM_CacheLimiterHandleC *)cast(This)->insert(data);
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_enforce_limits(MEM_CacheLimiterC *This)
|
||||
{
|
||||
cast(This)->get_cache()->enforce_limits();
|
||||
cast(This)->get_cache()->enforce_limits();
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_unmanage(MEM_CacheLimiterHandleC *handle)
|
||||
{
|
||||
cast(handle)->unmanage();
|
||||
cast(handle)->unmanage();
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_touch(MEM_CacheLimiterHandleC *handle)
|
||||
{
|
||||
cast(handle)->touch();
|
||||
cast(handle)->touch();
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_ref(MEM_CacheLimiterHandleC *handle)
|
||||
{
|
||||
cast(handle)->ref();
|
||||
cast(handle)->ref();
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_unref(MEM_CacheLimiterHandleC *handle)
|
||||
{
|
||||
cast(handle)->unref();
|
||||
cast(handle)->unref();
|
||||
}
|
||||
|
||||
int MEM_CacheLimiter_get_refcount(MEM_CacheLimiterHandleC *handle)
|
||||
{
|
||||
return cast(handle)->get_refcount();
|
||||
return cast(handle)->get_refcount();
|
||||
}
|
||||
|
||||
void *MEM_CacheLimiter_get(MEM_CacheLimiterHandleC *handle)
|
||||
{
|
||||
return cast(handle)->get()->get_data();
|
||||
return cast(handle)->get()->get_data();
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_ItemPriority_Func_set(MEM_CacheLimiterC *This,
|
||||
MEM_CacheLimiter_ItemPriority_Func item_priority_func)
|
||||
{
|
||||
cast(This)->get_cache()->set_item_priority_func(item_priority_func);
|
||||
cast(This)->get_cache()->set_item_priority_func(item_priority_func);
|
||||
}
|
||||
|
||||
void MEM_CacheLimiter_ItemDestroyable_Func_set(MEM_CacheLimiterC *This,
|
||||
MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func)
|
||||
void MEM_CacheLimiter_ItemDestroyable_Func_set(
|
||||
MEM_CacheLimiterC *This, MEM_CacheLimiter_ItemDestroyable_Func item_destroyable_func)
|
||||
{
|
||||
cast(This)->get_cache()->set_item_destroyable_func(item_destroyable_func);
|
||||
cast(This)->get_cache()->set_item_destroyable_func(item_destroyable_func);
|
||||
}
|
||||
|
||||
size_t MEM_CacheLimiter_get_memory_in_use(MEM_CacheLimiterC *This)
|
||||
{
|
||||
return cast(This)->get_cache()->get_memory_in_use();
|
||||
return cast(This)->get_cache()->get_memory_in_use();
|
||||
}
|
||||
|
||||
@@ -21,25 +21,20 @@
|
||||
* \ingroup memutil
|
||||
*/
|
||||
|
||||
|
||||
#include "MEM_RefCountedC-Api.h"
|
||||
#include "MEM_RefCounted.h"
|
||||
|
||||
|
||||
|
||||
int MEM_RefCountedGetRef(MEM_TRefCountedObjectPtr shared)
|
||||
{
|
||||
return shared ? ((MEM_RefCounted*)shared)->getRef() : 0;
|
||||
return shared ? ((MEM_RefCounted *)shared)->getRef() : 0;
|
||||
}
|
||||
|
||||
|
||||
int MEM_RefCountedIncRef(MEM_TRefCountedObjectPtr shared)
|
||||
{
|
||||
return shared ? ((MEM_RefCounted*)shared)->incRef() : 0;
|
||||
return shared ? ((MEM_RefCounted *)shared)->incRef() : 0;
|
||||
}
|
||||
|
||||
|
||||
int MEM_RefCountedDecRef(MEM_TRefCountedObjectPtr shared)
|
||||
{
|
||||
return shared ? ((MEM_RefCounted*)shared)->decRef() : 0;
|
||||
return shared ? ((MEM_RefCounted *)shared)->decRef() : 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user