| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2015 Google 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 are | |
| 6 * met: | |
| 7 * | |
| 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 | |
| 13 * distribution. | |
| 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. | |
| 17 * | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #ifndef FastSharedBufferReader_h | |
| 32 #define FastSharedBufferReader_h | |
| 33 | |
| 34 #include "platform/PlatformExport.h" | |
| 35 #include "platform/image-decoders/SegmentReader.h" | |
| 36 #include "platform/wtf/Allocator.h" | |
| 37 #include "platform/wtf/Noncopyable.h" | |
| 38 #include "platform/wtf/PassRefPtr.h" | |
| 39 #include "platform/wtf/RefPtr.h" | |
| 40 | |
| 41 namespace blink { | |
| 42 | |
| 43 // This class is used by image decoders to avoid memory consolidation and | |
| 44 // therefore minimizes the cost of memory copying when the decoders | |
| 45 // repeatedly read from a buffer that is continually growing due to network | |
| 46 // traffic. | |
| 47 class PLATFORM_EXPORT FastSharedBufferReader final { | |
| 48 DISALLOW_NEW(); | |
| 49 WTF_MAKE_NONCOPYABLE(FastSharedBufferReader); | |
| 50 | |
| 51 public: | |
| 52 FastSharedBufferReader(PassRefPtr<SegmentReader> data); | |
| 53 | |
| 54 void SetData(PassRefPtr<SegmentReader>); | |
| 55 | |
| 56 // Returns a consecutive buffer that carries the data starting | |
| 57 // at |data_position| with |length| bytes. | |
| 58 // This method returns a pointer to a memory segment stored in | |
| 59 // |data_| if such a consecutive buffer can be found. | |
| 60 // Otherwise copies into |buffer| and returns it. | |
| 61 // Caller must ensure there are enough bytes in |data_| and |buffer|. | |
| 62 const char* GetConsecutiveData(size_t data_position, | |
| 63 size_t length, | |
| 64 char* buffer) const; | |
| 65 | |
| 66 // Wraps SegmentReader::GetSomeData(). | |
| 67 size_t GetSomeData(const char*& some_data, size_t data_position) const; | |
| 68 | |
| 69 // Returns a byte at |data_position|. | |
| 70 // Caller must ensure there are enough bytes in |data_|. | |
| 71 inline char GetOneByte(size_t data_position) const { | |
| 72 return *GetConsecutiveData(data_position, 1, 0); | |
| 73 } | |
| 74 | |
| 75 size_t size() const { return data_->size(); } | |
| 76 | |
| 77 // This class caches the last access for faster subsequent reads. This | |
| 78 // method clears that cache in case the SegmentReader has been modified | |
| 79 // (e.g. with MergeSegmentsIntoBuffer on a wrapped SharedBuffer). | |
| 80 void ClearCache(); | |
| 81 | |
| 82 private: | |
| 83 void GetSomeDataInternal(size_t data_position) const; | |
| 84 | |
| 85 RefPtr<SegmentReader> data_; | |
| 86 | |
| 87 // Caches the last segment of |data_| accessed, since subsequent reads are | |
| 88 // likely to re-access it. | |
| 89 mutable const char* segment_; | |
| 90 mutable size_t segment_length_; | |
| 91 | |
| 92 // Data position in |data_| pointed to by |segment_|. | |
| 93 mutable size_t data_position_; | |
| 94 }; | |
| 95 | |
| 96 } // namespace blink | |
| 97 | |
| 98 #endif | |
| OLD | NEW |