X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Ffastbuf.h;h=4f0d99d3190be67c6fad8ae22e4e11a46760167d;hb=06cac26f0d8895b5cb8fbe03ca96444dc4ac8d51;hp=2c1c1a10aca2c693119b9e2aadf58122638ca45d;hpb=804b539f2f9d9af87ffa7036fd2b181fb12d86f1;p=libucw.git diff --git a/ucw/fastbuf.h b/ucw/fastbuf.h index 2c1c1a10..4f0d99d3 100644 --- a/ucw/fastbuf.h +++ b/ucw/fastbuf.h @@ -1,7 +1,7 @@ /* * UCW Library -- Fast Buffered I/O * - * (c) 1997--2008 Martin Mares + * (c) 1997--2011 Martin Mares * (c) 2004 Robert Spalek * * This software may be freely distributed and used according to the terms @@ -101,6 +101,7 @@ * * - Initialization: * * out: `buffer <= bstop <= bptr <= bufend` (flushed). + * * @fb_tie() should be called on the newly created fastbuf. * * - `refill`: * * in: `buffer <= bstop <= bptr <= bufend` (reading or flushed). @@ -128,19 +129,31 @@ * for how it works. **/ struct fastbuf { - byte is_fastbuf[0]; /* Dummy field for checking of type casts */ byte *bptr, *bstop; /* State of the buffer */ byte *buffer, *bufend; /* Start and end of the buffer */ char *name; /* File name (used for error messages) */ ucw_off_t pos; /* Position of bstop in the file */ + uns flags; /* See enum fb_flags */ int (*refill)(struct fastbuf *); /* Get a buffer with new data, returns 0 on EOF */ void (*spout)(struct fastbuf *); /* Write buffer data to the file */ int (*seek)(struct fastbuf *, ucw_off_t, int);/* Slow path for @bseek(), buffer already flushed; returns success */ void (*close)(struct fastbuf *); /* Close the stream */ int (*config)(struct fastbuf *, uns, int); /* Configure the stream */ int can_overwrite_buffer; /* Can the buffer be altered? 0=never, 1=temporarily, 2=permanently */ + struct resource *res; /* The fastbuf can be tied to a resource pool */ }; +/** + * Fastbuf flags + */ +enum fb_flags { + FB_DEAD = 0x1, /* Some fastbuf's method has thrown an exception */ + FB_DIE_ON_EOF = 0x2, /* Most of read operations throw "fb.eof" on EOF */ +}; + +/** Tie a fastbuf to a resource in the current resource pool. Returns the pointer to the same fastbuf. **/ +struct fastbuf *fb_tie(struct fastbuf *b); /* Tie fastbuf to a resource if there is an active pool */ + /*** * === Fastbuf on files [[fbparam]] * @@ -181,7 +194,7 @@ extern struct fb_params fbpar_def; /** The default `fb_params` **/ * Use @params to select the fastbuf back-end and its parameters or * pass NULL if you are fine with defaults. * - * Dies if the file does not exist. + * Raises `ucw.fb.open` if the file does not exist. **/ struct fastbuf *bopen_file(const char *name, int mode, struct fb_params *params); struct fastbuf *bopen_file_try(const char *name, int mode, struct fb_params *params); /** Like bopen_file(), but returns NULL on failure. **/ @@ -327,19 +340,21 @@ struct fastbuf *fbmem_clone_read(struct fastbuf *f); /** Given a writing fastbuf * of the buffer temporarily. In this case, set @can_overwrite as described * in <>. If you do not care, keep @can_overwrite zero. * - * It is not possible to close this fastbuf. + * It is not possible to close this fastbuf. This implies that no tying to + * resources takes place. */ void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite); /** * Creates a write-only fastbuf which writes into a provided memory buffer. * The fastbuf structure is allocated by the caller and pointed to by @f. - * An attempt to write behind the end of the buffer dies. + * An attempt to write behind the end of the buffer causes the `ucw.fb.write` exception. * * Data are written directly into the buffer, so it is not necessary to call @bflush() * at any moment. * - * It is not possible to close this fastbuf. + * It is not possible to close this fastbuf. This implies that no tying to + * resources takes place. */ void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size); @@ -358,10 +373,20 @@ static inline uns fbbuf_count_written(struct fastbuf *f) /** Calculates, how man * At every moment, you can use `fastbuf->buffer` to gain access to the stream. ***/ +struct mempool; + struct fastbuf *fbgrow_create(unsigned basic_size); /** Create the growing buffer pre-allocated to @basic_size bytes. **/ +struct fastbuf *fbgrow_create_mp(struct mempool *mp, unsigned basic_size); /** Create the growing buffer pre-allocated to @basic_size bytes. **/ void fbgrow_reset(struct fastbuf *b); /** Reset stream and prepare for writing. **/ void fbgrow_rewind(struct fastbuf *b); /** Prepare for reading (of already written data). **/ +/** + * Can be used in any state of @b (for example when writing or after + * @fbgrow_rewind()) to return the pointer to internal buffer and its length in + * bytes. The returned buffer can be invalidated by further requests. + **/ +uns fbgrow_get_buf(struct fastbuf *b, byte **buf); + /*** * === Fastbuf on memory pools [[fbpool]] * @@ -369,14 +394,14 @@ void fbgrow_rewind(struct fastbuf *b); /** Prepare for reading (of already wri * buffer, but this time the buffer is allocated from within a memory pool. ***/ -struct mempool; struct fbpool { /** Structure for fastbufs & mempools. **/ struct fastbuf fb; struct mempool *mp; }; /** - * Initialize a new `fbpool`. The structure is allocated by the caller. + * Initialize a new `fbpool`. The structure is allocated by the caller, + * so bclose() should not be called and no resource tying takes place. **/ void fbpool_init(struct fbpool *fb); /** Initialize a new mempool fastbuf. **/ /** @@ -419,7 +444,6 @@ struct fb_atomic { byte *expected_max_bptr; uns slack_size; }; -#define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf) /** * Open an atomic fastbuf. @@ -449,6 +473,53 @@ static inline void fbatomic_commit(struct fastbuf *b) fbatomic_internal_write(b); } +/*** + * === Fastbufs atop other fastbufs [[fbmulti]] + * + * Imagine some code which does massive string processing. It takes an input + * buffer, writes a part of it into an output buffer, then some other string + * and then the remaining part of the input buffer. Or anything else where you + * copy all the data at each stage of the complicated process. + * + * This backend takes multiple fastbufs and concatenates them formally into + * one. You may then read them consecutively as they were one fastbuf at all. + * + * This backend is read-only. + * + * This backend is seekable iff all of the supplied fastbufs are seekable. + * + * You aren't allowed to do anything with the underlying buffers while these + * are connected into fbmulti. + * + * The fbmulti is inited by @fbmulti_create(). It returns an empty fbmulti. + * Then you call @fbmulti_append() for each fbmulti. + * + * If @bclose() is called on fbmulti, all the underlying buffers get closed + * recursively. + * + * If you want to keep an underlying fastbuf open after @bclose, just remove it + * by @fbmulti_remove where the second parameter is a pointer to the removed + * fastbuf. If you pass NULL, all the underlying fastbufs are removed. + * + * After @fbmulti_remove, the state of the fbmulti is undefined. The only allowed + * operation is either another @fbmulti_remove or @bclose on the fbmulti. + ***/ + +/** + * Create an empty fbmulti + **/ +struct fastbuf *fbmulti_create(void); + +/** + * Append a fb to fbmulti + **/ +void fbmulti_append(struct fastbuf *f, struct fastbuf *fb); + +/** + * Remove a fb from fbmulti + **/ +void fbmulti_remove(struct fastbuf *f, struct fastbuf *fb); + /*** === Configuring stream parameters [[bconfig]] ***/ enum bconfig_type { /** Parameters that could be configured. **/ @@ -465,6 +536,9 @@ int bconfig(struct fastbuf *f, uns type, int data); /** Configure a fastbuf. Ret * Can not be used for fastbufs not returned from function (initialized in a parameter, for example the one from `fbbuf_init_read`). */ void bclose(struct fastbuf *f); +void bthrow(struct fastbuf *f, const char *id, const char *fmt, ...) FORMAT_CHECK(printf,3,4) NONRET; /** Throw exception on a given fastbuf **/ +int brefill(struct fastbuf *f, int allow_eof); +void bspout(struct fastbuf *f); void bflush(struct fastbuf *f); /** Write data (if it makes any sense, do not use for in-memory buffers). **/ void bseek(struct fastbuf *f, ucw_off_t pos, int whence); /** Seek in the buffer. See `man fseek` for description of @whence. Only for seekable fastbufs. **/ void bsetpos(struct fastbuf *f, ucw_off_t pos); /** Set position to @pos bytes from beginning. Only for seekable fastbufs. **/ @@ -488,6 +562,12 @@ static inline int bpeekc(struct fastbuf *f) /** Return next character from the return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f); } +int beof_slow(struct fastbuf *f); +static inline int beof(struct fastbuf *f) /** Have I reached EOF? **/ +{ + return (f->bptr < f->bstop) ? 0 : beof_slow(f); +} + static inline void bungetc(struct fastbuf *f) /** Return last read character back. Only one back is guaranteed to work. **/ { f->bptr--; @@ -533,7 +613,7 @@ static inline uns bread(struct fastbuf *f, void *b, uns l) /** * Reads exactly @l bytes of data into @b. * If at the end of file, it returns 0. - * If there are data, but less than @l, it dies. + * If there are data, but less than @l, it raises `ucw.fb.eof`. */ static inline uns breadb(struct fastbuf *f, void *b, uns l) { @@ -562,7 +642,7 @@ static inline void bwrite(struct fastbuf *f, const void *b, uns l) /** Writes bu /** * Reads a line into @b and strips trailing `\n`. * Returns pointer to the terminating 0 or NULL on `EOF`. - * Dies if the line is longer than @l. + * Raises `ucw.fb.toolong` if the line is longer than @l. **/ char *bgets(struct fastbuf *f, char *b, uns l); char *bgets0(struct fastbuf *f, char *b, uns l); /** The same as @bgets(), but for 0-terminated strings. **/ @@ -576,7 +656,7 @@ struct mempool; struct bb_t; /** * Read a string, strip the trailing `\n` and store it into growing buffer @b. - * Dies if the line is longer than @limit. + * Raises `ucw.fb.toolong` if the line is longer than @limit. **/ uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit); /**