[taken from the Zeta-headers]

Fix a race condition in std::string allocation/deallocation code.


git-svn-id: file:///srv/svn/repos/haiku/trunk/buildtools@9614 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe 2004-10-28 18:33:16 +00:00
parent 98cc8eab03
commit d2046ed9e3

View File

@ -61,6 +61,13 @@ extern void __length_error (const char *);
#endif #endif
#ifdef __BEOS__
// Needed for atomic_add():
typedef long int32;
typedef volatile long vint32;
extern "C" int32 atomic_add(vint32* value, int32 addvalue);
#endif /* __BEOS__ */
template <class charT, class traits = string_char_traits<charT>, template <class charT, class traits = string_char_traits<charT>,
class Allocator = alloc > class Allocator = alloc >
class basic_string class basic_string
@ -72,6 +79,10 @@ private:
charT* data () { return reinterpret_cast<charT *>(this + 1); } charT* data () { return reinterpret_cast<charT *>(this + 1); }
charT& operator[] (size_t s) { return data () [s]; } charT& operator[] (size_t s) { return data () [s]; }
#ifdef __BEOS__
charT* grab () { if (selfish) return clone (); atomic_add((vint32*) &ref, 1); return data (); }
void release() { if (atomic_add((int32*) &ref, -1) == 1) delete this; }
#else
charT* grab () { if (selfish) return clone (); ++ref; return data (); } charT* grab () { if (selfish) return clone (); ++ref; return data (); }
#if defined __i486__ || defined __i586__ || defined __i686__ #if defined __i486__ || defined __i586__ || defined __i686__
void release () void release ()
@ -108,7 +119,7 @@ private:
#else #else
void release () { if (--ref == 0) delete this; } void release () { if (--ref == 0) delete this; }
#endif #endif
#endif /* __BEOS__ */
inline static void * operator new (size_t, size_t); inline static void * operator new (size_t, size_t);
inline static void operator delete (void *); inline static void operator delete (void *);
inline static Rep* create (size_t); inline static Rep* create (size_t);