| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  60 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { |  60 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { | 
|  61     if ((long)len < 0) { |  61     if ((long)len < 0) { | 
|  62         SkASSERT(str); |  62         SkASSERT(str); | 
|  63         len = strlen(str); |  63         len = strlen(str); | 
|  64     } |  64     } | 
|  65     const size_t lenBytes = 4;    // we use 4 bytes to record the length |  65     const size_t lenBytes = 4;    // we use 4 bytes to record the length | 
|  66     // add 1 since we also write a terminating 0 |  66     // add 1 since we also write a terminating 0 | 
|  67     return SkAlign4(lenBytes + len + 1); |  67     return SkAlign4(lenBytes + len + 1); | 
|  68 } |  68 } | 
|  69  |  69  | 
 |  70 const size_t kMinBufferBytes=4096; | 
 |  71  | 
|  70 void SkWriter32::growToAtLeast(size_t size) { |  72 void SkWriter32::growToAtLeast(size_t size) { | 
|  71     bool wasExternal = (fExternal != NULL) && (fData == fExternal); |  73     bool wasExternal = (fExternal != NULL) && (fData == fExternal); | 
 |  74     fCapacity = kMinBufferBytes + | 
 |  75         SkTMax(size, fCapacity + (fCapacity >> 1)); | 
 |  76  | 
|  72     // cause the buffer to grow |  77     // cause the buffer to grow | 
|  73     fInternal.setCount(size); |  78     fInternal.setCountExact(fCapacity); | 
 |  79     SkASSERT(fInternal.count() == (int)fCapacity); | 
 |  80     // read back the capacity in case it was already larger than this | 
 |  81     fCapacity = fInternal.reserved(); | 
|  74     fData = fInternal.begin(); |  82     fData = fInternal.begin(); | 
|  75     if (wasExternal) { |  83     if (wasExternal) { | 
|  76         // we were external, so copy in the data |  84         // we were external, so copy in the data | 
|  77         memcpy(fData, fExternal, fUsed); |  85         memcpy(fData, fExternal, fUsed); | 
|  78     } |  86     } | 
|  79     // Find out the size the buffer grew to, it may be more than we asked for. |  | 
|  80     fCapacity = fInternal.reserved(); |  | 
|  81     // Expand the array so all reserved space is "used", we maintain the |  | 
|  82     // amount we have written manually outside the array |  | 
|  83     fInternal.setCount(fCapacity); |  | 
|  84     SkASSERT(fInternal.count() == (int)fCapacity); |  | 
|  85     SkASSERT(fInternal.reserved() == (int)fCapacity); |  87     SkASSERT(fInternal.reserved() == (int)fCapacity); | 
|  86 } |  88 } | 
| OLD | NEW |