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 #include "SkReader32.h" | 8 #include "SkReader32.h" |
9 #include "SkString.h" | 9 #include "SkString.h" |
10 #include "SkWriter32.h" | 10 #include "SkWriter32.h" |
11 #include "SkData.h" | |
11 | 12 |
12 /* | 13 /* |
13 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4 | 14 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4 |
14 */ | 15 */ |
15 | 16 |
16 const char* SkReader32::readString(size_t* outLen) { | 17 const char* SkReader32::readString(size_t* outLen) { |
17 size_t len = this->readInt(); | 18 size_t len = this->readInt(); |
18 const void* ptr = this->peek(); | 19 const void* ptr = this->peek(); |
19 | 20 |
20 // skip over the string + '\0' and then pad to a multiple of 4 | 21 // skip over the string + '\0' and then pad to a multiple of 4 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 // cause the buffer to grow | 78 // cause the buffer to grow |
78 fInternal.setCountExact(fCapacity); | 79 fInternal.setCountExact(fCapacity); |
79 fData = fInternal.begin(); | 80 fData = fInternal.begin(); |
80 if (wasExternal) { | 81 if (wasExternal) { |
81 // we were external, so copy in the data | 82 // we were external, so copy in the data |
82 memcpy(fData, fExternal, fUsed); | 83 memcpy(fData, fExternal, fUsed); |
83 } | 84 } |
84 SkASSERT(fInternal.count() == fCapacity); | 85 SkASSERT(fInternal.count() == fCapacity); |
85 SkASSERT(fInternal.reserved() == fCapacity); | 86 SkASSERT(fInternal.reserved() == fCapacity); |
86 } | 87 } |
88 | |
89 SkData* SkWriter32::detatchAsData() { | |
90 uint8_t* buffer = NULL; | |
91 if ((fExternal != NULL) && (fData == fExternal)) { | |
92 // We need to copy to an allocated buffer before returning. | |
93 buffer = (uint8_t*)sk_malloc_throw(fUsed); | |
94 memcpy(buffer, fData, fUsed); | |
95 } else { | |
96 buffer = fInternal.detach(); | |
97 } | |
98 SkData* data = SkData::NewFromMalloc(buffer, fUsed); | |
99 fData = NULL; | |
tomhudson
2014/02/07 17:15:58
Nit: the constructor uses 0 for all 4 fields.
I th
iancottrell
2014/02/11 13:28:40
Done.
| |
100 fCapacity = 0; | |
101 fUsed = 0; | |
102 fExternal = NULL; | |
103 return data; | |
104 } | |
OLD | NEW |