Julia buffer interface?

Does Julia have anything akin to Python’s buffer interface? That is, I’d like to be able to test whether an object is represented as a contiguous/strided mutable/immutable buffer of bytes/words/etc in memory, and if so, extract its pointer and size. In my particular use-case, I’m only really interested in the basic case that an object is a contiguous buffer of bytes, e.g. Array, SubArray, String, SubString, …

There isn’t really a concept of a pointer in regular julia semantics. Arrayelements aren’t necessarily stored in-line either.

Arrays themselves can act like a buffer with push! and pop! semantics (see here).

1 Like

I don’t mean an IOBuffer, I mean buffer in the sense of the Python buffer protocol which is a way to query whether an object is represented as a bunch of regularly-spaced bytes in memory, and if so find out the address of that memory.

The way to find out the address is basically to convert it to pointer for ccall.

I don’t think there’s a easy, builtin and generic way to find out if the conversioin is supported.

I’m not aware of anything generic. I don’t know of a method that’s basically “Give me your data!” that you can pass an object to and expect a pointer to the underlying data back.

This all begs the question of “what” you are trying to do…do you really need a pointer to the memory? Why do you want a pointer to the memory? And so forth…

What I’m actually doing is passing this data into a C function, which wants a pointer and a length. For speed and memory reasons, I don’t want to make a copy of the data, so currently I restrict to Array and String inputs which I know how to extract the pointer and size from. I was hoping to abstract this away and not care too much what the data is, so long as it’s a bunch of bytes.

Well you can probably get the pointer to ANYTHING…however in the case of strings and arrays that might not be the pointer you WANT. In the strings and array cases the pointer to the object points to the memory location where the size, length, and pointer to the real buffer resides. So yeah, there is no “give me a pointer to your real data” method you can call.

As I said above, this part does exist. There just isn’t a way to determine if this is supported.

Also given,

It seems that he already know about this part.

For this specific case, if you are just trying to avoid copying, I’d just ask for a conversion to pointer in ccall directly. The types can implement the proper method that does the copying.

Thanks :slight_smile:

Is doing that definitely OK? For example, I wouldn’t want a strided array to be accepted.

It seems that a strided array is allowed… which alsos makes some sense.

But that’s sth you can check with strides/stride.