diff --git a/BUILD b/BUILD index 00bd8fd5fb..b2ac9bbdec 100644 --- a/BUILD +++ b/BUILD @@ -106,6 +106,7 @@ cc_library( "upb/arena.c", "upb/decode.c", "upb/encode.c", + "upb/internal/arena.h", "upb/internal/decode.h", "upb/internal/table.h", "upb/internal/upb.h", @@ -269,8 +270,8 @@ cc_library( "upb/decode.h", "upb/decode_fast.c", "upb/decode_fast.h", + "upb/internal/arena.h", "upb/internal/decode.h", - "upb/internal/upb.h", "upb/msg.h", "upb/msg_internal.h", ], @@ -406,6 +407,7 @@ cc_library( cc_library( name = "json", srcs = [ + "upb/internal/arena.h", "upb/internal/upb.h", "upb/json_decode.c", "upb/json_encode.c", diff --git a/upb/arena.c b/upb/arena.c index 6cee9cfcb2..85eaf78c02 100644 --- a/upb/arena.c +++ b/upb/arena.c @@ -28,7 +28,7 @@ #include "upb/arena.h" #include "upb/alloc.h" -#include "upb/internal/upb.h" +#include "upb/internal/arena.h" // Must be last. #include "upb/port_def.inc" diff --git a/upb/decode.h b/upb/decode.h index 0c09497feb..985e3e4c39 100644 --- a/upb/decode.h +++ b/upb/decode.h @@ -32,6 +32,7 @@ #ifndef UPB_DECODE_H_ #define UPB_DECODE_H_ +#include "upb/arena.h" #include "upb/extension_registry.h" #include "upb/msg.h" diff --git a/upb/internal/arena.h b/upb/internal/arena.h new file mode 100644 index 0000000000..76707a30e0 --- /dev/null +++ b/upb/internal/arena.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2009-2021, Google LLC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Google LLC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef UPB_INTERNAL_ARENA_H_ +#define UPB_INTERNAL_ARENA_H_ + +#include "upb/arena.h" + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct mem_block mem_block; + +struct upb_Arena { + _upb_ArenaHead head; + /* Stores cleanup metadata for this arena. + * - a pointer to the current cleanup counter. + * - a boolean indicating if there is an unowned initial block. */ + uintptr_t cleanup_metadata; + + /* Allocator to allocate arena blocks. We are responsible for freeing these + * when we are destroyed. */ + upb_alloc* block_alloc; + uint32_t last_size; + + /* When multiple arenas are fused together, each arena points to a parent + * arena (root points to itself). The root tracks how many live arenas + * reference it. */ + uint32_t refcount; /* Only used when a->parent == a */ + struct upb_Arena* parent; + + /* Linked list of blocks to free/cleanup. */ + mem_block *freelist, *freelist_tail; +}; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_INTERNAL_ARENA_H_ */ + diff --git a/upb/internal/decode.h b/upb/internal/decode.h index 708874c9c7..d1bc30a4c2 100644 --- a/upb/internal/decode.h +++ b/upb/internal/decode.h @@ -36,7 +36,7 @@ #include #include "upb/decode.h" -#include "upb/internal/upb.h" +#include "upb/internal/arena.h" #include "upb/msg_internal.h" #include "third_party/utf8_range/utf8_range.h" diff --git a/upb/internal/upb.h b/upb/internal/upb.h index cdc8bfc228..7f72710020 100644 --- a/upb/internal/upb.h +++ b/upb/internal/upb.h @@ -30,30 +30,12 @@ #include "upb/upb.h" -struct mem_block; -typedef struct mem_block mem_block; +// Must be last. +#include "upb/port_def.inc" -struct upb_Arena { - _upb_ArenaHead head; - /* Stores cleanup metadata for this arena. - * - a pointer to the current cleanup counter. - * - a boolean indicating if there is an unowned initial block. */ - uintptr_t cleanup_metadata; - - /* Allocator to allocate arena blocks. We are responsible for freeing these - * when we are destroyed. */ - upb_alloc* block_alloc; - uint32_t last_size; - - /* When multiple arenas are fused together, each arena points to a parent - * arena (root points to itself). The root tracks how many live arenas - * reference it. */ - uint32_t refcount; /* Only used when a->parent == a */ - struct upb_Arena* parent; - - /* Linked list of blocks to free/cleanup. */ - mem_block *freelist, *freelist_tail; -}; +#ifdef __cplusplus +extern "C" { +#endif // Encodes a float or double that is round-trippable, but as short as possible. // These routines are not fully optimal (not guaranteed to be shortest), but are @@ -65,4 +47,10 @@ enum { kUpb_RoundTripBufferSize = 32 }; void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size); void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size); +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + #endif /* UPB_INTERNAL_UPB_H_ */