This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [top-level] C++-friendly allocators for libiberty


Ian Lance Taylor wrote:
> Bernardo Innocenti <bernie@develer.com> 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  <bernie@develer.com>

	* 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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]