guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Suggestion for change in the new array interface


From: Mikael Djurfeldt
Subject: Suggestion for change in the new array interface
Date: Sat, 19 Feb 2005 22:32:59 +0100

When programming with the new array interface I've noticed an
inconvenience that should be easy to fix:

When checking the arguments of a procedure there's a few things one
normally checks apart from the type. It's common that a procedure
takes an array of a specific rank, and it's not unusual that only
arrays of a specific dimension qualifies as an argument.

Since both rank and dimensionality are extracted from the array handle
this handle needs to be allocated *before* argument checking is
complete. This means that we have to free the handle on an error exit.
So, the code for argument checking only becomes:

  SCM_ASSERT (scm_is_typed_array (a, <array type>), a, <pos>, <function name>);
  scm_array_get_handle (a, &h);
  dims = scm_array_handle_dims (&h);
  if (scm_array_handle_rank (&h) != <rank>  || dims[0].ubnd -
dims[0].lbnd + 1 != <dim1> || ...)
  {
    scm_array_handle_release (&h);
    scm_wrong_type_arg (<function nam>, <pos>, a);
  }

Since it seems unreasonable to allow future code to modify the
dimension of an array, I don't see any reason why we couldn't ask for
the rank and dimensions fo an array without allocating the handle. The
above code could then instead look like this:

  SCM_ASSERT (scm_is_typed_array (a, <array type>), a, <pos>, <function name>);
  dims = scm_array_dims (a);
  SCM_ASSERT ( scm_array_rank (a) != <rank>  || dims[0].ubnd -
dims[0].lbnd + 1 != <dim1> || ...,
              a, <pos>, <function name>);
  scm_array_get_handle (a, &h);

This all of course depends on the first sentence in the description of
how to handle allocation of handles in the reference manual:

 "You must take care to always unreserve an array after reserving it,
also in the presence of non-local exits.  To simplify this, reserving
and unreserving work like a frame (*note Frames::): a call to
`scm_array_get_handle' can be thought of as beginning a frame and
`scm_array_handle_release' as ending it.  When a non-local exit happens
between these two calls, the array is implicitely unreserved."

The part after the first sentence indicates that I don't have to call
scm_array_handle_release before throwing the error. Is that so? In
that case this suggestion of change is unnecessary.

Comments?

M




reply via email to

[Prev in Thread] Current Thread [Next in Thread]