Serialization for idalloc and animqueue

This commit is contained in:
2021-03-13 13:05:00 -05:00
parent 2e0befe817
commit 4426fa157a
7 changed files with 289 additions and 39 deletions

View File

@@ -139,6 +139,18 @@ void StreamBuffer::write_int64(int64_t v) {
write_cursor_ += 8;
}
void StreamBuffer::write_float(float f) {
make_space(4);
memcpy(write_cursor_, &f, 4);
write_cursor_ += 4;
}
void StreamBuffer::write_double(double d) {
make_space(8);
memcpy(write_cursor_, &d, 8);
write_cursor_ += 8;
}
void StreamBuffer::write_bytes(const char *s, int64_t len) {
make_space(len);
memcpy(write_cursor_, s, len);
@@ -212,6 +224,34 @@ int64_t StreamBuffer::read_int64() {
return v;
}
float StreamBuffer::read_float() {
check_available(4);
float f;
memcpy(&f, read_cursor_, 4);
read_cursor_ += 4;
return f;
}
double StreamBuffer::read_double() {
check_available(8);
double d;
memcpy(&d, read_cursor_, 8);
read_cursor_ += 8;
return d;
}
size_t StreamBuffer::read_size() {
return read_size_limit(0xFFFFFFF);
}
size_t StreamBuffer::read_size_limit(size_t limit) {
int64_t value = read_int64();
if ((value < 0)||(value > int64_t(limit))) {
throw StreamCorruption();
}
return size_t(value);
}
const char *StreamBuffer::read_bytes(int64_t bytes) {
check_available(bytes);
char *data = read_cursor_;
@@ -219,7 +259,11 @@ const char *StreamBuffer::read_bytes(int64_t bytes) {
return data;
}
std::string StreamBuffer::read_string(int64_t max_allowed) {
std::string StreamBuffer::read_string() {
return read_string_limit(0xFFFFFFF);
}
std::string StreamBuffer::read_string_limit(int64_t max_allowed) {
int64_t len = read_uint8();
if (len == 255) {
len = read_int64();
@@ -388,9 +432,9 @@ LuaDefine(unittests_streambuffer, "c") {
sb11.write_string("");
sb11.write_string("de");
assert(sb11.layout_is(0, 8, 3));
assert(sb11.read_string(1000) == "abc");
assert(sb11.read_string(1000) == "");
assert(sb11.read_string(1000) == "de");
assert(sb11.read_string() == "abc");
assert(sb11.read_string() == "");
assert(sb11.read_string() == "de");
return 0;
}