| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#ifndef CVMFS_RING_BUFFER_H_ |
| 6 |
|
|
#define CVMFS_RING_BUFFER_H_ |
| 7 |
|
|
|
| 8 |
|
|
#include <cstddef> |
| 9 |
|
|
|
| 10 |
|
|
#include "util/single_copy.h" |
| 11 |
|
|
|
| 12 |
|
|
/** |
| 13 |
|
|
* A ring buffer that allows appending objects to the front and removing |
| 14 |
|
|
* objects from the back. In the memory area, we prepend the object data with |
| 15 |
|
|
* the size of the object. |
| 16 |
|
|
*/ |
| 17 |
|
|
class RingBuffer : SingleCopy { |
| 18 |
|
|
public: |
| 19 |
|
|
/** |
| 20 |
|
|
* The offset in buffer_ |
| 21 |
|
|
*/ |
| 22 |
|
|
typedef size_t ObjectHandle_t; |
| 23 |
|
|
static const ObjectHandle_t kInvalidObjectHandle; |
| 24 |
|
|
|
| 25 |
|
|
explicit RingBuffer(size_t total_size); |
| 26 |
|
|
~RingBuffer(); |
| 27 |
|
|
|
| 28 |
|
|
/** |
| 29 |
|
|
* Returns kInvalidObjectHandle if there is not enough space to insert the |
| 30 |
|
|
* object. |
| 31 |
|
|
*/ |
| 32 |
|
|
ObjectHandle_t PushFront(const void *obj, size_t size); |
| 33 |
|
|
|
| 34 |
|
|
/** |
| 35 |
|
|
* Returns the handle of the remove object or kInvalidObjectHandle if the |
| 36 |
|
|
* ring buffer is empty |
| 37 |
|
|
*/ |
| 38 |
|
|
ObjectHandle_t RemoveBack(); |
| 39 |
|
|
|
| 40 |
|
|
/** |
| 41 |
|
|
* The passed object handle must be valid |
| 42 |
|
|
*/ |
| 43 |
|
|
size_t GetObjectSize(ObjectHandle_t handle) const; |
| 44 |
|
|
|
| 45 |
|
|
/** |
| 46 |
|
|
* Note that we cannot just return a pointer because an object may be |
| 47 |
|
|
* split between the end and the beginning of the backing memory area |
| 48 |
|
|
*/ |
| 49 |
|
|
void CopyObject(ObjectHandle_t handle, void *to) const; |
| 50 |
|
|
|
| 51 |
|
|
/** |
| 52 |
|
|
* Copies a sub range of the object |
| 53 |
|
|
*/ |
| 54 |
|
|
void CopySlice(ObjectHandle_t handle, size_t size, size_t offset, |
| 55 |
|
|
void *to) const; |
| 56 |
|
|
|
| 57 |
|
|
// All objects are prepended by a size tag, so we can store objects only |
| 58 |
|
|
// up to the available space minus the size of the size tag |
| 59 |
|
15 |
size_t GetMaxObjectSize() const { return total_size_ - sizeof(size_t); } |
| 60 |
|
15 |
bool HasSpaceFor(size_t size) const { |
| 61 |
|
15 |
return free_space_ >= size + sizeof(size_t); |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
45 |
size_t free_space() const { return free_space_; } |
| 65 |
|
|
|
| 66 |
|
|
private: |
| 67 |
|
|
/** |
| 68 |
|
|
* Writes data into the ring buffer at front_, taking care of a potential |
| 69 |
|
|
* buffer wrap. Assumes that the buffer has enough space. |
| 70 |
|
|
*/ |
| 71 |
|
|
void Put(const void *data, size_t size); |
| 72 |
|
|
/** |
| 73 |
|
|
* Copies data from the buffer to the memory area "to". The memory area |
| 74 |
|
|
* has to be big enough. Takes care of a potential buffer wrap. |
| 75 |
|
|
*/ |
| 76 |
|
|
void Get(size_t from, size_t size, void *to) const; |
| 77 |
|
|
/** |
| 78 |
|
|
* Moves the back_ pointer forward, taking care of potential buffer wraps |
| 79 |
|
|
*/ |
| 80 |
|
|
void Shrink(size_t by); |
| 81 |
|
|
|
| 82 |
|
|
/** |
| 83 |
|
|
* Size in bytes of the memory area backing the ring buffer |
| 84 |
|
|
*/ |
| 85 |
|
|
const size_t total_size_; |
| 86 |
|
|
/** |
| 87 |
|
|
* Size in bytes of the unused space in the buffer |
| 88 |
|
|
*/ |
| 89 |
|
|
size_t free_space_; |
| 90 |
|
|
/** |
| 91 |
|
|
* Pointer to the front of the memory buffer, in [0, size_ - 1] |
| 92 |
|
|
*/ |
| 93 |
|
|
size_t front_; |
| 94 |
|
|
/** |
| 95 |
|
|
* Pointer to the back of the memory buffer, in [0, size_ - 1] |
| 96 |
|
|
*/ |
| 97 |
|
|
size_t back_; |
| 98 |
|
|
/** |
| 99 |
|
|
* The memory area backing the ring buffer |
| 100 |
|
|
*/ |
| 101 |
|
|
unsigned char *buffer_; |
| 102 |
|
|
}; // class RingBuffer |
| 103 |
|
|
|
| 104 |
|
|
#endif // CVMFS_RING_H_ |
| 105 |
|
|
|