p = (non_const_value_type*)PyArray_DATA(arr);
py_obj = obj;
}
~mem_block(){ // delete memory manually iif py_obj is not set. Otherwise the python interpreter will do that for us.
TRACE_MEM_DEBUG("deleting mem block p ="<ob_refcnt: -1));
if (py_obj==NULL) { if (p) { delete[] p;} }
else Py_DECREF(py_obj);
}
void operator=(const mem_block & X) {
//assert( py_obj==NULL);
assert(size_==X.size_);assert(p); assert(X.p);
storages::memcopy (p, X.p, size_);
}
mem_block ( mem_block const & X): size_(X.size()), py_obj(NULL) {
//p = new non_const_value_type[s]; // check speed penalty for try ??
try { p = new non_const_value_type[X.size()];}
catch (std::bad_alloc& ba) { TRIQS_RUNTIME_ERROR<< "Memory allocation error : bad_alloc : "<< ba.what();}
if ((X.py_obj==NULL) || (PyCObject_Check(X.py_obj))) { (*this) = X; }
else {
// else make a new copy of the numpy ...
import_numpy_array();
//assert(PyArray_Check(X.py_obj));
if (!is_scalar_or_pod::value) TRIQS_RUNTIME_ERROR << "Internal Error : memcpy on non-scalar";
#ifdef TRIQS_NUMPY_VERSION_LT_17
if ( ( PyArray_ISFORTRAN(X.py_obj)) || (PyArray_ISCONTIGUOUS(X.py_obj))) {
memcpy (p,PyArray_DATA(X.py_obj),size_ * sizeof(ValueType));
}
#else
// STRANGE : uncommenting this leads to a segfault on mac ???
// TO BE INVESTIGATED, IT IS NOT NORMAL
//if (!PyArray_Check(X.py_obj)) TRIQS_RUNTIME_ERROR<<"Internal error : is not an array";
PyArrayObject * arr3 = (PyArrayObject *)(X.py_obj);
if ( ( PyArray_ISFORTRAN(arr3)) || (PyArray_ISCONTIGUOUS(arr3))) {
memcpy (p,PyArray_DATA(arr3),size_ * sizeof(ValueType));
}
#endif
else { // if the X.py_obj is not contiguous, first let numpy copy it properly
PyObject * na = PyObject_CallMethod(X.py_obj,(char *)"copy",NULL);
assert(na);
#ifdef TRIQS_NUMPY_VERSION_LT_17
assert( ( PyArray_ISFORTRAN(na)) || (PyArray_ISCONTIGUOUS(na)));
memcpy (p,PyArray_DATA(na),size_ * sizeof(ValueType));
#else
if (!PyArray_Check(na)) TRIQS_RUNTIME_ERROR<<"Internal error : is not an array";
PyArrayObject * arr = (PyArrayObject *)(na);
assert( ( PyArray_ISFORTRAN(arr)) || (PyArray_ISCONTIGUOUS(arr)));
memcpy (p,PyArray_DATA(arr),size_ * sizeof(ValueType));
#endif
Py_DECREF(na);
}
}
}
static void delete_pointeur( void *ptr ) {
TRACE_MEM_DEBUG("deleting data block"<<(non_const_value_type*) ptr);
delete [] ( (non_const_value_type*) ptr) ;
}
PyObject * new_ref_to_guard() {
if (py_obj==NULL) {
TRACE_MEM_DEBUG(" activating python guard for C++ block"<
void load(Archive & ar, const unsigned int version) {
ar >> size_;
assert (p==NULL);
p = new non_const_value_type[size_];
for (size_t i=0; i> p[i];
}
BOOST_SERIALIZATION_SPLIT_MEMBER();
};
}}}}//namespace triqs::arrays
#undef TRACE_MEM_DEBUG
#endif