diff options
author | Ludovic Courtès <ludo@gnu.org> | 2009-06-22 00:51:08 +0200 |
---|---|---|
committer | Ludovic Courtès <ludo@gnu.org> | 2009-06-22 01:09:39 +0200 |
commit | 438974d08dcb96a01fe62ea9b0446b8420e703c4 (patch) | |
tree | 94b75ea2cf4c9d1b4c6fda2dbecaac1b49f9ecf6 /libguile/unif.c | |
parent | 404bb5f87b66709206507acdf7b899101185a7a0 (diff) | |
download | guile-438974d08dcb96a01fe62ea9b0446b8420e703c4.tar.gz |
Make bytevectors accessible using the generalized-vector API.
As a side effect, this allows compilation of literal bytevectors
("#vu8(...)"), which gets done by the generic array handling
of the GLIL->assembly compiler.
* doc/ref/api-compound.texi (Generalized Vectors): Mention bytevectors.
(Arrays, Array Syntax): Likewise.
* doc/ref/api-data.texi (Bytevectors as Generalized Vectors): New node.
* libguile/bytevectors.c (scm_i_bytevector_generalized_set_x): New.
* libguile/bytevectors.h (scm_i_bytevector_generalized_set_x): New
declaration.
* libguile/srfi-4.c (scm_i_generalized_vector_type,
scm_array_handle_uniform_element_size,
scm_array_handle_uniform_writable_elements): Add support for
bytevectors.
* libguile/unif.c (type_creator_table): Add `vu8'.
(bytevector_ref, bytevector_set): New functions.
(memoize_ref, memoize_set): Add support for bytevectors.
* libguile/vectors.c (scm_is_generalized_vector,
scm_c_generalized_vector_length, scm_c_generalized_vector_ref,
scm_c_generalized_vector_set_x): Add support for bytevectors.
* test-suite/tests/bytevectors.test ("Generalized Vectors"): New test
set.
Diffstat (limited to 'libguile/unif.c')
-rw-r--r-- | libguile/unif.c | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/libguile/unif.c b/libguile/unif.c index d393e8a1a..84b532347 100644 --- a/libguile/unif.c +++ b/libguile/unif.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 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 @@ -47,6 +47,7 @@ #include "libguile/srfi-13.h" #include "libguile/srfi-4.h" #include "libguile/vectors.h" +#include "libguile/bytevectors.h" #include "libguile/list.h" #include "libguile/deprecation.h" #include "libguile/dynwind.h" @@ -109,6 +110,7 @@ struct { { "f64", SCM_UNSPECIFIED, scm_make_f64vector }, { "c32", SCM_UNSPECIFIED, scm_make_c32vector }, { "c64", SCM_UNSPECIFIED, scm_make_c64vector }, + { "vu8", SCM_UNSPECIFIED, scm_make_bytevector }, { NULL } }; @@ -314,6 +316,12 @@ bitvector_ref (scm_t_array_handle *h, ssize_t pos) } static SCM +bytevector_ref (scm_t_array_handle *h, ssize_t pos) +{ + return scm_from_uint8 (((scm_t_uint8 *) h->elements)[pos]); +} + +static SCM memoize_ref (scm_t_array_handle *h, ssize_t pos) { SCM v = h->array; @@ -346,6 +354,11 @@ memoize_ref (scm_t_array_handle *h, ssize_t pos) h->elements = scm_array_handle_bit_elements (h); h->ref = bitvector_ref; } + else if (scm_is_bytevector (v)) + { + h->elements = scm_array_handle_uniform_elements (h); + h->ref = bytevector_ref; + } else scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (h->array)); @@ -387,6 +400,17 @@ bitvector_set (scm_t_array_handle *h, ssize_t pos, SCM val) } static void +bytevector_set (scm_t_array_handle *h, ssize_t pos, SCM val) +{ + scm_t_uint8 c_value; + scm_t_uint8 *elements; + + c_value = scm_to_uint8 (val); + elements = (scm_t_uint8 *) h->elements; + elements[pos] = (scm_t_uint8) c_value; +} + +static void memoize_set (scm_t_array_handle *h, ssize_t pos, SCM val) { SCM v = h->array; @@ -420,6 +444,11 @@ memoize_set (scm_t_array_handle *h, ssize_t pos, SCM val) h->writable_elements = scm_array_handle_bit_writable_elements (h); h->set = bitvector_set; } + else if (scm_is_bytevector (v)) + { + h->elements = scm_array_handle_uniform_writable_elements (h); + h->set = bytevector_set; + } else scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (h->array)); |