delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/06/23/23:34:50

From: "Tim Van Holder" <tim DOT van DOT holder AT pandora DOT be>
To: <djgpp-workers AT delorie DOT com>
Subject: Re: memalign hooks (was: LIBM patch for GCC 3.3 - math changes)
Date: Mon, 23 Jun 2003 08:09:26 +0200
MIME-Version: 1.0
X-Mailer: Microsoft Outlook, Build 11.0.4920
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3718.0
Thread-Index: AcM45EW5bt3FxrNDR9uU52gp2WuqzQAaGUMw
In-reply-to: <3405-Sun22Jun2003201552+0300-eliz@elta.co.il>
Message-Id: <20030623061004.0164E33DBBB@iceage.anubex.com>
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> > I think memalign should also fail for alignment parameter < ALIGN
> > value (i.e. 8 at present).
> 
> IMHO, it should behave in a way that is compatible with other
> implementations.  Could someone please look on their nearest Unix or
> GNU/Linux box and see what does memalign there do for such small
> alignment parameters?  (Sorry, no time to do this myself.)

Below is what the glibc 2.3.2 (Red Hat 8.0) has to say; note that it
does not say what it does if BOUNDARY is NOT a power of 2.
I could check the glibc sources if necessary.


Allocating Aligned Memory Blocks
................................

   The address of a block returned by `malloc' or `realloc' in the GNU
system is always a multiple of eight (or sixteen on 64-bit systems).
If you need a block whose address is a multiple of a higher power of
two than that, use `memalign', `posix_memalign', or `valloc'.
`memalign' is declared in `malloc.h' and `posix_memalign' is declared
in `stdlib.h'.

   With the GNU library, you can use `free' to free the blocks that
`memalign', `posix_memalign', and `valloc' return.  That does not work
in BSD, however--BSD does not provide any way to free such blocks.

 - Function: void * memalign (size_t BOUNDARY, size_t SIZE)
     The `memalign' function allocates a block of SIZE bytes whose
     address is a multiple of BOUNDARY.  The BOUNDARY must be a power
     of two!  The function `memalign' works by allocating a somewhat
     larger block, and then returning an address within the block that
     is on the specified boundary.

 - Function: int posix_memalign (void **MEMPTR, size_t ALIGNMENT,
          size_t SIZE)
     The `posix_memalign' function is similar to the `memalign'
     function in that it returns a buffer of SIZE bytes aligned to a
     multiple of ALIGNMENT.  But it adds one requirement to the
     parameter ALIGNMENT: the value must be a power of two multiple of
     `sizeof (void *)'.

     If the function succeeds in allocation memory a pointer to the
     allocated memory is returned in `*MEMPTR' and the return value is
     zero.  Otherwise the function returns an error value indicating
     the problem.

     This function was introduced in POSIX 1003.1d.

 - Function: void * valloc (size_t SIZE)
     Using `valloc' is like using `memalign' and passing the page size
     as the value of the second argument.  It is implemented like this:

          void *
          valloc (size_t size)
          {
            return memalign (getpagesize (), size);
          }


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019