This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: C++-compat clean build
- From: Pedro Alves <palves at redhat dot com>
- To: "Agovic, Sanimir" <sanimir dot agovic at intel dot com>
- Cc: "'Mark Kettenis'" <mark dot kettenis at xs4all dot nl>, "ooprala at redhat dot com" <ooprala at redhat dot com>, "gdb-patches at sourceware dot org" <gdb-patches at sourceware dot org>
- Date: Tue, 01 Oct 2013 15:16:22 +0100
- Subject: Re: C++-compat clean build
- Authentication-results: sourceware.org; auth=none
- References: <524AB12E dot 8090209 at redhat dot com> <201310011204 dot r91C4QVo006124 at glazunov dot sibelius dot xs4all dot nl> <0377C58828D86C4588AEEC42FC3B85A7176850E2 at IRSMSX105 dot ger dot corp dot intel dot com>
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