Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/cmemT is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_cmemT
0005 #define tools_cmemT
0006 
0007 #include <cstdlib>
0008 #include <cstring> //memcpy
0009 
0010 namespace tools {
0011 
0012 template <class T>
0013 inline void cmem_free(T*& a_p){
0014   if(!a_p) return;
0015   ::free(a_p);
0016   a_p = NULL;
0017 }
0018 
0019 template <class T>
0020 inline T* cmem_alloc(size_t a_num){
0021   if(a_num<=0) return 0;
0022   T* p = (T*)::malloc(a_num*sizeof(T));
0023   if(!p) return 0;
0024   return p;
0025 }
0026 
0027 template <class T>
0028 inline bool cmem_realloc(T*& a_pointer,size_t a_new_size,size_t a_old_size,bool a_init = false) {
0029   if(!a_new_size) {
0030     delete [] a_pointer;
0031     a_pointer = 0;
0032     return true;
0033   }
0034   if(!a_pointer) {
0035     a_pointer = new T[a_new_size];
0036     if(!a_pointer) return false;
0037     return true;
0038   }
0039   if(a_old_size==a_new_size) return true;
0040   T* pointer = new T[a_new_size];
0041   if(!pointer) {
0042     delete [] a_pointer;
0043     a_pointer = 0;
0044     return false;
0045   }
0046   if(a_new_size>a_old_size) {
0047     ::memcpy(pointer,a_pointer,a_old_size*sizeof(T));
0048     if(a_init){
0049       size_t num = a_new_size-a_old_size;
0050       T* pos = pointer+a_old_size;
0051       for(size_t i=0;i<num;i++,pos++) *pos = T();
0052     }
0053   } else {
0054     ::memcpy(pointer,a_pointer,a_new_size*sizeof(T));
0055   }
0056   delete [] a_pointer;
0057   a_pointer = pointer;
0058   return true;
0059 }
0060 
0061 }
0062 
0063 #endif