the flushed buffer, which is not necessarily the same as after the data you've
really read.
+Most fastbuf back-ends also participate in the libucw resource management system.
+If you have a resource pool active, newly created fastbufs are automatically tied
+to resources in the pool, so when the pool gets cleaned up, the fastbufs are
+freed, too. The bclose() function is still available and it removes the tie
+as needed.
+
.Back-ends:
- <<fbparam,Files (parametrized)>>
- <<fbfile,Regular files>>
#include "ucw/lib.h"
#include "ucw/fastbuf.h"
+#include "ucw/respool.h"
#include <stdio.h>
#include <stdlib.h>
bflush(f);
if (f->close)
f->close(f);
+ if (f->res)
+ res_drop(f->res);
}
}
bsetpos(f, pos);
return len;
}
+
+/* Resources */
+
+static void
+fb_res_detach(struct resource *r)
+{
+ struct fastbuf *f = r->priv;
+ f->res = NULL;
+}
+
+static void
+fb_res_free(struct resource *r)
+{
+ struct fastbuf *f = r->priv;
+ f->res = NULL;
+ bclose(f);
+}
+
+static void
+fb_res_dump(struct resource *r)
+{
+ struct fastbuf *f = r->priv;
+ printf(" name=%s", f->name);
+}
+
+static const struct res_class fb_res_class = {
+ .name = "fastbuf",
+ .detach = fb_res_detach,
+ .dump = fb_res_dump,
+ .free = fb_res_free,
+};
+
+void
+fb_tie(struct fastbuf *f)
+{
+ f->res = res_new(&fb_res_class, f);
+}
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 */
};
+void fb_tie(struct fastbuf *b); /* Tie fastbuf to a resource if there is an active pool */
+
/***
* === Fastbuf on files [[fbparam]]
*
* If you want to use fastbufs to access files, you can choose one of several
* back-ends and set their parameters.
+ *
+ * All file fastbufs are tied to resources automatically.
***/
/**
*
* The `fblim` back-end reads from a file handle, but at most a given
* number of bytes. This is frequently used for reading from sockets.
+ *
+ * All such fastbufs are tied to resources automatically.
***/
struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); /** Create a fastbuf which reads at most @limit bytes from @fd. **/
* First, you use @fbmem_create() to create the stream and the fastbuf
* used for writing to it. Then you can call @fbmem_clone_read() to get
* an arbitrary number of fastbuf for reading from the stream.
+ *
+ * All in-memory fastbufs are tied to resources automatically.
***/
struct fastbuf *fbmem_create(uns blocksize); /** Create stream and return its writing fastbuf. **/
* of the buffer temporarily. In this case, set @can_overwrite as described
* in <<internal,Internals>>. 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);
* 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);
* size and it is expanded to accomodate all data.
*
* At every moment, you can use `fastbuf->buffer` to gain access to the stream.
+ *
+ * All fastbufs of this type are tied to resources automatically.
***/
struct fastbuf *fbgrow_create(unsigned basic_size); /** Create the growing buffer pre-allocated to @basic_size bytes. **/
};
/**
- * 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. **/
/**
*
* Please note that initialization of the clones is not thread-safe,
* so you have to serialize it yourself.
+ *
+ * The atomic fastbufs are tied to resources automatically.
***/
struct fb_atomic {