Revert "Improve reliability of memory stats"
This commit is contained in:
parent
6758ba0d93
commit
711ebafa71
4 changed files with 52 additions and 123 deletions
|
@ -29,7 +29,6 @@
|
||||||
/*************************************************************************/
|
/*************************************************************************/
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "copymem.h"
|
#include "copymem.h"
|
||||||
#include "core/safe_refcount.h"
|
|
||||||
#include "error_macros.h"
|
#include "error_macros.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -44,44 +43,42 @@ void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) {
|
||||||
return p_allocfunc(p_size);
|
return p_allocfunc(p_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
size_t Memory::mem_usage = 0;
|
size_t Memory::mem_usage = 0;
|
||||||
size_t Memory::max_usage = 0;
|
size_t Memory::max_usage = 0;
|
||||||
#ifdef DEBUG_MEMORY_TAGGING
|
|
||||||
#define MEM_UNINIT_TAG 0xEA
|
|
||||||
#define MEM_RELEASED_TAG 0xAE
|
|
||||||
#endif
|
|
||||||
#define PREPAD true
|
|
||||||
#else
|
|
||||||
#define PREPAD p_pad_align
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint64_t Memory::alloc_count = 0;
|
size_t Memory::alloc_count = 0;
|
||||||
|
|
||||||
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
|
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
|
||||||
|
|
||||||
void *mem = malloc(p_bytes + (PREPAD ? PAD_ALIGN : 0));
|
#ifdef DEBUG_ENABLED
|
||||||
|
bool prepad = true;
|
||||||
|
#else
|
||||||
|
bool prepad = p_pad_align;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void *mem = malloc(p_bytes + (prepad ? PAD_ALIGN : 0));
|
||||||
|
|
||||||
|
alloc_count++;
|
||||||
|
|
||||||
ERR_FAIL_COND_V(!mem, NULL);
|
ERR_FAIL_COND_V(!mem, NULL);
|
||||||
|
|
||||||
atomic_increment(&alloc_count);
|
if (prepad) {
|
||||||
|
uint64_t *s = (uint64_t *)mem;
|
||||||
|
*s = p_bytes;
|
||||||
|
|
||||||
|
uint8_t *s8 = (uint8_t *)mem;
|
||||||
|
|
||||||
if (PREPAD) {
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage += p_bytes;
|
mem_usage += p_bytes;
|
||||||
if (mem_usage > max_usage) {
|
if (mem_usage > max_usage) {
|
||||||
max_usage = mem_usage;
|
max_usage = mem_usage;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
return s8 + PAD_ALIGN;
|
||||||
uint64_t *s = (uint64_t *)mem;
|
|
||||||
*s = p_bytes;
|
|
||||||
|
|
||||||
uint8_t *s8 = (uint8_t *)mem + PAD_ALIGN;
|
|
||||||
#if defined DEBUG_ENABLED && defined DEBUG_MEMORY_TAGGING
|
|
||||||
memset(s8, MEM_UNINIT_TAG, p_bytes);
|
|
||||||
#endif
|
|
||||||
return s8;
|
|
||||||
} else {
|
} else {
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
@ -95,44 +92,38 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
|
||||||
|
|
||||||
uint8_t *mem = (uint8_t *)p_memory;
|
uint8_t *mem = (uint8_t *)p_memory;
|
||||||
|
|
||||||
if (PREPAD) {
|
#ifdef DEBUG_ENABLED
|
||||||
|
bool prepad = true;
|
||||||
|
#else
|
||||||
|
bool prepad = p_pad_align;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (prepad) {
|
||||||
mem -= PAD_ALIGN;
|
mem -= PAD_ALIGN;
|
||||||
uint64_t *s = (uint64_t *)mem;
|
uint64_t *s = (uint64_t *)mem;
|
||||||
|
|
||||||
if (p_bytes == 0) {
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage -= *s;
|
mem_usage -= *s;
|
||||||
#ifdef DEBUG_MEMORY_TAGGING
|
mem_usage += p_bytes;
|
||||||
memset(mem, MEM_RELEASED_TAG, PAD_ALIGN + *s);
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (p_bytes == 0) {
|
||||||
free(mem);
|
free(mem);
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
#if defined DEBUG_ENABLED && defined DEBUG_MEMORY_TAGGING
|
*s = p_bytes;
|
||||||
if (p_bytes < *s) {
|
|
||||||
memset(mem + PAD_ALIGN + p_bytes, MEM_RELEASED_TAG, *s - p_bytes);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN);
|
mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN);
|
||||||
ERR_FAIL_COND_V(!mem, NULL);
|
ERR_FAIL_COND_V(!mem, NULL);
|
||||||
|
|
||||||
s = (uint64_t *)mem;
|
s = (uint64_t *)mem;
|
||||||
#ifdef DEBUG_ENABLED
|
|
||||||
mem_usage -= *s;
|
|
||||||
mem_usage += p_bytes;
|
|
||||||
#ifdef DEBUG_MEMORY_TAGGING
|
|
||||||
if (p_bytes > *s) {
|
|
||||||
memset(mem + PAD_ALIGN + *s, MEM_UNINIT_TAG, p_bytes - *s);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
*s = p_bytes;
|
*s = p_bytes;
|
||||||
|
|
||||||
return mem + PAD_ALIGN;
|
return mem + PAD_ALIGN;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
mem = (uint8_t *)realloc(mem, p_bytes);
|
mem = (uint8_t *)realloc(mem, p_bytes);
|
||||||
|
|
||||||
ERR_FAIL_COND_V(mem == NULL && p_bytes > 0, NULL);
|
ERR_FAIL_COND_V(mem == NULL && p_bytes > 0, NULL);
|
||||||
|
@ -147,19 +138,27 @@ void Memory::free_static(void *p_ptr, bool p_pad_align) {
|
||||||
|
|
||||||
uint8_t *mem = (uint8_t *)p_ptr;
|
uint8_t *mem = (uint8_t *)p_ptr;
|
||||||
|
|
||||||
if (PREPAD) {
|
|
||||||
mem -= PAD_ALIGN;
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
const uint64_t s = *((uint64_t *)mem);
|
bool prepad = true;
|
||||||
mem_usage -= s;
|
#else
|
||||||
#ifdef DEBUG_MEMORY_TAGGING
|
bool prepad = p_pad_align;
|
||||||
memset(mem, MEM_RELEASED_TAG, PAD_ALIGN + s);
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
}
|
|
||||||
atomic_decrement(&alloc_count);
|
|
||||||
|
|
||||||
free(mem);
|
alloc_count--;
|
||||||
|
|
||||||
|
if (prepad) {
|
||||||
|
mem -= PAD_ALIGN;
|
||||||
|
uint64_t *s = (uint64_t *)mem;
|
||||||
|
|
||||||
|
#ifdef DEBUG_ENABLED
|
||||||
|
mem_usage -= *s;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
free(mem);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
free(mem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Memory::get_mem_available() {
|
size_t Memory::get_mem_available() {
|
||||||
|
|
|
@ -48,7 +48,8 @@ class Memory {
|
||||||
static size_t mem_usage;
|
static size_t mem_usage;
|
||||||
static size_t max_usage;
|
static size_t max_usage;
|
||||||
#endif
|
#endif
|
||||||
static uint64_t alloc_count;
|
|
||||||
|
static size_t alloc_count;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
|
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
|
||||||
|
|
|
@ -57,30 +57,6 @@ uint32_t atomic_decrement(register uint32_t *pw) {
|
||||||
return *pw;
|
return *pw;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t atomic_conditional_increment(register uint64_t *pw) {
|
|
||||||
|
|
||||||
if (*pw == 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
(*pw)++;
|
|
||||||
|
|
||||||
return *pw;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t atomic_increment(register uint64_t *pw) {
|
|
||||||
|
|
||||||
(*pw)++;
|
|
||||||
|
|
||||||
return *pw;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t atomic_decrement(register uint64_t *pw) {
|
|
||||||
|
|
||||||
(*pw)--;
|
|
||||||
|
|
||||||
return *pw;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
@ -108,28 +84,6 @@ uint32_t atomic_decrement(register uint32_t *pw) {
|
||||||
uint32_t atomic_increment(register uint32_t *pw) {
|
uint32_t atomic_increment(register uint32_t *pw) {
|
||||||
return InterlockedIncrement((LONG volatile *)pw);
|
return InterlockedIncrement((LONG volatile *)pw);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t atomic_conditional_increment(register uint64_t *pw) {
|
|
||||||
|
|
||||||
/* try to increment until it actually works */
|
|
||||||
// taken from boost
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
uint64_t tmp = static_cast<uint64_t const volatile &>(*pw);
|
|
||||||
if (tmp == 0)
|
|
||||||
return 0; // if zero, can't add to it anymore
|
|
||||||
if (InterlockedCompareExchange64((LONGLONG volatile *)pw, tmp + 1, tmp) == tmp)
|
|
||||||
return tmp + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t atomic_decrement(register uint64_t *pw) {
|
|
||||||
return InterlockedDecrement64((LONGLONG volatile *)pw);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t atomic_increment(register uint64_t *pw) {
|
|
||||||
return InterlockedIncrement64((LONGLONG volatile *)pw);
|
|
||||||
}
|
|
||||||
#elif defined(__GNUC__)
|
#elif defined(__GNUC__)
|
||||||
|
|
||||||
uint32_t atomic_conditional_increment(register uint32_t *pw) {
|
uint32_t atomic_conditional_increment(register uint32_t *pw) {
|
||||||
|
@ -153,27 +107,6 @@ uint32_t atomic_increment(register uint32_t *pw) {
|
||||||
return __sync_add_and_fetch(pw, 1);
|
return __sync_add_and_fetch(pw, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t atomic_conditional_increment(register uint64_t *pw) {
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
uint64_t tmp = static_cast<uint64_t const volatile &>(*pw);
|
|
||||||
if (tmp == 0)
|
|
||||||
return 0; // if zero, can't add to it anymore
|
|
||||||
if (__sync_val_compare_and_swap(pw, tmp, tmp + 1) == tmp)
|
|
||||||
return tmp + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t atomic_decrement(register uint64_t *pw) {
|
|
||||||
|
|
||||||
return __sync_sub_and_fetch(pw, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t atomic_increment(register uint64_t *pw) {
|
|
||||||
|
|
||||||
return __sync_add_and_fetch(pw, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
//no threads supported?
|
//no threads supported?
|
||||||
#error Must provide atomic functions for this platform or compiler!
|
#error Must provide atomic functions for this platform or compiler!
|
||||||
|
|
|
@ -40,10 +40,6 @@ uint32_t atomic_conditional_increment(register uint32_t *counter);
|
||||||
uint32_t atomic_decrement(register uint32_t *pw);
|
uint32_t atomic_decrement(register uint32_t *pw);
|
||||||
uint32_t atomic_increment(register uint32_t *pw);
|
uint32_t atomic_increment(register uint32_t *pw);
|
||||||
|
|
||||||
uint64_t atomic_conditional_increment(register uint64_t *counter);
|
|
||||||
uint64_t atomic_decrement(register uint64_t *pw);
|
|
||||||
uint64_t atomic_increment(register uint64_t *pw);
|
|
||||||
|
|
||||||
struct SafeRefCount {
|
struct SafeRefCount {
|
||||||
|
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
|
|
Loading…
Reference in a new issue