1 /*
2 * Copyright (c) 2009-2021, Google LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Google LLC nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef UPB_MEM_ARENA_INTERNAL_H_
29 #define UPB_MEM_ARENA_INTERNAL_H_
30
31 #include "upb/mem/arena.h"
32
33 // Must be last.
34 #include "upb/port/def.inc"
35
36 typedef struct _upb_MemBlock _upb_MemBlock;
37
38 struct upb_Arena {
39 _upb_ArenaHead head;
40
41 // upb_alloc* together with a low bit which signals if there is an initial
42 // block.
43 uintptr_t block_alloc;
44
45 // When multiple arenas are fused together, each arena points to a parent
46 // arena (root points to itself). The root tracks how many live arenas
47 // reference it.
48
49 // The low bit is tagged:
50 // 0: pointer to parent
51 // 1: count, left shifted by one
52 UPB_ATOMIC(uintptr_t) parent_or_count;
53
54 // All nodes that are fused together are in a singly-linked list.
55 UPB_ATOMIC(upb_Arena*) next; // NULL at end of list.
56
57 // The last element of the linked list. This is present only as an
58 // optimization, so that we do not have to iterate over all members for every
59 // fuse. Only significant for an arena root. In other cases it is ignored.
60 UPB_ATOMIC(upb_Arena*) tail; // == self when no other list members.
61
62 // Linked list of blocks to free/cleanup. Atomic only for the benefit of
63 // upb_Arena_SpaceAllocated().
64 UPB_ATOMIC(_upb_MemBlock*) blocks;
65 };
66
_upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count)67 UPB_INLINE bool _upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count) {
68 return (parent_or_count & 1) == 1;
69 }
70
_upb_Arena_IsTaggedPointer(uintptr_t parent_or_count)71 UPB_INLINE bool _upb_Arena_IsTaggedPointer(uintptr_t parent_or_count) {
72 return (parent_or_count & 1) == 0;
73 }
74
_upb_Arena_RefCountFromTagged(uintptr_t parent_or_count)75 UPB_INLINE uintptr_t _upb_Arena_RefCountFromTagged(uintptr_t parent_or_count) {
76 UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
77 return parent_or_count >> 1;
78 }
79
_upb_Arena_TaggedFromRefcount(uintptr_t refcount)80 UPB_INLINE uintptr_t _upb_Arena_TaggedFromRefcount(uintptr_t refcount) {
81 uintptr_t parent_or_count = (refcount << 1) | 1;
82 UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
83 return parent_or_count;
84 }
85
_upb_Arena_PointerFromTagged(uintptr_t parent_or_count)86 UPB_INLINE upb_Arena* _upb_Arena_PointerFromTagged(uintptr_t parent_or_count) {
87 UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
88 return (upb_Arena*)parent_or_count;
89 }
90
_upb_Arena_TaggedFromPointer(upb_Arena * a)91 UPB_INLINE uintptr_t _upb_Arena_TaggedFromPointer(upb_Arena* a) {
92 uintptr_t parent_or_count = (uintptr_t)a;
93 UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
94 return parent_or_count;
95 }
96
upb_Arena_BlockAlloc(upb_Arena * arena)97 UPB_INLINE upb_alloc* upb_Arena_BlockAlloc(upb_Arena* arena) {
98 return (upb_alloc*)(arena->block_alloc & ~0x1);
99 }
100
upb_Arena_MakeBlockAlloc(upb_alloc * alloc,bool has_initial)101 UPB_INLINE uintptr_t upb_Arena_MakeBlockAlloc(upb_alloc* alloc,
102 bool has_initial) {
103 uintptr_t alloc_uint = (uintptr_t)alloc;
104 UPB_ASSERT((alloc_uint & 1) == 0);
105 return alloc_uint | (has_initial ? 1 : 0);
106 }
107
upb_Arena_HasInitialBlock(upb_Arena * arena)108 UPB_INLINE bool upb_Arena_HasInitialBlock(upb_Arena* arena) {
109 return arena->block_alloc & 0x1;
110 }
111
112 #include "upb/port/undef.inc"
113
114 #endif /* UPB_MEM_ARENA_INTERNAL_H_ */
115