From a136267dd75e2933fead8ec911472b9321ddc3b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ludovic=20Court=C3=A8s?= Date: Sun, 8 Jun 2008 22:18:28 +0200 Subject: [PATCH] Use `scm_set_port_read ()' when available for bytevector input ports. * configure.ac: Look for `scm_set_port_read ()'. * src/Makefile.am (noinst_HEADERS): Add `compat.h'. * src/compat.h: New file. * src/ports.c: Include "compat.h". (MIN): New macro. (bip_read): New. (initialize_bytevector_input_ports): Use it. --- configure.ac | 11 +++++++++++ src/Makefile.am | 2 +- src/compat.h | 36 ++++++++++++++++++++++++++++++++++++ src/ports.c | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/compat.h diff --git a/configure.ac b/configure.ac index 7cc92a2..04c302d 100644 --- a/configure.ac +++ b/configure.ac @@ -52,6 +52,17 @@ AC_CHECK_SIZEOF([void *]) # Checks for library functions. AC_FUNC_MEMCMP +# Look for `scm_set_port_read ()' in libguile, scheduled to arrive in +# Guile 1.8.6. +save_CFLAGS="$CFLAGS" +save_LDFLAGS="$LDFLAGS" +CFLAGS="$CFLAGS $GUILE_CFLAGS" +LDFLAGS="$LDFLAGS $GUILE_LDFLAGS" +AC_CHECK_FUNCS([scm_set_port_read]) +CFLAGS="$save_CFLAGS" +LDFLAGS="$save_LDFLAGS" + + # Low-level details. AC_C_BIGENDIAN diff --git a/src/Makefile.am b/src/Makefile.am index e79e1dd..bb2258b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ AM_LDFLAGS = $(GUILE_LDFLAGS) $(LDFLAGS) r6rsincludedir = $(includedir)/guile-r6rs-libs r6rsinclude_HEADERS = bytevector.h ports.h -noinst_HEADERS = utils.h ieee-754.h +noinst_HEADERS = utils.h ieee-754.h compat.h snarfcppopts = $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ $(CFLAGS) $(AM_CFLAGS) diff --git a/src/compat.h b/src/compat.h new file mode 100644 index 0000000..cafdfc6 --- /dev/null +++ b/src/compat.h @@ -0,0 +1,36 @@ +/* Guile-R6RS-Libs --- Implementation of R6RS standard libraries. + Copyright (C) 2008 Ludovic Courtès + + Guile-R6RS-Libs is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + Guile-R6RS-Libs is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with Guile-R6RS-Libs; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#ifndef GUILE_R6RS_COMPAT_H +#define GUILE_R6RS_COMPAT_H + +#ifdef HAVE_CONFIG_H +# include +#endif + +/* The `scm_set_port_read ()' method is scheduled to appear in Guile 1.8.6, + providing a flexible and better performing replacement for the + `fill_input' port method. */ + +#ifdef HAVE_SCM_SET_PORT_READ +# define PORT_FILL_INPUT_METHOD(m) NULL +#else +# define PORT_FILL_INPUT_METHOD(m) (m) +# define scm_set_port_read(p, r) do { } while (0) +#endif + +#endif diff --git a/src/ports.c b/src/ports.c index 8d5c016..27c419d 100644 --- a/src/ports.c +++ b/src/ports.c @@ -1,5 +1,5 @@ /* Guile-R6RS-Libs --- Implementation of R6RS standard libraries. - Copyright (C) 2007 Ludovic Courtès + Copyright (C) 2007, 2008 Ludovic Courtès Guile-R6RS-Libs is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,6 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h" +#include "compat.h" #include #include @@ -54,6 +55,10 @@ SCM_DEFINE (scm_r6rs_eof_object, "eof-object", 0, 0, 0, /* Input ports. */ +#ifndef MIN +# define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + /* Bytevector input ports or "bip" for short. */ static scm_t_bits bytevector_input_port_type = 0; @@ -92,6 +97,8 @@ bip_mark (SCM port) return (SCM_PACK (SCM_STREAM (port))); } +#ifndef HAVE_SCM_SET_PORT_READ + static int bip_fill_input (SCM port) { @@ -106,6 +113,25 @@ bip_fill_input (SCM port) return result; } +#else /* HAVE_SCM_SET_PORT_READ */ + +static size_t +bip_read (SCM port, void *buffer, size_t size) +{ + size_t remaining, count; + scm_t_port *c_port = SCM_PTAB_ENTRY (port); + + remaining = c_port->read_end - c_port->read_pos; + count = MIN (remaining, size); + + memcpy (buffer, c_port->read_pos, count); + c_port->read_pos += count; + + return count; +} + +#endif /* HAVE_SCM_SET_PORT_READ */ + static off_t bip_seek (SCM port, off_t offset, int whence) #define FUNC_NAME "bip_seek" @@ -155,8 +181,10 @@ initialize_bytevector_input_ports (void) { bytevector_input_port_type = scm_make_port_type ("r6rs-bytevector-input-port", - bip_fill_input, NULL); + PORT_FILL_INPUT_METHOD (bip_fill_input), + NULL); + scm_set_port_read (bytevector_input_port_type, bip_read); scm_set_port_mark (bytevector_input_port_type, bip_mark); scm_set_port_seek (bytevector_input_port_type, bip_seek); } -- 2.11.4.GIT