SharedBuffer.cpp 9.47 KB
Newer Older
andersca's avatar
andersca committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3
 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
andersca's avatar
andersca committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "SharedBuffer.h"

30
#include "PlatformMemoryInstrumentation.h"
31
#include "PurgeableBuffer.h"
32
#include <wtf/MemoryInstrumentationVector.h>
33
#include <wtf/PassOwnPtr.h>
34 35
#include <wtf/unicode/UTF8.h>
#include <wtf/unicode/Unicode.h>
36

37 38
using namespace std;

andersca's avatar
andersca committed
39 40
namespace WebCore {

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
static const unsigned segmentSize = 0x1000;
static const unsigned segmentPositionMask = 0x0FFF;

static inline unsigned segmentIndex(unsigned position)
{
    return position / segmentSize;
}

static inline unsigned offsetInSegment(unsigned position)
{
    return position & segmentPositionMask;
}

static inline char* allocateSegment()
{
    return static_cast<char*>(fastMalloc(segmentSize));
}

static inline void freeSegment(char* p)
{
    fastFree(p);
}

andersca's avatar
andersca committed
64
SharedBuffer::SharedBuffer()
65
    : m_size(0)
andersca's avatar
andersca committed
66 67 68
{
}

69 70 71 72 73 74
SharedBuffer::SharedBuffer(size_t size)
    : m_size(size)
    , m_buffer(size)
{
}

andersca's avatar
andersca committed
75
SharedBuffer::SharedBuffer(const char* data, int size)
76
    : m_size(0)
andersca's avatar
andersca committed
77
{
78 79 80 81
    // FIXME: Use unsigned consistently, and check for invalid casts when calling into SharedBuffer from other code.
    if (size < 0)
        CRASH();

82
    append(data, size);
andersca's avatar
andersca committed
83 84
}

85
SharedBuffer::SharedBuffer(const unsigned char* data, int size)
86
    : m_size(0)
87
{
88 89 90 91
    // FIXME: Use unsigned consistently, and check for invalid casts when calling into SharedBuffer from other code.
    if (size < 0)
        CRASH();

92
    append(reinterpret_cast<const char*>(data), size);
93
}
94 95 96
    
SharedBuffer::~SharedBuffer()
{
97
    clear();
98
}
99

100 101 102 103
PassRefPtr<SharedBuffer> SharedBuffer::adoptVector(Vector<char>& vector)
{
    RefPtr<SharedBuffer> buffer = create();
    buffer->m_buffer.swap(vector);
104
    buffer->m_size = buffer->m_buffer.size();
105 106 107
    return buffer.release();
}

108
PassRefPtr<SharedBuffer> SharedBuffer::adoptPurgeableBuffer(PassOwnPtr<PurgeableBuffer> purgeableBuffer) 
109 110 111
{ 
    ASSERT(!purgeableBuffer->isPurgeable());
    RefPtr<SharedBuffer> buffer = create();
112
    buffer->m_purgeableBuffer = purgeableBuffer;
113 114 115
    return buffer.release();
}

andersca's avatar
andersca committed
116 117 118 119 120
unsigned SharedBuffer::size() const
{
    if (hasPlatformData())
        return platformDataSize();
    
121 122 123
    if (m_purgeableBuffer)
        return m_purgeableBuffer->size();
    
124
    return m_size;
andersca's avatar
andersca committed
125 126 127 128 129 130 131
}

const char* SharedBuffer::data() const
{
    if (hasPlatformData())
        return platformData();
    
132 133 134
    if (m_purgeableBuffer)
        return m_purgeableBuffer->data();
    
135
    return buffer().data();
andersca's avatar
andersca committed
136 137
}

138 139 140 141 142 143 144 145 146 147
void SharedBuffer::append(SharedBuffer* data)
{
    const char* segment;
    size_t position = 0;
    while (size_t length = data->getSomeData(segment, position)) {
        append(segment, length);
        position += length;
    }
}

148
void SharedBuffer::append(const char* data, unsigned length)
andersca's avatar
andersca committed
149
{
150
    ASSERT(!m_purgeableBuffer);
151 152
    if (!length)
        return;
153

andersca's avatar
andersca committed
154 155
    maybeTransferPlatformData();
    
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    unsigned positionInSegment = offsetInSegment(m_size - m_buffer.size());
    m_size += length;

    if (m_size <= segmentSize) {
        // No need to use segments for small resource data
        m_buffer.append(data, length);
        return;
    }

    char* segment;
    if (!positionInSegment) {
        segment = allocateSegment();
        m_segments.append(segment);
    } else
        segment = m_segments.last() + positionInSegment;

    unsigned segmentFreeSpace = segmentSize - positionInSegment;
173
    unsigned bytesToCopy = min(length, segmentFreeSpace);
174 175 176

    for (;;) {
        memcpy(segment, data, bytesToCopy);
mitz@apple.com's avatar
mitz@apple.com committed
177
        if (static_cast<unsigned>(length) == bytesToCopy)
178 179 180 181 182 183
            break;

        length -= bytesToCopy;
        data += bytesToCopy;
        segment = allocateSegment();
        m_segments.append(segment);
184
        bytesToCopy = min(length, segmentSize);
185
    }
andersca's avatar
andersca committed
186 187
}

188 189 190 191 192
void SharedBuffer::append(const Vector<char>& data)
{
    append(data.data(), data.size());
}

andersca's avatar
andersca committed
193 194 195 196
void SharedBuffer::clear()
{
    clearPlatformData();
    
197 198 199 200 201 202
    for (unsigned i = 0; i < m_segments.size(); ++i)
        freeSegment(m_segments[i]);

    m_segments.clear();
    m_size = 0;

darin@apple.com's avatar
darin@apple.com committed
203
    m_buffer.clear();
204
    m_purgeableBuffer.clear();
205
#if HAVE(NETWORK_CFDATA_ARRAY_CALLBACK)
206 207
    m_dataArray.clear();
#endif
andersca's avatar
andersca committed
208 209
}

beidson's avatar
beidson committed
210 211
PassRefPtr<SharedBuffer> SharedBuffer::copy() const
{
212 213 214 215 216 217 218 219 220 221 222 223
    RefPtr<SharedBuffer> clone(adoptRef(new SharedBuffer));
    if (m_purgeableBuffer || hasPlatformData()) {
        clone->append(data(), size());
        return clone;
    }

    clone->m_size = m_size;
    clone->m_buffer.reserveCapacity(m_size);
    clone->m_buffer.append(m_buffer.data(), m_buffer.size());
    for (unsigned i = 0; i < m_segments.size(); ++i)
        clone->m_buffer.append(m_segments[i], segmentSize);
    return clone;
beidson's avatar
beidson committed
224 225
}

226
PassOwnPtr<PurgeableBuffer> SharedBuffer::releasePurgeableBuffer()
227 228 229 230
{ 
    ASSERT(hasOneRef()); 
    return m_purgeableBuffer.release(); 
}
beidson's avatar
beidson committed
231

232 233 234 235 236 237 238 239
const Vector<char>& SharedBuffer::buffer() const
{
    unsigned bufferSize = m_buffer.size();
    if (m_size > bufferSize) {
        m_buffer.resize(m_size);
        char* destination = m_buffer.data() + bufferSize;
        unsigned bytesLeft = m_size - bufferSize;
        for (unsigned i = 0; i < m_segments.size(); ++i) {
240
            unsigned bytesToCopy = min(bytesLeft, segmentSize);
241 242 243 244 245 246
            memcpy(destination, m_segments[i], bytesToCopy);
            destination += bytesToCopy;
            bytesLeft -= bytesToCopy;
            freeSegment(m_segments[i]);
        }
        m_segments.clear();
247
#if HAVE(NETWORK_CFDATA_ARRAY_CALLBACK)
248 249
        copyDataArrayAndClear(destination, bytesLeft);
#endif
250 251 252 253
    }
    return m_buffer;
}

254 255
void SharedBuffer::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
256
    MemoryClassInfo info(memoryObjectInfo, this);
257 258
    info.addMember(m_buffer);
    info.addMember(m_segments);
259 260 261 262 263
    for (unsigned i = 0; i < m_segments.size(); ++i)
        info.addRawBuffer(m_segments[i], segmentSize);
    info.addMember(m_purgeableBuffer.get());
}

264 265
unsigned SharedBuffer::getSomeData(const char*& someData, unsigned position) const
{
266 267
    unsigned totalSize = size();
    if (position >= totalSize) {
268 269 270 271
        someData = 0;
        return 0;
    }

272 273 274 275 276 277 278
    if (hasPlatformData() || m_purgeableBuffer) {
        ASSERT(position < size());
        someData = data() + position;
        return totalSize - position;
    }

    ASSERT(position < m_size);
279 280 281 282 283 284 285 286
    unsigned consecutiveSize = m_buffer.size();
    if (position < consecutiveSize) {
        someData = m_buffer.data() + position;
        return consecutiveSize - position;
    }
 
    position -= consecutiveSize;
    unsigned segments = m_segments.size();
287
    unsigned maxSegmentedSize = segments * segmentSize;
288
    unsigned segment = segmentIndex(position);
289
    if (segment < segments) {
290
        unsigned bytesLeft = totalSize - consecutiveSize;
291
        unsigned segmentedSize = min(maxSegmentedSize, bytesLeft);
292

293 294 295 296 297 298 299 300 301 302 303 304
        unsigned positionInSegment = offsetInSegment(position);
        someData = m_segments[segment] + positionInSegment;
        return segment == segments - 1 ? segmentedSize - position : segmentSize - positionInSegment;
    }
#if HAVE(NETWORK_CFDATA_ARRAY_CALLBACK)
    ASSERT(maxSegmentedSize <= position);
    position -= maxSegmentedSize;
    return copySomeDataFromDataArray(someData, position);
#else
    ASSERT_NOT_REACHED();
    return 0;
#endif
305 306
}

307
#if !USE(CF) || PLATFORM(QT)
308

andersca's avatar
andersca committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
inline void SharedBuffer::clearPlatformData()
{
}

inline void SharedBuffer::maybeTransferPlatformData()
{
}

inline bool SharedBuffer::hasPlatformData() const
{
    return false;
}

inline const char* SharedBuffer::platformData() const
{
    ASSERT_NOT_REACHED();

    return 0;
}

329
inline unsigned SharedBuffer::platformDataSize() const
andersca's avatar
andersca committed
330 331 332 333 334
{
    ASSERT_NOT_REACHED();
    
    return 0;
}
beidson's avatar
beidson committed
335

336 337
#endif

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
PassRefPtr<SharedBuffer> utf8Buffer(const String& string)
{
    // Allocate a buffer big enough to hold all the characters.
    const int length = string.length();
    Vector<char> buffer(length * 3);

    // Convert to runs of 8-bit characters.
    char* p = buffer.data();
    const UChar* d = string.characters();
    WTF::Unicode::ConversionResult result = WTF::Unicode::convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), true);
    if (result != WTF::Unicode::conversionOK)
        return 0;

    buffer.shrink(p - buffer.data());
    return SharedBuffer::adoptVector(buffer);
andersca's avatar
andersca committed
353
}
354 355

} // namespace WebCore