[C++-sig] Passing memory allocated in C++ to Python

Tony Cappellini cappy2112 at gmail.com
Mon Jan 11 17:56:45 EST 2016


I've already got a project built for Linux, using Boost-python. My Python
code can import the C++ extension and access the members that are exposed.

My C++ code needs to call fill in C++ structures in order to call ioctl()
functions, while passing the addresses of the structures to the ioctl
functions.

I also need to allocate large (10's of MB - 100s of MB) buffers which must
be aligned to specific boundaries. A pointer to these buffers are part of
the above-mentioned C++ structs.
(I already have the C++ functions which handle the alignment and memory
allocation)

The problems I'm facing are:
     1. How to initiate the memory allocation from Python, so that the user
has access to the
         data after the ioctl() completes. This would eliminate the need
from copying the data from
        C++ to Python, after each ioctl() call.
     2. Using boost-python, which type of Python data structure is best
suited to be shared across
         the C++/Python boundary? Keep in mind that the allocation needs to
happen in C++ so that
         memory alignment can be maintained, but initiating the
allocation/alignment will be done from
         Python.

Essentially- Python needs access to the return value from a malloc() call.


A link link to a working example of Python & C++ code would be helpful.
.
The boost wiki only has examples for exposing classes to Python.
https://wiki.python.org/moin/boost.python/EmbeddingPython

The examples I've found on StackOverflow aren't quite what I'm looking for.


Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20160111/e7b51185/attachment.html>


More information about the Cplusplus-sig mailing list