Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.8 KiB

/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2010-2012 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*/
#include "upb/pb/glue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "upb/bytestream.h"
#include "upb/descriptor/reader.h"
#include "upb/pb/decoder.h"
upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n,
void *owner, upb_status *status) {
// Create handlers.
const upb_handlers *reader_h = upb_descreader_gethandlers(&reader_h);
const upb_handlers *decoder_h =
upb_pbdecoder_gethandlers(reader_h, false, &decoder_h);
// Create pipeline.
upb_pipeline pipeline;
upb_pipeline_init(&pipeline, NULL, 0, upb_realloc, NULL);
upb_pipeline_donateref(&pipeline, reader_h, &reader_h);
upb_pipeline_donateref(&pipeline, decoder_h, &decoder_h);
// Create sinks.
upb_sink *reader_sink = upb_pipeline_newsink(&pipeline, reader_h);
upb_sink *decoder_sink = upb_pipeline_newsink(&pipeline, decoder_h);
upb_pbdecoder *d = upb_sink_getobj(decoder_sink);
upb_pbdecoder_resetsink(d, reader_sink);
// Push input data.
bool ok = upb_bytestream_putstr(decoder_sink, str, len);
if (status) upb_status_copy(status, upb_pipeline_status(&pipeline));
if (!ok) {
upb_pipeline_uninit(&pipeline);
return NULL;
}
upb_descreader *r = upb_sink_getobj(reader_sink);
upb_def **defs = upb_descreader_getdefs(r, owner, n);
upb_def **defscopy = malloc(sizeof(upb_def*) * (*n));
memcpy(defscopy, defs, sizeof(upb_def*) * (*n));
upb_pipeline_uninit(&pipeline);
upb_stream: all callbacks registered ahead-of-time. This is a significant change to the upb_stream protocol, and should hopefully be the last significant change. All callbacks are now registered ahead-of-time instead of having delegated callbacks registered at runtime, which makes it much easier to aggressively optimize ahead-of-time (like with a JIT). Other impacts of this change: - You no longer need to have loaded descriptor.proto as a upb_def to load other descriptors! This means the special-case code we used for bootstrapping is no longer necessary, and we no longer need to link the descriptor for descriptor.proto into upb. - A client can now register any upb_value as what will be delivered to their value callback, not just a upb_fielddef*. This should allow for other clients to get more bang out of the streaming decoder. This change unfortunately causes a bit of a performance regression -- I think largely due to highly suboptimal code that GCC generates when structs are returned by value. See: http://blog.reverberate.org/2011/03/19/when-a-compilers-slow-code-actually-bites-you/ On the other hand, once we have a JIT this should no longer matter. Performance numbers: plain.parsestream_googlemessage1.upb_table: 374 -> 396 (5.88) plain.parsestream_googlemessage2.upb_table: 616 -> 449 (-27.11) plain.parsetostruct_googlemessage1.upb_table_byref: 268 -> 269 (0.37) plain.parsetostruct_googlemessage1.upb_table_byval: 215 -> 204 (-5.12) plain.parsetostruct_googlemessage2.upb_table_byref: 307 -> 281 (-8.47) plain.parsetostruct_googlemessage2.upb_table_byval: 297 -> 272 (-8.42) omitfp.parsestream_googlemessage1.upb_table: 423 -> 410 (-3.07) omitfp.parsestream_googlemessage2.upb_table: 679 -> 483 (-28.87) omitfp.parsetostruct_googlemessage1.upb_table_byref: 287 -> 282 (-1.74) omitfp.parsetostruct_googlemessage1.upb_table_byval: 226 -> 219 (-3.10) omitfp.parsetostruct_googlemessage2.upb_table_byref: 315 -> 298 (-5.40) omitfp.parsetostruct_googlemessage2.upb_table_byval: 297 -> 287 (-3.37)
14 years ago
return defscopy;
}
bool upb_load_descriptor_into_symtab(upb_symtab *s, const char *str, size_t len,
upb_status *status) {
int n;
upb_def **defs = upb_load_defs_from_descriptor(str, len, &n, &defs, status);
if (!defs) return false;
bool success = upb_symtab_add(s, defs, n, &defs, status);
free(defs);
return success;
}
char *upb_readfile(const char *filename, size_t *len) {
FILE *f = fopen(filename, "rb");
if(!f) return NULL;
if(fseek(f, 0, SEEK_END) != 0) goto error;
long size = ftell(f);
if(size < 0) goto error;
if(fseek(f, 0, SEEK_SET) != 0) goto error;
char *buf = malloc(size);
if(fread(buf, size, 1, f) != 1) goto error;
fclose(f);
if (len) *len = size;
return buf;
error:
fclose(f);
return NULL;
}
bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname,
upb_status *status) {
size_t len;
char *data = upb_readfile(fname, &len);
if (!data) {
if (status) upb_status_seterrf(status, "Couldn't read file: %s", fname);
return false;
}
bool success = upb_load_descriptor_into_symtab(symtab, data, len, status);
free(data);
return success;
}