X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Ffastbuf.h;h=abcf7118499f0a76a66b5ab1b4a0aa3e11f03e1f;hb=9ff2d1d3d98e39cfe57e38519427a7754d73cb6c;hp=c2e12419009b7cb07e8ac5e657ad84673b48170e;hpb=2cbbf299beda4d0f108e753d46341d89184dfd46;p=libucw.git diff --git a/lib/fastbuf.h b/lib/fastbuf.h index c2e12419..abcf7118 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -16,6 +16,7 @@ #endif #include +#include #include "lib/unaligned.h" @@ -81,11 +82,15 @@ struct fastbuf { /* FastIO on standard files (specify buffer size 0 to enable mmaping) */ struct fastbuf *bopen(byte *name, uns mode, uns buflen); +struct fastbuf *bopen_try(byte *name, uns mode, uns buflen); struct fastbuf *bopen_tmp(uns buflen); struct fastbuf *bfdopen(int fd, uns buflen); struct fastbuf *bfdopen_shared(int fd, uns buflen); void bfilesync(struct fastbuf *b); +#define TEMP_FILE_NAME_LEN 256 +void temp_file_name(byte *name); + /* FastIO on in-memory streams */ struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */ @@ -95,6 +100,14 @@ struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */ struct fastbuf *bopen_mm(byte *name, uns mode); +/* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */ + +struct asio_queue; +struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue); +struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue); +struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue); +struct fastbuf *fbdir_open_tmp(struct asio_queue *io_queue); + /* FastI on file descriptors with limit */ struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); @@ -111,9 +124,29 @@ fbbuf_count_written(struct fastbuf *f) /* FastIO on recyclable growing buffers */ -struct fastbuf *fbgbuf_create(unsigned basic_size); -void fbgbuf_write(struct fastbuf *b); /* Reset stream and prepare for writing */ -void fbgbuf_rewind(struct fastbuf *b); /* Close reading if needed and prepare for reading */ +struct fastbuf *fbgrow_create(unsigned basic_size); +void fbgrow_reset(struct fastbuf *b); /* Reset stream and prepare for writing */ +void fbgrow_rewind(struct fastbuf *b); /* Prepare for reading */ + +/* FastO with atomic writes for multi-threaded programs */ + +struct fb_atomic { + struct fastbuf fb; + struct fb_atomic_file *af; + byte *expected_max_bptr; + uns slack_size; +}; +#define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf) + +struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len); +void fbatomic_internal_write(struct fastbuf *b); + +static inline void +fbatomic_commit(struct fastbuf *b) +{ + if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr) + fbatomic_internal_write(b); +} /* Configuring stream parameters */ @@ -318,6 +351,20 @@ byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */ int bgets_nodie(struct fastbuf *f, byte *b, uns l); byte *bgets0(struct fastbuf *f, byte *b, uns l); +struct mempool; +struct bb_t; +uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit); +byte *bgets_mp(struct fastbuf *f, struct mempool *mp); + +struct bgets_stk_struct { + struct fastbuf *f; + byte *old_buf, *cur_buf, *src; + uns old_len, cur_len, src_len; +}; +void bgets_stk_init(struct bgets_stk_struct *s); +void bgets_stk_step(struct bgets_stk_struct *s); +#define bgets_stk(fb) ({ struct bgets_stk_struct _s; _s.f = (fb); for (bgets_stk_init(&_s); _s.cur_len; _s.cur_buf = alloca(_s.cur_len), bgets_stk_step(&_s)); _s.cur_buf; }) + static inline void bputs(struct fastbuf *f, byte *b) {