Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:29:08

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 #ifndef UPB_MEM_ARENA_HPP_
0009 #define UPB_MEM_ARENA_HPP_
0010 
0011 #include "upb/mem/alloc.h"
0012 #ifdef __cplusplus
0013 
0014 #include <cstddef>
0015 #include <memory>
0016 
0017 #include "upb/mem/arena.h"
0018 
0019 namespace upb {
0020 
0021 class Arena {
0022  public:
0023   // A simple arena with no initial memory block and the default allocator.
0024   Arena() : ptr_(upb_Arena_New(), upb_Arena_Free) {}
0025   Arena(char* initial_block, size_t size)
0026       : ptr_(upb_Arena_Init(initial_block, size, &upb_alloc_global),
0027              upb_Arena_Free) {}
0028   explicit Arena(size_t size)
0029       : ptr_(upb_Arena_NewSized(size), upb_Arena_Free) {}
0030 
0031   upb_Arena* ptr() const { return ptr_.get(); }
0032 
0033   // Fuses the arenas together.
0034   // This operation can only be performed on arenas with no initial blocks. Will
0035   // return false if the fuse failed due to either arena having an initial
0036   // block.
0037   bool Fuse(Arena& other) { return upb_Arena_Fuse(ptr(), other.ptr()); }
0038 
0039   bool IsFused(Arena& other) const {
0040     return upb_Arena_IsFused(ptr(), other.ptr());
0041   }
0042 
0043  protected:
0044   std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
0045 };
0046 }  // namespace upb
0047 
0048 #endif  // __cplusplus
0049 
0050 #endif  // UPB_MEM_ARENA_HPP_