From c7b730c655583e83cd08cb429acebac79df639b8 Mon Sep 17 00:00:00 2001 From: koalefant Date: Fri, 4 Dec 2015 05:43:48 +0100 Subject: [PATCH] Vector<>::_ptr is now typed and points to the beginning of array rather than reference count block --- core/vector.h | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/core/vector.h b/core/vector.h index d103400622..78dff5eadb 100644 --- a/core/vector.h +++ b/core/vector.h @@ -42,7 +42,7 @@ template class Vector { - mutable void* _ptr; + mutable T* _ptr; // internal helpers @@ -51,21 +51,21 @@ class Vector { if (!_ptr) return NULL; - return reinterpret_cast(_ptr); + return reinterpret_cast((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount)); } _FORCE_INLINE_ int* _get_size() const { if (!_ptr) return NULL; - return reinterpret_cast(((uint8_t*)(_ptr))+sizeof(SafeRefCount)); + return reinterpret_cast((uint8_t*)_ptr-sizeof(int)); } _FORCE_INLINE_ T* _get_data() const { if (!_ptr) return NULL; - return reinterpret_cast(((uint8_t*)(_ptr))+sizeof(SafeRefCount)+sizeof(int)); + return reinterpret_cast(_ptr); } @@ -88,11 +88,11 @@ public: _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ int size() const { - - if (!_ptr) - return 0; + int* size = _get_size(); + if (size) + return *size; else - return *reinterpret_cast(((uint8_t*)(_ptr))+sizeof(SafeRefCount)); + return 0; } _FORCE_INLINE_ bool empty() const { return _ptr == 0; } Error resize(int p_size); @@ -174,7 +174,7 @@ void Vector::_unref(void *p_data) { if (!p_data) return; - SafeRefCount *src = reinterpret_cast(p_data); + SafeRefCount *src = reinterpret_cast((uint8_t*)p_data-sizeof(int)-sizeof(SafeRefCount)); if (!src->unref()) return; // still in use @@ -189,7 +189,7 @@ void Vector::_unref(void *p_data) { } // free mem - memfree(p_data); + memfree((uint8_t*)p_data-sizeof(int)-sizeof(SafeRefCount)); } @@ -201,7 +201,8 @@ void Vector::_copy_on_write() { if (_get_refcount()->get() > 1 ) { /* in use by more than me */ - SafeRefCount *src_new=(SafeRefCount *)memalloc(_get_alloc_size(*_get_size())); + void* mem_new = memalloc(_get_alloc_size(*_get_size())); + SafeRefCount *src_new=(SafeRefCount *)mem_new; src_new->init(); int * _size = (int*)(src_new+1); *_size=*_get_size(); @@ -215,7 +216,7 @@ void Vector::_copy_on_write() { } _unref(_ptr); - _ptr=src_new; + _ptr=_data; } } @@ -260,16 +261,17 @@ Error Vector::resize(int p_size) { if (size()==0) { // alloc from scratch - _ptr = (T*)memalloc(_get_alloc_size(p_size)); - ERR_FAIL_COND_V( !_ptr ,ERR_OUT_OF_MEMORY); + void* ptr=memalloc(_get_alloc_size(p_size)); + ERR_FAIL_COND_V( !ptr ,ERR_OUT_OF_MEMORY); + _ptr=(T*)((uint8_t*)ptr+sizeof(int)+sizeof(SafeRefCount)); _get_refcount()->init(); // init refcount *_get_size()=0; // init size (currently, none) } else { - void *_ptrnew = (T*)memrealloc(_ptr,_get_alloc_size(p_size)); + void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount),_get_alloc_size(p_size)); ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); - _ptr=_ptrnew; + _ptr=(T*)((uint8_t*)_ptrnew+sizeof(int)+sizeof(SafeRefCount)); } // construct the newly created elements @@ -291,10 +293,10 @@ Error Vector::resize(int p_size) { t->~T(); } - void *_ptrnew = (T*)memrealloc(_ptr,_get_alloc_size(p_size)); + void *_ptrnew = (T*)memrealloc((uint8_t*)_ptr-sizeof(int)-sizeof(SafeRefCount),_get_alloc_size(p_size)); ERR_FAIL_COND_V( !_ptrnew ,ERR_OUT_OF_MEMORY); - _ptr=_ptrnew; + _ptr=(T*)((uint8_t*)_ptrnew+sizeof(int)+sizeof(SafeRefCount)); *_get_size()=p_size;