From 3ef7ecf5468960c362511a69b153471b33544464 Mon Sep 17 00:00:00 2001 From: Hein-Pieter van Braam Date: Fri, 14 Dec 2018 20:03:02 +0000 Subject: [PATCH] Don't allocate in Vector's ctor By relying on the fact that a struct or class's first member has the same address as the struct itself we can cast VectorWriteProxy to Vector and access the CowData field. This allows a Vector to be moved in memory without invalidating the pointer to the cowdata field. --- core/vector.h | 56 +++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/core/vector.h b/core/vector.h index 38a10824074..2fb6f6c1c4d 100644 --- a/core/vector.h +++ b/core/vector.h @@ -44,17 +44,11 @@ template class VectorWriteProxy { - friend class Vector; - CowData *_parent; - - _FORCE_INLINE_ VectorWriteProxy(CowData *parent) : - _parent(parent){}; - public: _FORCE_INLINE_ T &operator[](int p_index) { - CRASH_BAD_INDEX(p_index, _parent->size()); + CRASH_BAD_INDEX(p_index, ((Vector *)(this))->_cowdata.size()); - return _parent->ptrw()[p_index]; + return ((Vector *)(this))->_cowdata.ptrw()[p_index]; } }; @@ -62,39 +56,41 @@ template class Vector { friend class VectorWriteProxy; - CowData *_cowdata; - public: VectorWriteProxy write; +private: + CowData _cowdata; + +public: bool push_back(const T &p_elem); - void remove(int p_index) { _cowdata->remove(p_index); } + void remove(int p_index) { _cowdata.remove(p_index); } void erase(const T &p_val) { int idx = find(p_val); if (idx >= 0) remove(idx); }; void invert(); - _FORCE_INLINE_ T *ptrw() { return _cowdata->ptrw(); } - _FORCE_INLINE_ const T *ptr() const { return _cowdata->ptr(); } + _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } + _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } _FORCE_INLINE_ void clear() { resize(0); } - _FORCE_INLINE_ bool empty() const { return _cowdata->empty(); } + _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); } - _FORCE_INLINE_ T get(int p_index) { return _cowdata->get(p_index); } - _FORCE_INLINE_ const T get(int p_index) const { return _cowdata->get(p_index); } - _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata->set(p_index, p_elem); } - _FORCE_INLINE_ int size() const { return _cowdata->size(); } - Error resize(int p_size) { return _cowdata->resize(p_size); } - _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata->get(p_index); } - Error insert(int p_pos, const T &p_val) { return _cowdata->insert(p_pos, p_val); } + _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); } + _FORCE_INLINE_ const T get(int p_index) const { return _cowdata.get(p_index); } + _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } + _FORCE_INLINE_ int size() const { return _cowdata.size(); } + Error resize(int p_size) { return _cowdata.resize(p_size); } + _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); } + Error insert(int p_pos, const T &p_val) { return _cowdata.insert(p_pos, p_val); } void append_array(const Vector &p_other); template void sort_custom() { - int len = _cowdata->size(); + int len = _cowdata.size(); if (len == 0) return; @@ -110,7 +106,7 @@ public: void ordered_insert(const T &p_val) { int i; - for (i = 0; i < _cowdata->size(); i++) { + for (i = 0; i < _cowdata.size(); i++) { if (p_val < operator[](i)) { break; @@ -135,20 +131,14 @@ public: return ret; } - _FORCE_INLINE_ Vector() : - _cowdata(new CowData()), - write(VectorWriteProxy(_cowdata)) {} - _FORCE_INLINE_ Vector(const Vector &p_from) : - _cowdata(new CowData()), - write(VectorWriteProxy(_cowdata)) { _cowdata->_ref(p_from._cowdata); } + _FORCE_INLINE_ Vector() {} + _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); } inline Vector &operator=(const Vector &p_from) { - _cowdata->_ref(p_from._cowdata); + _cowdata._ref(p_from._cowdata); return *this; } - _FORCE_INLINE_ ~Vector() { - delete _cowdata; - } + _FORCE_INLINE_ ~Vector() {} }; template