Fixed use of finalizers for guardians and SMOBs (undoes patches 23-24).
[bpt/guile.git] / libguile / srfi-4.c
index 2ee6516..940698b 100644 (file)
@@ -1,6 +1,6 @@
-/* srfi-4.c --- Homogeneous numeric vector datatypes.
+/* srfi-4.c --- Uniform numeric vector datatypes.
  *
- *     Copyright (C) 2001, 2004 Free Software Foundation, Inc.
+ *     Copyright (C) 2001, 2004, 2006 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -14,7 +14,7 @@
  *
  * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #if HAVE_CONFIG_H
@@ -35,6 +35,7 @@
 #include "libguile/vectors.h"
 #include "libguile/unif.h"
 #include "libguile/strings.h"
+#include "libguile/strports.h"
 #include "libguile/dynwind.h"
 #include "libguile/deprecation.h"
 
 #include <io.h>
 #endif
 
-/* Smob type code for homogeneous numeric vectors.  */
+/* Smob type code for uniform numeric vectors.  */
 int scm_tc16_uvec = 0;
 
 #define SCM_IS_UVEC(obj) SCM_SMOB_PREDICATE (scm_tc16_uvec, (obj))
 
-/* Accessor macros for the three components of a homogeneous numeric
+/* Accessor macros for the three components of a uniform numeric
    vector:
    - The type tag (one of the symbolic constants below).
    - The vector's length (counted in elements).
@@ -62,7 +63,7 @@ int scm_tc16_uvec = 0;
 #define SCM_UVEC_BASE(u)   ((void *)SCM_CELL_WORD_3(u))
 
 
-/* Symbolic constants encoding the various types of homogeneous
+/* Symbolic constants encoding the various types of uniform
    numeric vectors.  */
 #define SCM_UVEC_U8    0
 #define SCM_UVEC_S8    1
@@ -83,7 +84,11 @@ static const int uvec_sizes[12] = {
   1, 1,
   2, 2,
   4, 4,
+#if SCM_HAVE_T_INT64
   8, 8,
+#else
+  sizeof (SCM), sizeof (SCM),
+#endif
   sizeof(float), sizeof(double),
   2*sizeof(float), 2*sizeof(double)
 };
@@ -111,7 +116,7 @@ static const char *uvec_names[12] = {
 /* ================================================================ */
 
 
-/* Smob print hook for homogeneous vectors.  */
+/* Smob print hook for uniform vectors.  */
 static int
 uvec_print (SCM uvec, SCM port, scm_print_state *pstate)
 {
@@ -128,6 +133,7 @@ uvec_print (SCM uvec, SCM port, scm_print_state *pstate)
 #endif
     float *f32;
     double *f64;
+    SCM *fake_64;
   } np;
 
   size_t i = 0;
@@ -145,7 +151,10 @@ uvec_print (SCM uvec, SCM port, scm_print_state *pstate)
 #if SCM_HAVE_T_INT64
     case SCM_UVEC_U64: np.u64 = (scm_t_uint64 *) uptr; break;
     case SCM_UVEC_S64: np.s64 = (scm_t_int64 *) uptr; break;
-#endif
+#else
+    case SCM_UVEC_U64:
+    case SCM_UVEC_S64: np.fake_64 = (SCM *) uptr; break;
+#endif      
     case SCM_UVEC_F32: np.f32 = (float *) uptr; break;
     case SCM_UVEC_F64: np.f64 = (double *) uptr; break;
     case SCM_UVEC_C32: np.f32 = (float *) uptr; break;
@@ -173,6 +182,10 @@ uvec_print (SCM uvec, SCM port, scm_print_state *pstate)
 #if SCM_HAVE_T_INT64
        case SCM_UVEC_U64: scm_uintprint (*np.u64, 10, port); np.u64++; break;
        case SCM_UVEC_S64: scm_intprint (*np.s64, 10, port); np.s64++; break;
+#else
+       case SCM_UVEC_U64:
+       case SCM_UVEC_S64: scm_iprin1 (*np.fake_64, port, pstate);
+         np.fake_64++; break;
 #endif
        case SCM_UVEC_F32: scm_i_print_double (*np.f32, port); np.f32++; break;
        case SCM_UVEC_F64: scm_i_print_double (*np.f64, port); np.f64++; break;
@@ -209,6 +222,20 @@ uvec_equalp (SCM a, SCM b)
     result = SCM_BOOL_F;
   else if (SCM_UVEC_LENGTH (a) != SCM_UVEC_LENGTH (b))
     result = SCM_BOOL_F;
+#if SCM_HAVE_T_INT64 == 0
+  else if (SCM_UVEC_TYPE (a) == SCM_UVEC_U64
+          || SCM_UVEC_TYPE (a) == SCM_UVEC_S64)
+    {
+      SCM *aptr = (SCM *)SCM_UVEC_BASE (a), *bptr = (SCM *)SCM_UVEC_BASE (b);
+      size_t len = SCM_UVEC_LENGTH (a), i;
+      for (i = 0; i < len; i++)
+       if (scm_is_false (scm_num_eq_p (*aptr++, *bptr++)))
+         {
+           result = SCM_BOOL_F;
+           break;
+         }
+    }
+#endif
   else if (memcmp (SCM_UVEC_BASE (a), SCM_UVEC_BASE (b),
                   SCM_UVEC_LENGTH (a) * uvec_sizes[SCM_UVEC_TYPE(a)]) != 0)
     result = SCM_BOOL_F;
@@ -217,22 +244,12 @@ uvec_equalp (SCM a, SCM b)
   return result;
 }
 
-/* Smob free hook for homogeneous numeric vectors. */
-static size_t
-uvec_free (SCM uvec)
-{
-  int type = SCM_UVEC_TYPE (uvec);
-  scm_gc_free (SCM_UVEC_BASE (uvec),
-              SCM_UVEC_LENGTH (uvec) * uvec_sizes[type],
-              uvec_names[type]);
-  return 0;
-}
 
 /* ================================================================ */
 /* Utility procedures.                                              */
 /* ================================================================ */
 
-static SCM_C_INLINE int
+static SCM_C_INLINE_KEYWORD int
 is_uvec (int type, SCM obj)
 {
   if (SCM_IS_UVEC (obj))
@@ -245,13 +262,13 @@ is_uvec (int type, SCM obj)
   return 0;
 }
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 uvec_p (int type, SCM obj)
 {
   return scm_from_bool (is_uvec (type, obj));
 }
 
-static SCM_C_INLINE void
+static SCM_C_INLINE_KEYWORD void
 uvec_assert (int type, SCM obj)
 {
   if (!is_uvec (type, obj))
@@ -259,12 +276,12 @@ uvec_assert (int type, SCM obj)
 }
 
 static SCM
-take_uvec (int type, const void *base, size_t len)
+take_uvec (int type, void *base, size_t len)
 {
   SCM_RETURN_NEWSMOB3 (scm_tc16_uvec, type, len, (scm_t_bits) base);
 }
   
-/* Create a new, uninitialized homogeneous numeric vector of type TYPE
+/* Create a new, uninitialized uniform numeric vector of type TYPE
    with space for LEN elements.  */
 static SCM
 alloc_uvec (int type, size_t len)
@@ -273,6 +290,15 @@ alloc_uvec (int type, size_t len)
   if (len > SCM_I_SIZE_MAX / uvec_sizes[type])
     scm_out_of_range (NULL, scm_from_size_t (len));
   base = scm_gc_malloc (len * uvec_sizes[type], uvec_names[type]);
+#if SCM_HAVE_T_INT64 == 0
+  if (type == SCM_UVEC_U64 || type == SCM_UVEC_S64)
+    {
+      SCM *ptr = (SCM *)base;
+      size_t i;
+      for (i = 0; i < len; i++)
+       *ptr++ = SCM_UNSPECIFIED;
+    }
+#endif
   return take_uvec (type, base, len);
 }
 
@@ -280,7 +306,7 @@ alloc_uvec (int type, size_t len)
    so we use a big 'if' in the next two functions.
 */
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 uvec_fast_ref (int type, const void *base, size_t c_idx)
 {
   if (type == SCM_UVEC_U8)
@@ -300,6 +326,11 @@ uvec_fast_ref (int type, const void *base, size_t c_idx)
     return scm_from_uint64 (((scm_t_uint64*)base)[c_idx]);
   else if (type == SCM_UVEC_S64)
     return scm_from_int64 (((scm_t_int64*)base)[c_idx]);
+#else
+  else if (type == SCM_UVEC_U64)
+    return ((SCM *)base)[c_idx];
+  else if (type == SCM_UVEC_S64)
+    return ((SCM *)base)[c_idx];
 #endif
   else if (type == SCM_UVEC_F32)
     return scm_from_double (((float*)base)[c_idx]);
@@ -315,7 +346,23 @@ uvec_fast_ref (int type, const void *base, size_t c_idx)
     return SCM_BOOL_F;
 }
 
-static SCM_C_INLINE void
+#if SCM_HAVE_T_INT64 == 0
+static SCM scm_uint64_min, scm_uint64_max;
+static SCM scm_int64_min, scm_int64_max;
+
+static void
+assert_exact_integer_range (SCM val, SCM min, SCM max)
+{
+  if (!scm_is_integer (val)
+      || scm_is_false (scm_exact_p (val)))
+    scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
+  if (scm_is_true (scm_less_p (val, min))
+      || scm_is_true (scm_gr_p (val, max)))
+    scm_out_of_range (NULL, val);
+}
+#endif
+
+static SCM_C_INLINE_KEYWORD void
 uvec_fast_set_x (int type, void *base, size_t c_idx, SCM val)
 {
   if (type == SCM_UVEC_U8)
@@ -335,6 +382,17 @@ uvec_fast_set_x (int type, void *base, size_t c_idx, SCM val)
     (((scm_t_uint64*)base)[c_idx]) = scm_to_uint64 (val);
   else if (type == SCM_UVEC_S64)
     (((scm_t_int64*)base)[c_idx]) = scm_to_int64 (val);
+#else
+  else if (type == SCM_UVEC_U64)
+    {
+      assert_exact_integer_range (val, scm_uint64_min, scm_uint64_max);
+      ((SCM *)base)[c_idx] = val;
+    }
+  else if (type == SCM_UVEC_S64)
+    {
+      assert_exact_integer_range (val, scm_int64_min, scm_int64_max);
+      ((SCM *)base)[c_idx] = val;
+    }
 #endif
   else if (type == SCM_UVEC_F32)
     (((float*)base)[c_idx]) = scm_to_double (val);
@@ -352,7 +410,7 @@ uvec_fast_set_x (int type, void *base, size_t c_idx, SCM val)
     }
 }
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 make_uvec (int type, SCM len, SCM fill)
 {
   size_t c_len = scm_to_size_t (len);
@@ -367,7 +425,7 @@ make_uvec (int type, SCM len, SCM fill)
   return uvec;
 }
 
-static SCM_C_INLINE void *
+static SCM_C_INLINE_KEYWORD void *
 uvec_writable_elements (int type, SCM uvec, scm_t_array_handle *handle,
                        size_t *lenp, ssize_t *incp)
 {
@@ -382,7 +440,7 @@ uvec_writable_elements (int type, SCM uvec, scm_t_array_handle *handle,
   return scm_uniform_vector_writable_elements (uvec, handle, lenp, incp);
 }
 
-static SCM_C_INLINE const void *
+static SCM_C_INLINE_KEYWORD const void *
 uvec_elements (int type, SCM uvec, scm_t_array_handle *handle,
               size_t *lenp, ssize_t *incp)
 {
@@ -417,7 +475,7 @@ uvec_to_list (int type, SCM uvec)
   return res;
 }
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 uvec_length (int type, SCM uvec)
 {
   scm_t_array_handle handle;
@@ -428,7 +486,7 @@ uvec_length (int type, SCM uvec)
   return scm_from_size_t (len);
 }
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 uvec_ref (int type, SCM uvec, SCM idx)
 {
   scm_t_array_handle handle;
@@ -446,7 +504,7 @@ uvec_ref (int type, SCM uvec, SCM idx)
   return res;
 }
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 uvec_set_x (int type, SCM uvec, SCM idx, SCM val)
 {
   scm_t_array_handle handle;
@@ -463,7 +521,7 @@ uvec_set_x (int type, SCM uvec, SCM idx, SCM val)
   return SCM_UNSPECIFIED;
 }
 
-static SCM_C_INLINE SCM
+static SCM_C_INLINE_KEYWORD SCM
 list_to_uvec (int type, SCM list)
 {
   SCM uvec;
@@ -648,7 +706,7 @@ SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0,
 
 SCM_DEFINE (scm_uniform_vector_to_list, "uniform-vector->list", 1, 0, 0,
             (SCM uvec),
-           "Convert the homogeneous numeric vector @var{uvec} to a list.")
+           "Convert the uniform numeric vector @var{uvec} to a list.")
 #define FUNC_NAME s_scm_uniform_vector_to_list
 {
   return uvec_to_list (-1, uvec);
@@ -764,10 +822,10 @@ SCM_DEFINE (scm_uniform_vector_read_x, "uniform-vector-read!", 1, 3, 0,
   ssize_t inc;
   size_t cstart, cend;
   size_t remaining, off;
-  void *base;
+  char *base;
 
   if (SCM_UNBNDP (port_or_fd))
-    port_or_fd = scm_cur_inp;
+    port_or_fd = scm_current_input_port ();
   else
     SCM_ASSERT (scm_is_integer (port_or_fd)
                || (SCM_OPINPORTP (port_or_fd)),
@@ -877,12 +935,12 @@ SCM_DEFINE (scm_uniform_vector_write, "uniform-vector-write", 1, 3, 0,
   ssize_t inc;
   size_t cstart, cend;
   size_t amount, off;
-  const void *base;
+  const char *base;
 
   port_or_fd = SCM_COERCE_OUTPORT (port_or_fd);
 
   if (SCM_UNBNDP (port_or_fd))
-    port_or_fd = scm_cur_outp;
+    port_or_fd = scm_current_output_port ();
   else
     SCM_ASSERT (scm_is_integer (port_or_fd)
                || (SCM_OPOUTPORTP (port_or_fd)),
@@ -968,12 +1026,16 @@ SCM_DEFINE (scm_uniform_vector_write, "uniform-vector-write", 1, 3, 0,
 
 #define TYPE  SCM_UVEC_U64
 #define TAG   u64
+#if SCM_HAVE_T_UINT64
 #define CTYPE scm_t_uint64
+#endif
 #include "libguile/srfi-4.i.c"
 
 #define TYPE  SCM_UVEC_S64
 #define TAG   s64
+#if SCM_HAVE_T_INT64
 #define CTYPE scm_t_int64
+#endif
 #include "libguile/srfi-4.i.c"
 
 #define TYPE  SCM_UVEC_F32
@@ -1031,9 +1093,19 @@ scm_init_srfi_4 (void)
 {
   scm_tc16_uvec = scm_make_smob_type ("uvec", 0);
   scm_set_smob_equalp (scm_tc16_uvec, uvec_equalp);
-  scm_set_smob_free (scm_tc16_uvec, uvec_free);
   scm_set_smob_print (scm_tc16_uvec, uvec_print);
 
+#if SCM_HAVE_T_INT64 == 0
+  scm_uint64_min =
+    scm_permanent_object (scm_from_int (0));
+  scm_uint64_max =
+    scm_permanent_object (scm_c_read_string ("18446744073709551615"));
+  scm_int64_min =
+    scm_permanent_object (scm_c_read_string ("-9223372036854775808"));
+  scm_int64_max =
+    scm_permanent_object (scm_c_read_string ("9223372036854775807"));
+#endif
+
 #include "libguile/srfi-4.x"
 
 }