Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Joel Brobecker <brobecker@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 3/9] gmp-utils: New API to simply use of GMP's integer/rational/float objects
Date: Tue, 10 Nov 2020 15:15:24 -0500	[thread overview]
Message-ID: <2966f3cf-955c-1849-e7ab-f01843cb7a33@simark.ca> (raw)
In-Reply-To: <1604817017-25807-4-git-send-email-brobecker@adacore.com>

On 2020-11-08 1:30 a.m., Joel Brobecker wrote:
> This API was motivated by a number of reasons:
>   - GMP's API does not handle "long long" and "unsigned long long",
>     so using LONGEST and ULONGEST is not straightforward;
>   - Automate the need to initialize GMP objects before use, and
>     clear them when no longer used.
>
> However, this API grew also to help with similar matter such
> as formatting to a string, and also reading/writing fixed-point
> values from byte buffers.
>
> Dedicated unit testing is also added.

Here are some comments.  Most of them are really just suggestions, what you
have looks good to me.  The suggestions can easily be implemented later, so you
don't have to re-do your patch series.

> +extern void _initialize_gmp_utils ();

You can drop "extern".

> +
> +void
> +_initialize_gmp_utils ()
> +{
> +  /* Tell GMP to use GDB's memory management routines.  */
> +  mp_set_memory_functions (xmalloc, xrealloc_for_gmp, xfree_for_gmp);

What you have is fine, but if you prefer you could also use lambda
functions, since they are trivial wrappers:

  /* Tell GMP to use GDB's memory management routines.  */
  mp_set_memory_functions (xmalloc,
			   [] (void *ptr, size_t old_size, size_t new_size)
			   {
			     return xrealloc (ptr, new_size);
			   },
			   [] (void *ptr, size_t size)
			   {
			     return xfree (ptr);
			   });

> +}
> diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
> new file mode 100644
> index 0000000..8a4fbfe
> --- /dev/null
> +++ b/gdb/gmp-utils.h
> @@ -0,0 +1,282 @@
> +/* Miscellaneous routines making it easier to use GMP within GDB's framework.
> +
> +   Copyright (C) 2019-2020 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GMP_UTILS_H
> +#define GMP_UTILS_H
> +
> +#include "defs.h"
> +
> +/* Include <stdio.h> and <stdarg.h> ahead of <gmp.h>, so as to get
> +   access to GMP's various formatting functions.  */
> +#include <stdio.h>
> +#include <stdarg.h>
> +#include <gmp.h>
> +#include "gdbsupport/traits.h"
> +
> +/* Same as gmp_asprintf, but returning a convenient wrapper type.  */
> +
> +gdb::unique_xmalloc_ptr<char> gmp_string_asprintf (const char *fmt, ...);

I don't know how gmp_string_asprintf will be used, but would it make
sense to make it return an std::string?  Does gmp_sprintf support
passing NULL as BUF, so that you could replicate what is done in
string_printf?

> +
> +/* A class to make it easier to use GMP's mpz_t values within GDB.  */
> +
> +struct gdb_mpz
> +{
> +  mpz_t val;

I don't know what's coming in the following patches, but would it work
to make the field private and only use it through methods?  Having it
totally encapsulated would make me more confident that the callers don't
do anything they are not supposed to with it.

There would need to be methods for arithmetic operations that we use
(e.g. mpz_add), but we don't have to add methods for all existing
functions in gmp, just the ones we use.  And I presume we don't use that
many.

> +
> +  /* Constructors.  */
> +  gdb_mpz () { mpz_init (val); }
> +
> +  explicit gdb_mpz (const mpz_t &from_val)
> +  {
> +    mpz_init (val);
> +    mpz_set (val, from_val);
> +  }
> +
> +  gdb_mpz (const gdb_mpz &from)
> +  {
> +    mpz_init (val);
> +    mpz_set (val, from.val);
> +  }
> +
> +  /* Initialize using the given integral value.
> +
> +     The main advantage of this method is that it handles both signed
> +     and unsigned types, with no size restriction.  */
> +  template<typename T, typename = gdb::Requires<std::is_integral<T>>>
> +  explicit gdb_mpz (T src)
> +  {
> +    mpz_init (val);
> +    set (src);
> +  }
> +
> +  explicit gdb_mpz (gdb_mpz &&from)
> +  {
> +    mpz_init (val);
> +    mpz_swap (val, from.val);
> +  }
> +
> +
> +  gdb_mpz &operator= (const gdb_mpz &from)
> +  {
> +    mpz_set (val, from.val);
> +    return *this;
> +  }
> +
> +  gdb_mpz &operator== (gdb_mpz &&other)
> +  {
> +    mpz_swap (val, other.val);
> +    return *this;
> +  }

Is this meant to be "operator="?

> +
> +  template<typename T, typename = gdb::Requires<std::is_integral<T>>>
> +  gdb_mpz &operator= (T src)
> +  {
> +    set (src);
> +    return *this;
> +  }
> +
> +  /* Convert VAL to an integer of the given type.
> +
> +     The return type can signed or unsigned, with no size restriction.  */
> +  template<typename T> T as_integer () const;
> +
> +  /* Set VAL by importing the number stored in the byte buffer (BUF),
> +     given its size (LEN) and BYTE_ORDER.
> +
> +     UNSIGNED_P indicates whether the number has an unsigned type.  */
> +  void read (const gdb_byte *buf, int len, enum bfd_endian byte_order,
> +	     bool unsigned_p);
> +
> +  /* Write VAL into BUF as a LEN-bytes number with the given BYTE_ORDER.
> +
> +     UNSIGNED_P indicates whether the number has an unsigned type.  */
> +  void write (gdb_byte *buf, int len, enum bfd_endian byte_order,
> +	      bool unsigned_p) const;

These two would be good candidates for gdb::array_view.

> +
> +  /* Return a string containing VAL.  */
> +  gdb::unique_xmalloc_ptr<char> str () const
> +  { return gmp_string_asprintf ("%Zd", val); }
> +
> +  /* The destructor.  */
> +  ~gdb_mpz () { mpz_clear (val); }
> +
> +private:
> +
> +  /* Helper template for constructor and operator=.  */
> +  template<typename T> void set (T src);
> +};
> +
> +/* A class to make it easier to use GMP's mpq_t values within GDB.  */
> +
> +struct gdb_mpq
> +{
> +  mpq_t val;
> +
> +  /* Constructors.  */
> +  gdb_mpq () { mpq_init (val); }
> +
> +  explicit gdb_mpq (const mpq_t &from_val)
> +  {
> +    mpq_init (val);
> +    mpq_set (val, from_val);
> +  }
> +
> +  gdb_mpq (const gdb_mpq &from)
> +  {
> +    mpq_init (val);
> +    mpq_set (val, from.val);
> +  }
> +
> +  explicit gdb_mpq (gdb_mpq &&from)
> +  {
> +    mpq_init (val);
> +    mpq_swap (val, from.val);
> +  }
> +
> +  /* Copy assignment operator.  */
> +  gdb_mpq &operator= (const gdb_mpq &from)
> +  {
> +    mpq_set (val, from.val);
> +    return *this;
> +  }
> +
> +  gdb_mpq &operator= (gdb_mpq &&from)
> +  {
> +    mpq_swap (val, from.val);
> +    return *this;
> +  }
> +
> +  /* Return a string representing VAL as "<numerator> / <denominator>".  */
> +  gdb::unique_xmalloc_ptr<char> str () const
> +  { return gmp_string_asprintf ("%Qd", val); }
> +
> +  /* Return VAL rounded to the nearest integer.  */
> +  gdb_mpz get_rounded () const;
> +
> +  /* Set VAL from the contents of the given buffer (BUF), which
> +     contains the unscaled value of a fixed point type object
> +     with the given size (LEN) and byte order (BYTE_ORDER).
> +
> +     UNSIGNED_P indicates whether the number has an unsigned type.
> +     SCALING_FACTOR is the scaling factor to apply after having
> +     read the unscaled value from our buffer.  */
> +  void read_fixed_point (const gdb_byte *buf, int len,
> +			 enum bfd_endian byte_order, bool unsigned_p,
> +			 const gdb_mpq &scaling_factor);
> +
> +  /* Write VAL into BUF as a LEN-bytes fixed point value following
> +     the given BYTE_ORDER.
> +
> +     UNSIGNED_P indicates whether the number has an unsigned type.
> +     SCALING_FACTOR is the scaling factor to apply before writing
> +     the unscaled value to our buffer.  */
> +  void write_fixed_point (gdb_byte *buf, int len,
> +			  enum bfd_endian byte_order, bool unsigned_p,
> +			  const gdb_mpq &scaling_factor) const;
> +
> +  /* The destructor.  */
> +  ~gdb_mpq () { mpq_clear (val); }
> +};
> +
> +/* A class to make it easier to use GMP's mpz_t values within GDB.

mpz_t -> mpf_t

> +extern void _initialize_gmp_utils_selftests ();

You can drop "extern".

Simon

  reply	other threads:[~2020-11-10 20:15 UTC|newest]

Thread overview: 140+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-08  6:30 RFA: Add support for DWARF-based fixed point types Joel Brobecker
2020-11-08  6:30 ` [PATCH 1/9] gdb/configure: Add --with-libgmp-prefix option Joel Brobecker
2020-11-08  6:30 ` [PATCH 2/9] gdb: Make GMP a required dependency for building GDB Joel Brobecker
2020-12-15  6:55   ` Sebastian Huber
2020-12-15  8:57     ` Joel Brobecker
2020-11-08  6:30 ` [PATCH 3/9] gmp-utils: New API to simply use of GMP's integer/rational/float objects Joel Brobecker
2020-11-10 20:15   ` Simon Marchi [this message]
2020-11-13  8:12     ` Joel Brobecker
2020-11-13 15:04       ` Tom Tromey
2020-11-13 15:06         ` Simon Marchi
2020-11-16 16:18         ` Tom Tromey
2020-11-16 16:34   ` Luis Machado via Gdb-patches
2020-11-18  3:52     ` Joel Brobecker
2020-11-08  6:30 ` [PATCH 4/9] Move uinteger_pow gdb/valarith.c to gdb/utils.c and make it public Joel Brobecker
2020-11-08  6:30 ` [PATCH 5/9] Add support for printing value of DWARF-based fixed-point type objects Joel Brobecker
2020-11-10 21:06   ` Simon Marchi
2020-11-14 10:48     ` Joel Brobecker
2020-11-14 13:20       ` Simon Marchi
2020-11-14 11:30     ` Joel Brobecker
2020-11-14 16:14       ` Simon Marchi
2020-11-15  5:30         ` Joel Brobecker
2020-11-15  6:33     ` Joel Brobecker
2020-11-16  0:13       ` Simon Marchi
2020-11-08  6:30 ` [PATCH 6/9] fix printing of DWARF fixed-point type objects with format modifier Joel Brobecker
2020-11-10 22:50   ` Simon Marchi
2020-11-08  6:30 ` [PATCH 7/9] Add ptype support for DWARF-based fixed-point types Joel Brobecker
2020-11-10 23:00   ` Simon Marchi
2020-11-15  6:57     ` Joel Brobecker
2020-11-15  7:09       ` Joel Brobecker
2020-11-16  0:16         ` Simon Marchi
2020-11-16  4:03           ` Joel Brobecker
2020-11-08  6:30 ` [PATCH 8/9] Add support for fixed-point type arithmetic Joel Brobecker
2020-11-10 23:18   ` Simon Marchi
2020-11-08  6:30 ` [PATCH 9/9] Add support for fixed-point type comparison operators Joel Brobecker
2020-11-10 23:21 ` RFA: Add support for DWARF-based fixed point types Simon Marchi
2020-11-11  4:53   ` Joel Brobecker
2020-11-15  8:35 ` pushed: " Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 1/9] gdb/configure: Add --with-libgmp-prefix option Joel Brobecker
2020-11-15 15:52     ` Bernd Edlinger
2020-11-16  3:45       ` Joel Brobecker
2020-11-16 14:20         ` Bernd Edlinger
2020-11-17  7:41           ` [PATCH] Enable GDB build with in-tree GMP and MPFR Bernd Edlinger
2020-11-18  3:44             ` Joel Brobecker
2020-11-18  8:14               ` Bernd Edlinger
2020-12-01 19:29                 ` Bernd Edlinger
2020-12-01 19:32                   ` Simon Marchi
2020-12-01 19:38                     ` Bernd Edlinger
2020-12-01 20:29                       ` Bernd Edlinger
2020-12-01 20:30                         ` Simon Marchi
2020-12-02  3:21                           ` Joel Brobecker
2020-12-08 20:39                             ` [PING] " Bernd Edlinger
2020-12-14 17:40                         ` [PATCH v2] " Bernd Edlinger
2020-12-14 18:47                           ` Simon Marchi
2020-12-14 21:35                             ` Tom Tromey
2020-12-14 22:17                               ` Simon Marchi
2020-12-15  2:33                                 ` Joel Brobecker
2020-12-15 14:39                                   ` Simon Marchi via Gdb-patches
2020-12-15 16:24                                     ` Bernd Edlinger
2020-12-16  7:33                                     ` Joel Brobecker
2020-12-16 18:16                                       ` Bernd Edlinger
2020-12-25 12:05                                         ` Bernd Edlinger
2020-12-27 22:01                                           ` Simon Marchi via Gdb-patches
2020-12-29  8:36                                             ` Bernd Edlinger
2020-12-29 14:50                                               ` Simon Marchi via Gdb-patches
2021-01-10 14:12                                                 ` Bernd Edlinger
2021-01-10 15:32                                                   ` Simon Marchi via Gdb-patches
2021-01-11  3:22                                                   ` Joel Brobecker
2021-01-16 18:01                                                     ` Bernd Edlinger
2020-12-15 15:33                                 ` Bernd Edlinger
2020-12-15 15:10                             ` Bernd Edlinger
2020-11-15  8:35   ` [pushed/v2 2/9] gdb: Make GMP a required dependency for building GDB Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 3/9] gmp-utils: New API to simply use of GMP's integer/rational/float objects Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 4/9] Move uinteger_pow gdb/valarith.c to gdb/utils.c and make it public Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 5/9] Add support for printing value of DWARF-based fixed-point type objects Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 6/9] fix printing of DWARF fixed-point type objects with format modifier Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 7/9] Add ptype support for DWARF-based fixed-point types Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 8/9] Add support for fixed-point type arithmetic Joel Brobecker
2020-11-15  8:35   ` [pushed/v2 9/9] Add support for fixed-point type comparison operators Joel Brobecker
2020-11-16 23:48   ` pushed: Add support for DWARF-based fixed point types Pedro Alves
2020-11-17 14:25     ` Simon Marchi
2020-11-18  3:47       ` Joel Brobecker
2020-11-22 13:12         ` [RFA] Add TYPE_CODE_FIXED_POINT handling in print_type_scalar Joel Brobecker
2020-11-22 14:35           ` Simon Marchi
2020-11-24  3:04             ` Joel Brobecker
2020-11-22 14:00         ` pushed: Add support for DWARF-based fixed point types Joel Brobecker
2020-11-22 20:11           ` Simon Marchi
2020-11-23  4:27             ` Joel Brobecker
2020-11-23 16:12               ` Simon Marchi
2020-11-24  2:39                 ` Joel Brobecker
2020-11-29 15:45               ` RFA: wrap mpz_export into gdb_mpz::safe_export Joel Brobecker
2020-11-29 15:45                 ` [RFA 1/2] Fix TARGET_CHAR_BIT/HOST_CHAR_BIT confusion in gmp-utils.c Joel Brobecker
2020-11-30 15:42                   ` Simon Marchi
2020-12-05  8:05                     ` Joel Brobecker
2020-11-29 15:45                 ` [RFA 2/2] gmp-utils: protect gdb_mpz exports against out-of-range values Joel Brobecker
2020-11-30 15:56                   ` Simon Marchi
2020-12-01  3:37                     ` Joel Brobecker
2020-12-01  4:02                       ` Simon Marchi
2020-12-01  7:11                         ` Joel Brobecker
2020-12-05  8:10                   ` [RFAv2 " Joel Brobecker
2020-12-05 23:26                     ` Simon Marchi
2020-12-06  4:58                       ` Joel Brobecker
2020-11-30 12:44                 ` RFA: wrap mpz_export into gdb_mpz::safe_export Christian Biesinger via Gdb-patches
2020-11-20 14:08   ` pushed: Add support for DWARF-based fixed point types Pedro Alves
2020-11-20 14:14     ` Joel Brobecker
2020-11-22 11:56   ` RFA/doco: Various changes related to GMP and fixed point type support Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 1/4] gdb/NEWS: Document that building GDB now requires GMP Joel Brobecker
2020-11-22 15:31       ` Eli Zaretskii via Gdb-patches
2020-11-24  3:11         ` Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 2/4] gdb/NEWS: Document that GDB now supports DWARF-based fixed point types Joel Brobecker
2020-11-22 15:33       ` Eli Zaretskii via Gdb-patches
2020-11-24  3:12         ` Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 3/4] gdb/README: Document the --with-libgmp-prefix configure option Joel Brobecker
2020-11-22 15:32       ` Eli Zaretskii via Gdb-patches
2020-11-24  3:11         ` Joel Brobecker
2020-11-22 11:56     ` [RFA/doco 4/4] gdb/README: Fix the URL of the MPFR website (now https) Joel Brobecker
2020-11-22 15:33       ` Eli Zaretskii via Gdb-patches
2020-11-24  3:11         ` Joel Brobecker
2020-11-15  8:49 ` RFA: Various enhancements to the fixed-point support implementation Joel Brobecker
2020-11-15  8:49   ` [RFA 1/6] change gmp_string_asprintf to return an std::string Joel Brobecker
2020-11-16  0:41     ` Simon Marchi
2020-11-16  3:55       ` Joel Brobecker
2020-11-16 20:10         ` Simon Marchi
2020-11-15  8:49   ` [RFA 2/6] gmp-utils: Convert the read/write methods to using gdb::array_view Joel Brobecker
2020-11-16  0:52     ` Simon Marchi
2020-11-16 23:05       ` Pedro Alves
2020-11-17 14:32         ` Simon Marchi
2020-11-15  8:49   ` [RFA 3/6] gdbtypes.h: Get rid of the TYPE_FIXED_POINT_INFO macro Joel Brobecker
2020-11-15  8:49   ` [RFA 4/6] Make fixed_point_type_base_type a method of struct type Joel Brobecker
2020-11-15  8:49   ` [RFA 5/6] Make function fixed_point_scaling_factor " Joel Brobecker
2020-11-15  8:49   ` [RFA 6/6] valarith.c: Replace INIT_VAL_WITH_FIXED_POINT_VAL macro by lambda Joel Brobecker
2020-11-16  1:01   ` RFA: Various enhancements to the fixed-point support implementation Simon Marchi
2020-11-22 11:14   ` RFA v2: " Joel Brobecker
2020-11-22 11:14     ` [RFA v2 1/6] change and rename gmp_string_asprintf to return an std::string Joel Brobecker
2020-11-22 11:14     ` [RFA v2 2/6] gmp-utils: Convert the read/write methods to using gdb::array_view Joel Brobecker
2020-11-22 11:14     ` [RFA v2 3/6] gdbtypes.h: Get rid of the TYPE_FIXED_POINT_INFO macro Joel Brobecker
2020-11-22 11:14     ` [RFA v2 4/6] Make fixed_point_type_base_type a method of struct type Joel Brobecker
2020-11-22 11:14     ` [RFA v2 5/6] Make function fixed_point_scaling_factor " Joel Brobecker
2020-11-22 11:14     ` [RFA v2 6/6] valarith.c: Replace INIT_VAL_WITH_FIXED_POINT_VAL macro by lambda Joel Brobecker
2020-11-23 16:46     ` RFA v2: Various enhancements to the fixed-point support implementation Simon Marchi
2020-11-24  2:56       ` Joel Brobecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2966f3cf-955c-1849-e7ab-f01843cb7a33@simark.ca \
    --to=simark@simark.ca \
    --cc=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox