Let `scm_mkstrport' allocate buffers on the caller's behalf.
authorLudovic Courtès <ludo@gnu.org>
Sun, 6 Mar 2011 10:42:37 +0000 (11:42 +0100)
committerLudovic Courtès <ludo@gnu.org>
Sun, 6 Mar 2011 22:05:00 +0000 (23:05 +0100)
* 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.

libguile/backtrace.c
libguile/gdbint.c
libguile/print.c
libguile/strports.c

index c7abe31..7140228 100644 (file)
@@ -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);
 
index 7cc9535..77fdbd1 100644 (file)
@@ -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);
index 3855146..e3c9e1c 100644 (file)
@@ -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;
index 3d951ce..83674e2 100644 (file)
@@ -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,