Joedb 10.4.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1#ifndef joedb_Buffer_declared
2#define joedb_Buffer_declared
3
5
6#include <stdint.h>
7#include <cstring>
8
9namespace joedb
10{
11 /// @ingroup journal
12 template<int log_size> class Buffer
13 {
14 public:
15 static constexpr size_t size = (1 << log_size);
16 static constexpr ptrdiff_t ssize = (1 << log_size);
17 static constexpr size_t extra_size = 8;
18
20 size_t index;
21
22 //////////////////////////////////////////////////////////////////////////
23 template<typename T> void write(T x)
24 //////////////////////////////////////////////////////////////////////////
25 {
26 JOEDB_DEBUG_ASSERT(index + sizeof(T) < size + extra_size);
27 std::memcpy(data + index, (const char *)&x, sizeof(T));
28 index += sizeof(T);
29 }
30
31 //////////////////////////////////////////////////////////////////////////
32 template<typename T> T read()
33 //////////////////////////////////////////////////////////////////////////
34 {
35 JOEDB_DEBUG_ASSERT(index + sizeof(T) < size + extra_size);
36 T result;
37 std::memcpy((char *)&result, data + index, sizeof(T));
38 index += sizeof(T);
39 return result;
40 }
41
42 //////////////////////////////////////////////////////////////////////////
43 template<typename T> void compact_write(T x)
44 //////////////////////////////////////////////////////////////////////////
45 {
46 JOEDB_DEBUG_ASSERT(x >= 0);
47
48 if (x < 0x20)
49 {
50 if (x < 0)
51 write<uint8_t>(0);
52 else
53 write<uint8_t>(uint8_t(x));
54 }
55 else if (x < 0x20 * 0x100)
56 {
57 write<uint8_t>(uint8_t(0x20 | (x >> 8)));
58 write<uint8_t>(uint8_t(x));
59 }
60 else
61 {
62 uint32_t extra_bytes = 2;
63
64 while ((x >> (8 * extra_bytes)) >= 0x20 && extra_bytes < sizeof(T) - 1)
65 extra_bytes++;
66
67 write<uint8_t>(uint8_t((extra_bytes << 5) | (x >> (8 * extra_bytes))));
68 write<uint8_t>(uint8_t(x >> (8 * --extra_bytes)));
69 write<uint8_t>(uint8_t(x >> (8 * --extra_bytes)));
70 while (extra_bytes)
71 write<uint8_t>(uint8_t(x >> (8 * --extra_bytes)));
72 }
73 }
74
75 //////////////////////////////////////////////////////////////////////////
76 template<typename T> T compact_read()
77 //////////////////////////////////////////////////////////////////////////
78 {
79 const uint8_t first_byte = read<uint8_t>();
80#ifdef JOEDB_DIRTY_WORKAROUND
81 if (first_byte == 0xff)
82 return 0;
83#endif
84 int extra_bytes = first_byte >> 5;
85 T result = first_byte & 0x1f;
86 while (--extra_bytes >= 0)
87 result = T((result << 8) | read<uint8_t>());
88 return result;
89 }
90 };
91}
92
93#endif
T compact_read()
Definition Buffer.h:76
size_t index
Definition Buffer.h:20
void write(T x)
Definition Buffer.h:23
static constexpr size_t extra_size
Definition Buffer.h:17
char data[size+extra_size]
Definition Buffer.h:19
static constexpr size_t size
Definition Buffer.h:15
void compact_write(T x)
Definition Buffer.h:43
static constexpr ptrdiff_t ssize
Definition Buffer.h:16
#define JOEDB_DEBUG_ASSERT(x)
assertion tested in debug mode
Definition assert.h:19