SharedBuffer.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "SharedBuffer.h"
  28. #include "PurgeableBuffer.h"
  29. #include <wtf/PassOwnPtr.h>
  30. #include <wtf/unicode/UTF8.h>
  31. #include <wtf/unicode/Unicode.h>
  32. using namespace std;
  33. namespace WebCore {
  34. static const unsigned segmentSize = 0x1000;
  35. static const unsigned segmentPositionMask = 0x0FFF;
  36. static inline unsigned segmentIndex(unsigned position)
  37. {
  38. return position / segmentSize;
  39. }
  40. static inline unsigned offsetInSegment(unsigned position)
  41. {
  42. return position & segmentPositionMask;
  43. }
  44. static inline char* allocateSegment()
  45. {
  46. return static_cast<char*>(fastMalloc(segmentSize));
  47. }
  48. static inline void freeSegment(char* p)
  49. {
  50. fastFree(p);
  51. }
  52. SharedBuffer::SharedBuffer()
  53. : m_size(0)
  54. {
  55. }
  56. SharedBuffer::SharedBuffer(size_t size)
  57. : m_size(size)
  58. , m_buffer(size)
  59. {
  60. }
  61. SharedBuffer::SharedBuffer(const char* data, int size)
  62. : m_size(0)
  63. {
  64. // FIXME: Use unsigned consistently, and check for invalid casts when calling into SharedBuffer from other code.
  65. if (size < 0)
  66. CRASH();
  67. append(data, size);
  68. }
  69. SharedBuffer::SharedBuffer(const unsigned char* data, int size)
  70. : m_size(0)
  71. {
  72. // FIXME: Use unsigned consistently, and check for invalid casts when calling into SharedBuffer from other code.
  73. if (size < 0)
  74. CRASH();
  75. append(reinterpret_cast<const char*>(data), size);
  76. }
  77. SharedBuffer::~SharedBuffer()
  78. {
  79. clear();
  80. }
  81. PassRefPtr<SharedBuffer> SharedBuffer::adoptVector(Vector<char>& vector)
  82. {
  83. RefPtr<SharedBuffer> buffer = create();
  84. buffer->m_buffer.swap(vector);
  85. buffer->m_size = buffer->m_buffer.size();
  86. return buffer.release();
  87. }
  88. PassRefPtr<SharedBuffer> SharedBuffer::adoptPurgeableBuffer(PassOwnPtr<PurgeableBuffer> purgeableBuffer)
  89. {
  90. ASSERT(!purgeableBuffer->isPurgeable());
  91. RefPtr<SharedBuffer> buffer = create();
  92. buffer->m_purgeableBuffer = purgeableBuffer;
  93. return buffer.release();
  94. }
  95. unsigned SharedBuffer::size() const
  96. {
  97. if (hasPlatformData())
  98. return platformDataSize();
  99. if (m_purgeableBuffer)
  100. return m_purgeableBuffer->size();
  101. return m_size;
  102. }
  103. void SharedBuffer::createPurgeableBuffer() const
  104. {
  105. if (m_purgeableBuffer)
  106. return;
  107. if (hasPlatformData())
  108. return;
  109. #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
  110. if (singleDataArrayBuffer())
  111. return;
  112. #endif
  113. m_purgeableBuffer = PurgeableBuffer::create(buffer().data(), m_size);
  114. }
  115. const char* SharedBuffer::data() const
  116. {
  117. if (hasPlatformData())
  118. return platformData();
  119. #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
  120. if (const char* buffer = singleDataArrayBuffer())
  121. return buffer;
  122. #endif
  123. if (m_purgeableBuffer)
  124. return m_purgeableBuffer->data();
  125. return this->buffer().data();
  126. }
  127. void SharedBuffer::append(SharedBuffer* data)
  128. {
  129. const char* segment;
  130. size_t position = 0;
  131. while (size_t length = data->getSomeData(segment, position)) {
  132. append(segment, length);
  133. position += length;
  134. }
  135. }
  136. void SharedBuffer::append(const char* data, unsigned length)
  137. {
  138. ASSERT(!m_purgeableBuffer);
  139. if (!length)
  140. return;
  141. maybeTransferPlatformData();
  142. unsigned positionInSegment = offsetInSegment(m_size - m_buffer.size());
  143. m_size += length;
  144. if (m_size <= segmentSize) {
  145. // No need to use segments for small resource data
  146. if (m_buffer.isEmpty())
  147. m_buffer.reserveInitialCapacity(length);
  148. m_buffer.append(data, length);
  149. return;
  150. }
  151. char* segment;
  152. if (!positionInSegment) {
  153. segment = allocateSegment();
  154. m_segments.append(segment);
  155. } else
  156. segment = m_segments.last() + positionInSegment;
  157. unsigned segmentFreeSpace = segmentSize - positionInSegment;
  158. unsigned bytesToCopy = min(length, segmentFreeSpace);
  159. for (;;) {
  160. memcpy(segment, data, bytesToCopy);
  161. if (static_cast<unsigned>(length) == bytesToCopy)
  162. break;
  163. length -= bytesToCopy;
  164. data += bytesToCopy;
  165. segment = allocateSegment();
  166. m_segments.append(segment);
  167. bytesToCopy = min(length, segmentSize);
  168. }
  169. }
  170. void SharedBuffer::append(const Vector<char>& data)
  171. {
  172. append(data.data(), data.size());
  173. }
  174. void SharedBuffer::clear()
  175. {
  176. clearPlatformData();
  177. for (unsigned i = 0; i < m_segments.size(); ++i)
  178. freeSegment(m_segments[i]);
  179. m_segments.clear();
  180. m_size = 0;
  181. m_buffer.clear();
  182. m_purgeableBuffer.clear();
  183. #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
  184. m_dataArray.clear();
  185. #endif
  186. }
  187. PassRefPtr<SharedBuffer> SharedBuffer::copy() const
  188. {
  189. RefPtr<SharedBuffer> clone(adoptRef(new SharedBuffer));
  190. if (m_purgeableBuffer || hasPlatformData()) {
  191. clone->append(data(), size());
  192. return clone;
  193. }
  194. clone->m_size = m_size;
  195. clone->m_buffer.reserveCapacity(m_size);
  196. clone->m_buffer.append(m_buffer.data(), m_buffer.size());
  197. for (unsigned i = 0; i < m_segments.size(); ++i)
  198. clone->m_buffer.append(m_segments[i], segmentSize);
  199. return clone;
  200. }
  201. PassOwnPtr<PurgeableBuffer> SharedBuffer::releasePurgeableBuffer()
  202. {
  203. ASSERT(hasOneRef());
  204. return m_purgeableBuffer.release();
  205. }
  206. const Vector<char>& SharedBuffer::buffer() const
  207. {
  208. unsigned bufferSize = m_buffer.size();
  209. if (m_size > bufferSize) {
  210. m_buffer.resize(m_size);
  211. char* destination = m_buffer.data() + bufferSize;
  212. unsigned bytesLeft = m_size - bufferSize;
  213. for (unsigned i = 0; i < m_segments.size(); ++i) {
  214. unsigned bytesToCopy = min(bytesLeft, segmentSize);
  215. memcpy(destination, m_segments[i], bytesToCopy);
  216. destination += bytesToCopy;
  217. bytesLeft -= bytesToCopy;
  218. freeSegment(m_segments[i]);
  219. }
  220. m_segments.clear();
  221. #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
  222. copyDataArrayAndClear(destination, bytesLeft);
  223. #endif
  224. }
  225. return m_buffer;
  226. }
  227. unsigned SharedBuffer::getSomeData(const char*& someData, unsigned position) const
  228. {
  229. unsigned totalSize = size();
  230. if (position >= totalSize) {
  231. someData = 0;
  232. return 0;
  233. }
  234. if (hasPlatformData() || m_purgeableBuffer) {
  235. ASSERT_WITH_SECURITY_IMPLICATION(position < size());
  236. someData = data() + position;
  237. return totalSize - position;
  238. }
  239. ASSERT_WITH_SECURITY_IMPLICATION(position < m_size);
  240. unsigned consecutiveSize = m_buffer.size();
  241. if (position < consecutiveSize) {
  242. someData = m_buffer.data() + position;
  243. return consecutiveSize - position;
  244. }
  245. position -= consecutiveSize;
  246. unsigned segments = m_segments.size();
  247. unsigned maxSegmentedSize = segments * segmentSize;
  248. unsigned segment = segmentIndex(position);
  249. if (segment < segments) {
  250. unsigned bytesLeft = totalSize - consecutiveSize;
  251. unsigned segmentedSize = min(maxSegmentedSize, bytesLeft);
  252. unsigned positionInSegment = offsetInSegment(position);
  253. someData = m_segments[segment] + positionInSegment;
  254. return segment == segments - 1 ? segmentedSize - position : segmentSize - positionInSegment;
  255. }
  256. #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
  257. ASSERT(maxSegmentedSize <= position);
  258. position -= maxSegmentedSize;
  259. return copySomeDataFromDataArray(someData, position);
  260. #else
  261. ASSERT_NOT_REACHED();
  262. return 0;
  263. #endif
  264. }
  265. #if !USE(CF) || PLATFORM(QT)
  266. inline void SharedBuffer::clearPlatformData()
  267. {
  268. }
  269. inline void SharedBuffer::maybeTransferPlatformData()
  270. {
  271. }
  272. inline bool SharedBuffer::hasPlatformData() const
  273. {
  274. return false;
  275. }
  276. inline const char* SharedBuffer::platformData() const
  277. {
  278. ASSERT_NOT_REACHED();
  279. return 0;
  280. }
  281. inline unsigned SharedBuffer::platformDataSize() const
  282. {
  283. ASSERT_NOT_REACHED();
  284. return 0;
  285. }
  286. #endif
  287. PassRefPtr<SharedBuffer> utf8Buffer(const String& string)
  288. {
  289. // Allocate a buffer big enough to hold all the characters.
  290. const int length = string.length();
  291. Vector<char> buffer(length * 3);
  292. // Convert to runs of 8-bit characters.
  293. char* p = buffer.data();
  294. const UChar* d = string.characters();
  295. WTF::Unicode::ConversionResult result = WTF::Unicode::convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), true);
  296. if (result != WTF::Unicode::conversionOK)
  297. return 0;
  298. buffer.shrink(p - buffer.data());
  299. return SharedBuffer::adoptVector(buffer);
  300. }
  301. } // namespace WebCore