* _Realloc() did not initialize the reference count either.

* _Alloc() can now preserve the original reference count, only _Clone()
  still initializes it to 1. As Karsten pointed out, this is necessary to
  preserve the "shareable" status of the private data.
* I hope that's finally it. What happened to our testing suite, anyway? :-)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24355 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-03-10 22:56:27 +00:00
parent ffac272e35
commit 4b83262008
2 changed files with 11 additions and 4 deletions
+1 -1
View File
@@ -221,7 +221,7 @@ private:
// Management
status_t _Detach();
char* _Alloc(int32 length);
char* _Alloc(int32 length, bool adoptReferenceCount = true);
char* _Realloc(int32 length);
void _Init(const char* src, int32 length);
char* _Clone(const char* data, int32 length);
+10 -3
View File
@@ -1679,7 +1679,7 @@ BString::_Detach()
char*
BString::_Alloc(int32 length)
BString::_Alloc(int32 length, bool adoptReferenceCount)
{
if (length < 0)
return NULL;
@@ -1692,7 +1692,11 @@ BString::_Alloc(int32 length)
newData[length] = '\0';
// initialize reference count & length
*(((vint32*)newData) - 2) = 1;
int32 referenceCount = 1;
if (adoptReferenceCount && fPrivateData != NULL)
referenceCount = _ReferenceCount();
*(((vint32*)newData) - 2) = referenceCount;
*(((int32*)newData) - 1) = length & 0x7fffffff;
return newData;
@@ -1711,12 +1715,15 @@ BString::_Realloc(int32 length)
dataPtr = (char*)realloc(dataPtr, length + kPrivateDataOffset + 1);
if (dataPtr) {
int32 oldReferenceCount = _ReferenceCount();
dataPtr += kPrivateDataOffset;
fPrivateData = dataPtr;
fPrivateData[length] = '\0';
_SetLength(length);
_ReferenceCount() = oldReferenceCount;
}
return dataPtr;
}
@@ -1734,7 +1741,7 @@ BString::_Init(const char* src, int32 length)
char*
BString::_Clone(const char* data, int32 length)
{
char* newData = _Alloc(length);
char* newData = _Alloc(length, false);
if (newData == NULL)
return NULL;