This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [top-level] C++-friendly allocators for libiberty
Falk Hueffner wrote:
> Zack Weinberg <zack@codesourcery.com> writes:
>
>
>>Alexandre Oliva <aoliva@redhat.com> writes:
>>
>>
>>>>+#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))
>>>
>>>You're missing parentheses around N, P (twice), S and O.
>>
>>They're not actually necessary in this context.
>
>
> How about parentheses around the whole cast? Somebody might want to
> write
>
> &xnew(T)->el
>
> Not very likely, but IMHO it's not really worth the trouble to try to
> think of every possible or sensible use instead of just adding a bunch
> of parentheses.
Agreed. This is the full patch again updated with your suggestions
and a proposed fix for the realloc() interface.
include/
2004-06-26 Bernardo Innocenti <bernie@develer.com>
* include/libiberty.h (xnew, xcnew, xnewvec, xcnewvec, xobnew): Move
here from libcpp/internal.h.
(xdelete, xresizevec, xdeletevec, xnewvar, xcnewvar, xresizevar): New
macros.
diff -u -p -r1.35 libiberty.h
--- include/libiberty.h 15 May 2003 19:02:12 -0000 1.35
+++ include/libiberty.h 26 Jun 2004 16:35:34 -0000
@@ -250,6 +250,37 @@ 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[]. */
+
+/* Scalar allocators. */
+
+#define xnew(T) ((T *) xmalloc (sizeof (T)))
+#define xcnew(T) ((T *) xcalloc (1, sizeof (T)))
+#define xdelete(P) free (P)
+
+/* Array allocators. */
+
+#define xnewvec(T, N) ((T *) xmalloc (sizeof (T) * (N)))
+#define xcnewvec(T, N) ((T *) xcalloc (N, sizeof (T)))
+#define xresizevec(T, P, N) ((T *) xrealloc (P, sizeof (T) * (N)))
+#define xdeletevec(P) free (P)
+
+/* Allocators for variable-sized structures and raw buffers. */
+
+#define xnewvar(T, S) ((T *) xmalloc (S))
+#define xcnewvar(T, S) ((T *) xcalloc (1, S))
+#define xresizevar(T, P, S) ((T *) xrealloc (P, S))
+
+/* Type-safe obstack allocator. */
+
+#define xobnew(O, T) ((T *) obstack_alloc (O, sizeof (T)))
+
+
/* hex character manipulation routines */
#define _hex_array_size 256
--
// Bernardo Innocenti - Develer S.r.l., R&D dept.
\X/ http://www.develer.com/