summaryrefslogtreecommitdiff
path: root/tools/upbc.c
diff options
context:
space:
mode:
authorJoshua Haberman <joshua@reverberate.org>2009-08-12 13:47:24 -0700
committerJoshua Haberman <joshua@reverberate.org>2009-08-12 13:47:24 -0700
commit2282d2489bd8db3cd4ddbe0dd813732bffcf6452 (patch)
tree814fc94cd8ca4993363fb0ebe45b14e320a7a792 /tools/upbc.c
parent89a6c6d71f87bab63ee17c60f0feb56215f5c116 (diff)
Refactoring: unify upb_msg.
The cost is that a upb_msg will now always have an overhead of 2*sizeof(void*). This is comparable to proto2 overhead. The benefit is that upb_msg is now self-describing, and read-only algorithms can now operate on a upb_msg regardless of the memory-management scheme. Also, upb_array and upb_string now know inherently if they own their associated memory, and upb_array has a generic pointer for memory management purposes like upb_msg does.
Diffstat (limited to 'tools/upbc.c')
-rw-r--r--tools/upbc.c75
1 files changed, 37 insertions, 38 deletions
diff --git a/tools/upbc.c b/tools/upbc.c
index f593ae9..f9890c6 100644
--- a/tools/upbc.c
+++ b/tools/upbc.c
@@ -5,8 +5,6 @@
* easier to understand, but by its nature it is doing some very "meta"
* kinds of things.
*
- * TODO: compiler currently has memory leaks (trivial to fix with valgrind).
- *
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
*/
@@ -22,19 +20,19 @@
* the string (and thus never need to re-allocate). */
/* Convert to C identifier: foo.bar.Baz -> foo_bar_Baz. */
-static void to_cident(struct upb_string str)
+static void to_cident(struct upb_string *str)
{
- for(uint32_t i = 0; i < str.byte_len; i++)
- if(str.ptr[i] == '.' || str.ptr[i] == '/')
- str.ptr[i] = '_';
+ for(uint32_t i = 0; i < str->byte_len; i++)
+ if(str->ptr[i] == '.' || str->ptr[i] == '/')
+ str->ptr[i] = '_';
}
/* Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ. */
-static void to_preproc(struct upb_string str)
+static void to_preproc(struct upb_string *str)
{
to_cident(str);
- for(uint32_t i = 0; i < str.byte_len; i++)
- str.ptr[i] = toupper(str.ptr[i]);
+ for(uint32_t i = 0; i < str->byte_len; i++)
+ str->ptr[i] = toupper(str->ptr[i]);
}
static int memrchr(char *data, char c, size_t len)
@@ -64,7 +62,7 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
char *outfile_name, char *descriptor_cident, FILE *stream)
{
/* Header file prologue. */
- struct upb_string include_guard_name = upb_strdupc(outfile_name);
+ struct upb_string *include_guard_name = upb_strdupc(outfile_name);
to_preproc(include_guard_name);
fputs("/* This file was generated by upbc (the upb compiler). "
"Do not edit. */\n\n", stream),
@@ -90,21 +88,21 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
struct upb_enum *e = entry->ref._enum;
google_protobuf_EnumDescriptorProto *ed = e->descriptor;
/* We use entry->e.key (the fully qualified name) instead of ed->name. */
- struct upb_string enum_name = upb_strdup(entry->e.key);
+ struct upb_string *enum_name = upb_strdup(&entry->e.key);
to_cident(enum_name);
- struct upb_string enum_val_prefix = upb_strdup(entry->e.key);
- enum_val_prefix.byte_len = memrchr(enum_val_prefix.ptr,
+ struct upb_string *enum_val_prefix = upb_strdup(&entry->e.key);
+ enum_val_prefix->byte_len = memrchr(enum_val_prefix->ptr,
UPB_SYMBOL_SEPARATOR,
- enum_val_prefix.byte_len);
- enum_val_prefix.byte_len++;
+ enum_val_prefix->byte_len);
+ enum_val_prefix->byte_len++;
to_preproc(enum_val_prefix);
fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name));
if(ed->set_flags.has.value) {
for(uint32_t j = 0; j < ed->value->len; j++) { /* Foreach enum value. */
google_protobuf_EnumValueDescriptorProto *v = ed->value->elements[j];
- struct upb_string value_name = upb_strdup(*v->name);
+ struct upb_string *value_name = upb_strdup(v->name);
to_preproc(value_name);
/* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */
fprintf(stream, " " UPB_STRFMT UPB_STRFMT " = %" PRIu32,
@@ -128,7 +126,7 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
if(entries[i]->type != UPB_SYM_MESSAGE) continue;
struct upb_symtab_entry *entry = entries[i];
/* We use entry->e.key (the fully qualified name). */
- struct upb_string msg_name = upb_strdup(entry->e.key);
+ struct upb_string *msg_name = upb_strdup(&entry->e.key);
to_cident(msg_name);
fprintf(stream, "struct " UPB_STRFMT ";\n", UPB_STRARG(msg_name));
fprintf(stream, "typedef struct " UPB_STRFMT "\n " UPB_STRFMT ";\n\n",
@@ -143,9 +141,11 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
struct upb_symtab_entry *entry = entries[i];
struct upb_msgdef *m = entry->ref.msg;
/* We use entry->e.key (the fully qualified name). */
- struct upb_string msg_name = upb_strdup(entry->e.key);
+ struct upb_string *msg_name = upb_strdup(&entry->e.key);
to_cident(msg_name);
fprintf(stream, "struct " UPB_STRFMT " {\n", UPB_STRARG(msg_name));
+ fputs(" struct upb_msgdef *def;\n", stream);
+ fputs(" void *gptr;\n", stream);
fputs(" union {\n", stream);
fprintf(stream, " uint8_t bytes[%" PRIu32 "];\n", m->set_flags_bytes);
fputs(" struct {\n", stream);
@@ -153,7 +153,7 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
static char* labels[] = {"", "optional", "required", "repeated"};
struct google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
fprintf(stream, " bool " UPB_STRFMT ":1; /* = %" PRIu32 ", %s. */\n",
- UPB_STRARG(*fd->name), fd->number, labels[fd->label]);
+ UPB_STRARG(fd->name), fd->number, labels[fd->label]);
}
fputs(" } has;\n", stream);
fputs(" } set_flags;\n", stream);
@@ -170,14 +170,14 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
type_name_ref.ptr++;
type_name_ref.byte_len--;
}
- struct upb_string type_name = upb_strdup(type_name_ref);
+ struct upb_string *type_name = upb_strdup(&type_name_ref);
to_cident(type_name);
if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) {
fprintf(stream, " UPB_MSG_ARRAY(" UPB_STRFMT ")* " UPB_STRFMT ";\n",
- UPB_STRARG(type_name), UPB_STRARG(*fd->name));
+ UPB_STRARG(type_name), UPB_STRARG(fd->name));
} else {
fprintf(stream, " " UPB_STRFMT "* " UPB_STRFMT ";\n",
- UPB_STRARG(type_name), UPB_STRARG(*fd->name));
+ UPB_STRARG(type_name), UPB_STRARG(fd->name));
}
upb_strfree(type_name);
} else if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) {
@@ -193,7 +193,7 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
"struct upb_int64_array*"
};
fprintf(stream, " %s " UPB_STRFMT ";\n",
- c_types[fd->type], UPB_STRARG(*fd->name));
+ c_types[fd->type], UPB_STRARG(fd->name));
} else {
static char* c_types[] = {
"", "double", "float", "int64_t", "uint64_t", "int32_t", "uint64_t",
@@ -202,7 +202,7 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries,
"int32_t", "int64_t"
};
fprintf(stream, " %s " UPB_STRFMT ";\n",
- c_types[fd->type], UPB_STRARG(*fd->name));
+ c_types[fd->type], UPB_STRARG(fd->name));
}
}
fputs("};\n", stream);
@@ -230,7 +230,7 @@ struct strtable_entry {
struct typetable_entry {
struct upb_strtable_entry e;
struct upb_msg_fielddef *field;
- struct upb_string cident; /* Type name converted with to_cident(). */
+ struct upb_string *cident; /* Type name converted with to_cident(). */
/* A list of all values of this type, in an established order. */
union upb_value *values;
int values_size, values_len;
@@ -251,7 +251,7 @@ struct msgtable_entry {
int compare_entries(const void *_e1, const void *_e2)
{
struct strtable_entry *const*e1 = _e1, *const*e2 = _e2;
- return upb_strcmp((*e1)->e.key, (*e2)->e.key);
+ return upb_strcmp(&(*e1)->e.key, &(*e2)->e.key);
}
/* Mutually recursive functions to recurse over a set of possibly nested
@@ -297,7 +297,6 @@ static void add_strings_from_msg(void *data, struct upb_msgdef *m,
*
* TODO: make these use a generic msg visitor. */
-
struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t,
struct upb_msg_fielddef *f)
{
@@ -306,7 +305,7 @@ struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t,
struct typetable_entry *type_e = upb_strtable_lookup(t, &type_name);
if(type_e == NULL) {
struct typetable_entry new_type_e = {
- .e = {.key = type_name}, .field = f, .cident = upb_strdup(type_name),
+ .e = {.key = type_name}, .field = f, .cident = upb_strdup(&type_name),
.values = NULL, .values_size = 0, .values_len = 0,
.arrays = NULL, .arrays_size = 0, .arrays_len = 0
};
@@ -323,7 +322,7 @@ static void add_value(union upb_value value, struct upb_msg_fielddef *f,
{
struct typetable_entry *type_e = get_or_insert_typeentry(t, f);
if(type_e->values_len == type_e->values_size) {
- type_e->values_size = max(type_e->values_size * 2, 4);
+ type_e->values_size = UPB_MAX(type_e->values_size * 2, 4);
type_e->values = realloc(type_e->values, sizeof(*type_e->values) * type_e->values_size);
}
type_e->values[type_e->values_len++] = value;
@@ -343,7 +342,7 @@ static void add_submsgs(void *data, struct upb_msgdef *m, struct upb_strtable *t
struct typetable_entry *arr_type_e =
get_or_insert_typeentry(t, f);
if(arr_type_e->arrays_len == arr_type_e->arrays_size) {
- arr_type_e->arrays_size = max(arr_type_e->arrays_size * 2, 4);
+ arr_type_e->arrays_size = UPB_MAX(arr_type_e->arrays_size * 2, 4);
arr_type_e->arrays = realloc(arr_type_e->arrays,
sizeof(*arr_type_e->arrays)*arr_type_e->arrays_size);
}
@@ -492,7 +491,7 @@ static void write_message_c(void *data, struct upb_msgdef *m,
for(unsigned int j = 0; j < m->num_fields; j++) {
struct upb_msg_fielddef *f = &m->fields[j];
google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
- fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(*fd->name));
+ fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(fd->name));
if(upb_msg_isset(msgdata, f))
fprintf(stream, "true");
else
@@ -505,7 +504,7 @@ static void write_message_c(void *data, struct upb_msgdef *m,
struct upb_msg_fielddef *f = &m->fields[j];
google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
union upb_value val = upb_msg_get(msgdata, f);
- fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(*fd->name));
+ fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(fd->name));
if(!upb_msg_isset(msgdata, f)) {
fputs("0, /* Not set. */", stream);
} else if(upb_isstring(f)) {
@@ -516,7 +515,7 @@ static void write_message_c(void *data, struct upb_msgdef *m,
struct strtable_entry *str_e = upb_strtable_lookup(&strings, val.str);
assert(str_e);
fprintf(stream, "&strings[%d], /* \"" UPB_STRFMT "\" */",
- str_e->num, UPB_STRARG(*val.str));
+ str_e->num, UPB_STRARG(val.str));
}
} else if(upb_isarray(f)) {
/* Find this submessage in the list of msgs for that type. */
@@ -642,15 +641,15 @@ int main(int argc, char *argv[])
if(!outfile_base) outfile_base = input_file;
/* Read input file. */
- struct upb_string descriptor;
- if(!upb_strreadfile(input_file, &descriptor))
+ struct upb_string *descriptor = upb_strnew();
+ if(!upb_strreadfile(input_file, descriptor))
error("Couldn't read input file.");
/* Parse input file. */
struct upb_context c;
upb_context_init(&c);
- google_protobuf_FileDescriptorSet *fds =
- upb_msg_parsenew(c.fds_msg, &descriptor);
+ struct upb_msg *fds_msg = upb_msg_parsenew(c.fds_msg, descriptor);
+ google_protobuf_FileDescriptorSet *fds = (void*)fds_msg;
if(!fds)
error("Failed to parse input file descriptor.");
if(!upb_context_addfds(&c, fds))
@@ -686,7 +685,7 @@ int main(int argc, char *argv[])
write_message_c(fds, c.fds_msg, cident, h_filename, argc, argv, input_file, c_file);
fclose(c_file);
}
- upb_msg_free(fds, c.fds_msg);
+ upb_msg_free(fds_msg);
upb_context_free(&c);
upb_strfree(descriptor);
fclose(h_file);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback