Re: Problem with alignment under Linux
2012-05-13 08:27:39 GMT
- check each member of a union for a type which requires the 8 byte alignment and mark that union appropriately if such a type is found (ie double or long long);
- check each member of a struct for a type which requires the 8 byte alignment, and if such a member is encountered insert appropriate padding before the member
1. in your example below, you suggest a function align_union(union_class), which could be called in _init_. This could be used when my code instantiates such unions; but what about unions that are created by the server side that I’m only trying to read?
2. Therefore, I changed the sample code; as I know which unions need to be aligned differently, I guess I don’t need this function, and could set class._alignment immediately, e.g.:
Axel
I think the comment in the documentation has to be a red herring, as it would make that compiler noncompliant with the Linux i386 ABI by default which would probably cause all sorts of problems compiling other software.
It seems to me that you will need to do several things:- check each member of a union for a type which requires the 8 byte alignment and mark that union appropriately if such a type is found (ie double or long long);- check each member of a struct for a type which requires the 8 byte alignment, and if such a member is encountered insert appropriate padding before the member.
The approach that occurs to me is to use an attribute which can be tested for using hasattr() (or AttributeError if you prefer exceptions rather than look before leap).
Perhaps something along the lines of (untested! and not necessarily all encompassing):
...from ctypes import *
...
# this foreign DLL is built with –malign_doublec_double._alignment = 8c_long_long._alignment = 8
...
# check a Union for members requiring non-standard alignmentdef align_union(union_class):for n, t in union_class._fields_:if hasattr(t, ‘_alignment’):union_class._alignment = t._alignmentbreak
# patch the ‘fields’ sequence of a ctypes Structure by inserting# dummy fields to achieve required alignmentdef pad_struct(fields):result = []size = 0for n, t in fields:if hasattr(t, ‘_alignment’):padding = size % t._alignmentif padding:padding = t._alignment – paddingresult.append((‘’, c_char * padding))size += paddingsize += sizeof(t)result.append((n, t))return result
...
class U(Union):
_fields_ = ((‘a’, c_long),(‘b’, c_double))
def __init__(self):align_union(self)Union.__init__(self)
class S(Structure):_fields_ = pad_struct(((‘c’, c_long),(‘d’, U)))
...
-------------------------> "These thoughts are mine alone!" <---------Andrew MacIntyre Operations Branchtel: +61 2 6219 5356 Communications Infrastructure Divisionfax: +61 2 6253 3277 Australian Communications & Media Authority
From: Axel Seibert [mailto:axel <at> globonaut.org]
Sent: Tuesday, 14 February 2012 6:34 AM
To: ctypes-users <at> lists.sourceforge.net
Subject: Re: [ctypes-users] Problem with alignment under Linux [SEC=UNCLASSIFIED]
Hi, Andrew!
According to http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html, "aligned" is the attribute to look for. There is another possibility that occurs to me: the use of the gcc option "-malign-double" forcing doubles to be 8 byte aligned when building the library.
This is the hint that brought me in the right direction; looking at one of the supplied examples shows that the Makefile under Linux calls the compiler with the following arguments:
-malign-double
And the documentation says:Configure the compiler as follows: Structure member alignment: 8 bytes (default)
In other words, the software supplier requires this for whatever reasons. What can we now deduct from this for my problem?
Thanx again for your help,Axel
NOTICE: This email message is for the sole use of the intended recipient(s)
------------------------------------------------------------------------------
and may contain confidential and privileged information. Any unauthorized
review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all
copies of the original message.
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________ ctypes-users mailing list ctypes-users <at> lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ctypes-users
RSS Feed