This is an automated email from the ASF dual-hosted git repository.
colinlee pushed a commit to branch fix_err
in repository https://gitbox.apache.org/repos/asf/tsfile.git
The following commit(s) were added to refs/heads/fix_err by this push:
new 605939fd fix length
605939fd is described below
commit 605939fdd2f07bf350595a053d750e7ee51a8427
Author: ColinLee <[email protected]>
AuthorDate: Tue Jun 24 17:56:04 2025 +0800
fix length
---
cpp/src/common/allocator/alloc_base.h | 8 +-
cpp/src/common/allocator/byte_stream.h | 283 ++++++++++++++---------------
cpp/src/common/allocator/mem_alloc.cc | 30 +--
cpp/src/common/error_info/error_define.inc | 7 +-
cpp/src/common/error_info/error_info.cc | 62 ++++---
cpp/src/common/error_info/error_info.h | 16 +-
6 files changed, 207 insertions(+), 199 deletions(-)
diff --git a/cpp/src/common/allocator/alloc_base.h
b/cpp/src/common/allocator/alloc_base.h
index 153516fc..2cca8e37 100644
--- a/cpp/src/common/allocator/alloc_base.h
+++ b/cpp/src/common/allocator/alloc_base.h
@@ -75,15 +75,15 @@ enum AllocModID {
extern const char *g_mod_names[__LAST_MOD_ID];
/* very basic alloc/free interface in C style */
-void *mem_alloc(uint32_t size, AllocModID mid);
+void *mem_alloc(size_t size, AllocModID mid);
void mem_free(void *ptr);
-void *mem_realloc(void *ptr, uint32_t size);
+void *mem_realloc(void *ptr, size_t size);
/* base allocator */
class BaseAllocator {
public:
- void *alloc(uint32_t size, AllocModID mid) { return mem_alloc(size, mid); }
- void free(void *ptr) { mem_free(ptr); }
+ static void *alloc(const size_t size, const AllocModID mid) { return
mem_alloc(size, mid); }
+ static void free(void *ptr) { mem_free(ptr); }
};
extern BaseAllocator g_base_allocator;
diff --git a/cpp/src/common/allocator/byte_stream.h
b/cpp/src/common/allocator/byte_stream.h
index e0a1be89..348af328 100644
--- a/cpp/src/common/allocator/byte_stream.h
+++ b/cpp/src/common/allocator/byte_stream.h
@@ -33,6 +33,8 @@
namespace common {
+using E_CODE = error_info::E_CODE;
+
template <typename T>
class OptionalAtomic {
public:
@@ -236,7 +238,7 @@ class ByteStream {
private:
struct Page {
OptionalAtomic<Page *> next_; // 9 bytes
- const uint8_t *buf_; // 8 bytes
+ uint8_t *buf_; // 8 bytes
explicit Page(bool enable_atomic) : next_(nullptr, enable_atomic) {
buf_ = (uint8_t
@@ -287,9 +289,9 @@ class ByteStream {
/* ================ Part 0: wrap from outside buffer ================ */
// if you wrap a buffer as a ByteStream, you should
// manage the outside buffer yourself.
- void wrap_from(const char *buf, const size_t buf_len) {
+ void wrap_from(char *buf, const size_t buf_len) {
wrapped_page_.next_.store(nullptr);
- wrapped_page_.buf_ = reinterpret_cast<uint8_t const *>(buf);
+ wrapped_page_.buf_ = reinterpret_cast<uint8_t *>(buf);
page_size_ = buf_len;
head_.store(&wrapped_page_);
@@ -306,14 +308,14 @@ class ByteStream {
void clear_wrapped_buf() { wrapped_page_.buf_ = nullptr; }
/* ================ Part 1: basic ================ */
- FORCE_INLINE int64_t remaining_size() const {
+ FORCE_INLINE size_t remaining_size() const {
ASSERT(total_size_.load() >= read_pos_);
return total_size_.load() - read_pos_;
}
FORCE_INLINE bool has_remaining() const { return remaining_size() > 0; }
FORCE_INLINE void mark_read_pos() { marked_read_pos_ = read_pos_; }
- FORCE_INLINE int64_t get_mark_len() const {
+ FORCE_INLINE size_t get_mark_len() const {
ASSERT(marked_read_pos_ <= read_pos_);
return read_pos_ - marked_read_pos_;
}
@@ -346,9 +348,9 @@ class ByteStream {
this->total_size_.store(other.total_size_.load());
}
- FORCE_INLINE int64_t total_size() const { return total_size_.load(); }
- FORCE_INLINE int64_t read_pos() const { return read_pos_; };
- FORCE_INLINE void wrapped_buf_advance_read_pos(uint32_t size) {
+ FORCE_INLINE size_t total_size() const { return total_size_.load(); }
+ FORCE_INLINE size_t read_pos() const { return read_pos_; };
+ FORCE_INLINE void wrapped_buf_advance_read_pos(size_t size) {
if (size + read_pos_ > total_size_.load()) {
read_pos_ = total_size_.load();
} else {
@@ -358,8 +360,8 @@ class ByteStream {
/* ================ Part 2: write_xxx and read_xxx ================ */
// writer @buf with length @len into this bytestream
- int write_buf(const uint8_t *buf, const size_t len) {
- int ret = error_info::E_OK;
+ E_CODE write_buf(const uint8_t *buf, const size_t len) {
+ E_CODE ret = error_info::E_OK;
size_t write_len = 0;
while (write_len < len) {
if (RET_FAIL(prepare_space())) {
@@ -380,39 +382,34 @@ class ByteStream {
// reader @want_len bytes to @buf, @read_len indicates real len we reader.
// if ByteStream do not have so many bytes, it will return E_PARTIAL_READ
if
// no other error occure.
- int read_buf(uint8_t *buf, const uint32_t want_len, uint32_t &read_len) {
- int ret = error_info::E_OK;
+ E_CODE read_buf(uint8_t *buf, const size_t want_len, size_t &read_len) {
+ E_CODE ret = E_CODE::E_OK;
bool partial_read = (read_pos_ + want_len > total_size_.load());
- uint32_t want_len_limited =
+ const size_t want_len_limited =
partial_read ? (total_size_.load() - read_pos_) : want_len;
read_len = 0;
while (read_len < want_len_limited) {
if (RET_FAIL(check_space())) {
return ret;
}
- uint32_t remainder = page_size_ - (read_pos_ % page_size_);
- uint32_t copy_len = remainder < want_len_limited - read_len
- ? remainder
- : want_len_limited - read_len;
+ const size_t remainder = page_size_ - (read_pos_ % page_size_);
+ const size_t copy_len = remainder < want_len_limited - read_len
+ ? remainder
+ : want_len_limited - read_len;
memcpy(buf + read_len, read_page_->buf_ + (read_pos_ % page_size_),
copy_len);
read_len += copy_len;
read_pos_ += copy_len;
}
- return partial_read ? common::E_PARTIAL_READ : error_info::E_OK;
+ return partial_read ? E_CODE::E_PARTIAL_READ : E_CODE::E_OK;
}
- FORCE_INLINE int write_buf(const char *buf, const uint32_t len) {
- return write_buf((const uint8_t *)buf, len);
+ FORCE_INLINE int write_buf(const char *buf, const size_t len) {
+ return write_buf(reinterpret_cast<const uint8_t *>(buf), len);
}
- FORCE_INLINE int read_buf(char *buf, const uint32_t want_len,
- uint32_t &read_len) {
- return read_buf((uint8_t *)buf, want_len, read_len);
- }
- FORCE_INLINE int read_buf(char *buf, const int32_t want_len,
- int32_t &read_len) {
- return read_buf((uint8_t *)buf, (uint32_t &)want_len,
- (uint32_t &)read_len);
+ FORCE_INLINE E_CODE read_buf(char *buf, const size_t want_len,
+ size_t &read_len) {
+ return read_buf(reinterpret_cast<uint8_t *>(buf), want_len, read_len);
}
void purge_prev_pages(int purge_page_count = INT32_MAX) {
@@ -435,7 +432,7 @@ class ByteStream {
*/
struct Buffer {
char *buf_;
- uint32_t len_;
+ size_t len_;
Buffer() : buf_(nullptr), len_(0) {}
};
@@ -445,8 +442,8 @@ class ByteStream {
if (error_info::E_OK != prepare_space()) {
return b;
}
- b.buf_ =
- (char *)(tail_.load()->buf_ + (total_size_.load() % page_size_));
+ b.buf_ = reinterpret_cast<char *>(tail_.load()->buf_ +
+ (total_size_.load() % page_size_));
b.len_ = page_size_ - (total_size_.load() % page_size_);
return b;
}
@@ -468,7 +465,7 @@ class ByteStream {
const ByteStream &host_;
Page *cur_;
Page *end_;
- int64_t total_size_;
+ size_t total_size_;
BufferIterator(const ByteStream &bs) : host_(bs) {
cur_ = bs.head_.load();
end_ = bs.tail_.load();
@@ -503,8 +500,8 @@ class ByteStream {
struct Consumer {
const ByteStream &host_;
Page *cur_;
- uint32_t read_offset_within_cur_page_;
- int64_t total_end_offset_; // for DEBUG
+ size_t read_offset_within_cur_page_;
+ size_t total_end_offset_; // for DEBUG
Consumer(const ByteStream &bs) : host_(bs) {
ASSERT(bs.head_.enable_atomic());
@@ -527,7 +524,7 @@ class ByteStream {
// get tail position <tail_, total_size_> atomically
Page *host_end = nullptr;
- int64_t host_total_size = 0;
+ size_t host_total_size = 0;
while (true) {
host_end = host_.tail_.load();
host_total_size = host_.total_size_.load();
@@ -555,7 +552,7 @@ class ByteStream {
(host_total_size % host_.page_size_)) {
return b;
} else {
- b.buf_ = ((char *)(cur_->buf_)) +
+ b.buf_ = reinterpret_cast<char *>(cur_->buf_) +
read_offset_within_cur_page_;
b.len_ = (host_total_size % host_.page_size_) -
read_offset_within_cur_page_;
@@ -587,8 +584,8 @@ class ByteStream {
};
private:
- FORCE_INLINE int prepare_space() {
- int ret = error_info::E_OK;
+ FORCE_INLINE E_CODE prepare_space() {
+ E_CODE ret = error_info::E_OK;
if (UNLIKELY(tail_.load() == nullptr ||
total_size_.load() % page_size_ == 0)) {
Page *p = nullptr;
@@ -601,9 +598,10 @@ class ByteStream {
return ret;
}
- FORCE_INLINE int check_space() {
+ FORCE_INLINE E_CODE check_space() {
if (UNLIKELY(read_pos_ >= total_size_.load())) {
- return common::E_OUT_OF_RANGE;
+ RETURN_ERR(E_CODE::E_PARTIAL_READ,
+ "read pos is larger than totalsize")
}
if (UNLIKELY(read_page_ == nullptr)) {
read_page_ = head_.load();
@@ -611,29 +609,28 @@ class ByteStream {
read_page_ = read_page_->next_.load();
}
if (UNLIKELY(read_page_ == nullptr)) {
- return common::E_OUT_OF_RANGE;
+ RETURN_ERR(E_CODE::E_OUT_OF_RANGE, "current read page is null")
}
- return error_info::E_OK;
+ return E_CODE::E_OK;
}
- FORCE_INLINE int alloc_page(Page *&p) {
- int ret = error_info::E_OK;
- char *buf = (char *)allocator_.alloc(page_size_ + sizeof(Page), mid_);
+ FORCE_INLINE E_CODE alloc_page(Page *&p) {
+ char *buf = static_cast<char *>(
+ common::BaseAllocator::alloc(page_size_ + sizeof(Page), mid_));
if (UNLIKELY(buf == nullptr)) {
- ret = common::E_OOM;
+ RETURN_ERR(E_CODE::E_OOM, "allocate page failed")
+ }
+ p = new (buf) Page(head_.enable_atomic());
+ p->next_.store(nullptr);
+ if (head_.load()) {
+ tail_.load()->next_.store(p);
+ tail_.store(p);
} else {
- p = new (buf) Page(head_.enable_atomic());
- p->next_.store(nullptr);
- if (head_.load()) {
- tail_.load()->next_.store(p);
- tail_.store(p);
- } else {
- head_.store(p);
- tail_.store(p);
- }
+ head_.store(p);
+ tail_.store(p);
}
// printf("\nByteStream alloc_page, this=%p, new_page=%p\n", this, p);
- return ret;
+ return E_CODE::E_OK;
}
DISALLOW_COPY_AND_ASSIGN(ByteStream);
@@ -671,23 +668,23 @@ FORCE_INLINE int merge_byte_stream(ByteStream &sea,
ByteStream &river,
return ret;
}
-FORCE_INLINE int copy_bs_to_buf(ByteStream &bs, char *src_buf,
- uint32_t src_buf_len) {
+FORCE_INLINE E_CODE copy_bs_to_buf(ByteStream &bs, char *src_buf,
+ const size_t src_buf_len) {
ByteStream::BufferIterator buf_iter = bs.init_buffer_iterator();
- uint32_t copyed_len = 0;
+ size_t copied_len = 0;
while (true) {
ByteStream::Buffer buf = buf_iter.get_next_buf();
if (buf.buf_ == nullptr) {
break;
} else {
- if (src_buf_len - copyed_len < buf.len_) {
- return E_BUF_NOT_ENOUGH;
+ if (src_buf_len - copied_len < buf.len_) {
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "buffer not enough")
}
- memcpy(src_buf + copyed_len, buf.buf_, buf.len_);
- copyed_len += buf.len_;
+ memcpy(src_buf + copied_len, buf.buf_, buf.len_);
+ copied_len += buf.len_;
}
}
- return E_OK;
+ return E_CODE::E_OK;
}
FORCE_INLINE uint32_t get_var_uint_size(
@@ -743,50 +740,50 @@ FORCE_INLINE void DEBUG_hex_dump_buf(const char
*print_tag, const char *buf,
class SerializationUtil {
public:
- FORCE_INLINE static int write_ui8(uint8_t ui8, ByteStream &out) {
+ FORCE_INLINE static E_CODE write_ui8(uint8_t ui8, ByteStream &out) {
return out.write_buf(&ui8, 1);
}
- FORCE_INLINE static int write_ui16(uint16_t ui16, ByteStream &out) {
+ FORCE_INLINE static E_CODE write_ui16(uint16_t ui16, ByteStream &out) {
uint8_t buf[2];
- buf[0] = (uint8_t)((ui16 >> 8) & 0xFF);
- buf[1] = (uint8_t)((ui16) & 0xFF);
+ buf[0] = static_cast<uint8_t>((ui16 >> 8) & 0xFF);
+ buf[1] = static_cast<uint8_t>((ui16) & 0xFF);
return out.write_buf(buf, 2);
}
- FORCE_INLINE static int write_ui32(uint32_t ui32, ByteStream &out) {
+ FORCE_INLINE static E_CODE write_ui32(uint32_t ui32, ByteStream &out) {
uint8_t buf[4];
- buf[0] = (uint8_t)((ui32 >> 24) & 0xFF);
- buf[1] = (uint8_t)((ui32 >> 16) & 0xFF);
- buf[2] = (uint8_t)((ui32 >> 8) & 0xFF);
- buf[3] = (uint8_t)((ui32) & 0xFF);
+ buf[0] = static_cast<uint8_t>((ui32 >> 24) & 0xFF);
+ buf[1] = static_cast<uint8_t>((ui32 >> 16) & 0xFF);
+ buf[2] = static_cast<uint8_t>((ui32 >> 8) & 0xFF);
+ buf[3] = static_cast<uint8_t>((ui32) & 0xFF);
return out.write_buf(buf, 4);
}
- FORCE_INLINE static int write_ui64(uint64_t ui64, ByteStream &out) {
+ FORCE_INLINE static E_CODE write_ui64(uint64_t ui64, ByteStream &out) {
// big-endian: most signification byte at smaller address
// refer to tsfile.utils.BytesUtil
uint8_t buf[8];
- buf[0] = (uint8_t)((ui64 >> 56) & 0xFF);
- buf[1] = (uint8_t)((ui64 >> 48) & 0xFF);
- buf[2] = (uint8_t)((ui64 >> 40) & 0xFF);
- buf[3] = (uint8_t)((ui64 >> 32) & 0xFF);
- buf[4] = (uint8_t)((ui64 >> 24) & 0xFF);
- buf[5] = (uint8_t)((ui64 >> 16) & 0xFF);
- buf[6] = (uint8_t)((ui64 >> 8) & 0xFF);
- buf[7] = (uint8_t)((ui64) & 0xFF);
+ buf[0] = static_cast<uint8_t>((ui64 >> 56) & 0xFF);
+ buf[1] = static_cast<uint8_t>((ui64 >> 48) & 0xFF);
+ buf[2] = static_cast<uint8_t>((ui64 >> 40) & 0xFF);
+ buf[3] = static_cast<uint8_t>((ui64 >> 32) & 0xFF);
+ buf[4] = static_cast<uint8_t>((ui64 >> 24) & 0xFF);
+ buf[5] = static_cast<uint8_t>((ui64 >> 16) & 0xFF);
+ buf[6] = static_cast<uint8_t>((ui64 >> 8) & 0xFF);
+ buf[7] = static_cast<uint8_t>((ui64) & 0xFF);
return out.write_buf(buf, 8);
}
- FORCE_INLINE static int read_ui8(uint8_t &ui8, ByteStream &in) {
- int ret = error_info::E_OK;
+ FORCE_INLINE static E_CODE read_ui8(uint8_t &ui8, ByteStream &in) {
+ E_CODE ret = E_CODE::E_OK;
char buf[1];
- uint32_t read_len = 0;
+ size_t read_len = 0;
ret = in.read_buf(buf, 1, read_len);
- ui8 = (uint8_t)buf[0];
+ ui8 = static_cast<uint8_t>(buf[0]);
return ret;
}
- FORCE_INLINE static int read_ui16(uint16_t &ui16, ByteStream &in) {
- int ret = error_info::E_OK;
+ FORCE_INLINE static E_CODE read_ui16(uint16_t &ui16, ByteStream &in) {
+ E_CODE ret = E_CODE::E_OK;
uint8_t buf[2];
- uint32_t read_len = 0;
+ size_t read_len = 0;
if (RET_FAIL(in.read_buf(buf, 2, read_len))) {
return ret;
}
@@ -794,10 +791,10 @@ class SerializationUtil {
ui16 = (ui16 << 8) | buf[1];
return ret;
}
- FORCE_INLINE static int read_ui32(uint32_t &ui32, ByteStream &in) {
- int ret = error_info::E_OK;
+ FORCE_INLINE static E_CODE read_ui32(uint32_t &ui32, ByteStream &in) {
+ E_CODE ret = E_CODE::E_OK;
uint8_t buf[4];
- uint32_t read_len = 0;
+ size_t read_len = 0;
if (RET_FAIL(in.read_buf(buf, 4, read_len))) {
return ret;
}
@@ -807,10 +804,10 @@ class SerializationUtil {
ui32 = (ui32 << 8) | (buf[3] & 0xFF);
return ret;
}
- FORCE_INLINE static int read_ui64(uint64_t &ui64, ByteStream &in) {
- int ret = error_info::E_OK;
+ FORCE_INLINE static E_CODE read_ui64(uint64_t &ui64, ByteStream &in) {
+ E_CODE ret = error_info::E_OK;
uint8_t buf[8];
- uint32_t read_len = 0;
+ size_t read_len = 0;
if (RET_FAIL(in.read_buf(buf, 8, read_len))) {
return ret;
}
@@ -826,19 +823,19 @@ class SerializationUtil {
}
// caller guarantee buffer has at least 1 byte
FORCE_INLINE static uint8_t read_ui8(char *buffer) {
- return *(uint8_t *)buffer;
+ return *reinterpret_cast<uint8_t *>(buffer);
}
// caller guarantee buffer has at least 2 bytes
FORCE_INLINE static uint16_t read_ui16(char *buffer) {
- uint8_t *buf = (uint8_t *)buffer;
+ uint8_t *buf = reinterpret_cast<uint8_t *>(buffer);
uint16_t ui16 = buf[0];
ui16 = (ui16 << 8) | (buf[1] & 0xFF);
return ui16;
}
// caller guarantee buffer has at least 4 bytes
FORCE_INLINE static uint32_t read_ui32(char *buffer) {
- uint8_t *buf = (uint8_t *)buffer;
+ uint8_t *buf = reinterpret_cast<uint8_t *>(buffer);
uint32_t ui32 = buf[0];
ui32 = (ui32 << 8) | (buf[1] & 0xFF);
ui32 = (ui32 << 8) | (buf[2] & 0xFF);
@@ -847,7 +844,7 @@ class SerializationUtil {
}
// caller guarantee buffer has at least 8 bytes
FORCE_INLINE static uint64_t read_ui64(char *buffer) {
- uint8_t *buf = (uint8_t *)buffer;
+ uint8_t *buf = reinterpret_cast<uint8_t *>(buffer);
uint64_t ui64 = buf[0];
ui64 = (ui64 << 8) | (buf[1] & 0xFF);
ui64 = (ui64 << 8) | (buf[2] & 0xFF);
@@ -859,78 +856,78 @@ class SerializationUtil {
return ui64;
}
- FORCE_INLINE static int write_float(float f, ByteStream &out) {
+ FORCE_INLINE static E_CODE write_float(float f, ByteStream &out) {
uint8_t bytes[4];
float_to_bytes(f, bytes);
return out.write_buf(bytes, 4);
}
- FORCE_INLINE static int read_float(float &f, ByteStream &in) {
- int ret = error_info::E_OK;
+ FORCE_INLINE static E_CODE read_float(float &f, ByteStream &in) {
+ E_CODE ret = error_info::E_OK;
uint8_t bytes[4];
- uint32_t read_len = 0;
+ size_t read_len = 0;
if (RET_FAIL(in.read_buf(bytes, 4, read_len))) {
} else if (read_len != 4) {
- ret = common::E_BUF_NOT_ENOUGH;
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "")
} else {
f = bytes_to_float(bytes);
}
return ret;
}
FORCE_INLINE static float read_float(char *buffer) {
- uint8_t *buf = (uint8_t *)buffer;
+ auto *buf = reinterpret_cast<uint8_t *>(buffer);
return bytes_to_float(buf);
}
- FORCE_INLINE static int write_double(double d, ByteStream &out) {
+ FORCE_INLINE static E_CODE write_double(double d, ByteStream &out) {
uint8_t bytes[8];
double_to_bytes(d, bytes);
return out.write_buf(bytes, 8);
}
- FORCE_INLINE static int read_double(double &d, ByteStream &in) {
- int ret = error_info::E_OK;
- uint32_t read_len = 0;
+ FORCE_INLINE static E_CODE read_double(double &d, ByteStream &in) {
+ E_CODE ret = E_CODE::E_OK;
+ size_t read_len = 0;
uint8_t bytes[8];
if (RET_FAIL(in.read_buf(bytes, 8, read_len))) {
} else if (read_len != 8) {
- ret = common::E_BUF_NOT_ENOUGH;
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "")
} else {
d = bytes_to_double(bytes);
}
return ret;
}
FORCE_INLINE static double read_double(char *buffer) {
- uint8_t *buf = (uint8_t *)buffer;
+ uint8_t *buf = reinterpret_cast<uint8_t *>(buffer);
return bytes_to_double(buf);
}
- FORCE_INLINE static int write_i8(int8_t i8, ByteStream &out) {
- return write_ui8((uint8_t)i8, out);
+ FORCE_INLINE static E_CODE write_i8(int8_t i8, ByteStream &out) {
+ return write_ui8(static_cast<uint8_t>(i8), out);
}
- FORCE_INLINE static int write_i16(int16_t i16, ByteStream &out) {
- return write_ui16((uint16_t)i16, out);
+ FORCE_INLINE static E_CODE write_i16(int16_t i16, ByteStream &out) {
+ return write_ui16(static_cast<uint16_t>(i16), out);
}
- FORCE_INLINE static int write_i32(int32_t i32, ByteStream &out) {
- return write_ui32((uint32_t)i32, out);
+ FORCE_INLINE static E_CODE write_i32(int32_t i32, ByteStream &out) {
+ return write_ui32(static_cast<uint32_t>(i32), out);
}
- FORCE_INLINE static int write_i64(int64_t i64, ByteStream &out) {
- return write_ui64((uint64_t)i64, out);
+ FORCE_INLINE static E_CODE write_i64(int64_t i64, ByteStream &out) {
+ return write_ui64(static_cast<uint64_t>(i64), out);
}
- FORCE_INLINE static int read_i8(int8_t &i8, ByteStream &in) {
- return read_ui8((uint8_t &)i8, in);
+ FORCE_INLINE static E_CODE read_i8(int8_t &i8, ByteStream &in) {
+ return read_ui8(reinterpret_cast<uint8_t &>(i8), in);
}
- FORCE_INLINE static int read_i16(int16_t &i16, ByteStream &in) {
- return read_ui16((uint16_t &)i16, in);
+ FORCE_INLINE static E_CODE read_i16(int16_t &i16, ByteStream &in) {
+ return read_ui16(reinterpret_cast<uint16_t &>(i16), in);
}
- FORCE_INLINE static int read_i32(int32_t &i32, ByteStream &in) {
- return read_ui32((uint32_t &)i32, in);
+ FORCE_INLINE static E_CODE read_i32(int32_t &i32, ByteStream &in) {
+ return read_ui32(reinterpret_cast<uint32_t &>(i32), in);
}
- FORCE_INLINE static int read_i64(int64_t &i64, ByteStream &in) {
- return read_ui64((uint64_t &)i64, in);
+ FORCE_INLINE static E_CODE read_i64(int64_t &i64, ByteStream &in) {
+ return read_ui64(reinterpret_cast<uint64_t &>(i64), in);
}
// TODO more test on var_xxx
- FORCE_INLINE static int do_write_var_uint(uint32_t ui32, ByteStream &out) {
- int ret = error_info::E_OK;
+ FORCE_INLINE static E_CODE do_write_var_uint(uint32_t ui32, ByteStream
&out) {
+ E_CODE ret = E_CODE::E_OK;
while ((ui32 & 0xFFFFFF80) != 0) {
if (RET_FAIL(write_ui8((ui32 & 0x7F) | 0x80, out))) {
return ret;
@@ -939,26 +936,26 @@ class SerializationUtil {
}
return write_ui8(ui32 & 0x7F, out);
}
- FORCE_INLINE static int do_write_var_uint(uint32_t ui32, char *out_buf,
+ FORCE_INLINE static E_CODE do_write_var_uint(uint32_t ui32, char *out_buf,
const uint32_t out_buf_len) {
uint32_t offset = 0;
while ((ui32 & 0xFFFFFF80) != 0) {
if (offset >= out_buf_len) {
- return common::E_BUF_NOT_ENOUGH;
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "")
}
*(out_buf + offset) = (ui32 & 0x7F) | 0x80;
ui32 = ui32 >> 7;
offset++;
}
if (offset >= out_buf_len) {
- return common::E_BUF_NOT_ENOUGH;
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "")
}
- *(out_buf + offset) = (ui32 & 0x7F);
+ *(out_buf + offset) = (0x7F & ui32);
return error_info::E_OK;
}
- FORCE_INLINE static int do_read_var_uint(uint32_t &ui32, ByteStream &in) {
+ FORCE_INLINE static E_CODE do_read_var_uint(uint32_t &ui32, ByteStream
&in) {
// Follow readUnsignedVarInt in ReadWriteForEncodingUtils.java
- int ret = error_info::E_OK;
+ E_CODE ret = E_CODE::E_OK;
ui32 = 0;
int i = 0;
uint8_t ui8 = 0;
@@ -975,26 +972,26 @@ class SerializationUtil {
ui32 = ui32 | (ui8 << i);
return ret;
}
- FORCE_INLINE static int do_read_var_uint(uint32_t &ui32, char *in_buf,
- int in_buf_len, int *ret_offset) {
+ FORCE_INLINE static E_CODE do_read_var_uint(uint32_t &ui32, char *in_buf,
+ size_t in_buf_len, size_t
*ret_offset) {
ui32 = 0;
int i = 0;
uint8_t ui8 = 0;
int offset = 0;
if (offset < in_buf_len) {
- ui8 = *(uint8_t *)(in_buf + offset);
+ ui8 = *reinterpret_cast<uint8_t *>(in_buf + offset);
offset++;
} else {
- return common::E_BUF_NOT_ENOUGH;
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "")
}
while (ui8 != 0xF && (ui8 & 0x80) != 0) {
ui32 = ui32 | ((ui8 & 0x7F) << i);
i = i + 7;
if (offset < in_buf_len) {
- ui8 = *(uint8_t *)(in_buf + offset);
+ ui8 = *reinterpret_cast<uint8_t *>(in_buf + offset);
offset++;
} else {
- return common::E_BUF_NOT_ENOUGH;
+ RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "")
}
}
ui32 = ui32 | (ui8 << i);
@@ -1035,8 +1032,8 @@ class SerializationUtil {
return do_read_var_uint(ui32, in);
}
FORCE_INLINE static int read_var_uint(uint32_t &ui32, char *in_buf,
- int in_buf_len,
- int *ret_offset = nullptr) {
+ size_t in_buf_len,
+ size_t *ret_offset = nullptr) {
return do_read_var_uint(ui32, in_buf, in_buf_len, ret_offset);
}
diff --git a/cpp/src/common/allocator/mem_alloc.cc
b/cpp/src/common/allocator/mem_alloc.cc
index e84f5867..705a4fb5 100644
--- a/cpp/src/common/allocator/mem_alloc.cc
+++ b/cpp/src/common/allocator/mem_alloc.cc
@@ -67,7 +67,7 @@ const char *g_mod_names[__LAST_MOD_ID] = {
constexpr uint32_t HEADER_SIZE_4B = 4;
constexpr uint32_t HEADER_SIZE_8B = 8;
-void *mem_alloc(const uint32_t size, const AllocModID mid) {
+void *mem_alloc(const size_t size, const AllocModID mid) {
// use 7bit at most
ASSERT(mid <= 127);
@@ -77,8 +77,8 @@ void *mem_alloc(const uint32_t size, const AllocModID mid) {
if (UNLIKELY(p == nullptr)) {
return nullptr;
} else {
- uint32_t header = (size << 8) | ((uint32_t)mid);
- *((uint32_t *)p) = header;
+ uint32_t header = (size << 8) | static_cast<uint32_t>(mid);
+ *reinterpret_cast<uint32_t *>(p) = header;
return p + HEADER_SIZE_4B;
}
} else {
@@ -93,7 +93,7 @@ void *mem_alloc(const uint32_t size, const AllocModID mid) {
} else {
const uint64_t large_size = size;
const uint64_t header =
- ((large_size) << 8) | (((uint32_t)mid) | (0x80));
+ ((large_size) << 8) | (static_cast<uint32_t>(mid) | (0x80));
const auto low4b = static_cast<uint32_t>(header & 0xFFFFFFFF);
const auto high4b = static_cast<uint32_t>(header >> 32);
*reinterpret_cast<uint32_t *>(p) = high4b;
@@ -106,7 +106,7 @@ void *mem_alloc(const uint32_t size, const AllocModID mid) {
#ifndef _WIN32
void printCallers() {
int layers = 0, i = 0;
- char **symbols = NULL;
+ char **symbols = nullptr;
const int64_t MAX_FRAMES = 32;
@@ -132,21 +132,21 @@ void printCallers() {
void mem_free(void *ptr) {
// try as 4Byte header
- char *p = (char *)ptr;
- uint32_t header = *(uint32_t *)(p - HEADER_SIZE_4B);
+ char *p = static_cast<char *>(ptr);
+ uint32_t header = *reinterpret_cast<uint32_t *>(p - HEADER_SIZE_4B);
if ((header & 0x80) == 0) {
// 4Byte header
- uint32_t size = header >> 8;
- AllocModID mid = (AllocModID)(header & 0x7F);
- ModStat::get_instance().update_free(mid, size);
+ // uint32_t size = header >> 8;
+ // AllocModID mid = (AllocModID)(header & 0x7F);
+ // // ModStat::get_instance().update_free(mid, size);
::free(p - HEADER_SIZE_4B);
} else {
// 8Byte header
- uint64_t header8b = ((uint64_t)(*(uint32_t *)(p - 4))) |
- ((uint64_t)(*(uint32_t *)(p - 8)) << 32);
- AllocModID mid = (AllocModID)(header8b & 0x7F);
- uint32_t size = (uint32_t)(header8b >> 8);
- ModStat::get_instance().update_free(mid, size);
+ uint64_t header8b = static_cast<uint64_t>(*reinterpret_cast<uint32_t
*>(p - 4)) |
+ (static_cast<uint64_t>(*reinterpret_cast<uint32_t
*>(p - 8)) << 32);
+ // AllocModID mid = (AllocModID)(header8b & 0x7F);
+ // uint32_t size = (uint32_t)(header8b >> 8);
+ // ModStat::get_instance().update_free(mid, size);
::free(p - HEADER_SIZE_8B);
}
}
diff --git a/cpp/src/common/error_info/error_define.inc
b/cpp/src/common/error_info/error_define.inc
index 68cb4fa2..2326a960 100644
--- a/cpp/src/common/error_info/error_define.inc
+++ b/cpp/src/common/error_info/error_define.inc
@@ -18,4 +18,9 @@
*/
ERRNO(E_OK, 0, "success")
-ERRNO(E_OOM, 1, "out of memory")
\ No newline at end of file
+
+// MEMORY AND BUF READ
+ERRNO(E_OOM, 1, "out of memory")
+ERRNO(E_PARTIAL_READ, 2, "partial read")
+ERRNO(E_OUT_OF_RANGE, 3, "out of range")
+ERRNO(E_BUF_NOT_ENOUGH, 4, "buf not enough")
\ No newline at end of file
diff --git a/cpp/src/common/error_info/error_info.cc
b/cpp/src/common/error_info/error_info.cc
index a2cf59f3..ef70defc 100644
--- a/cpp/src/common/error_info/error_info.cc
+++ b/cpp/src/common/error_info/error_info.cc
@@ -1,21 +1,21 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements. See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership. The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* License); you may not use this file except in compliance
-* with the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing,
-* software distributed under the License is distributed on an
-* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-* KIND, either express or implied. See the License for the
-* specific language governing permissions and limitations
-* under the License.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
#include "error_info.h"
@@ -26,10 +26,14 @@ namespace error_info {
static std::string tsfile_err_msg;
static E_CODE tsfile_err_code;
+static std::string tsfile_err_filename;
+static int tsfile_err_line;
+static std::string tsfile_err_function;
-static const std::unordered_map<E_CODE, const char*> err_name_map = {
-#define ERROR(name, val, desc) {val, desc}
+static const std::unordered_map<int, const char*> err_name_map = {
+#define ERRNO(name, val, desc) {val, desc},
#include "error_define.inc"
+
#undef ERRNO
};
@@ -38,23 +42,23 @@ std::string error_name(E_CODE code) {
return it == err_name_map.end() ? "unknown error" : it->second;
}
+void set_err_no(E_CODE error) { tsfile_err_code = error; }
-void set_err_no(E_CODE error) { tsfile_err_code = error;
-}
+void set_err_msg(const std::string& msg) { tsfile_err_msg = msg; }
-void set_err_msg(const std::string& msg) {
- tsfile_err_msg = msg;
-}
-
-void set_err_info(E_CODE error, const std::string& msg) {
+void set_err_info(E_CODE error, const std::string& msg, const std::string&
file,
+ int line, const std::string& function) {
tsfile_err_code = error;
tsfile_err_msg = msg;
+ tsfile_err_filename = file;
+ tsfile_err_line = line;
+ tsfile_err_function = function;
}
void print_err_info() {
- std::cout << " error no is " << tsfile_err_code << "(" <<
error_name(tsfile_err_code) << ")";
+ std::cout << " error no is " << tsfile_err_code << "("
+ << error_name(tsfile_err_code) << ")";
std::cout << " error message : " << tsfile_err_msg;
}
-}
-
+} // namespace error_info
diff --git a/cpp/src/common/error_info/error_info.h
b/cpp/src/common/error_info/error_info.h
index e1a20cc9..2bf4cd4e 100644
--- a/cpp/src/common/error_info/error_info.h
+++ b/cpp/src/common/error_info/error_info.h
@@ -35,13 +35,15 @@ std::string error_name(E_CODE code);
void print_err_info();
void set_err_no(int error);
void set_err_msg(const std::string& msg);
-void set_err_info(int error, const std::string& msg);
-
-#define RETURN_ERR(code, msg) \
- do { \
- ::error_info::set_err_info((code), msg); \
- return (code); \
- } while (0)
+void set_err_info(int error, const std::string& msg, const std::string& file,
+ int line, const std::string& function);
+
+#define RETURN_ERR(code, msg) \
+ do { \
+ ::error_info::set_err_info((code), msg, __FILE__, __LINE__, \
+ __FUNCTION__); \
+ return (code); \
+ } while (0);
} // namespace error_info
#endif
\ No newline at end of file