sprout42/StarStruct

Allow for alignment / packing settings

tjdevries opened this issue · 8 comments

Currently, I don't know of any way to automagically get the same padding as the C compiler, but maybe we could add some way for it to know at least to align up to 4 bytes, or something like that.

That seems like it could be tricky considering how different C compilers can pack completely differently... but it might be worth thinking about.

Should alignment propagate from the top-level message object to all elements? like the Big/Little endian flag?

I was thinking, at least initially, we could do something similar to:

m = Message([
   ....
], Mode, alignment=4)

maybe later that could be more complex, but that would at least let you add some auto padding.

Oh, and another thing is, for messages where we have

m = Message([
    ...
    this: {
        enum.value: MessageType
        ....
    },
])

Maybe we could add some of alignment / padding for those, to automatically put padding behind smaller objects.

That might be a different issue.

I like it, it's a simple parameter for the Message object. It should be passed recursively to every individual field so that:

  1. each field can pad itself out to the specified alignment on packing, and make sure to consume the necessary number of bytes when unpacking.
  2. each field that contains Message objects can pass the alignment on to each of those elements.

And if we find in the future that we need to handle alignment differently, then we can always change it. But it keeps it simple for now.

you know. this is something that should be standard. Perhaps the Element class should have pack/unpack functions that are called by the other element objects after they are done, and the high-level element class will handle global things like managing alignment?

If necessary then the individual elements can not call the parent pack/unpack if they have to manage data in a different way.

That seems like a good idea.

It might be difficult to enforce alignment for an item that has multiple items inside of itself (like elementvariable).

If alignment is 4, and they have a length 9 element, would we expect it to pack each one next to each other, or 9 data, 3 pad, 9 data, 3 pad. Not sure how we would handle it when that is the case. I do prefer if more happens standard in the Element class.

I think that works ok in this case, because then the alignment is passed to the Message object referenced by the elementvariable, and that Message object when packed or unpacked will consume the correct alignment of bytes. The elementvariable won't need to do anything extra to get the correct alignment since the referenced Message object would take care of it all.

This would also be an example where it wouldn't be necessary to call the parent class's pack/unpack function to get the correct alignment.