This is the mail archive of the
gdb-patches@sources.redhat.com
mailing list for the GDB project.
[top-level] C++-friendly allocators for libiberty
- From: Bernardo Innocenti <bernie at develer dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>, gdb-patches at sources dot redhat dot com, binutils at sources dot redhat dot com
- Cc: DJ Delorie <dj at redhat dot com>, ian at wasabisystems dot com
- Date: Sat, 26 Jun 2004 02:50:50 +0200
- Subject: [top-level] C++-friendly allocators for libiberty
- Organization: Develer S.r.l.
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 <bernie@develer.com>
* 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 <bernie@develer.com>
* 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/