| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/ring_buffer.cc |
| Date: | 2025-11-30 02:35:17 |
| 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 | 22 | RingBuffer::RingBuffer(size_t total_size) | |
| 18 | 22 | : total_size_(total_size) | |
| 19 | 22 | , free_space_(total_size) | |
| 20 | 22 | , front_(0) | |
| 21 | 22 | , back_(0) | |
| 22 | 22 | , buffer_(reinterpret_cast<unsigned char *>(sxmmap(total_size_))) { | |
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | assert(total_size_ >= sizeof(size_t)); |
| 24 | 22 | } | |
| 25 | |||
| 26 | |||
| 27 | 22 | RingBuffer::~RingBuffer() { sxunmap(buffer_, total_size_); } | |
| 28 | |||
| 29 | |||
| 30 | 4026 | void RingBuffer::Put(const void *data, size_t size) { | |
| 31 | 4026 | const size_t size_head = std::min(size, total_size_ - front_); | |
| 32 |
2/2✓ Branch 0 taken 4024 times.
✓ Branch 1 taken 2 times.
|
4026 | if (size_head > 0) |
| 33 | 4024 | memcpy(buffer_ + front_, data, size_head); | |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 678 times.
✓ Branch 1 taken 3348 times.
|
4026 | if (size_head < size) { |
| 36 | 678 | const size_t size_tail = size - size_head; | |
| 37 | 678 | memcpy(buffer_, reinterpret_cast<const unsigned char *>(data) + size_head, | |
| 38 | size_tail); | ||
| 39 | } | ||
| 40 | |||
| 41 | 4026 | front_ = (front_ + size) % total_size_; | |
| 42 | 4026 | free_space_ -= size; | |
| 43 | 4026 | } | |
| 44 | |||
| 45 | |||
| 46 | 207606 | void RingBuffer::Get(size_t from, size_t size, void *to) const { | |
| 47 | 207606 | const size_t size_head = std::min(size, total_size_ - from); | |
| 48 |
2/2✓ Branch 0 taken 197898 times.
✓ Branch 1 taken 9708 times.
|
207606 | if (size_head > 0) |
| 49 | 197898 | memcpy(to, buffer_ + from, size_head); | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 17438 times.
✓ Branch 1 taken 190168 times.
|
207606 | if (size_head < size) { |
| 52 | 17438 | const size_t size_tail = size - size_head; | |
| 53 | 17438 | memcpy(reinterpret_cast<unsigned char *>(to) + size_head, buffer_, | |
| 54 | size_tail); | ||
| 55 | } | ||
| 56 | 207606 | } | |
| 57 | |||
| 58 | |||
| 59 | 3996 | void RingBuffer::Shrink(size_t by) { | |
| 60 | 3996 | back_ = (back_ + by) % total_size_; | |
| 61 | 3996 | free_space_ += by; | |
| 62 | 3996 | } | |
| 63 | |||
| 64 | |||
| 65 | 6001 | size_t RingBuffer::GetObjectSize(ObjectHandle_t handle) const { | |
| 66 | size_t size_tag; | ||
| 67 | 6001 | Get(handle, sizeof(size_tag), &size_tag); | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6001 times.
|
6001 | assert(size_tag <= total_size_); |
| 69 | 6001 | return size_tag; | |
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | 2015 | RingBuffer::ObjectHandle_t RingBuffer::PushFront(const void *obj, size_t size) { | |
| 74 | 2015 | size_t size_tag = size; | |
| 75 | 2015 | size += sizeof(size_tag); | |
| 76 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2013 times.
|
2015 | if (size > free_space_) { |
| 77 | 2 | return kInvalidObjectHandle; | |
| 78 | } | ||
| 79 | |||
| 80 | 2013 | const ObjectHandle_t result = front_; | |
| 81 | |||
| 82 | 2013 | Put(&size_tag, sizeof(size_tag)); | |
| 83 | 2013 | Put(obj, size_tag); | |
| 84 | |||
| 85 | 2013 | return result; | |
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | 1998 | RingBuffer::ObjectHandle_t RingBuffer::RemoveBack() { | |
| 90 | 1998 | const ObjectHandle_t result = back_; | |
| 91 | |||
| 92 | 1998 | const size_t size_tag = GetObjectSize(result); | |
| 93 | 1998 | Shrink(sizeof(size_tag)); | |
| 94 | 1998 | Shrink(size_tag); | |
| 95 | |||
| 96 | 1998 | return result; | |
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | 1996 | void RingBuffer::CopyObject(ObjectHandle_t handle, void *to) const { | |
| 101 | 1996 | const size_t size_tag = GetObjectSize(handle); | |
| 102 | 1996 | const ObjectHandle_t object = (handle + sizeof(size_tag)) % total_size_; | |
| 103 | 1996 | Get(object, size_tag, to); | |
| 104 | 1996 | } | |
| 105 | |||
| 106 | |||
| 107 | 199609 | void RingBuffer::CopySlice(ObjectHandle_t handle, size_t size, size_t offset, | |
| 108 | void *to) const { | ||
| 109 | 199609 | const ObjectHandle_t begin = (handle + sizeof(size_t) + offset) % total_size_; | |
| 110 | 199609 | Get(begin, size, to); | |
| 111 | 199609 | } | |
| 112 |