Re: [Vala] how to *not* automagically copy arrays passed to methods?



On 12/20/11 13:11, Matto Marjanovic wrote:
On 12/20/11 11:49, Evan Nemerson wrote:
On Tue, 2011-12-20 at 00:39 -0800, Matto Marjanovic wrote:
The subject line explains what I am trying to do. In plain old C,
I would simply use plain old libgio:

GInputStream* s = ...;
guint8 buffer[4096];
GError* error = NULL;
...
g_input_stream_read(s, buffer + M, N, NULL,&error);

How do I do this in Vala?
...
Use array slicing. Something like stream.read (buffer[M:M + N]);

Ah-ha! Thank you.

The docs make it sound like slicing always duplicates the (slice of the)
array --- but I now see that doesn't happen when the slicing happens in
the method call. (Before writing my email, I had tried assigning a slice
to a temp local array variable, and that invoked a dup in the generate
C code....)

Ok, another bonus question (see revised subject) --- and I think this
issue was the root of the misfire with my earlier experiments with slices:

How does one tell valac to *not* copy arrays that are passed to methods?

For example:

   public void foo(uint8[] x) {
     ...
     x[0] = 7;
     ...
   }

The generated C code for the method duplicates the array x before executing
the body of the method; hence the common case of a method which modifies a
buffer, returning with side-effects, doesn't work.

Who has *ever* wanted to pass an array by value?

Even worse, this behavior is inconsistent with wrapped methods --- e.g.,
InputStream.read() happens to work because vala doesn't wrap an array-copying
wrapper around the underlying C/GIO function.

Is there some magic keyword to make the copying stop?

-m




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