Thread: align syntax

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    align syntax

    Is this code correct?

    Code:
    	#define GRANULARITY 4 //Bytes
    	#define ALIGN __attribute__ ((aligned (GRANULARITY)))
    
    	typedef pthread_mutex_t ALIGN MUTEX;
    
    typedef struct 
    {
    	MUTEX  Mutex;	
    	unsigned int MeasureActive;
    	unsigned int LastIndex;
    	unsigned int FirstIndex;
    	unsigned int ElementsNr;
    	unsigned long long int Overflow;
    	int64 RecordingInstant[MeasFIFO_SIZE];
    	unsigned int PacketIndex[MeasFIFO_SIZE];	
    	int Value[MeasFIFO_SIZE];
    	
    } ALIGN Type_IntMeasFIFOComm;
    
    typedef class  c_IntWrParCAM_FIFOComm
    {
    public:
    	c_IntWrParCAM_FIFOComm()	{	pthread_mutex_init(&Mutex, NULL);	}
    	~c_IntWrParCAM_FIFOComm()	{	pthread_mutex_destroy(&Mutex);	}
    	
    	MUTEX  Mutex;
    
    	unsigned int LastIndex;
    	unsigned int FirstIndex;
    	unsigned int ElementsNr;	
    	unsigned long long int Overflow;
    	
    	unsigned int TargetIndex[WrFIFO_SIZE];	// DUMMY !!
    	unsigned int ParIndex[WrFIFO_SIZE];
    	int Value[WrFIFO_SIZE];
    	unsigned int ServerIndex[WrFIFO_SIZE];
    	int sock[WrFIFO_SIZE];	
    	
    } ALIGN Type_IntWrParCAM_FIFOComm;
    I ask especially about
    (1) the 4 usages of ALIGN (2 direct usages, 2 indirect via MUTEX)
    (2) the use of the constructor destructor (those classes will have global object instances)
    (3) the use of typedef class ..Type_IntWrParCAM_FIFOComm;

    It is basically a variation of a structure just not to have to remember to call pthread_..init/destroy somewhere in the code (I often forget!!)
    Last edited by mynickmynick; 08-27-2008 at 10:23 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally wouldn't change the alignment of a pthread_mutex - it is either defined properly elsewhere, or it will work OK without further alignment [or you have a bug to report to the pthread library supplier]. You trying to align it further will never make anything better - but you may break the previous alignment [gcc accepts changes in alignment, as shown in this example]:
    Code:
    #include <stdio.h>
    
    typedef int foo __attribute__ ((aligned (16)));
    typedef foo bar __attribute__ ((aligned (4)));
    
    
    bar y;
    foo x;
    
    void xfunc(foo a, bar b)
    {
      foo x;
      bar y;
    
      printf("Func: x(foo) = &#37;p, y(bar) = %p, a(foo) = %p, b(bar) = %p\n", &x, &y, &a, &b);
    }
    
    
    
    int main()
    {
      bar a = 7;
      foo b = 8;
    
      xfunc(a, b);
      printf("Main: x(foo) = %p, y(bar) = %p, a(bar) = %p, b(foo) = %p\n", &x, &y, &a, &b);
    
      return 0;
    }
    Structures (and thus classes) are ALWAYS aligned to the largest alignment needed by the structure, so aligning the WHOLE class to the same value as you have aligned one of it's members is pointless and reduntant.

    Edit: On closer inspection, you also have int64 and unsigned long long members of your struct, which means that the struct itself SHOULD be aligned to 8 bytes. You are now REDUCING it's alignment (which you are allowed to do, but probably not a good idea unless you are trying to save a few bytes).

    --
    Mats
    Last edited by matsp; 08-27-2008 at 11:50 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post

    Structures (and thus classes) are ALWAYS aligned to the largest alignment needed by the structure, so aligning the WHOLE class to the same value as you have aligned one of it's members is pointless and reduntant.

    Edit: On closer inspection, you also have int64 and unsigned long long members of your struct, which means that the struct itself SHOULD be aligned to 8 bytes. You are now REDUCING it's alignment (which you are allowed to do, but probably not a good idea unless you are trying to save a few bytes).

    --
    Mats

    Sorry I don't get this
    Can you give links to read about it? "needed by the struct" : needed for what? why if there is a field of 8 bytes (like int64) the struct should be aligned to 8 bytes? May be the whole size of the struct is not a multiple of 8 bytes (in this special case it is butit is sufficient to add one int dummy; andit is not any more) so I guess it doesn't need it?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Consider this:
    Code:
    struct x
    {
       int64 a;
       int b;
    };
    
    struct x arr[10];
    If the entire struct is not aligned to the size of int64, what is the alignment of arr[1].a?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also note that aligning is compiler dependant. Other compilers require different syntax and some do not even have the ability to align at all.
    Unless you really do need to align, consider not doing so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Also note that aligning is compiler dependant. Other compilers require different syntax and some do not even have the ability to align at all.
    Unless you really do need to align, consider not doing so.
    I heartily agree with this. I think you are trying to solve something that isn't a problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM