Contiguous memory zone with C API
Hi all, I try to change my old C API numpy code to the 'new' API. I used to hack some internal stuff in Numpy (yes it's bad...) and I wonder now how to change it. Let's take an example: I have ocount numpy arrays with data allocated elsewhere than numpy, the data pointer of the PyArrayObject is set with the malloc'd zone. Now I select one of these array to be the memory base for all of them, then I realloc each data pointer to make sure the ocount of them have a contiguous zone. The code is now 'rejected' by the new API, how can I do that without hacking into the PyArray_Object? first=(PyArrayObject*)context->ctg_obj[0]; psize=PyArray_NBYTES(first); for (j=1;j<ocount;j++) { current=(PyArrayObject*)context->ctg_obj[j]; tsize=PyArray_NBYTES(current); psize+=tsize; ((PyArrayObject*)first)->data=realloc(PyArray_DATA(first),psize); /* *** how to do that with the API ? */ memcpy(PyArray_DATA(first)+psize-tsize,PyArray_DATA(current),tsize); free(PyArray_DATA(current)); ((PyArrayObject*)current)->data=PyArray_DATA(first)+psize-tsize; } I use that trick to make sure that separate numpy each representing a coordinate of a vector can be gather in a single array. I've had a look at PyArray_resize but it requires a own_data flag which I do not have... Any hint, remark? -- -- ---------------------------------------------------------------------------- -- Marc POINOT [ONERA/DSNA] Tel:+33.1.46.73.42.84 Fax:+33.1.46.73.41.66 -- Avertissement/disclaimer http://www.onera.fr/en/emails-terms -- ---------------------------------------------------------------------------- --
On 10/21/13 13:22, Marc.Poinot@onera.fr wrote:
Hi all,
I try to change my old C API numpy code to the 'new' API. I used to hack some internal stuff in Numpy (yes it's bad...) and I wonder now how to change it.
Ok, I've used PyDataMem_RENEW to reallocate through the API, but there's no way to change data pointer or ownership flag. I think the API miss with some kind of: PyDataMem_SET(PyArrayObject *object, void *ptr) to set actual data field (hidden now in PyArrayObject_fields) and PyArray_SETOWNDATA(arr) PyArray_UNSETOWNDATA(arr) to set/unset OWN DATA flag if you change memory from external allocation (at my own risks) PyArray_ISOWNDATA(arr) to check it This is more or less what is performed in shape.c or ctors.c for example, I use to operate on arr->data and arr->flags but if you plan to hide it and to move it 'to a private header in a future release' it would be nice to have some macros. -- -- ---------------------------------------------------------------------------- -- Marc POINOT [ONERA/DSNA] Tel:+33.1.46.73.42.84 Fax:+33.1.46.73.41.66 -- Avertissement/disclaimer http://www.onera.fr/en/emails-terms -- ---------------------------------------------------------------------------- --
participants (1)
-
Marc.Poinot@onera.fr