/*
* Sherlock Library -- Fast File Buffering
*
- * (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ * (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
*/
#include <stdio.h>
#include "lib.h"
#include "fastbuf.h"
+#include "lfs.h"
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",
}
}
-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)
{
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;
#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
#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)
/*
* Sherlock Library -- Fast File Buffering
*
- * (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ * (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
*/
#ifndef EOF
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 */
};
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);
}
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)
{
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)
{
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