Add `scm_t_off' type so that `scm_t_port' has a fixed layout.
[bpt/guile.git] / libguile / ports.h
index 866b74d..8a21b09 100644 (file)
@@ -2,55 +2,33 @@
 
 #ifndef SCM_PORTS_H
 #define SCM_PORTS_H
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
- * 
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * This program 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 General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
+
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
  *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
+ * This library 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 3 of
+ * the License, or (at your option) any later version.
  *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
+ * This library 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.
  *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 
 \f
+
 #include "libguile/__scm.h"
 
 #include "libguile/print.h"
 #include "libguile/struct.h"
+#include "libguile/threads.h"
 
-/* Not sure if this is a good idea.  We need it for off_t.  */
-#include <sys/types.h>
 
 \f
 
@@ -68,7 +46,6 @@ typedef enum scm_t_port_rw_active {
 typedef struct 
 {
   SCM port;                    /* Link back to the port object.  */
-  long entry;                  /* Index in port table. */
   int revealed;                        /* 0 not revealed, > 1 revealed.
                                 * Revealed ports do not get GC'd.
                                 */
@@ -91,7 +68,7 @@ typedef struct
   unsigned char *read_buf;     /* buffer start.  */
   const unsigned char *read_pos;/* the next unread char.  */
   unsigned char *read_end;      /* pointer to last buffered char + 1.  */
-  off_t read_buf_size;         /* size of the buffer.  */
+  scm_t_off read_buf_size;             /* size of the buffer.  */
 
   /* when chars are put back into the buffer, e.g., using peek-char or
      unread-string, the read-buffer pointers are switched to cbuf.
@@ -100,7 +77,7 @@ typedef struct
   unsigned char *saved_read_buf;
   const unsigned char *saved_read_pos;
   unsigned char *saved_read_end;
-  off_t saved_read_buf_size;
+  scm_t_off saved_read_buf_size;
 
   /* write requests are saved into this buffer at write_pos until it
      reaches write_buf + write_buf_size, then the ptob flush is
@@ -109,7 +86,7 @@ typedef struct
   unsigned char *write_buf;     /* buffer start.  */
   unsigned char *write_pos;     /* pointer to last buffered char + 1.  */
   unsigned char *write_end;     /* pointer to end of buffer + 1.  */
-  off_t write_buf_size;                /* size of the buffer.  */
+  scm_t_off write_buf_size;            /* size of the buffer.  */
 
   unsigned char shortbuf;       /* buffer for "unbuffered" streams.  */
 
@@ -130,14 +107,16 @@ typedef struct
   size_t putback_buf_size;        /* allocated size of putback_buf.  */
 } scm_t_port;
 
-extern scm_t_port **scm_t_portable;
-extern long scm_t_portable_size; /* Number of ports in scm_t_portable.  */
+
+SCM_INTERNAL scm_i_pthread_mutex_t scm_i_port_table_mutex;
+SCM_INTERNAL SCM scm_i_port_weak_hash;
+
 
 #define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
 
 \f
 
-#define SCM_EOF_OBJECT_P(x) (SCM_EQ_P ((x), SCM_EOF_VAL))
+#define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
 
 /* PORT FLAGS
  * A set of flags characterizes a port.
@@ -150,36 +129,41 @@ extern long scm_t_portable_size; /* Number of ports in scm_t_portable.  */
 #define SCM_BUF0       (8L<<16) /* Is it unbuffered? */
 #define SCM_BUFLINE     (64L<<16) /* Is it line-buffered? */
 
-#define SCM_PORTP(x) (SCM_NIMP (x) && (SCM_TYP7 (x) == scm_tc7_port))
-#define SCM_OPPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
-#define SCM_OPINPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
-#define SCM_OPOUTPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
+#define SCM_PORTP(x) (!SCM_IMP (x) && (SCM_TYP7 (x) == scm_tc7_port))
+#define SCM_OPPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
+#define SCM_OPINPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
+#define SCM_OPOUTPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
 #define SCM_INPUT_PORT_P(x) \
-  (SCM_NIMP(x) \
+  (!SCM_IMP(x) \
    && (((0x7f | SCM_RDNG) & SCM_CELL_WORD_0(x)) == (scm_tc7_port | SCM_RDNG)))
 #define SCM_OUTPUT_PORT_P(x) \
-  (SCM_NIMP(x) \
+  (!SCM_IMP(x) \
    && (((0x7f | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_WRTNG)))
-#define SCM_OPENP(x) (SCM_NIMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
+#define SCM_OPENP(x) (!SCM_IMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
 #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
 #define SCM_CLR_PORT_OPEN_FLAG(p) \
   SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
 
 #define SCM_PTAB_ENTRY(x)         ((scm_t_port *) SCM_CELL_WORD_1 (x))
-#define SCM_SETPTAB_ENTRY(x,ent)  (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
+#define SCM_SETPTAB_ENTRY(x, ent)  (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
 #define SCM_STREAM(x)             (SCM_PTAB_ENTRY(x)->stream)
-#define SCM_SETSTREAM(x,s)        (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
+#define SCM_SETSTREAM(x, s)        (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
 #define SCM_FILENAME(x)           (SCM_PTAB_ENTRY(x)->file_name)
 #define SCM_SET_FILENAME(x, n)    (SCM_PTAB_ENTRY(x)->file_name = (n))
 #define SCM_LINUM(x)              (SCM_PTAB_ENTRY(x)->line_number)
 #define SCM_COL(x)                (SCM_PTAB_ENTRY(x)->column_number)
 #define SCM_REVEALED(x)           (SCM_PTAB_ENTRY(x)->revealed)
-#define SCM_SETREVEALED(x,s)      (SCM_PTAB_ENTRY(x)->revealed = (s))
+#define SCM_SETREVEALED(x, s)      (SCM_PTAB_ENTRY(x)->revealed = (s))
 
 #define SCM_INCLINE(port)      {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
+#define SCM_ZEROCOL(port)      {SCM_COL (port) = 0;}
 #define SCM_INCCOL(port)       {SCM_COL (port) += 1;}
+#define SCM_DECCOL(port)       {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;}
 #define SCM_TABCOL(port)       {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
 
+/* Maximum number of port types.  */
+#define SCM_I_MAX_PORT_TYPE_COUNT  256
+
 \f
 
 /* port-type description.  */
@@ -199,17 +183,11 @@ typedef struct scm_t_ptob_descriptor
   int (*fill_input) (SCM port);
   int (*input_waiting) (SCM port);
 
-  off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
-  void (*truncate) (SCM port, off_t length);
+  scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
+  void (*truncate) (SCM port, scm_t_off length);
 
 } scm_t_ptob_descriptor;
 
-#if (SCM_DEBUG_DEPRECATED == 0)
-# define scm_port scm_t_port
-# define scm_ptob_descriptor scm_t_ptob_descriptor
-# define scm_port_rw_active scm_t_port_rw_active
-#endif
-
 #define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
 #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
 /* SCM_PTOBNAME can be 0 if name is missing */
@@ -217,114 +195,118 @@ typedef struct scm_t_ptob_descriptor
 
 \f
 
-extern scm_t_ptob_descriptor *scm_ptobs;
-extern long scm_numptob;
-extern long scm_t_portable_room;
+SCM_API scm_t_ptob_descriptor *scm_ptobs;
+SCM_API long scm_numptob;
+SCM_INTERNAL long scm_i_port_table_room;
 
 \f
 
-extern SCM scm_markstream (SCM ptr);
-extern scm_t_bits scm_make_port_type (char *name,
-                                     int (*fill_input) (SCM port),
-                                     void (*write) (SCM port, 
-                                                    const void *data,
-                                                    size_t size));
-extern void scm_set_port_mark (long tc, SCM (*mark) (SCM));
-extern void scm_set_port_free (long tc, size_t (*free) (SCM));
-extern void scm_set_port_print (long tc,
-                               int (*print) (SCM exp,
-                                             SCM port,
-                                             scm_print_state *pstate));
-extern void scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM));
-extern void scm_set_port_close (long tc, int (*close) (SCM));
-
-extern void scm_set_port_flush (long tc, 
-                               void (*flush) (SCM port));
-extern void scm_set_port_end_input (long tc,
-                                   void (*end_input) (SCM port,
-                                                      int offset));
-extern void scm_set_port_seek (long tc,
-                              off_t (*seek) (SCM port,
-                                             off_t OFFSET,
-                                             int WHENCE));
-extern void scm_set_port_truncate (long tc,
-                                  void (*truncate) (SCM port,
-                                                    off_t length));
-extern void scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM));
-extern SCM scm_char_ready_p (SCM port);
+SCM_API SCM scm_markstream (SCM ptr);
+SCM_API scm_t_bits scm_make_port_type (char *name,
+                                      int (*fill_input) (SCM port),
+                                      void (*write) (SCM port, 
+                                                     const void *data,
+                                                     size_t size));
+SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
+SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
+SCM_API void scm_set_port_print (scm_t_bits tc,
+                                int (*print) (SCM exp,
+                                              SCM port,
+                                              scm_print_state *pstate));
+SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
+SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
+
+SCM_API void scm_set_port_flush (scm_t_bits tc, 
+                                void (*flush) (SCM port));
+SCM_API void scm_set_port_end_input (scm_t_bits tc,
+                                    void (*end_input) (SCM port,
+                                                       int offset));
+SCM_API void scm_set_port_seek (scm_t_bits tc,
+                               scm_t_off (*seek) (SCM port,
+                                                  scm_t_off OFFSET,
+                                                  int WHENCE));
+SCM_API void scm_set_port_truncate (scm_t_bits tc,
+                                   void (*truncate) (SCM port,
+                                                     scm_t_off length));
+SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
+SCM_API SCM scm_char_ready_p (SCM port);
 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
-extern SCM scm_drain_input (SCM port);
-extern SCM scm_current_input_port (void);
-extern SCM scm_current_output_port (void);
-extern SCM scm_current_error_port (void);
-extern SCM scm_current_load_port (void);
-extern SCM scm_set_current_input_port (SCM port);
-extern SCM scm_set_current_output_port (SCM port);
-extern SCM scm_set_current_error_port (SCM port);
-extern scm_t_port * scm_add_to_port_table (SCM port);
-extern void scm_remove_from_port_table (SCM port);
-extern void scm_grow_port_cbuf (SCM port, size_t requested);
-extern SCM scm_pt_size (void);
-extern SCM scm_pt_member (SCM member);
-extern void scm_port_non_buffer (scm_t_port *pt);
-extern int scm_revealed_count (SCM port);
-extern SCM scm_port_revealed (SCM port);
-extern SCM scm_set_port_revealed_x (SCM port, SCM rcount);
-extern long scm_mode_bits (char *modes);
-extern SCM scm_port_mode (SCM port);
-extern SCM scm_close_input_port (SCM port);
-extern SCM scm_close_output_port (SCM port);
-extern SCM scm_close_port (SCM port);
-extern SCM scm_port_for_each (SCM proc);
-extern SCM scm_input_port_p (SCM x);
-extern SCM scm_output_port_p (SCM x);
-extern SCM scm_port_p (SCM x);
-extern SCM scm_port_closed_p (SCM port);
-extern SCM scm_eof_object_p (SCM x);
-extern SCM scm_force_output (SCM port);
-extern SCM scm_flush_all_ports (void);
-extern SCM scm_read_char (SCM port);
-extern void scm_putc (char c, SCM port);
-extern void scm_puts (const char *str_data, SCM port);
-extern size_t scm_c_read (SCM port, void *buffer, size_t size);
-extern void scm_c_write (SCM port, const void *buffer, size_t size);
-extern void scm_lfwrite (const char *ptr, size_t size, SCM port);
-extern void scm_flush (SCM port);
-extern void scm_end_input (SCM port);
-extern int scm_fill_input (SCM port);
-extern int scm_getc (SCM port);
-extern void scm_ungetc (int c, SCM port);
-extern void scm_ungets (const char *s, int n, SCM port);
-extern SCM scm_peek_char (SCM port);
-extern SCM scm_unread_char (SCM cobj, SCM port);
-extern SCM scm_unread_string (SCM str, SCM port);
-extern SCM scm_seek (SCM object, SCM offset, SCM whence);
-extern SCM scm_truncate_file (SCM object, SCM length);
-extern SCM scm_port_line (SCM port);
-extern SCM scm_set_port_line_x (SCM port, SCM line);
-extern SCM scm_port_column (SCM port);
-extern SCM scm_set_port_column_x (SCM port, SCM line);
-extern SCM scm_port_filename (SCM port);
-extern SCM scm_set_port_filename_x (SCM port, SCM filename);
-extern int scm_port_print (SCM exp, SCM port, scm_print_state *);
-extern void scm_print_port_mode (SCM exp, SCM port);
-extern void scm_ports_prehistory (void);
-extern SCM scm_void_port (char * mode_str);
-extern SCM scm_sys_make_void_port (SCM mode);
-extern void scm_init_ports (void);
+SCM_API SCM scm_drain_input (SCM port);
+SCM_API SCM scm_current_input_port (void);
+SCM_API SCM scm_current_output_port (void);
+SCM_API SCM scm_current_error_port (void);
+SCM_API SCM scm_current_load_port (void);
+SCM_API SCM scm_set_current_input_port (SCM port);
+SCM_API SCM scm_set_current_output_port (SCM port);
+SCM_API SCM scm_set_current_error_port (SCM port);
+SCM_API void scm_dynwind_current_input_port (SCM port);
+SCM_API void scm_dynwind_current_output_port (SCM port);
+SCM_API void scm_dynwind_current_error_port (SCM port);
+SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
+SCM_INTERNAL void scm_i_remove_port (SCM port);
+SCM_API void scm_grow_port_cbuf (SCM port, size_t requested);
+SCM_API SCM scm_pt_size (void);
+SCM_API SCM scm_pt_member (SCM member);
+SCM_API void scm_port_non_buffer (scm_t_port *pt);
+SCM_API int scm_revealed_count (SCM port);
+SCM_API SCM scm_port_revealed (SCM port);
+SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
+SCM_API long scm_mode_bits (char *modes);
+SCM_API SCM scm_port_mode (SCM port);
+SCM_API SCM scm_close_input_port (SCM port);
+SCM_API SCM scm_close_output_port (SCM port);
+SCM_API SCM scm_close_port (SCM port);
+SCM_API SCM scm_port_for_each (SCM proc);
+SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
+SCM_API SCM scm_input_port_p (SCM x);
+SCM_API SCM scm_output_port_p (SCM x);
+SCM_API SCM scm_port_p (SCM x);
+SCM_API SCM scm_port_closed_p (SCM port);
+SCM_API SCM scm_eof_object_p (SCM x);
+SCM_API SCM scm_force_output (SCM port);
+SCM_API SCM scm_flush_all_ports (void);
+SCM_API SCM scm_read_char (SCM port);
+SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
+SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
+SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
+SCM_API void scm_flush (SCM port);
+SCM_API void scm_end_input (SCM port);
+SCM_API int scm_fill_input (SCM port);
+SCM_API void scm_ungetc (int c, SCM port);
+SCM_API void scm_ungets (const char *s, int n, SCM port);
+SCM_API SCM scm_peek_char (SCM port);
+SCM_API SCM scm_unread_char (SCM cobj, SCM port);
+SCM_API SCM scm_unread_string (SCM str, SCM port);
+SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
+SCM_API SCM scm_truncate_file (SCM object, SCM length);
+SCM_API SCM scm_port_line (SCM port);
+SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
+SCM_API SCM scm_port_column (SCM port);
+SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
+SCM_API SCM scm_port_filename (SCM port);
+SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
+SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
+SCM_API void scm_print_port_mode (SCM exp, SCM port);
+SCM_API void scm_ports_prehistory (void);
+SCM_API SCM scm_void_port (char * mode_str);
+SCM_API SCM scm_sys_make_void_port (SCM mode);
+SCM_INTERNAL void scm_init_ports (void);
+
+
+#if SCM_ENABLE_DEPRECATED==1
+SCM_API scm_t_port * scm_add_to_port_table (SCM port);
+#endif
 
 #ifdef GUILE_DEBUG
-extern SCM scm_pt_size (void);
-extern SCM scm_pt_member (SCM member);
+SCM_API SCM scm_pt_size (void);
+SCM_API SCM scm_pt_member (SCM member);
 #endif /* GUILE_DEBUG */
 
-\f
-
-#if (SCM_DEBUG_DEPRECATED == 0)
+/* internal */
 
-extern SCM scm_close_all_ports_except (SCM ports);
+SCM_INTERNAL long scm_i_mode_bits (SCM modes);
+SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
 
-#endif  /* SCM_DEBUG_DEPRECATED == 0 */
 
 #endif  /* SCM_PORTS_H */