public:
static void CPPDeallocator(void *pt, void *param);
static void CDeallocator(void *pt, void *param);
+ static void COffsetDeallocator(void *pt, void *param);
private:
static void DestroyPointer(T *pt, Deallocator dealloc, void *param);
static Deallocator BuildFromType(DeallocType type);
free(pt);
}
+ template<class T>
+ void MemArray<T>::COffsetDeallocator(void *pt, void *param)
+ {
+ int64_t *offset(reinterpret_cast<int64_t *>(param));
+ char *ptcast(reinterpret_cast<char *>(pt));
+ free(ptcast+*offset);
+ }
+
template<class T>
typename MemArray<T>::Deallocator MemArray<T>::BuildFromType(DeallocType type)
{
return CPPDeallocator;
case DeallocType::C_DEALLOC:
return CDeallocator;
+ case DeallocType::C_DEALLOC_WITH_OFFSET:
+ return COffsetDeallocator;
default:
throw INTERP_KERNEL::Exception("Invalid deallocation requested ! Unrecognized enum DeallocType !");
}
enum class DeallocType
{
C_DEALLOC = 2,
- CPP_DEALLOC = 3
+ CPP_DEALLOC = 3,
+ C_DEALLOC_WITH_OFFSET = 4
};
//! The various spatial discretization of a field
{
typedef void (*MyDeallocator)(void *,void *);
MyDeallocator deall=(MyDeallocator)wronc[1];
- deall(pt,NULL);
+ int64_t *offset=reinterpret_cast<int64_t*>(wronc[2]);
+ deall(pt,offset);
+ delete offset;
Py_XDECREF(weakRefOnOwner);
}
delete [] wronc;
}
else
{
- ret->useArray(reinterpret_cast<const T *>(data),true,MEDCoupling::DeallocType::C_DEALLOC,sz0,sz1);
+ ret->useArray(reinterpret_cast<const T *>(data),true,MEDCoupling::DeallocType::C_DEALLOC_WITH_OFFSET,sz0,sz1);
PyObject *ref=PyWeakref_NewRef(reinterpret_cast<PyObject *>(eltOwning),NULL);
- typename MEDCoupling::MemArray<T>::Deallocator tmp(MEDCoupling::MemArray<T>::CDeallocator);
+ typename MEDCoupling::MemArray<T>::Deallocator tmp(MEDCoupling::MemArray<T>::COffsetDeallocator);
void **tmp2 = reinterpret_cast<void**>(&tmp); // MSVC2010 does not support constructor()
- void **objs=new void *[2]; objs[0]=ref; objs[1]=*tmp2;
+ const char *dataEltOwning(PyArray_BYTES(eltOwning));//In case of input array is a sub array of a 2D,3D... array there is an offset
+ int64_t offset(0);
+ if(data!=dataEltOwning)
+ {
+ offset=data>dataEltOwning?-((int64_t)(std::distance(dataEltOwning,data))):(int64_t)std::distance(data,dataEltOwning);
+ }
+ void **objs=new void *[3]; objs[0]=ref; objs[1]=*tmp2; objs[2]=new int64_t(offset);
mma.setParameterForDeallocator(objs);
mma.setSpecificDeallocator(numarrdeal);
}
PyObject *ref(PyWeakref_NewRef(ret,NULL));
typename MEDCoupling::MemArray<T>::Deallocator tmp(mem.getDeallocator());
void **tmp2 = reinterpret_cast<void**>(&tmp); // MSVC2010 does not support constructor()
- void **objs=new void *[2]; objs[0]=reinterpret_cast<void*>(ref); objs[1]=*tmp2;
+ void **objs=new void *[3]; objs[0]=reinterpret_cast<void*>(ref); objs[1]=*tmp2; objs[2]=new int64_t(0);
mem.setParameterForDeallocator(objs);
mem.setSpecificDeallocator(numarrdeal);
return ret;
if sys.platform == "win32":
from MEDCouplingCompat import *
else:
- from MEDCoupling import *
+ from medcoupling import *
if MEDCouplingHasNumPyBindings():
from numpy import *
self.assertTrue(not b.flags["OWNDATA"])
pass
+ @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
+ def test41(self):
+ """ This non regression test is focused on a numpy subarray of a bigger numpy array. Typically a 1D array coming from a 2D array. When medcoupling takes the ownership, medcoupling must store an offset to deallocate correctly the pointer. The pointer of medcoupling array is NOT the pointer to be transmited to free. The offset is typically the distance between the start of the main 2D array and the start of 1D array medcouplingized."""
+ import numpy as np
+ array = np.array([[1,2,3,10],[4,5,6,20],[7,8,9,30]],dtype=np.float64) # create a 2D array
+ b = array[2] # b data pointer starts at array+2*4*sizeof(float64) so offset is expected to be equal to -2*4*sizeof(float64)=-64
+ self.assertTrue(array.flags["OWNDATA"])
+ self.assertTrue(not b.flags["OWNDATA"])
+ d=DataArrayDouble(b)
+ self.assertTrue(not array.flags["OWNDATA"])
+ self.assertTrue(not b.flags["OWNDATA"])
+ del b ; gc.collect()
+ del array ; gc.collect()
+ del d ; gc.collect() # important : destroy d after b and array to be sure to let the ownership to d.
+ pass
+
def setUp(self):
pass
pass