#include "SkString.h"
#include "SkFixed.h"
+#include "SkThread.h"
#include "SkUtils.h"
#include <stdarg.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////
-#define kMaxRefCnt_SkString SK_MaxU16
-
// the 3 values are [length] [refcnt] [terminating zero data]
const SkString::Rec SkString::gEmptyRec = { 0, 0, 0 };
#define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec)
-SkString::Rec* SkString::AllocRec(const char text[], U16CPU len) {
+SkString::Rec* SkString::AllocRec(const char text[], size_t len) {
Rec* rec;
- if (len == 0) {
+ if (0 == len) {
rec = const_cast<Rec*>(&gEmptyRec);
} else {
// add 1 for terminating 0, then align4 so we can have some slop when growing the string
rec = (Rec*)sk_malloc_throw(SizeOfRec() + SkAlign4(len + 1));
- rec->fLength = SkToU16(len);
+ rec->fLength = len;
rec->fRefCnt = 1;
if (text) {
memcpy(rec->data(), text, len);
SkString::Rec* SkString::RefRec(Rec* src) {
if (src != &gEmptyRec) {
- if (src->fRefCnt == kMaxRefCnt_SkString) {
- src = AllocRec(src->data(), src->fLength);
- } else {
- src->fRefCnt += 1;
- }
+ sk_atomic_inc(&src->fRefCnt);
}
return src;
}
#ifdef SK_DEBUG
void SkString::validate() const {
// make sure know one has written over our global
- SkASSERT(gEmptyRec.fLength == 0);
- SkASSERT(gEmptyRec.fRefCnt == 0);
- SkASSERT(gEmptyRec.data()[0] == 0);
+ SkASSERT(0 == gEmptyRec.fLength);
+ SkASSERT(0 == gEmptyRec.fRefCnt);
+ SkASSERT(0 == gEmptyRec.data()[0]);
if (fRec != &gEmptyRec) {
SkASSERT(fRec->fLength > 0);
SkASSERT(fRec->fRefCnt > 0);
- SkASSERT(fRec->data()[fRec->fLength] == 0);
+ SkASSERT(0 == fRec->data()[fRec->fLength]);
}
SkASSERT(fStr == c_str());
}
if (fRec->fLength) {
SkASSERT(fRec->fRefCnt > 0);
- if (--fRec->fRefCnt == 0) {
+ if (sk_atomic_dec(&fRec->fRefCnt) == 1) {
sk_free(fRec);
}
}
if (fRec->fLength) {
SkASSERT(fRec->fRefCnt > 0);
- if (--fRec->fRefCnt == 0) {
+ if (sk_atomic_dec(&fRec->fRefCnt) == 1) {
sk_free(fRec);
}
}
if (fRec->fLength) {
if (fRec->fRefCnt > 1) {
- fRec->fRefCnt -= 1;
- fRec = AllocRec(fRec->data(), fRec->fLength);
+ Rec* rec = AllocRec(fRec->data(), fRec->fLength);
+ if (sk_atomic_dec(&fRec->fRefCnt) == 1) {
+ // In this case after our check of fRecCnt > 1, we suddenly
+ // did become the only owner, so now we have two copies of the
+ // data (fRec and rec), so we need to delete one of them.
+ sk_free(fRec);
+ }
+ fRec = rec;
#ifdef SK_DEBUG
fStr = fRec->data();
#endif
}
void SkString::set(const char text[], size_t len) {
- if (len == 0) {
+ if (0 == len) {
this->reset();
- } else if (fRec->fRefCnt == 1 && len <= fRec->fLength) {
+ } else if (1 == fRec->fRefCnt && len <= fRec->fLength) {
// should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))?
// just use less of the buffer without allocating a smaller one
char* p = this->writable_str();
memcpy(p, text, len);
}
p[len] = 0;
- fRec->fLength = SkToU16(len);
- } else if (fRec->fRefCnt == 1 && ((unsigned)fRec->fLength >> 2) == (len >> 2)) {
+ fRec->fLength = len;
+ } else if (1 == fRec->fRefCnt && (fRec->fLength >> 2) == (len >> 2)) {
// we have spare room in the current allocation, so don't alloc a larger one
char* p = this->writable_str();
if (text) {
memcpy(p, text, len);
}
p[len] = 0;
- fRec->fLength = SkToU16(len);
+ fRec->fLength = len;
} else {
SkString tmp(text, len);
this->swap(tmp);
}
void SkString::setUTF16(const uint16_t src[], size_t count) {
- if (count == 0) {
+ if (0 == count) {
this->reset();
} else if (count <= fRec->fLength) {
// should we resize if len <<<< fLength, to save RAM? (e.g. len < (fLength>>1))
which is equivalent for testing to (length + 1 + 3) >> 2 == (length + 1 + 3 + len) >> 2
and we can then eliminate the +1+3 since that doesn't affec the answer
*/
- if (fRec->fRefCnt == 1 && (length >> 2) == ((length + len) >> 2)) {
+ if (1 == fRec->fRefCnt && (length >> 2) == ((length + len) >> 2)) {
char* dst = this->writable_str();
if (offset < length) {
memcpy(dst + offset, text, len);
dst[length + len] = 0;
- fRec->fLength = SkToU16(length + len);
+ fRec->fLength = length + len;
} else {
/* Seems we should use realloc here, since that is safe if it fails
(we have the original data), and might be faster than alloc/copy/free.