diff options
author | Ludovic Courtès <ludo@gnu.org> | 2011-03-06 11:42:37 +0100 |
---|---|---|
committer | Ludovic Courtès <ludo@gnu.org> | 2011-03-06 23:05:00 +0100 |
commit | 0b2c2ba353d9dcf0b288950b88d6f205a5ec67ab (patch) | |
tree | 80941b1bfb370bb062f70b8751e8c44d5673b931 | |
parent | 691fcf66c0a823b2c4f4018e925cf9f338a4de27 (diff) | |
download | guile-0b2c2ba353d9dcf0b288950b88d6f205a5ec67ab.tar.gz |
Let `scm_mkstrport' allocate buffers on the caller's behalf.
* libguile/strports.c (INITIAL_BUFFER_SIZE): New macro.
(scm_mkstrport): If STR is false, allocate a bytevector on the
caller's behalf.
(scm_object_to_string, scm_call_with_output_string,
scm_open_output_string): Pass SCM_BOOL_F as the STR argument of
`scm_mkstrport'.
* libguile/backtrace.c (scm_display_application,
display_backtrace_body): Likewise.
* libguile/gdbint.c (scm_init_gdbint): Likewise.
* libguile/print.c (scm_simple_format): Likewise.
-rw-r--r-- | libguile/backtrace.c | 7 | ||||
-rw-r--r-- | libguile/gdbint.c | 12 | ||||
-rw-r--r-- | libguile/print.c | 3 | ||||
-rw-r--r-- | libguile/strports.c | 61 |
4 files changed, 46 insertions, 37 deletions
diff --git a/libguile/backtrace.c b/libguile/backtrace.c index c7abe3173..7140228c2 100644 --- a/libguile/backtrace.c +++ b/libguile/backtrace.c @@ -278,9 +278,7 @@ SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0, scm_print_state *pstate; /* Create a string port used for adaptation of printing parameters. */ - sport = scm_mkstrport (SCM_INUM0, - scm_make_string (scm_from_int (240), - SCM_UNDEFINED), + sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_WRTNG, FUNC_NAME); @@ -473,8 +471,7 @@ display_backtrace_body (struct display_backtrace_args *a) SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace); /* Create a string port used for adaptation of printing parameters. */ - sport = scm_mkstrport (SCM_INUM0, - scm_make_string (scm_from_int (240), SCM_UNDEFINED), + sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_WRTNG, FUNC_NAME); diff --git a/libguile/gdbint.c b/libguile/gdbint.c index 7cc9535d7..77fdbd17a 100644 --- a/libguile/gdbint.c +++ b/libguile/gdbint.c @@ -1,5 +1,5 @@ /* GDB interface for Guile - * Copyright (C) 1996,1997,1999,2000,2001,2002,2004,2009 + * Copyright (C) 1996,1997,1999,2000,2001,2002,2004,2009,2011 * Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or @@ -248,15 +248,13 @@ scm_init_gdbint () SCM port; scm_print_carefully_p = 0; - - port = scm_mkstrport (SCM_INUM0, - scm_c_make_string (0, SCM_UNDEFINED), + + port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_WRTNG, s); gdb_output_port = scm_permanent_object (port); - - port = scm_mkstrport (SCM_INUM0, - scm_c_make_string (0, SCM_UNDEFINED), + + port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_RDNG | SCM_WRTNG, s); gdb_input_port = scm_permanent_object (port); diff --git a/libguile/print.c b/libguile/print.c index 3855146b1..e3c9e1c92 100644 --- a/libguile/print.c +++ b/libguile/print.c @@ -1284,8 +1284,7 @@ SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1, else if (scm_is_false (destination)) { fReturnString = 1; - port = scm_mkstrport (SCM_INUM0, - scm_make_string (SCM_INUM0, SCM_UNDEFINED), + port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_WRTNG, FUNC_NAME); destination = port; diff --git a/libguile/strports.c b/libguile/strports.c index 3d951ce0b..83674e29e 100644 --- a/libguile/strports.c +++ b/libguile/strports.c @@ -280,6 +280,12 @@ st_truncate (SCM port, scm_t_off length) pt->write_pos = pt->read_end; } +/* The initial size in bytes of a string port's buffer. */ +#define INITIAL_BUFFER_SIZE 128 + +/* Return a new string port with MODES. If STR is #f, a new backing + buffer is allocated; otherwise STR must be a string and a copy of it + serves as the buffer for the new port. */ SCM scm_mkstrport (SCM pos, SCM str, long modes, const char *caller) { @@ -297,23 +303,36 @@ scm_mkstrport (SCM pos, SCM str, long modes, const char *caller) z = scm_new_port_table_entry (scm_tc16_strport); pt = SCM_PTAB_ENTRY(z); - { - /* STR is a string. */ - char *copy; + if (scm_is_false (str)) + { + /* Allocate a new buffer to write to. */ + str_len = INITIAL_BUFFER_SIZE; + buf = scm_c_make_bytevector (str_len); + c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf); + + /* Reset `read_buf_size'. It will contain the actual number of + bytes written to PT. */ + pt->read_buf_size = 0; + c_pos = 0; + } + else + { + /* STR is a string. */ + char *copy; - SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller); + SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller); - /* Create a copy of STR in the encoding of PT. */ - copy = scm_to_stringn (str, &str_len, pt->encoding, - SCM_FAILED_CONVERSION_ERROR); - buf = scm_c_make_bytevector (str_len); - c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf); - memcpy (c_buf, copy, str_len); - free (copy); + /* Create a copy of STR in the encoding of PT. */ + copy = scm_to_stringn (str, &str_len, pt->encoding, + SCM_FAILED_CONVERSION_ERROR); + buf = scm_c_make_bytevector (str_len); + c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf); + memcpy (c_buf, copy, str_len); + free (copy); - c_pos = scm_to_unsigned_integer (pos, 0, str_len); - pt->read_buf_size = str_len; - } + c_pos = scm_to_unsigned_integer (pos, 0, str_len); + pt->read_buf_size = str_len; + } SCM_SETSTREAM (z, SCM_UNPACK (buf)); SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes); @@ -369,13 +388,13 @@ SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0, "argument @var{printer} (default: @code{write}).") #define FUNC_NAME s_scm_object_to_string { - SCM str, port; + SCM port; if (!SCM_UNBNDP (printer)) SCM_VALIDATE_PROC (2, printer); - str = scm_c_make_string (0, SCM_UNDEFINED); - port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, FUNC_NAME); + port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, + SCM_OPN | SCM_WRTNG, FUNC_NAME); if (SCM_UNBNDP (printer)) scm_write (obj, port); @@ -395,8 +414,7 @@ SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0, { SCM p; - p = scm_mkstrport (SCM_INUM0, - scm_make_string (SCM_INUM0, SCM_UNDEFINED), + p = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_WRTNG, FUNC_NAME); scm_call_1 (proc, p); @@ -441,8 +459,7 @@ SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0, { SCM p; - p = scm_mkstrport (SCM_INUM0, - scm_make_string (SCM_INUM0, SCM_UNDEFINED), + p = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_OPN | SCM_WRTNG, FUNC_NAME); return p; @@ -467,8 +484,6 @@ SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0, SCM scm_c_read_string (const char *expr) { - /* FIXME: the c string gets packed into a string, only to get - immediately unpacked in scm_mkstrport. */ SCM port = scm_mkstrport (SCM_INUM0, scm_from_locale_string (expr), SCM_OPN | SCM_RDNG, |