summaryrefslogtreecommitdiff
path: root/src/upb_stdio.h
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2011-07-14 23:15:00 -0700
committerJoshua Haberman <jhaberman@gmail.com>2011-07-14 23:15:00 -0700
commit6a1f3a66939308668ab8dce0d195afec16e02af9 (patch)
tree8d1236c0d7269caa1ece95bfe584afe9b550c006 /src/upb_stdio.h
parent559e23c796f973a65d05c76e211835b126ee8ac8 (diff)
Major refactoring: upb_string is gone in favor of upb_strref.
Diffstat (limited to 'src/upb_stdio.h')
-rw-r--r--src/upb_stdio.h54
1 files changed, 41 insertions, 13 deletions
diff --git a/src/upb_stdio.h b/src/upb_stdio.h
index a164821..858830c 100644
--- a/src/upb_stdio.h
+++ b/src/upb_stdio.h
@@ -5,7 +5,12 @@
* Author: Josh Haberman <jhaberman@gmail.com>
*
* This file provides upb_bytesrc and upb_bytesink implementations for
- * ANSI C stdio.
+ * ANSI C stdio, which is less efficient than posixfd, but more portable.
+ *
+ * Specifically, stdio functions acquire locks on every operation (unless you
+ * use the f{read,write,...}_unlocked variants, which are not standard) and
+ * performs redundant buffering (unless you disable it with setvbuf(), but we
+ * can only do this on newly-opened filehandles).
*/
#include <stdio.h>
@@ -18,21 +23,44 @@
extern "C" {
#endif
-struct upb_stdio;
-typedef struct upb_stdio upb_stdio;
+typedef struct {
+ uint64_t ofs;
+ uint32_t refcount;
+ char data[];
+} upb_stdio_buf;
+
+// We use a single object for both bytesrc and bytesink for simplicity.
+// The object is still not thread-safe, and may only be used by one reader
+// and one writer at a time.
+typedef struct {
+ upb_bytesrc src;
+ upb_bytesink sink;
+ FILE *file;
+ bool should_close;
+ upb_stdio_buf **bufs;
+ uint32_t nbuf, szbuf;
+} upb_stdio;
+
+void upb_stdio_init(upb_stdio *stdio);
+// Caller should call upb_stdio_flush prior to calling this to ensure that
+// all data is flushed, otherwise data can be silently dropped if an error
+// occurs flushing the remaining buffers.
+void upb_stdio_uninit(upb_stdio *stdio);
+
+// Resets the object to read/write to the given "file." The caller is
+// responsible for closing the file, which must outlive this object.
+void upb_stdio_reset(upb_stdio *stdio, FILE *file);
-// Creation/deletion.
-upb_stdio *upb_stdio_new();
-void upb_stdio_free(upb_stdio *stdio);
+// As an alternative to upb_stdio_reset(), initializes the object by opening a
+// file, and will handle closing it. This may result in more efficient I/O
+// than the previous since we can call setvbuf() to disable buffering.
+void upb_stdio_open(upb_stdio *stdio, const char *filename, const char *mode,
+ upb_status *s);
-// Reset/initialize the object for use. The src or sink will call
-// fread()/fwrite()/etc. on the given FILE*.
-void upb_stdio_reset(upb_stdio *stdio, FILE* file);
+// Must be called to cleanup after the object, including closing the file if
+// it was opened with upb_stdio_open() (which can fail, hence the status).
+//
-// Gets a bytesrc or bytesink for the given stdio. The returned pointer is
-// invalidated by upb_stdio_reset above. It is perfectly valid to get both
-// a bytesrc and a bytesink for the same stdio if the FILE* is open for reading
-// and writing.
upb_bytesrc *upb_stdio_bytesrc(upb_stdio *stdio);
upb_bytesink *upb_stdio_bytesink(upb_stdio *stdio);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback