haiku/headers/private/support/StringPrivate.h
Pawel Dziepak 077c84eb27 kernel: atomic_*() functions rework
* No need for the atomically changed variables to be declared as
   volatile.
 * Drop support for atomically getting and setting unaligned data.
 * Introduce atomic_get_and_set[64]() which works the same as
   atomic_set[64]() used to. atomic_set[64]() does not return the
   previous value anymore.
2013-11-05 22:32:59 +01:00

81 lines
1.2 KiB
C++

/*
* Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#ifndef _SUPPORT_BSTRING_PRIVATE_H_
#define _SUPPORT_BSTRING_PRIVATE_H_
#include <stdlib.h>
#include <String.h>
class BString::Private {
public:
static const uint32 kPrivateDataOffset = 2 * sizeof(int32);
public:
Private(const BString& string)
:
fString(string)
{
}
char* Data()
{
return fString.fPrivateData;
}
bool IsShareable() const
{
return fString._IsShareable();
}
static int32& DataRefCount(char* data)
{
return *(((int32 *)data) - 2);
}
int32& DataRefCount()
{
return DataRefCount(Data());
}
static int32& DataLength(char* data)
{
return *(((int32*)data) - 1);
}
int32& DataLength()
{
return DataLength(Data());
}
static void IncrementDataRefCount(char* data)
{
if (data != NULL)
atomic_add(&DataRefCount(data), 1);
}
static void DecrementDataRefCount(char* data)
{
if (data != NULL) {
if (atomic_add(&DataRefCount(data), -1) == 1)
free(data - kPrivateDataOffset);
}
}
static BString StringFromData(char* data)
{
return BString(data, BString::PRIVATE_DATA);
}
private:
const BString& fString;
};
#endif // _SUPPORT_BSTRING_PRIVATE_H_