Changeset 12448
- Timestamp:
- 06/18/12 14:08:17 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Alloc/xNewDelete.h
r12447 r12448 61 61 T* xReNew(T* old, unsigned int old_size, unsigned int size) { 62 62 #ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES 63 T* aT_p= NULL;63 T* aT_p=0; 64 64 if (!old) { // no old memory 65 65 if (size) 66 aT_p=xNew<T>(size); 66 aT_p=xNew<T>(size); // according to realloc behavior in manual page 67 67 } 68 68 else { // have old memory 69 69 if (!size) // but 0 size 70 xDelete<T>(old); // according to realloc behavior in manual page 71 else { // non-zero size 72 assert(old_size); // have old memory - need to have old_size set or this call is bad 73 // allocate new, delete old; ; even for the case when size is 74 // less than old_size we can't just keep the memory unchanged 75 // because otherwise classes that have ctors/dtors with side-effects 76 // may misbehave, for example classes with static instance/operations counters. 77 aT_p=xNew<T>(size); 78 unsigned int iMax=(old_size<size)?old_size:size; 79 for (unsigned int i=0; i<iMax;++i) { 80 // we need to copy the items by explicit assignments 81 aT_p[i]=old[i]; 82 } 70 83 xDelete<T>(old); 71 else { // non-zero size72 if (size>old_size) { // do something only if it is bigger73 aT_p=xNew<T>(size);74 for (unsigned int i=0; i<old_size;++i)75 aT_p[i]=old[i]; // copy the items76 xDelete<T>(old);77 }78 else // size is equal or less than old size79 aT_p=old; // do nothing80 84 } 81 85 } 82 86 return aT_p; 83 #else 84 T* aT_p=(T*)realloc((void*)old,size*sizeof(T)); 85 assert(aT_p); 87 #else 88 T* aT_p=0; 89 aT_p=(T*)realloc((void*)old,size*sizeof(T)); 90 if (size) 91 assert(aT_p); // according to realloc behavior in manual page 86 92 return aT_p; 87 93 #endif
Note:
See TracChangeset
for help on using the changeset viewer.