46 lines
927 B
Plaintext
46 lines
927 B
Plaintext
#ifndef tools_realloc
|
|
#define tools_realloc
|
|
|
|
#include "typedefs"
|
|
|
|
#include <cstring> //memcpy
|
|
|
|
namespace tools {
|
|
|
|
template <class T>
|
|
inline bool realloc(T*& a_pointer,uint32 a_new_size,uint32 a_old_size,bool a_init = false) {
|
|
if(!a_new_size) {
|
|
delete [] a_pointer;
|
|
a_pointer = 0;
|
|
return true;
|
|
}
|
|
if(!a_pointer) {
|
|
a_pointer = new T[a_new_size];
|
|
return true;
|
|
}
|
|
if(a_old_size==a_new_size) return true;
|
|
T* pointer = new T[a_new_size];
|
|
if(!pointer) {
|
|
delete [] a_pointer;
|
|
a_pointer = 0;
|
|
return false;
|
|
}
|
|
if(a_new_size>a_old_size) {
|
|
::memcpy(pointer,a_pointer,a_old_size*sizeof(T));
|
|
if(a_init){
|
|
uint32 num = a_new_size-a_old_size;
|
|
T* pos = pointer+a_old_size;
|
|
for(uint32 i=0;i<num;i++,pos++) *pos = T();
|
|
}
|
|
} else {
|
|
::memcpy(pointer,a_pointer,a_new_size*sizeof(T));
|
|
}
|
|
delete [] a_pointer;
|
|
a_pointer = pointer;
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|