From: Miles Bader <miles@lsi.nec.co.jp>
Cc: emacs-devel@gnu.org
Subject: Re: malloc and alignment
Date: 17 Jun 2003 11:25:32 +0900 [thread overview]
Message-ID: <buoof0xphib.fsf@mcspd15.ucom.lsi.nec.co.jp> (raw)
In-Reply-To: <200306162311.h5GNBPG2024716@rum.cs.yale.edu>
"Stefan Monnier" <monnier+gnu/emacs@rum.cs.yale.edu> writes:
> > You could use `memalign'....
>
> How standard is it ?
Here's the glibc info page about it. The traditional `portable' version
is exactly the sort of over-allocate-and-use-the-middle type thing you
mentioned. I guess the biggest problem is how to _free_ such blocks --
as noted below, glibc can do so, but BSD can't (who knows how old that
info is, though)...
-Miles
File: libc.info, Node: Aligned Memory Blocks, Next: Malloc Tunable Parameters, Prev: Efficiency and Malloc, Up: Unconstrained Allocation
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);
}
*Note Query Memory Parameters:: for more information about the
memory subsystem.
--
The secret to creativity is knowing how to hide your sources.
--Albert Einstein
next prev parent reply other threads:[~2003-06-17 2:25 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-16 14:38 malloc and alignment Stefan Monnier
2003-06-16 15:15 ` David Kastrup
2003-06-16 15:39 ` Stefan Monnier
2003-06-16 15:59 ` Stefan Monnier
2003-06-17 4:57 ` Stephen J. Turnbull
2003-06-17 7:35 ` David Kastrup
2003-06-16 22:35 ` Miles Bader
2003-06-16 23:11 ` Stefan Monnier
2003-06-17 2:25 ` Miles Bader [this message]
2003-06-17 4:48 ` Stephen J. Turnbull
[not found] ` <E19SJCe-0008Bb-Vp@fencepost.gnu.org>
2003-06-24 22:52 ` Stefan Monnier
[not found] ` <E19VGK5-0000CQ-F4@fencepost.gnu.org>
2003-06-27 23:17 ` Stefan Monnier
2003-06-27 23:47 ` Miles Bader
2003-06-29 2:30 ` Richard Stallman
2003-07-04 20:42 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=buoof0xphib.fsf@mcspd15.ucom.lsi.nec.co.jp \
--to=miles@lsi.nec.co.jp \
--cc=emacs-devel@gnu.org \
--cc=miles@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).