[Scummvm-cvs-logs] scummvm master -> 9081ab440242da4e3e7373f0d044c7373f97b5dc

fingolfin max at quendi.de
Mon May 16 14:25:57 CEST 2011


This automated email contains information about 3 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
85d5eec950 COMMON: Set _capacity to just _size in Common::Array::operator=
eedb2d721f COMMON: Change Array::insert_aux to immediately assign newly allocated memory to _storage
9081ab4402 COMMON: Unify Array memory allocation


Commit: 85d5eec95056f438bca709a0d3998885fb81e774
    https://github.com/scummvm/scummvm/commit/85d5eec95056f438bca709a0d3998885fb81e774
Author: Max Horn (max at quendi.de)
Date: 2011-05-16T04:50:06-07:00

Commit Message:
COMMON: Set _capacity to just _size in Common::Array::operator=

Changed paths:
    common/array.h



diff --git a/common/array.h b/common/array.h
index 9b94709..6f8e7da 100644
--- a/common/array.h
+++ b/common/array.h
@@ -179,7 +179,7 @@ public:
 
 		delete[] _storage;
 		_size = array._size;
-		_capacity = _size + 32;
+		_capacity = _size;
 		_storage = new T[_capacity];
 		assert(_storage);
 		copy(array._storage, array._storage + _size, _storage);


Commit: eedb2d721fa2577a02695f95135f856b97e224c8
    https://github.com/scummvm/scummvm/commit/eedb2d721fa2577a02695f95135f856b97e224c8
Author: Max Horn (max at quendi.de)
Date: 2011-05-16T04:58:59-07:00

Commit Message:
COMMON: Change Array::insert_aux to immediately assign newly allocated memory to _storage

Changed paths:
    common/array.h



diff --git a/common/array.h b/common/array.h
index 6f8e7da..a1db9a6 100644
--- a/common/array.h
+++ b/common/array.h
@@ -238,15 +238,15 @@ public:
 		if (newCapacity <= _capacity)
 			return;
 
-		T *old_storage = _storage;
+		T *oldStorage = _storage;
 		_capacity = newCapacity;
 		_storage = new T[newCapacity];
 		assert(_storage);
 
-		if (old_storage) {
+		if (oldStorage) {
 			// Copy old data
-			copy(old_storage, old_storage + _size, _storage);
-			delete[] old_storage;
+			copy(oldStorage, oldStorage + _size, _storage);
+			delete[] oldStorage;
 		}
 	}
 
@@ -286,29 +286,28 @@ protected:
 		const uint n = last - first;
 		if (n) {
 			const uint idx = pos - _storage;
-			T *newStorage = _storage;
+			T *oldStorage = _storage;
 			if (_size + n > _capacity) {
 				// If there is not enough space, allocate more and
 				// copy old elements over.
 				uint newCapacity = roundUpCapacity(_size + n);
-				newStorage = new T[newCapacity];
-				assert(newStorage);
-				copy(_storage, _storage + idx, newStorage);
-				pos = newStorage + idx;
+				_storage = new T[newCapacity];
+				assert(_storage);
+				copy(oldStorage, oldStorage + idx, _storage);
+				pos = _storage + idx;
 			}
 
 			// Make room for the new elements by shifting back
 			// existing ones.
-			copy_backward(_storage + idx, _storage + _size, newStorage + _size + n);
+			copy_backward(oldStorage + idx, oldStorage + _size, _storage + _size + n);
 
 			// Insert the new elements.
 			copy(first, last, pos);
 
 			// Finally, update the internal state
-			if (newStorage != _storage) {
-				delete[] _storage;
+			if (_storage != oldStorage) {
+				delete[] oldStorage;
 				_capacity = roundUpCapacity(_size + n);
-				_storage = newStorage;
 			}
 			_size += n;
 		}


Commit: 9081ab440242da4e3e7373f0d044c7373f97b5dc
    https://github.com/scummvm/scummvm/commit/9081ab440242da4e3e7373f0d044c7373f97b5dc
Author: Max Horn (max at quendi.de)
Date: 2011-05-16T05:22:54-07:00

Commit Message:
COMMON: Unify Array memory allocation

We also change how alloc failures are handled: Instead of using
assert(), which is usually disabled in release builds on various
platforms, we now *always* catch this situation and invoke error() if
necessary.

Changed paths:
    common/array.h



diff --git a/common/array.h b/common/array.h
index a1db9a6..26ee2af 100644
--- a/common/array.h
+++ b/common/array.h
@@ -24,6 +24,7 @@
 
 #include "common/scummsys.h"
 #include "common/algorithm.h"
+#include "common/textconsole.h"	// For error()
 
 namespace Common {
 
@@ -72,8 +73,7 @@ public:
 
 	Array(const Array<T> &array) : _capacity(array._size), _size(array._size), _storage(0) {
 		if (array._storage) {
-			_storage = new T[_capacity];
-			assert(_storage);
+			allocCapacity(_size);
 			copy(array._storage, array._storage + _size, _storage);
 		}
 	}
@@ -83,9 +83,8 @@ public:
 	 */
 	template<class T2>
 	Array(const T2 *data, int n) {
-		_capacity = _size = n;
-		_storage = new T[_capacity];
-		assert(_storage);
+		_size = n;
+		allocCapacity(n);
 		copy(data, data + _size, _storage);
 	}
 
@@ -179,9 +178,7 @@ public:
 
 		delete[] _storage;
 		_size = array._size;
-		_capacity = _size;
-		_storage = new T[_capacity];
-		assert(_storage);
+		allocCapacity(_size);
 		copy(array._storage, array._storage + _size, _storage);
 
 		return *this;
@@ -239,9 +236,7 @@ public:
 			return;
 
 		T *oldStorage = _storage;
-		_capacity = newCapacity;
-		_storage = new T[newCapacity];
-		assert(_storage);
+		allocCapacity(newCapacity);
 
 		if (oldStorage) {
 			// Copy old data
@@ -267,6 +262,13 @@ protected:
 		return capa;
 	}
 
+	void allocCapacity(uint capacity) {
+		_capacity = capacity;
+		_storage = new T[capacity];
+		if (!_storage)
+			::error("Common::Array: failure to allocate %d bytes", capacity);
+	}
+
 	/**
 	 * Insert a range of elements coming from this or another array.
 	 * Unlike std::vector::insert, this method does not accept
@@ -290,9 +292,7 @@ protected:
 			if (_size + n > _capacity) {
 				// If there is not enough space, allocate more and
 				// copy old elements over.
-				uint newCapacity = roundUpCapacity(_size + n);
-				_storage = new T[newCapacity];
-				assert(_storage);
+				allocCapacity(roundUpCapacity(_size + n));
 				copy(oldStorage, oldStorage + idx, _storage);
 				pos = _storage + idx;
 			}
@@ -307,7 +307,6 @@ protected:
 			// Finally, update the internal state
 			if (_storage != oldStorage) {
 				delete[] oldStorage;
-				_capacity = roundUpCapacity(_size + n);
 			}
 			_size += n;
 		}






More information about the Scummvm-git-logs mailing list