diff options
Diffstat (limited to 'core/os/memory.cpp')
| -rw-r--r-- | core/os/memory.cpp | 156 |
1 files changed, 115 insertions, 41 deletions
diff --git a/core/os/memory.cpp b/core/os/memory.cpp index c2ff2aa78..37a523b76 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* 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 */ @@ -30,9 +30,13 @@ #include "error_macros.h" #include "copymem.h" #include <stdio.h> +#include <stdlib.h> + + + void * operator new(size_t p_size,const char *p_description) { - return Memory::alloc_static( p_size, p_description ); + return Memory::alloc_static( p_size, false ); } void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)) { @@ -42,74 +46,144 @@ void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)) { #include <stdio.h> -void * Memory::alloc_static(size_t p_bytes,const char *p_alloc_from) { +#ifdef DEBUG_ENABLED +size_t Memory::mem_usage=0; +size_t Memory::max_usage=0; +#endif - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), NULL ); - return MemoryPoolStatic::get_singleton()->alloc(p_bytes,p_alloc_from); -} -void * Memory::realloc_static(void *p_memory,size_t p_bytes) { +size_t Memory::alloc_count=0; - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), NULL ); - return MemoryPoolStatic::get_singleton()->realloc(p_memory,p_bytes); -} -void Memory::free_static(void *p_ptr) { +void * Memory::alloc_static(size_t p_bytes,bool p_pad_align) { - ERR_FAIL_COND( !MemoryPoolStatic::get_singleton()); - MemoryPoolStatic::get_singleton()->free(p_ptr); -} +#ifdef DEBUG_ENABLED + bool prepad=true; +#else + bool prepad=p_pad_align; +#endif -size_t Memory::get_static_mem_available() { + void * mem = malloc( p_bytes + (prepad?PAD_ALIGN:0)); - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0); - return MemoryPoolStatic::get_singleton()->get_available_mem(); + alloc_count++; -} + ERR_FAIL_COND_V(!mem,NULL); -size_t Memory::get_static_mem_max_usage() { + if (prepad) { + uint64_t *s = (uint64_t*)mem; + *s=p_bytes; - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0); - return MemoryPoolStatic::get_singleton()->get_max_usage(); + uint8_t *s8 = (uint8_t*)mem; + +#ifdef DEBUG_ENABLED + mem_usage+=p_bytes; + if (mem_usage>max_usage) { + max_usage=mem_usage; + } +#endif + return s8 + PAD_ALIGN; + } else { + return mem; + } } -size_t Memory::get_static_mem_usage() { +void * Memory::realloc_static(void *p_memory,size_t p_bytes,bool p_pad_align) { - ERR_FAIL_COND_V( !MemoryPoolStatic::get_singleton(), 0); - return MemoryPoolStatic::get_singleton()->get_total_usage(); + if (p_memory==NULL) { + return alloc_static(p_bytes,p_pad_align); + } -} + uint8_t *mem = (uint8_t*)p_memory; -void Memory::dump_static_mem_to_file(const char* p_file) { +#ifdef DEBUG_ENABLED + bool prepad=true; +#else + bool prepad=p_pad_align; +#endif - MemoryPoolStatic::get_singleton()->dump_mem_to_file(p_file); -} + if (prepad) { + mem-=PAD_ALIGN; + uint64_t *s = (uint64_t*)mem; -MID Memory::alloc_dynamic(size_t p_bytes, const char *p_descr) { +#ifdef DEBUG_ENABLED + mem_usage-=*s; + mem_usage+=p_bytes; +#endif - MemoryPoolDynamic::ID id = MemoryPoolDynamic::get_singleton()->alloc(p_bytes,p_descr); + if (p_bytes==0) { + free(mem); + return NULL; + } else { + *s=p_bytes; - return MID(id); -} -Error Memory::realloc_dynamic(MID p_mid,size_t p_bytes) { + mem = (uint8_t*)realloc(mem,p_bytes+PAD_ALIGN); + ERR_FAIL_COND_V(!mem,NULL); + + s = (uint64_t*)mem; + + *s=p_bytes; - MemoryPoolDynamic::ID id = p_mid.data?p_mid.data->id:MemoryPoolDynamic::INVALID_ID; + return mem+PAD_ALIGN; + } + } else { - if (id==MemoryPoolDynamic::INVALID_ID) - return ERR_INVALID_PARAMETER; + mem = (uint8_t*)realloc(mem,p_bytes); - return MemoryPoolDynamic::get_singleton()->realloc(p_mid, p_bytes); + ERR_FAIL_COND_V(mem==NULL && p_bytes>0,NULL); + return mem; + } } -size_t Memory::get_dynamic_mem_available() { +void Memory::free_static(void *p_ptr,bool p_pad_align) { + + ERR_FAIL_COND(p_ptr==NULL); + + uint8_t *mem = (uint8_t*)p_ptr; + +#ifdef DEBUG_ENABLED + bool prepad=true; +#else + bool prepad=p_pad_align; +#endif + + 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); + } - return MemoryPoolDynamic::get_singleton()->get_available_mem(); } -size_t Memory::get_dynamic_mem_usage() { +size_t Memory::get_mem_available() { + + return 0xFFFFFFFFFFFFF; - return MemoryPoolDynamic::get_singleton()->get_total_usage(); +} + +size_t Memory::get_mem_usage(){ +#ifdef DEBUG_ENABLED + return mem_usage; +#else + return 0; +#endif +} +size_t Memory::get_mem_max_usage(){ +#ifdef DEBUG_ENABLED + return max_usage; +#else + return 0; +#endif } |
