Shop OBEX P1 Docs P2 Docs Learn Events
Alignment — Parallax Forums

Alignment

rokickirokicki Posts: 1,000
edited 2006-12-08 03:56 in Propeller 1
How do I enforce alignment? I want to allocate a byte buffer in spin (and use that byte buffer
in spin for a lot of things), but that byte buffer must be longword aligned.

How can I guarantee this? If it is the first var in a object, will that guarantee the alignment?
Or is there some other way?

I would just declare it as a long array, but I use it as an array in Spin all over the place and it's
very messy to do all the indexing myself.

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-12-08 01:58
    The way variables are allocated is all declared longs are grouped together and are allocated in order of appearance, then all words are grouped together and declared in order of appearance, finally all bytes are grouped and declared in order of appearance. So if you dont have any word sized variable and you declare your array before all other byte variables, your array will be long aligned because it will come out of declaring the longs in long alignment. If you use word variables you must make sure you have an even number of variables by using a dummy variable to pad the alignment out to long alignment if nessesary so that when your byte array is allocated as the first of your byte variables it will be long aligned as well.

    This alignment is done within the scope of each object, you do not need to do your word variable counting for any other objects.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • rokickirokicki Posts: 1,000
    edited 2006-12-08 03:23
    Thanks! I can manage that. That's exactly the detail I needed.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-12-08 03:56
    No problem, glad to help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.