unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org,
	Samuel Thibault <samuel.thibault@ens-lyon.org>
Subject: Re: [PATCH 1/3] <fd_to_filename.h>: Add type safety and port to Hurd
Date: Fri, 14 Feb 2020 12:28:52 -0800	[thread overview]
Message-ID: <61b49643-9c7b-7060-6eb7-21060dd6e22f@cs.ucla.edu> (raw)
In-Reply-To: <c6b3a5e519cd3b0d7d77c7f2dfe35384b36cb39a.1581703185.git.fweimer@redhat.com>

On 2/14/20 10:11 AM, Florian Weimer wrote:
> +  /* A positive int value has at most 10 decimal digits.  */
> +  char buffer[sizeof (FD_TO_FILENAME_PREFIX) + 10];

This should use 'INT_STRLEN_BOUND (int)' rather than '10', where 
INT_STRLEN_BOUND is taken from intprops.h. Then you don't need the 
comment (and the code won't break on future ILP64 platforms :-).

> +char *
> +__fd_to_filename (int descriptor, struct fd_to_filename *storage)
> +{
> +  char *p = mempcpy (storage->buffer, FD_TO_FILENAME_PREFIX,
> +                     strlen (FD_TO_FILENAME_PREFIX));
> +  if (__glibc_likely (descriptor >= 0))
> +    {
> +      if (descriptor < 1000)
> +        p = digit123 (p, descriptor);
> +      else if (descriptor < 1000 * 1000) ...

It's not clear what that "descriptor >= 0" test is doing. I assume that 
a precondition is that DESCRIPTOR is nonnegative; if so, this should be 
mentioned and the test removed. If it's not a precondition, the code 
should do the right thing if DESCRIPTOR is negative (I suppose, return a 
filename that cannot be opened, though it doesn't do that currently) -- 
which should also be documented but I think this'd be overkill.

As Adhemerval writes, there's no need for special cases for powers of 
1000 etc. Something like the following should do (this is a bit shorter 
than Adhemerval's version):

   /* Convert nonnegative DESCRIPTOR to a file name.  */

   char *
   __fd_to_filename (int descriptor, struct fd_to_filename *storage)
   {
     char *p = mempcpy (storage->buffer, FD_TO_FILENAME_PREFIX,
		       strlen (FD_TO_FILENAME_PREFIX) - 1);
     for (int d = descriptor; p++, (d /= 10) != 0; )
       continue;
     *p = '\0';
     for (int d = descriptor; *--p = '0' + d % 10, (d /= 10) != 0; )
       continue;
     return storage->buffer;
   }

GCC turns those divisions and remainders into the usual 
multiply+shift+add and this should be good enough.

  parent reply	other threads:[~2020-02-14 20:29 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-14 18:10 [PATCH 0/3] <fd_to_filename.h> improvements Florian Weimer
2020-02-14 18:11 ` [PATCH 1/3] <fd_to_filename.h>: Add type safety and port to Hurd Florian Weimer
2020-02-14 18:43   ` Samuel Thibault
2020-02-14 19:08   ` Adhemerval Zanella
2020-02-14 20:21     ` Florian Weimer
2020-02-14 20:28   ` Paul Eggert [this message]
2020-02-15 13:16     ` Florian Weimer
2020-02-16  0:02       ` Paul Eggert
2020-02-17 15:18         ` Florian Weimer
2020-02-17 18:26           ` Paul Eggert
2020-02-17 18:59             ` Adhemerval Zanella
2020-02-17 19:14               ` Paul Eggert
2020-02-14 18:11 ` [PATCH 2/3] Linux: Port ttyname, ttyname_r to <fd_file_name.h> Florian Weimer
2020-02-14 18:11 ` [PATCH 3/3] Linux: Port fexecve to <fd_to_filename.h> Florian Weimer

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

  List information: https://www.gnu.org/software/libc/involved.html

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

  git send-email \
    --in-reply-to=61b49643-9c7b-7060-6eb7-21060dd6e22f@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=samuel.thibault@ens-lyon.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).