diff --git a/src/nvcore/Prefetch.h b/src/nvcore/Prefetch.h index aa0bcfc..71bd0ed 100644 --- a/src/nvcore/Prefetch.h +++ b/src/nvcore/Prefetch.h @@ -24,7 +24,7 @@ __forceinline void nvPrefetch(const void * mem) #else // NV_CC_MSVC // do nothing in other case. -#define piPrefetch(ptr) +#define nvPrefetch(ptr) #endif // NV_CC_MSVC diff --git a/src/nvcore/Ptr.h b/src/nvcore/Ptr.h index 4b35718..b5e9fe7 100644 --- a/src/nvcore/Ptr.h +++ b/src/nvcore/Ptr.h @@ -48,8 +48,11 @@ public: /** Delete owned pointer and assign new one. */ void operator=( T * p ) { - delete m_ptr; - m_ptr = p; + if (p != m_ptr) + { + delete m_ptr; + m_ptr = p; + } } /** Member access. */ diff --git a/src/nvcore/ThreadLocalStorage.h b/src/nvcore/ThreadLocalStorage.h new file mode 100755 index 0000000..f71a07d --- /dev/null +++ b/src/nvcore/ThreadLocalStorage.h @@ -0,0 +1,73 @@ +// This code is in the public domain -- castanyo@yahoo.es + +#ifndef NV_CORE_THREADLOCALSTORAGE_H +#define NV_CORE_THREADLOCALSTORAGE_H + +#include + +// ThreadLocal context; +// +// context.allocate(); +// +// context = new Context(); +// context->member(); +// context = NULL; +// +// context.free(); + +#if NV_CC_GNUC + +#elif NV_CC_MSVC + +template +class ThreadLocal +{ +public: + ThreadLocal() : index(0) {} + ~ThreadLocal() { nvCheck(index == 0); } + + void allocate() + { + index = TlsAlloc(); + } + void free() + { + delete ptr(); + TlsFree(index); + index = 0; + } + bool isValid() + { + return index != 0; + } + + void operator=( T * p ) + { + if (p != ptr()) + { + delete ptr(); + TlsSetValue(index, p); + } + } + + T * operator -> () const + { + return ptr(); + } + + T & operator*() const + { + return *ptr(); + } + + T * ptr() const { + return static_cast(TlsGetValue(index)); + } + + DWORD index; +}; + + +#endif // NV_CC_MSVC + +#endif // NV_CORE_THREADLOCALSTORAGE_H