Warning, /include/Geant4/tools/realloc is written in an unsupported language. File is not indexed.
0001 #ifndef tools_realloc
0002 #define tools_realloc
0003
0004 #include <cstring> //memcpy
0005
0006 namespace tools {
0007
0008 template <class T>
0009 inline bool realloc(T*& a_pointer,size_t a_new_size,size_t a_old_size,bool a_init = false) {
0010 if(!a_new_size) {
0011 delete [] a_pointer;
0012 a_pointer = 0;
0013 return true;
0014 }
0015 if(!a_pointer) {
0016 a_pointer = new T[a_new_size];
0017 if(!a_pointer) return false;
0018 return true;
0019 }
0020 if(a_old_size==a_new_size) return true;
0021 T* pointer = new T[a_new_size];
0022 if(!pointer) {
0023 delete [] a_pointer;
0024 a_pointer = 0;
0025 return false;
0026 }
0027 if(a_new_size>a_old_size) {
0028 ::memcpy(pointer,a_pointer,a_old_size*sizeof(T));
0029 if(a_init){
0030 size_t num = a_new_size-a_old_size;
0031 T* pos = pointer+a_old_size;
0032 for(size_t i=0;i<num;i++,pos++) *pos = T();
0033 }
0034 } else {
0035 ::memcpy(pointer,a_pointer,a_new_size*sizeof(T));
0036 }
0037 delete [] a_pointer;
0038 a_pointer = pointer;
0039 return true;
0040 }
0041
0042 }
0043
0044 #endif