• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
43 // Returns whether |ptr| was allocated directly by |a| (so care must be used
44 // with fused arenas).
45 UPB_API bool UPB_ONLYBITS(_upb_Arena_Contains)(const struct upb_Arena* a,
46                                                void* ptr);
47 
UPB_PRIVATE(_upb_ArenaHas)48 UPB_INLINE size_t UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) {
49   return (size_t)(a->UPB_ONLYBITS(end) - a->UPB_ONLYBITS(ptr));
50 }
51 
upb_Arena_Malloc(struct upb_Arena * a,size_t size)52 UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size) {
53   void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t size);
54 
55   size = UPB_ALIGN_MALLOC(size);
56   const size_t span = size + UPB_ASAN_GUARD_SIZE;
57   if (UPB_UNLIKELY(UPB_PRIVATE(_upb_ArenaHas)(a) < span)) {
58     return UPB_PRIVATE(_upb_Arena_SlowMalloc)(a, span);
59   }
60 
61   // We have enough space to do a fast malloc.
62   void* ret = a->UPB_ONLYBITS(ptr);
63   UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
64   UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
65   UPB_UNPOISON_MEMORY_REGION(ret, size);
66 
67   a->UPB_ONLYBITS(ptr) += span;
68 
69   return ret;
70 }
71 
upb_Arena_Realloc(struct upb_Arena * a,void * ptr,size_t oldsize,size_t size)72 UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a, void* ptr,
73                                        size_t oldsize, size_t size) {
74   oldsize = UPB_ALIGN_MALLOC(oldsize);
75   size = UPB_ALIGN_MALLOC(size);
76   bool is_most_recent_alloc =
77       (uintptr_t)ptr + oldsize == (uintptr_t)a->UPB_ONLYBITS(ptr);
78 
79   if (is_most_recent_alloc) {
80     ptrdiff_t diff = size - oldsize;
81     if ((ptrdiff_t)UPB_PRIVATE(_upb_ArenaHas)(a) >= diff) {
82       a->UPB_ONLYBITS(ptr) += diff;
83       return ptr;
84     }
85   } else if (size <= oldsize) {
86     return ptr;
87   }
88 
89   void* ret = upb_Arena_Malloc(a, size);
90 
91   if (ret && oldsize > 0) {
92     memcpy(ret, ptr, UPB_MIN(oldsize, size));
93   }
94 
95   return ret;
96 }
97 
upb_Arena_ShrinkLast(struct upb_Arena * a,void * ptr,size_t oldsize,size_t size)98 UPB_API_INLINE void upb_Arena_ShrinkLast(struct upb_Arena* a, void* ptr,
99                                          size_t oldsize, size_t size) {
100   oldsize = UPB_ALIGN_MALLOC(oldsize);
101   size = UPB_ALIGN_MALLOC(size);
102   // Must be the last alloc.
103   UPB_ASSERT((char*)ptr + oldsize ==
104              a->UPB_ONLYBITS(ptr) - UPB_ASAN_GUARD_SIZE);
105   UPB_ASSERT(size <= oldsize);
106   a->UPB_ONLYBITS(ptr) = (char*)ptr + size;
107 }
108 
109 #ifdef __cplusplus
110 } /* extern "C" */
111 #endif
112 
113 #include "upb/port/undef.inc"
114 
115 #endif /* UPB_MEM_INTERNAL_ARENA_H_ */
116