Use atomics for memory use tracking
Plus: - An allocation is counted only after checking its success. - Max usage is updated after growing reallocs as well. - Drop unused header. - Changed the 0xFFF.. at get_mem_available() to -1 with a comment telling it's the same, but more universal.
This commit is contained in:
parent
3ad68c282e
commit
02607b3103
2 changed files with 27 additions and 25 deletions
|
@ -29,6 +29,7 @@
|
||||||
/*************************************************************************/
|
/*************************************************************************/
|
||||||
#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>
|
||||||
|
@ -43,14 +44,12 @@ 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;
|
uint64_t Memory::mem_usage = 0;
|
||||||
size_t Memory::max_usage = 0;
|
uint64_t Memory::max_usage = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
size_t Memory::alloc_count = 0;
|
uint64_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) {
|
||||||
|
|
||||||
|
@ -62,10 +61,10 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
|
||||||
|
|
||||||
void *mem = malloc(p_bytes + (prepad ? PAD_ALIGN : 0));
|
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) {
|
if (prepad) {
|
||||||
uint64_t *s = (uint64_t *)mem;
|
uint64_t *s = (uint64_t *)mem;
|
||||||
*s = p_bytes;
|
*s = p_bytes;
|
||||||
|
@ -73,10 +72,8 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
|
||||||
uint8_t *s8 = (uint8_t *)mem;
|
uint8_t *s8 = (uint8_t *)mem;
|
||||||
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage += p_bytes;
|
atomic_add(&mem_usage, p_bytes);
|
||||||
if (mem_usage > max_usage) {
|
atomic_exchange_if_greater(&max_usage, mem_usage);
|
||||||
max_usage = mem_usage;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
return s8 + PAD_ALIGN;
|
return s8 + PAD_ALIGN;
|
||||||
} else {
|
} else {
|
||||||
|
@ -103,8 +100,12 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
|
||||||
uint64_t *s = (uint64_t *)mem;
|
uint64_t *s = (uint64_t *)mem;
|
||||||
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage -= *s;
|
if (p_bytes > *s) {
|
||||||
mem_usage += p_bytes;
|
atomic_add(&mem_usage, p_bytes - *s);
|
||||||
|
atomic_exchange_if_greater(&max_usage, mem_usage);
|
||||||
|
} else {
|
||||||
|
atomic_sub(&mem_usage, *s - p_bytes);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (p_bytes == 0) {
|
if (p_bytes == 0) {
|
||||||
|
@ -144,14 +145,14 @@ void Memory::free_static(void *p_ptr, bool p_pad_align) {
|
||||||
bool prepad = p_pad_align;
|
bool prepad = p_pad_align;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
alloc_count--;
|
atomic_decrement(&alloc_count);
|
||||||
|
|
||||||
if (prepad) {
|
if (prepad) {
|
||||||
mem -= PAD_ALIGN;
|
mem -= PAD_ALIGN;
|
||||||
uint64_t *s = (uint64_t *)mem;
|
uint64_t *s = (uint64_t *)mem;
|
||||||
|
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
mem_usage -= *s;
|
atomic_sub(&mem_usage, *s);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
free(mem);
|
free(mem);
|
||||||
|
@ -161,19 +162,20 @@ void Memory::free_static(void *p_ptr, bool p_pad_align) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Memory::get_mem_available() {
|
uint64_t Memory::get_mem_available() {
|
||||||
|
|
||||||
return 0xFFFFFFFFFFFFF;
|
return -1; // 0xFFFF...
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Memory::get_mem_usage() {
|
uint64_t Memory::get_mem_usage() {
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
return mem_usage;
|
return mem_usage;
|
||||||
#else
|
#else
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
size_t Memory::get_mem_max_usage() {
|
|
||||||
|
uint64_t Memory::get_mem_max_usage() {
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
return max_usage;
|
return max_usage;
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -45,20 +45,20 @@ class Memory {
|
||||||
|
|
||||||
Memory();
|
Memory();
|
||||||
#ifdef DEBUG_ENABLED
|
#ifdef DEBUG_ENABLED
|
||||||
static size_t mem_usage;
|
static uint64_t mem_usage;
|
||||||
static size_t max_usage;
|
static uint64_t max_usage;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static size_t alloc_count;
|
static uint64_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);
|
||||||
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
|
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
|
||||||
static void free_static(void *p_ptr, bool p_pad_align = false);
|
static void free_static(void *p_ptr, bool p_pad_align = false);
|
||||||
|
|
||||||
static size_t get_mem_available();
|
static uint64_t get_mem_available();
|
||||||
static size_t get_mem_usage();
|
static uint64_t get_mem_usage();
|
||||||
static size_t get_mem_max_usage();
|
static uint64_t get_mem_max_usage();
|
||||||
};
|
};
|
||||||
|
|
||||||
class DefaultAllocator {
|
class DefaultAllocator {
|
||||||
|
|
Loading…
Reference in a new issue