Hot!Align string literals

Author
Koen van Vliet
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2018/05/14 04:15:44
  • Location: 0
  • Status: offline
2018/05/16 00:39:21 (permalink)
0

Align string literals

I have a function in a third party library
void MqWrite(const char *msg, const int msg_length)

where msg must be aligned to 2 bytes.
 
String literals are aligned to 1 byte by default. I would like all string literals to be aligned to 2 bytes, so I do not need to memcpy to an aligned buffer. Is there a compiler setting for this?
#1

6 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 1885
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Align string literals 2018/05/16 10:21:53 (permalink)
    0
    Not sure about the alignment...
     
    As a "last resort" you could create a union data type ("WordByteUnion_t") where one element is a (u)int16_t. This way you would get the alignment required - - - exempt maybe when directly passing constant strings to the function.
    MqWrite("TEST", 4);                        // Uncontrolled alignment
    MqWrite((WordByteUnion_t*)"TEST", 4);      // Not sure about the outcome  :)


    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    Mysil
    Super Member
    • Total Posts : 2758
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Align string literals 2018/05/16 10:55:24 (permalink)
    +1 (1)
    Hi,
    You may use:
    char   msg[] __attribute__ ((aligned (2))) = {"This is a test \r\n"};
    See XC16 User's Guide chapter 8.11 Variable attributes.
     
       Mysil
    #3
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2596
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Align string literals 2018/05/16 11:08:31 (permalink)
    +1 (1)
    This is one way:
    const char  *Gort1951 __attribute__(( aligned(2) )) ="Klatuu Barada Nikto";

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2596
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Align string literals 2018/05/16 11:16:42 (permalink)
    0
    Or:
    const char const *Gort1951 __attribute__(( aligned(2) )) ="Klatuu Barada Nikto";
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    jtemples
    Super Member
    • Total Posts : 10797
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Align string literals 2018/05/16 11:17:29 (permalink)
    0
    const char  *Gort1951 __attribute__(( aligned(2) ))

     
    That aligns the pointer, not what it points to.
    #6
    Koen van Vliet
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2018/05/14 04:15:44
    • Location: 0
    • Status: offline
    Re: Align string literals 2018/05/17 01:37:40 (permalink)
    0
    Thank you for all the suggestions.

    du00000001

    Not sure about the alignment...

     

    As a "last resort" you could create a union data type ("WordByteUnion_t") where one element is a (u)int16_t. This way you would get the alignment required - - - exempt maybe when directly passing constant strings to the function.

    MqWrite("TEST", 4);                        // Uncontrolled alignment
    MqWrite((WordByteUnion_t*)"TEST", 4);      // Not sure about the outcome  :)


    I have seen this trick before, I have tried it and it works. stackoverflow.com/a/36531529

     

    Calling MqWrite with a pointer that is not aligned results in a soft reset. In case anyone forgets the macro the fault can be handled safely using memcpy to an aligned buffer.


    void SafeMqWrite(const char* str, const int len) {
        if ((uintptr_t )str & 1) {
            char *aligned_str;
            aligned_str = malloc(len);
            if (aligned_str) {
                    memcpy(aligned_str, str, len);
                    MqWrite(aligned_str, len);
            }
        } else {
            MqWrite(str, len);
        }
    }


     
    post edited by Koen van Vliet - 2018/05/18 02:57:48
    #7
    Jump to:
    © 2018 APG vNext Commercial Version 4.5