From c8f4b2b31f28fc981da92536e4bede7712a0ba81 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 24 Oct 1999 19:59:21 +0000 Subject: [PATCH] Large File Support in fastbuf library. --- lib/fastbuf.c | 70 ++++++++++++++++++++++++++++++++++---- lib/fastbuf.h | 94 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 151 insertions(+), 13 deletions(-) diff --git a/lib/fastbuf.c b/lib/fastbuf.c index d420b3c9..ee7d6346 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -1,7 +1,7 @@ /* * Sherlock Library -- Fast File Buffering * - * (c) 1997 Martin Mares, + * (c) 1997--1999 Martin Mares */ #include @@ -12,6 +12,7 @@ #include "lib.h" #include "fastbuf.h" +#include "lfs.h" struct fastbuf *__bfdopen(int fd, uns buffer, byte *name) { @@ -30,7 +31,12 @@ struct fastbuf *__bfdopen(int fd, uns buffer, byte *name) struct fastbuf * bopen(byte *name, uns mode, uns buffer) { - int fd = open(name, mode, 0666); + int fd; + +#ifdef SHERLOCK_CONFIG_LFS + mode |= O_LARGEFILE; +#endif + fd = open(name, mode, 0666); if (fd < 0) die("Unable to %s file %s: %m", @@ -99,22 +105,22 @@ void bflush(struct fastbuf *f) } } -inline void bsetpos(struct fastbuf *f, uns pos) +inline void bsetpos(struct fastbuf *f, sh_off_t 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); - if (f->fdpos != pos && lseek(f->fd, pos, SEEK_SET) < 0) + if (f->fdpos != pos && sh_seek(f->fd, pos, SEEK_SET) < 0) die("lseek on %s: %m", f->name); f->fdpos = f->pos = pos; } } -void bseek(struct fastbuf *f, uns pos, int whence) +void bseek(struct fastbuf *f, sh_off_t pos, int whence) { - int l; + sh_off_t l; switch (whence) { @@ -124,7 +130,7 @@ void bseek(struct fastbuf *f, uns pos, int whence) return bsetpos(f, btell(f) + pos); case SEEK_END: bflush(f); - l = lseek(f->fd, pos, whence); + l = sh_seek(f->fd, pos, whence); if (l < 0) die("lseek on %s: %m", f->name); f->fdpos = f->pos = l; @@ -183,6 +189,32 @@ u32 bgetl_slow(struct fastbuf *f) #endif } +u64 bgetq_slow(struct fastbuf *f) +{ + u32 l, h; +#ifdef CPU_BIG_ENDIAN + h = bgetl_slow(f); + l = bgetl_slow(f); +#else + l = bgetl_slow(f); + h = bgetl_slow(f); +#endif + return ((u64) h << 32) | l; +} + +u64 bget5_slow(struct fastbuf *f) +{ + u32 l, h; +#ifdef CPU_BIG_ENDIAN + h = bgetc_slow(f); + l = bgetl_slow(f); +#else + l = bgetl_slow(f); + h = bgetc_slow(f); +#endif + return ((u64) h << 32) | l; +} + void bputw_slow(struct fastbuf *f, word w) { #ifdef CPU_BIG_ENDIAN @@ -209,6 +241,30 @@ void bputl_slow(struct fastbuf *f, u32 l) #endif } +void bputq_slow(struct fastbuf *f, u64 q) +{ +#ifdef CPU_BIG_ENDIAN + bputl_slow(f, q >> 32); + bputl_slow(f, q); +#else + bputl_slow(f, q); + bputl_slow(f, q >> 32); +#endif +} + +void bput5_slow(struct fastbuf *f, u64 o) +{ + u32 hi = o >> 32; + u32 low = o; +#ifdef CPU_BIG_ENDIAN + bputc_slow(f, hi); + bputl_slow(f, low); +#else + bputl_slow(f, low); + bputc_slow(f, hi); +#endif +} + void bread_slow(struct fastbuf *f, void *b, uns l) { while (l) diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 3bc2b72f..fc0bf0b3 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -1,7 +1,7 @@ /* * Sherlock Library -- Fast File Buffering * - * (c) 1997 Martin Mares, + * (c) 1997--1999 Martin Mares */ #ifndef EOF @@ -13,8 +13,8 @@ struct fastbuf { byte *buffer, *bufend; /* Start and end of the buffer */ byte *name; /* File name for error messages */ uns buflen; /* Size of standard portion of the buffer */ - uns pos; /* Position of bptr in the file */ - uns fdpos; /* Current position in the file */ + sh_off_t pos; /* Position of bptr in the file */ + sh_off_t fdpos; /* Current position in the file */ int fd; /* File descriptor */ }; @@ -23,10 +23,10 @@ struct fastbuf *bfdopen(int fd, uns buffer); void bclose(struct fastbuf *f); void bflush(struct fastbuf *f); -void bseek(struct fastbuf *f, uns pos, int whence); -void bsetpos(struct fastbuf *f, uns pos); +void bseek(struct fastbuf *f, sh_off_t pos, int whence); +void bsetpos(struct fastbuf *f, sh_off_t pos); -extern inline uns btell(struct fastbuf *f) +extern inline sh_off_t btell(struct fastbuf *f) { return f->pos + (f->bptr - f->buffer); } @@ -103,6 +103,39 @@ extern inline u32 bgetl(struct fastbuf *f) return bgetl_slow(f); } +u64 bgetq_slow(struct fastbuf *f); +extern inline u64 bgetq(struct fastbuf *f) +{ + if (f->bptr + 8 <= f->bstop) + { + u64 l; + memcpy(&l, f->bptr, 8); + f->bptr += 8; + return l; + } + else + return bgetq_slow(f); +} + +u64 bget5_slow(struct fastbuf *f); +extern inline u64 bget5(struct fastbuf *f) +{ + u64 l; + if (f->bptr + 5 <= f->bstop) + { + byte *p = f->bptr; +#ifdef CPU_BIG_ENDIAN + l = ((u64)p[0] << 32) | ((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]); +#else + l = ((u64)p[4] << 32) | ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); +#endif + f->bptr += 5; + return l; + } + else + return bget5_slow(f); +} + void bputw_slow(struct fastbuf *f, word w); extern inline void bputw(struct fastbuf *f, word w) { @@ -153,6 +186,44 @@ extern inline void bputl(struct fastbuf *f, u32 l) bputl_slow(f, l); } +void bputq_slow(struct fastbuf *f, u64 l); +extern inline void bputq(struct fastbuf *f, u64 l) +{ + if (f->bptr + 8 <= f->bufend) + { + memcpy(f->bptr, &l, 8); + f->bptr += 8; + } + else + bputq_slow(f, l); +} + +void bput5_slow(struct fastbuf *f, u64 l); +extern inline void bput5(struct fastbuf *f, u64 l) +{ + if (f->bptr + 5 <= f->bufend) + { + byte *p = f->bptr; + u32 low = l; +#ifdef CPU_BIG_ENDIAN + p[0] = l >> 32; + p[1] = low >> 24U; + p[2] = low >> 16U; + p[3] = low >> 8U; + p[4] = low; +#else + p[4] = l >> 32; + p[3] = low >> 24U; + p[2] = low >> 16U; + p[1] = low >> 8U; + p[0] = low; +#endif + f->bptr += 5; + } + else + bput5_slow(f, l); +} + void bread_slow(struct fastbuf *f, void *b, uns l); extern inline void bread(struct fastbuf *f, void *b, uns l) { @@ -193,3 +264,14 @@ bputsn(struct fastbuf *f, byte *b) bputc(f, '\n'); } +#ifdef SHERLOCK_CONFIG_LARGE_DB +#define bgeto(f) bget5(f) +#define bputo(f,l) bput5(f,l) +#define bgetp(f) bgetq(f) +#define bputp(f,l) bputq(f,l) +#else +#define bgeto(f) bgetl(f) +#define bputo(f,l) bputl(f,l) +#define bgetp(f) bgetl(f) +#define bputp(f,l) bputl(f,l) +#endif -- 2.39.2