This is the mail archive of the gdb-patches@sourceware.org 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: C++-compat clean build


On 10/01/2013 01:53 PM, Agovic, Sanimir wrote:
> Thanks Ondrej for taking the initiative!
> 
> 
>> -----Original Message-----
>> From: gdb-patches-owner@sourceware.org [mailto:gdb-patches-owner@sourceware.org] On Behalf
>> Of Mark Kettenis
>> Sent: Tuesday, October 01, 2013 02:04 PM
>> To: ooprala@redhat.com
>> Cc: gdb-patches@sourceware.org
>> Subject: Re: C++-compat clean build
>>
>> Rejected.  Introducing all these casts can't be right.  They hide type
>> conversion issues and make the code unreadable.
>>
> Afaik, some casts are required to some extend as C++ does no implicit from/to void *
> conversion. But in general I agree about using casts too vast.
> 
> @@ -1598,7 +1598,7 @@ amd64_relocate_instruction (struct gdbarch *gdbarch,
>    int len = gdbarch_max_insn_length (gdbarch);
>    /* Extra space for sentinels.  */
>    int fixup_sentinel_space = len;
> -  gdb_byte *buf = xmalloc (len + fixup_sentinel_space);
> +  gdb_byte *buf = (gdb_byte *) xmalloc (len + fixup_sentinel_space);
> This looks OK to me and could be replaced with a type save new[] once C++ is used.

libiberty gives us a series of macros that kind of mirror
c++ new/new[] (minus the constructors, of course):

 libiberty.h:#define XALLOCA(T)              ((T *) alloca (sizeof (T)))
...
 libiberty.h:#define XNEW(T)                     ((T *) xmalloc (sizeof (T)))
 libiberty.h:#define XCNEW(T)            ((T *) xcalloc (1, sizeof (T)))
 libiberty.h:#define XNEWVEC(T, N)               ((T *) xmalloc (sizeof (T) * (N)))
 libiberty.h:#define XCNEWVEC(T, N)              ((T *) xcalloc ((N), sizeof (T)))
 libiberty.h:#define XNEWVAR(T, S)               ((T *) xmalloc ((S)))
 libiberty.h:#define XCNEWVAR(T, S)              ((T *) xcalloc (1, (S)))
...
etc.

so this case could be written as:

 gdb_byte *buf = XNEWVAR (gdb_byte, len + fixup_sentinel_space);

This:

 struct foo *f = (struct foo *) xmalloc (sizeof (struct foo));

Can be written as:

 struct foo *f = XNEW (struct foo);

There are variants for alloca as well.

We have several uses of these macros already in the tree.
They seem to have the advantage that they hide the casts and perhaps
make the intention of the code clearer while we can't use new/new[].
E.g., XNEW prevents errors of the type:

 struct foo *f = (struct foo *) xmalloc (sizeof (f));

We already use these macros in gdb in several places.
Not sure what people feel about using them more?

-- 
Pedro Alves


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