avr-libc-dev
[Top][All Lists]
Advanced

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

[avr-libc-dev] malloc bug: casting woes


From: Theodore A. Roth
Subject: [avr-libc-dev] malloc bug: casting woes
Date: Fri, 27 Sep 2002 14:45:41 -0700 (PDT)

Regarding this change:

2002-09-09  Joerg Wunsch <address@hidden>

        * libc/stdlib/malloc.c: use _SFR_IO16() around the SP address.

I just spend the better part of the week figuring out that it broke
malloc. :-\

It turns out that the _SFR_IO16() macro includes all the proper casts, so
the extra casts were really hosing the compiler.

>From this code:

    199                 cp = STACK_POINTER() - __malloc_margin;

the compiler generated this with the broken (extra casts) macro [note
that __malloc_margin is at 0x0129:0x012a for my app]:

        if (cp == 0)
    13ea:       20 97           sbiw    r28, 0x00       ; 0
    13ec:       99 f0           breq    .+38            ; 0x1414
                cp = STACK_POINTER() - __malloc_margin;
  <snip>
    1414:       ad b7           in      r26, 0x3d       ; 61
    1416:       be b7           in      r27, 0x3e       ; 62
    1418:       cd 91           ld      r28, X+
    141a:       dc 91           ld      r29, X
    141c:       a0 91 29 01     lds     r26, 0x0129
    1420:       b0 91 2a 01     lds     r27, 0x012A
    1424:       ca 1b           sub     r28, r26
    1426:       db 0b           sbc     r29, r27
    1428:       e2 cf           rjmp    .-60            ; 0x13ee

I leave it as an exercise to the read as to why this is wrong. ;-)

Here's the code generated from the fixed macro:

        if (cp == 0)
    13e2:       10 97           sbiw    r26, 0x00       ; 0
    13e4:       99 f0           breq    .+38            ; 0x140c
                cp = STACK_POINTER() - __malloc_margin;
  <snip>
    140c:       ad b7           in      r26, 0x3d       ; 61
    140e:       be b7           in      r27, 0x3e       ; 62
    1410:       e0 91 29 01     lds     r30, 0x0129
    1414:       f0 91 2a 01     lds     r31, 0x012A
    1418:       ae 1b           sub     r26, r30
    141a:       bf 0b           sbc     r27, r31
    141c:       e4 cf           rjmp    .-56            ; 0x13e6

Much nicer, no?

I've committed this as obvious (after seeing the above proof).

Ted Roth

Attachment: avr-libc-malloc-fix.diff
Description: Text document


reply via email to

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