bytevectors have "element type" field, e.g. for generalized-vector-ref
authorAndy Wingo <wingo@pobox.com>
Sun, 19 Jul 2009 13:11:53 +0000 (15:11 +0200)
committerAndy Wingo <wingo@pobox.com>
Sun, 19 Jul 2009 13:34:59 +0000 (15:34 +0200)
commite286c973fcd63c0930d9302cc5f1a280b9b22615
tree7d8e3f4b9d765876f59fae1e4c07db779a148eb9
parentf332089ed43761440a2a8c272ee61a709b38cc24
bytevectors have "element type" field, e.g. for generalized-vector-ref

Bytevectors have a very close relationship to other forms of uniform
vectors. Often you want to view a u64vector as a series of bytes, for
writing over a socket; or to process an incoming stream using the
convenient and less error-prone s16vector-ref API rather than
bytevector-s16-native-ref.

The essential needs of the representation of a bytevector and an
s64vector are the same, so we take advantage of that and extend the
bytevector implementation to have a "native type" field, which defaults
to VU8.

This commit doesn't actually expose any user-noticeable changes,
however.

* libguile/bytevectors.h (SCM_BYTEVECTOR_ELEMENT_TYPE): New internal
  defines.
  (scm_i_make_typed_bytevector, scm_c_take_typed_bytevector): New
  internal functions.

* libguile/bytevectors.c (SCM_BYTEVECTOR_SET_ELEMENT_TYPE):
  (SCM_BYTEVECTOR_TYPE_SIZE):
  (SCM_BYTEVECTOR_TYPED_LENGTH): New internal macros.
  (make_bytevector, make_bytevector_from_buffer): Take an extra
  argument, the element type. The length argument is interpreted as
  being the number of elements, which corresponds to the number of bytes
  in the default VU8 case. Doing it this way eliminates a class of bugs
  -- e.g. a u32vector of length 3 bytes doesn't make sense. We do have
  to check for another class of bugs: overflow. The length stored on the
  bytevector itself is still the byte length, though.
  (scm_i_make_typed_bytevector):
  (scm_c_take_typed_bytevector): New internal functions.
  (scm_i_shrink_bytevector): Make sure the new size is valid for the
  bytevector's type.
  (scm_i_bytevector_generalized_set_x): Remove this function, the
  array-handle infrastructure takes care of this for us.
  (print_bytevector): Print the bytevector according to its type.
  (scm_make_bytevector, scm_bytevector_copy)
  (scm_uniform_array_to_bytevector)
  (scm_u8_list_to_bytevector, scm_bytevector_to_uint_list): Adapt to
  make_bytevector extra arg.
  (bv_handle_ref, bv_handle_set_x): Adapt to ref and set based on the
  type of the bytevector, e.g. f64 or u8.
  (bytevector_get_handle): Set the typed length of the vector, not the
  byte length.

Conflicts:

libguile/bytevectors.c
libguile/bytevectors.c
libguile/bytevectors.h