guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 02/15: Refactor to internal get/peek-byte functions


From: Andy Wingo
Subject: [Guile-commits] 02/15: Refactor to internal get/peek-byte functions
Date: Tue, 26 Apr 2016 21:38:52 +0000

wingo pushed a commit to branch wip-port-refactor
in repository guile.

commit fb577b59af2619edd78fea71ce6250a36376abdd
Author: Andy Wingo <address@hidden>
Date:   Fri Apr 22 17:12:58 2016 +0200

    Refactor to internal get/peek-byte functions
    
    * libguile/ports.h (scm_get_byte_or_eof_unlocked)
      (scm_peek_byte_or_eof_unlocked): Remove inline functions.  The
      important uses are in ports.c anyway and we will use a static function
      there.
      (scm_slow_get_byte_or_eof_unlocked)
      (scm_slow_peek_byte_or_eof_unlocked): Remove declarations without
      definitions.
    * libguile/ports.c (looking_at_bytes): Use scm_peek_byte_or_eof instead
      of the _unlocked variant.
      (get_byte_or_eof, peek_byte_or_eof): New static functions.
      (scm_get_byte_or_eof, scm_peek_byte_or_eof): Don't lock: the port
      buffer mechanism means that we won't crash.  More comments to come.
      (get_utf8_codepoint, get_latin1_codepoint, get_iconv_codepoint): Use
      new static functions.
    * libguile/read.c (read_token, scm_read_semicolon_comment): Use
      scm_get_byte_or_eof, not scm_get_byte_or_eof_unlocked.
---
 libguile/ports.c |  115 +++++++++++++++++++++++++++++++++++++++++-------------
 libguile/ports.h |   81 --------------------------------------
 libguile/read.c  |    6 +--
 3 files changed, 90 insertions(+), 112 deletions(-)

diff --git a/libguile/ports.c b/libguile/ports.c
index 0a424e0..b754e1b 100644
--- a/libguile/ports.c
+++ b/libguile/ports.c
@@ -1001,7 +1001,7 @@ looking_at_bytes (SCM port, const unsigned char *bytes, 
int len)
   scm_t_port *pt = SCM_PTAB_ENTRY (port);
   int i = 0;
 
-  while (i < len && scm_peek_byte_or_eof_unlocked (port) == bytes[i])
+  while (i < len && scm_peek_byte_or_eof (port) == bytes[i])
     {
       scm_port_buffer_did_take (pt->read_buf, 1);
       i++;
@@ -1364,32 +1364,91 @@ scm_dynwind_lock_port (SCM port)
 
 /* Input.  */
 
-int
-scm_get_byte_or_eof (SCM port)
+static int
+get_byte_or_eof (SCM port)
 {
-  scm_i_pthread_mutex_t *lock;
-  int ret;
+  SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
+  SCM buf_bv, buf_cur, buf_end;
+  size_t cur;
 
-  scm_c_lock_port (port, &lock);
-  ret = scm_get_byte_or_eof_unlocked (port);
-  if (lock)
-    scm_i_pthread_mutex_unlock (lock);
+  buf_bv = scm_port_buffer_bytevector (buf);
+  buf_cur = scm_port_buffer_cur (buf);
+  buf_end = scm_port_buffer_end (buf);
+  cur = SCM_I_INUM (buf_cur);
 
-  return ret;
+  if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
+      && SCM_LIKELY (SCM_I_INUMP (buf_end))
+      && SCM_LIKELY (cur < SCM_I_INUM (buf_end))
+      && SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
+    {
+      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
+      scm_port_buffer_set_cur (buf, SCM_I_MAKINUM (cur + 1));
+      return ret;
+    }
+
+  buf = scm_fill_input (port);
+  buf_bv = scm_port_buffer_bytevector (buf);
+  buf_cur = scm_port_buffer_cur (buf);
+  buf_end = scm_port_buffer_end (buf);
+  cur = scm_to_size_t (buf_cur);
+  if (cur < scm_to_size_t (buf_end))
+    {
+      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
+      scm_port_buffer_set_cur (buf, SCM_I_MAKINUM (cur + 1));
+      return ret;
+    }
+
+  /* The next peek or get should cause the read() function to be called
+     to see if we still have EOF.  */
+  scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F);
+  return EOF;
 }
 
-int
-scm_peek_byte_or_eof (SCM port)
+/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'.  */
+static int
+peek_byte_or_eof (SCM port)
 {
-  scm_i_pthread_mutex_t *lock;
-  int ret;
+  SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
+  SCM buf_bv, buf_cur, buf_end;
+  size_t cur;
 
-  scm_c_lock_port (port, &lock);
-  ret = scm_peek_byte_or_eof_unlocked (port);
-  if (lock)
-    scm_i_pthread_mutex_unlock (lock);
+  buf_bv = scm_port_buffer_bytevector (buf);
+  buf_cur = scm_port_buffer_cur (buf);
+  buf_end = scm_port_buffer_end (buf);
+  cur = scm_to_size_t (buf_cur);
+  if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
+      && SCM_LIKELY (SCM_I_INUMP (buf_end))
+      && SCM_LIKELY (cur < SCM_I_INUM (buf_end))
+      && SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
+    {
+      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
+      return ret;
+    }
 
-  return ret;
+  buf = scm_fill_input (port);
+  buf_bv = scm_port_buffer_bytevector (buf);
+  buf_cur = scm_port_buffer_cur (buf);
+  buf_end = scm_port_buffer_end (buf);
+  cur = scm_to_size_t (buf_cur);
+  if (cur < scm_to_size_t (buf_end))
+    {
+      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
+      return ret;
+    }
+
+  return EOF;
+}
+
+int
+scm_get_byte_or_eof (SCM port)
+{
+  return get_byte_or_eof (port);
+}
+
+int
+scm_peek_byte_or_eof (SCM port)
+{
+  return peek_byte_or_eof (port);
 }
 
 static size_t
@@ -1648,7 +1707,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
   *len = 0;
   pt = SCM_PTAB_ENTRY (port);
 
-  byte = scm_get_byte_or_eof_unlocked (port);
+  byte = get_byte_or_eof (port);
   if (byte == EOF)
     {
       *codepoint = EOF;
@@ -1664,7 +1723,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
   else if (buf[0] >= 0xc2 && buf[0] <= 0xdf)
     {
       /* 2-byte form.  */
-      byte = scm_peek_byte_or_eof_unlocked (port);
+      byte = peek_byte_or_eof (port);
       ASSERT_NOT_EOF (byte);
 
       if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
@@ -1680,7 +1739,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
   else if ((buf[0] & 0xf0) == 0xe0)
     {
       /* 3-byte form.  */
-      byte = scm_peek_byte_or_eof_unlocked (port);
+      byte = peek_byte_or_eof (port);
       ASSERT_NOT_EOF (byte);
 
       if (SCM_UNLIKELY ((byte & 0xc0) != 0x80
@@ -1692,7 +1751,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
       buf[1] = (scm_t_uint8) byte;
       *len = 2;
 
-      byte = scm_peek_byte_or_eof_unlocked (port);
+      byte = peek_byte_or_eof (port);
       ASSERT_NOT_EOF (byte);
 
       if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
@@ -1709,7 +1768,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
   else if (buf[0] >= 0xf0 && buf[0] <= 0xf4)
     {
       /* 4-byte form.  */
-      byte = scm_peek_byte_or_eof_unlocked (port);
+      byte = peek_byte_or_eof (port);
       ASSERT_NOT_EOF (byte);
 
       if (SCM_UNLIKELY (((byte & 0xc0) != 0x80)
@@ -1721,7 +1780,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
       buf[1] = (scm_t_uint8) byte;
       *len = 2;
 
-      byte = scm_peek_byte_or_eof_unlocked (port);
+      byte = peek_byte_or_eof (port);
       ASSERT_NOT_EOF (byte);
 
       if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
@@ -1731,7 +1790,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
       buf[2] = (scm_t_uint8) byte;
       *len = 3;
 
-      byte = scm_peek_byte_or_eof_unlocked (port);
+      byte = peek_byte_or_eof (port);
       ASSERT_NOT_EOF (byte);
 
       if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
@@ -1771,7 +1830,7 @@ static int
 get_latin1_codepoint (SCM port, scm_t_wchar *codepoint,
                       char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
 {
-  *codepoint = scm_get_byte_or_eof_unlocked (port);
+  *codepoint = get_byte_or_eof (port);
 
   if (*codepoint == EOF)
     *len = 0;
@@ -1801,7 +1860,7 @@ get_iconv_codepoint (SCM port, scm_t_wchar *codepoint,
       char *input, *output;
       size_t input_left, output_left, done;
 
-      byte_read = scm_get_byte_or_eof_unlocked (port);
+      byte_read = get_byte_or_eof (port);
       if (SCM_UNLIKELY (byte_read == EOF))
        {
           if (SCM_LIKELY (input_size == 0))
diff --git a/libguile/ports.h b/libguile/ports.h
index 92799cb..2cd6f8b 100644
--- a/libguile/ports.h
+++ b/libguile/ports.h
@@ -308,11 +308,7 @@ SCM_INLINE int scm_c_try_lock_port (SCM port, 
scm_i_pthread_mutex_t **lock);
 
 /* Input.  */
 SCM_API int scm_get_byte_or_eof (SCM port);
-SCM_INLINE int scm_get_byte_or_eof_unlocked (SCM port);
-SCM_API int scm_slow_get_byte_or_eof_unlocked (SCM port);
 SCM_API int scm_peek_byte_or_eof (SCM port);
-SCM_INLINE int scm_peek_byte_or_eof_unlocked (SCM port);
-SCM_API int scm_slow_peek_byte_or_eof_unlocked (SCM port);
 SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
 SCM_API size_t scm_c_read_unlocked (SCM port, void *buffer, size_t size);
 SCM_API size_t scm_c_read_bytes (SCM port, SCM dst, size_t start, size_t 
count);
@@ -421,83 +417,6 @@ scm_c_try_lock_port (SCM port, scm_i_pthread_mutex_t 
**lock)
     return 0;
 }
 
-SCM_INLINE_IMPLEMENTATION int
-scm_get_byte_or_eof_unlocked (SCM port)
-{
-  SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
-  SCM buf_bv, buf_cur, buf_end;
-  size_t cur;
-
-  buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
-  buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
-  buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
-  cur = SCM_I_INUM (buf_cur);
-
-  if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
-      && SCM_LIKELY (SCM_I_INUMP (buf_end))
-      && SCM_LIKELY (cur < SCM_I_INUM (buf_end))
-      && SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
-    {
-      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
-      buf_cur = SCM_I_MAKINUM (cur + 1);
-      SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_CUR, buf_cur);
-      return ret;
-    }
-
-  buf = scm_fill_input_unlocked (port);
-  buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
-  buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
-  buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
-  cur = scm_to_size_t (buf_cur);
-  if (cur < scm_to_size_t (buf_end))
-    {
-      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
-      buf_cur = SCM_I_MAKINUM (cur + 1);
-      SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_CUR, buf_cur);
-      return ret;
-    }
-
-  /* The next peek or get should cause the read() function to be called
-     to see if we still have EOF.  */
-  SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_HAS_EOF_P, SCM_BOOL_F);
-  return EOF;
-}
-
-/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'.  */
-SCM_INLINE_IMPLEMENTATION int
-scm_peek_byte_or_eof_unlocked (SCM port)
-{
-  SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
-  SCM buf_bv, buf_cur, buf_end;
-  size_t cur;
-
-  buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
-  buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
-  buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
-  cur = SCM_I_INUM (buf_cur);
-  if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
-      && SCM_LIKELY (SCM_I_INUMP (buf_end))
-      && SCM_LIKELY (cur < SCM_I_INUM (buf_end))
-      && SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
-    {
-      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
-      return ret;
-    }
-
-  buf = scm_fill_input_unlocked (port);
-  buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
-  buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
-  buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
-  cur = scm_to_size_t (buf_cur);
-  if (cur < scm_to_size_t (buf_end))
-    {
-      scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
-      return ret;
-    }
-
-  return EOF;
-}
-
 SCM_INLINE_IMPLEMENTATION void
 scm_putc_unlocked (char c, SCM port)
 {
diff --git a/libguile/read.c b/libguile/read.c
index d717ea2..144e39d 100644
--- a/libguile/read.c
+++ b/libguile/read.c
@@ -263,7 +263,7 @@ read_token (SCM port, scm_t_read_opts *opts,
      {
        int chr;
 
-       chr = scm_get_byte_or_eof_unlocked (port);
+       chr = scm_get_byte_or_eof (port);
 
        if (chr == EOF)
         return 0;
@@ -965,9 +965,9 @@ scm_read_semicolon_comment (int chr, SCM port)
   /* We use the get_byte here because there is no need to get the
      locale correct with comment input. This presumes that newline
      always represents itself no matter what the encoding is.  */
-  for (c = scm_get_byte_or_eof_unlocked (port);
+  for (c = scm_get_byte_or_eof (port);
        (c != EOF) && (c != '\n');
-       c = scm_get_byte_or_eof_unlocked (port));
+       c = scm_get_byte_or_eof (port));
 
   return SCM_UNSPECIFIED;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]