Hi

On Fri, Feb 19, 2010 at 06:21:30AM +0100, Fulvio Ciriaco wrote:
> Yes, this is quite a more linear patch.
> It has one defect, in my opinion, i.e. that it does
> buffer-size reallocations, usually this is avoided
> by increasing the buffer by a factor 2 rather than increasing
> it by 1 byte. That amounts to changing

This is probably an unnecessary optimisation, nobody is going to be loading a
huge file here, and modern allocators tend to allocate blocks anyway.

I think it may be better just to stick with simpler code in this case.

The diff should perhaps check for overflow however, in case anyone does do
anything silly.

> ------------------------------------------------------
>       pdata = NULL;
>       psize = 0;
>       while ((ch = getc(f)) != EOF) {
>               /* Do not let the server die due to memory exhaustion. */
>               if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
>                       ctx->error(ctx, "realloc error: %s", strerror(errno));
>                       goto error;
>               }
>               pdata = new_pdata;
>               pdata[psize++] = ch;
>       }
> -----------------------------------------------
> with
> -----------------------------------------------
>       pdata = NULL;
>       psize = 0;
>   bufsize=16;
>   if ((new_pdata = malloc(pdata, bufsize)) == NULL) {
>                       ctx->error(ctx, "realloc error: %s", strerror(errno));
>                       goto error;
>               }
>       while ((ch = getc(f)) != EOF) {
>     if (psize+2>bufsize) {
>       bufsize*=2;
>                 /* Do not let the server die due to memory exhaustion. */
>                 if ((new_pdata = realloc(pdata, bufsize)) == NULL) {
>                         ctx->error(ctx, "realloc error: %s", strerror(errno));
>                         goto error;
>                 }
>                 pdata = new_pdata;
>     }
>               pdata[psize++] = ch;
>       }
> ----------------------------------------------------
> Fulvio
> 
> At Thu, 18 Feb 2010 23:20:19 +0000,
> Nicholas Marriott wrote:
> > 
> > Hi
> > 
> > Thanks for the diff.
> > 
> > Tiago Cunha who originally wrote the load-buffer command sent me an update 
> > to
> > do this as well which is quite similar to yours but a bit simpler, so we 
> > will
> > probably use it. I've attached it below as well if you want to take a look.
> > 
> > You can't just use stdin because the loadb command is running in the server 
> > so
> > stdin doesn't exist.
> > 
> > At the moment the client doesn't send the stdin fd up to the client unless 
> > it
> > is a tty, we could change that by removing the isatty() check in client.c.
> > 
> > There may be a couple of things that need checked after that, eg would 
> > probably
> > need an isatty() check in tty_open() and maybe elsewhere.
> > 
> > Once that is done, the load-buffer command could check that ctx->cmdclient 
> > is
> > non-NULL. If it is then the command was run from the command-line and the 
> > tty
> > fd will be in c->tty.fd.
> > 
> > Then it should be able to call isatty() itself, then freopen() on that fd 
> > if it
> > isn't a tty, and read it.
> > 
> > It might be nice to have stdin available for other things as well so this 
> > could
> > be worth looking into.
> > 
> > 
> > --- cmd-load-buffer.c.orig  2010-02-18 21:44:50.539052646 +0000
> > +++ cmd-load-buffer.c       2010-02-18 21:50:52.395061286 +0000
> > @@ -16,13 +16,10 @@
> >   * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> >   */
> >  
> > -#include <sys/types.h>
> > -#include <sys/stat.h>
> > -
> >  #include <errno.h>
> > +#include <stdio.h>
> >  #include <stdlib.h>
> >  #include <string.h>
> > -#include <unistd.h>
> >  
> >  #include "tmux.h"
> >  
> > @@ -48,11 +45,11 @@
> >  {
> >     struct cmd_buffer_data  *data = self->data;
> >     struct session          *s;
> > -   struct stat              sb;
> >     FILE                    *f;
> > -   char                    *pdata = NULL;
> > +   char                    *pdata, *new_pdata;
> >     size_t                   psize;
> >     u_int                    limit;
> > +   int                      ch;
> >  
> >     if ((s = cmd_find_session(ctx, data->target)) == NULL)
> >             return (-1);
> > @@ -62,29 +59,23 @@
> >             return (-1);
> >     }
> >  
> > -   if (fstat(fileno(f), &sb) < 0) {
> > -           ctx->error(ctx, "%s: %s", data->arg, strerror(errno));
> > -           goto error;
> > +   pdata = NULL;
> > +   psize = 0;
> > +   while ((ch = getc(f)) != EOF) {
> > +           /* Do not let the server die due to memory exhaustion. */
> > +           if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
> > +                   ctx->error(ctx, "realloc error: %s", strerror(errno));
> > +                   goto error;
> > +           }
> > +           pdata = new_pdata;
> > +           pdata[psize++] = ch;
> >     }
> > -   if (sb.st_size <= 0 || (uintmax_t) sb.st_size > SIZE_MAX) {
> > -           ctx->error(ctx, "%s: file empty or too large", data->arg);
> > -           goto error;
> > -   }
> > -   psize = (size_t) sb.st_size;
> > -
> > -   /*
> > -    * We don't want to die due to memory exhaustion, hence xmalloc can't
> > -    * be used here.
> > -    */
> > -   if ((pdata = malloc(psize)) == NULL) {
> > -           ctx->error(ctx, "malloc error: %s", strerror(errno));
> > -           goto error;
> > -   }
> > -
> > -   if (fread(pdata, 1, psize, f) != psize) {
> > -           ctx->error(ctx, "%s: fread error", data->arg);
> > +   if (ferror(f)) {
> > +           ctx->error(ctx, "%s: %s", data->arg, strerror(errno));
> >             goto error;
> >     }
> > +   if (pdata != NULL)
> > +           pdata[psize] = '\0';
> >  
> >     fclose(f);
> >  

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to