diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/upb_bytestream.h | 123 | ||||
-rw-r--r-- | src/upb_decoder.c | 1 | ||||
-rw-r--r-- | src/upb_decoder.h | 2 | ||||
-rw-r--r-- | src/upb_handlers.c (renamed from src/upb_stream.c) | 2 | ||||
-rw-r--r-- | src/upb_handlers.h (renamed from src/upb_stream.h) | 101 | ||||
-rw-r--r-- | src/upb_msg.h | 2 | ||||
-rw-r--r-- | src/upb_stdio.h | 6 | ||||
-rw-r--r-- | src/upb_strstream.c | 1 | ||||
-rw-r--r-- | src/upb_strstream.h | 2 | ||||
-rw-r--r-- | src/upb_textprinter.h | 3 | ||||
-rw-r--r-- | tests/tests.c | 2 |
12 files changed, 136 insertions, 111 deletions
@@ -72,7 +72,7 @@ $(ALLSRC): perf-cppflags # The core library. CORE= \ src/upb.c \ - src/upb_stream.c \ + src/upb_handlers.c \ src/upb_table.c \ src/upb_string.c \ src/upb_def.c \ diff --git a/src/upb_bytestream.h b/src/upb_bytestream.h new file mode 100644 index 0000000..e4b51fd --- /dev/null +++ b/src/upb_bytestream.h @@ -0,0 +1,123 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2010-2011 Google Inc. See LICENSE for details. + * Author: Josh Haberman <jhaberman@gmail.com> + * + * Defines the interfaces upb_bytesrc and upb_bytesink, which are abstractions + * of read()/write() with useful buffering/sharing semantics. + */ + +#ifndef UPB_BYTESTREAM_H +#define UPB_BYTESTREAM_H + +#include <stdarg.h> +#include "upb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* upb_bytesrc ****************************************************************/ + +// upb_bytesrc is a pull interface for streams of bytes, basically an +// abstraction of read()/fread(), but it avoids copies where possible. + +typedef upb_strlen_t (*upb_bytesrc_read_fptr)( + upb_bytesrc *src, void *buf, upb_strlen_t count, upb_status *status); +typedef bool (*upb_bytesrc_getstr_fptr)( + upb_bytesrc *src, upb_string *str, upb_status *status); + +typedef struct { + upb_bytesrc_read_fptr read; + upb_bytesrc_getstr_fptr getstr; +} upb_bytesrc_vtbl; + +struct _upb_bytesrc { + upb_bytesrc_vtbl *vtbl; +}; + +INLINE void upb_bytesrc_init(upb_bytesrc *s, upb_bytesrc_vtbl *vtbl) { + s->vtbl = vtbl; +} + +// Reads up to "count" bytes into "buf", returning the total number of bytes +// read. If 0, indicates error and puts details in "status". +INLINE upb_strlen_t upb_bytesrc_read(upb_bytesrc *src, void *buf, + upb_strlen_t count, upb_status *status) { + return src->vtbl->read(src, buf, count, status); +} + +// Like upb_bytesrc_read(), but modifies "str" in-place. Caller must ensure +// that "str" is created or just recycled. Returns "false" if no data was +// returned, either due to error or EOF (check status for details). +// +// In comparison to upb_bytesrc_read(), this call can possibly alias existing +// string data (which avoids a copy). On the other hand, if the data was *not* +// already in an existing string, this copies it into a upb_string, and if the +// data needs to be put in a specific range of memory (because eg. you need to +// put it into a different kind of string object) then upb_bytesrc_get() could +// save you a copy. +INLINE bool upb_bytesrc_getstr(upb_bytesrc *src, upb_string *str, + upb_status *status) { + return src->vtbl->getstr(src, str, status); +} + + +/* upb_bytesink ***************************************************************/ + +struct _upb_bytesink; +typedef struct _upb_bytesink upb_bytesink; +typedef upb_strlen_t (*upb_bytesink_putstr_fptr)( + upb_bytesink *bytesink, upb_string *str, upb_status *status); +typedef upb_strlen_t (*upb_bytesink_vprintf_fptr)( + upb_bytesink *bytesink, upb_status *status, const char *fmt, va_list args); + +typedef struct { + upb_bytesink_putstr_fptr putstr; + upb_bytesink_vprintf_fptr vprintf; +} upb_bytesink_vtbl; + +struct _upb_bytesink { + upb_bytesink_vtbl *vtbl; +}; + +INLINE void upb_bytesink_init(upb_bytesink *s, upb_bytesink_vtbl *vtbl) { + s->vtbl = vtbl; +} + + +// TODO: Figure out how buffering should be handled. Should the caller buffer +// data and only call these functions when a buffer is full? Seems most +// efficient, but then buffering has to be configured in the caller, which +// could be anything, which makes it hard to have a standard interface for +// controlling buffering. +// +// The downside of having the bytesink buffer is efficiency: the caller is +// making more (virtual) function calls, and the caller can't arrange to have +// a big contiguous buffer. The bytesink can do this, but will have to copy +// to make the data contiguous. + +// Returns the number of bytes written. +INLINE upb_strlen_t upb_bytesink_printf(upb_bytesink *sink, upb_status *status, + const char *fmt, ...) { + va_list args; + va_start(args, fmt); + upb_strlen_t ret = sink->vtbl->vprintf(sink, status, fmt, args); + va_end(args); + return ret; +} + +// Puts the given string, returning true if the operation was successful, otherwise +// check "status" for details. Ownership of the string is *not* passed; if +// the callee wants a reference he must call upb_string_getref() on it. +INLINE upb_strlen_t upb_bytesink_putstr(upb_bytesink *sink, upb_string *str, + upb_status *status) { + return sink->vtbl->putstr(sink, str, status); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/src/upb_decoder.c b/src/upb_decoder.c index a10c0ba..9383474 100644 --- a/src/upb_decoder.c +++ b/src/upb_decoder.c @@ -8,6 +8,7 @@ #include <inttypes.h> #include <stddef.h> #include <stdlib.h> +#include "upb_bytestream.h" #include "upb_decoder.h" #include "upb_varint.h" diff --git a/src/upb_decoder.h b/src/upb_decoder.h index 954b33c..3d1b06b 100644 --- a/src/upb_decoder.h +++ b/src/upb_decoder.h @@ -20,7 +20,7 @@ #include <setjmp.h> #include <stdbool.h> #include <stdint.h> -#include "upb_stream.h" +#include "upb_handlers.h" #ifdef __cplusplus extern "C" { diff --git a/src/upb_stream.c b/src/upb_handlers.c index fe3a552..d80d59f 100644 --- a/src/upb_stream.c +++ b/src/upb_handlers.c @@ -6,7 +6,7 @@ */ #include <stdlib.h> -#include "upb_stream.h" +#include "upb_handlers.h" /* upb_handlers ***************************************************************/ diff --git a/src/upb_stream.h b/src/upb_handlers.h index e749964..d0ef1a4 100644 --- a/src/upb_stream.h +++ b/src/upb_handlers.h @@ -390,107 +390,6 @@ INLINE bool upb_dispatcher_stackempty(upb_dispatcher *d) { return d->top == d->stack; } -/* upb_bytesrc ****************************************************************/ - -// upb_bytesrc is a pull interface for streams of bytes, basically an -// abstraction of read()/fread(), but it avoids copies where possible. - -typedef upb_strlen_t (*upb_bytesrc_read_fptr)( - upb_bytesrc *src, void *buf, upb_strlen_t count, upb_status *status); -typedef bool (*upb_bytesrc_getstr_fptr)( - upb_bytesrc *src, upb_string *str, upb_status *status); - -typedef struct { - upb_bytesrc_read_fptr read; - upb_bytesrc_getstr_fptr getstr; -} upb_bytesrc_vtbl; - -struct _upb_bytesrc { - upb_bytesrc_vtbl *vtbl; -}; - -INLINE void upb_bytesrc_init(upb_bytesrc *s, upb_bytesrc_vtbl *vtbl) { - s->vtbl = vtbl; -} - -// Reads up to "count" bytes into "buf", returning the total number of bytes -// read. If 0, indicates error and puts details in "status". -INLINE upb_strlen_t upb_bytesrc_read(upb_bytesrc *src, void *buf, - upb_strlen_t count, upb_status *status) { - return src->vtbl->read(src, buf, count, status); -} - -// Like upb_bytesrc_read(), but modifies "str" in-place. Caller must ensure -// that "str" is created or just recycled. Returns "false" if no data was -// returned, either due to error or EOF (check status for details). -// -// In comparison to upb_bytesrc_read(), this call can possibly alias existing -// string data (which avoids a copy). On the other hand, if the data was *not* -// already in an existing string, this copies it into a upb_string, and if the -// data needs to be put in a specific range of memory (because eg. you need to -// put it into a different kind of string object) then upb_bytesrc_get() could -// save you a copy. -INLINE bool upb_bytesrc_getstr(upb_bytesrc *src, upb_string *str, - upb_status *status) { - return src->vtbl->getstr(src, str, status); -} - - -/* upb_bytesink ***************************************************************/ - -// upb_bytesink: push interface for streams of bytes, basically an abstraction -// of write()/fwrite(), but it avoids copies where possible. - -struct _upb_bytesink; -typedef struct _upb_bytesink upb_bytesink; -typedef upb_strlen_t (*upb_bytesink_putstr_fptr)( - upb_bytesink *bytesink, upb_string *str, upb_status *status); -typedef upb_strlen_t (*upb_bytesink_vprintf_fptr)( - upb_bytesink *bytesink, upb_status *status, const char *fmt, va_list args); - -typedef struct { - upb_bytesink_putstr_fptr putstr; - upb_bytesink_vprintf_fptr vprintf; -} upb_bytesink_vtbl; - -struct _upb_bytesink { - upb_bytesink_vtbl *vtbl; -}; - -INLINE void upb_bytesink_init(upb_bytesink *s, upb_bytesink_vtbl *vtbl) { - s->vtbl = vtbl; -} - - -// TODO: Figure out how buffering should be handled. Should the caller buffer -// data and only call these functions when a buffer is full? Seems most -// efficient, but then buffering has to be configured in the caller, which -// could be anything, which makes it hard to have a standard interface for -// controlling buffering. -// -// The downside of having the bytesink buffer is efficiency: the caller is -// making more (virtual) function calls, and the caller can't arrange to have -// a big contiguous buffer. The bytesink can do this, but will have to copy -// to make the data contiguous. - -// Returns the number of bytes written. -INLINE upb_strlen_t upb_bytesink_printf(upb_bytesink *sink, upb_status *status, - const char *fmt, ...) { - va_list args; - va_start(args, fmt); - upb_strlen_t ret = sink->vtbl->vprintf(sink, status, fmt, args); - va_end(args); - return ret; -} - -// Puts the given string, returning true if the operation was successful, otherwise -// check "status" for details. Ownership of the string is *not* passed; if -// the callee wants a reference he must call upb_string_getref() on it. -INLINE upb_strlen_t upb_bytesink_putstr(upb_bytesink *sink, upb_string *str, - upb_status *status) { - return sink->vtbl->putstr(sink, str, status); -} - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/upb_msg.h b/src/upb_msg.h index 180f918..68ba13a 100644 --- a/src/upb_msg.h +++ b/src/upb_msg.h @@ -18,8 +18,8 @@ #ifndef UPB_MSG_H #define UPB_MSG_H -#include "upb_stream.h" #include <stdlib.h> +#include "upb_handlers.h" #ifdef __cplusplus extern "C" { diff --git a/src/upb_stdio.h b/src/upb_stdio.h index ba5fe1a..a164821 100644 --- a/src/upb_stdio.h +++ b/src/upb_stdio.h @@ -9,7 +9,7 @@ */ #include <stdio.h> -#include "upb_stream.h" +#include "upb_bytestream.h" #ifndef UPB_STDIO_H_ #define UPB_STDIO_H_ @@ -33,8 +33,8 @@ void upb_stdio_reset(upb_stdio *stdio, FILE* file); // 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); +upb_bytesrc *upb_stdio_bytesrc(upb_stdio *stdio); +upb_bytesink *upb_stdio_bytesink(upb_stdio *stdio); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/upb_strstream.c b/src/upb_strstream.c index 37b5179..284a4d7 100644 --- a/src/upb_strstream.c +++ b/src/upb_strstream.c @@ -8,6 +8,7 @@ #include "upb_strstream.h" #include <stdlib.h> +#include "upb_string.h" /* upb_stringsrc **************************************************************/ diff --git a/src/upb_strstream.h b/src/upb_strstream.h index 8da5393..e092b55 100644 --- a/src/upb_strstream.h +++ b/src/upb_strstream.h @@ -11,7 +11,7 @@ #ifndef UPB_STRSTREAM_H #define UPB_STRSTREAM_H -#include "upb_stream.h" +#include "upb_bytestream.h" #ifdef __cplusplus extern "C" { diff --git a/src/upb_textprinter.h b/src/upb_textprinter.h index aa9febb..f8c7f78 100644 --- a/src/upb_textprinter.h +++ b/src/upb_textprinter.h @@ -8,7 +8,8 @@ #ifndef UPB_TEXT_H_ #define UPB_TEXT_H_ -#include "upb_stream.h" +#include "upb_bytestream.h" +#include "upb_handlers.h" #ifdef __cplusplus extern "C" { diff --git a/tests/tests.c b/tests/tests.c index 2fe8b8d..c7a86fb 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -5,7 +5,7 @@ #include "upb_def.h" #include "upb_glue.h" #include "upb_test.h" -#include "upb_stream.h" +#include "upb_handlers.h" #include "upb_decoder.h" static upb_symtab *load_test_proto() { |