Skip to content

Commit

Permalink
Add method to create memory object from address and size in bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcinl committed Jun 7, 2016
1 parent 200b86f commit 167ae5a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/MPI/asbuffer.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ cdef class memory:
def __dealloc__(self):
PyBuffer_Release(&self.view)

@staticmethod
def fromaddress(address, nbytes, readonly=False):
"""Memory from address and size in bytes"""
cdef void *buf = PyLong_AsVoidPtr(address)
cdef Py_ssize_t size = nbytes
if size < 0:
raise ValueError("expecting non-negative buffer length")
elif size > 0 and buf == NULL:
raise ValueError("expecting non-NULL address")
cdef memory mem = <memory>memory.__new__(memory)
PyBuffer_FillInfo(&mem.view, <object>NULL,
buf, size, readonly, PyBUF_SIMPLE)
return mem

# properties

property address:
Expand Down

0 comments on commit 167ae5a

Please sign in to comment.