btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / support / StringPrivate.h
blob0ad274c8c4b5e7887e11f951a86c5e560470acb1
1 /*
2 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _SUPPORT_BSTRING_PRIVATE_H_
6 #define _SUPPORT_BSTRING_PRIVATE_H_
9 #include <stdlib.h>
11 #include <String.h>
14 class BString::Private {
15 public:
16 static const uint32 kPrivateDataOffset = 2 * sizeof(int32);
18 public:
19 Private(const BString& string)
21 fString(string)
25 char* Data()
27 return fString.fPrivateData;
30 bool IsShareable() const
32 return fString._IsShareable();
35 static int32& DataRefCount(char* data)
37 return *(((int32 *)data) - 2);
40 int32& DataRefCount()
42 return DataRefCount(Data());
45 static int32& DataLength(char* data)
47 return *(((int32*)data) - 1);
50 int32& DataLength()
52 return DataLength(Data());
55 static void IncrementDataRefCount(char* data)
57 if (data != NULL)
58 atomic_add(&DataRefCount(data), 1);
61 static void DecrementDataRefCount(char* data)
63 if (data != NULL) {
64 if (atomic_add(&DataRefCount(data), -1) == 1)
65 free(data - kPrivateDataOffset);
69 static BString StringFromData(char* data)
71 return BString(data, BString::PRIVATE_DATA);
74 private:
75 const BString& fString;
79 #endif // _SUPPORT_BSTRING_PRIVATE_H_