From aecbfe42243ccd06425f021df6fe8d8d65974db7 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 3 Jul 2009 11:50:13 -0700 Subject: Moved upb_enum to a proper C file, updated upb_inlinedefs. --- Makefile | 2 +- tests.c | 1 + upb.h | 2 ++ upb_context.h | 6 ++++-- upb_enum.c | 31 +++++++++++++++++++++++++++++++ upb_enum.h | 25 +++---------------------- upb_inlinedefs.c | 11 ++++++++++- 7 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 upb_enum.c diff --git a/Makefile b/Makefile index ced605c..08c56cf 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CC=gcc CXX=g++ CFLAGS=-std=c99 CPPFLAGS=-O0 -Wall -Wextra -pedantic -g -DUPB_UNALIGNED_READS_OK -fomit-frame-pointer -OBJ=upb_parse.o upb_table.o upb_msg.o upb_context.o descriptor.o +OBJ=upb_parse.o upb_table.o upb_msg.o upb_enum.o upb_context.o descriptor.o all: $(OBJ) test_table tests clean: rm -f *.o test_table tests diff --git a/tests.c b/tests.c index c42ebd9..7bc5dde 100644 --- a/tests.c +++ b/tests.c @@ -3,6 +3,7 @@ #include #include #include "descriptor.c" +#include "upb_enum.c" #include "upb_parse.c" #include "upb_context.c" #include "upb_msg.c" diff --git a/upb.h b/upb.h index d62fba6..d76ba75 100644 --- a/upb.h +++ b/upb.h @@ -26,7 +26,9 @@ extern "C" { #endif /* inline if possible, emit standalone code if required. */ +#ifndef INLINE #define INLINE static inline +#endif /* The maximum that any submessages can be nested. Matches proto2's limit. */ #define UPB_MAX_NESTING 64 diff --git a/upb_context.h b/upb_context.h index d96ee39..8229ae4 100644 --- a/upb_context.h +++ b/upb_context.h @@ -16,6 +16,8 @@ #include "upb.h" #include "upb_table.h" +struct google_protobuf_FileDescriptorProto; + #ifdef __cplusplus extern "C" { #endif @@ -34,7 +36,7 @@ struct upb_context { /* A list of the FileDescriptorProtos we own (from having parsed them * ourselves) and must free on destruction. */ size_t fd_size, fd_len; - google_protobuf_FileDescriptorProto **fd; + struct google_protobuf_FileDescriptorProto **fd; }; /* Initializes and frees a upb_context, respectively. Newly initialized @@ -89,7 +91,7 @@ INLINE struct upb_symtab_entry *upb_context_symnext( * about what happened in the case of failure. This is because the descriptor * is expected to have been validated at the time it was parsed/generated. */ bool upb_context_addfd(struct upb_context *c, - google_protobuf_FileDescriptorProto *fd); + struct google_protobuf_FileDescriptorProto *fd); /* Like the previous, but takes a serialized FileDescriptorProto and parses * it before adding to the context. */ diff --git a/upb_enum.c b/upb_enum.c new file mode 100644 index 0000000..b599c9b --- /dev/null +++ b/upb_enum.c @@ -0,0 +1,31 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2009 Joshua Haberman. See LICENSE for details. + */ + +#include "descriptor.h" +#include "upb_enum.h" + +void upb_enum_init(struct upb_enum *e, + struct google_protobuf_EnumDescriptorProto *ed) { + int num_values = ed->set_flags.has.value ? ed->value->len : 0; + e->descriptor = ed; + upb_strtable_init(&e->nametoint, num_values, sizeof(struct upb_enum_ntoi_entry)); + upb_inttable_init(&e->inttoname, num_values, sizeof(struct upb_enum_iton_entry)); + + for(int i = 0; i < num_values; i++) { + google_protobuf_EnumValueDescriptorProto *value = ed->value->elements[i]; + struct upb_enum_ntoi_entry ntoi_entry = {.e = {.key = *value->name}, + .value = value->number}; + struct upb_enum_iton_entry iton_entry = {.e = {.key = value->number}, + .string = value->name}; + upb_strtable_insert(&e->nametoint, &ntoi_entry.e); + upb_inttable_insert(&e->inttoname, &iton_entry.e); + } +} + +void upb_enum_free(struct upb_enum *e) { + upb_strtable_free(&e->nametoint); + upb_inttable_free(&e->inttoname); +} diff --git a/upb_enum.h b/upb_enum.h index 2c4010b..9fea3a4 100644 --- a/upb_enum.h +++ b/upb_enum.h @@ -34,27 +34,8 @@ struct upb_enum_iton_entry { /* Initializes and frees an enum, respectively. Caller retains ownership of * ed, but it must outlive e. */ -INLINE void upb_enum_init(struct upb_enum *e, - struct google_protobuf_EnumDescriptorProto *ed) { - int num_values = ed->set_flags.has.value ? ed->value->len : 0; - e->descriptor = ed; - upb_strtable_init(&e->nametoint, num_values, sizeof(struct upb_enum_ntoi_entry)); - upb_inttable_init(&e->inttoname, num_values, sizeof(struct upb_enum_iton_entry)); - - for(int i = 0; i < num_values; i++) { - google_protobuf_EnumValueDescriptorProto *value = ed->value->elements[i]; - struct upb_enum_ntoi_entry ntoi_entry = {.e = {.key = *value->name}, - .value = value->number}; - struct upb_enum_iton_entry iton_entry = {.e = {.key = value->number}, - .string = value->name}; - upb_strtable_insert(&e->nametoint, &ntoi_entry.e); - upb_inttable_insert(&e->inttoname, &iton_entry.e); - } -} - -INLINE void upb_enum_free(struct upb_enum *e) { - upb_strtable_free(&e->nametoint); - upb_inttable_free(&e->inttoname); -} +void upb_enum_init(struct upb_enum *e, + struct google_protobuf_EnumDescriptorProto *ed); +void upb_enum_free(struct upb_enum *e); #endif /* UPB_ENUM_H_ */ diff --git a/upb_inlinedefs.c b/upb_inlinedefs.c index 8f9514a..60863c4 100644 --- a/upb_inlinedefs.c +++ b/upb_inlinedefs.c @@ -1,10 +1,19 @@ /* * upb - a minimalist implementation of protocol buffers. * + * This file, if compiled, will contain standalone (non-inlined) versions of + * all inline functions defined in header files. We don't generally use this + * file since we use "static inline" for inline functions (which will put a + * standalone version of the function in any .o file that needs it, but + * compiling this file and dumping the object file will let us inspect how + * inline functions are compiled, so we keep it around. + * * Copyright (c) 2009 Joshua Haberman. See LICENSE for details. */ #define INLINE -#include "upb_parse.h" +#include "upb_context.h" +#include "upb_enum.h" #include "upb_msg.h" +#include "upb_parse.h" #include "upb_table.h" -- cgit v1.2.3