X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Futil%2Fpointer.h;h=f68269882f22ccd1ce36fbb298292f0c5aaf5044;hb=5cc8ad946efb3612eb6ea8655780b29fe4c62e19;hp=f656833328f716a57aefd8d36fb80ded147ec94b;hpb=22e186b4aa88b585e71500c4e9a03bf69b0b6191;p=minetest.git diff --git a/src/util/pointer.h b/src/util/pointer.h index f65683332..f68269882 100644 --- a/src/util/pointer.h +++ b/src/util/pointer.h @@ -24,80 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "../debug.h" // For assert() #include -template -class SharedPtr -{ -public: - SharedPtr(T *t=NULL) - { - refcount = new int; - *refcount = 1; - ptr = t; - } - SharedPtr(SharedPtr &t) - { - //*this = t; - drop(); - refcount = t.refcount; - (*refcount)++; - ptr = t.ptr; - } - ~SharedPtr() - { - drop(); - } - SharedPtr & operator=(T *t) - { - drop(); - refcount = new int; - *refcount = 1; - ptr = t; - return *this; - } - SharedPtr & operator=(SharedPtr &t) - { - drop(); - refcount = t.refcount; - (*refcount)++; - ptr = t.ptr; - return *this; - } - T* operator->() - { - return ptr; - } - T & operator*() - { - return *ptr; - } - bool operator!=(T *t) - { - return ptr != t; - } - bool operator==(T *t) - { - return ptr == t; - } - T & operator[](unsigned int i) - { - return ptr[i]; - } -private: - void drop() - { - assert((*refcount) > 0); - (*refcount)--; - if(*refcount == 0) - { - delete refcount; - if(ptr != NULL) - delete ptr; - } - } - T *ptr; - int *refcount; -}; - template class Buffer { @@ -171,13 +97,20 @@ class Buffer private: void drop() { - if(data) - delete[] data; + delete[] data; } T *data; unsigned int m_size; }; +/************************************************ + * !!! W A R N I N G !!! * + * !!! A C H T U N G !!! * + * * + * This smart pointer class is NOT thread safe. * + * ONLY use in a single-threaded context! * + * * + ************************************************/ template class SharedBuffer { @@ -197,6 +130,7 @@ class SharedBuffer else data = NULL; refcount = new unsigned int; + memset(data,0,sizeof(T)*m_size); (*refcount) = 1; } SharedBuffer(const SharedBuffer &buffer) @@ -257,7 +191,7 @@ class SharedBuffer } T & operator[](unsigned int i) const { - //assert(i < m_size) + assert(i < m_size); return data[i]; } T * operator*() const @@ -279,8 +213,7 @@ class SharedBuffer (*refcount)--; if(*refcount == 0) { - if(data) - delete[] data; + delete[] data; delete refcount; } }