util/arena.cc

filter: Statementstmt 94.4% 34 / 36 Branchbran 68.7% 11 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.5% 45 / 52 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« db/memtable.h ↑ Summary util/crc32c.cc »
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 #include <assert.h>
7  
8 namespace leveldb {
9  
10 static const int kBlockSize = 4096;
11  
12 5 Arena::Arena() {
13 5 blocks_memory_ = 0;
14 5 alloc_ptr_ = NULL; // First allocation will allocate a block
15 5 alloc_bytes_remaining_ = 0;
16 5 }
17  
18 5 Arena::~Arena() {
19 10 ▶ for (size_t i = 0; i < blocks_.size(); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < blocks_.size(); i++) { 5 5 10
20 5 ▶ delete[] blocks_[i];
Branch: 1 branch
BranchTrueFalseTotal
delete[] blocks_[i]; 5 0 5
21 }
22 5 }
23  
24 5 char* Arena::AllocateFallback(size_t bytes) {
25 5 ▶ if (bytes > kBlockSize / 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes > kBlockSize / 4) { 0 5 5
26 // Object is more than a quarter of our block size. Allocate it separately
27 // to avoid wasting too much space in leftover bytes.
28 0 char* result = AllocateNewBlock(bytes);
29 0 return result;
30 }
31  
32 // We waste the remaining space in the current block.
33 5 alloc_ptr_ = AllocateNewBlock(kBlockSize);
34 5 alloc_bytes_remaining_ = kBlockSize;
35  
36 5 char* result = alloc_ptr_;
37 5 alloc_ptr_ += bytes;
38 5 alloc_bytes_remaining_ -= bytes;
39 5 return result;
40 }
41  
42 36 char* Arena::AllocateAligned(size_t bytes) {
43 36 const int align = sizeof(void*); // We'll align to pointer size
44 assert((align & (align-1)) == 0); // Pointer size should be a power of 2
45 36 size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
46 36 ▶ size_t slop = (current_mod == 0 ? 0 : align - current_mod);
Branch: 1 branch
BranchTrueFalseTotal
size_t slop = (current_mod == 0 ? 0 : align - current_mod); 25 11 36
47 36 size_t needed = bytes + slop;
48 char* result;
49 36 ▶ if (needed <= alloc_bytes_remaining_) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed <= alloc_bytes_remaining_) { 31 5 36
50 31 result = alloc_ptr_ + slop;
51 31 alloc_ptr_ += needed;
52 31 alloc_bytes_remaining_ -= needed;
53 } else {
54 // AllocateFallback always returned aligned memory
55 5 result = AllocateFallback(bytes);
56 }
57 36 ▶ assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0); 0 36 36
58 36 return result;
59 }
60  
61 5 char* Arena::AllocateNewBlock(size_t block_bytes) {
62 5 ▶ char* result = new char[block_bytes];
Branch: 1 branch
BranchTrueFalseTotal
char* result = new char[block_bytes]; 5 0 5
63 5 blocks_memory_ += block_bytes;
64 5 ▶ blocks_.push_back(result);
Branch: 1 branch
BranchTrueFalseTotal
blocks_.push_back(result); 5 0 5
65 5 return result;
66 }
67  
68 }
« db/memtable.h ↑ Summary util/crc32c.cc »