16 #include "gu_vector.hpp"
17 #include "gu_alloc.hpp"
18 #include "gu_digest.hpp"
20 #ifdef GU_RSET_CHECK_SIZE
21 # include "gu_throw.hpp"
38 static Version
const MAX_VERSION = VER1;
49 size_t size()
const {
return size_; }
52 int count()
const {
return count_; }
62 CheckType check_type_;
65 RecordSet (Version
const version, CheckType
const ct);
69 : size_(0), count_(0), version_(EMPTY), check_type_(CHECK_NONE) {}
71 void init (
const byte_t* buf, ssize_t
size);
78 # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || (__GNUC__ > 4)
79 # pragma GCC diagnostic push
80 # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || (__GNUC__ > 4)
81 # pragma GCC diagnostic ignored "-Weffc++"
95 ssize_t
gather (GatherVector& out);
101 RecordSetOutBase (byte_t* reserved,
102 size_t reserved_size,
103 const BaseName& base_name,
106 Version version = MAX_VERSION
107 #ifdef GU_RSET_CHECK_SIZE
108 ,ssize_t max_size = 0x7fffffff
119 void process (
const R& record,
126 byte_t*
const dst(alloc_.
alloc (size, new_page));
128 new_page = (new_page || !prev_stored_);
132 record.serialize_to (dst, size);
134 size_t const ssize (record.serialize_to (dst, size));
135 assert (ssize == size);
142 void process (
const R& record,
151 process (record, ptr, new_page, size,
true, HasPtr<false>());
160 template <
class R,
bool has_ptr>
161 std::pair<const byte_t*, size_t>
162 append_base (
const R& record,
163 bool const store =
true,
164 bool const new_record =
true)
166 ssize_t
const size (record.serial_size());
168 #ifdef GU_RSET_CHECK_SIZE
169 if (gu_unlikely(size > max_size_ - size_)) gu_throw_error(EMSGSIZE);
175 process (record, ptr, new_page, size, store, HasPtr<has_ptr>());
177 prev_stored_ = store;
179 count_ += new_record || (0 == count_);
181 post_append (new_page, ptr, size);
183 return std::pair<const byte_t*, size_t>(ptr,
size);
188 #ifdef GU_RSET_CHECK_SIZE
189 ssize_t
const max_size_;
194 Vector<Buf, Allocator::INITIAL_VECTOR_SIZE> bufs_;
198 post_alloc (
bool const new_page,
const byte_t*
const ptr,
202 post_append (
bool const new_page,
const byte_t*
const ptr,
205 int header_size ()
const;
206 int header_size_max ()
const;
210 ssize_t write_header (byte_t* ptr, ssize_t size);
226 size_t reserved_size,
229 Version version = MAX_VERSION
230 #ifdef GU_RSET_CHECK_SIZE
231 ,ssize_t max_size = 0x7fffffff
235 #ifdef GU_RSET_CHECK_SIZE
241 std::pair<const byte_t*, size_t>
244 return append_base<R, false> (r);
248 std::pair<const byte_t*, size_t>
249 append (
const void*
const src, ssize_t
const size,
250 bool const store =
true,
bool const new_record =
true)
255 BufWrap bw (src, size);
256 return append_base<BufWrap, true> (bw, store, new_record);
266 const byte_t*
const ptr_;
271 BufWrap (
const void*
const ptr,
size_t const size)
272 : ptr_(reinterpret_cast<const byte_t*>(ptr)), size_(size)
275 size_t serial_size()
const {
return size_; }
276 const byte_t* ptr()
const {
return ptr_; }
278 size_t serialize_to (byte_t*
const dst,
size_t)
const
280 ::memcpy (dst, ptr_, size_);
297 bool check_now =
true);
300 void init (
const byte_t* buf,
302 bool check_now =
true);
305 void rewind()
const { next_ = begin_; }
307 void checksum()
const;
311 gu::Buf ret = { head_, size_ };
return ret;
317 void next_base (
Buf& n)
const
319 if (gu_likely (next_ < size_))
321 size_t const next_size(R::serial_size(head_ + next_, size_ -next_));
324 if (gu_likely (next_ + next_size <=
size_t(size_)))
326 n.ptr = head_ + next_;
332 throw_error (E_FAULT);
335 assert (next_ == size_);
337 throw_error (E_PERM);
343 if (gu_likely (next_ < size_))
345 R
const rec(head_ + next_, size_ - next_);
346 size_t const tmp_size(rec.serial_size());
349 if (gu_likely (next_ + tmp_size <=
size_t(size_)))
355 throw_error (E_FAULT);
358 assert (next_ == size_);
360 throw_error (E_PERM);
366 ssize_t
mutable next_;
371 void parse_header_v1 (
size_t size);
379 GU_NORETURN
void throw_error (Error code)
const;
393 std::swap(head_, r.head_);
394 std::swap(next_, r.next_);
395 std::swap(begin, r.begin_);
410 bool check_first =
true)
418 void next (
Buf& n)
const { next_base<R> (n); }
420 R next ()
const {
return next_base<R> (); }
424 #if defined(__GNUG__)
425 # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || (__GNUC__ > 4)
426 # pragma GCC diagnostic pop
427 # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || (__GNUC__ > 4)
byte_t * alloc(page_size_type const size, bool &new_page)
ssize_t gather(GatherVector &out)
Definition: gu_vector.hpp:61
ssize_t page_count() const
Definition: gu_rset.hpp:92
Definition: gu_rset.hpp:404
Definition: gu_rset.hpp:28
size_t size() const
Definition: gu_rset.hpp:49
Definition: gu_rset.hpp:114
Definition: gu_rset.hpp:217
int count() const
Definition: gu_rset.hpp:52
Definition: gu_rset.hpp:291
Definition: gu_rset.hpp:85
Definition: gu_alloc.hpp:31