How to turn off alignment (emulate "#pragma pack(1)") for struct (node-ffi, ref-struct)

196 views
Skip to first unread message

Alexander Seliverstov

unread,
Jan 30, 2013, 2:38:53 AM1/30/13
to nod...@googlegroups.com
Hi,

I need to turn off alignment for struct. For example,

var MyStruct = Struct({
    'Version':'long', // 4 bytes
    'MaxSize':'short' // 2 bytes;
});

MyStruct.size will be equal to 8 instead of 6, due to alignment. In C alignmnet can be turned off by #pragma pack(1). 
How can I turn off alignment for struct in node-ffi?

Nathan Rajlich

unread,
Jan 30, 2013, 3:05:10 AM1/30/13
to nod...@googlegroups.com
I actually don't have this implemented in "ref-struct" as of yet,
since I haven't had a need for it yet. However, I did already have an
issue created for it from a while back:
https://github.com/TooTallNate/ref-struct/issues/2

Patch welcome :) But also, a good API for that still needs to be
decided on as well.
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Alexander Seliverstov

unread,
Jan 30, 2013, 10:16:54 AM1/30/13
to nod...@googlegroups.com
Thnx for your answer! 
I have solved this problem by patching StructType class in struct.js file.
I have added field isPacked (with defaul value 'false'), and two methods 'pack()' and 'unpack()' to StructType class in struct.js. 
These methods simply switch isPacked field and run recalc() method from initial StructType definition. Then I have made some changes to recalc() and addType() methods.
In recalc() method I have replaced 

struct.alignment = Math.max(struct.alignment, alignment)

with

if (struct.isPacked){
    if (struct.alignment === 0){
        struct.alignment = alignment;
    }else{
        struct.alignment = Math.min(struct.alignment, alignment)
    }
}else{
    struct.alignment = Math.max(struct.alignment, alignment)
}

And in addType() method I have replaced

var padding = (align - (offset % align)) % align

with

var padding = struct.pack ? 0 : (align - (offset % align)) % align

And

assert.equal(offset % align, 0, "offset should align")

with

if (!struct.isPacked){
    assert.equal(offset % align, 0, "offset should align")
}

среда, 30 января 2013 г., 12:05:10 UTC+4 пользователь Nathan Rajlich написал:

Alexander Seliverstov

unread,
Jan 30, 2013, 10:22:05 AM1/30/13
to nod...@googlegroups.com
Sorry, 

var padding = struct.isPacked ? 0 : (align - (offset % align)) % align

instead of 

Alexander Seliverstov

unread,
Jan 30, 2013, 10:42:18 AM1/30/13
to nod...@googlegroups.com
Also, I have very strange bug:

My script works fine and all foreign functions call perfectly only when environment variable "DEBUG=ref,ref-struct" is set. 

If I unset DEBUG, script will crash with exit code -1073741819.

Some foreign  functions work well and without DEBUG variable, but script will also crash when I simply add yet another function declaration in ffi.Library(..) and even don't call it in my code.

Do you have any ideas where I should find the reason of such strange behavior?
Reply all
Reply to author
Forward
0 new messages