[Scummvm-cvs-logs] SF.net SVN: scummvm: [22935] scummvm/trunk/common

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Mon Jun 5 19:36:15 CEST 2006


Revision: 22935
Author:   fingolfin
Date:     2006-06-05 10:36:08 -0700 (Mon, 05 Jun 2006)
ViewCVS:  http://svn.sourceforge.net/scummvm/?rev=22935&view=rev

Log Message:
-----------
Lazily allocate _refCount in class String

Modified Paths:
--------------
    scummvm/trunk/common/str.cpp
    scummvm/trunk/common/str.h
Modified: scummvm/trunk/common/str.cpp
===================================================================
--- scummvm/trunk/common/str.cpp	2006-06-05 14:52:57 UTC (rev 22934)
+++ scummvm/trunk/common/str.cpp	2006-06-05 17:36:08 UTC (rev 22935)
@@ -46,8 +46,6 @@
 String::String(const char *str, int len, int capacity)
 : _str(0), _len(0), _refCount(0) {
 
-	incRefCount();
-
 	if (str && *str && len != 0) {
 		if (len > 0)
 			_len = len;
@@ -68,9 +66,14 @@
 }
 
 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() {
@@ -79,18 +82,21 @@
 
 void String::incRefCount() const {
 	if (_refCount == 0) {
-		_refCount = new int(1);
+		_refCount = new int(2);
 	} else {
 		++(*_refCount);
 	}
 }
 
 void String::decRefCount() {
-	assert(_refCount);
-	--(*_refCount);
-	if (*_refCount <= 0) {
+	if (_refCount) {
+		--(*_refCount);
+	}
+	if (!_refCount || *_refCount <= 0) {
 		delete _refCount;
+		_refCount = 0;
 		free(_str);
+		_str = 0;
 	}
 }
 
@@ -103,9 +109,8 @@
 		memcpy(_str, str, _len + 1);
 	} else if (_len > 0) {
 		decRefCount();
+
 		_refCount = 0;
-		incRefCount();
-
 		_capacity = 0;
 		_len = 0;
 		_str = 0;
@@ -212,9 +217,8 @@
 void String::clear() {
 	if (_capacity) {
 		decRefCount();
+
 		_refCount = 0;
-		incRefCount();
-
 		_capacity = 0;
 		_len = 0;
 		_str = 0;
@@ -254,7 +258,7 @@
 void String::ensureCapacity(int new_len, bool keep_old) {
 	// If there is not enough space, or if we are not the only owner
 	// 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;
 
 	int newCapacity = computeCapacity(new_len);
@@ -273,9 +277,8 @@
 	}
 
 	decRefCount();
+
 	_refCount = 0;
-	incRefCount();
-
 	_capacity = newCapacity;
 	_str = newStr;
 }

Modified: scummvm/trunk/common/str.h
===================================================================
--- scummvm/trunk/common/str.h	2006-06-05 14:52:57 UTC (rev 22934)
+++ scummvm/trunk/common/str.h	2006-06-05 17:36:08 UTC (rev 22935)
@@ -44,7 +44,7 @@
 	static const char *emptyString;
 #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 String &str);
 	virtual ~String();


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.





More information about the Scummvm-git-logs mailing list