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 #ifndef UPB_MEM_ARENA_HPP_
0009 #define UPB_MEM_ARENA_HPP_
0010 
0011 #ifdef __cplusplus
0012 
0013 #include <memory>
0014 
0015 #include "upb/mem/arena.h"
0016 
0017 namespace upb {
0018 
0019 class Arena {
0020  public:
0021   // A simple arena with no initial memory block and the default allocator.
0022   Arena() : ptr_(upb_Arena_New(), upb_Arena_Free) {}
0023   Arena(char* initial_block, size_t size)
0024       : ptr_(upb_Arena_Init(initial_block, size, &upb_alloc_global),
0025              upb_Arena_Free) {}
0026 
0027   upb_Arena* ptr() const { return ptr_.get(); }
0028 
0029   // Fuses the arenas together.
0030   // This operation can only be performed on arenas with no initial blocks. Will
0031   // return false if the fuse failed due to either arena having an initial
0032   // block.
0033   bool Fuse(Arena& other) { return upb_Arena_Fuse(ptr(), other.ptr()); }
0034 
0035  protected:
0036   std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
0037 };
0038 
0039 // InlinedArena seeds the arenas with a predefined amount of memory. No heap
0040 // memory will be allocated until the initial block is exceeded.
0041 template <int N>
0042 class InlinedArena : public Arena {
0043  public:
0044   InlinedArena() : Arena(initial_block_, N) {}
0045   ~InlinedArena() {
0046     // Explicitly destroy the arena now so that it does not outlive
0047     // initial_block_.
0048     ptr_.reset();
0049   }
0050 
0051  private:
0052   InlinedArena(const InlinedArena&) = delete;
0053   InlinedArena& operator=(const InlinedArena&) = delete;
0054 
0055   char initial_block_[N];
0056 };
0057 
0058 }  // namespace upb
0059 
0060 #endif  // __cplusplus
0061 
0062 #endif  // UPB_MEM_ARENA_HPP_