From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12616 invoked by alias); 26 Jun 2004 00:50:54 -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 12389 invoked from network); 26 Jun 2004 00:50:52 -0000 Received: from unknown (HELO develer.com) (151.38.19.110) by sourceware.org with SMTP; 26 Jun 2004 00:50:52 -0000 Received: (qmail 29229 invoked from network); 26 Jun 2004 00:50:50 -0000 Received: from beetle.trilan (HELO ?10.3.2.254?) (?V7KO5Ex0wMBxT5HDchoetp16WkSMt0sF?@10.3.2.254) by ns.trilan with SMTP; 26 Jun 2004 00:50:50 -0000 Message-ID: <40DCC86A.4010306@develer.com> Date: Sat, 26 Jun 2004 00:50: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: GCC Patches , gdb-patches@sources.redhat.com, binutils@sources.redhat.com CC: DJ Delorie , ian@wasabisystems.com Subject: [top-level] C++-friendly allocators for libiberty Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-SW-Source: 2004-06/txt/msg00542.txt.bz2 Hello, this patch has been discussed here: http://gcc.gnu.org/ml/gcc-patches/2004-06/msg02122.html The idea is to use a familiar C++ idiom for allocation and destruction of structures. Hiding both the sizeof expression and the cast from void * (or char *) inside the macro avoids problems with mismatching types, allows a shorter syntax. Moreover, using these macros make client code portable across all C dialects (K&R C, C89, C99 and C++). top-level: 2004-06-26 Bernardo Innocenti * include/libiberty.h (xnew, xcnew, xnewvec, xcnewvec, xobnew): Move here from libcpp/internal.h. (xcrealloc, xdelete, xdeletevec): New macros. libcpp: 2004-06-26 Bernardo Innocenti * internal.h (xnew, xcnew, xnewvec, xcnewvec, xobnew): Move these macros to include/libiberty.h. 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 xcrealloc(P, T) (T *) xrealloc (P, sizeof(T)) +#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 diff -u -p -u -r1.3 internal.h --- libcpp/internal.h 9 Jun 2004 20:10:13 -0000 1.3 +++ libcpp/internal.h 26 Jun 2004 00:27:50 -0000 @@ -559,11 +559,6 @@ extern const char *_cpp_default_encoding /* Utility routines and macros. */ #define DSC(str) (const uchar *)str, sizeof str - 1 -#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 xobnew(O, T) (T *) obstack_alloc (O, sizeof(T)) /* These are inline functions instead of macros so we can get type checking. */ -- // Bernardo Innocenti - Develer S.r.l., R&D dept. \X/ http://www.develer.com/