public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Pedro Alves <palves@redhat.com>
Cc: Tom Tromey <tom@tromey.com>,  gdb-patches@sourceware.org
Subject: Re: [RFC 2/2] Move gdb's xmalloc and friends to new file
Date: Wed, 05 Jun 2019 22:33:00 -0000	[thread overview]
Message-ID: <87ef47br1x.fsf@tromey.com> (raw)
In-Reply-To: <df6ca8a5-ea43-60ad-8010-6c44e21a9f31@redhat.com> (Pedro Alves's	message of "Wed, 5 Jun 2019 10:40:49 +0100")

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> This will be the first case of gdbserver building a file
Pedro> from gdb/ .  I suppose we could preserve the gdb/common/
Pedro> directory for such files.  But I guess moving it out of the
Pedro> way until gdb/common/ moves to top level helps.

Yeah.  We can shuffle it around again later if we want.

>> +   Copyright (C) 2019 Free Software Foundation, Inc.

Pedro> The file is new, but the contents aren't, and it's the contents
Pedro> that matter wrt to copyright years.

Fixed.

Pedro> There should be some comment here about why this is in a separate file
Pedro> instead of living in the common library, to help people that read
Pedro> the code from the tree without having this commit in context.

Also fixed.

>> +#include "common/common-defs.h"

Pedro> There should also be a comment explaining why this includes common-defs.h
Pedro> instead of defs.h.

Fixed.

>> +#include "libiberty.h"
>> +#include <cstdlib>

Pedro> Including <cstdlib> looks strange, given common-defs.h includes stdlib.h.
Pedro> Why did you need this?

Removed.

I've appended the new file.

I'll probably push these patches sometime soon.

Tom

/* Shared allocation functions for GDB, the GNU debugger.

   Copyright (C) 1986-2019 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/>.  */

/* This file is unusual.

   Because both libiberty and readline define xmalloc and friends, the
   functions in this file can't appear in a library -- that will cause
   link errors.

   And, because we want to turn the common code into a library, this
   file can't live there.

   So, it lives in gdb and is built separately by gdb and gdbserver.
   Please be aware of this when modifying it.

   This also explains why this file includes common-defs.h and not
   defs.h or server.h -- we'd prefer to avoid depending on the
   GDBSERVER define when possible, and for this file it seemed
   simple to do so.  */

#include "common/common-defs.h"
#include "libiberty.h"
#include "common/errors.h"

/* The xmalloc() (libiberty.h) family of memory management routines.

   These are like the ISO-C malloc() family except that they implement
   consistent semantics and guard against typical memory management
   problems.  */

/* NOTE: These are declared using PTR to ensure consistency with
   "libiberty.h".  xfree() is GDB local.  */

PTR                            /* ARI: PTR */
xmalloc (size_t size)
{
  void *val;

  /* See libiberty/xmalloc.c.  This function need's to match that's
     semantics.  It never returns NULL.  */
  if (size == 0)
    size = 1;

  val = malloc (size);         /* ARI: malloc */
  if (val == NULL)
    malloc_failure (size);

  return val;
}

PTR                              /* ARI: PTR */
xrealloc (PTR ptr, size_t size)          /* ARI: PTR */
{
  void *val;

  /* See libiberty/xmalloc.c.  This function need's to match that's
     semantics.  It never returns NULL.  */
  if (size == 0)
    size = 1;

  if (ptr != NULL)
    val = realloc (ptr, size);	/* ARI: realloc */
  else
    val = malloc (size);	        /* ARI: malloc */
  if (val == NULL)
    malloc_failure (size);

  return val;
}

PTR                            /* ARI: PTR */
xcalloc (size_t number, size_t size)
{
  void *mem;

  /* See libiberty/xmalloc.c.  This function need's to match that's
     semantics.  It never returns NULL.  */
  if (number == 0 || size == 0)
    {
      number = 1;
      size = 1;
    }

  mem = calloc (number, size);      /* ARI: xcalloc */
  if (mem == NULL)
    malloc_failure (number * size);

  return mem;
}

void
xmalloc_failed (size_t size)
{
  malloc_failure (size);
}

  reply	other threads:[~2019-06-05 22:33 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-30 21:30 [RFC 0/2] Let's discuss moving gdbserver to top-level Tom Tromey
2019-05-30 21:30 ` [RFC 2/2] Move gdb's xmalloc and friends to new file Tom Tromey
2019-06-03 15:03   ` Simon Marchi
2019-06-03 16:33     ` Tom Tromey
2019-06-05  9:40   ` Pedro Alves
2019-06-05 22:33     ` Tom Tromey [this message]
2019-06-17 15:45   ` Alan Hayward
2019-06-17 17:43     ` Tom Tromey
2019-06-17 18:37       ` Pedro Alves
2019-06-18  9:31         ` Alan Hayward
2019-07-03 16:18           ` Alan Hayward
2019-07-13 16:04             ` Tom Tromey
2019-07-16 19:47             ` Pedro Alves
2019-05-30 21:30 ` [RFC 1/2] Remove linux-waitpid.c debugging code Tom Tromey
2019-06-03 14:57   ` Simon Marchi
2019-06-03 16:32     ` Tom Tromey
2019-06-05  9:32       ` Pedro Alves
2019-06-03 10:24 ` [RFC 0/2] Let's discuss moving gdbserver to top-level Alan Hayward
2019-06-03 13:13   ` Tom Tromey
2019-06-03 14:27 ` Simon Marchi
2019-06-03 16:30   ` Tom Tromey
2019-06-05  9:16 ` Pedro Alves

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=87ef47br1x.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).