1 /*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "SharedBuffer.h"
28
29 #include "PurgeableBuffer.h"
30
31 namespace WebCore {
32
SharedBuffer()33 SharedBuffer::SharedBuffer()
34 {
35 }
36
SharedBuffer(const char * data,int size)37 SharedBuffer::SharedBuffer(const char* data, int size)
38 {
39 m_buffer.append(data, size);
40 }
41
SharedBuffer(const unsigned char * data,int size)42 SharedBuffer::SharedBuffer(const unsigned char* data, int size)
43 {
44 m_buffer.append(data, size);
45 }
46
~SharedBuffer()47 SharedBuffer::~SharedBuffer()
48 {
49 }
50
adoptVector(Vector<char> & vector)51 PassRefPtr<SharedBuffer> SharedBuffer::adoptVector(Vector<char>& vector)
52 {
53 RefPtr<SharedBuffer> buffer = create();
54 buffer->m_buffer.swap(vector);
55 return buffer.release();
56 }
57
adoptPurgeableBuffer(PurgeableBuffer * purgeableBuffer)58 PassRefPtr<SharedBuffer> SharedBuffer::adoptPurgeableBuffer(PurgeableBuffer* purgeableBuffer)
59 {
60 ASSERT(!purgeableBuffer->isPurgeable());
61 RefPtr<SharedBuffer> buffer = create();
62 buffer->m_purgeableBuffer.set(purgeableBuffer);
63 return buffer.release();
64 }
65
size() const66 unsigned SharedBuffer::size() const
67 {
68 if (hasPlatformData())
69 return platformDataSize();
70
71 if (m_purgeableBuffer)
72 return m_purgeableBuffer->size();
73
74 return m_buffer.size();
75 }
76
data() const77 const char* SharedBuffer::data() const
78 {
79 if (hasPlatformData())
80 return platformData();
81
82 if (m_purgeableBuffer)
83 return m_purgeableBuffer->data();
84
85 return m_buffer.data();
86 }
87
append(const char * data,int len)88 void SharedBuffer::append(const char* data, int len)
89 {
90 ASSERT(!m_purgeableBuffer);
91
92 maybeTransferPlatformData();
93
94 m_buffer.append(data, len);
95 }
96
clear()97 void SharedBuffer::clear()
98 {
99 clearPlatformData();
100
101 m_buffer.clear();
102 m_purgeableBuffer.clear();
103 }
104
copy() const105 PassRefPtr<SharedBuffer> SharedBuffer::copy() const
106 {
107 return SharedBuffer::create(data(), size());
108 }
109
releasePurgeableBuffer()110 PurgeableBuffer* SharedBuffer::releasePurgeableBuffer()
111 {
112 ASSERT(hasOneRef());
113 return m_purgeableBuffer.release();
114 }
115
116 #if !PLATFORM(CF)
117
clearPlatformData()118 inline void SharedBuffer::clearPlatformData()
119 {
120 }
121
maybeTransferPlatformData()122 inline void SharedBuffer::maybeTransferPlatformData()
123 {
124 }
125
hasPlatformData() const126 inline bool SharedBuffer::hasPlatformData() const
127 {
128 return false;
129 }
130
platformData() const131 inline const char* SharedBuffer::platformData() const
132 {
133 ASSERT_NOT_REACHED();
134
135 return 0;
136 }
137
platformDataSize() const138 inline unsigned SharedBuffer::platformDataSize() const
139 {
140 ASSERT_NOT_REACHED();
141
142 return 0;
143 }
144
145 #endif
146
147 }
148