From f409c9985b6f5adfb7fe9ba18fff8ee210365aeb Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Thu, 3 Nov 2022 15:48:59 +0000 Subject: [PATCH] create mem/ subdir for all arena code PiperOrigin-RevId: 485884460 --- BUILD | 51 ++++-- upb/alloc.h | 69 +------ upb/arena.h | 139 +-------------- upb/decode.h | 2 +- upb/internal/decode.h | 2 +- upb/io/chunked_input_stream.h | 2 +- upb/io/chunked_output_stream.h | 2 +- upb/io/string.h | 2 +- upb/{ => mem}/alloc.c | 2 +- upb/mem/alloc.h | 98 ++++++++++ upb/{ => mem}/arena.c | 2 +- upb/mem/arena.h | 168 ++++++++++++++++++ .../arena.h => mem/arena_internal.h} | 9 +- upb/mini_table/common.c | 2 +- upb/mini_table/decode.c | 2 +- upb/mini_table/encode.c | 2 +- upb/msg.h | 2 +- upb/upb.h | 2 +- 18 files changed, 321 insertions(+), 237 deletions(-) rename upb/{ => mem}/alloc.c (98%) create mode 100644 upb/mem/alloc.h rename upb/{ => mem}/arena.c (99%) create mode 100644 upb/mem/arena.h rename upb/{internal/arena.h => mem/arena_internal.h} (94%) diff --git a/BUILD b/BUILD index ebf5139c35..705e28aa37 100644 --- a/BUILD +++ b/BUILD @@ -109,8 +109,6 @@ cc_library( cc_library( name = "upb", srcs = [ - "upb/alloc.c", - "upb/arena.c", "upb/array.c", "upb/decode.c", "upb/encode.c", @@ -130,6 +128,8 @@ cc_library( "upb/decode.h", "upb/encode.h", "upb/extension_registry.h", + "upb/mem/alloc.h", + "upb/mem/arena.h", "upb/message_value.h", "upb/msg.h", "upb/status.h", @@ -140,12 +140,12 @@ cc_library( copts = UPB_DEFAULT_COPTS, visibility = ["//visibility:public"], deps = [ - ":arena_internal", ":array_internal", ":decode_internal", ":encode_internal", ":extension_registry", ":fastdecode", + ":mem", ":port", ":table_internal", ":unicode_internal", @@ -287,10 +287,10 @@ cc_library( ], copts = UPB_DEFAULT_COPTS, deps = [ - ":arena_internal", ":array_internal", ":decode_internal", ":extension_registry", + ":mem_internal", ":port", ":table_internal", ], @@ -944,8 +944,6 @@ cc_library( cc_library( name = "array_internal", srcs = [ - "upb/alloc.h", - "upb/arena.h", "upb/array.h", "upb/internal/array.c", "upb/message_value.h", @@ -958,24 +956,38 @@ cc_library( copts = UPB_DEFAULT_COPTS, visibility = ["//:__subpackages__"], deps = [ + ":mem", ":port", ], ) cc_library( - name = "arena_internal", + name = "mem", srcs = [ - "upb/alloc.h", - "upb/arena.h", + "upb/mem/alloc.c", + "upb/mem/arena.c", + "upb/mem/arena_internal.h", + ], + hdrs = [ + "upb/mem/alloc.h", + "upb/mem/arena.h", ], - hdrs = ["upb/internal/arena.h"], - aspect_hints = [":suppress_kotlin_interop"], - compatible_with = ["//buildenv/target:non_prod"], copts = UPB_DEFAULT_COPTS, visibility = ["//:__subpackages__"], deps = [":port"], ) +cc_library( + name = "mem_internal", + hdrs = ["upb/mem/arena_internal.h"], + copts = UPB_DEFAULT_COPTS, + visibility = ["//:__subpackages__"], + deps = [ + ":mem", + ":port", + ], +) + cc_library( name = "atoi_internal", srcs = ["upb/internal/atoi.c"], @@ -999,7 +1011,7 @@ cc_library( copts = UPB_DEFAULT_COPTS, visibility = ["//:__subpackages__"], deps = [ - ":arena_internal", + ":mem_internal", ":port", ":table_internal", "//third_party/utf8_range", @@ -1022,9 +1034,9 @@ cc_library( "upb/internal/table.c", ], hdrs = [ - "upb/alloc.h", - "upb/arena.h", "upb/internal/table.h", + "upb/mem/alloc.h", + "upb/mem/arena.h", "upb/status.h", "upb/string_view.h", "upb/upb.h", @@ -1058,7 +1070,6 @@ upb_amalgamation( "upb.h", ], libs = [ - ":arena_internal", ":array_internal", ":atoi_internal", ":collections", @@ -1067,6 +1078,8 @@ upb_amalgamation( ":encode_internal", ":extension_registry", ":fastdecode", + ":mem", + ":mem_internal", ":mini_table", ":port", ":reflection", @@ -1091,7 +1104,6 @@ upb_amalgamation( "php-upb.h", ], libs = [ - ":arena_internal", ":array_internal", ":atoi_internal", ":collections", @@ -1102,6 +1114,8 @@ upb_amalgamation( ":extension_registry", ":fastdecode", ":json", + ":mem", + ":mem_internal", ":mini_table", ":port", ":reflection", @@ -1130,7 +1144,6 @@ upb_amalgamation( "ruby-upb.h", ], libs = [ - ":arena_internal", ":array_internal", ":atoi_internal", ":collections", @@ -1140,6 +1153,8 @@ upb_amalgamation( ":extension_registry", ":fastdecode", ":json", + ":mem", + ":mem_internal", ":mini_table", ":port", ":reflection", diff --git a/upb/alloc.h b/upb/alloc.h index e6719c5731..35ca9cf17a 100644 --- a/upb/alloc.h +++ b/upb/alloc.h @@ -25,74 +25,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +// This header is deprecated, use upb/mem/alloc.h instead + #ifndef UPB_ALLOC_H_ #define UPB_ALLOC_H_ -// Must be last. -#include "upb/port_def.inc" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct upb_alloc upb_alloc; - -/* A combined `malloc()`/`free()` function. - * If `size` is 0 then the function acts like `free()`, otherwise it acts like - * `realloc()`. Only `oldsize` bytes from a previous allocation are - * preserved. */ -typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize, - size_t size); - -/* A upb_alloc is a possibly-stateful allocator object. - * - * It could either be an arena allocator (which doesn't require individual - * `free()` calls) or a regular `malloc()` (which does). The client must - * therefore free memory unless it knows that the allocator is an arena - * allocator. */ -struct upb_alloc { - upb_alloc_func* func; -}; - -UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) { - UPB_ASSERT(alloc); - return alloc->func(alloc, NULL, 0, size); -} - -UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize, - size_t size) { - UPB_ASSERT(alloc); - return alloc->func(alloc, ptr, oldsize, size); -} - -UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) { - UPB_ASSERT(alloc); - alloc->func(alloc, ptr, 0, 0); -} - -/* The global allocator used by upb. Uses the standard malloc()/free(). */ - -extern upb_alloc upb_alloc_global; - -/* Functions that hard-code the global malloc. - * - * We still get benefit because we can put custom logic into our global - * allocator, like injecting out-of-memory faults in debug/testing builds. */ - -UPB_INLINE void* upb_gmalloc(size_t size) { - return upb_malloc(&upb_alloc_global, size); -} - -UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) { - return upb_realloc(&upb_alloc_global, ptr, oldsize, size); -} - -UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); } - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#include "upb/port_undef.inc" +#include "upb/mem/alloc.h" #endif /* UPB_ALLOC_H_ */ diff --git a/upb/arena.h b/upb/arena.h index 5887a84051..c9c9223bc6 100644 --- a/upb/arena.h +++ b/upb/arena.h @@ -25,144 +25,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +// This header is deprecated, use upb/mem/arena.h instead + #ifndef UPB_ARENA_H_ #define UPB_ARENA_H_ -#include - -#include "upb/alloc.h" - -// Must be last. -#include "upb/port_def.inc" - -#ifdef __cplusplus -extern "C" { -#endif - -/* upb_Arena is a specific allocator implementation that uses arena allocation. - * The user provides an allocator that will be used to allocate the underlying - * arena blocks. Arenas by nature do not require the individual allocations - * to be freed. However the Arena does allow users to register cleanup - * functions that will run when the arena is destroyed. - * - * A upb_Arena is *not* thread-safe. - * - * You could write a thread-safe arena allocator that satisfies the - * upb_alloc interface, but it would not be as efficient for the - * single-threaded case. */ - -typedef void upb_CleanupFunc(void* ud); - -typedef struct upb_Arena upb_Arena; - -typedef struct { - /* We implement the allocator interface. - * This must be the first member of upb_Arena! - * TODO(haberman): remove once handlers are gone. */ - upb_alloc alloc; - - char *ptr, *end; -} _upb_ArenaHead; - -/* Creates an arena from the given initial block (if any -- n may be 0). - * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this - * is a fixed-size arena and cannot grow. */ -upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc); -void upb_Arena_Free(upb_Arena* a); -bool upb_Arena_AddCleanup(upb_Arena* a, void* ud, upb_CleanupFunc* func); -bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b); -void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size); -size_t upb_Arena_SpaceAllocated(upb_Arena* arena); -uint32_t upb_Arena_DebugRefCount(upb_Arena* arena); - -UPB_INLINE upb_alloc* upb_Arena_Alloc(upb_Arena* a) { return (upb_alloc*)a; } - -UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) { - _upb_ArenaHead* h = (_upb_ArenaHead*)a; - return (size_t)(h->end - h->ptr); -} - -UPB_INLINE void* _upb_Arena_FastMalloc(upb_Arena* a, size_t size) { - _upb_ArenaHead* h = (_upb_ArenaHead*)a; - void* ret = h->ptr; - UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret); - UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size); - UPB_UNPOISON_MEMORY_REGION(ret, size); - - h->ptr += size; - -#if UPB_ASAN - { - size_t guard_size = 32; - if (_upb_ArenaHas(a) >= guard_size) { - h->ptr += guard_size; - } else { - h->ptr = h->end; - } - } -#endif - - return ret; -} - -UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) { - size = UPB_ALIGN_MALLOC(size); - - if (UPB_UNLIKELY(_upb_ArenaHas(a) < size)) { - return _upb_Arena_SlowMalloc(a, size); - } - - return _upb_Arena_FastMalloc(a, size); -} - -// Shrinks the last alloc from arena. -// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena. -// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if -// this was not the last alloc. -UPB_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, size_t oldsize, - size_t size) { - _upb_ArenaHead* h = (_upb_ArenaHead*)a; - oldsize = UPB_ALIGN_MALLOC(oldsize); - size = UPB_ALIGN_MALLOC(size); - UPB_ASSERT((char*)ptr + oldsize == h->ptr); // Must be the last alloc. - UPB_ASSERT(size <= oldsize); - h->ptr = (char*)ptr + size; -} - -UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize, - size_t size) { - _upb_ArenaHead* h = (_upb_ArenaHead*)a; - oldsize = UPB_ALIGN_MALLOC(oldsize); - size = UPB_ALIGN_MALLOC(size); - bool is_most_recent_alloc = (uintptr_t)ptr + oldsize == (uintptr_t)h->ptr; - - if (is_most_recent_alloc) { - ptrdiff_t diff = size - oldsize; - if ((ptrdiff_t)_upb_ArenaHas(a) >= diff) { - h->ptr += diff; - return ptr; - } - } else if (size <= oldsize) { - return ptr; - } - - void* ret = upb_Arena_Malloc(a, size); - - if (ret && oldsize > 0) { - memcpy(ret, ptr, UPB_MIN(oldsize, size)); - } - - return ret; -} - -UPB_INLINE upb_Arena* upb_Arena_New(void) { - return upb_Arena_Init(NULL, 0, &upb_alloc_global); -} - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#include "upb/port_undef.inc" +#include "upb/mem/arena.h" #endif /* UPB_ARENA_H_ */ diff --git a/upb/decode.h b/upb/decode.h index f9ad98f7c9..526f56942f 100644 --- a/upb/decode.h +++ b/upb/decode.h @@ -32,8 +32,8 @@ #ifndef UPB_DECODE_H_ #define UPB_DECODE_H_ -#include "upb/arena.h" #include "upb/extension_registry.h" +#include "upb/mem/arena.h" #include "upb/msg.h" // Must be last. diff --git a/upb/internal/decode.h b/upb/internal/decode.h index ddd6fec962..ec6b3758de 100644 --- a/upb/internal/decode.h +++ b/upb/internal/decode.h @@ -34,7 +34,7 @@ #define UPB_INTERNAL_DECODE_H_ #include "upb/decode.h" -#include "upb/internal/arena.h" +#include "upb/mem/arena_internal.h" #include "upb/msg_internal.h" #include "third_party/utf8_range/utf8_range.h" diff --git a/upb/io/chunked_input_stream.h b/upb/io/chunked_input_stream.h index 40ce3621ed..9993de3b4c 100644 --- a/upb/io/chunked_input_stream.h +++ b/upb/io/chunked_input_stream.h @@ -28,8 +28,8 @@ #ifndef UPB_IO_CHUNKED_INPUT_STREAM_H_ #define UPB_IO_CHUNKED_INPUT_STREAM_H_ -#include "upb/arena.h" #include "upb/io/zero_copy_input_stream.h" +#include "upb/mem/arena.h" // Must be last. #include "upb/port_def.inc" diff --git a/upb/io/chunked_output_stream.h b/upb/io/chunked_output_stream.h index 1c0c47e8b8..bc8c542ef8 100644 --- a/upb/io/chunked_output_stream.h +++ b/upb/io/chunked_output_stream.h @@ -28,8 +28,8 @@ #ifndef UPB_IO_CHUNKED_OUTPUT_STREAM_H_ #define UPB_IO_CHUNKED_OUTPUT_STREAM_H_ -#include "upb/arena.h" #include "upb/io/zero_copy_output_stream.h" +#include "upb/mem/arena.h" // Must be last. #include "upb/port_def.inc" diff --git a/upb/io/string.h b/upb/io/string.h index 62b74e2ec4..f5e54d6ddc 100644 --- a/upb/io/string.h +++ b/upb/io/string.h @@ -36,7 +36,7 @@ #include -#include "upb/arena.h" +#include "upb/mem/arena.h" // Must be last. #include "upb/port_def.inc" diff --git a/upb/alloc.c b/upb/mem/alloc.c similarity index 98% rename from upb/alloc.c rename to upb/mem/alloc.c index 6e242cd6c4..9eb215cb32 100644 --- a/upb/alloc.c +++ b/upb/mem/alloc.c @@ -25,7 +25,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "upb/alloc.h" +#include "upb/mem/alloc.h" #include diff --git a/upb/mem/alloc.h b/upb/mem/alloc.h new file mode 100644 index 0000000000..c4bd97b80b --- /dev/null +++ b/upb/mem/alloc.h @@ -0,0 +1,98 @@ +/* + * 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_MEM_ALLOC_H_ +#define UPB_MEM_ALLOC_H_ + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct upb_alloc upb_alloc; + +/* A combined `malloc()`/`free()` function. + * If `size` is 0 then the function acts like `free()`, otherwise it acts like + * `realloc()`. Only `oldsize` bytes from a previous allocation are + * preserved. */ +typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize, + size_t size); + +/* A upb_alloc is a possibly-stateful allocator object. + * + * It could either be an arena allocator (which doesn't require individual + * `free()` calls) or a regular `malloc()` (which does). The client must + * therefore free memory unless it knows that the allocator is an arena + * allocator. */ +struct upb_alloc { + upb_alloc_func* func; +}; + +UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) { + UPB_ASSERT(alloc); + return alloc->func(alloc, NULL, 0, size); +} + +UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize, + size_t size) { + UPB_ASSERT(alloc); + return alloc->func(alloc, ptr, oldsize, size); +} + +UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) { + UPB_ASSERT(alloc); + alloc->func(alloc, ptr, 0, 0); +} + +// The global allocator used by upb. Uses the standard malloc()/free(). + +extern upb_alloc upb_alloc_global; + +/* Functions that hard-code the global malloc. + * + * We still get benefit because we can put custom logic into our global + * allocator, like injecting out-of-memory faults in debug/testing builds. */ + +UPB_INLINE void* upb_gmalloc(size_t size) { + return upb_malloc(&upb_alloc_global, size); +} + +UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) { + return upb_realloc(&upb_alloc_global, ptr, oldsize, size); +} + +UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); } + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_MEM_ALLOC_H_ */ diff --git a/upb/arena.c b/upb/mem/arena.c similarity index 99% rename from upb/arena.c rename to upb/mem/arena.c index 2043470d71..69d609f9de 100644 --- a/upb/arena.c +++ b/upb/mem/arena.c @@ -25,7 +25,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "upb/internal/arena.h" +#include "upb/mem/arena_internal.h" // Must be last. #include "upb/port_def.inc" diff --git a/upb/mem/arena.h b/upb/mem/arena.h new file mode 100644 index 0000000000..072e502881 --- /dev/null +++ b/upb/mem/arena.h @@ -0,0 +1,168 @@ +/* + * 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_MEM_ARENA_H_ +#define UPB_MEM_ARENA_H_ + +#include + +#include "upb/mem/alloc.h" + +// Must be last. +#include "upb/port_def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +/* upb_Arena is a specific allocator implementation that uses arena allocation. + * The user provides an allocator that will be used to allocate the underlying + * arena blocks. Arenas by nature do not require the individual allocations + * to be freed. However the Arena does allow users to register cleanup + * functions that will run when the arena is destroyed. + * + * A upb_Arena is *not* thread-safe. + * + * You could write a thread-safe arena allocator that satisfies the + * upb_alloc interface, but it would not be as efficient for the + * single-threaded case. */ + +typedef void upb_CleanupFunc(void* ud); + +typedef struct upb_Arena upb_Arena; + +typedef struct { + /* We implement the allocator interface. + * This must be the first member of upb_Arena! + * TODO(haberman): remove once handlers are gone. */ + upb_alloc alloc; + + char *ptr, *end; +} _upb_ArenaHead; + +/* Creates an arena from the given initial block (if any -- n may be 0). + * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this + * is a fixed-size arena and cannot grow. */ +upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc); +void upb_Arena_Free(upb_Arena* a); +bool upb_Arena_AddCleanup(upb_Arena* a, void* ud, upb_CleanupFunc* func); +bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b); +void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size); +size_t upb_Arena_SpaceAllocated(upb_Arena* arena); +uint32_t upb_Arena_DebugRefCount(upb_Arena* arena); + +UPB_INLINE upb_alloc* upb_Arena_Alloc(upb_Arena* a) { return (upb_alloc*)a; } + +UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) { + _upb_ArenaHead* h = (_upb_ArenaHead*)a; + return (size_t)(h->end - h->ptr); +} + +UPB_INLINE void* _upb_Arena_FastMalloc(upb_Arena* a, size_t size) { + _upb_ArenaHead* h = (_upb_ArenaHead*)a; + void* ret = h->ptr; + UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret); + UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size); + UPB_UNPOISON_MEMORY_REGION(ret, size); + + h->ptr += size; + +#if UPB_ASAN + { + size_t guard_size = 32; + if (_upb_ArenaHas(a) >= guard_size) { + h->ptr += guard_size; + } else { + h->ptr = h->end; + } + } +#endif + + return ret; +} + +UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) { + size = UPB_ALIGN_MALLOC(size); + + if (UPB_UNLIKELY(_upb_ArenaHas(a) < size)) { + return _upb_Arena_SlowMalloc(a, size); + } + + return _upb_Arena_FastMalloc(a, size); +} + +// Shrinks the last alloc from arena. +// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena. +// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if +// this was not the last alloc. +UPB_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, size_t oldsize, + size_t size) { + _upb_ArenaHead* h = (_upb_ArenaHead*)a; + oldsize = UPB_ALIGN_MALLOC(oldsize); + size = UPB_ALIGN_MALLOC(size); + UPB_ASSERT((char*)ptr + oldsize == h->ptr); // Must be the last alloc. + UPB_ASSERT(size <= oldsize); + h->ptr = (char*)ptr + size; +} + +UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize, + size_t size) { + _upb_ArenaHead* h = (_upb_ArenaHead*)a; + oldsize = UPB_ALIGN_MALLOC(oldsize); + size = UPB_ALIGN_MALLOC(size); + bool is_most_recent_alloc = (uintptr_t)ptr + oldsize == (uintptr_t)h->ptr; + + if (is_most_recent_alloc) { + ptrdiff_t diff = size - oldsize; + if ((ptrdiff_t)_upb_ArenaHas(a) >= diff) { + h->ptr += diff; + return ptr; + } + } else if (size <= oldsize) { + return ptr; + } + + void* ret = upb_Arena_Malloc(a, size); + + if (ret && oldsize > 0) { + memcpy(ret, ptr, UPB_MIN(oldsize, size)); + } + + return ret; +} + +UPB_INLINE upb_Arena* upb_Arena_New(void) { + return upb_Arena_Init(NULL, 0, &upb_alloc_global); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port_undef.inc" + +#endif /* UPB_MEM_ARENA_H_ */ diff --git a/upb/internal/arena.h b/upb/mem/arena_internal.h similarity index 94% rename from upb/internal/arena.h rename to upb/mem/arena_internal.h index 76707a30e0..6887feb18f 100644 --- a/upb/internal/arena.h +++ b/upb/mem/arena_internal.h @@ -25,10 +25,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef UPB_INTERNAL_ARENA_H_ -#define UPB_INTERNAL_ARENA_H_ +#ifndef UPB_MEM_ARENA_INTERNAL_H_ +#define UPB_MEM_ARENA_INTERNAL_H_ -#include "upb/arena.h" +#include "upb/mem/arena.h" // Must be last. #include "upb/port_def.inc" @@ -67,5 +67,4 @@ struct upb_Arena { #include "upb/port_undef.inc" -#endif /* UPB_INTERNAL_ARENA_H_ */ - +#endif /* UPB_MEM_ARENA_INTERNAL_H_ */ diff --git a/upb/mini_table/common.c b/upb/mini_table/common.c index df9c485b79..e9c02408f2 100644 --- a/upb/mini_table/common.c +++ b/upb/mini_table/common.c @@ -29,7 +29,7 @@ #include -#include "upb/arena.h" +#include "upb/mem/arena.h" #include "upb/mini_table/common_internal.h" #include "upb/upb.h" diff --git a/upb/mini_table/decode.c b/upb/mini_table/decode.c index f140f51074..d5e5b820cb 100644 --- a/upb/mini_table/decode.c +++ b/upb/mini_table/decode.c @@ -29,7 +29,7 @@ #include -#include "upb/arena.h" +#include "upb/mem/arena.h" #include "upb/mini_table/common.h" #include "upb/mini_table/common_internal.h" #include "upb/upb.h" diff --git a/upb/mini_table/encode.c b/upb/mini_table/encode.c index 466265f039..61fa5e7c0f 100644 --- a/upb/mini_table/encode.c +++ b/upb/mini_table/encode.c @@ -29,7 +29,7 @@ #include -#include "upb/arena.h" +#include "upb/mem/arena.h" #include "upb/mini_table/common.h" #include "upb/mini_table/common_internal.h" #include "upb/upb.h" diff --git a/upb/msg.h b/upb/msg.h index f71bf36e29..c06e52d084 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -36,7 +36,7 @@ #ifndef UPB_MSG_H_ #define UPB_MSG_H_ -#include "upb/arena.h" +#include "upb/mem/arena.h" // Must be last. #include "upb/port_def.inc" diff --git a/upb/upb.h b/upb/upb.h index c6c3275759..2bbf848323 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -40,7 +40,7 @@ #include // TODO(b/232091617): Remove these and fix everything that breaks as a result. -#include "upb/arena.h" +#include "upb/mem/arena.h" #include "upb/status.h" #include "upb/string_view.h"