Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
FileMappedStorage.h
Go to the documentation of this file.
1#pragma once
2#include <gii/global.h>
3#include <iostream>
5#include <misc/gen/TVector.h>
6#include <misc/gen/Thread.h>
7
8namespace sf
9{
10
55{
56 public:
60 typedef uint64_t size_type;
61
65 static constexpr size_t npos = std::numeric_limits<size_type>::max();
66
73 FileMappedStorage(size_type seg_sz, size_type blk_sz, size_type recycle = 0);
74
79
84
92 bool reserve(size_type block_count);
93
99 void flush();
100
108 inline bool blockWrite(size_type ofs, size_type sz, const void* src);
109
117 inline bool blockRead(size_type ofs, size_type sz, void* dst) const;
118
125 [[nodiscard]] size_type getBlockCount() const;
126
130 [[nodiscard]] size_type getSegmentCount() const;
131
135 [[nodiscard]] size_type getSegmentLocks() const;
136
140 [[nodiscard]] size_type getBlockSize() const;
141
145 [[nodiscard]] size_type getSegmentSize() const;
146
152 [[nodiscard]] size_type getSize() const;
153
157 [[nodiscard]] inline size_type getRecycleCount() const;
158
166
170 std::ostream& writeStatus(std::ostream& os) const;
171
172 private:
181 bool blockReadWrite(bool rd, size_type ofs, size_type sz, void* src);
182
186 class Segment
187 {
188 public:
193 explicit Segment(size_type sz);
194
198 ~Segment();
199
205 [[nodiscard]] inline bool isLocked() const;
206
212 [[nodiscard]] inline size_type getSize() const;
213
219 [[nodiscard]] inline void* lockMemory();
220
226 [[nodiscard]] inline const void* lockMemory() const;
227
231 inline void unlockMemory() const;
232
240 bool write(size_type ofs, size_type sz, const void* src);
241
249 bool read(size_type ofs, size_type sz, void* dst) const;
250
254 Segment(Segment&) = delete;
255
260 Segment& operator=(Segment&) = delete;
261
262 protected:
267 bool doLockMemory();
268
272 void doUnlockMemory();
273
274 private:
278 int _lockCount{0};
282 size_type _size{0};
286 char* _dataPtr{nullptr};
290 IFileMapper& _fileMapper;
291
292 friend class FileMappedStorage;
293 };
294
298 struct Reference
299 {
303 Reference();
304
308 ~Reference();
309
313 Thread::id_type _threadId{0};
317 size_type _blockSize{0};
321 size_type _segmentSize{0};
325 int _referenceCount{0};
329 TVector<Segment*> _segmentList;
333 Mutex _mutex;
337 typedef Mutex::Lock MtLock;
338 };
339
343 Reference* _reference{nullptr};
344
345 public:
350 {
351 public:
357 explicit inline Lock(FileMappedStorage& ds);
358
365 inline Lock(FileMappedStorage& ds, size_type seg_idx);
366
370 ~Lock();
371
378 bool acquire(size_type seg_idx);
379
384 [[nodiscard]] bool isAcquired() const;
385
390 explicit operator bool() const;
391
398 void* data(size_type blk_ofs);
399
406 template<typename T = void*>
407 T* ptr(size_type blk_ofs)
408 {
409 return static_cast<T*>(data(blk_ofs));
410 }
411
415 void release();
416
417 private:
421 FileMappedStorage& _store;
425 size_type _segmentIndex{0};
429 size_type _blockSize{0};
433 Segment* _segment{nullptr};
437 void* _data{nullptr};
438 };
439
440 private:
444 bool cacheSegment(size_type idx);
445
449 void uncacheSegment(size_type idx);
450
455 size_type _cachedSegmentIndex{npos};
459 size_type _segmentRecycleCount{0};
463 static Mutex _staticSync;
464};
465
466inline FileMappedStorage::size_type FileMappedStorage::Segment::getSize() const
467{
468 return _size;
469}
470
471inline bool FileMappedStorage::Segment::isLocked() const
472{
473 return (_lockCount > 0);
474}
475
476inline void* FileMappedStorage::Segment::lockMemory()
477{
478 doLockMemory();
479 return _dataPtr;
480}
481
482inline const void* FileMappedStorage::Segment::lockMemory() const
483{
484 const_cast<Segment*>(this)->doLockMemory();
485 return const_cast<Segment*>(this)->_dataPtr;
486}
487
488inline void FileMappedStorage::Segment::unlockMemory() const
489{
490 const_cast<Segment*>(this)->doUnlockMemory();
491}
492
493inline bool FileMappedStorage::blockWrite(size_type ofs, size_type sz, const void* src)
494{
495 return blockReadWrite(false, ofs, sz, (void*) src);
496}
497
498inline bool FileMappedStorage::blockRead(size_type ofs, size_type sz, void* dst) const
499{
500 return const_cast<FileMappedStorage*>(this)->blockReadWrite(true, ofs, sz, dst);
501}
502
504{
505 return _segmentRecycleCount;
506}
507
509 : _store(ds)
510 , _blockSize(ds.getBlockSize())
511{}
512
514 : _store(ds)
515 , _segmentIndex(-1)
516 , _blockSize(ds.getBlockSize())
517{
518 acquire(seg_idx);
519}
520
522{
523 release();
524}
525
527{
528 return _data != nullptr;
529}
530
531inline FileMappedStorage::Lock::operator bool() const
532{
533 return isAcquired();
534}
535
536}// namespace sf
~Lock()
Destructor.
Definition FileMappedStorage.h:521
Lock(FileMappedStorage &ds)
Constructor.
Definition FileMappedStorage.h:508
bool isAcquired() const
Gets the segment acquired status.
Definition FileMappedStorage.h:526
void * data(size_type blk_ofs)
Returns pointer to .
T * ptr(size_type blk_ofs)
Definition FileMappedStorage.h:407
void release()
Unlocks memory locked by the segment.
bool acquire(size_type seg_idx)
Tries to lock the segment index of the data store and returns true when successful.
FileMappedStorage(const FileMappedStorage &)
Copy constructor.
bool reserve(size_type block_count)
Reserves an amount of blocks and rounds it up to the nearest segment size.
size_type getSegmentLocks() const
Gets the accumulation of locks on segments.
virtual ~FileMappedStorage()
Virtual destructor.
bool blockRead(size_type ofs, size_type sz, void *dst) const
Function that store blocks in several segments when needed.
Definition FileMappedStorage.h:498
std::ostream & writeStatus(std::ostream &os) const
Writes the status to the output stream.
static constexpr size_t npos
Unsigned size value indicating not found or no index.
Definition FileMappedStorage.h:65
FileMappedStorage(size_type seg_sz, size_type blk_sz, size_type recycle=0)
bool blockWrite(size_type ofs, size_type sz, const void *src)
Function that store blocks in several segments when needed.
Definition FileMappedStorage.h:493
size_type getBlockCount() const
Gets the amount of blocks reserved.
uint64_t size_type
Type used for sizes of storage.
Definition FileMappedStorage.h:60
void flush()
Flushes all data stored by this instance.
bool setRecycleCount(size_type count)
Sets the recycle segment count.
size_type getSegmentCount() const
Gets the amount of segments used by this instance.
size_type getBlockSize() const
Gets the block size in bytes of this instance.
size_type getRecycleCount() const
Gets the amount of segments being recycled.
Definition FileMappedStorage.h:503
size_type getSize() const
size_type getSegmentSize() const
Gets the size of the segments in blocks of this instance.
#define _GII_CLASS
Definition gii/global.h:38
Definition Application.h:10