From: Martin Mares Date: Mon, 28 Jul 1997 20:38:50 +0000 (+0000) Subject: New buffering code and objget caching changes. X-Git-Tag: holmes-import~1694 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=cdcccde56995c2c1263c4d209fefff596c8731d3;p=libucw.git New buffering code and objget caching changes. --- diff --git a/lib/fastbuf.c b/lib/fastbuf.c index 9756c978..4c0bf6db 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -89,24 +89,20 @@ void bflush(struct fastbuf *f) { if (f->bptr != f->buffer) { /* Have something to flush */ - if (f->bstop > f->buffer) - { /* And it's read data */ + if (f->bstop > f->buffer) /* Read data? */ + { f->bptr = f->bstop = f->buffer; + f->pos = f->fdpos; } - else - { /* Write data */ - wrbuf(f); - } + else /* Write data... */ + wrbuf(f); } } inline void bsetpos(struct fastbuf *f, uns pos) { - if (pos >= f->pos - && (pos <= f->pos + (f->bptr - f->buffer) || pos <= f->pos + (f->bstop - f->buffer))) - { - f->bptr = f->buffer + (pos - f->pos); - } + if (pos >= f->pos && (pos <= f->pos + (f->bptr - f->buffer) || pos <= f->pos + (f->bstop - f->buffer))) + f->bptr = f->buffer + (pos - f->pos); else { bflush(f); @@ -147,6 +143,15 @@ int bgetc_slow(struct fastbuf *f) return *f->bptr++; } +int bpeekc_slow(struct fastbuf *f) +{ + if (f->bptr < f->bstop) + return *f->bptr; + if (!rdbuf(f)) + return EOF; + return *f->bptr; +} + void bputc_slow(struct fastbuf *f, byte c) { if (f->bptr >= f->bufend) @@ -246,6 +251,28 @@ void bwrite_slow(struct fastbuf *f, void *b, uns l) } } +byte * /* Non-standard */ +bgets(struct fastbuf *f, byte *b, uns l) +{ + byte *e = b + l - 1; + int k; + + k = bgetc(f); + if (k == EOF) + return NULL; + while (b < e) + { + if (k == '\n' || k == EOF) + { + *b = 0; + return b; + } + *b++ = k; + k = bgetc(f); + } + die("%s: Line too long", f->name); +} + void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l) { uns rf = f->bstop - f->bptr; diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 8195250e..ba9c42c5 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -37,6 +37,12 @@ extern inline int bgetc(struct fastbuf *f) return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f); } +int bpeekc_slow(struct fastbuf *f); +extern inline int bpeekc(struct fastbuf *f) +{ + return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f); +} + extern inline void bungetc(struct fastbuf *f, byte c) { *--f->bptr = c; @@ -54,99 +60,97 @@ extern inline void bputc(struct fastbuf *f, byte c) word bgetw_slow(struct fastbuf *f); extern inline word bgetw(struct fastbuf *f) { -#ifdef CPU_CAN_DO_UNALIGNED_WORDS word w; if (f->bptr + 2 <= f->bstop) { - w = * ((word *) f->bptr); - f->bptr += 2; - return w; - } - else - return bgetw_slow(f); + byte *p = f->bptr; +#ifdef CPU_CAN_DO_UNALIGNED_WORDS + w = * ((word *) p); #else - word w = bgetc(f); #ifdef CPU_BIG_ENDIAN - return (w << 8) | bgetc(f); + w = (p[0] << 8) | p[1]; #else - return w | (bgetc(f) << 8); + w = (p[1] << 8) | p[0]; #endif #endif + f->bptr += 2; + return w; + } + else + return bgetw_slow(f); } ulg bgetl_slow(struct fastbuf *f); extern inline ulg bgetl(struct fastbuf *f) { -#ifdef CPU_CAN_DO_UNALIGNED_LONGS ulg l; if (f->bptr + 4 <= f->bstop) { - l = * ((ulg *) f->bptr); - f->bptr += 4; - return l; - } - else - return bgetl_slow(f); + byte *p = f->bptr; +#ifdef CPU_CAN_DO_UNALIGNED_LONGS + l = * ((ulg *) p); #else - ulg l = bgetc(f); #ifdef CPU_BIG_ENDIAN - l = (l << 8) | bgetc(f); - l = (l << 8) | bgetc(f); - return (l << 8) | bgetc(f); + l = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; #else - l = (bgetc(f) << 8) | l; - l = (bgetc(f) << 16) | l; - return (bgetc(f) << 24) | l; + l = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; #endif #endif + f->bptr += 4; + return l; + } + else + return bgetl_slow(f); } void bputw_slow(struct fastbuf *f, word w); extern inline void bputw(struct fastbuf *f, word w) { -#ifdef CPU_CAN_DO_UNALIGNED_WORDS if (f->bptr + 2 <= f->bufend) { - * ((word *) f->bptr) = w; - f->bptr += 2; - } - else - bputw_slow(f, w); + byte *p = f->bptr; +#ifdef CPU_CAN_DO_UNALIGNED_WORDS + * ((word *) p) = w; #else #ifdef CPU_BIG_ENDIAN - bputc(f, w >> 8); - bputc(f, w); + p[0] = w >> 8U; + p[1] = w; #else - bputc(f, w); - bputc(f, w >> 8); + p[1] = w >> 8U; + p[0] = w; #endif #endif + f->bptr += 2; + } + else + bputw_slow(f, w); } void bputl_slow(struct fastbuf *f, ulg l); extern inline void bputl(struct fastbuf *f, ulg l) { -#ifdef CPU_CAN_DO_UNALIGNED_LONGS if (f->bptr + 4 <= f->bufend) { - * ((ulg *) f->bptr) = l; - f->bptr += 4; - } - else - bputl_slow(f, l); + byte *p = f->bptr; +#ifdef CPU_CAN_DO_UNALIGNED_LONGS + * ((ulg *) p) = l; #else #ifdef CPU_BIG_ENDIAN - bputc(f, l >> 24); - bputc(f, l >> 16); - bputc(f, l >> 8); - bputc(f, l); + p[0] = l >> 24U; + p[1] = l >> 16U; + p[2] = l >> 8U; + p[3] = l; #else - bputc(f, l); - bputc(f, l >> 8); - bputc(f, l >> 16); - bputc(f, l >> 24); + p[3] = l >> 24U; + p[2] = l >> 16U; + p[1] = l >> 8U; + p[0] = l; #endif #endif + f->bptr += 4; + } + else + bputl_slow(f, l); } void bread_slow(struct fastbuf *f, void *b, uns l); @@ -174,34 +178,12 @@ extern inline void bwrite(struct fastbuf *f, void *b, uns l) } void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l); - -extern inline byte * /* Non-standard */ -bgets(struct fastbuf *f, byte *b, uns l) -{ - byte *e = b + l - 1; - int k; - - k = bgetc(f); - if (k == EOF) - return NULL; - while (b < e) - { - if (k == '\n' || k == EOF) - { - *b = 0; - return b; - } - *b++ = k; - k = bgetc(f); - } - die("%s: Line too long", f->name); -} +byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */ extern inline void bputs(struct fastbuf *f, byte *b) { - while (*b) - bputc(f, *b++); + bwrite(f, b, strlen(b)); } extern inline void