GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/malloc_heap.cc
Date: 2026-05-24 02:35:55
Exec Total Coverage
Lines: 69 69 100.0%
Branches: 22 32 68.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5
6 #include "malloc_heap.h"
7
8 #include <cassert>
9 #include <cstring>
10 #include <new>
11
12 #include "util/smalloc.h"
13
14 using namespace std; // NOLINT
15
16 109184 void *MallocHeap::Allocate(uint64_t size, void *header, unsigned header_size) {
17
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109184 times.
109184 assert(size > 0);
18
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109184 times.
109184 assert(header_size <= size);
19 109184 const uint64_t rounded_size = RoundUp8(size);
20 109184 const int64_t real_size = rounded_size + sizeof(Tag);
21
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 109179 times.
109184 if (gauge_ + real_size > capacity_)
22 5 return NULL;
23
24 109179 unsigned char *new_block = heap_ + gauge_;
25 109179 new (new_block) Tag(rounded_size);
26 109179 new_block += sizeof(Tag);
27 109179 memcpy(new_block, header, header_size);
28 109179 gauge_ += real_size;
29 109179 stored_ += rounded_size;
30 109179 num_blocks_++;
31 109179 return new_block;
32 }
33
34
35 10 void MallocHeap::Compact() {
36
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (gauge_ == 0)
37 2 return;
38
39 // Not really a tag, just the top memory address
40 8 Tag *heap_top = reinterpret_cast<Tag *>(heap_ + gauge_);
41 8 Tag *current_tag = reinterpret_cast<Tag *>(heap_);
42 8 Tag *next_tag = current_tag->JumpToNext();
43 // Move a sliding window of two blocks over the heap and compact where
44 // possible
45
2/2
✓ Branch 0 taken 165090 times.
✓ Branch 1 taken 8 times.
165098 while (next_tag < heap_top) {
46
2/2
✓ Branch 1 taken 108139 times.
✓ Branch 2 taken 56951 times.
165090 if (current_tag->IsFree()) {
47
2/2
✓ Branch 1 taken 53186 times.
✓ Branch 2 taken 54953 times.
108139 if (next_tag->IsFree()) {
48 // Adjacent free blocks, merge and try again
49 53186 current_tag->size -= sizeof(Tag) + next_tag->GetSize();
50 53186 next_tag = next_tag->JumpToNext();
51 } else {
52 // Free block followed by a reserved block, move memory and create a
53 // new free tag at the end of the moved block
54 54953 const int64_t free_space = current_tag->size;
55 54953 current_tag->size = next_tag->size;
56 54953 memmove(current_tag->GetBlock(), next_tag->GetBlock(),
57 next_tag->GetSize());
58
1/2
✓ Branch 3 taken 54953 times.
✗ Branch 4 not taken.
54953 (*callback_ptr_)(BlockPtr(current_tag->GetBlock()));
59 54953 next_tag = current_tag->JumpToNext();
60 54953 next_tag->size = free_space;
61 }
62 } else {
63 // Current block allocated, move on
64 56951 current_tag = next_tag;
65 56951 next_tag = next_tag->JumpToNext();
66 }
67 }
68
69 8 gauge_ = (reinterpret_cast<unsigned char *>(current_tag) - heap_);
70
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
8 if (!current_tag->IsFree())
71 2 gauge_ += sizeof(Tag) + current_tag->GetSize();
72 }
73
74
75 4 void *MallocHeap::Expand(void *block, uint64_t new_size) {
76 4 const uint64_t old_size = GetSize(block);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert(old_size <= new_size);
78 4 void *new_block = Allocate(new_size, block, old_size);
79
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (new_block != NULL)
80 4 MarkFree(block);
81 4 return new_block;
82 }
83
84
85 4108 bool MallocHeap::HasSpaceFor(uint64_t nbytes) {
86 4108 return RoundUp8(gauge_ + nbytes + sizeof(Tag)) <= capacity_;
87 }
88
89
90 53196 void MallocHeap::MarkFree(void *block) {
91 53196 Tag *tag = reinterpret_cast<Tag *>(block) - 1;
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53196 times.
53196 assert(tag->size > 0);
93 53196 tag->size = -(tag->size);
94 53196 stored_ -= tag->GetSize();
95 53196 num_blocks_--;
96 // TODO(jblomer): if MarkFree() takes place at the top of the heap, one could
97 // move back the gauge_ pointer. If this is an optimization or unnecessary
98 // extra work depends on how the MallocHeap is used.
99 53196 }
100
101
102 49839 uint64_t MallocHeap::GetSize(void *block) {
103 49839 Tag *tag = reinterpret_cast<Tag *>(block) - 1;
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49839 times.
49839 assert(tag->size > 0);
105 49839 return tag->size;
106 }
107
108
109 506 MallocHeap::MallocHeap(uint64_t capacity, CallbackPtr callback_ptr)
110 506 : callback_ptr_(callback_ptr)
111 506 , capacity_(capacity)
112 506 , gauge_(0)
113 506 , stored_(0)
114 506 , num_blocks_(0) {
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 506 times.
506 assert(capacity_ > kMinCapacity);
116 // Ensure 8-byte alignment
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 506 times.
506 assert((capacity_ % 8) == 0);
118 506 heap_ = reinterpret_cast<unsigned char *>(sxmmap(capacity));
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 506 times.
506 assert(uintptr_t(heap_) % 8 == 0);
120 506 }
121
122
123 505 MallocHeap::~MallocHeap() { sxunmap(heap_, capacity_); }
124