fixing output-string ports on linux

This commit is contained in:
Alex Shinn 2009-06-21 04:14:09 -04:00
parent 450548e3e2
commit cafb396745
3 changed files with 15 additions and 12 deletions

10
gc.c
View file

@ -21,7 +21,11 @@ struct sexp_heap {
}; };
static sexp_heap heap; static sexp_heap heap;
#if USE_DEBUG_GC
static sexp* stack_base; static sexp* stack_base;
#endif
extern sexp continuation_resumer, final_resumer; extern sexp continuation_resumer, final_resumer;
static sexp_heap sexp_heap_last (sexp_heap h) { 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 free, next;
sexp_heap h = (sexp_heap) malloc(sizeof(struct sexp_heap) + size); sexp_heap h = (sexp_heap) malloc(sizeof(struct sexp_heap) + size);
if (! h) { 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); exit(70);
} }
h->size = size; h->size = size;
@ -222,7 +226,7 @@ void* sexp_alloc (sexp ctx, size_t size) {
sexp_grow_heap(ctx, size); sexp_grow_heap(ctx, size);
res = sexp_try_alloc(ctx, size); res = sexp_try_alloc(ctx, size);
if (! res) { 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); exit(70);
} }
} }
@ -232,7 +236,9 @@ void* sexp_alloc (sexp ctx, size_t size) {
void sexp_gc_init () { void sexp_gc_init () {
sexp_uint_t size = sexp_heap_align(SEXP_INITIAL_HEAP_SIZE); sexp_uint_t size = sexp_heap_align(SEXP_INITIAL_HEAP_SIZE);
heap = sexp_make_heap(size); heap = sexp_make_heap(size);
#if USE_DEBUG_GC
/* the +32 is a hack, but this is just for debugging anyway */ /* the +32 is a hack, but this is just for debugging anyway */
stack_base = ((sexp*)&size) + 32; stack_base = ((sexp*)&size) + 32;
#endif
} }

View file

@ -122,7 +122,9 @@ struct sexp_struct {
} symbol; } symbol;
struct { struct {
FILE *stream; FILE *stream;
char *buf;
sexp_uint_t line; sexp_uint_t line;
size_t size;
sexp name; sexp name;
sexp cookie; sexp cookie;
} port; } 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_name(p) ((p)->value.port.name)
#define sexp_port_line(p) ((p)->value.port.line) #define sexp_port_line(p) ((p)->value.port.line)
#define sexp_port_cookie(p) ((p)->value.port.cookie) #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_kind(p) ((p)->value.exception.kind)
#define sexp_exception_message(p) ((p)->value.exception.message) #define sexp_exception_message(p) ((p)->value.exception.message)

13
sexp.c
View file

@ -629,21 +629,14 @@ sexp sexp_make_input_string_port (sexp ctx, sexp str) {
} }
sexp sexp_make_output_string_port (sexp ctx) { sexp sexp_make_output_string_port (sexp ctx) {
FILE *out; sexp res = sexp_make_output_port(ctx, NULL, SEXP_FALSE);
sexp buf = sexp_alloc_type(ctx, string, SEXP_STRING), res; sexp_port_stream(res) = open_memstream(&sexp_port_buf(res), &sexp_port_size(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;
return res; return res;
} }
sexp sexp_get_output_string (sexp ctx, sexp port) { sexp sexp_get_output_string (sexp ctx, sexp port) {
sexp cookie = sexp_port_cookie(port);
fflush(sexp_port_stream(port)); fflush(sexp_port_stream(port));
return sexp_substring(ctx, return sexp_c_string(ctx, sexp_port_buf(port), sexp_port_size(port));
cookie,
sexp_make_integer(0),
sexp_make_integer(sexp_string_length(cookie)));
} }
#endif #endif