From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Miles Bader Newsgroups: gmane.emacs.devel Subject: Re: malloc and alignment Date: 17 Jun 2003 11:25:32 +0900 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: References: <200306161438.h5GEcodM011551@rum.cs.yale.edu> <20030616223526.GA8010@gnu.org> <200306162311.h5GNBPG2024716@rum.cs.yale.edu> Reply-To: Miles Bader NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1055816702 13293 80.91.224.249 (17 Jun 2003 02:25:02 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Tue, 17 Jun 2003 02:25:02 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Tue Jun 17 04:25:00 2003 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19S69B-0003Qy-00 for ; Tue, 17 Jun 2003 04:24:33 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 19S6Ws-0003Y6-00 for ; Tue, 17 Jun 2003 04:49:02 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.20) id 19S6BZ-0005h7-15 for emacs-devel@quimby.gnus.org; Mon, 16 Jun 2003 22:27:01 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.20) id 19S6B2-00054s-WD for emacs-devel@gnu.org; Mon, 16 Jun 2003 22:26:29 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.20) id 19S6Ah-0004Ah-PR for emacs-devel@gnu.org; Mon, 16 Jun 2003 22:26:08 -0400 Original-Received: from tyo202.gate.nec.co.jp ([202.32.8.202]) by monty-python.gnu.org with esmtp (Exim 4.20) id 19S6AY-0003dc-MI; Mon, 16 Jun 2003 22:25:59 -0400 Original-Received: from mailgate4.nec.co.jp (mailgate54.nec.co.jp [10.7.69.197]) h5H2PbH28679; Tue, 17 Jun 2003 11:25:38 +0900 (JST) Original-Received: (from root@localhost) by mailgate4.nec.co.jp (8.11.6/3.7W-MAILGATE-NEC) id h5H2Pbr02893; Tue, 17 Jun 2003 11:25:37 +0900 (JST) Original-Received: from edtmg01.lsi.nec.co.jp ([10.26.16.201]) by mailsv4.nec.co.jp (8.11.6/3.7W-MAILSV4-NEC) with ESMTP id h5H2Pa306260; Tue, 17 Jun 2003 11:25:36 +0900 (JST) Original-Received: from mcsss2.ucom.lsi.nec.co.jp (localhost [127.0.0.1]) id LAA15620; Tue, 17 Jun 2003 11:25:35 +0900 (JST) Original-Received: from mcspd15.ucom.lsi.nec.co.jp (mcspd15 [10.30.114.174]) with ESMTP id h5H2PWNH017219; Tue, 17 Jun 2003 11:25:32 +0900 (JST) Original-Received: by mcspd15.ucom.lsi.nec.co.jp (Postfix, from userid 31295) id AD4D13732; Tue, 17 Jun 2003 11:25:32 +0900 (JST) Original-To: "Stefan Monnier" System-Type: i686-pc-linux-gnu Blat: Foop In-Reply-To: <200306162311.h5GNBPG2024716@rum.cs.yale.edu> Original-Lines: 69 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Emacs development discussions. List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:15143 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:15143 "Stefan Monnier" 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