guile-devel
[Top][All Lists]
Advanced

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

struct-copy func


From: Kevin Ryde
Subject: struct-copy func
Date: Fri, 17 Aug 2007 10:46:06 +1000
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/22.1 (gnu/linux)

This is an idea I had for copying structures, mainly to make a
`record-copy' function (just an alias of struct-copy).  I've found it
pretty useful.

You can do a record copy with `record-constructor' and a map over
`record-accessor' for each field, but of course a block copy is heaps
more efficient.


SCM_DEFINE_PUBLIC (struct_copy, "struct-copy", 1, 0, 0,
            (SCM st),
            "Return a copy of structure @var{st}.  This is a shallow copy,\n"
            "there's no recursive copying of the objects in the fields.")
#define FUNC_NAME s_struct_copy
{
  scm_t_bits *vtable_data, *st_data, *new_st_data;
  size_t n_words;

  SCM_VALIDATE_STRUCT (SCM_ARG1, st);

  vtable_data = SCM_STRUCT_VTABLE_DATA (st);

  /* Only standard structs handled here, not goops entity or light forms.
     Believe that's enough for ordinary public uses of structures as created
     from `make-struct', `make-vtable', `make-vtable-vtable'.  */
  if (vtable_data[scm_struct_i_flags]
      & (SCM_STRUCTF_ENTITY | SCM_STRUCTF_LIGHT))
    SCM_WRONG_TYPE_ARG (SCM_ARG1, st);

  st_data = SCM_STRUCT_DATA (st);
  n_words = st_data[scm_struct_i_n_words];
  new_st_data = scm_alloc_struct (n_words,
                                  scm_struct_n_extra_words,
                                  "struct");

  memcpy (new_st_data, st_data, n_words * sizeof(scm_t_bits));
  scm_remember_upto_here_1 (st);

  return scm_double_cell ((scm_t_bits) vtable_data + scm_tc3_struct,
                          (scm_t_bits) new_st_data, 0, 0);
}
#undef FUNC_NAME




reply via email to

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