WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
arena.h
Go to the documentation of this file.
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/* upb_Arena is a specific allocator implementation that uses arena allocation.
9 * The user provides an allocator that will be used to allocate the underlying
10 * arena blocks. Arenas by nature do not require the individual allocations
11 * to be freed. However the Arena does allow users to register cleanup
12 * functions that will run when the arena is destroyed.
13 *
14 * A upb_Arena is *not* thread-safe, although some functions related to its
15 * managing its lifetime are, and are documented as such.
16 *
17 * You could write a thread-safe arena allocator that satisfies the
18 * upb_alloc interface, but it would not be as efficient for the
19 * single-threaded case. */
20
21#ifndef UPB_MEM_ARENA_H_
22#define UPB_MEM_ARENA_H_
23
24#include <stddef.h>
25#include <stdint.h>
26
27#include "upb/mem/alloc.h"
29
30// Must be last.
31#include "upb/port/def.inc"
32
33typedef struct upb_Arena upb_Arena;
34
35typedef void upb_AllocCleanupFunc(upb_alloc* alloc);
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41// Creates an arena from the given initial block (if any -- mem may be NULL). If
42// an initial block is specified, the arena's lifetime cannot be extended by
43// |upb_Arena_IncRefFor| or |upb_Arena_Fuse|. Additional blocks will be
44// allocated from |alloc|. If |alloc| is NULL, this is a fixed-size arena and
45// cannot grow. If an initial block is specified, |n| is its length; if there is
46// no initial block, |n| is a hint of the size that should be allocated for the
47// first block of the arena, such that `upb_Arena_Malloc(hint)` will not require
48// another call to |alloc|.
49UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
50
52// Sets the cleanup function for the upb_alloc used by the arena. Only one
53// cleanup function can be set, which will be called after all blocks are
54// freed.
57
58// Fuses the lifetime of two arenas, such that no arenas that have been
59// transitively fused together will be freed until all of them have reached a
60// zero refcount. This operation is safe to use concurrently from multiple
61// threads.
62UPB_API bool upb_Arena_Fuse(const upb_Arena* a, const upb_Arena* b);
63
64// This operation is safe to use concurrently from multiple threads.
66
67// Returns the upb_alloc used by the arena.
69
70// This operation is safe to use concurrently from multiple threads.
71bool upb_Arena_IncRefFor(const upb_Arena* a, const void* owner);
72// This operation is safe to use concurrently from multiple threads.
73void upb_Arena_DecRefFor(const upb_Arena* a, const void* owner);
74
75// This operation is safe to use concurrently from multiple threads.
76uintptr_t upb_Arena_SpaceAllocated(const upb_Arena* a, size_t* fused_count);
77// This operation is safe to use concurrently from multiple threads.
79
83
85 return upb_Arena_Init(NULL, size_hint, upb_alloc_global());
86}
87
88UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size);
89
90UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
91 size_t size);
92
93static const size_t UPB_PRIVATE(kUpbDefaultMaxBlockSize) =
95
96// Sets the maximum block size for all arenas. This is a global configuration
97// setting that will affect all existing and future arenas. If
98// upb_Arena_Malloc() is called with a size larger than this, we will exceed
99// this size and allocate a larger block.
100//
101// This API is meant for experimentation only. It will likely be removed in
102// the future.
103// This operation is safe to use concurrently from multiple threads.
105
106// Shrinks the last alloc from arena.
107// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena.
108// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if
109// this was not the last alloc.
111 size_t oldsize, size_t size);
112
113// Attempts to extend the given alloc from arena, in place. Is generally
114// only likely to succeed for the most recent allocation from this arena. If it
115// succeeds, returns true and `ptr`'s allocation is now `size` rather than
116// `oldsize`. Returns false if the allocation cannot be extended; `ptr`'s
117// allocation is unmodified. See also upb_Arena_Realloc.
118// REQUIRES: `size > oldsize`; to shrink, use `upb_Arena_Realloc` or
119// `upb_Arena_ShrinkLast`.
120UPB_API_INLINE bool upb_Arena_TryExtend(upb_Arena* a, void* ptr, size_t oldsize,
121 size_t size);
122
123#ifdef UPB_TRACING_ENABLED
124void upb_Arena_SetTraceHandler(void (*initArenaTraceHandler)(const upb_Arena*,
125 size_t size),
126 void (*fuseArenaTraceHandler)(const upb_Arena*,
127 const upb_Arena*),
128 void (*freeArenaTraceHandler)(const upb_Arena*));
129#endif
130
131#ifdef __cplusplus
132} /* extern "C" */
133#endif
134
135#include "upb/port/undef.inc"
136
137#endif /* UPB_MEM_ARENA_H_ */
upb_alloc * upb_alloc_global(void)
UPB_API_INLINE upb_Arena * upb_Arena_NewSized(size_t size_hint)
Definition arena.h:84
bool upb_Arena_IncRefFor(const upb_Arena *a, const void *owner)
UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena *a, void *ptr, size_t oldsize, size_t size)
Definition arena.h:84
UPB_API_INLINE void * upb_Arena_Realloc(upb_Arena *a, void *ptr, size_t oldsize, size_t size)
Definition arena.h:124
UPB_API void upb_Arena_SetAllocCleanup(upb_Arena *a, upb_AllocCleanupFunc *func)
UPB_API upb_Arena * upb_Arena_Init(void *mem, size_t n, upb_alloc *alloc)
UPB_API bool upb_Arena_Fuse(const upb_Arena *a, const upb_Arena *b)
UPB_API bool upb_Arena_IsFused(const upb_Arena *a, const upb_Arena *b)
uintptr_t upb_Arena_SpaceAllocated(const upb_Arena *a, size_t *fused_count)
void upb_Arena_SetMaxBlockSize(size_t max)
UPB_API_INLINE bool upb_Arena_TryExtend(upb_Arena *a, void *ptr, size_t oldsize, size_t size)
Definition arena.h:106
UPB_API_INLINE void * upb_Arena_Malloc(struct upb_Arena *a, size_t size)
Definition arena.h:65
void upb_AllocCleanupFunc(upb_alloc *alloc)
Definition arena.h:35
UPB_API upb_alloc * upb_Arena_GetUpbAlloc(upb_Arena *a)
uint32_t upb_Arena_DebugRefCount(const upb_Arena *a)
void upb_Arena_DecRefFor(const upb_Arena *a, const void *owner)
UPB_API_INLINE upb_Arena * upb_Arena_New(void)
Definition arena.h:80
UPB_API void upb_Arena_Free(upb_Arena *a)
#define UPB_API_INLINE
Definition def.inc:163
#define UPB_DEFAULT_MAX_BLOCK_SIZE
Definition def.inc:354
#define UPB_PRIVATE(x)
Definition def.inc:393
#define UPB_API
Definition def.inc:162
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3963
Definition arena.h:29
Definition alloc.h:38