Help translating Cython to Julia

(Sorry for all the different venues for this, I’ve had this conversation piecewise with many of you already)

I’m trying to write the equivalent julia code for the following Cython code:

https://github.com/mapd/pymapd/blob/01019c6a48814a44644499bf4053f46e488e7ac6/pymapd/shm.pyx#L31-L52

I receive the following from calling an external API, where I’m trying to IPC using shared memory:

julia> tdf.sm_handle
4-element Array{UInt8,1}:
 0x9b
 0x76
 0x9a
 0x18

julia> tdf.sm_size
93856

I’ve gotten this far:

shmkey = reinterpret(UInt32, tdf.sm_handle)[1]
shmid = ccall((:shmget, "libc"), Cint, (Cuint, Int32, Int32), shmkey, tdf.sm_size, 0)
ptr = ccall((:shmat, "libc"), Ptr{Void}, (Cint, Ptr{Void}, Cint), shmid, C_NULL, 0)

julia> ptr
Ptr{Void} @0x00007f67fc7b4000

I believe the code above is equivalent to the Cython code, but I’m not quite sure. Would take any help there. But ultimately, my question is, if ptr is actually the location of the first element of the array of shared memory, how do I bring all of the data from shared memory into Julia?

When I debug the Cython code, pabuff is intermingled with ASCII text I recognize, along with a bunch of 0x00 and the like in between all the text. But not having any real experience with C and shared memory, I’m stuck between how I take the shared memory ID I’m passed and actually read the data into julia.

Disclaimer: I haven’t used shmat in real life; I just read the man page.

It looks like you’ve got the pointer to the array as expected. In you cython code, you cast that to a pointer to uint8 and then wrap that as an array. You can do the same in Julia:

julia> unsafe_wrap(Array, convert(Ptr{UInt8}, ptr), sm_size)
100-element Array{UInt8,1}:
<your data here>

Note that this Julia array will reference the same data as the attached shared memory. If you’re going to free that shared memory, you should copy() the resulting array to avoid referencing garbage.

Thanks so much @rdeits, it was convert(Ptr{UInt8}, ptr) that I was missing. I tried unsafe_wrap, but with Ptr{Void}, I kept getting a Julia error.