GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ring_buffer.cc
Date: 2026-11-08 02:41:18
Exec Total Coverage
Lines: 59 59 100.0%
Branches: 12 14 85.7%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "ring_buffer.h"
6
7 #include <algorithm>
8 #include <cassert>
9 #include <cstdlib>
10 #include <cstring>
11
12 #include "util/smalloc.h"
13
14 const RingBuffer::ObjectHandle_t RingBuffer::kInvalidObjectHandle = size_t(-1);
15
16
17 44 RingBuffer::RingBuffer(size_t total_size)
18 44 : total_size_(total_size)
19 44 , free_space_(total_size)
20 44 , front_(0)
21 44 , back_(0)
22 44 , buffer_(reinterpret_cast<unsigned char *>(sxmmap(total_size_))) {
23
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 assert(total_size_ >= sizeof(size_t));
24 44 }
25
26
27 44 RingBuffer::~RingBuffer() { sxunmap(buffer_, total_size_); }
28
29
30 32076 void RingBuffer::Put(const void *data, size_t size) {
31 32076 const size_t size_head = std::min(size, total_size_ - front_);
32
2/2
✓ Branch 0 taken 32060 times.
✓ Branch 1 taken 16 times.
32076 if (size_head > 0)
33 32060 memcpy(buffer_ + front_, data, size_head);
34
35
2/2
✓ Branch 0 taken 5424 times.
✓ Branch 1 taken 26652 times.
32076 if (size_head < size) {
36 5424 const size_t size_tail = size - size_head;
37 5424 memcpy(buffer_, reinterpret_cast<const unsigned char *>(data) + size_head,
38 size_tail);
39 }
40
41 32076 front_ = (front_ + size) % total_size_;
42 32076 free_space_ -= size;
43 32076 }
44
45
46 1660716 void RingBuffer::Get(size_t from, size_t size, void *to) const {
47 1660716 const size_t size_head = std::min(size, total_size_ - from);
48
2/2
✓ Branch 0 taken 1583052 times.
✓ Branch 1 taken 77664 times.
1660716 if (size_head > 0)
49 1583052 memcpy(to, buffer_ + from, size_head);
50
51
2/2
✓ Branch 0 taken 139504 times.
✓ Branch 1 taken 1521212 times.
1660716 if (size_head < size) {
52 139504 const size_t size_tail = size - size_head;
53 139504 memcpy(reinterpret_cast<unsigned char *>(to) + size_head, buffer_,
54 size_tail);
55 }
56 1660716 }
57
58
59 31968 void RingBuffer::Shrink(size_t by) {
60 31968 back_ = (back_ + by) % total_size_;
61 31968 free_space_ += by;
62 31968 }
63
64
65 47942 size_t RingBuffer::GetObjectSize(ObjectHandle_t handle) const {
66 size_t size_tag;
67 47942 Get(handle, sizeof(size_tag), &size_tag);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47942 times.
47942 assert(size_tag <= total_size_);
69 47942 return size_tag;
70 }
71
72
73 16054 RingBuffer::ObjectHandle_t RingBuffer::PushFront(const void *obj, size_t size) {
74 16054 size_t size_tag = size;
75 16054 size += sizeof(size_tag);
76
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16038 times.
16054 if (size > free_space_) {
77 16 return kInvalidObjectHandle;
78 }
79
80 16038 const ObjectHandle_t result = front_;
81
82 16038 Put(&size_tag, sizeof(size_tag));
83 16038 Put(obj, size_tag);
84
85 16038 return result;
86 }
87
88
89 15984 RingBuffer::ObjectHandle_t RingBuffer::RemoveBack() {
90 15984 const ObjectHandle_t result = back_;
91
92 15984 const size_t size_tag = GetObjectSize(result);
93 15984 Shrink(sizeof(size_tag));
94 15984 Shrink(size_tag);
95
96 15984 return result;
97 }
98
99
100 15968 void RingBuffer::CopyObject(ObjectHandle_t handle, void *to) const {
101 15968 const size_t size_tag = GetObjectSize(handle);
102 15968 const ObjectHandle_t object = (handle + sizeof(size_tag)) % total_size_;
103 15968 Get(object, size_tag, to);
104 15968 }
105
106
107 1596806 void RingBuffer::CopySlice(ObjectHandle_t handle, size_t size, size_t offset,
108 void *to) const {
109 1596806 const ObjectHandle_t begin = (handle + sizeof(size_t) + offset) % total_size_;
110 1596806 Get(begin, size, to);
111 1596806 }
112