1

I'd like to allocate, zero-initialize and return a cython memoryview of an aribitrary number of elements. Element type should be uint32_t (and in some cases uint16_t).

A problem seems to be to find the right format code for the fixed size integer type, is there a good way to do this, or does it come down to testing each of the alternatives (for example using struct.calcsize)?

(Without using numpy.)

creanion
  • 2,319
  • 2
  • 13
  • 17
  • I tend to just let NumPy create the contiguous arrays. Then you can pass them through to C functions in Cython just by using &arrayname[0], and you get the memory management for free because it's a Python object. – Ryan Pepper Jun 28 '21 at 17:25

1 Answers1

2

If your compiler supports stdint.h, int32_t (and similar) could be cimported from libc.stdint:

%%cython

from libc.stdint cimport uint32_t
from cython.view cimport array as cvarray

def create(len):
    cdef uint32_t[:] memview = cvarray(shape=(len,), itemsize=sizeof(uint32_t), format=b"=I")
    return memview

Memory view needs to be backed by something, that owns the memory - I have chosen cython's array.

Also format=b"=I" is used. Important here is the character = which guarantees the "standard" itemsize of 4bytes (which b"I" does not by the way). An alternative would be to create the format string at the run time as, e.g.

%%cython

...
cdef uint32_t tmp = 1
fuint32_t = (<uint32_t[:1]>(&tmp)).format  # use this to create carray
creanion
  • 2,319
  • 2
  • 13
  • 17
ead
  • 32,758
  • 6
  • 90
  • 153