Lazily allocate _refCount in class String
svn-id: r22935
This commit is contained in:
parent
fd4feeada3
commit
6d0a06463a
2 changed files with 19 additions and 16 deletions
|
@ -46,8 +46,6 @@ static int computeCapacity(int len) {
|
||||||
String::String(const char *str, int len, int capacity)
|
String::String(const char *str, int len, int capacity)
|
||||||
: _str(0), _len(0), _refCount(0) {
|
: _str(0), _len(0), _refCount(0) {
|
||||||
|
|
||||||
incRefCount();
|
|
||||||
|
|
||||||
if (str && *str && len != 0) {
|
if (str && *str && len != 0) {
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
_len = len;
|
_len = len;
|
||||||
|
@ -68,9 +66,14 @@ String::String(const char *str, int len, int capacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String(const String &str)
|
String::String(const String &str)
|
||||||
: _str(str._str), _len(str._len), _refCount(str._refCount), _capacity(str._capacity) {
|
: _str(str._str), _len(str._len), _refCount(0), _capacity(str._capacity) {
|
||||||
|
|
||||||
incRefCount();
|
if (_str != 0) {
|
||||||
|
// If the string we are copying is non-empty, we increment its
|
||||||
|
// refcount.
|
||||||
|
str.incRefCount();
|
||||||
|
_refCount = str._refCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String::~String() {
|
String::~String() {
|
||||||
|
@ -79,18 +82,21 @@ String::~String() {
|
||||||
|
|
||||||
void String::incRefCount() const {
|
void String::incRefCount() const {
|
||||||
if (_refCount == 0) {
|
if (_refCount == 0) {
|
||||||
_refCount = new int(1);
|
_refCount = new int(2);
|
||||||
} else {
|
} else {
|
||||||
++(*_refCount);
|
++(*_refCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void String::decRefCount() {
|
void String::decRefCount() {
|
||||||
assert(_refCount);
|
if (_refCount) {
|
||||||
--(*_refCount);
|
--(*_refCount);
|
||||||
if (*_refCount <= 0) {
|
}
|
||||||
|
if (!_refCount || *_refCount <= 0) {
|
||||||
delete _refCount;
|
delete _refCount;
|
||||||
|
_refCount = 0;
|
||||||
free(_str);
|
free(_str);
|
||||||
|
_str = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,9 +109,8 @@ String& String::operator =(const char *str) {
|
||||||
memcpy(_str, str, _len + 1);
|
memcpy(_str, str, _len + 1);
|
||||||
} else if (_len > 0) {
|
} else if (_len > 0) {
|
||||||
decRefCount();
|
decRefCount();
|
||||||
_refCount = 0;
|
|
||||||
incRefCount();
|
|
||||||
|
|
||||||
|
_refCount = 0;
|
||||||
_capacity = 0;
|
_capacity = 0;
|
||||||
_len = 0;
|
_len = 0;
|
||||||
_str = 0;
|
_str = 0;
|
||||||
|
@ -212,9 +217,8 @@ void String::deleteChar(int p) {
|
||||||
void String::clear() {
|
void String::clear() {
|
||||||
if (_capacity) {
|
if (_capacity) {
|
||||||
decRefCount();
|
decRefCount();
|
||||||
_refCount = 0;
|
|
||||||
incRefCount();
|
|
||||||
|
|
||||||
|
_refCount = 0;
|
||||||
_capacity = 0;
|
_capacity = 0;
|
||||||
_len = 0;
|
_len = 0;
|
||||||
_str = 0;
|
_str = 0;
|
||||||
|
@ -254,7 +258,7 @@ void String::toUppercase() {
|
||||||
void String::ensureCapacity(int new_len, bool keep_old) {
|
void String::ensureCapacity(int new_len, bool keep_old) {
|
||||||
// If there is not enough space, or if we are not the only owner
|
// If there is not enough space, or if we are not the only owner
|
||||||
// of the current data, then we have to reallocate it.
|
// of the current data, then we have to reallocate it.
|
||||||
if (new_len <= _capacity && *_refCount == 1)
|
if (new_len <= _capacity && (_refCount == 0 || *_refCount == 1))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int newCapacity = computeCapacity(new_len);
|
int newCapacity = computeCapacity(new_len);
|
||||||
|
@ -273,9 +277,8 @@ void String::ensureCapacity(int new_len, bool keep_old) {
|
||||||
}
|
}
|
||||||
|
|
||||||
decRefCount();
|
decRefCount();
|
||||||
_refCount = 0;
|
|
||||||
incRefCount();
|
|
||||||
|
|
||||||
|
_refCount = 0;
|
||||||
_capacity = newCapacity;
|
_capacity = newCapacity;
|
||||||
_str = newStr;
|
_str = newStr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public:
|
||||||
static const char *emptyString;
|
static const char *emptyString;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
String() : _str(0), _len(0), _refCount(0), _capacity(0) { incRefCount(); }
|
String() : _str(0), _len(0), _refCount(0), _capacity(0) {}
|
||||||
String(const char *str, int len = -1, int capacity = 16);
|
String(const char *str, int len = -1, int capacity = 16);
|
||||||
String(const String &str);
|
String(const String &str);
|
||||||
virtual ~String();
|
virtual ~String();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue