xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/mem/internal/arena.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef UPB_MEM_INTERNAL_ARENA_H_
9 #define UPB_MEM_INTERNAL_ARENA_H_
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <string.h>
14 
15 // Must be last.
16 #include "upb/port/def.inc"
17 
18 // This is QUITE an ugly hack, which specifies the number of pointers needed
19 // to equal (or exceed) the storage required for one upb_Arena.
20 //
21 // We need this because the decoder inlines a upb_Arena for performance but
22 // the full struct is not visible outside of arena.c. Yes, I know, it's awful.
23 #define UPB_ARENA_SIZE_HACK 7
24 
25 // LINT.IfChange(upb_Arena)
26 
27 struct upb_Arena {
28   char* UPB_ONLYBITS(ptr);
29   char* UPB_ONLYBITS(end);
30 };
31 
32 // LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/arena.ts:upb_Arena)
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 void UPB_PRIVATE(_upb_Arena_SwapIn)(struct upb_Arena* des,
39                                     const struct upb_Arena* src);
40 void UPB_PRIVATE(_upb_Arena_SwapOut)(struct upb_Arena* des,
41                                      const struct upb_Arena* src);
42 
UPB_PRIVATE(_upb_ArenaHas)43 UPB_INLINE size_t UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) {
44   return (size_t)(a->UPB_ONLYBITS(end) - a->UPB_ONLYBITS(ptr));
45 }
46 
UPB_PRIVATE(_upb_Arena_Malloc)47 UPB_INLINE void* UPB_PRIVATE(_upb_Arena_Malloc)(struct upb_Arena* a,
48                                                 size_t size) {
49   void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t size);
50 
51   size = UPB_ALIGN_MALLOC(size);
52   const size_t span = size + UPB_ASAN_GUARD_SIZE;
53   if (UPB_UNLIKELY(UPB_PRIVATE(_upb_ArenaHas)(a) < span)) {
54     return UPB_PRIVATE(_upb_Arena_SlowMalloc)(a, span);
55   }
56 
57   // We have enough space to do a fast malloc.
58   void* ret = a->UPB_ONLYBITS(ptr);
59   UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
60   UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
61   UPB_UNPOISON_MEMORY_REGION(ret, size);
62 
63   a->UPB_ONLYBITS(ptr) += span;
64 
65   return ret;
66 }
67 
UPB_PRIVATE(_upb_Arena_Realloc)68 UPB_INLINE void* UPB_PRIVATE(_upb_Arena_Realloc)(struct upb_Arena* a, void* ptr,
69                                                  size_t oldsize, size_t size) {
70   oldsize = UPB_ALIGN_MALLOC(oldsize);
71   size = UPB_ALIGN_MALLOC(size);
72   bool is_most_recent_alloc =
73       (uintptr_t)ptr + oldsize == (uintptr_t)a->UPB_ONLYBITS(ptr);
74 
75   if (is_most_recent_alloc) {
76     ptrdiff_t diff = size - oldsize;
77     if ((ptrdiff_t)UPB_PRIVATE(_upb_ArenaHas)(a) >= diff) {
78       a->UPB_ONLYBITS(ptr) += diff;
79       return ptr;
80     }
81   } else if (size <= oldsize) {
82     return ptr;
83   }
84 
85   void* ret = UPB_PRIVATE(_upb_Arena_Malloc)(a, size);
86 
87   if (ret && oldsize > 0) {
88     memcpy(ret, ptr, UPB_MIN(oldsize, size));
89   }
90 
91   return ret;
92 }
93 
UPB_PRIVATE(_upb_Arena_ShrinkLast)94 UPB_INLINE void UPB_PRIVATE(_upb_Arena_ShrinkLast)(struct upb_Arena* a,
95                                                    void* ptr, size_t oldsize,
96                                                    size_t size) {
97   oldsize = UPB_ALIGN_MALLOC(oldsize);
98   size = UPB_ALIGN_MALLOC(size);
99   // Must be the last alloc.
100   UPB_ASSERT((char*)ptr + oldsize ==
101              a->UPB_ONLYBITS(ptr) - UPB_ASAN_GUARD_SIZE);
102   UPB_ASSERT(size <= oldsize);
103   a->UPB_ONLYBITS(ptr) = (char*)ptr + size;
104 }
105 
106 #ifdef __cplusplus
107 } /* extern "C" */
108 #endif
109 
110 #include "upb/port/undef.inc"
111 
112 #endif /* UPB_MEM_INTERNAL_ARENA_H_ */
113