2 * Copyright (C) 2015 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef FastSharedBufferReader_h
32 #define FastSharedBufferReader_h
34 #include "platform/PlatformExport.h"
35 #include "platform/SharedBuffer.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RefPtr.h"
41 // This class is used by image decoders to avoid memory consolidation and
42 // therefore minimizes the cost of memory copying when the decoders
43 // repeatedly read from a buffer that is continually growing due to network
45 class PLATFORM_EXPORT FastSharedBufferReader
{
47 FastSharedBufferReader(PassRefPtr
<SharedBuffer
> data
);
49 // Returns a consecutive buffer that carries the data starting
50 // at |dataPosition| with |length| bytes.
51 // This method returns a pointer to a memory segment stored in
52 // |m_data| if such a consecutive buffer can be found.
53 // Otherwise copies into |buffer| and returns it.
54 // Caller must ensure there are enough bytes in |m_data| and |buffer|.
55 const char* getConsecutiveData(size_t dataPosition
, size_t length
, char* buffer
) const;
57 // Wraps SharedBuffer::getSomeData().
58 size_t getSomeData(const char*& someData
, size_t dataPosition
) const;
60 // Returns a byte at |dataPosition|.
61 // Caller must ensure there are enough bytes in |m_data|.
62 inline char getOneByte(size_t dataPosition
) const
64 return *getConsecutiveData(dataPosition
, 1, 0);
69 return m_data
->size();
73 void getSomeDataInternal(unsigned dataPosition
) const;
75 RefPtr
<SharedBuffer
> m_data
;
77 // Caches the last segment of |m_data| accessed, since subsequent reads are
78 // likely to re-access it.
79 mutable const char* m_segment
;
80 mutable size_t m_segmentLength
;
82 // Data position in |m_data| pointed to by |m_segment|.
83 mutable size_t m_dataPosition
;