|
|
|||
Warning, file /include/upb/mem/arena.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // Protocol Buffers - Google's data interchange format 0002 // Copyright 2023 Google LLC. All rights reserved. 0003 // 0004 // Use of this source code is governed by a BSD-style 0005 // license that can be found in the LICENSE file or at 0006 // https://developers.google.com/open-source/licenses/bsd 0007 0008 /* upb_Arena is a specific allocator implementation that uses arena allocation. 0009 * The user provides an allocator that will be used to allocate the underlying 0010 * arena blocks. Arenas by nature do not require the individual allocations 0011 * to be freed. However the Arena does allow users to register cleanup 0012 * functions that will run when the arena is destroyed. 0013 * 0014 * A upb_Arena is *not* thread-safe, although some functions related to its 0015 * managing its lifetime are, and are documented as such. 0016 * 0017 * You could write a thread-safe arena allocator that satisfies the 0018 * upb_alloc interface, but it would not be as efficient for the 0019 * single-threaded case. */ 0020 0021 #ifndef UPB_MEM_ARENA_H_ 0022 #define UPB_MEM_ARENA_H_ 0023 0024 #include <stddef.h> 0025 #include <stdint.h> 0026 0027 #include "upb/mem/alloc.h" 0028 #include "upb/mem/internal/arena.h" 0029 0030 // Must be last. 0031 #include "upb/port/def.inc" 0032 0033 typedef struct upb_Arena upb_Arena; 0034 0035 typedef void upb_AllocCleanupFunc(upb_alloc* alloc); 0036 0037 #ifdef __cplusplus 0038 extern "C" { 0039 #endif 0040 0041 // Creates an arena from the given initial block (if any -- mem may be NULL). If 0042 // an initial block is specified, the arena's lifetime cannot be extended by 0043 // |upb_Arena_IncRefFor| or |upb_Arena_Fuse|. Additional blocks will be 0044 // allocated from |alloc|. If |alloc| is NULL, this is a fixed-size arena and 0045 // cannot grow. If an initial block is specified, |n| is its length; if there is 0046 // no initial block, |n| is a hint of the size that should be allocated for the 0047 // first block of the arena, such that `upb_Arena_Malloc(hint)` will not require 0048 // another call to |alloc|. 0049 UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc); 0050 0051 UPB_API void upb_Arena_Free(upb_Arena* a); 0052 // Sets the cleanup function for the upb_alloc used by the arena. Only one 0053 // cleanup function can be set, which will be called after all blocks are 0054 // freed. 0055 UPB_API void upb_Arena_SetAllocCleanup(upb_Arena* a, 0056 upb_AllocCleanupFunc* func); 0057 0058 // Fuses the lifetime of two arenas, such that no arenas that have been 0059 // transitively fused together will be freed until all of them have reached a 0060 // zero refcount. This operation is safe to use concurrently from multiple 0061 // threads. 0062 UPB_API bool upb_Arena_Fuse(const upb_Arena* a, const upb_Arena* b); 0063 0064 // This operation is safe to use concurrently from multiple threads. 0065 UPB_API bool upb_Arena_IsFused(const upb_Arena* a, const upb_Arena* b); 0066 0067 // Returns the upb_alloc used by the arena. 0068 UPB_API upb_alloc* upb_Arena_GetUpbAlloc(upb_Arena* a); 0069 0070 // This operation is safe to use concurrently from multiple threads. 0071 bool upb_Arena_IncRefFor(const upb_Arena* a, const void* owner); 0072 // This operation is safe to use concurrently from multiple threads. 0073 void upb_Arena_DecRefFor(const upb_Arena* a, const void* owner); 0074 0075 // This operation is safe to use concurrently from multiple threads. 0076 uintptr_t upb_Arena_SpaceAllocated(const upb_Arena* a, size_t* fused_count); 0077 // This operation is safe to use concurrently from multiple threads. 0078 uint32_t upb_Arena_DebugRefCount(const upb_Arena* a); 0079 0080 UPB_API_INLINE upb_Arena* upb_Arena_New(void) { 0081 return upb_Arena_Init(NULL, 0, &upb_alloc_global); 0082 } 0083 0084 UPB_API_INLINE upb_Arena* upb_Arena_NewSized(size_t size_hint) { 0085 return upb_Arena_Init(NULL, size_hint, &upb_alloc_global); 0086 } 0087 0088 UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size); 0089 0090 UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize, 0091 size_t size); 0092 0093 static const size_t UPB_PRIVATE(kUpbDefaultMaxBlockSize) = 0094 UPB_DEFAULT_MAX_BLOCK_SIZE; 0095 0096 // Sets the maximum block size for all arenas. This is a global configuration 0097 // setting that will affect all existing and future arenas. If 0098 // upb_Arena_Malloc() is called with a size larger than this, we will exceed 0099 // this size and allocate a larger block. 0100 // 0101 // This API is meant for experimentation only. It will likely be removed in 0102 // the future. 0103 // This operation is safe to use concurrently from multiple threads. 0104 void upb_Arena_SetMaxBlockSize(size_t max); 0105 0106 // Shrinks the last alloc from arena. 0107 // REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena. 0108 // We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if 0109 // this was not the last alloc. 0110 UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, 0111 size_t oldsize, size_t size); 0112 0113 // Attempts to extend the given alloc from arena, in place. Is generally 0114 // only likely to succeed for the most recent allocation from this arena. If it 0115 // succeeds, returns true and `ptr`'s allocation is now `size` rather than 0116 // `oldsize`. Returns false if the allocation cannot be extended; `ptr`'s 0117 // allocation is unmodified. See also upb_Arena_Realloc. 0118 // REQUIRES: `size > oldsize`; to shrink, use `upb_Arena_Realloc` or 0119 // `upb_Arena_ShrinkLast`. 0120 UPB_API_INLINE bool upb_Arena_TryExtend(upb_Arena* a, void* ptr, size_t oldsize, 0121 size_t size); 0122 0123 #ifdef UPB_TRACING_ENABLED 0124 void upb_Arena_SetTraceHandler(void (*initArenaTraceHandler)(const upb_Arena*, 0125 size_t size), 0126 void (*fuseArenaTraceHandler)(const upb_Arena*, 0127 const upb_Arena*), 0128 void (*freeArenaTraceHandler)(const upb_Arena*)); 0129 #endif 0130 0131 #ifdef __cplusplus 0132 } /* extern "C" */ 0133 #endif 0134 0135 #include "upb/port/undef.inc" 0136 0137 #endif /* UPB_MEM_ARENA_H_ */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|