* Made _Allocate() static.
* Moved the private inline methods up in the source file, so they can actually be inlined. * UnlockBuffer(): Removed superfluous "if". Maybe the one who wrote it can have a look and check whether something else was intended originally. * _MakeWritable() (both versions): Removed the superfluous ref count increment and the matching decrements. * _Resize(): Fixed ref count ASSERT. It would always be triggered when called from UnlockBuffer(), since the ref count is -1 in that case. * Clarified some comments. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31662 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -246,7 +246,7 @@ private:
|
|||||||
// Management
|
// Management
|
||||||
status_t _MakeWritable();
|
status_t _MakeWritable();
|
||||||
status_t _MakeWritable(int32 length, bool copy);
|
status_t _MakeWritable(int32 length, bool copy);
|
||||||
char* _Allocate(int32 length);
|
static char* _Allocate(int32 length);
|
||||||
char* _Resize(int32 length);
|
char* _Resize(int32 length);
|
||||||
void _Init(const char* src, int32 length);
|
void _Init(const char* src, int32 length);
|
||||||
char* _Clone(const char* data, int32 length);
|
char* _Clone(const char* data, int32 length);
|
||||||
|
|||||||
+41
-41
@@ -196,6 +196,27 @@ BStringRef::operator&()
|
|||||||
// #pragma mark - BString
|
// #pragma mark - BString
|
||||||
|
|
||||||
|
|
||||||
|
inline vint32&
|
||||||
|
BString::_ReferenceCount()
|
||||||
|
{
|
||||||
|
return data_reference_count(fPrivateData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline const vint32&
|
||||||
|
BString::_ReferenceCount() const
|
||||||
|
{
|
||||||
|
return data_reference_count(fPrivateData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline bool
|
||||||
|
BString::_IsShareable() const
|
||||||
|
{
|
||||||
|
return fPrivateData != NULL && _ReferenceCount() >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BString::BString()
|
BString::BString()
|
||||||
:
|
:
|
||||||
fPrivateData(NULL)
|
fPrivateData(NULL)
|
||||||
@@ -1466,12 +1487,10 @@ BString::LockBuffer(int32 maxLength)
|
|||||||
BString&
|
BString&
|
||||||
BString::UnlockBuffer(int32 length)
|
BString::UnlockBuffer(int32 length)
|
||||||
{
|
{
|
||||||
if (length > 0) {
|
if (length > 0)
|
||||||
if (length)
|
length = min_clamp0(length, Length());
|
||||||
length = min_clamp0(length, Length());
|
else
|
||||||
} else {
|
|
||||||
length = fPrivateData == NULL ? 0 : strlen(fPrivateData);
|
length = fPrivateData == NULL ? 0 : strlen(fPrivateData);
|
||||||
}
|
|
||||||
|
|
||||||
if (_Resize(length) != NULL) {
|
if (_Resize(length) != NULL) {
|
||||||
fPrivateData[length] = '\0';
|
fPrivateData[length] = '\0';
|
||||||
@@ -1712,10 +1731,10 @@ BString::operator<<(float f)
|
|||||||
status_t
|
status_t
|
||||||
BString::_MakeWritable()
|
BString::_MakeWritable()
|
||||||
{
|
{
|
||||||
if (atomic_add(&_ReferenceCount(), 1) > 1) {
|
if (atomic_get(&_ReferenceCount()) > 1) {
|
||||||
// It might be shared, and this requires special treatment
|
// It might be shared, and this requires special treatment
|
||||||
char* newData = _Clone(fPrivateData, Length());
|
char* newData = _Clone(fPrivateData, Length());
|
||||||
if (atomic_add(&_ReferenceCount(), -2) == 1) {
|
if (atomic_add(&_ReferenceCount(), -1) == 1) {
|
||||||
// someone else left, we were the last owner
|
// someone else left, we were the last owner
|
||||||
_FreePrivateData();
|
_FreePrivateData();
|
||||||
}
|
}
|
||||||
@@ -1723,14 +1742,14 @@ BString::_MakeWritable()
|
|||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
fPrivateData = newData;
|
fPrivateData = newData;
|
||||||
} else
|
}
|
||||||
atomic_add(&_ReferenceCount(), -1);
|
|
||||||
|
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! Makes this string writable, and resizes the buffer to \a length bytes.
|
/*! Makes this string writable, and resizes the buffer to \a length bytes (not
|
||||||
|
including the terminating null).
|
||||||
|
|
||||||
@param length The length of the new buffer in bytes.
|
@param length The length of the new buffer in bytes.
|
||||||
@param copy If true, the current string will be copied into the new string.
|
@param copy If true, the current string will be copied into the new string.
|
||||||
@@ -1740,35 +1759,37 @@ BString::_MakeWritable(int32 length, bool copy)
|
|||||||
{
|
{
|
||||||
char* newData = NULL;
|
char* newData = NULL;
|
||||||
|
|
||||||
if (atomic_add(&_ReferenceCount(), 1) > 1) {
|
if (atomic_get(&_ReferenceCount()) > 1) {
|
||||||
// we might share our data with someone else
|
// we might share our data with someone else
|
||||||
if (copy)
|
if (copy)
|
||||||
newData = _Clone(fPrivateData, length);
|
newData = _Clone(fPrivateData, length);
|
||||||
else
|
else
|
||||||
newData = _Allocate(length);
|
newData = _Allocate(length);
|
||||||
|
|
||||||
if (atomic_add(&_ReferenceCount(), -2) == 1) {
|
if (newData == NULL)
|
||||||
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
|
if (atomic_add(&_ReferenceCount(), -1) == 1) {
|
||||||
// someone else left, we were the last owner
|
// someone else left, we were the last owner
|
||||||
_FreePrivateData();
|
_FreePrivateData();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// we don't share our data with someone else
|
// we don't share our data with someone else
|
||||||
atomic_add(&_ReferenceCount(), -1);
|
|
||||||
newData = _Resize(length);
|
newData = _Resize(length);
|
||||||
}
|
|
||||||
|
|
||||||
if (newData == NULL)
|
if (newData == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
fPrivateData = newData;
|
fPrivateData = newData;
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! Allocates a new private data buffer with the space to store \a length bytes,
|
/*! Allocates a new private data buffer with the space to store \a length bytes
|
||||||
but does not change the current one.
|
(not including the terminating null).
|
||||||
*/
|
*/
|
||||||
char*
|
/*static*/ char*
|
||||||
BString::_Allocate(int32 length)
|
BString::_Allocate(int32 length)
|
||||||
{
|
{
|
||||||
if (length < 0)
|
if (length < 0)
|
||||||
@@ -1795,7 +1816,7 @@ BString::_Allocate(int32 length)
|
|||||||
char*
|
char*
|
||||||
BString::_Resize(int32 length)
|
BString::_Resize(int32 length)
|
||||||
{
|
{
|
||||||
ASSERT(_ReferenceCount() == 1);
|
ASSERT(_ReferenceCount() == 1 || _ReferenceCount() == -1);
|
||||||
|
|
||||||
if (length == Length())
|
if (length == Length())
|
||||||
return fPrivateData;
|
return fPrivateData;
|
||||||
@@ -1879,27 +1900,6 @@ BString::_SetLength(int32 length)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline vint32&
|
|
||||||
BString::_ReferenceCount()
|
|
||||||
{
|
|
||||||
return data_reference_count(fPrivateData);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline const vint32&
|
|
||||||
BString::_ReferenceCount() const
|
|
||||||
{
|
|
||||||
return data_reference_count(fPrivateData);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
inline bool
|
|
||||||
BString::_IsShareable() const
|
|
||||||
{
|
|
||||||
return fPrivateData != NULL && _ReferenceCount() >= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BString::_FreePrivateData()
|
BString::_FreePrivateData()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user