libcryfs/random/PseudoRandomPool.h

40 lines
1.0 KiB
C
Raw Normal View History

2015-10-22 17:36:03 +02:00
#pragma once
#ifndef MESSMER_CPPUTILS_RANDOM_PSEUDORANDOMPOOL_H
#define MESSMER_CPPUTILS_RANDOM_PSEUDORANDOMPOOL_H
#include <boost/thread.hpp>
#include "RandomGenerator.h"
#include "ThreadsafeRandomDataBuffer.h"
#include "RandomGeneratorThread.h"
#include <mutex>
namespace cpputils {
//TODO Test
class PseudoRandomPool final : public RandomGenerator {
public:
PseudoRandomPool();
protected:
void _get(void *target, size_t bytes) override;
2015-10-22 17:36:03 +02:00
private:
static constexpr size_t MIN_BUFFER_SIZE = 1*1024*1024; // 1MB
static constexpr size_t MAX_BUFFER_SIZE = 2*1024*1024; // 2MB
ThreadsafeRandomDataBuffer _buffer;
RandomGeneratorThread _refillThread;
DISALLOW_COPY_AND_ASSIGN(PseudoRandomPool);
};
inline void PseudoRandomPool::_get(void *target, size_t bytes) {
2015-10-22 17:36:03 +02:00
_buffer.get(target, bytes);
}
inline PseudoRandomPool::PseudoRandomPool(): _buffer(), _refillThread(&_buffer, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE) {
_refillThread.start();
}
}
#endif