-/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009 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
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * 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 library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * 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.
*
* 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
*/
#include "libguile/validate.h"
#include "libguile/vectors.h"
-#include "libguile/unif.h"
-#include "libguile/ramap.h"
+#include "libguile/generalized-vectors.h"
+#include "libguile/arrays.h"
+#include "libguile/bitvectors.h"
+#include "libguile/bytevectors.h"
+#include "libguile/array-map.h"
#include "libguile/srfi-4.h"
#include "libguile/strings.h"
#include "libguile/srfi-13.h"
#include "libguile/dynwind.h"
#include "libguile/deprecation.h"
+#include "libguile/boehm-gc.h"
+
+
\f
#define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8)
scm_vector_elements (SCM vec, scm_t_array_handle *h,
size_t *lenp, ssize_t *incp)
{
+ if (SCM_I_WVECTP (vec))
+ /* FIXME: We should check each (weak) element of the vector for NULL and
+ convert it to SCM_BOOL_F. */
+ abort ();
+
scm_generalized_vector_get_handle (vec, h);
if (lenp)
{
scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
size_t *lenp, ssize_t *incp)
{
+ if (SCM_I_WVECTP (vec))
+ /* FIXME: We should check each (weak) element of the vector for NULL and
+ convert it to SCM_BOOL_F. */
+ abort ();
+
scm_generalized_vector_get_handle (vec, h);
if (lenp)
{
{
if (SCM_I_IS_VECTOR (v))
{
+ register SCM elt;
+
if (k >= SCM_I_VECTOR_LENGTH (v))
- scm_out_of_range (NULL, scm_from_size_t (k));
- return (SCM_I_VECTOR_ELTS(v))[k];
+ scm_out_of_range (NULL, scm_from_size_t (k));
+ elt = (SCM_I_VECTOR_ELTS(v))[k];
+
+ if ((elt == SCM_PACK (NULL)) && SCM_I_WVECTP (v))
+ /* ELT was a weak pointer and got nullified by the GC. */
+ return SCM_BOOL_F;
+
+ return elt;
}
else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
{
SCM vv = SCM_I_ARRAY_V (v);
if (SCM_I_IS_VECTOR (vv))
{
+ register SCM elt;
+
if (k >= dim->ubnd - dim->lbnd + 1)
scm_out_of_range (NULL, scm_from_size_t (k));
k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
- return (SCM_I_VECTOR_ELTS (vv))[k];
+ elt = (SCM_I_VECTOR_ELTS (vv))[k];
+
+ if ((elt == SCM_PACK (NULL)) && (SCM_I_WVECTP (vv)))
+ /* ELT was a weak pointer and got nullified by the GC. */
+ return SCM_BOOL_F;
+
+ return elt;
}
scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
}
if (k >= SCM_I_VECTOR_LENGTH (v))
scm_out_of_range (NULL, scm_from_size_t (k));
(SCM_I_VECTOR_WELTS(v))[k] = obj;
+ if (SCM_I_WVECTP (v))
+ {
+ /* Make it a weak pointer. */
+ GC_PTR link = (GC_PTR) & ((SCM_I_VECTOR_WELTS (v))[k]);
+ SCM_I_REGISTER_DISAPPEARING_LINK (link, obj);
+ }
}
else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1)
{
scm_out_of_range (NULL, scm_from_size_t (k));
k = SCM_I_ARRAY_BASE (v) + k*dim->inc;
(SCM_I_VECTOR_WELTS (vv))[k] = obj;
+
+ if (SCM_I_WVECTP (vv))
+ {
+ /* Make it a weak pointer. */
+ GC_PTR link = (GC_PTR) & ((SCM_I_VECTOR_WELTS (vv))[k]);
+ SCM_I_REGISTER_DISAPPEARING_LINK (link, obj);
+ }
}
else
scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector");
else
base = NULL;
- v = scm_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base);
+ v = scm_immutable_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base);
scm_remember_upto_here_1 (fill);
return v;
"vector");
}
-/* Allocate memory for a weak vector on behalf of the caller. The allocated
- * vector will be of the given weak vector subtype. It will contain size
- * elements which are initialized with the 'fill' object, or, if 'fill' is
- * undefined, with an unspecified object.
- */
-SCM
-scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill)
+\f
+/* Weak vectors. */
+
+
+/* Initialize RET as a weak vector of type TYPE of SIZE elements pointed to
+ by BASE. */
+#define MAKE_WEAK_VECTOR(_ret, _type, _size, _base) \
+ (_ret) = scm_double_cell ((_size << 8) | scm_tc7_wvect, \
+ (scm_t_bits) (_base), \
+ (_type), \
+ SCM_UNPACK (SCM_EOL));
+
+
+/* Allocate memory for the elements of a weak vector on behalf of the
+ caller. */
+static SCM *
+allocate_weak_vector (scm_t_bits type, size_t c_size)
{
- size_t c_size;
SCM *base;
- SCM v;
+
+ if (c_size > 0)
+ /* The base itself should not be scanned for pointers otherwise those
+ pointers will always be reachable. */
+ base = scm_gc_malloc_pointerless (c_size * sizeof (SCM), "weak vector");
+ else
+ base = NULL;
+
+ return base;
+}
+
+/* Return a new weak vector. The allocated vector will be of the given weak
+ vector subtype. It will contain SIZE elements which are initialized with
+ the FILL object, or, if FILL is undefined, with an unspecified object. */
+SCM
+scm_i_make_weak_vector (scm_t_bits type, SCM size, SCM fill)
+{
+ SCM wv, *base;
+ size_t c_size, j;
+
+ if (SCM_UNBNDP (fill))
+ fill = SCM_UNSPECIFIED;
c_size = scm_to_unsigned_integer (size, 0, VECTOR_MAX_LENGTH);
+ base = allocate_weak_vector (type, c_size);
- if (c_size > 0)
+ for (j = 0; j != c_size; ++j)
+ base[j] = fill;
+
+ MAKE_WEAK_VECTOR (wv, type, c_size, base);
+
+ return wv;
+}
+
+/* Return a new weak vector with type TYPE and whose content are taken from
+ list LST. */
+SCM
+scm_i_make_weak_vector_from_list (scm_t_bits type, SCM lst)
+{
+ SCM wv, *base, *elt;
+ long c_size;
+
+ c_size = scm_ilength (lst);
+ SCM_ASSERT (c_size >= 0, lst, SCM_ARG2, "scm_i_make_weak_vector_from_list");
+
+ base = allocate_weak_vector (type, (size_t)c_size);
+ for (elt = base;
+ scm_is_pair (lst);
+ lst = SCM_CDR (lst), elt++)
{
- size_t j;
-
- if (SCM_UNBNDP (fill))
- fill = SCM_UNSPECIFIED;
-
- base = scm_gc_malloc (c_size * sizeof (SCM), "weak vector");
- for (j = 0; j != c_size; ++j)
- base[j] = fill;
+ *elt = SCM_CAR (lst);
}
- else
- base = NULL;
- v = scm_double_cell ((c_size << 8) | scm_tc7_wvect,
- (scm_t_bits) base,
- type,
- SCM_UNPACK (SCM_EOL));
- scm_remember_upto_here_1 (fill);
+ MAKE_WEAK_VECTOR (wv, type, (size_t)c_size, base);
- return v;
+ return wv;
}
+
+\f
SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
(SCM v),
"Return a newly allocated list composed of the elements of @var{v}.\n"
}
#undef FUNC_NAME
-
-/* Generalized vectors. */
-
-int
-scm_is_generalized_vector (SCM obj)
-{
- return (scm_is_vector (obj)
- || scm_is_string (obj)
- || scm_is_bitvector (obj)
- || scm_is_uniform_vector (obj));
-}
-
-SCM_DEFINE (scm_generalized_vector_p, "generalized-vector?", 1, 0, 0,
- (SCM obj),
- "Return @code{#t} if @var{obj} is a vector, string,\n"
- "bitvector, or uniform numeric vector.")
-#define FUNC_NAME s_scm_generalized_vector_p
-{
- return scm_from_bool (scm_is_generalized_vector (obj));
-}
-#undef FUNC_NAME
-
-void
-scm_generalized_vector_get_handle (SCM vec, scm_t_array_handle *h)
-{
- scm_array_get_handle (vec, h);
- if (scm_array_handle_rank (h) != 1)
- scm_wrong_type_arg_msg (NULL, 0, vec, "vector");
-}
-
-size_t
-scm_c_generalized_vector_length (SCM v)
-{
- if (scm_is_vector (v))
- return scm_c_vector_length (v);
- else if (scm_is_string (v))
- return scm_c_string_length (v);
- else if (scm_is_bitvector (v))
- return scm_c_bitvector_length (v);
- else if (scm_is_uniform_vector (v))
- return scm_c_uniform_vector_length (v);
- else
- scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
-}
-
-SCM_DEFINE (scm_generalized_vector_length, "generalized-vector-length", 1, 0, 0,
- (SCM v),
- "Return the length of the generalized vector @var{v}.")
-#define FUNC_NAME s_scm_generalized_vector_length
-{
- return scm_from_size_t (scm_c_generalized_vector_length (v));
-}
-#undef FUNC_NAME
-
-SCM
-scm_c_generalized_vector_ref (SCM v, size_t idx)
-{
- if (scm_is_vector (v))
- return scm_c_vector_ref (v, idx);
- else if (scm_is_string (v))
- return scm_c_string_ref (v, idx);
- else if (scm_is_bitvector (v))
- return scm_c_bitvector_ref (v, idx);
- else if (scm_is_uniform_vector (v))
- return scm_c_uniform_vector_ref (v, idx);
- else
- scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
-}
-
-SCM_DEFINE (scm_generalized_vector_ref, "generalized-vector-ref", 2, 0, 0,
- (SCM v, SCM idx),
- "Return the element at index @var{idx} of the\n"
- "generalized vector @var{v}.")
-#define FUNC_NAME s_scm_generalized_vector_ref
+\f
+static SCM
+vector_handle_ref (scm_t_array_handle *h, size_t idx)
{
- return scm_c_generalized_vector_ref (v, scm_to_size_t (idx));
+ if (idx > h->dims[0].ubnd)
+ scm_out_of_range ("vector-handle-ref", scm_from_size_t (idx));
+ return ((SCM*)h->elements)[idx];
}
-#undef FUNC_NAME
-void
-scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
+static void
+vector_handle_set (scm_t_array_handle *h, size_t idx, SCM val)
{
- if (scm_is_vector (v))
- scm_c_vector_set_x (v, idx, val);
- else if (scm_is_string (v))
- scm_c_string_set_x (v, idx, val);
- else if (scm_is_bitvector (v))
- scm_c_bitvector_set_x (v, idx, val);
- else if (scm_is_uniform_vector (v))
- scm_c_uniform_vector_set_x (v, idx, val);
- else
- scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
+ if (idx > h->dims[0].ubnd)
+ scm_out_of_range ("vector-handle-set!", scm_from_size_t (idx));
+ ((SCM*)h->writable_elements)[idx] = val;
}
-SCM_DEFINE (scm_generalized_vector_set_x, "generalized-vector-set!", 3, 0, 0,
- (SCM v, SCM idx, SCM val),
- "Set the element at index @var{idx} of the\n"
- "generalized vector @var{v} to @var{val}.")
-#define FUNC_NAME s_scm_generalized_vector_set_x
+static void
+vector_get_handle (SCM v, scm_t_array_handle *h)
{
- scm_c_generalized_vector_set_x (v, scm_to_size_t (idx), val);
- return SCM_UNSPECIFIED;
+ h->array = v;
+ h->ndims = 1;
+ h->dims = &h->dim0;
+ h->dim0.lbnd = 0;
+ h->dim0.ubnd = SCM_I_VECTOR_LENGTH (v) - 1;
+ h->dim0.inc = 1;
+ h->element_type = SCM_ARRAY_ELEMENT_TYPE_SCM;
+ h->elements = h->writable_elements = SCM_I_VECTOR_WELTS (v);
}
-#undef FUNC_NAME
-SCM_DEFINE (scm_generalized_vector_to_list, "generalized-vector->list", 1, 0, 0,
- (SCM v),
- "Return a new list whose elements are the elements of the\n"
- "generalized vector @var{v}.")
-#define FUNC_NAME s_scm_generalized_vector_to_list
-{
- if (scm_is_vector (v))
- return scm_vector_to_list (v);
- else if (scm_is_string (v))
- return scm_string_to_list (v);
- else if (scm_is_bitvector (v))
- return scm_bitvector_to_list (v);
- else if (scm_is_uniform_vector (v))
- return scm_uniform_vector_to_list (v);
- else
- scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector");
-}
-#undef FUNC_NAME
+SCM_ARRAY_IMPLEMENTATION (scm_tc7_vector, 0x7f & ~2,
+ vector_handle_ref, vector_handle_set,
+ vector_get_handle);
+SCM_ARRAY_IMPLEMENTATION (scm_tc7_wvect, 0x7f & ~2,
+ vector_handle_ref, vector_handle_set,
+ vector_get_handle);
+SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_SCM, scm_make_vector);
void