[Pharo-project] Cheating NB FFI ByteArray size
Hi Igor. I wonder if I can do the following. I have to pass a ByteArray to a function that expects an already malloced char*. So far it is working fine. Now...what I would like is from the image side to allocate a ByteArray of size + 4. I need to reserve the first 4 bytes of the ByteArray for something. But the library I am calling doesn't let me specify where to start from that char*. So...can I cheat? I mean, can I allocate ByteArray+4 in the image, but when passing it as an argument somehow say "ok, take this ByteArray but think the the beginning is the byte 4" ? or something like that ? Thanks! -- Mariano http://marianopeck.wordpress.com
just never forget that we have a moving GC! ;) so directly passing references to native function can be quite dangerous! Otherwise you could first manually adjust the start address by writing the corresponding ASM code ;) then run the C callout. I should check if we already have a proper helper method for that. On 2012-11-07, at 14:33, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
Hi Igor. I wonder if I can do the following. I have to pass a ByteArray to a function that expects an already malloced char*. So far it is working fine. Now...what I would like is from the image side to allocate a ByteArray of size + 4. I need to reserve the first 4 bytes of the ByteArray for something. But the library I am calling doesn't let me specify where to start from that char*. So...can I cheat? I mean, can I allocate ByteArray+4 in the image, but when passing it as an argument somehow say "ok, take this ByteArray but think the the beginning is the byte 4" ? or something like that ?
Thanks!
-- Mariano http://marianopeck.wordpress.com
On 7 November 2012 11:09, Camillo Bruni <camillobruni@gmail.com> wrote:
just never forget that we have a moving GC! ;) so directly passing references to native function can be quite dangerous!
i hope LZ4 written in a good way, which doesn't requires you to keep a valid pointer to a buffer once it return(s) from LZ4 function.
Otherwise you could first manually adjust the start address by writing the corresponding ASM code ;) then run the C callout. I should check if we already have a proper helper method for that.
@Mariano, i described the trick how you can do that in another reply. This is trivial to do with ASM code, the non-trivial part is how to "formally" express that in callout .. which will turn it from being "cheat" into "feature" :) Of course, if that actually necessary at all. IMO, cheat is fine.
On 2012-11-07, at 14:33, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
Hi Igor. I wonder if I can do the following. I have to pass a ByteArray to a function that expects an already malloced char*. So far it is working fine. Now...what I would like is from the image side to allocate a ByteArray of size + 4. I need to reserve the first 4 bytes of the ByteArray for something. But the library I am calling doesn't let me specify where to start from that char*. So...can I cheat? I mean, can I allocate ByteArray+4 in the image, but when passing it as an argument somehow say "ok, take this ByteArray but think the the beginning is the byte 4" ? or something like that ?
Thanks!
-- Mariano http://marianopeck.wordpress.com
-- Best regards, Igor Stasenko.
participants (3)
-
Camillo Bruni -
Igor Stasenko -
Mariano Martinez Peck