diff --git a/BUILD b/BUILD index 4e83463f36..b9a3cebef3 100644 --- a/BUILD +++ b/BUILD @@ -681,6 +681,33 @@ cc_library( }), ) +upb_amalgamation( + name = "gen_core_amalgamation", + prefix = "core-", + outs = [ + "core-upb.c", + "core-upb.h", + ], + amalgamator = ":amalgamate", + libs = [ + ":upb", + ":descriptor_upbproto", + ":reflection", + ":port", + ":json", + ], +) + +cc_library( + name = "core_amalgamation", + srcs = ["core-upb.c"], + hdrs = ["core-upb.h"], + copts = select({ + ":windows": [], + "//conditions:default": COPTS, + }), +) + # Lua ########################################################################## cc_library( diff --git a/bazel/build_defs.bzl b/bazel/build_defs.bzl index 7544e12517..c51aded057 100644 --- a/bazel/build_defs.bzl +++ b/bazel/build_defs.bzl @@ -135,7 +135,7 @@ def _upb_amalgamation(ctx): ctx.actions.run( inputs = inputs, outputs = ctx.outputs.outs, - arguments = [ctx.bin_dir.path + "/"] + [f.path for f in srcs] + ["-I" + root for root in _get_real_roots(inputs)], + arguments = [ctx.bin_dir.path + "/", ctx.attr.prefix] + [f.path for f in srcs] + ["-I" + root for root in _get_real_roots(inputs)], progress_message = "Making amalgamation", executable = ctx.executable.amalgamator, ) @@ -147,6 +147,9 @@ upb_amalgamation = rule( executable = True, cfg = "host", ), + "prefix": attr.string( + default = "", + ), "libs": attr.label_list(aspects = [_file_list_aspect]), "outs": attr.output_list(), }, diff --git a/tools/amalgamate.py b/tools/amalgamate.py index dff1bc3ce0..b244eff8e8 100755 --- a/tools/amalgamate.py +++ b/tools/amalgamate.py @@ -11,14 +11,14 @@ def parse_include(line): return match.groups()[0] if match else None class Amalgamator: - def __init__(self, output_path): + def __init__(self, output_path, prefix): self.include_paths = ["."] self.included = set(["upb/port_def.inc", "upb/port_undef.inc"]) - self.output_h = open(output_path + "upb.h", "w") - self.output_c = open(output_path + "upb.c", "w") + self.output_h = open(output_path + prefix + "upb.h", "w") + self.output_c = open(output_path + prefix + "upb.c", "w") self.output_c.write("/* Amalgamated source file */\n") - self.output_c.write('#include "upb.h"\n') + self.output_c.write('#include "%supb.h"\n' % (prefix)) self.output_c.write(open("upb/port_def.inc").read()) self.output_h.write("/* Amalgamated source file */\n") @@ -73,10 +73,11 @@ class Amalgamator: # ---- main ---- output_path = sys.argv[1] -amalgamator = Amalgamator(output_path) +prefix = sys.argv[2] +amalgamator = Amalgamator(output_path, prefix) files = [] -for arg in sys.argv[2:]: +for arg in sys.argv[3:]: arg = arg.strip() if arg.startswith("-I"): amalgamator.add_include_path(arg[2:]) diff --git a/tools/make_cmakelists.py b/tools/make_cmakelists.py index 76e3ee1598..5fe7a657e2 100755 --- a/tools/make_cmakelists.py +++ b/tools/make_cmakelists.py @@ -38,7 +38,7 @@ class BuildFileFunctions(object): def cc_library(self, **kwargs): if kwargs["name"] == "amalgamation" or kwargs["name"] == "upbc_generator": return - if kwargs["name"] == "lupb": + if kwargs["name"] == "core_amalgamation" or kwargs["name"] == "lupb": return files = kwargs.get("srcs", []) + kwargs.get("hdrs", []) found_files = [] diff --git a/upb/json_decode.c b/upb/json_decode.c index bb4337c06b..4b19b43613 100644 --- a/upb/json_decode.c +++ b/upb/json_decode.c @@ -405,7 +405,9 @@ static upb_strview jsondec_string(jsondec *d) { switch (ch) { case '"': { - upb_strview ret = {buf, end - buf}; + upb_strview ret; + ret.data = buf; + ret.size = end - buf; return ret; } case '\\': @@ -413,7 +415,7 @@ static upb_strview jsondec_string(jsondec *d) { if (*d->ptr == 'u') { d->ptr++; if (buf_end - end < 4) { - // Allow space for maximum-sized code point (4 bytes). + /* Allow space for maximum-sized code point (4 bytes). */ jsondec_resize(d, &buf, &end, &buf_end); } end += jsondec_unicode(d, end); @@ -888,7 +890,8 @@ static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) { if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) { /* JSON "null" indicates a default value, so no need to set anything. */ - return jsondec_null(d); + jsondec_null(d); + return; } preserved = d->debug_field; @@ -1005,7 +1008,7 @@ static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) { if (str.size < 20) goto malformed; { - // 1972-01-01T01:00:00 + /* 1972-01-01T01:00:00 */ int year = jsondec_tsdigits(d, &ptr, 4, "-"); int mon = jsondec_tsdigits(d, &ptr, 2, "-"); int day = jsondec_tsdigits(d, &ptr, 2, "T"); @@ -1019,7 +1022,7 @@ static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) { nanos.int32_val = jsondec_nanos(d, &ptr, end); { - // [+-]08:00 or Z + /* [+-]08:00 or Z */ int ofs = 0; bool neg = false; @@ -1062,7 +1065,7 @@ static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) { const char *ptr = str.data; const char *end = ptr + str.size; - // "3.000000001s", "3s", etc. + /* "3.000000001s", "3s", etc. */ ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val); nanos.int32_val = jsondec_nanos(d, &ptr, end); @@ -1311,10 +1314,10 @@ static void jsondec_any(jsondec *d, upb_msg *msg, const upb_msgdef *m) { if (pre_type_data) { size_t len = pre_type_end - pre_type_data + 1; char *tmp = upb_arena_malloc(d->arena, len); - memcpy(tmp, pre_type_data, len - 1); - tmp[len - 1] = '}'; const char *saved_ptr = d->ptr; const char *saved_end = d->end; + memcpy(tmp, pre_type_data, len - 1); + tmp[len - 1] = '}'; d->ptr = tmp; d->end = tmp + len; d->is_first = true; diff --git a/upb/msg.c b/upb/msg.c index a211f1a46b..a1acaa4aa9 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -149,19 +149,10 @@ static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type, return arr; } -static bool resize_array(upb_array *arr, size_t size, upb_arena *arena) { - if (size > arr->size && !_upb_array_realloc(arr, size, arena)) { - return false; - } - - arr->len = size; - return true; -} - void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size, upb_fieldtype_t type, upb_arena *arena) { upb_array *arr = getorcreate_array(arr_ptr, type, arena); - return arr && resize_array(arr, size, arena) ? _upb_array_ptr(arr) : NULL; + return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr) : NULL; } bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value, @@ -171,7 +162,7 @@ bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value, int lg2 = _upb_fieldtype_to_sizelg2[type]; char *data; - if (!arr || !resize_array(arr, elem + 1, arena)) return false; + if (!arr || !_upb_array_resize(arr, elem + 1, arena)) return false; data = _upb_array_ptr(arr); memcpy(data + (elem << lg2), value, 1 << lg2); diff --git a/upb/msg.h b/upb/msg.h index 2109348909..1fc302f8d9 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -142,6 +142,19 @@ void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size, bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value, upb_fieldtype_t type, upb_arena *arena); +UPB_INLINE bool _upb_array_reserve(upb_array *arr, size_t size, + upb_arena *arena) { + if (arr->size < size) return _upb_array_realloc(arr, size, arena); + return true; +} + +UPB_INLINE bool _upb_array_resize(upb_array *arr, size_t size, + upb_arena *arena) { + if (!_upb_array_reserve(arr, size, arena)) return false; + arr->len = size; + return true; +} + UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs, size_t *size) { const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*); @@ -285,7 +298,7 @@ UPB_INLINE bool _upb_map_get(const upb_map *map, const void *key, upb_value tabval; upb_strview k = _upb_map_tokey(key, key_size); bool ret = upb_strtable_lookup2(&map->table, k.data, k.size, &tabval); - if (ret) { + if (ret && val) { _upb_map_fromvalue(tabval, val, val_size); } return ret; @@ -296,8 +309,8 @@ UPB_INLINE void* _upb_map_next(const upb_map *map, size_t *iter) { it.t = &map->table; it.index = *iter; upb_strtable_next(&it); - if (upb_strtable_done(&it)) return NULL; *iter = it.index; + if (upb_strtable_done(&it)) return NULL; return (void*)str_tabent(&it); } diff --git a/upb/reflection.c b/upb/reflection.c index 190bde5ef7..446dc7e5a3 100644 --- a/upb/reflection.c +++ b/upb/reflection.c @@ -254,10 +254,8 @@ bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) { return true; } -/* Resizes the array to the given size, reallocating if necessary, and returns a - * pointer to the new array elements. */ bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) { - return _upb_array_realloc(arr, size, arena); + return _upb_array_resize(arr, size, arena); } /** upb_map *******************************************************************/ @@ -289,6 +287,14 @@ bool upb_mapiter_next(const upb_map *map, size_t *iter) { return _upb_map_next(map, iter); } +bool upb_mapiter_done(const upb_map *map, size_t iter) { + upb_strtable_iter i; + UPB_ASSERT(iter != UPB_MAP_BEGIN); + i.t = &map->table; + i.index = iter; + return upb_strtable_done(&i); +} + /* Returns the key and value for this entry of the map. */ upb_msgval upb_mapiter_key(const upb_map *map, size_t iter) { upb_strtable_iter i; diff --git a/upb/reflection.h b/upb/reflection.h index c55fe4a2e6..5371ca777b 100644 --- a/upb/reflection.h +++ b/upb/reflection.h @@ -143,6 +143,11 @@ bool upb_map_delete(upb_map *map, upb_msgval key); /* Advances to the next entry. Returns false if no more entries are present. */ bool upb_mapiter_next(const upb_map *map, size_t *iter); +/* Returns true if the iterator still points to a valid entry, or false if the + * iterator is past the last element. It is an error to call this function with + * UPB_MAP_BEGIN (you must call next() at least once first). */ +bool upb_mapiter_done(const upb_map *map, size_t iter); + /* Returns the key and value for this entry of the map. */ upb_msgval upb_mapiter_key(const upb_map *map, size_t iter); upb_msgval upb_mapiter_value(const upb_map *map, size_t iter); diff --git a/upb/table.c b/upb/table.c index 21f8fcf6f8..c5ca3ac49b 100644 --- a/upb/table.c +++ b/upb/table.c @@ -243,7 +243,7 @@ static bool rm(upb_table *t, lookupkey_t key, upb_value *val, static size_t next(const upb_table *t, size_t i) { do { if (++i >= upb_table_size(t)) - return SIZE_MAX; + return SIZE_MAX - 1; /* Distinct from -1. */ } while(upb_tabent_isempty(&t->entries[i])); return i;