unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* struct-copy func
@ 2007-08-17  0:46 Kevin Ryde
  2007-08-20  8:35 ` Ludovic Courtès
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Ryde @ 2007-08-17  0:46 UTC (permalink / raw)
  To: guile-devel

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


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: struct-copy func
  2007-08-17  0:46 struct-copy func Kevin Ryde
@ 2007-08-20  8:35 ` Ludovic Courtès
  2007-08-20 23:27   ` Kevin Ryde
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Courtès @ 2007-08-20  8:35 UTC (permalink / raw)
  To: Kevin Ryde; +Cc: guile-devel

Hi,

Good idea.

Kevin Ryde <user42@zip.com.au> writes:

>   memcpy (new_st_data, st_data, n_words * sizeof(scm_t_bits));

That's not possible: `struct-copy' must check fields permission and type
before copying.  For instance, `s' fields must be updated, and a struct
with `o' fields cannot be copied I think.

To that end, we should probably factorize code that deals with struct
layout, field permission checks and the likes.

Thanks,
Ludovic.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: struct-copy func
  2007-08-20  8:35 ` Ludovic Courtès
@ 2007-08-20 23:27   ` Kevin Ryde
  2007-08-21  7:49     ` Ludovic Courtès
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Ryde @ 2007-08-20 23:27 UTC (permalink / raw)
  To: guile-devel

ludovic.courtes@laas.fr (Ludovic Courtès) writes:
>
> `struct-copy' must check fields permission

I didn't want to do that.

> `s' fields must be updated

Ah yes, I forgot that.

> `o' fields cannot be copied I think.

I had in mind just bitwise copying them.  But I guess that slightly
defeats the purpose of such fields, and could conceivably be a bit evil.

I might rethink it as just a record-copy, since plain records have none
of those questions.  But that'd need at least `record?' down in C code
for a type check, so it might have to wait until most of the record
stuff is in C.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: struct-copy func
  2007-08-20 23:27   ` Kevin Ryde
@ 2007-08-21  7:49     ` Ludovic Courtès
  0 siblings, 0 replies; 4+ messages in thread
From: Ludovic Courtès @ 2007-08-21  7:49 UTC (permalink / raw)
  To: Kevin Ryde; +Cc: guile-devel

Hi,

Kevin Ryde <user42@zip.com.au> writes:

> ludovic.courtes@laas.fr (Ludovic Courtès) writes:
>>
>> `struct-copy' must check fields permission
>
> I didn't want to do that.

I think it has to be done, otherwise that'd just make field permissions
useless.

>> `o' fields cannot be copied I think.
>
> I had in mind just bitwise copying them.  But I guess that slightly
> defeats the purpose of such fields, and could conceivably be a bit evil.

Indeed.  I believe it should be an error to try to copy a struct with
such fields.

> I might rethink it as just a record-copy, since plain records have none
> of those questions.  But that'd need at least `record?' down in C code
> for a type check, so it might have to wait until most of the record
> stuff is in C.

Better leave records as Scheme code IMO.  If you add the above rules to
`struct-copy', then you will be able to
`(define record-copy struct-copy)' and it will actually work because
records have no opaque fields.

Thanks,
Ludovic.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-21  7:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-17  0:46 struct-copy func Kevin Ryde
2007-08-20  8:35 ` Ludovic Courtès
2007-08-20 23:27   ` Kevin Ryde
2007-08-21  7:49     ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).