-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfqueue.h
More file actions
97 lines (75 loc) · 1.91 KB
/
fqueue.h
File metadata and controls
97 lines (75 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef _FQUEUE_H_
#define _FQUEUE_H_
#include <memory>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstddef>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
class File_IO {
public:
File_IO() = delete;
File_IO(const File_IO &) = delete;
File_IO& operator= (const File_IO&) = delete;
File_IO(const char* fname);
~File_IO();
bool is_open();
void close();
std::size_t size();
bool resize(std::size_t size);
bool seek(std::size_t pos);
bool write(const void* ptr, std::size_t size);
bool read(void* ptr, std::size_t size);
bool remove_file();
private:
std::string m_fname;
int m_fd;
};
class Fqueue {
public:
Fqueue() = delete;
Fqueue(const Fqueue &) = delete;
Fqueue operator= (const Fqueue &) = delete;
Fqueue(const char* fname, size_t size = 32);
virtual ~Fqueue();
//get them number of records in the data-file
std::size_t records() ;
std::size_t alloc_records() ;
bool empty() ;
void reset();
//clear records
void truncate();
void push(const void* ptr, std::uint32_t size);
//一个记录的结构
struct record{
std::unique_ptr<char[]> ptr;
std::uint32_t size;
};
record front();
record pop();
record first_record() ;
record next_record();
//删除文件队列,即删除文件
bool remove_fqueue();
private:
//每个文件队列头部都有这个这样的信息对文件进行记录并控制
struct queue_info {
std::uint64_t rpos;
std::uint64_t wpos;
std::uint64_t records;
std::uint64_t alloc_records;
};
private:
File_IO m_file;
const std::size_t m_fsize;
private:
Fqueue::record read();
void write_info(const queue_info &qi);
void read_info(queue_info *qi);
private:
enum { ENUM_SIZE_OF_QUEUE_INFO = sizeof(queue_info) };
static_assert(ENUM_SIZE_OF_QUEUE_INFO == sizeof(std::uint64_t)*4, "bad sizeof(queue_info)");
};
#endif