From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29252 invoked by alias); 26 Jun 2004 02:39:20 -0000 Mailing-List: contact gdb-patches-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sources.redhat.com Received: (qmail 29212 invoked from network); 26 Jun 2004 02:39:16 -0000 Received: from unknown (HELO develer.com) (151.38.19.110) by sourceware.org with SMTP; 26 Jun 2004 02:39:16 -0000 Received: (qmail 1281 invoked from network); 26 Jun 2004 02:39:04 -0000 Received: from beetle.trilan (HELO ?10.3.2.254?) (?GzVK/NJb+9xum1Ng43HMWt1NDJnF2l84?@10.3.2.254) by trinity.trilan with SMTP; 26 Jun 2004 02:39:04 -0000 Message-ID: <40DCE1C8.4020202@develer.com> Date: Sat, 26 Jun 2004 02:39:00 -0000 From: Bernardo Innocenti Organization: Develer S.r.l. User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040622 MIME-Version: 1.0 To: Ian Lance Taylor CC: GCC Patches , gdb-patches@sources.redhat.com, binutils@sources.redhat.com, DJ Delorie Subject: Re: [top-level] C++-friendly allocators for libiberty References: <40DCC86A.4010306@develer.com> <40DCD0EE.9010208@develer.com> In-Reply-To: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-SW-Source: 2004-06/txt/msg00547.txt.bz2 Ian Lance Taylor wrote: > Bernardo Innocenti writes: > > >>On second thought, the interface for xrenew() or xresize() wasn't >>even usable without a size argument. > > Oh yeah. > > >>Maybe this would be better? >> >> #define xrenewvec(P, T, N) (T *) xrealloc ((P), sizeof(T) * (N)) > > > No, people use realloc with variable size arrays at the end of > structs. xrenewvec (or xresizevec) is a good idea, but you still need > xrenew (or xresize). > > Also I noticed that you should have a space between "sizeof" and "(" > in each use of "sizeof". I keep forgetting about it. Fixed. > I prefer xresize, since that was my idea. Any other ideas? I have no preferences. For consistency, I've renamed mine to xresizevec() and moved the type argument in front of the others. This is what I'm going to commit: include/ 2004-06-26 Bernardo Innocenti * include/libiberty.h (xnew, xcnew, xnewvec, xcnewvec, xobnew): Move here from libcpp/internal.h. (xresize, xresizevec, xdelete, xdeletevec): New macros. diff -u -p -u -r1.35 libiberty.h --- include/libiberty.h 15 May 2003 19:02:12 -0000 1.35 +++ include/libiberty.h 26 Jun 2004 00:27:50 -0000 @@ -250,6 +250,21 @@ extern PTR xmemdup PARAMS ((const PTR, s extern double physmem_total PARAMS ((void)); extern double physmem_available PARAMS ((void)); +/* These macros provide a K&R/C89/C++-friendly way of allocating structures + with nice encapsulation. The xdelete*() macros are technically + superfluous, but provided here for symmetry. Using them consistently + makes it easier to update client code to use different allocators such + as new/delete and new[]/delete[]. */ + +#define xnew(T) (T *) xmalloc (sizeof (T)) +#define xcnew(T) (T *) xcalloc (1, sizeof (T)) +#define xnewvec(T, N) (T *) xmalloc (sizeof (T) * (N)) +#define xcnewvec(T, N) (T *) xcalloc (N, sizeof (T)) +#define xresize(T, P, S) (T *) xrealloc (P, S) +#define xresizevec(T, P, N) (T *) xrealloc (P, sizeof (T) * (N)) +#define xobnew(O, T) (T *) obstack_alloc (O, sizeof (T)) +#define xdelete(P) free (P) +#define xdeletevec(P) free (P) + /* hex character manipulation routines */ #define _hex_array_size 256 -- // Bernardo Innocenti - Develer S.r.l., R&D dept. \X/ http://www.develer.com/