discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alignment of NSMutableData


From: Pascal Bourguignon
Subject: Re: Alignment of NSMutableData
Date: Tue, 6 Aug 2002 01:35:28 +0200 (CEST)

> From: Pete French <pete@twisted.org.uk>
> Date: Mon, 05 Aug 2002 23:50:42 +0100
> 
> Does anybidy know if anything is guaranteed about the alignment of an
> NSMutableData object's actual data section ? I want to use one instead
> of malloc() to create an array of C structures, but it occcurs to me that
> it might not be aligned properly.
> 
> Any ideas ?

You can always use something like:

    void* align(void* address,int alignment)
        /*
            PRE:    alignment is a power of two.
            RETURN: address+delta, such as (address+delta)%alignment==0
                                       and (0<=delta<alilgnment)
        */
    {
        unsigned long int iddress=address;
        /*
            alignment   alignment-1   ~(alignment-1)
            0000 0001   0000 0000       1111 1111
            0000 0010   0000 0001       1111 1110
            0000 0100   0000 0011       1111 1100
            0000 1000   0000 0111       1111 1000
        */
        return((void*)((iaddress+alignment-1)&(~(alignment-1))));
    }/*align*/


    NSMutableData* data=[NSMutableData 
                           dataWithCapacity:wanted_size+wanted_align+1];
    void* aligned_bytes=align([data bytes],wanted_align);

But most  probably, the memory  block allocated by  NSMutableData will
already be 32-bit aligned.  You may need 64-bit alignment thought.



-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]