+ SCM_SET_CELL_TYPE (z, scm_tc16_strport);
+ pt = SCM_PTAB_ENTRY (z);
+
+ /* Make PT initially empty, and release the port-table mutex
+ immediately. This is so that if one of the function calls below
+ raises an exception, a pre-unwind catch handler can still create
+ new ports; for instance, `display-backtrace' needs to be able to
+ allocate a new string port. See <http://bugs.gnu.org/11197>. */
+ scm_port_non_buffer (pt);
+ SCM_SETSTREAM (z, SCM_UNPACK (scm_null_bytevector));
+
+ scm_dynwind_end ();
+
+ 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);
+ c_pos = 0;
+ }
+ else
+ {
+ /* STR is a string. */
+ char *copy;
+
+ 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);
+
+ c_pos = scm_to_unsigned_integer (pos, 0, str_len);
+ }
+
+ /* Now, finish up the port. */
+ scm_i_pthread_mutex_lock (&scm_i_port_table_mutex);
+
+ SCM_SETSTREAM (z, SCM_UNPACK (buf));