Moved upb_enum to a proper C file, updated upb_inlinedefs.

pull/13171/head
Joshua Haberman 16 years ago
parent 62be5969a1
commit aecbfe4224
  1. 2
      Makefile
  2. 1
      tests.c
  3. 2
      upb.h
  4. 6
      upb_context.h
  5. 31
      upb_enum.c
  6. 25
      upb_enum.h
  7. 11
      upb_inlinedefs.c

@ -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

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "descriptor.c"
#include "upb_enum.c"
#include "upb_parse.c"
#include "upb_context.c"
#include "upb_msg.c"

@ -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

@ -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. */

@ -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);
}

@ -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_ */

@ -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"

Loading…
Cancel
Save