Skip to content

Instantly share code, notes, and snippets.

@davidreynolds
Created August 6, 2010 23:21
Show Gist options
  • Save davidreynolds/512183 to your computer and use it in GitHub Desktop.
Save davidreynolds/512183 to your computer and use it in GitHub Desktop.
;; this function is defined in vector_of_pairs.c and used in this file
(define vector_of_pairs (foreign-safe-lambda void "vector_of_pairs" int))
;; define the scheme callback -- note that this is extern'd in vector_of_pairs.c
(define-external (scm_cb (scheme-object x)) void
(print x))
;; call C function to generate vector of pairs, passing N into it
(vector_of_pairs 10)
#include "chicken.h"
/* This is defined in test.scm */
extern void scm_cb(C_word x);
void vector_of_pairs(int N) {
int i;
C_word *vecp, *v0;
C_word *pairp;
/* allocate memory for vector and pairs */
vecp = C_alloc(C_SIZEOF_VECTOR(N));
/* there are N pairs in the vector */
pairp = C_alloc(C_SIZEOF_PAIR * N);
/* v0 is a pointer to the start of the vector's allocated memory */
v0 = vecp;
/* first location sets up number of elements in the object */
*(vecp++) = N;
/* insert pairs of integers into the vector */
for (i = 0; i < N; ++i) {
/* vecp iterates over the memory starting at v0 and inserts pairs */
*(vecp++) = C_pair(&pairp, C_fix(i), C_fix(N-i));
}
/* call the scheme callback with the newly-created vector stored at v0 */
scm_cb((C_word)v0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment