Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * 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 |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkData_DEFINED | 8 #ifndef SkData_DEFINED |
| 9 #define SkData_DEFINED | 9 #define SkData_DEFINED |
| 10 | 10 |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 | 12 |
| 13 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
| 14 | 14 |
| 15 class SkStream; | 15 class SkStream; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * SkData holds an immutable data buffer. Not only is the data immutable, | 18 * SkData holds an immutable data buffer. Not only is the data immutable, |
| 19 * but the actual ptr that is returned (by data() or bytes()) is guaranteed | 19 * but the actual ptr that is returned (by data() or bytes()) is guaranteed |
| 20 * to always be the same for the life of this instance. | 20 * to always be the same for the life of this instance. |
| 21 */ | 21 */ |
| 22 class SK_API SkData : public SkRefCnt { | 22 class SK_API SkData : public SkNVRefCnt<SkData> { |
|
reed1
2016/08/09 18:36:59
can we mark SkData as final?
| |
| 23 public: | 23 public: |
| 24 /** | 24 /** |
| 25 * Returns the number of bytes stored. | 25 * Returns the number of bytes stored. |
| 26 */ | 26 */ |
| 27 size_t size() const { return fSize; } | 27 size_t size() const { return fSize; } |
| 28 | 28 |
| 29 bool isEmpty() const { return 0 == fSize; } | 29 bool isEmpty() const { return 0 == fSize; } |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Returns the ptr to the data. | 32 * Returns the ptr to the data. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 * 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 |
| 73 * of scope, allowing for custom allocation/freeing of the data's contents. | 73 * of scope, allowing for custom allocation/freeing of the data's contents. |
| 74 */ | 74 */ |
| 75 typedef void (*ReleaseProc)(const void* ptr, void* context); | 75 typedef void (*ReleaseProc)(const void* ptr, void* context); |
| 76 | 76 |
| 77 /** | 77 /** |
| 78 * Create a new dataref by copying the specified data | 78 * Create a new dataref by copying the specified data |
| 79 */ | 79 */ |
| 80 static sk_sp<SkData> MakeWithCopy(const void* data, size_t length); | 80 static sk_sp<SkData> MakeWithCopy(const void* data, size_t length); |
| 81 | 81 |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * Create a new data with uninitialized contents. The caller should call wr itable_data() | 84 * Create a new data with uninitialized contents. The caller should call wr itable_data() |
| 85 * to write into the buffer, but this must be done before another ref() is made. | 85 * to write into the buffer, but this must be done before another ref() is made. |
| 86 */ | 86 */ |
| 87 static sk_sp<SkData> MakeUninitialized(size_t length); | 87 static sk_sp<SkData> MakeUninitialized(size_t length); |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * Create a new dataref by copying the specified c-string | 90 * Create a new dataref by copying the specified c-string |
| 91 * (a null-terminated array of bytes). The returned SkData will have size() | 91 * (a null-terminated array of bytes). The returned SkData will have size() |
| 92 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same | 92 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 */ | 151 */ |
| 152 static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t len gth); | 152 static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t len gth); |
| 153 | 153 |
| 154 /** | 154 /** |
| 155 * Returns a new empty dataref (or a reference to a shared empty dataref). | 155 * Returns a new empty dataref (or a reference to a shared empty dataref). |
| 156 * New or shared, the caller must see that unref() is eventually called. | 156 * New or shared, the caller must see that unref() is eventually called. |
| 157 */ | 157 */ |
| 158 static sk_sp<SkData> MakeEmpty(); | 158 static sk_sp<SkData> MakeEmpty(); |
| 159 | 159 |
| 160 private: | 160 private: |
| 161 friend class SkNVRefCnt<SkData>; | |
| 161 ReleaseProc fReleaseProc; | 162 ReleaseProc fReleaseProc; |
| 162 void* fReleaseProcContext; | 163 void* fReleaseProcContext; |
| 163 void* fPtr; | 164 void* fPtr; |
| 164 size_t fSize; | 165 size_t fSize; |
| 165 | 166 |
| 166 SkData(const void* ptr, size_t size, ReleaseProc, void* context); | 167 SkData(const void* ptr, size_t size, ReleaseProc, void* context); |
| 167 explicit SkData(size_t size); // inplace new/delete | 168 explicit SkData(size_t size); // inplace new/delete |
| 168 virtual ~SkData(); | 169 ~SkData(); |
| 169 | 170 |
| 170 | 171 |
| 171 // Objects of this type are sometimes created in a custom fashion using sk_m alloc_throw and | 172 // Objects of this type are sometimes created in a custom fashion using sk_m alloc_throw and |
| 172 // therefore must be sk_freed. We overload new to also call sk_malloc_throw so that memory | 173 // therefore must be sk_freed. We overload new to also call sk_malloc_throw so that memory |
| 173 // can be unconditionally released using sk_free in an overloaded delete. Ov erloading regular | 174 // can be unconditionally released using sk_free in an overloaded delete. Ov erloading regular |
| 174 // new means we must also overload placement new. | 175 // new means we must also overload placement new. |
| 175 void* operator new(size_t size) { return sk_malloc_throw(size); } | 176 void* operator new(size_t size) { return sk_malloc_throw(size); } |
| 176 void* operator new(size_t, void* p) { return p; } | 177 void* operator new(size_t, void* p) { return p; } |
| 177 void operator delete(void* p) { sk_free(p); } | 178 void operator delete(void* p) { sk_free(p); } |
| 178 | 179 |
| 179 // Called the first time someone calls NewEmpty to initialize the singleton. | 180 // Called the first time someone calls NewEmpty to initialize the singleton. |
| 180 friend SkData* sk_new_empty_data(); | 181 friend SkData* sk_new_empty_data(); |
| 181 | 182 |
| 182 // shared internal factory | 183 // shared internal factory |
| 183 static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length ); | 184 static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length ); |
| 184 | 185 |
| 185 static void DummyReleaseProc(const void*, void*); // {} | 186 static void DummyReleaseProc(const void*, void*); // {} |
| 186 | 187 |
| 187 typedef SkRefCnt INHERITED; | 188 typedef SkRefCnt INHERITED; |
| 188 }; | 189 }; |
| 189 | 190 |
| 190 #endif | 191 #endif |
| OLD | NEW |