| OLD | NEW |
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 4 * | 3 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 8 | 7 |
| 9 | |
| 10 | |
| 11 #ifndef SkData_DEFINED | 8 #ifndef SkData_DEFINED |
| 12 #define SkData_DEFINED | 9 #define SkData_DEFINED |
| 13 | 10 |
| 14 #include "SkRefCnt.h" | 11 #include "SkRefCnt.h" |
| 15 | 12 |
| 16 struct SkFILE; | 13 struct SkFILE; |
| 17 | 14 |
| 18 /** | 15 /** |
| 19 * SkData holds an immutable data buffer. Not only is the data immutable, | 16 * SkData holds an immutable data buffer. Not only is the data immutable, |
| 20 * but the actual ptr that is returned (by data() or bytes()) is guaranteed | 17 * but the actual ptr that is returned (by data() or bytes()) is guaranteed |
| (...skipping 17 matching lines...) Expand all Loading... |
| 38 | 35 |
| 39 /** | 36 /** |
| 40 * Like data(), returns a read-only ptr into the data, but in this case | 37 * Like data(), returns a read-only ptr into the data, but in this case |
| 41 * it is cast to uint8_t*, to make it easy to add an offset to it. | 38 * it is cast to uint8_t*, to make it easy to add an offset to it. |
| 42 */ | 39 */ |
| 43 const uint8_t* bytes() const { | 40 const uint8_t* bytes() const { |
| 44 return reinterpret_cast<const uint8_t*>(fPtr); | 41 return reinterpret_cast<const uint8_t*>(fPtr); |
| 45 } | 42 } |
| 46 | 43 |
| 47 /** | 44 /** |
| 45 * USE WITH CAUTION. |
| 46 * This call will assert that the refcnt is 1, as a precaution against modi
fying the |
| 47 * contents when another client/thread has access to the data. |
| 48 */ |
| 49 void* writable_data() { |
| 50 if (fSize) { |
| 51 // only assert we're unique if we're not empty |
| 52 SkASSERT(this->unique()); |
| 53 } |
| 54 return fPtr; |
| 55 } |
| 56 |
| 57 /** |
| 48 * Helper to copy a range of the data into a caller-provided buffer. | 58 * Helper to copy a range of the data into a caller-provided buffer. |
| 49 * Returns the actual number of bytes copied, after clamping offset and | 59 * Returns the actual number of bytes copied, after clamping offset and |
| 50 * length to the size of the data. If buffer is NULL, it is ignored, and | 60 * length to the size of the data. If buffer is NULL, it is ignored, and |
| 51 * only the computed number of bytes is returned. | 61 * only the computed number of bytes is returned. |
| 52 */ | 62 */ |
| 53 size_t copyRange(size_t offset, size_t length, void* buffer) const; | 63 size_t copyRange(size_t offset, size_t length, void* buffer) const; |
| 54 | 64 |
| 55 /** | 65 /** |
| 56 * Returns true if these two objects have the same length and contents, | 66 * Returns true if these two objects have the same length and contents, |
| 57 * effectively returning 0 == memcmp(...) | 67 * effectively returning 0 == memcmp(...) |
| 58 */ | 68 */ |
| 59 bool equals(const SkData* other) const; | 69 bool equals(const SkData* other) const; |
| 60 | 70 |
| 61 /** | 71 /** |
| 62 * Function that, if provided, will be called when the SkData goes out | 72 * Function that, if provided, will be called when the SkData goes out |
| 63 * of scope, allowing for custom allocation/freeing of the data. | 73 * of scope, allowing for custom allocation/freeing of the data. |
| 64 */ | 74 */ |
| 65 typedef void (*ReleaseProc)(const void* ptr, size_t length, void* context); | 75 typedef void (*ReleaseProc)(const void* ptr, size_t length, void* context); |
| 66 | 76 |
| 67 /** | 77 /** |
| 68 * Create a new dataref by copying the specified data | 78 * Create a new dataref by copying the specified data |
| 69 */ | 79 */ |
| 70 static SkData* NewWithCopy(const void* data, size_t length); | 80 static SkData* NewWithCopy(const void* data, size_t length); |
| 71 | 81 |
| 72 /** | 82 /** |
| 83 * Create a new data with uninitialized contents. The caller should call wr
itable_data() |
| 84 * to write into the buffer, but this must be done before another ref() is
made. |
| 85 */ |
| 86 static SkData* NewUninitialized(size_t length); |
| 87 |
| 88 /** |
| 73 * Create a new dataref by copying the specified c-string | 89 * Create a new dataref by copying the specified c-string |
| 74 * (a null-terminated array of bytes). The returned SkData will have size() | 90 * (a null-terminated array of bytes). The returned SkData will have size() |
| 75 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same | 91 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same |
| 76 * as "". | 92 * as "". |
| 77 */ | 93 */ |
| 78 static SkData* NewWithCString(const char cstr[]); | 94 static SkData* NewWithCString(const char cstr[]); |
| 79 | 95 |
| 80 /** | 96 /** |
| 81 * Create a new dataref, taking the data ptr as is, and using the | 97 * Create a new dataref, taking the data ptr as is, and using the |
| 82 * releaseproc to free it. The proc may be NULL. | 98 * releaseproc to free it. The proc may be NULL. |
| 83 */ | 99 */ |
| 84 static SkData* NewWithProc(const void* data, size_t length, | 100 static SkData* NewWithProc(const void* data, size_t length, ReleaseProc proc
, void* context); |
| 85 ReleaseProc proc, void* context); | 101 |
| 102 /** |
| 103 * Call this when the data parameter is already const and will outlive the
lifetime of the |
| 104 * SkData. Suitable for with const globals. |
| 105 */ |
| 106 static SkData* NewWithoutCopy(const void* data, size_t length) { |
| 107 return NewWithProc(data, length, NULL, NULL); |
| 108 } |
| 86 | 109 |
| 87 /** | 110 /** |
| 88 * Create a new dataref from a pointer allocated by malloc. The Data object | 111 * Create a new dataref from a pointer allocated by malloc. The Data object |
| 89 * takes ownership of that allocation, and will handling calling sk_free. | 112 * takes ownership of that allocation, and will handling calling sk_free. |
| 90 */ | 113 */ |
| 91 static SkData* NewFromMalloc(const void* data, size_t length); | 114 static SkData* NewFromMalloc(const void* data, size_t length); |
| 92 | 115 |
| 93 /** | 116 /** |
| 94 * Create a new dataref the file with the specified path. | 117 * Create a new dataref the file with the specified path. |
| 95 * If the file cannot be opened, this returns NULL. | 118 * If the file cannot be opened, this returns NULL. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 123 /** | 146 /** |
| 124 * Returns a new empty dataref (or a reference to a shared empty dataref). | 147 * Returns a new empty dataref (or a reference to a shared empty dataref). |
| 125 * New or shared, the caller must see that unref() is eventually called. | 148 * New or shared, the caller must see that unref() is eventually called. |
| 126 */ | 149 */ |
| 127 static SkData* NewEmpty(); | 150 static SkData* NewEmpty(); |
| 128 | 151 |
| 129 private: | 152 private: |
| 130 ReleaseProc fReleaseProc; | 153 ReleaseProc fReleaseProc; |
| 131 void* fReleaseProcContext; | 154 void* fReleaseProcContext; |
| 132 | 155 |
| 133 const void* fPtr; | 156 void* fPtr; |
| 134 size_t fSize; | 157 size_t fSize; |
| 135 | 158 |
| 136 SkData(const void* ptr, size_t size, ReleaseProc, void* context); | 159 SkData(const void* ptr, size_t size, ReleaseProc, void* context); |
| 160 SkData(size_t size); // inplace new/delete |
| 137 virtual ~SkData(); | 161 virtual ~SkData(); |
| 138 | 162 |
| 163 virtual void internal_dispose() const SK_OVERRIDE; |
| 164 |
| 139 // Called the first time someone calls NewEmpty to initialize the singleton. | 165 // Called the first time someone calls NewEmpty to initialize the singleton. |
| 140 static SkData* NewEmptyImpl(); | 166 static SkData* NewEmptyImpl(); |
| 141 static void DeleteEmpty(SkData*); | 167 static void DeleteEmpty(SkData*); |
| 142 | 168 |
| 169 // shared internal factory |
| 170 static SkData* PrivateNewWithCopy(const void* srcOrNull, size_t length); |
| 171 |
| 143 typedef SkRefCnt INHERITED; | 172 typedef SkRefCnt INHERITED; |
| 144 }; | 173 }; |
| 145 | 174 |
| 146 /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ | 175 /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ |
| 147 typedef SkAutoTUnref<SkData> SkAutoDataUnref; | 176 typedef SkAutoTUnref<SkData> SkAutoDataUnref; |
| 148 | 177 |
| 149 #endif | 178 #endif |
| OLD | NEW |