5

My question is composed of 2 parts.

  1. I noticed that memalign(block_size,bytes) on sparc(sun) with a cc compiler doesn't check that bytes is a power of 2 as opposed to memalign on intel linux and _aligned_malloc on windows with mvsc compiler that do check that bytes are a power of 2.

is this a bug in a sun, is there a patch or i need to write a memalign by my self.

  1. Additionally i have a structure (not my code):

    typedef struct CLHLockStruct {
        volatile CLHLockNode CACHE_ALIGN *Tail ;
        volatile CLHLockNode CACHE_ALIGN *MyNode[N_THREADS] ;
        volatile CLHLockNode CACHE_ALIGN *MyPred[N_THREADS] ;
    } CLHLockStruct;
    

I am compiling under MVSC (visual studio 2008):

    CACHE_LINE_SIZE = 64
    CACHE_ALIGN = __declspec(align(CACHE_LINE_SIZE)) 
    N_THREADS = 8
    sizeof(CLHLockStruct)=192

The code has been written initially for a sparc architecture, and i try to migrate it to MVSC without changing to much code.

in their code they use memalign(CACHE_LINE_SIZE,sizeof(CLHLockStruct)), and i have changed it to _aligned_malloc , my problem is that sizeof(CLHLockStruct) is not a power of 2, i may write some function that finds the next number that is a power of 2.

Is their a better approach ?

EDIT1

How may i padd this struct so that its size will be a power of 2?

EDIT2

Is there a function that acts like _aligned_malloc and malloc : returns a memory pointer aligned to the multiple of block_size but doesn't require the bytes to be a power of 2?

Michael
  • 2,827
  • 4
  • 30
  • 47
  • `cc` is a symbolic link on linux. What are the compilers and which version ? – log0 Aug 08 '12 at 06:44
  • Sorry , i had a small mistake, the compiler on linux is g++ (gcc version 4.4.3) i have edited the title and added a tag – Michael Aug 08 '12 at 06:52

2 Answers2

4

in their code they use memalign(CACHE_LINE_SIZE,sizeof(CLHLockStruct)), and i have changed it to _aligned_malloc , my problem is that sizeof(CLHLockStruct) is not a power of 2,

Did you change it to _aligned_malloc(CACHE_LINE_SIZE,sizeof(CLHLockStruct))? Yes, that would cause this. _aligned_malloc expects the size as its first argument, and the alignment as the second, the opposite of memalign, so you need to swap the arguments. The size does not need to be a power of two.

1

Answer 1: The POSIX standard defines memalign to return a power of two and a multiple of the system's pointer size. But it also requires the blocksize parameter to be a power of two. Is that the case for your code? Could also be that your Win library does not fully comply with POSIX in this case.

Answer 2: This SO post lists a couple of ways of determining the next power of two, which you may use in your aligned_malloc implementation.

Answer 3: I (better: Google) found another post that explains padding a struct to a multiple of a power of two.

Community
  • 1
  • 1
BjoernD
  • 4,720
  • 27
  • 32
  • I will go over answer 2,3 , it seems very relevant to me, although i preferred some pragma directive if there is one. as regards to answer 1, It is not my code, and it seems that they demand the structure size to be a power of 2 in-order that memalign will work, although initially this code was intend to be on a sparc architecture, and if you check the size of the structure their it is not a power of 2, it seems that they had a bug in their code, but the bug in memalign on sparc(sun) cc compiler may not reviled it. – Michael Aug 08 '12 at 06:59
  • Sure, that may be the case. I was just trying to give hints on what may be wrong. ;) – BjoernD Aug 08 '12 at 07:03
  • i have edited my question with one more suggestion, could you answer it? – Michael Aug 08 '12 at 07:08