| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
| 3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. | 3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 namespace blink { | 38 namespace blink { |
| 39 | 39 |
| 40 class WebProcessMemoryDump; | 40 class WebProcessMemoryDump; |
| 41 | 41 |
| 42 class PLATFORM_EXPORT SharedBuffer : public RefCounted<SharedBuffer> { | 42 class PLATFORM_EXPORT SharedBuffer : public RefCounted<SharedBuffer> { |
| 43 public: | 43 public: |
| 44 enum : unsigned { kSegmentSize = 0x1000 }; | 44 enum : unsigned { kSegmentSize = 0x1000 }; |
| 45 | 45 |
| 46 static PassRefPtr<SharedBuffer> create() { return adoptRef(new SharedBuffer)
; } | 46 static PassRefPtr<SharedBuffer> create() { return adoptRef(new SharedBuffer)
; } |
| 47 static PassRefPtr<SharedBuffer> create(size_t size) { return adoptRef(new Sh
aredBuffer(size)); } | |
| 48 static PassRefPtr<SharedBuffer> create(const char* c, int i) { return adoptR
ef(new SharedBuffer(c, i)); } | |
| 49 static PassRefPtr<SharedBuffer> create(const unsigned char* c, int i) { retu
rn adoptRef(new SharedBuffer(c, i)); } | |
| 50 | 47 |
| 51 static PassRefPtr<SharedBuffer> createPurgeable(const char* c, unsigned size
) { return adoptRef(new SharedBuffer(c, size, PurgeableVector::Purgeable)); } | 48 HAS_STRICTLY_TYPED_ARG |
| 49 static PassRefPtr<SharedBuffer> create(STRICTLY_TYPED_ARG(size)) |
| 50 { |
| 51 STRICT_ARG_TYPE(size_t); |
| 52 return adoptRef(new SharedBuffer(size)); |
| 53 } |
| 54 |
| 55 HAS_STRICTLY_TYPED_ARG |
| 56 static PassRefPtr<SharedBuffer> create(const char* data, STRICTLY_TYPED_ARG(
size)) |
| 57 { |
| 58 STRICT_ARG_TYPE(size_t); |
| 59 return adoptRef(new SharedBuffer(data, size)); |
| 60 } |
| 61 |
| 62 HAS_STRICTLY_TYPED_ARG |
| 63 static PassRefPtr<SharedBuffer> create(const unsigned char* data, STRICTLY_T
YPED_ARG(size)) |
| 64 { |
| 65 STRICT_ARG_TYPE(size_t); |
| 66 return adoptRef(new SharedBuffer(data, size)); |
| 67 } |
| 68 |
| 69 HAS_STRICTLY_TYPED_ARG |
| 70 static PassRefPtr<SharedBuffer> createPurgeable(const char* data, STRICTLY_T
YPED_ARG(size)) |
| 71 { |
| 72 STRICT_ARG_TYPE(size_t); |
| 73 return adoptRef(new SharedBuffer(data, size, PurgeableVector::Purgeable)
); |
| 74 } |
| 52 | 75 |
| 53 static PassRefPtr<SharedBuffer> adoptVector(Vector<char>&); | 76 static PassRefPtr<SharedBuffer> adoptVector(Vector<char>&); |
| 54 | 77 |
| 55 ~SharedBuffer(); | 78 ~SharedBuffer(); |
| 56 | 79 |
| 57 // Calling this function will force internal segmented buffers to be merged | 80 // Calling this function will force internal segmented buffers to be merged |
| 58 // into a flat buffer. Use getSomeData() whenever possible for better | 81 // into a flat buffer. Use getSomeData() whenever possible for better |
| 59 // performance. | 82 // performance. |
| 60 const char* data() const; | 83 const char* data() const; |
| 61 | 84 |
| 62 unsigned size() const; | 85 size_t size() const; |
| 63 | 86 |
| 64 bool isEmpty() const { return !size(); } | 87 bool isEmpty() const { return !size(); } |
| 65 | 88 |
| 66 void append(PassRefPtr<SharedBuffer>); | 89 void append(PassRefPtr<SharedBuffer>); |
| 67 void append(const char*, unsigned); | 90 |
| 91 HAS_STRICTLY_TYPED_ARG |
| 92 void append(const char* data, STRICTLY_TYPED_ARG(size)) |
| 93 { |
| 94 ALLOW_NUMERIC_ARG_TYPES_PROMOTABLE_TO(size_t); |
| 95 appendInternal(data, size); |
| 96 } |
| 68 void append(const Vector<char>&); | 97 void append(const Vector<char>&); |
| 69 | 98 |
| 70 void clear(); | 99 void clear(); |
| 71 | 100 |
| 72 PassRefPtr<SharedBuffer> copy() const; | 101 PassRefPtr<SharedBuffer> copy() const; |
| 73 | 102 |
| 74 // Return the number of consecutive bytes after "position". "data" | 103 // Return the number of consecutive bytes after "position". "data" |
| 75 // points to the first byte. | 104 // points to the first byte. |
| 76 // Return 0 when no more data left. | 105 // Return 0 when no more data left. |
| 77 // When extracting all data with getSomeData(), the caller should | 106 // When extracting all data with getSomeData(), the caller should |
| 78 // repeat calling it until it returns 0. | 107 // repeat calling it until it returns 0. |
| 79 // Usage: | 108 // Usage: |
| 80 // const char* segment; | 109 // const char* segment; |
| 81 // unsigned pos = 0; | 110 // size_t pos = 0; |
| 82 // while (unsigned length = sharedBuffer->getSomeData(segment, pos)) { | 111 // while (size_t length = sharedBuffer->getSomeData(segment, pos)) { |
| 83 // // Use the data. for example: decoder->decode(segment, length); | 112 // // Use the data. for example: decoder->decode(segment, length); |
| 84 // pos += length; | 113 // pos += length; |
| 85 // } | 114 // } |
| 86 unsigned getSomeData(const char*& data, unsigned position = 0) const; | 115 HAS_STRICTLY_TYPED_ARG |
| 116 size_t getSomeData(const char*& data, STRICTLY_TYPED_ARG(position) = static_
cast<size_t>(0)) const |
| 117 { |
| 118 STRICT_ARG_TYPE(size_t); |
| 119 return getSomeDataInternal(data, position); |
| 120 } |
| 87 | 121 |
| 88 // Returns the content data into "dest" as a flat buffer. "byteLength" must | 122 // Returns the content data into "dest" as a flat buffer. "byteLength" must |
| 89 // exactly match with size(). Returns true on success, otherwise the content | 123 // exactly match with size(). Returns true on success, otherwise the content |
| 90 // of "dest" is not guaranteed. | 124 // of "dest" is not guaranteed. |
| 91 bool getAsBytes(void* dest, unsigned byteLength) const; | 125 HAS_STRICTLY_TYPED_ARG |
| 126 bool getAsBytes(void* dest, STRICTLY_TYPED_ARG(byteLength)) const |
| 127 { |
| 128 STRICT_ARG_TYPE(size_t); |
| 129 return getAsBytesInternal(dest, byteLength); |
| 130 } |
| 92 | 131 |
| 93 // Creates an SkData and copies this SharedBuffer's contents to that | 132 // Creates an SkData and copies this SharedBuffer's contents to that |
| 94 // SkData without merging segmented buffers into a flat buffer. | 133 // SkData without merging segmented buffers into a flat buffer. |
| 95 PassRefPtr<SkData> getAsSkData() const; | 134 PassRefPtr<SkData> getAsSkData() const; |
| 96 | 135 |
| 97 // See PurgeableVector::lock(). | 136 // See PurgeableVector::lock(). |
| 98 bool lock(); | 137 bool lock(); |
| 99 | 138 |
| 100 // WARNING: Calling unlock() on a SharedBuffer that wasn't created with the | 139 // WARNING: Calling unlock() on a SharedBuffer that wasn't created with the |
| 101 // purgeability option does an extra memcpy(). Please use | 140 // purgeability option does an extra memcpy(). Please use |
| 102 // SharedBuffer::createPurgeable() if you intend to call unlock(). | 141 // SharedBuffer::createPurgeable() if you intend to call unlock(). |
| 103 void unlock(); | 142 void unlock(); |
| 104 | 143 |
| 105 bool isLocked() const; | 144 bool isLocked() const; |
| 106 | 145 |
| 107 void onMemoryDump(const String& dumpPrefix, WebProcessMemoryDump*) const; | 146 void onMemoryDump(const String& dumpPrefix, WebProcessMemoryDump*) const; |
| 108 | 147 |
| 109 private: | 148 private: |
| 110 SharedBuffer(); | 149 SharedBuffer(); |
| 111 explicit SharedBuffer(size_t); | 150 explicit SharedBuffer(size_t); |
| 112 SharedBuffer(const char*, int); | 151 SharedBuffer(const char*, size_t); |
| 113 SharedBuffer(const unsigned char*, int); | 152 SharedBuffer(const unsigned char*, size_t); |
| 114 SharedBuffer(const char*, unsigned, PurgeableVector::PurgeableOption); | 153 SharedBuffer(const char*, size_t, PurgeableVector::PurgeableOption); |
| 115 | 154 |
| 116 // See SharedBuffer::data(). | 155 // See SharedBuffer::data(). |
| 117 void mergeSegmentsIntoBuffer() const; | 156 void mergeSegmentsIntoBuffer() const; |
| 118 | 157 |
| 119 unsigned m_size; | 158 void appendInternal(const char* data, size_t); |
| 159 bool getAsBytesInternal(void* dest, size_t) const; |
| 160 size_t getSomeDataInternal(const char*& data, size_t position) const; |
| 161 |
| 162 size_t m_size; |
| 120 mutable PurgeableVector m_buffer; | 163 mutable PurgeableVector m_buffer; |
| 121 mutable Vector<char*> m_segments; | 164 mutable Vector<char*> m_segments; |
| 122 }; | 165 }; |
| 123 | 166 |
| 124 } // namespace blink | 167 } // namespace blink |
| 125 | 168 |
| 126 #endif // SharedBuffer_h | 169 #endif // SharedBuffer_h |
| OLD | NEW |