Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:16

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.
0015  *
0016  * You could write a thread-safe arena allocator that satisfies the
0017  * upb_alloc interface, but it would not be as efficient for the
0018  * single-threaded case. */
0019 
0020 #ifndef UPB_MEM_ARENA_H_
0021 #define UPB_MEM_ARENA_H_
0022 
0023 #include <stddef.h>
0024 #include <stdint.h>
0025 
0026 #include "upb/mem/alloc.h"
0027 #include "upb/mem/internal/arena.h"
0028 
0029 // Must be last.
0030 #include "upb/port/def.inc"
0031 
0032 typedef struct upb_Arena upb_Arena;
0033 
0034 #ifdef __cplusplus
0035 extern "C" {
0036 #endif
0037 
0038 // Creates an arena from the given initial block (if any -- n may be 0).
0039 // Additional blocks will be allocated from |alloc|.  If |alloc| is NULL, this
0040 // is a fixed-size arena and cannot grow.
0041 UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
0042 
0043 UPB_API void upb_Arena_Free(upb_Arena* a);
0044 UPB_API bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
0045 
0046 bool upb_Arena_IncRefFor(upb_Arena* a, const void* owner);
0047 void upb_Arena_DecRefFor(upb_Arena* a, const void* owner);
0048 
0049 size_t upb_Arena_SpaceAllocated(upb_Arena* a, size_t* fused_count);
0050 uint32_t upb_Arena_DebugRefCount(upb_Arena* a);
0051 
0052 UPB_API_INLINE upb_Arena* upb_Arena_New(void) {
0053   return upb_Arena_Init(NULL, 0, &upb_alloc_global);
0054 }
0055 
0056 UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size);
0057 
0058 UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
0059                                        size_t size);
0060 
0061 // Sets the maximum block size for all arenas. This is a global configuration
0062 // setting that will affect all existing and future arenas. If
0063 // upb_Arena_Malloc() is called with a size larger than this, we will exceed
0064 // this size and allocate a larger block.
0065 //
0066 // This API is meant for experimentation only. It will likely be removed in
0067 // the future.
0068 void upb_Arena_SetMaxBlockSize(size_t max);
0069 
0070 // Shrinks the last alloc from arena.
0071 // REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena.
0072 // We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if
0073 // this was not the last alloc.
0074 UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr,
0075                                          size_t oldsize, size_t size);
0076 
0077 #ifdef UPB_TRACING_ENABLED
0078 void upb_Arena_SetTraceHandler(void (*initArenaTraceHandler)(const upb_Arena*,
0079                                                              size_t size),
0080                                void (*fuseArenaTraceHandler)(const upb_Arena*,
0081                                                              const upb_Arena*),
0082                                void (*freeArenaTraceHandler)(const upb_Arena*));
0083 #endif
0084 
0085 #ifdef __cplusplus
0086 } /* extern "C" */
0087 #endif
0088 
0089 #include "upb/port/undef.inc"
0090 
0091 #endif /* UPB_MEM_ARENA_H_ */