Welcome to the CHICKEN Scheme pasting service

dynamic reloads 2 added by megane on Thu Jun 16 20:42:34 2011




File: Makefile
-----

CSC=csc
CSCCC=$(CSC) -static-extension chicken-syntax -debug-level 2 -include-path . -debug F
CSCLD=$(CSC)

TARGETS=\
	main

all: $(TARGETS)

lib:
	#gcc -c testlib_c_foo.c
	$(CSCCC) -s testlib_c.scm testlib_c_foo.c

main: clean lib
	$(CSCCC) -o main test.scm testlib_c_foo.c

clean:
	rm -f main *.o *.so

test: main
	(rm -fr foo && \
	mkdir foo && \
	cp main testlib_c.so testlib.scm foo && \
	cd foo && \
	(./main &) && \
	#sleep 1 && \
	sed 's/a 11111111/a 999999/' testlib.scm > o && \
	mv o testlib.scm && \
	sleep 1)



File: testlib_c_foo.c
-----

#include <stdlib.h>
#include <stdio.h>

int *myarray = NULL;
int inited = 0;

int my_function(int *retarr) {
  if (0 == inited) {
    myarray = (int*) malloc(10 * sizeof(int));
    inited = 1;
  }
  int amt = 1 + (int)(rand() % 9);
  int i;
  for(i=0; i < amt; i++) {
    myarray[i] = (int)(rand() % 100);
    printf("%d: %d\n", i, myarray[i]);
  }
  
  *retarr = (int)myarray;
  return amt;
}



File: testlib_c.scm
-----

(module
 foo-testlib
 *
 (import chicken scheme)
 (import foreign)
 (use srfi-4)
 (use srfi-1)
 (use lolevel)

 (define-foreign-type size_t int)
 (define my-function (foreign-lambda int "my_function" (c-pointer int)))
 
 (foreign-declare "#include <string.h>")
 (define C-memcpy (foreign-lambda void "memcpy" (c-pointer void) (c-pointer void) size_t)))

(module testlib-c ()
	(reexport foo-testlib))


File: testlib.scm
-----

(module
 testlib-foo
 *
 (import chicken scheme)
 (define a 11111111))

(module
    testlib
    ()
    (reexport testlib-c)
    (reexport testlib-foo))




File: test.scm
-----

(include "testlib_c")
(load "testlib_c.so")
(include "testlib")

(module
 test
 *
 (import chicken scheme)
 (import foreign)
 (import testlib)
 (use srfi-4)
 (use posix)
 (use srfi-1)
 (use lolevel)

 (let-location
  [(arrpointer (c-pointer int))]
  (let* [(n (my-function (location arrpointer)))
	 (vec (make-s32vector n 1))]
    (C-memcpy (make-locative vec)
	      arrpointer
	      (* n (foreign-value "sizeof(int)" size_t)))
    (print vec)))

 (define dyn-reload
   (lambda ()
     (print "value of var a: " a)
     (print "Doing dynamic reload...")
     (load "testlib")
     (import testlib)
     (print "dynamic reload complete")
     (print "value of var a: " a)))

 (dyn-reload))

------------------------------
output:
0: 47
1: 51
2: 47
3: 36
4: 68
5: 59
6: 9
7: 98
#s32(47 51 47 36 68 59 9 98)
value of var a: 11111111
Doing dynamic reload...
dynamic reload complete
value of var a: 999999

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Which operator can be represented by an apostrophe?
Visually impaired? Let me spell it for you (wav file) download WAV