bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFE] function to read a file descriptor


From: Debarshi Ray
Subject: Re: [RFE] function to read a file descriptor
Date: Wed, 20 Aug 2008 02:27:33 +0530

> FWIW, your current algorithm of adding BUFSIZ to size causes nonlinear
> amount of work done in realloc, for reading large files.  You might
> want to increase memory by a constant factor or BUFSIZ, whichever is
> larger (e.g., just like read-file does), and thus less often than you
> recv.

So the following has been changed to:

>>     if (nread < BUFSIZ)
>>       break;
>>     else
>>       *size += BUFSIZ;


      if (nread < BUFSIZ)
        break;
      else
        *size += (*size / 2 > BUFSIZ) ? *size / 2 : BUFSIZ;


Here is the entire function:

size_t
recvbuf (int sockfd, void **buffer, size_t *size)
{
  size_t count = 0;
  ssize_t nread;

  if (*buffer == NULL)
    *size = BUFSIZ;

  for (;;)
    {
      *buffer = xrealloc (*buffer, *size);
      nread = recv (sockfd, *buffer + count, BUFSIZ, 0);

      if (nread == -1)
        error (EXIT_FAILURE, errno, "recv");

      count += nread;

      if (nread < BUFSIZ)
        break;
      else
        *size += (*size / 2 > BUFSIZ) ? *size / 2 : BUFSIZ;
    }

  return count;
}

Happy hacking,
Debarshi




reply via email to

[Prev in Thread] Current Thread [Next in Thread]