2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef SharedBuffer_h
28 #define SharedBuffer_h
30 #include "platform/PlatformExport.h"
31 #include "platform/PurgeableVector.h"
32 #include "third_party/skia/include/core/SkData.h"
33 #include "wtf/Forward.h"
34 #include "wtf/OwnPtr.h"
35 #include "wtf/RefCounted.h"
36 #include "wtf/text/WTFString.h"
40 class PLATFORM_EXPORT SharedBuffer
: public RefCounted
<SharedBuffer
> {
42 enum : unsigned { kSegmentSize
= 0x1000 };
44 static PassRefPtr
<SharedBuffer
> create() { return adoptRef(new SharedBuffer
); }
45 static PassRefPtr
<SharedBuffer
> create(size_t size
) { return adoptRef(new SharedBuffer(size
)); }
46 static PassRefPtr
<SharedBuffer
> create(const char* c
, int i
) { return adoptRef(new SharedBuffer(c
, i
)); }
47 static PassRefPtr
<SharedBuffer
> create(const unsigned char* c
, int i
) { return adoptRef(new SharedBuffer(c
, i
)); }
49 static PassRefPtr
<SharedBuffer
> createPurgeable(const char* c
, unsigned size
) { return adoptRef(new SharedBuffer(c
, size
, PurgeableVector::Purgeable
)); }
51 static PassRefPtr
<SharedBuffer
> adoptVector(Vector
<char>&);
55 // Calling this function will force internal segmented buffers to be merged
56 // into a flat buffer. Use getSomeData() whenever possible for better
58 const char* data() const;
60 unsigned size() const;
62 bool isEmpty() const { return !size(); }
64 void append(PassRefPtr
<SharedBuffer
>);
65 void append(const char*, unsigned);
66 void append(const Vector
<char>&);
70 PassRefPtr
<SharedBuffer
> copy() const;
72 // Return the number of consecutive bytes after "position". "data"
73 // points to the first byte.
74 // Return 0 when no more data left.
75 // When extracting all data with getSomeData(), the caller should
76 // repeat calling it until it returns 0.
78 // const char* segment;
80 // while (unsigned length = sharedBuffer->getSomeData(segment, pos)) {
81 // // Use the data. for example: decoder->decode(segment, length);
84 unsigned getSomeData(const char*& data
, unsigned position
= 0) const;
86 // Returns the content data into "dest" as a flat buffer. "byteLength" must
87 // exactly match with size(). Returns true on success, otherwise the content
88 // of "dest" is not guaranteed.
89 bool getAsBytes(void* dest
, unsigned byteLength
) const;
91 // Creates an SkData and copies this SharedBuffer's contents to that
92 // SkData without merging segmented buffers into a flat buffer.
93 PassRefPtr
<SkData
> getAsSkData() const;
95 // See PurgeableVector::lock().
98 // WARNING: Calling unlock() on a SharedBuffer that wasn't created with the
99 // purgeability option does an extra memcpy(). Please use
100 // SharedBuffer::createPurgeable() if you intend to call unlock().
103 bool isLocked() const;
107 explicit SharedBuffer(size_t);
108 SharedBuffer(const char*, int);
109 SharedBuffer(const unsigned char*, int);
110 SharedBuffer(const char*, unsigned, PurgeableVector::PurgeableOption
);
112 // See SharedBuffer::data().
113 void mergeSegmentsIntoBuffer() const;
116 mutable PurgeableVector m_buffer
;
117 mutable Vector
<char*> m_segments
;
122 #endif // SharedBuffer_h