1 /* 2 * Copyright (C) 2006 Apple Computer, 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 27 #ifndef SharedBuffer_h 28 #define SharedBuffer_h 29 30 #include "platform/PlatformExport.h" 31 #include "third_party/skia/include/core/SkData.h" 32 #include "wtf/ArrayBuffer.h" 33 #include "wtf/Forward.h" 34 #include "wtf/OwnPtr.h" 35 #include "wtf/RefCounted.h" 36 #include "wtf/Vector.h" 37 #include "wtf/text/WTFString.h" 38 39 namespace WebCore { 40 41 class PurgeableBuffer; 42 43 class PLATFORM_EXPORT SharedBuffer : public RefCounted<SharedBuffer> { 44 public: 45 static PassRefPtr<SharedBuffer> create() { return adoptRef(new SharedBuffer); } 46 static PassRefPtr<SharedBuffer> create(size_t size) { return adoptRef(new SharedBuffer(size)); } 47 static PassRefPtr<SharedBuffer> create(const char* c, int i) { return adoptRef(new SharedBuffer(c, i)); } 48 static PassRefPtr<SharedBuffer> create(const unsigned char* c, int i) { return adoptRef(new SharedBuffer(c, i)); } 49 50 static PassRefPtr<SharedBuffer> adoptVector(Vector<char>& vector); 51 52 // The buffer must be in non-purgeable state before adopted to a SharedBuffer. 53 // It will stay that way until released. 54 static PassRefPtr<SharedBuffer> adoptPurgeableBuffer(PassOwnPtr<PurgeableBuffer>); 55 56 ~SharedBuffer(); 57 58 // Calling this function will force internal segmented buffers 59 // to be merged into a flat buffer. Use getSomeData() whenever possible 60 // for better performance. 61 const char* data() const; 62 63 void moveTo(Vector<char>&); 64 65 unsigned size() const; 66 67 bool isEmpty() const { return !size(); } 68 69 void append(SharedBuffer*); 70 void append(const char*, unsigned); 71 void append(const Vector<char>&); 72 73 void clear(); 74 75 PassRefPtr<SharedBuffer> copy() const; 76 77 bool hasPurgeableBuffer() const { return m_purgeableBuffer.get(); } 78 79 // Ensure this buffer has no other clients before calling this. 80 PassOwnPtr<PurgeableBuffer> releasePurgeableBuffer(); 81 82 // Return the number of consecutive bytes after "position". "data" 83 // points to the first byte. 84 // Return 0 when no more data left. 85 // When extracting all data with getSomeData(), the caller should 86 // repeat calling it until it returns 0. 87 // Usage: 88 // const char* segment; 89 // unsigned pos = 0; 90 // while (unsigned length = sharedBuffer->getSomeData(segment, pos)) { 91 // // Use the data. for example: decoder->decode(segment, length); 92 // pos += length; 93 // } 94 unsigned getSomeData(const char*& data, unsigned position = 0) const; 95 96 void createPurgeableBuffer() const; 97 98 // Creates an ArrayBuffer and copies this SharedBuffer's contents to that 99 // ArrayBuffer without merging segmented buffers into a flat buffer. 100 PassRefPtr<ArrayBuffer> getAsArrayBuffer() const; 101 102 // Creates an SkData and copies this SharedBuffer's contents to that 103 // SkData without merging segmented buffers into a flat buffer. 104 PassRefPtr<SkData> getAsSkData() const; 105 106 private: 107 SharedBuffer(); 108 explicit SharedBuffer(size_t); 109 SharedBuffer(const char*, int); 110 SharedBuffer(const unsigned char*, int); 111 112 // Calling this function will force internal segmented buffers 113 // to be merged into a flat buffer. Use getSomeData() whenever possible 114 // for better performance. 115 // As well, be aware that this method does *not* return any purgeable 116 // memory, which can be a source of bugs. 117 const Vector<char>& buffer() const; 118 119 unsigned m_size; 120 mutable Vector<char> m_buffer; 121 mutable Vector<char*> m_segments; 122 mutable OwnPtr<PurgeableBuffer> m_purgeableBuffer; 123 }; 124 125 } // namespace WebCore 126 127 #endif // SharedBuffer_h 128