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" |
(...skipping 26 matching lines...) Expand all Loading... |
37 } | 37 } |
38 | 38 |
39 void SkWriter32::writeString(const char str[], size_t len) { | 39 void SkWriter32::writeString(const char str[], size_t len) { |
40 if (NULL == str) { | 40 if (NULL == str) { |
41 str = ""; | 41 str = ""; |
42 len = 0; | 42 len = 0; |
43 } | 43 } |
44 if ((long)len < 0) { | 44 if ((long)len < 0) { |
45 len = strlen(str); | 45 len = strlen(str); |
46 } | 46 } |
47 this->write32(len); | 47 |
48 // add 1 since we also write a terminating 0 | 48 // [ 4 byte len ] [ str ... ] [1 - 4 \0s] |
49 size_t alignedLen = SkAlign4(len + 1); | 49 uint32_t* ptr = this->reservePad(sizeof(uint32_t) + len + 1); |
50 char* ptr = (char*)this->reserve(alignedLen); | 50 *ptr = len; |
51 { | 51 char* chars = (char*)(ptr + 1); |
52 // Write the terminating 0 and fill in the rest with zeroes | 52 memcpy(chars, str, len); |
53 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4)); | 53 chars[len] = '\0'; |
54 *padding = 0; | |
55 } | |
56 // Copy the string itself. | |
57 memcpy(ptr, str, len); | |
58 } | 54 } |
59 | 55 |
60 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { | 56 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { |
61 if ((long)len < 0) { | 57 if ((long)len < 0) { |
62 SkASSERT(str); | 58 SkASSERT(str); |
63 len = strlen(str); | 59 len = strlen(str); |
64 } | 60 } |
65 const size_t lenBytes = 4; // we use 4 bytes to record the length | 61 const size_t lenBytes = 4; // we use 4 bytes to record the length |
66 // add 1 since we also write a terminating 0 | 62 // add 1 since we also write a terminating 0 |
67 return SkAlign4(lenBytes + len + 1); | 63 return SkAlign4(lenBytes + len + 1); |
68 } | 64 } |
69 | 65 |
70 const size_t kMinBufferBytes = 4096; | 66 const size_t kMinBufferBytes = 4096; |
71 | 67 |
72 void SkWriter32::growToAtLeast(size_t size) { | 68 void SkWriter32::growToAtLeast(size_t size) { |
73 const bool wasExternal = (fExternal != NULL) && (fData == fExternal); | 69 const bool wasExternal = (fExternal != NULL) && (fData == fExternal); |
74 const size_t minCapacity = kMinBufferBytes + | 70 const size_t minCapacity = kMinBufferBytes + |
75 SkTMax(size, fCapacity + (fCapacity >> 1)); | 71 SkTMax(size, fCapacity + (fCapacity >> 1)); |
76 | 72 |
77 // cause the buffer to grow | 73 // cause the buffer to grow |
78 fInternal.setCountExact(minCapacity); | 74 fInternal.setCountExact(minCapacity); |
79 fData = fInternal.begin(); | 75 fData = fInternal.begin(); |
80 fCapacity = fInternal.reserved(); | 76 fCapacity = fInternal.reserved(); |
81 if (wasExternal) { | 77 if (wasExternal) { |
82 // we were external, so copy in the data | 78 // we were external, so copy in the data |
83 memcpy(fData, fExternal, fUsed); | 79 memcpy(fData, fExternal, fUsed); |
84 } | 80 } |
85 } | 81 } |
OLD | NEW |