mirror of
https://github.com/libretro/scummvm.git
synced 2025-04-14 04:30:43 +00:00
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:
parent
f108a31ad7
commit
882c24d2ee
@ -54,6 +54,7 @@ public:
|
|||||||
Array(const Array<T> &array) : _capacity(0), _size(0), _storage(0) {
|
Array(const Array<T> &array) : _capacity(0), _size(0), _storage(0) {
|
||||||
_capacity = _size = array._size;
|
_capacity = _size = array._size;
|
||||||
_storage = new T[_capacity];
|
_storage = new T[_capacity];
|
||||||
|
assert(_storage);
|
||||||
copy(array._storage, array._storage + _size, _storage);
|
copy(array._storage, array._storage + _size, _storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,6 +65,7 @@ public:
|
|||||||
Array(const T2 *data, int n) {
|
Array(const T2 *data, int n) {
|
||||||
_capacity = _size = n;
|
_capacity = _size = n;
|
||||||
_storage = new T[_capacity];
|
_storage = new T[_capacity];
|
||||||
|
assert(_storage);
|
||||||
copy(data, data + _size, _storage);
|
copy(data, data + _size, _storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,6 +151,7 @@ public:
|
|||||||
_size = array._size;
|
_size = array._size;
|
||||||
_capacity = _size + 32;
|
_capacity = _size + 32;
|
||||||
_storage = new T[_capacity];
|
_storage = new T[_capacity];
|
||||||
|
assert(_storage);
|
||||||
copy(array._storage, array._storage + _size, _storage);
|
copy(array._storage, array._storage + _size, _storage);
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
@ -193,6 +196,7 @@ public:
|
|||||||
T *old_storage = _storage;
|
T *old_storage = _storage;
|
||||||
_capacity = newCapacity;
|
_capacity = newCapacity;
|
||||||
_storage = new T[newCapacity];
|
_storage = new T[newCapacity];
|
||||||
|
assert(_storage);
|
||||||
|
|
||||||
if (old_storage) {
|
if (old_storage) {
|
||||||
// Copy old data
|
// Copy old data
|
||||||
|
@ -247,6 +247,7 @@ protected:
|
|||||||
*/
|
*/
|
||||||
void insert(NodeBase *pos, const t_T &element) {
|
void insert(NodeBase *pos, const t_T &element) {
|
||||||
ListInternal::NodeBase *newNode = new Node(element);
|
ListInternal::NodeBase *newNode = new Node(element);
|
||||||
|
assert(newNode);
|
||||||
|
|
||||||
newNode->_next = pos;
|
newNode->_next = pos;
|
||||||
newNode->_prev = pos->_prev;
|
newNode->_prev = pos->_prev;
|
||||||
|
@ -79,7 +79,7 @@ void String::initWithCStr(const char *str, uint32 len) {
|
|||||||
// Not enough internal storage, so allocate more
|
// Not enough internal storage, so allocate more
|
||||||
_extern._capacity = computeCapacity(len+1);
|
_extern._capacity = computeCapacity(len+1);
|
||||||
_extern._refCount = 0;
|
_extern._refCount = 0;
|
||||||
_str = (char *)malloc(_extern._capacity);
|
_str = new char[_extern._capacity];
|
||||||
assert(_str != 0);
|
assert(_str != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +159,7 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) {
|
|||||||
newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1));
|
newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1));
|
||||||
|
|
||||||
// Allocate new storage
|
// Allocate new storage
|
||||||
newStorage = (char *)malloc(newCapacity);
|
newStorage = new char[newCapacity];
|
||||||
assert(newStorage);
|
assert(newStorage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,8 +190,10 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) {
|
|||||||
void String::incRefCount() const {
|
void String::incRefCount() const {
|
||||||
assert(!isStorageIntern());
|
assert(!isStorageIntern());
|
||||||
if (_extern._refCount == 0) {
|
if (_extern._refCount == 0) {
|
||||||
if (g_refCountPool == 0)
|
if (g_refCountPool == 0) {
|
||||||
g_refCountPool = new MemoryPool(sizeof(int));
|
g_refCountPool = new MemoryPool(sizeof(int));
|
||||||
|
assert(g_refCountPool);
|
||||||
|
}
|
||||||
|
|
||||||
_extern._refCount = (int *)g_refCountPool->allocChunk();
|
_extern._refCount = (int *)g_refCountPool->allocChunk();
|
||||||
*_extern._refCount = 2;
|
*_extern._refCount = 2;
|
||||||
@ -214,7 +216,7 @@ void String::decRefCount(int *oldRefCount) {
|
|||||||
assert(g_refCountPool);
|
assert(g_refCountPool);
|
||||||
g_refCountPool->freeChunk(oldRefCount);
|
g_refCountPool->freeChunk(oldRefCount);
|
||||||
}
|
}
|
||||||
free(_str);
|
delete _str;
|
||||||
|
|
||||||
// Even though _str points to a freed memory block now,
|
// Even though _str points to a freed memory block now,
|
||||||
// we do not change its value, because any code that calls
|
// we do not change its value, because any code that calls
|
||||||
|
Loading…
x
Reference in New Issue
Block a user