Memory pool vectors (DVector) have been enormously simplified in code, and renamed to PoolVector

This commit is contained in:
Juan Linietsky
2017-01-07 18:25:37 -03:00
parent 2a38a5eaa8
commit 2ab83e1abb
257 changed files with 2818 additions and 3130 deletions
-83
View File
@@ -33,61 +33,6 @@
#include <stdlib.h>
MID::MID(MemoryPoolDynamic::ID p_id) {
data = (Data*)memalloc(sizeof(Data));
data->refcount.init();
data->id=p_id;
}
void MID::unref() {
if (!data)
return;
if (data->refcount.unref()) {
if (data->id!=MemoryPoolDynamic::INVALID_ID)
MemoryPoolDynamic::get_singleton()->free(data->id);
memfree(data);
}
data=NULL;
}
Error MID::_resize(size_t p_size) {
if (p_size==0 && (!data || data->id==MemoryPoolDynamic::INVALID_ID))
return OK;
if (p_size && !data) {
// create data because we'll need it
data = (Data*)memalloc(sizeof(Data));
ERR_FAIL_COND_V( !data,ERR_OUT_OF_MEMORY );
data->refcount.init();
data->id=MemoryPoolDynamic::INVALID_ID;
}
if (p_size==0 && data && data->id==MemoryPoolDynamic::INVALID_ID) {
MemoryPoolDynamic::get_singleton()->free(data->id);
data->id=MemoryPoolDynamic::INVALID_ID;
}
if (p_size>0) {
if (data->id==MemoryPoolDynamic::INVALID_ID) {
data->id=MemoryPoolDynamic::get_singleton()->alloc(p_size,"Unnamed MID");
ERR_FAIL_COND_V( data->id==MemoryPoolDynamic::INVALID_ID, ERR_OUT_OF_MEMORY );
} else {
MemoryPoolDynamic::get_singleton()->realloc(data->id,p_size);
ERR_FAIL_COND_V( data->id==MemoryPoolDynamic::INVALID_ID, ERR_OUT_OF_MEMORY );
}
}
return OK;
}
void * operator new(size_t p_size,const char *p_description) {
@@ -242,34 +187,6 @@ size_t Memory::get_mem_max_usage(){
}
MID Memory::alloc_dynamic(size_t p_bytes, const char *p_descr) {
MemoryPoolDynamic::ID id = MemoryPoolDynamic::get_singleton()->alloc(p_bytes,p_descr);
return MID(id);
}
Error Memory::realloc_dynamic(MID p_mid,size_t p_bytes) {
MemoryPoolDynamic::ID id = p_mid.data?p_mid.data->id:MemoryPoolDynamic::INVALID_ID;
if (id==MemoryPoolDynamic::INVALID_ID)
return ERR_INVALID_PARAMETER;
return MemoryPoolDynamic::get_singleton()->realloc(p_mid, p_bytes);
}
size_t Memory::get_dynamic_mem_available() {
return MemoryPoolDynamic::get_singleton()->get_available_mem();
}
size_t Memory::get_dynamic_mem_usage() {
return MemoryPoolDynamic::get_singleton()->get_total_usage();
}
_GlobalNil::_GlobalNil() {
+1 -103
View File
@@ -31,8 +31,6 @@
#include <stddef.h>
#include "safe_refcount.h"
#include "os/memory_pool_dynamic.h"
/**
@@ -44,88 +42,6 @@
#endif
class MID {
struct Data {
SafeRefCount refcount;
MemoryPoolDynamic::ID id;
};
mutable Data *data;
void ref(Data *p_data) {
if (data==p_data)
return;
unref();
if (p_data && p_data->refcount.ref())
data=p_data;
}
friend class MID_Lock;
inline void lock() {
if (data && data->id!=MemoryPoolDynamic::INVALID_ID)
MemoryPoolDynamic::get_singleton()->lock(data->id);
}
inline void unlock() {
if (data && data->id!=MemoryPoolDynamic::INVALID_ID)
MemoryPoolDynamic::get_singleton()->unlock(data->id);
}
inline void * get() {
if (data && data->id!=MemoryPoolDynamic::INVALID_ID)
return MemoryPoolDynamic::get_singleton()->get(data->id);
return NULL;
}
void unref();
Error _resize(size_t p_size);
friend class Memory;
MID(MemoryPoolDynamic::ID p_id);
public:
bool is_valid() const { return data; }
operator bool() const { return data; }
size_t get_size() const { return (data && data->id!=MemoryPoolDynamic::INVALID_ID) ? MemoryPoolDynamic::get_singleton()->get_size(data->id) : 0; }
Error resize(size_t p_size) { return _resize(p_size); }
inline void operator=(const MID& p_mid) { ref( p_mid.data ); }
inline bool is_locked() const { return (data && data->id!=MemoryPoolDynamic::INVALID_ID) ? MemoryPoolDynamic::get_singleton()->is_locked(data->id) : false; }
inline MID(const MID& p_mid) { data=NULL; ref( p_mid.data ); }
inline MID() { data = NULL; }
~MID() { unref(); }
};
class MID_Lock {
MID mid;
public:
void *data() { return mid.get(); }
void operator=(const MID_Lock& p_lock ) { mid.unlock(); mid = p_lock.mid; mid.lock(); }
inline MID_Lock(const MID& p_mid) { mid=p_mid; mid.lock(); }
inline MID_Lock(const MID_Lock& p_lock) { mid=p_lock.mid; mid.lock(); }
MID_Lock() {}
~MID_Lock() { mid.unlock(); }
};
class Memory{
@@ -148,12 +64,6 @@ public:
static size_t get_mem_max_usage();
static MID alloc_dynamic(size_t p_bytes, const char *p_descr="");
static Error realloc_dynamic(MID p_mid,size_t p_bytes);
static size_t get_dynamic_mem_available();
static size_t get_dynamic_mem_usage();
};
class DefaultAllocator {
@@ -174,18 +84,6 @@ void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_de
#define memfree(m_size) Memory::free_static(m_size)
#ifdef DEBUG_MEMORY_ENABLED
#define dynalloc(m_size) Memory::alloc_dynamic(m_size, __FILE__ ":" __STR(__LINE__) ", type: DYNAMIC")
#define dynrealloc(m_mem,m_size) m_mem.resize(m_size)
#else
#define dynalloc(m_size) Memory::alloc_dynamic(m_size)
#define dynrealloc(m_mem,m_size) m_mem.resize(m_size)
#endif
_ALWAYS_INLINE_ void postinitialize_handler(void *) {}
@@ -241,7 +139,7 @@ T* memnew_arr_template(size_t p_elements,const char *p_descr="") {
if (p_elements==0)
return 0;
/** overloading operator new[] cannot be done , because it may not return the real allocated address (it may pad the 'element count' before the actual array). Because of that, it must be done by hand. This is the
same strategy used by std::vector, and the DVector class, so it should be safe.*/
same strategy used by std::vector, and the PoolVector class, so it should be safe.*/
size_t len = sizeof(T) * p_elements;
uint64_t *mem = (uint64_t*)Memory::alloc_static( len , true );
-50
View File
@@ -1,50 +0,0 @@
/*************************************************************************/
/* memory_pool_dynamic.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "memory_pool_dynamic.h"
MemoryPoolDynamic* MemoryPoolDynamic::singleton=NULL;
MemoryPoolDynamic* MemoryPoolDynamic::get_singleton() {
return singleton;
}
MemoryPoolDynamic::MemoryPoolDynamic() {
ERR_FAIL_COND(singleton!=NULL);
singleton=this;
}
MemoryPoolDynamic::~MemoryPoolDynamic() {
singleton=NULL;
}
-79
View File
@@ -1,79 +0,0 @@
/*************************************************************************/
/* memory_pool_dynamic.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef MEMORY_POOL_DYNAMIC_H
#define MEMORY_POOL_DYNAMIC_H
#include "typedefs.h"
class MemoryPoolDynamic {
static MemoryPoolDynamic* singleton;
protected:
friend class Memory;
friend class MID;
enum {
INVALID_ID=0xFFFFFFFF
};
static MemoryPoolDynamic* get_singleton();
typedef uint64_t ID;
virtual ID alloc(size_t p_amount,const char* p_description)=0;
virtual void free(ID p_id)=0;
virtual Error realloc(ID p_id, size_t p_amount)=0;
virtual bool is_valid(ID p_id)=0;
virtual size_t get_size(ID p_id) const=0;
virtual const char* get_description(ID p_id) const=0;
virtual Error lock(ID p_id)=0;
virtual void * get(ID p_ID)=0;
virtual Error unlock(ID p_id)=0;
virtual bool is_locked(ID p_id) const=0;
virtual size_t get_available_mem() const=0;
virtual size_t get_total_usage() const=0;
MemoryPoolDynamic();
public:
virtual ~MemoryPoolDynamic();
};
#endif
-116
View File
@@ -1,116 +0,0 @@
/*************************************************************************/
/* memory_pool_dynamic_prealloc.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "memory_pool_dynamic_prealloc.h"
#include "os/memory.h"
#include "print_string.h"
MemoryPoolDynamicPrealloc::ID MemoryPoolDynamicPrealloc::alloc(size_t p_amount,const char* p_description) {
// print_line("dynpool - allocating: "+itos(p_amount));
ID id = pool_alloc->alloc(p_amount);
// print_line("dynpool - free: "+itos(pool_alloc->get_free_mem()));
return id;
}
void MemoryPoolDynamicPrealloc::free(ID p_id) {
pool_alloc->free(p_id);
}
Error MemoryPoolDynamicPrealloc::realloc(ID p_id, size_t p_amount) {
return pool_alloc->resize(p_id,p_amount);
}
bool MemoryPoolDynamicPrealloc::is_valid(ID p_id) {
return true;
}
size_t MemoryPoolDynamicPrealloc::get_size(ID p_id) const {
return pool_alloc->get_size(p_id);
}
const char* MemoryPoolDynamicPrealloc::get_description(ID p_id) const {
return "";
}
Error MemoryPoolDynamicPrealloc::lock(ID p_id) {
// print_line("lock: "+itos(p_id));
return pool_alloc->lock(p_id);
}
void * MemoryPoolDynamicPrealloc::get(ID p_ID) {
// print_line("get: "+itos(p_ID));
return pool_alloc->get(p_ID);
}
Error MemoryPoolDynamicPrealloc::unlock(ID p_id) {
// print_line("unlock: "+itos(p_id));
pool_alloc->unlock(p_id);
return OK;
}
bool MemoryPoolDynamicPrealloc::is_locked(ID p_id) const {
return pool_alloc->is_locked(p_id);
}
size_t MemoryPoolDynamicPrealloc::get_available_mem() const {
return pool_alloc->get_free_mem();
}
size_t MemoryPoolDynamicPrealloc::get_total_usage() const {
return pool_alloc->get_used_mem();
}
MemoryPoolDynamicPrealloc::MemoryPoolDynamicPrealloc(void * p_mem,int p_size, int p_align, int p_max_entries) {
pool_alloc = memnew( PoolAllocator(p_mem,p_size,p_align,true,p_max_entries));
}
MemoryPoolDynamicPrealloc::~MemoryPoolDynamicPrealloc() {
memdelete( pool_alloc );
}
-60
View File
@@ -1,60 +0,0 @@
/*************************************************************************/
/* memory_pool_dynamic_prealloc.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef MEMORY_POOL_DYNAMIC_PREALLOC_H
#define MEMORY_POOL_DYNAMIC_PREALLOC_H
#include "pool_allocator.h"
#include "core/os/memory_pool_dynamic.h"
class MemoryPoolDynamicPrealloc : public MemoryPoolDynamic {
PoolAllocator *pool_alloc;
public:
virtual ID alloc(size_t p_amount,const char* p_description);
virtual void free(ID p_id);
virtual Error realloc(ID p_id, size_t p_amount);
virtual bool is_valid(ID p_id);
virtual size_t get_size(ID p_id) const;
virtual const char* get_description(ID p_id) const;
virtual Error lock(ID p_id);
virtual void * get(ID p_ID);
virtual Error unlock(ID p_id);
virtual bool is_locked(ID p_id) const;
virtual size_t get_available_mem() const;
virtual size_t get_total_usage() const;
MemoryPoolDynamicPrealloc(void * p_mem,int p_size, int p_align = 16, int p_max_entries=PoolAllocator::DEFAULT_MAX_ALLOCS);
~MemoryPoolDynamicPrealloc();
};
#endif // MEMORY_POOL_DYNAMIC_PREALLOC_H
-272
View File
@@ -1,272 +0,0 @@
/*************************************************************************/
/* memory_pool_dynamic_static.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "memory_pool_dynamic_static.h"
#include "os/memory.h"
#include "os/os.h"
#include "ustring.h"
#include "print_string.h"
#include <stdio.h>
MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) {
uint64_t check = p_id/MAX_CHUNKS;
uint64_t idx = p_id%MAX_CHUNKS;
if (!chunk[idx].mem || chunk[idx].check!=check)
return NULL;
return &chunk[idx];
}
const MemoryPoolDynamicStatic::Chunk *MemoryPoolDynamicStatic::get_chunk(ID p_id) const {
uint64_t check = p_id/MAX_CHUNKS;
uint64_t idx = p_id%MAX_CHUNKS;
if (!chunk[idx].mem || chunk[idx].check!=check)
return NULL;
return &chunk[idx];
}
MemoryPoolDynamic::ID MemoryPoolDynamicStatic::alloc(size_t p_amount,const char* p_description) {
_THREAD_SAFE_METHOD_
int idx=-1;
for (int i=0;i<MAX_CHUNKS;i++) {
last_alloc++;
if (last_alloc>=MAX_CHUNKS)
last_alloc=0;
if ( !chunk[last_alloc].mem ) {
idx=last_alloc;
break;
}
}
if (idx==-1) {
ERR_EXPLAIN("Out of dynamic Memory IDs");
ERR_FAIL_V(INVALID_ID);
//return INVALID_ID;
}
//chunk[idx].mem = Memory::alloc_static(p_amount,p_description);
chunk[idx].mem = memalloc(p_amount);
if (!chunk[idx].mem)
return INVALID_ID;
chunk[idx].size=p_amount;
chunk[idx].check=++last_check;
chunk[idx].descr=p_description;
chunk[idx].lock=0;
total_usage+=p_amount;
if (total_usage>max_usage)
max_usage=total_usage;
ID id = chunk[idx].check*MAX_CHUNKS + (uint64_t)idx;
return id;
}
void MemoryPoolDynamicStatic::free(ID p_id) {
_THREAD_SAFE_METHOD_
Chunk *c = get_chunk(p_id);
ERR_FAIL_COND(!c);
total_usage-=c->size;
memfree(c->mem);
c->mem=0;
if (c->lock>0) {
ERR_PRINT("Freed ID Still locked");
}
}
Error MemoryPoolDynamicStatic::realloc(ID p_id, size_t p_amount) {
_THREAD_SAFE_METHOD_
Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(c->lock > 0 , ERR_LOCKED );
void * new_mem = memrealloc(c->mem,p_amount);
ERR_FAIL_COND_V(!new_mem,ERR_OUT_OF_MEMORY);
total_usage-=c->size;
c->mem=new_mem;
c->size=p_amount;
total_usage+=c->size;
if (total_usage>max_usage)
max_usage=total_usage;
return OK;
}
bool MemoryPoolDynamicStatic::is_valid(ID p_id) {
_THREAD_SAFE_METHOD_
Chunk *c = get_chunk(p_id);
return c!=NULL;
}
size_t MemoryPoolDynamicStatic::get_size(ID p_id) const {
_THREAD_SAFE_METHOD_
const Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,0);
return c->size;
}
const char* MemoryPoolDynamicStatic::get_description(ID p_id) const {
_THREAD_SAFE_METHOD_
const Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,"");
return c->descr;
}
bool MemoryPoolDynamicStatic::is_locked(ID p_id) const {
_THREAD_SAFE_METHOD_
const Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,false);
return c->lock>0;
}
Error MemoryPoolDynamicStatic::lock(ID p_id) {
_THREAD_SAFE_METHOD_
Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER);
c->lock++;
return OK;
}
void * MemoryPoolDynamicStatic::get(ID p_id) {
_THREAD_SAFE_METHOD_
const Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,NULL);
ERR_FAIL_COND_V( c->lock==0, NULL );
return c->mem;
}
Error MemoryPoolDynamicStatic::unlock(ID p_id) {
_THREAD_SAFE_METHOD_
Chunk *c = get_chunk(p_id);
ERR_FAIL_COND_V(!c,ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V( c->lock<=0, ERR_INVALID_PARAMETER );
c->lock--;
return OK;
}
size_t MemoryPoolDynamicStatic::get_available_mem() const {
return Memory::get_mem_available();
}
size_t MemoryPoolDynamicStatic::get_total_usage() const {
_THREAD_SAFE_METHOD_
return total_usage;
}
MemoryPoolDynamicStatic::MemoryPoolDynamicStatic() {
last_check=1;
last_alloc=0;
total_usage=0;
max_usage=0;
}
MemoryPoolDynamicStatic::~MemoryPoolDynamicStatic() {
#ifdef DEBUG_MEMORY_ENABLED
if (OS::get_singleton()->is_stdout_verbose()) {
if (total_usage>0) {
ERR_PRINT("DYNAMIC ALLOC: ** MEMORY LEAKS DETECTED **");
ERR_PRINT(String("DYNAMIC ALLOC: "+String::num(total_usage)+" bytes of memory in use at exit.").ascii().get_data());
ERR_PRINT("DYNAMIC ALLOC: Following is the list of leaked allocations:");
for (int i=0;i<MAX_CHUNKS;i++) {
if (chunk[i].mem) {
ERR_PRINT(String("\t"+String::num(chunk[i].size)+" bytes - "+String(chunk[i].descr)).ascii().get_data());
}
}
ERR_PRINT("DYNAMIC ALLOC: End of Report.");
print_line("INFO: dynmem - max: "+itos(max_usage)+", "+itos(total_usage)+" leaked.");
} else {
print_line("INFO: dynmem - max: "+itos(max_usage)+", no leaks.");
}
}
#endif
}
-86
View File
@@ -1,86 +0,0 @@
/*************************************************************************/
/* memory_pool_dynamic_static.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef MEMORY_POOL_DYNAMIC_STATIC_H
#define MEMORY_POOL_DYNAMIC_STATIC_H
#include "os/memory_pool_dynamic.h"
#include "typedefs.h"
#include "os/thread_safe.h"
class MemoryPoolDynamicStatic : public MemoryPoolDynamic {
_THREAD_SAFE_CLASS_
enum {
MAX_CHUNKS=65536
};
struct Chunk {
uint64_t lock;
uint64_t check;
void *mem;
size_t size;
const char *descr;
Chunk() { mem=NULL; lock=0; check=0; }
};
Chunk chunk[MAX_CHUNKS];
uint64_t last_check;
int last_alloc;
size_t total_usage;
size_t max_usage;
Chunk *get_chunk(ID p_id);
const Chunk *get_chunk(ID p_id) const;
public:
virtual ID alloc(size_t p_amount,const char* p_description);
virtual void free(ID p_id);
virtual Error realloc(ID p_id, size_t p_amount);
virtual bool is_valid(ID p_id);
virtual size_t get_size(ID p_id) const;
virtual const char* get_description(ID p_id) const;
virtual bool is_locked(ID p_id) const;
virtual Error lock(ID p_id);
virtual void * get(ID p_ID);
virtual Error unlock(ID p_id);
virtual size_t get_available_mem() const;
virtual size_t get_total_usage() const;
MemoryPoolDynamicStatic();
virtual ~MemoryPoolDynamicStatic();
};
#endif
+1 -1
View File
@@ -371,7 +371,7 @@ int OS::get_static_memory_usage() const {
}
int OS::get_dynamic_memory_usage() const{
return Memory::get_dynamic_mem_usage();
return MemoryPool::total_memory;
}
int OS::get_static_memory_peak_usage() const {
+21
View File
@@ -0,0 +1,21 @@
#include "rw_lock.h"
#include "error_macros.h"
#include <stddef.h>
RWLock* (*RWLock::create_func)()=0;
RWLock *RWLock::create() {
ERR_FAIL_COND_V( !create_func, 0 );
return create_func();
}
RWLock::~RWLock() {
}
+46
View File
@@ -0,0 +1,46 @@
#ifndef RWLOCK_H
#define RWLOCK_H
#include "error_list.h"
class RWLock {
protected:
static RWLock* (*create_func)();
public:
virtual void read_lock()=0; ///< Lock the rwlock, block if locked by someone else
virtual void read_unlock()=0; ///< Unlock the rwlock, let other threads continue
virtual Error read_try_lock()=0; ///< Attempt to lock the rwlock, OK on success, ERROR means it can't lock.
virtual void write_lock()=0; ///< Lock the rwlock, block if locked by someone else
virtual void write_unlock()=0; ///< Unlock the rwlock, let other thwrites continue
virtual Error write_try_lock()=0; ///< Attempt to lock the rwlock, OK on success, ERROR means it can't lock.
static RWLock * create(); ///< Create a rwlock
virtual ~RWLock();
};
class RWLockRead {
RWLock *lock;
public:
RWLockRead(RWLock* p_lock) { lock=p_lock; if (lock) lock->read_lock(); }
~RWLockRead() { if (lock) lock->read_unlock(); }
};
class RWLockWrite {
RWLock *lock;
public:
RWLockWrite(RWLock* p_lock) { lock=p_lock; if (lock) lock->write_lock(); }
~RWLockWrite() { if (lock) lock->write_unlock(); }
};
#endif // RWLOCK_H