Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef nsStringBuffer_h__ |
michael@0 | 8 | #define nsStringBuffer_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/Atomics.h" |
michael@0 | 11 | #include "mozilla/MemoryReporting.h" |
michael@0 | 12 | |
michael@0 | 13 | template<class T> struct already_AddRefed; |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * This structure precedes the string buffers "we" allocate. It may be the |
michael@0 | 17 | * case that nsTAString::mData does not point to one of these special |
michael@0 | 18 | * buffers. The mFlags member variable distinguishes the buffer type. |
michael@0 | 19 | * |
michael@0 | 20 | * When this header is in use, it enables reference counting, and capacity |
michael@0 | 21 | * tracking. NOTE: A string buffer can be modified only if its reference |
michael@0 | 22 | * count is 1. |
michael@0 | 23 | */ |
michael@0 | 24 | class nsStringBuffer |
michael@0 | 25 | { |
michael@0 | 26 | private: |
michael@0 | 27 | friend class CheckStaticAtomSizes; |
michael@0 | 28 | |
michael@0 | 29 | mozilla::Atomic<int32_t> mRefCount; |
michael@0 | 30 | uint32_t mStorageSize; |
michael@0 | 31 | |
michael@0 | 32 | public: |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * Allocates a new string buffer, with given size in bytes and a |
michael@0 | 36 | * reference count of one. When the string buffer is no longer needed, |
michael@0 | 37 | * it should be released via Release. |
michael@0 | 38 | * |
michael@0 | 39 | * It is up to the caller to set the bytes corresponding to the string |
michael@0 | 40 | * buffer by calling the Data method to fetch the raw data pointer. Care |
michael@0 | 41 | * must be taken to properly null terminate the character array. The |
michael@0 | 42 | * storage size can be greater than the length of the actual string |
michael@0 | 43 | * (i.e., it is not required that the null terminator appear in the last |
michael@0 | 44 | * storage unit of the string buffer's data). |
michael@0 | 45 | * |
michael@0 | 46 | * @return new string buffer or null if out of memory. |
michael@0 | 47 | */ |
michael@0 | 48 | static already_AddRefed<nsStringBuffer> Alloc(size_t storageSize); |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Resizes the given string buffer to the specified storage size. This |
michael@0 | 52 | * method must not be called on a readonly string buffer. Use this API |
michael@0 | 53 | * carefully!! |
michael@0 | 54 | * |
michael@0 | 55 | * This method behaves like the ANSI-C realloc function. (i.e., If the |
michael@0 | 56 | * allocation fails, null will be returned and the given string buffer |
michael@0 | 57 | * will remain unmodified.) |
michael@0 | 58 | * |
michael@0 | 59 | * @see IsReadonly |
michael@0 | 60 | */ |
michael@0 | 61 | static nsStringBuffer* Realloc(nsStringBuffer* buf, size_t storageSize); |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Increment the reference count on this string buffer. |
michael@0 | 65 | */ |
michael@0 | 66 | void NS_FASTCALL AddRef(); |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Decrement the reference count on this string buffer. The string |
michael@0 | 70 | * buffer will be destroyed when its reference count reaches zero. |
michael@0 | 71 | */ |
michael@0 | 72 | void NS_FASTCALL Release(); |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * This method returns the string buffer corresponding to the given data |
michael@0 | 76 | * pointer. The data pointer must have been returned previously by a |
michael@0 | 77 | * call to the nsStringBuffer::Data method. |
michael@0 | 78 | */ |
michael@0 | 79 | static nsStringBuffer* FromData(void* data) |
michael@0 | 80 | { |
michael@0 | 81 | return reinterpret_cast<nsStringBuffer*> (data) - 1; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * This method returns the data pointer for this string buffer. |
michael@0 | 86 | */ |
michael@0 | 87 | void* Data() const |
michael@0 | 88 | { |
michael@0 | 89 | return const_cast<char*> (reinterpret_cast<const char*> (this + 1)); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * This function returns the storage size of a string buffer in bytes. |
michael@0 | 94 | * This value is the same value that was originally passed to Alloc (or |
michael@0 | 95 | * Realloc). |
michael@0 | 96 | */ |
michael@0 | 97 | uint32_t StorageSize() const |
michael@0 | 98 | { |
michael@0 | 99 | return mStorageSize; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * If this method returns false, then the caller can be sure that their |
michael@0 | 104 | * reference to the string buffer is the only reference to the string |
michael@0 | 105 | * buffer, and therefore it has exclusive access to the string buffer and |
michael@0 | 106 | * associated data. However, if this function returns true, then other |
michael@0 | 107 | * consumers may rely on the data in this buffer being immutable and |
michael@0 | 108 | * other threads may access this buffer simultaneously. |
michael@0 | 109 | */ |
michael@0 | 110 | bool IsReadonly() const |
michael@0 | 111 | { |
michael@0 | 112 | return mRefCount > 1; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * The FromString methods return a string buffer for the given string |
michael@0 | 117 | * object or null if the string object does not have a string buffer. |
michael@0 | 118 | * The reference count of the string buffer is NOT incremented by these |
michael@0 | 119 | * methods. If the caller wishes to hold onto the returned value, then |
michael@0 | 120 | * the returned string buffer must have its reference count incremented |
michael@0 | 121 | * via a call to the AddRef method. |
michael@0 | 122 | */ |
michael@0 | 123 | static nsStringBuffer* FromString(const nsAString &str); |
michael@0 | 124 | static nsStringBuffer* FromString(const nsACString &str); |
michael@0 | 125 | |
michael@0 | 126 | /** |
michael@0 | 127 | * The ToString methods assign this string buffer to a given string |
michael@0 | 128 | * object. If the string object does not support sharable string |
michael@0 | 129 | * buffers, then its value will be set to a copy of the given string |
michael@0 | 130 | * buffer. Otherwise, these methods increment the reference count of the |
michael@0 | 131 | * given string buffer. It is important to specify the length (in |
michael@0 | 132 | * storage units) of the string contained in the string buffer since the |
michael@0 | 133 | * length of the string may be less than its storage size. The string |
michael@0 | 134 | * must have a null terminator at the offset specified by |len|. |
michael@0 | 135 | * |
michael@0 | 136 | * NOTE: storage size is measured in bytes even for wide strings; |
michael@0 | 137 | * however, string length is always measured in storage units |
michael@0 | 138 | * (2-byte units for wide strings). |
michael@0 | 139 | */ |
michael@0 | 140 | void ToString(uint32_t len, nsAString &str, |
michael@0 | 141 | bool aMoveOwnership = false); |
michael@0 | 142 | void ToString(uint32_t len, nsACString &str, |
michael@0 | 143 | bool aMoveOwnership = false); |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * This measures the size. It should only be used if the StringBuffer is |
michael@0 | 147 | * unshared. This is checked. |
michael@0 | 148 | */ |
michael@0 | 149 | size_t SizeOfIncludingThisMustBeUnshared(mozilla::MallocSizeOf aMallocSizeOf) const; |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * This measures the size only if the StringBuffer is unshared. |
michael@0 | 153 | */ |
michael@0 | 154 | size_t SizeOfIncludingThisIfUnshared(mozilla::MallocSizeOf aMallocSizeOf) const; |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * This measures the size regardless of whether the StringBuffer is |
michael@0 | 158 | * unshared. |
michael@0 | 159 | * |
michael@0 | 160 | * WARNING: Only use this if you really know what you are doing, because |
michael@0 | 161 | * it can easily lead to double-counting strings. If you do use them, |
michael@0 | 162 | * please explain clearly in a comment why it's safe and won't lead to |
michael@0 | 163 | * double-counting. |
michael@0 | 164 | */ |
michael@0 | 165 | size_t SizeOfIncludingThisEvenIfShared(mozilla::MallocSizeOf aMallocSizeOf) const; |
michael@0 | 166 | }; |
michael@0 | 167 | |
michael@0 | 168 | #endif /* !defined(nsStringBuffer_h__ */ |