BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/RawFile/include/RawFile/RawDataCache.h
Go to the documentation of this file.
1#ifndef RAW_DATA_CACHE_H
2#define RAW_DATA_CACHE_H
3
4#include <cstdint>
5#include <cstdlib>
6#include <zlib.h>
7
8class RawDataCache final
9{
10 public:
11 RawDataCache() = default;
12 RawDataCache(uint64_t size);
14
15 template<typename T>
16 T* data() { return reinterpret_cast<T*>(m_data); }
17
18 uint64_t capacity() { return m_capacity; } // in bytes
19
20 // expand the cache size if necessary
21 template<typename T>
22 T *reserve(uint64_t size) {
23 if (uint64_t _size = size * sizeof(T); m_capacity < _size) {
24 free(m_data);
25 if (m_capacity == 0) m_capacity = 128*1024;
26 do {
27 m_capacity *= 2;
28 } while (m_capacity < _size);
29 m_data = malloc(m_capacity);
30 }
31 return reinterpret_cast<T*>(m_data);
32 }
33
34 // expand the cache size and keep the original data if necessary
35 template<typename T>
36 T *expand(uint64_t size) {
37 if (uint64_t _size = size * sizeof(T); m_capacity < _size) {
38 do {
39 m_capacity *= 2;
40 } while (m_capacity < _size);
41 m_data = realloc(m_data, m_capacity);
42 }
43 return reinterpret_cast<T*>(m_data);
44 }
45
46 private:
47 void *m_data{nullptr};
48 uint64_t m_capacity{0};
49
50 RawDataCache(const RawDataCache &) = delete;
51 RawDataCache &operator=(const RawDataCache &) = delete;
52};
53
54#endif
RawDataCache()=default