COMMON: Check for failed memory allocations; changed Common::String to use new/delete instead of malloc/free

svn-id: r40291
This commit is contained in:
Max Horn 2009-05-03 22:45:31 +00:00
parent f108a31ad7
commit 882c24d2ee
3 changed files with 11 additions and 4 deletions

View File

@ -54,6 +54,7 @@ public:
Array(const Array<T> &array) : _capacity(0), _size(0), _storage(0) {
_capacity = _size = array._size;
_storage = new T[_capacity];
assert(_storage);
copy(array._storage, array._storage + _size, _storage);
}
@ -64,6 +65,7 @@ public:
Array(const T2 *data, int n) {
_capacity = _size = n;
_storage = new T[_capacity];
assert(_storage);
copy(data, data + _size, _storage);
}
@ -149,6 +151,7 @@ public:
_size = array._size;
_capacity = _size + 32;
_storage = new T[_capacity];
assert(_storage);
copy(array._storage, array._storage + _size, _storage);
return *this;
@ -193,6 +196,7 @@ public:
T *old_storage = _storage;
_capacity = newCapacity;
_storage = new T[newCapacity];
assert(_storage);
if (old_storage) {
// Copy old data

View File

@ -247,6 +247,7 @@ protected:
*/
void insert(NodeBase *pos, const t_T &element) {
ListInternal::NodeBase *newNode = new Node(element);
assert(newNode);
newNode->_next = pos;
newNode->_prev = pos->_prev;

View File

@ -79,7 +79,7 @@ void String::initWithCStr(const char *str, uint32 len) {
// Not enough internal storage, so allocate more
_extern._capacity = computeCapacity(len+1);
_extern._refCount = 0;
_str = (char *)malloc(_extern._capacity);
_str = new char[_extern._capacity];
assert(_str != 0);
}
@ -159,7 +159,7 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) {
newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1));
// Allocate new storage
newStorage = (char *)malloc(newCapacity);
newStorage = new char[newCapacity];
assert(newStorage);
}
@ -190,8 +190,10 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) {
void String::incRefCount() const {
assert(!isStorageIntern());
if (_extern._refCount == 0) {
if (g_refCountPool == 0)
if (g_refCountPool == 0) {
g_refCountPool = new MemoryPool(sizeof(int));
assert(g_refCountPool);
}
_extern._refCount = (int *)g_refCountPool->allocChunk();
*_extern._refCount = 2;
@ -214,7 +216,7 @@ void String::decRefCount(int *oldRefCount) {
assert(g_refCountPool);
g_refCountPool->freeChunk(oldRefCount);
}
free(_str);
delete _str;
// Even though _str points to a freed memory block now,
// we do not change its value, because any code that calls