Bug 1824490 - Use the end page value rather than the start page value of the previous...
[gecko.git] / xpcom / string / nsStringBuffer.h
blob3c929599322c877cd2df995666acdb327ebba798
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsStringBuffer_h__
8 #define nsStringBuffer_h__
10 #include <atomic>
11 #include "mozilla/MemoryReporting.h"
12 #include "nsStringFwd.h"
14 template <class T>
15 struct already_AddRefed;
17 /**
18 * This structure precedes the string buffers "we" allocate. It may be the
19 * case that nsTAString::mData does not point to one of these special
20 * buffers. The mDataFlags member variable distinguishes the buffer type.
22 * When this header is in use, it enables reference counting, and capacity
23 * tracking. NOTE: A string buffer can be modified only if its reference
24 * count is 1.
26 class nsStringBuffer {
27 private:
28 friend class CheckStaticAtomSizes;
30 std::atomic<uint32_t> mRefCount;
31 uint32_t mStorageSize;
33 public:
34 /**
35 * Allocates a new string buffer, with given size in bytes and a
36 * reference count of one. When the string buffer is no longer needed,
37 * it should be released via Release.
39 * It is up to the caller to set the bytes corresponding to the string
40 * buffer by calling the Data method to fetch the raw data pointer. Care
41 * must be taken to properly null terminate the character array. The
42 * storage size can be greater than the length of the actual string
43 * (i.e., it is not required that the null terminator appear in the last
44 * storage unit of the string buffer's data).
46 * @return new string buffer or null if out of memory.
48 static already_AddRefed<nsStringBuffer> Alloc(size_t aStorageSize);
50 /**
51 * Resizes the given string buffer to the specified storage size. This
52 * method must not be called on a readonly string buffer. Use this API
53 * carefully!!
55 * This method behaves like the ANSI-C realloc function. (i.e., If the
56 * allocation fails, null will be returned and the given string buffer
57 * will remain unmodified.)
59 * @see IsReadonly
61 static nsStringBuffer* Realloc(nsStringBuffer* aBuf, size_t aStorageSize);
63 /**
64 * Increment the reference count on this string buffer.
66 void NS_FASTCALL AddRef();
68 /**
69 * Decrement the reference count on this string buffer. The string
70 * buffer will be destroyed when its reference count reaches zero.
72 void NS_FASTCALL Release();
74 /**
75 * This method returns the string buffer corresponding to the given data
76 * pointer. The data pointer must have been returned previously by a
77 * call to the nsStringBuffer::Data method.
79 static nsStringBuffer* FromData(void* aData) {
80 return reinterpret_cast<nsStringBuffer*>(aData) - 1;
83 /**
84 * This method returns the data pointer for this string buffer.
86 void* Data() const {
87 return const_cast<char*>(reinterpret_cast<const char*>(this + 1));
90 /**
91 * This function returns the storage size of a string buffer in bytes.
92 * This value is the same value that was originally passed to Alloc (or
93 * Realloc).
95 uint32_t StorageSize() const { return mStorageSize; }
97 /**
98 * If this method returns false, then the caller can be sure that their
99 * reference to the string buffer is the only reference to the string
100 * buffer, and therefore it has exclusive access to the string buffer and
101 * associated data. However, if this function returns true, then other
102 * consumers may rely on the data in this buffer being immutable and
103 * other threads may access this buffer simultaneously.
105 bool IsReadonly() const {
106 // This doesn't lead to the destruction of the buffer, so we don't
107 // need to perform acquire memory synchronization for the normal
108 // reason that a reference count needs acquire synchronization
109 // (ensuring that all writes to the object made on other threads are
110 // visible to the thread destroying the object).
112 // We then need to consider the possibility that there were prior
113 // writes to the buffer on a different thread: one that has either
114 // since released its reference count, or one that also has access
115 // to this buffer through the same reference. There are two ways
116 // for that to happen: either the buffer pointer or a data structure
117 // (e.g., string object) pointing to the buffer was transferred from
118 // one thread to another, or the data structure pointing to the
119 // buffer was already visible on both threads. In the first case
120 // (transfer), the transfer of data from one thread to another would
121 // have handled the memory synchronization. In the latter case
122 // (data structure visible on both threads), the caller needed some
123 // sort of higher level memory synchronization to protect against
124 // the string object being mutated at the same time on multiple
125 // threads.
127 // See bug 1603504. TSan might complain about a race when using
128 // memory_order_relaxed, so use memory_order_acquire for making TSan
129 // happy.
130 #if defined(MOZ_TSAN)
131 return mRefCount.load(std::memory_order_acquire) > 1;
132 #else
133 return mRefCount.load(std::memory_order_relaxed) > 1;
134 #endif
138 * The FromString methods return a string buffer for the given string
139 * object or null if the string object does not have a string buffer.
140 * The reference count of the string buffer is NOT incremented by these
141 * methods. If the caller wishes to hold onto the returned value, then
142 * the returned string buffer must have its reference count incremented
143 * via a call to the AddRef method.
145 static nsStringBuffer* FromString(const nsAString& aStr);
146 static nsStringBuffer* FromString(const nsACString& aStr);
149 * The ToString methods assign this string buffer to a given string
150 * object. If the string object does not support sharable string
151 * buffers, then its value will be set to a copy of the given string
152 * buffer. Otherwise, these methods increment the reference count of the
153 * given string buffer. It is important to specify the length (in
154 * storage units) of the string contained in the string buffer since the
155 * length of the string may be less than its storage size. The string
156 * must have a null terminator at the offset specified by |len|.
158 * NOTE: storage size is measured in bytes even for wide strings;
159 * however, string length is always measured in storage units
160 * (2-byte units for wide strings).
162 void ToString(uint32_t aLen, nsAString& aStr, bool aMoveOwnership = false);
163 void ToString(uint32_t aLen, nsACString& aStr, bool aMoveOwnership = false);
166 * This measures the size only if the StringBuffer is unshared.
168 size_t SizeOfIncludingThisIfUnshared(
169 mozilla::MallocSizeOf aMallocSizeOf) const;
172 * This measures the size regardless of whether the StringBuffer is
173 * unshared.
175 * WARNING: Only use this if you really know what you are doing, because
176 * it can easily lead to double-counting strings. If you do use them,
177 * please explain clearly in a comment why it's safe and won't lead to
178 * double-counting.
180 size_t SizeOfIncludingThisEvenIfShared(
181 mozilla::MallocSizeOf aMallocSizeOf) const;
184 #endif /* !defined(nsStringBuffer_h__ */