die("%s: Line too long", f->name);
}
-int
-bdirect_read_prepare(struct fastbuf *f, byte **buf)
-{
- if (f->bptr == f->bstop && !f->refill(f))
- return EOF;
- *buf = f->bptr;
- return f->bstop - f->bptr;
-}
-
void
-bdirect_read_commit(struct fastbuf *f, byte *pos)
-{
- f->bptr = pos;
-}
-
-int
-bdirect_write_prepare(struct fastbuf *f, byte **buf)
+bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l)
{
- if (f->bptr == f->bufend)
- f->spout(f);
- *buf = f->bptr;
- return f->bufend - f->bptr;
-}
+ while (l)
+ {
+ byte *fptr, *tptr;
+ uns favail, tavail, n;
-void
-bdirect_write_commit(struct fastbuf *f, byte *pos)
-{
- f->bptr = pos;
+ favail = bdirect_read_prepare(f, &fptr);
+ if (favail == (uns)EOF)
+ die("bbcopy: source exhausted");
+ tavail = bdirect_write_prepare(t, &tptr);
+ n = MIN(l, favail);
+ n = MIN(n, tavail);
+ memcpy(tptr, fptr, n);
+ bdirect_read_commit(f, fptr + n);
+ bdirect_write_commit(t, tptr + n);
+ l -= n;
+ }
}
struct fastbuf *bopen_tmp(uns buffer);
struct fastbuf *bfdopen(int fd, uns buffer);
struct fastbuf *bfdopen_shared(int fd, uns buffer);
-void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
#define FB_IS_TEMP_FILE(f) FB_FILE(f)->is_temp_file
/* FastIO on in-memory streams */
bputc(f, '\n');
}
+void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
+static inline void
+bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
+{
+ if (f->bptr + l <= f->bstop &&
+ t->bptr + l <= t->bufend)
+ {
+ memcpy(t->bptr, f->bptr, l);
+ t->bptr += l;
+ f->bptr += l;
+ }
+ else
+ bbcopy_slow(f, t, l);
+}
+
/* I/O on addr_int_t */
#ifdef CPU_64BIT_POINTERS
/* Direct I/O on buffers */
-int bdirect_read_prepare(struct fastbuf *f, byte **buf);
-void bdirect_read_commit(struct fastbuf *f, byte *pos);
-int bdirect_write_prepare(struct fastbuf *f, byte **buf);
-void bdirect_write_commit(struct fastbuf *f, byte *pos);
+static inline int
+bdirect_read_prepare(struct fastbuf *f, byte **buf)
+{
+ if (f->bptr == f->bstop && !f->refill(f))
+ return EOF;
+ *buf = f->bptr;
+ return f->bstop - f->bptr;
+}
+
+static inline void
+bdirect_read_commit(struct fastbuf *f, byte *pos)
+{
+ f->bptr = pos;
+}
+
+static inline int
+bdirect_write_prepare(struct fastbuf *f, byte **buf)
+{
+ if (f->bptr == f->bufend)
+ f->spout(f);
+ *buf = f->bptr;
+ return f->bufend - f->bptr;
+}
+
+static inline void
+bdirect_write_commit(struct fastbuf *f, byte *pos)
+{
+ f->bptr = pos;
+}
#endif
return f;
}
-void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
-{
- uns rf = f->bstop - f->bptr;
- uns tbuflen = t->bufend - t->buffer;
-
- ASSERT(f->close == bfd_close);
- ASSERT(t->close == bfd_close);
- if (!l)
- return;
- if (rf)
- {
- uns k = MIN(rf, l);
- bwrite(t, f->bptr, k);
- f->bptr += k;
- l -= k;
- if (!l)
- return;
- }
- while (l >= tbuflen)
- {
- t->spout(t);
- if ((uns) read(FB_FILE(f)->fd, t->buffer, tbuflen) != tbuflen)
- die("bbcopy: %s exhausted", f->name);
- f->pos += tbuflen;
- f->bstop = f->bptr = f->buffer;
- t->bptr = t->bufend;
- l -= tbuflen;
- }
- while (l)
- {
- uns k = t->bufend - t->bptr;
-
- if (!k)
- {
- t->spout(t);
- k = t->bufend - t->bptr;
- }
- if (k > l)
- k = l;
- bread(f, t->bptr, k);
- t->bptr += k;
- l -= k;
- }
-}
-
#ifdef TEST
int main(int argc, char **argv)