[Chicken-users] Safely holding on to a reference in C
Hi, I'm experimenting with Chicken and libevent using the bind extension to build a event based webserver. I'm trying to figure out if there's a way to have C code hold on to a in a GC safe manner. The implementation I have so far works for a few requests, but eventually fails. Based on what I've learned so far it seems like this is because the GC is either throwing away or moving the making the pointer in C being held by libevent invalid. Is it possible to hold on to a like this? Here's the code (also here: https://gist.github.com/2b3884461320db313188) : (require-extension bind) (foreign-declare #< #include EOF ) (bind-opaque-type _evhttp_request (c-pointer "struct evhttp_request")) (bind-opaque-type _evbuffer (c-pointer "struct evbuffer")) (bind-opaque-type _evhttp (c-pointer "struct evhttp")) (bind-opaque-type _event_base (c-pointer "struct event_base")) (bind "___safe _event_base event_init();") (bind "___safe void event_dispatch();") (bind "___safe int event_dispatch();") (bind "___safe _evbuffer evbuffer_new();") (bind "___safe int evbuffer_add_printf(_evbuffer, const char*);") (bind "___safe _evhttp evhttp_start(const char*, unsigned short);") (bind "___safe void evhttp_free(_evhttp);") (bind "___safe void evhttp_send_reply(_evhttp_request, int, const char*, _evbuffer);") (bind "___safe void evhttp_set_gencb(_evhttp, void (*)(_evhttp_request, ___scheme_value), ___scheme_value);") (display "evented loaded\n") (define (hello-world-page req) (let ((buf (evbuffer_new))) (evbuffer_add_printf buf "hello world") (evhttp_send_reply req 200 "OK" buf))) (define-external (request_handler (_evhttp_request req) (scheme-object handler)) void (handler req)) (event_init) (let ((httpd (evhttp_start "0.0.0.0" 8080))) (evhttp_set_gencb httpd #$request_handler hello-world-page) (event_dispatch) (evhttp_free httpd)) Here's the error after a few hundred requests: naitik@towel ~/Development/cc csc -o evented -levent evented.scm && ./evented evented.c: In function ‘stub187’: evented.c:66: warning: passing argument 2 of ‘evhttp_set_gencb’ from incompatible pointer type evented.c:66: warning: passing argument 3 of ‘evhttp_set_gencb’ makes pointer from integer without a cast evented.c: In function ‘stub104’: evented.c:115: warning: format not a string literal and no format arguments evented loaded Error: call of non-procedure: # Call history: ##sys#gc g27 ##sys#make-c-string g12 evented.scm:27: evhttp_send_reply ##sys#gc ##sys#make-c-string g27 g171172 evented.scm:32: handler <-- Thanks for reading this far! -Naitik ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] bind bug?
I ran across what I think is a bug in bind parsing a preprocessor macro containing single-quoted escaped hexadecimal character value. Here is an example: (import foreign) (require-extension bind) (bind #< (##core#begin (bind "#define PARSE_THIS_ARRAY '\\x00','\\x00','\\x00','\\x00'")) (bind "#define PARSE_THIS_ARRAY '\\x00','\\x00','\\x00','\\x00'") <-- Error: shell command terminated with non-zero exit status 17920: /opt/chicken/bin/chicken bind-bug.scm -output-file bind-bug.c The header containing this is not mine and the example seems to be valid (or at least is consumed by gcc and is not obviously out of line with Sec. 2.7 of Harbison & Steele), so I wonder if it's something that can be fixed in bind. Thanks. - Ross ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] cvjm
Andy Bennett scripsit: > Wow! This is excellent. I always thought that the Java language should > be an entity in it's own right, separate from the JVM and be compilable > / interpreted or JIT as required. gcj is a variant of gcc that compiles static Java into native code ahead of time. It does so in a way that's compatible with how g++ compiles C++, and the two languages interoperate freely provided the libgcj library is linked. Now that Sun Java is open source, it's not being very actively developed, but it would certainly be possible to make Chicken and Java code interoperate nicely using it. -- Verbogeny is one of the pleasurettesJohn Cowan of a creatific thinkerizer. http://www.ccil.org/~cowan --Peter da Silva ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] Mapping wchar_t strings
Hi fellow Chicken users I'm write an extension for ncursesw, which uses wchar_t* strings for unicode string operations. It will hopefully become a ncursesw egg, with a revised API, sometime in the future! This is what i have so far (relevant code only). It works on my setup, but before I proceed any further, I'd like to get a confirmation that this is indeed the best way to pass a chicken utf8 string as a wchar_t*. Also, is that the best way to require utf8 in an extension? #> #include #include #include <# ;; I need utf8-aware string-length (require-extension utf8) (require-library srfi-4) (module ncursesw ... (import srfi-4) ;; setup locale from environment variables--nothing works otherwise (foreign-code "setlocale(LC_ALL, \"\");") ;; fill a pre-allocated wchar-string (u32vector) with the contents of an utf8 c-string (define fill-wchar-string (foreign-lambda* void ((u32vector vec) (c-string str) (int len)) "mbsrtowcs((wchar_t *) vec, (const char **) &str, len, NULL);")) ;; convert an utf8 c-string into a wchar-string (zero-terminated u32vector) (define (string->wchar-string str) (let* ((len (+ 1 (string-length str))) (vec (make-u32vector len))) (fill-wchar-string vec str len) vec)) (define-foreign-type wchar-string u32vector string->wchar-string) (define addwstr (foreign-lambda int "addwstr" wchar-string)) ... ) Tobia ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Chicken segfaults with large-ish inputs
Hello, On Sun, Feb 20, 2011 at 06:47, Sam Hardwick wrote: > > (define (sum term a next b) > (if (> a b) > 0 > (+ (term a) > (sum term (next a) next b You can use the following trick to accumulate intermediate results and thus turn the call to sum into a tail recursive one: (define (sum term a next b) (letrec ((helper (lambda (acc t a n b) (if (> a b) acc (helper (+ acc (t a)) t (n a) n b) (helper 0 term a next b))) With chicken 4.6.0, I get the following: #;14> (integral cube 0 1 10) 0.25 #;15> (integral cube 0 1 100) 0.25 #;16> (integral cube 0 1 1000) 0.25 #;17> (integral cube 0 1 1) 0.25 #;18> (integral cube 0 1 10) 0.249 #;19> (integral cube 0 1 100) ; Used to crash from that point on 0.25 #;20> (integral cube 0 1 1000) 0.25 Hope this helps, -- Nicolas ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] cvjm
On 02/20/11 13:31, Felix wrote: > Hello! > > > I have imported an attempt at a static Java->Scheme compiler into the > repository, which can be found in the "release/4/cvjm/trunk" > directory. It compiles Java ".class" files into low-level Scheme code > with full support for tail calls and continuations. *eyes pop out* wow! > (since this is a > static compiler, dynamic class definition is not supported and > introspection will be limited). I bet even that can be fixed, though. introspection can just be a matter of hauling around run-time metadata, and dynamic class definition a matter of implementing a sort of "indirection class" at worst, depending on how you've implemented the compiler. What made you do this, out of interest? Purely the thought it'd be cool, or was there some particular project in mind? ABS -- Alaric Snell-Pym http://www.snell-pym.org.uk/alaric/ ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] awful as cgi or fcgi?
On 02/20/11 15:36, Mario Domenech Goulart wrote: >> In your second awful scenario the (rather wonderful afaict) awful must >> still be a long running process, correct? > > Yes, that's correct. The CGI could, if awful is not responding, fire it up though (with, if rquired, steps taken to prevent two simultaneous requests firing up two awfuls), if "I can't start daemons at startup as I'm not root" is an issue here... > Best wishes. > Mario ABS -- Alaric Snell-Pym http://www.snell-pym.org.uk/alaric/ ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] cvjm
Hi, > I have imported an attempt at a static Java->Scheme compiler into the > repository, which can be found in the "release/4/cvjm/trunk" > directory. It compiles Java ".class" files into low-level Scheme code > with full support for tail calls and continuations. Wow! This is excellent. I always thought that the Java language should be an entity in it's own right, separate from the JVM and be compilable / interpreted or JIT as required. Good work! Regards, @ndy -- andy...@ashurst.eu.org http://www.ashurst.eu.org/ 0x7EBA75FF ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] awful as cgi or fcgi?
On Sun, 20 Feb 2011 07:01:37 -0700 matt welland wrote: > On Sun, 2011-02-20 at 06:56 -0500, Mario Domenech Goulart wrote: >> On Sun, 20 Feb 2011 05:32:23 -0500 Mario Domenech Goulart >> wrote: >> >> > On Sat, 19 Feb 2011 21:40:49 -0700 matt welland wrote: >> > >> >> I read though the docs but didn't see mention of cgi, is it supported? >> > >> > Unfortunately not. Awful runs on top of Spiffy. >> >> OTOH, if you can run a server on other ports, you can bind awful/spiffy >> to, say, port 8080 and use your front-end web server as a proxy for >> awful/spiffy. >> >> There's yet another approach, which is horrible and should probably not >> even be mentioned, but should still work (considering you can run a >> server and bind it to a port, and you _cannot_ use the front-end server >> as a proxy, but the CGI interface is available): make a CGI program >> which accesses awful giving it the request parameters (using the >> http-client egg, for example) and reply back to the front-end server the >> awful response. > > In your second awful scenario the (rather wonderful afaict) awful must > still be a long running process, correct? Yes, that's correct. Best wishes. Mario -- http://parenteses.org/mario ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] awful as cgi or fcgi?
On Sun, 2011-02-20 at 06:56 -0500, Mario Domenech Goulart wrote: > On Sun, 20 Feb 2011 05:32:23 -0500 Mario Domenech Goulart > wrote: > > > On Sat, 19 Feb 2011 21:40:49 -0700 matt welland wrote: > > > >> I read though the docs but didn't see mention of cgi, is it supported? > > > > Unfortunately not. Awful runs on top of Spiffy. > > OTOH, if you can run a server on other ports, you can bind awful/spiffy > to, say, port 8080 and use your front-end web server as a proxy for > awful/spiffy. > > There's yet another approach, which is horrible and should probably not > even be mentioned, but should still work (considering you can run a > server and bind it to a port, and you _cannot_ use the front-end server > as a proxy, but the CGI interface is available): make a CGI program > which accesses awful giving it the request parameters (using the > http-client egg, for example) and reply back to the front-end server the > awful response. In your second awful scenario the (rather wonderful afaict) awful must still be a long running process, correct? > That'd be totally awful, although awful would be just part of the whole > "solution". > > > Best wishes. > Mario ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] cvjm
Hello! I have imported an attempt at a static Java->Scheme compiler into the repository, which can be found in the "release/4/cvjm/trunk" directory. It compiles Java ".class" files into low-level Scheme code with full support for tail calls and continuations. The compiler seems to be working, but there is only a minimal runtime library yet, which is just about enough to run some basic tests. I have started once to port GNU classpath to it, but it turned out to be difficult a task for me, since I have very little Java experience. Usually, I would just throw this away, but too much work has already been put into it, so I have added it to the repository in the hope that someone can be found who would be interested in continuing on the project. I will happily give assistance as much as I can with the core runtime system and compiler (the latter being quite a complicated beast), if needed. This is quite an interesting project which could bring absolutely seemless Java-integration, at least for libraries written in pure Java, and which don't make too much use of reflection (since this is a static compiler, dynamic class definition is not supported and introspection will be limited). cheers, felix ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Chicken segfaults with large-ish inputs
From: Sam Hardwick Subject: [Chicken-users] Chicken segfaults with large-ish inputs Date: Sat, 19 Feb 2011 23:47:42 +0200 > (define (sum term a next b) > (if (> a b) > 0 > (+ (term a) > (sum term (next a) next b > > (define (adder n) (lambda (x) (+ x n))) > > (define (integral f a b n) > (let ((h (/ (- b a) n))) > (define (y k) (f (+ a (* k h > (* > (/ h 3) > (+ a b > (* 4 (sum y 1 (adder 2) (- n 1))) > (* 2 (sum y 2 (adder 2) (- n 2))) > > *** > > When I exercise it with intervals divided into up to 10 it works > fine, but with eg. > > *** > > (define (cube x) (* x x x)) > (integral cube 0 1 100) Ouch. This builds up an extremely deep chain of non-tail calls in "sum" (I believe), which overflows the stack inside the interpreter (when I compile the code, it seems to run ok - assumedly because the stack usage is more efficient in compiled code). Stack checks (which cause GC and thus reclamation of the stack) do not happen inside the continuation calls done when the deep chain of calls to "+" return (if this makes no sense to you, then nevermind, it barely makes sense to me). I do not have a quick fix for this, but will try to find a solution, which may be successful or maybe not. cheers, felix ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] awful as cgi or fcgi?
On Sun, 20 Feb 2011 05:32:23 -0500 Mario Domenech Goulart wrote: > On Sat, 19 Feb 2011 21:40:49 -0700 matt welland wrote: > >> I read though the docs but didn't see mention of cgi, is it supported? > > Unfortunately not. Awful runs on top of Spiffy. OTOH, if you can run a server on other ports, you can bind awful/spiffy to, say, port 8080 and use your front-end web server as a proxy for awful/spiffy. There's yet another approach, which is horrible and should probably not even be mentioned, but should still work (considering you can run a server and bind it to a port, and you _cannot_ use the front-end server as a proxy, but the CGI interface is available): make a CGI program which accesses awful giving it the request parameters (using the http-client egg, for example) and reply back to the front-end server the awful response. That'd be totally awful, although awful would be just part of the whole "solution". Best wishes. Mario -- http://parenteses.org/mario ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] awful as cgi or fcgi?
Hi Matt, On Sat, 19 Feb 2011 21:40:49 -0700 matt welland wrote: > I read though the docs but didn't see mention of cgi, is it supported? Unfortunately not. Awful runs on top of Spiffy. Best wishes. Mario -- http://parenteses.org/mario ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users