Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
3311fec1
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3311fec1
编写于
12月 28, 2021
作者:
H
Hongze Cheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refact
上级
7c564581
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
243 addition
and
261 deletion
+243
-261
include/util/tbuffer.h
include/util/tbuffer.h
+69
-69
source/util/src/tbuffer.c
source/util/src/tbuffer.c
+174
-192
未找到文件。
include/util/tbuffer.h
浏览文件 @
3311fec1
...
...
@@ -82,91 +82,91 @@ typedef struct SBufferWriter {
char
*
data
;
size_t
pos
;
size_t
size
;
void
*
(
*
allocator
)(
void
*
,
size_t
);
void
*
(
*
allocator
)(
void
*
,
size_t
);
}
SBufferWriter
;
////////////////////////////////////////////////////////////////////////////////
// common functions & macros for both reader & writer
#define tbufTell( buf ) ((buf)->pos)
#define tbufTell(buf) ((buf)->pos)
////////////////////////////////////////////////////////////////////////////////
// reader functions & macros
// *Endian*, if true, reader functions of primitive types will do 'ntoh' automatically
#define tbufInitReader(
Data, Size, Endian ) {.endian = (Endian), .data = (Data), .pos = 0, .size = ((Data) == NULL ? 0 :(Size))}
size_t
tbufSkip
(
SBufferReader
*
buf
,
size_t
size
);
const
char
*
tbufRead
(
SBufferReader
*
buf
,
size_t
size
);
void
tbufReadToBuffer
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
);
const
char
*
tbufReadString
(
SBufferReader
*
buf
,
size_t
*
len
);
size_t
tbufReadToString
(
SBufferReader
*
buf
,
char
*
dst
,
size_t
size
);
const
char
*
tbufReadBinary
(
SBufferReader
*
buf
,
size_t
*
len
);
size_t
tbufReadToBinary
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
);
bool
tbufReadBool
(
SBufferReader
*
buf
);
char
tbufReadChar
(
SBufferReader
*
buf
);
int8_t
tbufReadInt8
(
SBufferReader
*
buf
);
uint8_t
tbufReadUint8
(
SBufferReader
*
buf
);
int16_t
tbufReadInt16
(
SBufferReader
*
buf
);
uint16_t
tbufReadUint16
(
SBufferReader
*
buf
);
int32_t
tbufReadInt32
(
SBufferReader
*
buf
);
uint32_t
tbufReadUint32
(
SBufferReader
*
buf
);
int64_t
tbufReadInt64
(
SBufferReader
*
buf
);
uint64_t
tbufReadUint64
(
SBufferReader
*
buf
);
float
tbufReadFloat
(
SBufferReader
*
buf
);
double
tbufReadDouble
(
SBufferReader
*
buf
);
#define tbufInitReader(
Data, Size, Endian) \
{ .endian = (Endian), .data = (Data), .pos = 0, .size = ((Data) == NULL ? 0 : (Size)) }
size_t
tbufSkip
(
SBufferReader
*
buf
,
size_t
size
);
const
char
*
tbufRead
(
SBufferReader
*
buf
,
size_t
size
);
void
tbufReadToBuffer
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
);
const
char
*
tbufReadString
(
SBufferReader
*
buf
,
size_t
*
len
);
size_t
tbufReadToString
(
SBufferReader
*
buf
,
char
*
dst
,
size_t
size
);
const
char
*
tbufReadBinary
(
SBufferReader
*
buf
,
size_t
*
len
);
size_t
tbufReadToBinary
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
);
bool
tbufReadBool
(
SBufferReader
*
buf
);
char
tbufReadChar
(
SBufferReader
*
buf
);
int8_t
tbufReadInt8
(
SBufferReader
*
buf
);
uint8_t
tbufReadUint8
(
SBufferReader
*
buf
);
int16_t
tbufReadInt16
(
SBufferReader
*
buf
);
uint16_t
tbufReadUint16
(
SBufferReader
*
buf
);
int32_t
tbufReadInt32
(
SBufferReader
*
buf
);
uint32_t
tbufReadUint32
(
SBufferReader
*
buf
);
int64_t
tbufReadInt64
(
SBufferReader
*
buf
);
uint64_t
tbufReadUint64
(
SBufferReader
*
buf
);
float
tbufReadFloat
(
SBufferReader
*
buf
);
double
tbufReadDouble
(
SBufferReader
*
buf
);
////////////////////////////////////////////////////////////////////////////////
// writer functions & macros
// *Allocator*, function to allocate memory, will use 'realloc' if NULL
// *Endian*, if true, writer functions of primitive types will do 'hton' automatically
#define tbufInitWriter( Allocator, Endian ) {.endian = (Endian), .data = NULL, .pos = 0, .size = 0, .allocator = ((Allocator) == NULL ? realloc : (Allocator))}
void
tbufCloseWriter
(
SBufferWriter
*
buf
);
void
tbufEnsureCapacity
(
SBufferWriter
*
buf
,
size_t
size
);
size_t
tbufReserve
(
SBufferWriter
*
buf
,
size_t
size
);
char
*
tbufGetData
(
SBufferWriter
*
buf
,
bool
takeOver
);
void
tbufWrite
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
size
);
void
tbufWriteAt
(
SBufferWriter
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
);
void
tbufWriteStringLen
(
SBufferWriter
*
buf
,
const
char
*
str
,
size_t
len
);
void
tbufWriteString
(
SBufferWriter
*
buf
,
const
char
*
str
);
#define tbufInitWriter(Allocator, Endian) \
{ .endian = (Endian), .data = NULL, .pos = 0, .size = 0, .allocator = ((Allocator) == NULL ? realloc : (Allocator)) }
void
tbufCloseWriter
(
SBufferWriter
*
buf
);
void
tbufEnsureCapacity
(
SBufferWriter
*
buf
,
size_t
size
);
size_t
tbufReserve
(
SBufferWriter
*
buf
,
size_t
size
);
char
*
tbufGetData
(
SBufferWriter
*
buf
,
bool
takeOver
);
void
tbufWrite
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
size
);
void
tbufWriteAt
(
SBufferWriter
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
);
void
tbufWriteStringLen
(
SBufferWriter
*
buf
,
const
char
*
str
,
size_t
len
);
void
tbufWriteString
(
SBufferWriter
*
buf
,
const
char
*
str
);
// the prototype of tbufWriteBinary and tbufWrite are identical
// the difference is: tbufWriteBinary writes the length of the data to the buffer
// first, then the actual data, which means the reader don't need to know data
// size before read. Write only write the data itself, which means the reader
// need to know data size before read.
void
tbufWriteBinary
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
len
);
void
tbufWriteBool
(
SBufferWriter
*
buf
,
bool
data
);
void
tbufWriteBoolAt
(
SBufferWriter
*
buf
,
size_t
pos
,
bool
data
);
void
tbufWriteChar
(
SBufferWriter
*
buf
,
char
data
);
void
tbufWriteCharAt
(
SBufferWriter
*
buf
,
size_t
pos
,
char
data
);
void
tbufWriteInt8
(
SBufferWriter
*
buf
,
int8_t
data
);
void
tbufWriteInt8At
(
SBufferWriter
*
buf
,
size_t
pos
,
int8_t
data
);
void
tbufWriteUint8
(
SBufferWriter
*
buf
,
uint8_t
data
);
void
tbufWriteUint8At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint8_t
data
);
void
tbufWriteInt16
(
SBufferWriter
*
buf
,
int16_t
data
);
void
tbufWriteInt16At
(
SBufferWriter
*
buf
,
size_t
pos
,
int16_t
data
);
void
tbufWriteUint16
(
SBufferWriter
*
buf
,
uint16_t
data
);
void
tbufWriteUint16At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint16_t
data
);
void
tbufWriteInt32
(
SBufferWriter
*
buf
,
int32_t
data
);
void
tbufWriteInt32At
(
SBufferWriter
*
buf
,
size_t
pos
,
int32_t
data
);
void
tbufWriteUint32
(
SBufferWriter
*
buf
,
uint32_t
data
);
void
tbufWriteUint32At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint32_t
data
);
void
tbufWriteInt64
(
SBufferWriter
*
buf
,
int64_t
data
);
void
tbufWriteInt64At
(
SBufferWriter
*
buf
,
size_t
pos
,
int64_t
data
);
void
tbufWriteUint64
(
SBufferWriter
*
buf
,
uint64_t
data
);
void
tbufWriteUint64At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint64_t
data
);
void
tbufWriteFloat
(
SBufferWriter
*
buf
,
float
data
);
void
tbufWriteFloatAt
(
SBufferWriter
*
buf
,
size_t
pos
,
float
data
);
void
tbufWriteDouble
(
SBufferWriter
*
buf
,
double
data
);
void
tbufWriteDoubleAt
(
SBufferWriter
*
buf
,
size_t
pos
,
double
data
);
void
tbufWriteBinary
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
len
);
void
tbufWriteBool
(
SBufferWriter
*
buf
,
bool
data
);
void
tbufWriteBoolAt
(
SBufferWriter
*
buf
,
size_t
pos
,
bool
data
);
void
tbufWriteChar
(
SBufferWriter
*
buf
,
char
data
);
void
tbufWriteCharAt
(
SBufferWriter
*
buf
,
size_t
pos
,
char
data
);
void
tbufWriteInt8
(
SBufferWriter
*
buf
,
int8_t
data
);
void
tbufWriteInt8At
(
SBufferWriter
*
buf
,
size_t
pos
,
int8_t
data
);
void
tbufWriteUint8
(
SBufferWriter
*
buf
,
uint8_t
data
);
void
tbufWriteUint8At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint8_t
data
);
void
tbufWriteInt16
(
SBufferWriter
*
buf
,
int16_t
data
);
void
tbufWriteInt16At
(
SBufferWriter
*
buf
,
size_t
pos
,
int16_t
data
);
void
tbufWriteUint16
(
SBufferWriter
*
buf
,
uint16_t
data
);
void
tbufWriteUint16At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint16_t
data
);
void
tbufWriteInt32
(
SBufferWriter
*
buf
,
int32_t
data
);
void
tbufWriteInt32At
(
SBufferWriter
*
buf
,
size_t
pos
,
int32_t
data
);
void
tbufWriteUint32
(
SBufferWriter
*
buf
,
uint32_t
data
);
void
tbufWriteUint32At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint32_t
data
);
void
tbufWriteInt64
(
SBufferWriter
*
buf
,
int64_t
data
);
void
tbufWriteInt64At
(
SBufferWriter
*
buf
,
size_t
pos
,
int64_t
data
);
void
tbufWriteUint64
(
SBufferWriter
*
buf
,
uint64_t
data
);
void
tbufWriteUint64At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint64_t
data
);
void
tbufWriteFloat
(
SBufferWriter
*
buf
,
float
data
);
void
tbufWriteFloatAt
(
SBufferWriter
*
buf
,
size_t
pos
,
float
data
);
void
tbufWriteDouble
(
SBufferWriter
*
buf
,
double
data
);
void
tbufWriteDoubleAt
(
SBufferWriter
*
buf
,
size_t
pos
,
double
data
);
#ifdef __cplusplus
}
...
...
source/util/src/tbuffer.c
浏览文件 @
3311fec1
...
...
@@ -13,9 +13,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "os.h"
#include "tbuffer.h"
#include "exception.h"
#include "os.h"
//#include "taoserror.h"
typedef
union
Un4B
{
...
...
@@ -40,172 +40,172 @@ static_assert(sizeof(Un8B) == sizeof(double), "sizeof(Un8B) must equal to sizeof
// reader functions
size_t
tbufSkip
(
SBufferReader
*
buf
,
size_t
size
)
{
if
(
(
buf
->
pos
+
size
)
>
buf
->
size
)
{
THROW
(
-
1
);
if
((
buf
->
pos
+
size
)
>
buf
->
size
)
{
THROW
(
-
1
);
}
size_t
old
=
buf
->
pos
;
buf
->
pos
+=
size
;
return
old
;
}
const
char
*
tbufRead
(
SBufferReader
*
buf
,
size_t
size
)
{
const
char
*
tbufRead
(
SBufferReader
*
buf
,
size_t
size
)
{
const
char
*
ret
=
buf
->
data
+
buf
->
pos
;
tbufSkip
(
buf
,
size
);
tbufSkip
(
buf
,
size
);
return
ret
;
}
void
tbufReadToBuffer
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
void
tbufReadToBuffer
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
// always using memcpy, leave optimization to compiler
memcpy
(
dst
,
tbufRead
(
buf
,
size
),
size
);
memcpy
(
dst
,
tbufRead
(
buf
,
size
),
size
);
}
static
size_t
tbufReadLength
(
SBufferReader
*
buf
)
{
static
size_t
tbufReadLength
(
SBufferReader
*
buf
)
{
// maximum length is 65535, if larger length is required
// this function and the corresponding write function need to be
// revised.
uint16_t
l
=
tbufReadUint16
(
buf
);
uint16_t
l
=
tbufReadUint16
(
buf
);
return
l
;
}
const
char
*
tbufReadString
(
SBufferReader
*
buf
,
size_t
*
len
)
{
size_t
l
=
tbufReadLength
(
buf
);
const
char
*
tbufReadString
(
SBufferReader
*
buf
,
size_t
*
len
)
{
size_t
l
=
tbufReadLength
(
buf
);
const
char
*
ret
=
buf
->
data
+
buf
->
pos
;
tbufSkip
(
buf
,
l
+
1
);
if
(
ret
[
l
]
!=
0
)
{
THROW
(
-
1
);
tbufSkip
(
buf
,
l
+
1
);
if
(
ret
[
l
]
!=
0
)
{
THROW
(
-
1
);
}
if
(
len
!=
NULL
)
{
if
(
len
!=
NULL
)
{
*
len
=
l
;
}
return
ret
;
}
size_t
tbufReadToString
(
SBufferReader
*
buf
,
char
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
size_t
tbufReadToString
(
SBufferReader
*
buf
,
char
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
size_t
len
;
const
char
*
str
=
tbufReadString
(
buf
,
&
len
);
const
char
*
str
=
tbufReadString
(
buf
,
&
len
);
if
(
len
>=
size
)
{
len
=
size
-
1
;
}
memcpy
(
dst
,
str
,
len
);
memcpy
(
dst
,
str
,
len
);
dst
[
len
]
=
0
;
return
len
;
}
const
char
*
tbufReadBinary
(
SBufferReader
*
buf
,
size_t
*
len
)
{
size_t
l
=
tbufReadLength
(
buf
);
const
char
*
tbufReadBinary
(
SBufferReader
*
buf
,
size_t
*
len
)
{
size_t
l
=
tbufReadLength
(
buf
);
const
char
*
ret
=
buf
->
data
+
buf
->
pos
;
tbufSkip
(
buf
,
l
);
if
(
len
!=
NULL
)
{
tbufSkip
(
buf
,
l
);
if
(
len
!=
NULL
)
{
*
len
=
l
;
}
return
ret
;
}
size_t
tbufReadToBinary
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
size_t
tbufReadToBinary
(
SBufferReader
*
buf
,
void
*
dst
,
size_t
size
)
{
assert
(
dst
!=
NULL
);
size_t
len
;
const
char
*
data
=
tbufReadBinary
(
buf
,
&
len
);
if
(
len
>=
size
)
{
const
char
*
data
=
tbufReadBinary
(
buf
,
&
len
);
if
(
len
>=
size
)
{
len
=
size
;
}
memcpy
(
dst
,
data
,
len
);
memcpy
(
dst
,
data
,
len
);
return
len
;
}
bool
tbufReadBool
(
SBufferReader
*
buf
)
{
bool
tbufReadBool
(
SBufferReader
*
buf
)
{
bool
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
return
ret
;
}
char
tbufReadChar
(
SBufferReader
*
buf
)
{
char
tbufReadChar
(
SBufferReader
*
buf
)
{
char
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
return
ret
;
}
int8_t
tbufReadInt8
(
SBufferReader
*
buf
)
{
int8_t
tbufReadInt8
(
SBufferReader
*
buf
)
{
int8_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
return
ret
;
}
uint8_t
tbufReadUint8
(
SBufferReader
*
buf
)
{
uint8_t
tbufReadUint8
(
SBufferReader
*
buf
)
{
uint8_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
return
ret
;
}
int16_t
tbufReadInt16
(
SBufferReader
*
buf
)
{
int16_t
tbufReadInt16
(
SBufferReader
*
buf
)
{
int16_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
(
int16_t
)
ntohs
(
ret
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
(
int16_t
)
ntohs
(
ret
);
}
return
ret
;
}
uint16_t
tbufReadUint16
(
SBufferReader
*
buf
)
{
uint16_t
tbufReadUint16
(
SBufferReader
*
buf
)
{
uint16_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
ntohs
(
ret
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
ntohs
(
ret
);
}
return
ret
;
}
int32_t
tbufReadInt32
(
SBufferReader
*
buf
)
{
int32_t
tbufReadInt32
(
SBufferReader
*
buf
)
{
int32_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
(
int32_t
)
ntohl
(
ret
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
(
int32_t
)
ntohl
(
ret
);
}
return
ret
;
}
uint32_t
tbufReadUint32
(
SBufferReader
*
buf
)
{
uint32_t
tbufReadUint32
(
SBufferReader
*
buf
)
{
uint32_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
ntohl
(
ret
);
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
ntohl
(
ret
);
}
return
ret
;
}
int64_t
tbufReadInt64
(
SBufferReader
*
buf
)
{
int64_t
tbufReadInt64
(
SBufferReader
*
buf
)
{
int64_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
(
int64_t
)
htobe64
(
ret
);
// TODO: ntohll
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
(
int64_t
)
htobe64
(
ret
);
// TODO: ntohll
}
return
ret
;
}
uint64_t
tbufReadUint64
(
SBufferReader
*
buf
)
{
uint64_t
tbufReadUint64
(
SBufferReader
*
buf
)
{
uint64_t
ret
;
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
htobe64
(
ret
);
// TODO: ntohll
tbufReadToBuffer
(
buf
,
&
ret
,
sizeof
(
ret
)
);
if
(
buf
->
endian
)
{
return
htobe64
(
ret
);
// TODO: ntohll
}
return
ret
;
}
float
tbufReadFloat
(
SBufferReader
*
buf
)
{
float
tbufReadFloat
(
SBufferReader
*
buf
)
{
Un4B
_un
;
tbufReadToBuffer
(
buf
,
&
_un
,
sizeof
(
_un
)
);
if
(
buf
->
endian
)
{
_un
.
ui
=
ntohl
(
_un
.
ui
);
tbufReadToBuffer
(
buf
,
&
_un
,
sizeof
(
_un
)
);
if
(
buf
->
endian
)
{
_un
.
ui
=
ntohl
(
_un
.
ui
);
}
return
_un
.
f
;
}
double
tbufReadDouble
(
SBufferReader
*
buf
)
{
Un8B
_un
;
tbufReadToBuffer
(
buf
,
&
_un
,
sizeof
(
_un
)
);
if
(
buf
->
endian
)
{
_un
.
ull
=
htobe64
(
_un
.
ull
);
tbufReadToBuffer
(
buf
,
&
_un
,
sizeof
(
_un
)
);
if
(
buf
->
endian
)
{
_un
.
ull
=
htobe64
(
_un
.
ull
);
}
return
_un
.
d
;
}
...
...
@@ -213,38 +213,38 @@ double tbufReadDouble(SBufferReader* buf) {
////////////////////////////////////////////////////////////////////////////////
// writer functions
void
tbufCloseWriter
(
SBufferWriter
*
buf
)
{
void
tbufCloseWriter
(
SBufferWriter
*
buf
)
{
tfree
(
buf
->
data
);
// (*buf->allocator)( buf->data, 0 ); // potential memory leak.
// (*buf->allocator)( buf->data, 0 ); // potential memory leak.
buf
->
data
=
NULL
;
buf
->
pos
=
0
;
buf
->
size
=
0
;
}
void
tbufEnsureCapacity
(
SBufferWriter
*
buf
,
size_t
size
)
{
void
tbufEnsureCapacity
(
SBufferWriter
*
buf
,
size_t
size
)
{
size
+=
buf
->
pos
;
if
(
size
>
buf
->
size
)
{
if
(
size
>
buf
->
size
)
{
size_t
nsize
=
size
+
buf
->
size
;
char
*
data
=
(
*
buf
->
allocator
)(
buf
->
data
,
nsize
);
char
*
data
=
(
*
buf
->
allocator
)(
buf
->
data
,
nsize
);
// TODO: the exception should be thrown by the allocator function
if
(
data
==
NULL
)
{
THROW
(
-
1
);
if
(
data
==
NULL
)
{
THROW
(
-
1
);
}
buf
->
data
=
data
;
buf
->
size
=
nsize
;
}
}
size_t
tbufReserve
(
SBufferWriter
*
buf
,
size_t
size
)
{
tbufEnsureCapacity
(
buf
,
size
);
size_t
tbufReserve
(
SBufferWriter
*
buf
,
size_t
size
)
{
tbufEnsureCapacity
(
buf
,
size
);
size_t
old
=
buf
->
pos
;
buf
->
pos
+=
size
;
return
old
;
}
char
*
tbufGetData
(
SBufferWriter
*
buf
,
bool
takeOver
)
{
char
*
tbufGetData
(
SBufferWriter
*
buf
,
bool
takeOver
)
{
char
*
ret
=
buf
->
data
;
if
(
takeOver
)
{
if
(
takeOver
)
{
buf
->
pos
=
0
;
buf
->
size
=
0
;
buf
->
data
=
NULL
;
...
...
@@ -252,192 +252,174 @@ char* tbufGetData( SBufferWriter* buf, bool takeOver ) {
return
ret
;
}
void
tbufWrite
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
size
)
{
assert
(
data
!=
NULL
);
tbufEnsureCapacity
(
buf
,
size
);
memcpy
(
buf
->
data
+
buf
->
pos
,
data
,
size
);
void
tbufWrite
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
size
)
{
assert
(
data
!=
NULL
);
tbufEnsureCapacity
(
buf
,
size
);
memcpy
(
buf
->
data
+
buf
->
pos
,
data
,
size
);
buf
->
pos
+=
size
;
}
void
tbufWriteAt
(
SBufferWriter
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
)
{
assert
(
data
!=
NULL
);
void
tbufWriteAt
(
SBufferWriter
*
buf
,
size_t
pos
,
const
void
*
data
,
size_t
size
)
{
assert
(
data
!=
NULL
);
// this function can only be called to fill the gap on previous writes,
// so 'pos + size <= buf->pos' must be true
assert
(
pos
+
size
<=
buf
->
pos
);
memcpy
(
buf
->
data
+
pos
,
data
,
size
);
assert
(
pos
+
size
<=
buf
->
pos
);
memcpy
(
buf
->
data
+
pos
,
data
,
size
);
}
static
void
tbufWriteLength
(
SBufferWriter
*
buf
,
size_t
len
)
{
static
void
tbufWriteLength
(
SBufferWriter
*
buf
,
size_t
len
)
{
// maximum length is 65535, if larger length is required
// this function and the corresponding read function need to be
// revised.
assert
(
len
<=
0xffff
);
tbufWriteUint16
(
buf
,
(
uint16_t
)
len
);
assert
(
len
<=
0xffff
);
tbufWriteUint16
(
buf
,
(
uint16_t
)
len
);
}
void
tbufWriteStringLen
(
SBufferWriter
*
buf
,
const
char
*
str
,
size_t
len
)
{
tbufWriteLength
(
buf
,
len
);
tbufWrite
(
buf
,
str
,
len
);
tbufWriteChar
(
buf
,
'\0'
);
void
tbufWriteStringLen
(
SBufferWriter
*
buf
,
const
char
*
str
,
size_t
len
)
{
tbufWriteLength
(
buf
,
len
);
tbufWrite
(
buf
,
str
,
len
);
tbufWriteChar
(
buf
,
'\0'
);
}
void
tbufWriteString
(
SBufferWriter
*
buf
,
const
char
*
str
)
{
tbufWriteStringLen
(
buf
,
str
,
strlen
(
str
)
);
}
void
tbufWriteString
(
SBufferWriter
*
buf
,
const
char
*
str
)
{
tbufWriteStringLen
(
buf
,
str
,
strlen
(
str
));
}
void
tbufWriteBinary
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
len
)
{
tbufWriteLength
(
buf
,
len
);
tbufWrite
(
buf
,
data
,
len
);
void
tbufWriteBinary
(
SBufferWriter
*
buf
,
const
void
*
data
,
size_t
len
)
{
tbufWriteLength
(
buf
,
len
);
tbufWrite
(
buf
,
data
,
len
);
}
void
tbufWriteBool
(
SBufferWriter
*
buf
,
bool
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteBool
(
SBufferWriter
*
buf
,
bool
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteBoolAt
(
SBufferWriter
*
buf
,
size_t
pos
,
bool
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteBoolAt
(
SBufferWriter
*
buf
,
size_t
pos
,
bool
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteChar
(
SBufferWriter
*
buf
,
char
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteChar
(
SBufferWriter
*
buf
,
char
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteCharAt
(
SBufferWriter
*
buf
,
size_t
pos
,
char
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteCharAt
(
SBufferWriter
*
buf
,
size_t
pos
,
char
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteInt8
(
SBufferWriter
*
buf
,
int8_t
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt8
(
SBufferWriter
*
buf
,
int8_t
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteInt8At
(
SBufferWriter
*
buf
,
size_t
pos
,
int8_t
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt8At
(
SBufferWriter
*
buf
,
size_t
pos
,
int8_t
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteUint8
(
SBufferWriter
*
buf
,
uint8_t
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint8
(
SBufferWriter
*
buf
,
uint8_t
data
)
{
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteUint8At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint8_t
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint8At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint8_t
data
)
{
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
));
}
void
tbufWriteInt16
(
SBufferWriter
*
buf
,
int16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int16_t
)
htons
(
data
);
void
tbufWriteInt16
(
SBufferWriter
*
buf
,
int16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int16_t
)
htons
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt16At
(
SBufferWriter
*
buf
,
size_t
pos
,
int16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int16_t
)
htons
(
data
);
void
tbufWriteInt16At
(
SBufferWriter
*
buf
,
size_t
pos
,
int16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int16_t
)
htons
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint16
(
SBufferWriter
*
buf
,
uint16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htons
(
data
);
void
tbufWriteUint16
(
SBufferWriter
*
buf
,
uint16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htons
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint16At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htons
(
data
);
void
tbufWriteUint16At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint16_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htons
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt32
(
SBufferWriter
*
buf
,
int32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int32_t
)
htonl
(
data
);
void
tbufWriteInt32
(
SBufferWriter
*
buf
,
int32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int32_t
)
htonl
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt32At
(
SBufferWriter
*
buf
,
size_t
pos
,
int32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int32_t
)
htonl
(
data
);
void
tbufWriteInt32At
(
SBufferWriter
*
buf
,
size_t
pos
,
int32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int32_t
)
htonl
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint32
(
SBufferWriter
*
buf
,
uint32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htonl
(
data
);
void
tbufWriteUint32
(
SBufferWriter
*
buf
,
uint32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htonl
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint32At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htonl
(
data
);
void
tbufWriteUint32At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint32_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htonl
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt64
(
SBufferWriter
*
buf
,
int64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int64_t
)
htobe64
(
data
);
void
tbufWriteInt64
(
SBufferWriter
*
buf
,
int64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int64_t
)
htobe64
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteInt64At
(
SBufferWriter
*
buf
,
size_t
pos
,
int64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int64_t
)
htobe64
(
data
);
void
tbufWriteInt64At
(
SBufferWriter
*
buf
,
size_t
pos
,
int64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
(
int64_t
)
htobe64
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint64
(
SBufferWriter
*
buf
,
uint64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htobe64
(
data
);
void
tbufWriteUint64
(
SBufferWriter
*
buf
,
uint64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htobe64
(
data
);
}
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
tbufWrite
(
buf
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteUint64At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htobe64
(
data
);
void
tbufWriteUint64At
(
SBufferWriter
*
buf
,
size_t
pos
,
uint64_t
data
)
{
if
(
buf
->
endian
)
{
data
=
htobe64
(
data
);
}
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
tbufWriteAt
(
buf
,
pos
,
&
data
,
sizeof
(
data
)
);
}
void
tbufWriteFloat
(
SBufferWriter
*
buf
,
float
data
)
{
void
tbufWriteFloat
(
SBufferWriter
*
buf
,
float
data
)
{
Un4B
_un
;
_un
.
f
=
data
;
if
(
buf
->
endian
)
{
_un
.
ui
=
htonl
(
_un
.
ui
);
if
(
buf
->
endian
)
{
_un
.
ui
=
htonl
(
_un
.
ui
);
}
tbufWrite
(
buf
,
&
_un
,
sizeof
(
_un
)
);
tbufWrite
(
buf
,
&
_un
,
sizeof
(
_un
)
);
}
void
tbufWriteFloatAt
(
SBufferWriter
*
buf
,
size_t
pos
,
float
data
)
{
void
tbufWriteFloatAt
(
SBufferWriter
*
buf
,
size_t
pos
,
float
data
)
{
Un4B
_un
;
_un
.
f
=
data
;
if
(
buf
->
endian
)
{
_un
.
ui
=
htonl
(
_un
.
ui
);
if
(
buf
->
endian
)
{
_un
.
ui
=
htonl
(
_un
.
ui
);
}
tbufWriteAt
(
buf
,
pos
,
&
_un
,
sizeof
(
_un
)
);
tbufWriteAt
(
buf
,
pos
,
&
_un
,
sizeof
(
_un
)
);
}
void
tbufWriteDouble
(
SBufferWriter
*
buf
,
double
data
)
{
void
tbufWriteDouble
(
SBufferWriter
*
buf
,
double
data
)
{
Un8B
_un
;
_un
.
d
=
data
;
if
(
buf
->
endian
)
{
_un
.
ull
=
htobe64
(
_un
.
ull
);
if
(
buf
->
endian
)
{
_un
.
ull
=
htobe64
(
_un
.
ull
);
}
tbufWrite
(
buf
,
&
_un
,
sizeof
(
_un
)
);
tbufWrite
(
buf
,
&
_un
,
sizeof
(
_un
)
);
}
void
tbufWriteDoubleAt
(
SBufferWriter
*
buf
,
size_t
pos
,
double
data
)
{
void
tbufWriteDoubleAt
(
SBufferWriter
*
buf
,
size_t
pos
,
double
data
)
{
Un8B
_un
;
_un
.
d
=
data
;
if
(
buf
->
endian
)
{
_un
.
ull
=
htobe64
(
_un
.
ull
);
if
(
buf
->
endian
)
{
_un
.
ull
=
htobe64
(
_un
.
ull
);
}
tbufWriteAt
(
buf
,
pos
,
&
_un
,
sizeof
(
_un
)
);
tbufWriteAt
(
buf
,
pos
,
&
_un
,
sizeof
(
_un
)
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录