aboutsummaryrefslogtreecommitdiff
path: root/core/os/memory_pool_dynamic_static.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/os/memory_pool_dynamic_static.cpp')
-rw-r--r--core/os/memory_pool_dynamic_static.cpp128
1 files changed, 58 insertions, 70 deletions
diff --git a/core/os/memory_pool_dynamic_static.cpp b/core/os/memory_pool_dynamic_static.cpp
index 9db4916cb..991162753 100644
--- a/core/os/memory_pool_dynamic_static.cpp
+++ b/core/os/memory_pool_dynamic_static.cpp
@@ -29,54 +29,52 @@
#include "memory_pool_dynamic_static.h"
#include "os/memory.h"
#include "os/os.h"
-#include "ustring.h"
#include "print_string.h"
+#include "ustring.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;
+ uint64_t check = p_id / MAX_CHUNKS;
+ uint64_t idx = p_id % MAX_CHUNKS;
- if (!chunk[idx].mem || chunk[idx].check!=check)
+ 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;
+ uint64_t check = p_id / MAX_CHUNKS;
+ uint64_t idx = p_id % MAX_CHUNKS;
- if (!chunk[idx].mem || chunk[idx].check!=check)
+ 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) {
+MemoryPoolDynamic::ID MemoryPoolDynamicStatic::alloc(size_t p_amount, const char *p_description) {
_THREAD_SAFE_METHOD_
- int idx=-1;
+ int idx = -1;
- for (int i=0;i<MAX_CHUNKS;i++) {
+ for (int i = 0; i < MAX_CHUNKS; i++) {
last_alloc++;
- if (last_alloc>=MAX_CHUNKS)
- last_alloc=0;
+ if (last_alloc >= MAX_CHUNKS)
+ last_alloc = 0;
- if ( !chunk[last_alloc].mem ) {
+ if (!chunk[last_alloc].mem) {
- idx=last_alloc;
+ idx = last_alloc;
break;
}
}
-
- if (idx==-1) {
+ if (idx == -1) {
ERR_EXPLAIN("Out of dynamic Memory IDs");
ERR_FAIL_V(INVALID_ID);
//return INVALID_ID;
@@ -87,19 +85,18 @@ MemoryPoolDynamic::ID MemoryPoolDynamicStatic::alloc(size_t p_amount,const char*
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;
+ 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;
+ total_usage += p_amount;
+ if (total_usage > max_usage)
+ max_usage = total_usage;
- ID id = chunk[idx].check*MAX_CHUNKS + (uint64_t)idx;
+ ID id = chunk[idx].check * MAX_CHUNKS + (uint64_t)idx;
return id;
-
}
void MemoryPoolDynamicStatic::free(ID p_id) {
@@ -108,13 +105,12 @@ void MemoryPoolDynamicStatic::free(ID p_id) {
Chunk *c = get_chunk(p_id);
ERR_FAIL_COND(!c);
-
- total_usage-=c->size;
+ total_usage -= c->size;
memfree(c->mem);
- c->mem=0;
+ c->mem = 0;
- if (c->lock>0) {
+ if (c->lock > 0) {
ERR_PRINT("Freed ID Still locked");
}
@@ -125,20 +121,18 @@ 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 );
-
+ 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;
+ 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;
}
@@ -148,41 +142,35 @@ bool MemoryPoolDynamicStatic::is_valid(ID p_id) {
Chunk *c = get_chunk(p_id);
- return c!=NULL;
-
+ 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);
+ ERR_FAIL_COND_V(!c, 0);
return c->size;
-
-
}
-const char* MemoryPoolDynamicStatic::get_description(ID p_id) const {
+const char *MemoryPoolDynamicStatic::get_description(ID p_id) const {
_THREAD_SAFE_METHOD_
const Chunk *c = get_chunk(p_id);
- ERR_FAIL_COND_V(!c,"");
+ 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;
+ ERR_FAIL_COND_V(!c, false);
+ return c->lock > 0;
}
Error MemoryPoolDynamicStatic::lock(ID p_id) {
@@ -190,19 +178,19 @@ Error MemoryPoolDynamicStatic::lock(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, ERR_INVALID_PARAMETER);
c->lock++;
return OK;
}
-void * MemoryPoolDynamicStatic::get(ID p_id) {
+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 );
+ ERR_FAIL_COND_V(!c, NULL);
+ ERR_FAIL_COND_V(c->lock == 0, NULL);
return c->mem;
}
@@ -211,9 +199,9 @@ 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, ERR_INVALID_PARAMETER);
- ERR_FAIL_COND_V( c->lock<=0, ERR_INVALID_PARAMETER );
+ ERR_FAIL_COND_V(c->lock <= 0, ERR_INVALID_PARAMETER);
c->lock--;
return OK;
@@ -232,10 +220,10 @@ size_t MemoryPoolDynamicStatic::get_total_usage() const {
MemoryPoolDynamicStatic::MemoryPoolDynamicStatic() {
- last_check=1;
- last_alloc=0;
- total_usage=0;
- max_usage=0;
+ last_check = 1;
+ last_alloc = 0;
+ total_usage = 0;
+ max_usage = 0;
}
MemoryPoolDynamicStatic::~MemoryPoolDynamicStatic() {
@@ -244,27 +232,27 @@ MemoryPoolDynamicStatic::~MemoryPoolDynamicStatic() {
if (OS::get_singleton()->is_stdout_verbose()) {
- if (total_usage>0) {
+ 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(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++) {
+ 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(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.");
+ print_line("INFO: dynmem - max: " + itos(max_usage) + ", " + itos(total_usage) + " leaked.");
} else {
- print_line("INFO: dynmem - max: "+itos(max_usage)+", no leaks.");
+ print_line("INFO: dynmem - max: " + itos(max_usage) + ", no leaks.");
}
}