From cafb39674537e42a5a173d78d833f9488f1f4f08 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sun, 21 Jun 2009 04:14:09 -0400 Subject: [PATCH] fixing output-string ports on linux --- gc.c | 10 ++++++++-- include/chibi/sexp.h | 4 ++++ sexp.c | 13 +++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/gc.c b/gc.c index 7144cbee..2972e49b 100644 --- a/gc.c +++ b/gc.c @@ -21,7 +21,11 @@ struct sexp_heap { }; static sexp_heap heap; + +#if USE_DEBUG_GC static sexp* stack_base; +#endif + extern sexp continuation_resumer, final_resumer; static sexp_heap sexp_heap_last (sexp_heap h) { @@ -154,7 +158,7 @@ sexp_heap sexp_make_heap (size_t size) { sexp free, next; sexp_heap h = (sexp_heap) malloc(sizeof(struct sexp_heap) + size); if (! h) { - fprintf(stderr, "out of memory allocating %lu byte heap, aborting\n", size); + fprintf(stderr, "out of memory allocating %zu byte heap, aborting\n", size); exit(70); } h->size = size; @@ -222,7 +226,7 @@ void* sexp_alloc (sexp ctx, size_t size) { sexp_grow_heap(ctx, size); res = sexp_try_alloc(ctx, size); if (! res) { - fprintf(stderr, "out of memory allocating %lu bytes, aborting\n", size); + fprintf(stderr, "out of memory allocating %zu bytes, aborting\n", size); exit(70); } } @@ -232,7 +236,9 @@ void* sexp_alloc (sexp ctx, size_t size) { void sexp_gc_init () { sexp_uint_t size = sexp_heap_align(SEXP_INITIAL_HEAP_SIZE); heap = sexp_make_heap(size); +#if USE_DEBUG_GC /* the +32 is a hack, but this is just for debugging anyway */ stack_base = ((sexp*)&size) + 32; +#endif } diff --git a/include/chibi/sexp.h b/include/chibi/sexp.h index 1a77abaa..2fd879d6 100644 --- a/include/chibi/sexp.h +++ b/include/chibi/sexp.h @@ -122,7 +122,9 @@ struct sexp_struct { } symbol; struct { FILE *stream; + char *buf; sexp_uint_t line; + size_t size; sexp name; sexp cookie; } port; @@ -364,6 +366,8 @@ sexp sexp_make_flonum(sexp ctx, double f); #define sexp_port_name(p) ((p)->value.port.name) #define sexp_port_line(p) ((p)->value.port.line) #define sexp_port_cookie(p) ((p)->value.port.cookie) +#define sexp_port_buf(p) ((p)->value.port.buf) +#define sexp_port_size(p) ((p)->value.port.size) #define sexp_exception_kind(p) ((p)->value.exception.kind) #define sexp_exception_message(p) ((p)->value.exception.message) diff --git a/sexp.c b/sexp.c index 5835c5b6..aade93c1 100644 --- a/sexp.c +++ b/sexp.c @@ -629,21 +629,14 @@ sexp sexp_make_input_string_port (sexp ctx, sexp str) { } sexp sexp_make_output_string_port (sexp ctx) { - FILE *out; - sexp buf = sexp_alloc_type(ctx, string, SEXP_STRING), res; - out = open_memstream((char**)&sexp_string_data(buf), (size_t*)&sexp_string_length(buf)); - res = sexp_make_input_port(ctx, out, SEXP_FALSE); - sexp_port_cookie(res) = buf; + sexp res = sexp_make_output_port(ctx, NULL, SEXP_FALSE); + sexp_port_stream(res) = open_memstream(&sexp_port_buf(res), &sexp_port_size(res)); return res; } sexp sexp_get_output_string (sexp ctx, sexp port) { - sexp cookie = sexp_port_cookie(port); fflush(sexp_port_stream(port)); - return sexp_substring(ctx, - cookie, - sexp_make_integer(0), - sexp_make_integer(sexp_string_length(cookie))); + return sexp_c_string(ctx, sexp_port_buf(port), sexp_port_size(port)); } #endif