> On Sun, 6 Jul 2003, Matthew Wilcox wrote:
> 
> > Why on earth does it return the value of its argument?
> 
> Maybe for the same reason 'strcpy' returns 'dest'. It allows you to use
> the function in a function parameter :
Another possible benefit (although I'm not sure we should care)
is that if the return variable is the same as the first argument, 
the compiler can save an instruction or two on at least some archs.
Simple example:
char *tst(char *p, int i)
{
  return p;
}
void tst2(char *p, int i)
{
  *p = i;
  p = tst(p,i);
  p[1]=i;
}
void tst3(char *p, int i)
{
  *p = i;
  tst(p,i);
  p[1]=i;
}
i386 ts2 saves 3 instructions compared to tst3
tst2:
        pushl %ebp
        movl %esp,%ebp
        subl $20,%esp
        pushl %ebx
        movl 8(%ebp),%eax
        movl 12(%ebp),%ebx
        movb %bl,(%eax)
        addl $-8,%esp
        pushl %ebx
        pushl %eax
        call tst
        movb %bl,1(%eax)
        movl -24(%ebp),%ebx
        leave
        ret
.Lfe2:
        .size    tst2,.Lfe2-tst2
        .align 4
.globl tst3
        .type    tst3,@function
tst3:
        pushl %ebp
        movl %esp,%ebp
        subl $16,%esp
        pushl %esi
        pushl %ebx
        movl 8(%ebp),%esi
        movl 12(%ebp),%ebx
        movb %bl,(%esi)
        addl $-8,%esp
        pushl %ebx
        pushl %esi
        call tst
        movb %bl,1(%esi)
        leal -24(%ebp),%esp
        popl %ebx
        popl %esi
        leave
        ret
.Lfe3:
On CRIS you save one register on stack instead of two
tst2:
        Push $srp
        subq 4,$sp
        movem $r0,[$sp]
        move.d $r10,$r9
        move.d $r11,$r0
        move.b $r11,[$r9]
        Jsr tst
        move.b $r0,[$r10+1]
        movem [$sp+],$r0
        Jump [$sp+]
.Lfe2:
        .size   tst2,.Lfe2-tst2
        .align 1
        .global tst3
        .type   tst3,@function
tst3:
        Push $srp
        subq 8,$sp
        movem $r1,[$sp]
        move.d $r10,$r0
        move.d $r11,$r1
        move.b $r11,[$r0+]
        Jsr tst
        move.b $r1,[$r0]
        movem [$sp+],$r1
        Jump [$sp+]
.Lfe3:
/Johan
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/