X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDCoupling%2FMEDCouplingMemArray.txx;h=90773df2bf988fdc97fb6522ffaf3af13a069222;hb=6bfd8d6afb47fa46fd8fa0bc98d7fe057790460c;hp=a3f4d251088df5a568d93998d10e17c743017cfa;hpb=f1a947b32a36d8dc8e3079b25305bb50e8cb59a0;p=tools%2Fmedcoupling.git diff --git a/src/MEDCoupling/MEDCouplingMemArray.txx b/src/MEDCoupling/MEDCouplingMemArray.txx index a3f4d2510..90773df2b 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.txx +++ b/src/MEDCoupling/MEDCouplingMemArray.txx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2013 CEA/DEN, EDF R&D +// Copyright (C) 2007-2014 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -27,6 +27,7 @@ #include "InterpolationUtils.hxx" #include +#include #include namespace ParaMEDMEM @@ -46,42 +47,42 @@ namespace ParaMEDMEM } template - MemArray::MemArray(const MemArray& other):_nb_of_elem(0),_nb_of_elem_alloc(0),_ownership(false),_dealloc(CPP_DEALLOC) + MemArray::MemArray(const MemArray& other):_nb_of_elem(0),_nb_of_elem_alloc(0),_ownership(false),_dealloc(0),_param_for_deallocator(0) { if(!other._pointer.isNull()) { _nb_of_elem_alloc=other._nb_of_elem; - T *pointer=new T[_nb_of_elem_alloc]; + T *pointer=(T*)malloc(_nb_of_elem_alloc*sizeof(T)); std::copy(other._pointer.getConstPointer(),other._pointer.getConstPointer()+other._nb_of_elem,pointer); - useArray(pointer,true,CPP_DEALLOC,other._nb_of_elem); + useArray(pointer,true,C_DEALLOC,other._nb_of_elem); } } template void MemArray::useArray(const T *array, bool ownership, DeallocType type, std::size_t nbOfElem) { + destroy(); _nb_of_elem=nbOfElem; _nb_of_elem_alloc=nbOfElem; - destroy(); if(ownership) _pointer.setInternal(const_cast(array)); else _pointer.setExternal(array); _ownership=ownership; - _dealloc=type; + _dealloc=BuildFromType(type); } template void MemArray::useExternalArrayWithRWAccess(const T *array, std::size_t nbOfElem) { + destroy(); _nb_of_elem=nbOfElem; _nb_of_elem_alloc=nbOfElem; - destroy(); _pointer.setInternal(const_cast(array)); _ownership=false; - _dealloc=CPP_DEALLOC; + _dealloc=CPPDeallocator; } - + template void MemArray::writeOnPlace(std::size_t id, T element0, const T *others, std::size_t sizeOfOthers) { @@ -92,7 +93,7 @@ namespace ParaMEDMEM std::copy(others,others+sizeOfOthers,pointer+id+1); _nb_of_elem=std::max(_nb_of_elem,id+sizeOfOthers+1); } - + template template void MemArray::insertAtTheEnd(InputIterator first, InputIterator last) @@ -100,7 +101,7 @@ namespace ParaMEDMEM T *pointer=_pointer.getPointer(); while(first!=last) { - if(_nb_of_elem>=_nb_of_elem_alloc || _nb_of_elem==0) + if(_nb_of_elem>=_nb_of_elem_alloc) { reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1); pointer=_pointer.getPointer(); @@ -108,18 +109,18 @@ namespace ParaMEDMEM pointer[_nb_of_elem++]=*first++; } } - + template - void MemArray::pushBack(T elem) throw(INTERP_KERNEL::Exception) + void MemArray::pushBack(T elem) { if(_nb_of_elem>=_nb_of_elem_alloc) reserve(_nb_of_elem_alloc>0?2*_nb_of_elem_alloc:1); T *pt=getPointer(); pt[_nb_of_elem++]=elem; } - + template - T MemArray::popBack() throw(INTERP_KERNEL::Exception) + T MemArray::popBack() { if(_nb_of_elem>0) { @@ -128,12 +129,11 @@ namespace ParaMEDMEM } throw INTERP_KERNEL::Exception("MemArray::popBack : nothing to pop in array !"); } - + template void MemArray::pack() const { - if(_nb_of_elem>=0) - (const_cast * >(this))->reserve(_nb_of_elem); + (const_cast * >(this))->reserve(_nb_of_elem); } template @@ -180,7 +180,7 @@ namespace ParaMEDMEM { if(sl!=0) stream << _nb_of_elem/sl << std::endl << "Internal memory facts : " << _nb_of_elem << "/" << _nb_of_elem_alloc; - else + else stream << "Empty Data"; } else @@ -192,7 +192,7 @@ namespace ParaMEDMEM stream << "No data !\n"; return ret; } - + /*! * \param [in] sl is typically the number of components */ @@ -217,7 +217,7 @@ namespace ParaMEDMEM stream << "Empty Data\n"; } } - + /*! * \param [in] sl is typically the number of components */ @@ -257,14 +257,14 @@ namespace ParaMEDMEM else stream << "No data !\n"; } - + template void MemArray::fillWithValue(const T& val) { T *pt=_pointer.getPointer(); std::fill(pt,pt+_nb_of_elem,val); } - + template T *MemArray::fromNoInterlace(int nbOfComp) const { @@ -272,14 +272,14 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("MemArray::fromNoInterlace : number of components must be > 0 !"); const T *pt=_pointer.getConstPointer(); std::size_t nbOfTuples=_nb_of_elem/nbOfComp; - T *ret=new T[_nb_of_elem]; + T *ret=(T*)malloc(_nb_of_elem*sizeof(T)); T *w=ret; for(std::size_t i=0;i T *MemArray::toNoInterlace(int nbOfComp) const { @@ -287,7 +287,7 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("MemArray::toNoInterlace : number of components must be > 0 !"); const T *pt=_pointer.getConstPointer(); std::size_t nbOfTuples=_nb_of_elem/nbOfComp; - T *ret=new T[_nb_of_elem]; + T *ret=(T*)malloc(_nb_of_elem*sizeof(T)); T *w=ret; for(int i=0;i - void MemArray::alloc(std::size_t nbOfElements) throw(INTERP_KERNEL::Exception) + void MemArray::alloc(std::size_t nbOfElements) { destroy(); - if(nbOfElements<0) - throw INTERP_KERNEL::Exception("MemArray::alloc : request for negative length of data !"); _nb_of_elem=nbOfElements; _nb_of_elem_alloc=nbOfElements; - _pointer.setInternal(new T[_nb_of_elem_alloc]); + _pointer.setInternal((T*)malloc(_nb_of_elem_alloc*sizeof(T))); _ownership=true; - _dealloc=CPP_DEALLOC; + _dealloc=CDeallocator; } /*! @@ -355,21 +353,20 @@ namespace ParaMEDMEM * So this method should not be confused with MemArray::reserve that is close to MemArray::reAlloc but not same. */ template - void MemArray::reserve(std::size_t newNbOfElements) throw(INTERP_KERNEL::Exception) + void MemArray::reserve(std::size_t newNbOfElements) { - if(newNbOfElements<0) - throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !"); if(_nb_of_elem_alloc==newNbOfElements) return ; - T *pointer=new T[newNbOfElements]; + T *pointer=(T*)malloc(newNbOfElements*sizeof(T)); std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min(_nb_of_elem,newNbOfElements),pointer); if(_ownership) - destroyPointer(const_cast(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external + DestroyPointer(const_cast(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external _pointer.setInternal(pointer); _nb_of_elem=std::min(_nb_of_elem,newNbOfElements); _nb_of_elem_alloc=newNbOfElements; _ownership=true; - _dealloc=CPP_DEALLOC; + _dealloc=CDeallocator; + _param_for_deallocator=0; } /*! @@ -380,54 +377,68 @@ namespace ParaMEDMEM * So this method should not be confused with MemArray::reserve that is close to MemArray::reAlloc but not same. */ template - void MemArray::reAlloc(std::size_t newNbOfElements) throw(INTERP_KERNEL::Exception) + void MemArray::reAlloc(std::size_t newNbOfElements) { - if(newNbOfElements<0) - throw INTERP_KERNEL::Exception("MemArray::reAlloc : request for negative length of data !"); if(_nb_of_elem==newNbOfElements) return ; - T *pointer=new T[newNbOfElements]; + T *pointer=(T*)malloc(newNbOfElements*sizeof(T)); std::copy(_pointer.getConstPointer(),_pointer.getConstPointer()+std::min(_nb_of_elem,newNbOfElements),pointer); if(_ownership) - destroyPointer(const_cast(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external + DestroyPointer(const_cast(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external _pointer.setInternal(pointer); _nb_of_elem=newNbOfElements; _nb_of_elem_alloc=newNbOfElements; _ownership=true; - _dealloc=CPP_DEALLOC; + _dealloc=CDeallocator; + _param_for_deallocator=0; + } + + template + void MemArray::CPPDeallocator(void *pt, void *param) + { + delete [] reinterpret_cast(pt); } template - void MemArray::destroyPointer(T *pt, DeallocType type) + void MemArray::CDeallocator(void *pt, void *param) + { + free(pt); + } + + template + typename MemArray::Deallocator MemArray::BuildFromType(DeallocType type) { switch(type) - { + { case CPP_DEALLOC: - { - delete [] pt; - return ; - } + return CPPDeallocator; case C_DEALLOC: - { - free(pt); - return ; - } + return CDeallocator; default: - std::ostringstream stream; - stream << "Invalid deallocation requested for pointer " << pt; - throw INTERP_KERNEL::Exception(stream.str().c_str()); - } + throw INTERP_KERNEL::Exception("Invalid deallocation requested ! Unrecognized enum DeallocType !"); + } + } + + template + void MemArray::DestroyPointer(T *pt, typename MemArray::Deallocator dealloc, void *param) + { + if(dealloc) + dealloc(pt,param); } template void MemArray::destroy() { if(_ownership) - destroyPointer(const_cast(_pointer.getConstPointer()),_dealloc);//Do not use getPointer because in case of _external + DestroyPointer(const_cast(_pointer.getConstPointer()),_dealloc,_param_for_deallocator);//Do not use getPointer because in case of _external _pointer.null(); _ownership=false; + _dealloc=NULL; + _param_for_deallocator=NULL; + _nb_of_elem=0; + _nb_of_elem_alloc=0; } - + template MemArray &MemArray::operator=(const MemArray& other) {