Skip to content

Instantly share code, notes, and snippets.

@dpwright
Last active October 7, 2018 15:30
Show Gist options
  • Save dpwright/4615860 to your computer and use it in GitHub Desktop.
Save dpwright/4615860 to your computer and use it in GitHub Desktop.
Demonstrates filling in a u32vector from an FFI function returning a pointer in Chicken Scheme
static const int TEST_SIZE = 3;
static unsigned int testArray[TEST_SIZE] = {4, 9, 15};
static unsigned int* testFunc()
{
return testArray;
}
(use srfi-4)
(foreign-declare "#include \\"test.h\\"")
(define test-size 3)
(define c-test-func
(foreign-lambda* int ((nonnull-u32vector vector_out))
"unsigned int* array_from_func = testFunc();
memcpy(vector_out, array_from_func, TEST_SIZE * sizeof(unsigned int));
C_return(TEST_SIZE);"))
(define (test-func)
(let* ((buffer (make-u32vector test-size)))
(c-test-func buffer)
(u32vector->list buffer)))
(write (test-func))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment