MemoryView_C.c 13 KB
Newer Older
1
////////// MemviewSliceStruct.proto //////////
2

3

4 5 6
/* memoryview slice struct */

typedef struct {
Mark Florisson's avatar
Mark Florisson committed
7
  struct {{memview_struct_name}} *memview;
8
  /* For convenience and faster access */
9
  char *data;
Mark Florisson's avatar
Mark Florisson committed
10 11 12 13
  Py_ssize_t shape[{{max_dims}}];
  Py_ssize_t strides[{{max_dims}}];
  Py_ssize_t suboffsets[{{max_dims}}];
} {{memviewslice_name}};
14

15 16 17
/////////////// ObjectToMemviewSlice.proto ///////////////
{{# __Pyx_PyObject_to_MemoryviewSlice_<count> }}
static CYTHON_INLINE {{memviewslice_name}} {{funcname}}(PyObject *);
18

19
////////// MemviewSliceInit.proto //////////
20 21 22 23 24 25 26 27 28 29 30 31 32

#define __Pyx_BUF_MAX_NDIMS %(BUF_MAX_NDIMS)d

#define __Pyx_MEMVIEW_DIRECT   1
#define __Pyx_MEMVIEW_PTR      2
#define __Pyx_MEMVIEW_FULL     4
#define __Pyx_MEMVIEW_CONTIG   8
#define __Pyx_MEMVIEW_STRIDED  16
#define __Pyx_MEMVIEW_FOLLOW   32

#define __Pyx_IS_C_CONTIG 1
#define __Pyx_IS_F_CONTIG 2

33

34 35
/* #define __PYX_MEMSLICE_GETDATA(SLICE) ((char *) SLICE->memview->view->buf) */

36 37 38 39 40 41 42 43 44
static int __Pyx_ValidateAndInit_memviewslice(struct __pyx_memoryview_obj *memview,
                                int *axes_specs, int c_or_f_flag,  int ndim, __Pyx_TypeInfo *dtype,
                                __Pyx_BufFmt_StackElem stack[], __Pyx_memviewslice *memviewslice);

static int __Pyx_init_memviewslice(
                struct __pyx_memoryview_obj *memview,
                int ndim,
                __Pyx_memviewslice *memviewslice);

45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#define __PYX_INC_MEMVIEW(slice, have_gil) __Pyx_INC_MEMVIEW(slice, have_gil, __LINE__)
#define __PYX_XDEC_MEMVIEW(slice, have_gil) __Pyx_XDEC_MEMVIEW(slice, have_gil, __LINE__)
static CYTHON_INLINE void __Pyx_INC_MEMVIEW({{memviewslice_name}} *, int, int);
static CYTHON_INLINE void __Pyx_XDEC_MEMVIEW({{memviewslice_name}} *, int, int);

/////////////// MemviewSliceIndex.proto ///////////////

static CYTHON_INLINE char *__pyx_memviewslice_index_full(char *bufp, Py_ssize_t idx, Py_ssize_t stride, Py_ssize_t suboffset);
static CYTHON_INLINE char *__pyx_memviewslice_index_full_contig(char *bufp, Py_ssize_t suboffset);


/////////////// ObjectToMemviewSlice ///////////////

{{#__Pyx_PyObject_to_MemoryviewSlice_<count>}}

static CYTHON_INLINE {{memviewslice_name}} {{funcname}}(PyObject *obj) {
    {{memviewslice_name}} result;
    result.memview = NULL;
    result.data = NULL;
    struct __pyx_memoryview_obj *memview =  \
        (struct __pyx_memoryview_obj *) __pyx_memoryview_new(obj, {{buf_flag}});
    __Pyx_BufFmt_StackElem stack[{{struct_nesting_depth}}];
    int axes_specs[] = { {{axes_specs}} };
    int retcode;

    if (unlikely(!memview))
        goto __pyx_fail;

    retcode = __Pyx_ValidateAndInit_memviewslice(memview, axes_specs,
            {{c_or_f_flag}}, {{ndim}}, &{{dtype_typeinfo}}, stack, &result);

    if (unlikely(retcode == -1))
        goto __pyx_fail;

    return result;
__pyx_fail:
    Py_XDECREF(memview);
    result.memview = NULL;
    result.data = NULL;
    return result;
}

88
////////// MemviewSliceInit //////////
89 90 91 92 93 94

static int __Pyx_ValidateAndInit_memviewslice(
                struct __pyx_memoryview_obj *memview,
                int *axes_specs,
                int c_or_f_flag,
                int ndim,
95
                __Pyx_TypeInfo *dtype,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                __Pyx_BufFmt_StackElem stack[],
                __Pyx_memviewslice *memviewslice) {

    __Pyx_RefNannyDeclarations
    __Pyx_RefNannySetupContext("ValidateAndInit_memviewslice");
    Py_buffer *buf = &memview->view;
    int stride, i, spec = 0, retval = -1;

    if (!buf) goto fail;

    if(memviewslice->data || memviewslice->memview) {
        PyErr_SetString(PyExc_ValueError,
            "memoryviewslice struct must be initialized to NULL.");
        goto fail;
    }

    if (buf->ndim != ndim) {
        PyErr_Format(PyExc_ValueError,
                "Buffer has wrong number of dimensions (expected %d, got %d)",
                ndim, buf->ndim);
        goto fail;
    }

    __Pyx_BufFmt_Context ctx;
    __Pyx_BufFmt_Init(&ctx, stack, dtype);
    if (!__Pyx_BufFmt_CheckString(&ctx, buf->format)) goto fail;

    if ((unsigned)buf->itemsize != dtype->size) {
        PyErr_Format(PyExc_ValueError,
                     "Item size of buffer (%" PY_FORMAT_SIZE_T "d byte%s) "
                     "does not match size of '%s' (%" PY_FORMAT_SIZE_T "d byte%s)",
                     buf->itemsize,
                     (buf->itemsize > 1) ? "s" : "",
                     dtype->name,
                     dtype->size,
                     (dtype->size > 1) ? "s" : "");
        goto fail;
    }

    if (!buf->strides) {
        PyErr_SetString(PyExc_ValueError,
            "buffer does not supply strides necessary for memoryview.");
        goto fail;
    }

    for(i=0; i<ndim; i++) {
        spec = axes_specs[i];

        if (spec & __Pyx_MEMVIEW_CONTIG) {
145 146 147 148 149 150 151
            if (spec & (__Pyx_MEMVIEW_PTR|__Pyx_MEMVIEW_FULL)) {
                if (buf->strides[i] != sizeof(void *)) {
                    PyErr_Format(PyExc_ValueError,
                        "Buffer is not indirectly contiguous in dimension %d.", i);
                    goto fail;
                }
            } else if (buf->strides[i] != buf->itemsize) {
152 153 154 155 156 157 158 159 160 161 162 163 164 165
                PyErr_SetString(PyExc_ValueError,
                    "Buffer and memoryview are not contiguous in the same dimension.");
                goto fail;
            }
        }

        if (spec & (__Pyx_MEMVIEW_STRIDED | __Pyx_MEMVIEW_FOLLOW)) {
            if (buf->strides[i] < buf->itemsize) {
                PyErr_SetString(PyExc_ValueError,
                    "Buffer and memoryview are not contiguous in the same dimension.");
                goto fail;
            }
        }

166 167
        /* Todo: without PyBUF_INDIRECT we may not have suboffset information, i.e., the
           ptr may not be set to NULL but may be uninitialized? */
168 169
        if (spec & __Pyx_MEMVIEW_DIRECT) {
            if (buf->suboffsets && buf->suboffsets[i] >= 0) {
170 171
                PyErr_Format(PyExc_ValueError,
                    "Buffer not compatible with direct access in dimension %d.", i);
172 173 174 175
                goto fail;
            }
        }

176 177
        if (spec & (__Pyx_MEMVIEW_PTR|__Pyx_MEMVIEW_FULL) && !buf->suboffsets) {
            memviewslice->suboffsets[i] = -1;
178 179 180
        }

        if (spec & __Pyx_MEMVIEW_PTR) {
181
            if (buf->suboffsets && buf->suboffsets[i] < 0) {
182
                PyErr_Format(PyExc_ValueError,
183
                    "Buffer is not indirectly accessisble in dimension %d.", i);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
                goto fail;
            }
        }
    }

    if (c_or_f_flag & __Pyx_IS_F_CONTIG) {
        stride = 1;
        for(i=0; i<ndim; i++) {
            if(stride * buf->itemsize != buf->strides[i]) {
                PyErr_SetString(PyExc_ValueError,
                    "Buffer not fortran contiguous.");
                goto fail;
            }
            stride = stride * buf->shape[i];
        }
    } else if (c_or_f_flag & __Pyx_IS_F_CONTIG) {
        for(i=ndim-1; i>-1; i--) {
            if(stride * buf->itemsize != buf->strides[i]) {
                PyErr_SetString(PyExc_ValueError,
                    "Buffer not C contiguous.");
                goto fail;
            }
            stride = stride * buf->shape[i];
        }
    }

    if(unlikely(__Pyx_init_memviewslice(memview, ndim, memviewslice) == -1)) {
        goto fail;
    }

    retval = 0;
    goto no_fail;
fail:
    __Pyx_XDECREF(memviewslice->memview);
    memviewslice->memview = 0;
    memviewslice->data = 0;
    retval = -1;

no_fail:
    __Pyx_RefNannyFinishContext();
    return retval;
}

static int __Pyx_init_memviewslice(
                struct __pyx_memoryview_obj *memview,
                int ndim,
                __Pyx_memviewslice *memviewslice) {

    __Pyx_RefNannyDeclarations
    __Pyx_RefNannySetupContext("init_memviewslice");
    int i, retval=-1;
    Py_buffer *buf = &memview->view;

    if(!buf) {
        PyErr_SetString(PyExc_ValueError,
            "buf is NULL.");
        goto fail;
    } else if(memviewslice->memview || memviewslice->data) {
        PyErr_SetString(PyExc_ValueError,
            "memviewslice is already initialized!");
        goto fail;
    }

    for(i=0; i<ndim; i++) {
        memviewslice->strides[i] = buf->strides[i];
        memviewslice->shape[i]   = buf->shape[i];
        if(buf->suboffsets) {
            memviewslice->suboffsets[i] = buf->suboffsets[i];
        } else {
            memviewslice->suboffsets[i] = -1;
Mark Florisson's avatar
Mark Florisson committed
254
        }
255 256 257 258
    }

    memviewslice->memview = memview;
    memviewslice->data = (char *)buf->buf;
259
    memview->acquisition_count++;
260 261 262 263 264 265 266 267 268 269 270 271 272
    retval = 0;
    goto no_fail;

fail:
    __Pyx_XDECREF(memviewslice->memview);
    memviewslice->memview = 0;
    memviewslice->data = 0;
    retval = -1;
no_fail:
    __Pyx_RefNannyFinishContext();
    return retval;
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

static CYTHON_INLINE void __pyx_fatalerror(char *fmt, ...) {
    va_list vargs;
    char msg[200];

    va_start(vargs, fmt);

#ifdef HAVE_STDARG_PROTOTYPES
    va_start(vargs, fmt);
#else
    va_start(vargs);
#endif

    vsnprintf(msg, 200, fmt, vargs);
    Py_FatalError(msg);

    va_end(vargs);
}

292 293 294 295
static CYTHON_INLINE void __Pyx_INC_MEMVIEW({{memviewslice_name}} *memslice,
                                            int have_gil, int lineno) {
    int first_time;
    struct {{memview_struct_name}} *memview = memslice->memview;
296 297 298 299 300 301
    if (!memview)
        __pyx_fatalerror("memoryslice is not initialized (line %d)", lineno);

    if (memview->acquisition_count <= 0)
        __pyx_fatalerror("Acquisition count is %d (line %d)",
                         memview->acquisition_count, lineno);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

    PyThread_acquire_lock(memview->lock, 1);
    first_time = (memview->acquisition_count++ == 0);
    PyThread_release_lock(memview->lock);

    if (first_time) {
        if (have_gil) {
            Py_INCREF((PyObject *) memview);
        } else {
            PyGILState_STATE _gilstate = PyGILState_Ensure();
            Py_INCREF((PyObject *) memview);
            PyGILState_Release(_gilstate);
        }
    }
}

static CYTHON_INLINE void __Pyx_XDEC_MEMVIEW({{memviewslice_name}} *memslice,
                                             int have_gil, int lineno) {
    int last_time;
    struct {{memview_struct_name}} *memview = memslice->memview;

323
    if (!memview)
324
        return;
325 326 327 328

    if (memview->acquisition_count <= 0)
        __pyx_fatalerror("Acquisition count is %d (line %d)",
                         memview->acquisition_count, lineno);
329 330 331 332 333 334 335

    PyThread_acquire_lock(memview->lock, 1);
    last_time = (memview->acquisition_count-- == 1);
    PyThread_release_lock(memview->lock);

    if (last_time) {
        if (have_gil) {
336
            Py_CLEAR(memslice->memview);
337 338
        } else {
            PyGILState_STATE _gilstate = PyGILState_Ensure();
339
            Py_CLEAR(memslice->memview);
340 341
            PyGILState_Release(_gilstate);
        }
342
        memslice->memview = NULL;
343 344 345
    }
}

346
////////// MemviewSliceCopyTemplate //////////
347

Mark Florisson's avatar
Mark Florisson committed
348
static __Pyx_memviewslice {{copy_name}}(const __Pyx_memviewslice from_mvs) {
349 350 351 352 353 354 355 356 357 358

    __Pyx_RefNannyDeclarations
    int i;
    __Pyx_memviewslice new_mvs = {0, 0};
    struct __pyx_memoryview_obj *from_memview = from_mvs.memview;
    Py_buffer *buf = &from_memview->view;
    PyObject *shape_tuple = 0;
    PyObject *temp_int = 0;
    struct __pyx_array_obj *array_obj = 0;
    struct __pyx_memoryview_obj *memview_obj = 0;
Mark Florisson's avatar
Mark Florisson committed
359
    char *mode = "{{mode}}";
360

Mark Florisson's avatar
Mark Florisson committed
361
    __Pyx_RefNannySetupContext("{{copy_name}}");
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

    shape_tuple = PyTuple_New((Py_ssize_t)(buf->ndim));
    if(unlikely(!shape_tuple)) {
        goto fail;
    }
    __Pyx_GOTREF(shape_tuple);


    for(i=0; i<buf->ndim; i++) {
        temp_int = PyInt_FromLong(buf->shape[i]);
        if(unlikely(!temp_int)) {
            goto fail;
        } else {
            PyTuple_SET_ITEM(shape_tuple, i, temp_int);
        }
    }

Mark Florisson's avatar
Mark Florisson committed
379
    array_obj = __pyx_array_new(shape_tuple, {{sizeof_dtype}}, buf->format, mode);
380 381 382 383 384
    if (unlikely(!array_obj)) {
        goto fail;
    }
    __Pyx_GOTREF(array_obj);

385 386
    memview_obj = (struct __pyx_memoryview_obj *) __pyx_memoryview_new(
                                (PyObject *) array_obj, {{contig_flag}});
387 388 389 390 391 392 393 394 395 396 397
    if (unlikely(!memview_obj)) {
        goto fail;
    }

    /* initialize new_mvs */
    if (unlikely(-1 == __Pyx_init_memviewslice(memview_obj, buf->ndim, &new_mvs))) {
        PyErr_SetString(PyExc_RuntimeError,
            "Could not initialize new memoryviewslice object.");
        goto fail;
    }

Mark Florisson's avatar
Mark Florisson committed
398
    if (unlikely(-1 == {{copy_contents_name}}(&from_mvs, &new_mvs))) {
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        /* PyErr_SetString(PyExc_RuntimeError,
            "Could not copy contents of memoryview slice."); */
        goto fail;
    }

    goto no_fail;

fail:
    __Pyx_XDECREF(new_mvs.memview); new_mvs.memview = 0;
    new_mvs.data = 0;
no_fail:
    __Pyx_XDECREF(shape_tuple); shape_tuple = 0;
    __Pyx_GOTREF(temp_int);
    __Pyx_XDECREF(temp_int); temp_int = 0;
    __Pyx_XDECREF(array_obj); array_obj = 0;
    __Pyx_RefNannyFinishContext();
    return new_mvs;

}

419 420 421 422 423 424 425 426 427
/////////////// MemviewSliceIndex ///////////////

static CYTHON_INLINE char *__pyx_memviewslice_index_full(char *bufp, Py_ssize_t idx, Py_ssize_t stride, Py_ssize_t suboffset) {
    bufp = bufp + idx * stride;
    if (suboffset >= 0) {
        bufp = *((char **) bufp) + suboffset;
    }
    return bufp;
}