OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkWriter32_DEFINED | 10 #ifndef SkWriter32_DEFINED |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 SkASSERT(SkAlign4(size) == size); | 66 SkASSERT(SkAlign4(size) == size); |
67 size_t offset = fUsed; | 67 size_t offset = fUsed; |
68 size_t totalRequired = fUsed + size; | 68 size_t totalRequired = fUsed + size; |
69 if (totalRequired > fCapacity) { | 69 if (totalRequired > fCapacity) { |
70 this->growToAtLeast(totalRequired); | 70 this->growToAtLeast(totalRequired); |
71 } | 71 } |
72 fUsed = totalRequired; | 72 fUsed = totalRequired; |
73 return (uint32_t*)(fData + offset); | 73 return (uint32_t*)(fData + offset); |
74 } | 74 } |
75 | 75 |
76 // Read or write 4 bytes at offset, which must be a multiple of 4 <= size(). | 76 /** |
77 uint32_t read32At(size_t offset) { | 77 * Read a T record at offset, which must be a multiple of 4. Only legal if
the record |
| 78 * was writtern atomically using the write methods below. |
| 79 */ |
| 80 template<typename T> |
| 81 const T& readTAt(size_t offset) const { |
78 SkASSERT(SkAlign4(offset) == offset); | 82 SkASSERT(SkAlign4(offset) == offset); |
79 SkASSERT(offset < fUsed); | 83 SkASSERT(offset < fUsed); |
80 return *(uint32_t*)(fData + offset); | 84 return *(T*)(fData + offset); |
81 } | 85 } |
82 | 86 |
83 void write32At(size_t offset, uint32_t val) { | 87 /** |
| 88 * Overwrite a T record at offset, which must be a multiple of 4. Only lega
l if the record |
| 89 * was writtern atomically using the write methods below. |
| 90 */ |
| 91 template<typename T> |
| 92 void overwriteTAt(size_t offset, const T& value) { |
84 SkASSERT(SkAlign4(offset) == offset); | 93 SkASSERT(SkAlign4(offset) == offset); |
85 SkASSERT(offset < fUsed); | 94 SkASSERT(offset < fUsed); |
86 *(uint32_t*)(fData + offset) = val; | 95 *(T*)(fData + offset) = value; |
87 } | 96 } |
88 | 97 |
89 bool writeBool(bool value) { | 98 bool writeBool(bool value) { |
90 this->write32(value); | 99 this->write32(value); |
91 return value; | 100 return value; |
92 } | 101 } |
93 | 102 |
94 void writeInt(int32_t value) { | 103 void writeInt(int32_t value) { |
95 this->write32(value); | 104 this->write32(value); |
96 } | 105 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 void write(const void* values, size_t size) { | 170 void write(const void* values, size_t size) { |
162 SkASSERT(SkAlign4(size) == size); | 171 SkASSERT(SkAlign4(size) == size); |
163 memcpy(this->reserve(size), values, size); | 172 memcpy(this->reserve(size), values, size); |
164 } | 173 } |
165 | 174 |
166 /** | 175 /** |
167 * Reserve size bytes. Does not need to be 4 byte aligned. The remaining sp
ace (if any) will be | 176 * Reserve size bytes. Does not need to be 4 byte aligned. The remaining sp
ace (if any) will be |
168 * filled in with zeroes. | 177 * filled in with zeroes. |
169 */ | 178 */ |
170 uint32_t* reservePad(size_t size) { | 179 uint32_t* reservePad(size_t size) { |
171 uint32_t* p = this->reserve(SkAlign4(size)); | 180 size_t alignedSize = SkAlign4(size); |
172 uint8_t* tail = (uint8_t*)p + size; | 181 uint32_t* p = this->reserve(alignedSize); |
173 switch (SkAlign4(size) - size) { | 182 if (alignedSize != size) { |
174 default: SkDEBUGFAIL("SkAlign4(x) - x should always be 0, 1, 2, or 3
."); | 183 SkASSERT(alignedSize >= 4); |
175 case 3: *tail++ = 0x00; // fallthrough is intentional | 184 p[alignedSize / 4 - 1] = 0; |
176 case 2: *tail++ = 0x00; // fallthrough is intentional | |
177 case 1: *tail++ = 0x00; | |
178 case 0: ;/*nothing to do*/ | |
179 } | 185 } |
180 return p; | 186 return p; |
181 } | 187 } |
182 | 188 |
183 /** | 189 /** |
184 * Write size bytes from src, and pad to 4 byte alignment with zeroes. | 190 * Write size bytes from src, and pad to 4 byte alignment with zeroes. |
185 */ | 191 */ |
186 void writePad(const void* src, size_t size) { | 192 void writePad(const void* src, size_t size) { |
187 memcpy(this->reservePad(size), src, size); | 193 memcpy(this->reservePad(size), src, size); |
188 } | 194 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 union { | 261 union { |
256 void* fPtrAlignment; | 262 void* fPtrAlignment; |
257 double fDoubleAlignment; | 263 double fDoubleAlignment; |
258 char fStorage[SIZE]; | 264 char fStorage[SIZE]; |
259 } fData; | 265 } fData; |
260 | 266 |
261 typedef SkWriter32 INHERITED; | 267 typedef SkWriter32 INHERITED; |
262 }; | 268 }; |
263 | 269 |
264 #endif | 270 #endif |
OLD | NEW |