LCOV - code coverage report
Current view: top level - src/leveldb/util - arena.cc (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 94.3 % 35 33
Test Date: 2026-02-04 04:43:42 Functions: 100.0 % 5 5
Branches: 71.4 % 14 10

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
       2                 :             : // Use of this source code is governed by a BSD-style license that can be
       3                 :             : // found in the LICENSE file. See the AUTHORS file for names of contributors.
       4                 :             : 
       5                 :             : #include "util/arena.h"
       6                 :             : 
       7                 :             : namespace leveldb {
       8                 :             : 
       9                 :             : static const int kBlockSize = 4096;
      10                 :             : 
      11                 :         551 : Arena::Arena()
      12                 :         551 :     : alloc_ptr_(nullptr), alloc_bytes_remaining_(0), memory_usage_(0) {}
      13                 :             : 
      14                 :         551 : Arena::~Arena() {
      15   [ -  +  +  + ]:        3971 :   for (size_t i = 0; i < blocks_.size(); i++) {
      16         [ +  - ]:        3420 :     delete[] blocks_[i];
      17                 :             :   }
      18                 :         551 : }
      19                 :             : 
      20                 :        3420 : char* Arena::AllocateFallback(size_t bytes) {
      21         [ -  + ]:        3420 :   if (bytes > kBlockSize / 4) {
      22                 :             :     // Object is more than a quarter of our block size.  Allocate it separately
      23                 :             :     // to avoid wasting too much space in leftover bytes.
      24                 :           0 :     char* result = AllocateNewBlock(bytes);
      25                 :           0 :     return result;
      26                 :             :   }
      27                 :             : 
      28                 :             :   // We waste the remaining space in the current block.
      29                 :        3420 :   alloc_ptr_ = AllocateNewBlock(kBlockSize);
      30                 :        3420 :   alloc_bytes_remaining_ = kBlockSize;
      31                 :             : 
      32                 :        3420 :   char* result = alloc_ptr_;
      33                 :        3420 :   alloc_ptr_ += bytes;
      34                 :        3420 :   alloc_bytes_remaining_ -= bytes;
      35                 :        3420 :   return result;
      36                 :             : }
      37                 :             : 
      38                 :      102688 : char* Arena::AllocateAligned(size_t bytes) {
      39                 :      102688 :   const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
      40                 :      102688 :   static_assert((align & (align - 1)) == 0,
      41                 :             :                 "Pointer size should be a power of 2");
      42                 :      102688 :   size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align - 1);
      43         [ +  + ]:      102688 :   size_t slop = (current_mod == 0 ? 0 : align - current_mod);
      44                 :      102688 :   size_t needed = bytes + slop;
      45                 :      102688 :   char* result;
      46         [ +  + ]:      102688 :   if (needed <= alloc_bytes_remaining_) {
      47                 :      101876 :     result = alloc_ptr_ + slop;
      48                 :      101876 :     alloc_ptr_ += needed;
      49                 :      101876 :     alloc_bytes_remaining_ -= needed;
      50                 :             :   } else {
      51                 :             :     // AllocateFallback always returned aligned memory
      52                 :         812 :     result = AllocateFallback(bytes);
      53                 :             :   }
      54         [ -  + ]:      102688 :   assert((reinterpret_cast<uintptr_t>(result) & (align - 1)) == 0);
      55                 :      102688 :   return result;
      56                 :             : }
      57                 :             : 
      58                 :        3420 : char* Arena::AllocateNewBlock(size_t block_bytes) {
      59                 :        3420 :   char* result = new char[block_bytes];
      60                 :        3420 :   blocks_.push_back(result);
      61                 :        3420 :   memory_usage_.fetch_add(block_bytes + sizeof(char*),
      62                 :             :                           std::memory_order_relaxed);
      63                 :        3420 :   return result;
      64                 :             : }
      65                 :             : 
      66                 :             : }  // namespace leveldb
        

Generated by: LCOV version 2.0-1