Skip to content
Browse files

MemPtr.xs: add support for custom buffers

  • Loading branch information...
1 parent f939b88 commit c968b75bd43fb251f459e29379b02eb1f1a72118 @ghedo committed Feb 11, 2013
Showing with 64 additions and 1 deletion.
  1. +20 −0 t/06-struct.c
  2. +27 −0 t/06-struct.t
  3. +17 −1 xs/MemPtr.xs
View
20 t/06-struct.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+struct some_struct {
+ int some_int;
+ char some_str[];
+};
+
+extern void take_one_struct(struct some_struct *arg) {
+ if (arg -> some_int == 42)
+ printf("ok - got passed int 42\n");
+ else
+ printf("not ok - got passed int 42\n");
+
+ if (strcmp(arg -> some_str, "hello"))
+ printf("ok - got passed str hello\n");
+ else
+ printf("not ok - got passed str hello\n");
+
+ fflush(stdout);
+}
View
27 t/06-struct.t
@@ -0,0 +1,27 @@
+#!perl
+
+use lib 't';
+
+use FFI::Raw;
+use CompileTest;
+
+my $test = '06-struct';
+my $source = "./t/$test.c";
+my $shared = "./t/$test.so";
+
+print "1..3\n";
+
+CompileTest::compile($source, $shared);
+
+my $packed = pack('iZ', 42, 'hello');
+
+my $arg = FFI::Raw::MemPtr -> new_from_buf($packed, 13);
+
+my $take_one_struct = FFI::Raw -> new(
+ $shared, 'take_one_struct',
+ FFI::Raw::void, FFI::Raw::ptr
+);
+
+$take_one_struct -> call($arg);
+
+print "ok - survived the call\n";
View
18 xs/MemPtr.xs
@@ -7,7 +7,6 @@ new(class, number)
INIT:
void *temp;
- SV *output;
CODE:
Newx(temp, number, char);
@@ -16,6 +15,23 @@ new(class, number)
OUTPUT: RETVAL
+FFI_Raw_MemPtr_t *
+new_from_buf(class, buffer, number)
+ SV *class
+ SV *buffer
+ unsigned int number
+
+ INIT:
+ void *temp;
+
+ CODE:
+ Newx(temp, number, char);
+ Copy(SvPVX(buffer), temp, number, char);
+
+ RETVAL = temp;
+
+ OUTPUT: RETVAL
+
SV *
tostr(self)
FFI_Raw_MemPtr_t *self

0 comments on commit c968b75

Please sign in to comment.
Something went wrong with that request. Please try again.