[Tinyos-devel] call-by-value vs. -pointer
David Gay
dgay42 at gmail.com
Thu Jun 12 11:19:12 PDT 2008
On Thu, Jun 12, 2008 at 11:09 AM, Philip Levis <pal at cs.stanford.edu> wrote:
>
> On Jun 12, 2008, at 7:12 AM, Jan Hauer wrote:
>
>> I'd like to hear people's opinion about passing
>> command-/event-parameters by pointer vs. by value, in a situation
>> where you would like to use call-by-value semantics, but the number of
>> arguments / their total size is big. Background: some of the 802.15.4
>> management primitives use a lot of parameters (in some situations 7
>> parameters with a total of 25 byte) and there is generally a
>> perfomance penalty when passing them by value over the stack (more
>> code size, slower execution). One alternative is that the caller
>> encapsulates the parameters in a struct and passes a pointer to it.
>> The drawback is that one needs an agreement about when/how long the
>> pointer is valid. When you have written the callee component yourself
>> you can make sure that you keep no reference after the return
>> statement, but when you cannot control the callee (e.g. signal an
>> event to some app) passing a pointer can be problematic. As long as
>> the interfaces are not used very frequently, I tend to prefer the
>> call-by-value approach even if a long list of parameters is passed -
>> what do other people think?
>
> With the exception of allocators, TinyOS semantics tend to be:
>
> For single-phase calls, ownership of a pointer is granted to the
> callee and given back to the caller when the call returns. (e.g.,
> Packet)
>
> For split-phase calls, when ownership of a pointer is granted to the
> callee and given back to the caller when the completion event is
> signaled, the done event passes the pointer. (e.g., Send)
>
> I can't think of any interfaces off the top of my head which are split-
> phase yet return ownership of the pointer when the command returns/
I was hoping that we could annotate pointers in interfaces with an
annotation denoting when ownership is transferred (ie you shouldn't
access the object after the call returns), and I think I even used it
once (grep for "PASS" in the tinyos tree). I still think it's a good
idea, but:
- even I didn't use it more than a few times
- it's not enforced (the idea was that we might in the future build a
tool to enforce it)
- it's not quite as simple as "always passed": for split-phase calls,
ownership transfer (typically) only occurs if the command returns
SUCCESS - for a formal tool to use such annotations, we need to
capture this behaviour
I think TEP 3 actually discusses a bit of this.
David
More information about the Tinyos-devel
mailing list