Skip to content

Commit 70c8fa0

Browse files
committed
1 parent e908427 commit 70c8fa0

File tree

2 files changed

+71
-62
lines changed

2 files changed

+71
-62
lines changed

tp_transcode.c

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ enum type { TYPE_MAP = 1, TYPE_ARRAY = 2, TYPE_KEY = 4 };
2424
static inline void
2525
say_error_(tp_transcode_t *t, int code, const char *e, size_t len)
2626
{
27-
if (mp_unlikely(t->errmsg == NULL))
27+
if (mp_unlikely(t->errmsg != NULL))
2828
t->mf.free(t->mf.ctx, t->errmsg);
2929
t->errmsg = t->mf.alloc(t->mf.ctx, (len + 1) * sizeof(char));
3030
if (mp_unlikely(t->errmsg != NULL)) {
@@ -641,7 +641,7 @@ yajl_json2tp_transcode(void *ctx, const char *input, size_t input_size)
641641
yajl_status stat = yajl_parse(s_ctx->hand, input_, input_size);
642642
if (mp_unlikely(stat != yajl_status_ok)) {
643643

644-
if (!s_ctx->tc->errmsg) {
644+
if (s_ctx->tc->errmsg == NULL) {
645645

646646
stat = yajl_complete_parse(s_ctx->hand);
647647
unsigned char *err = yajl_get_error(s_ctx->hand, 0,
@@ -1032,17 +1032,23 @@ tp_codec_t codecs[TP_CODEC_MAX] = {
10321032
* Public API
10331033
*/
10341034

1035-
static void *def_alloc(void *ctx, size_t s) {
1035+
static void *
1036+
def_alloc(void *ctx, size_t s)
1037+
{
10361038
(void)ctx;
10371039
return malloc(s);
10381040
}
10391041

1040-
static void *def_realloc(void *ctx, void *m, size_t s) {
1042+
static void *
1043+
def_realloc(void *ctx, void *m, size_t s)
1044+
{
10411045
(void)ctx;
10421046
return realloc(m, s);
10431047
}
10441048

1045-
static void def_free(void *ctx, void *m) {
1049+
static void
1050+
def_free(void *ctx, void *m)
1051+
{
10461052
(void)ctx;
10471053
free(m);
10481054
}
@@ -1077,6 +1083,61 @@ tp_transcode_init(tp_transcode_t *t, char *output, size_t output_size,
10771083
return TP_TRANSCODE_OK;
10781084
}
10791085

1086+
void
1087+
tp_transcode_free(tp_transcode_t *t)
1088+
{
1089+
assert(t);
1090+
assert(t->codec.ctx);
1091+
1092+
if (mp_unlikely(t->errmsg != NULL)) {
1093+
t->mf.free(t->mf.ctx, t->errmsg);
1094+
t->errmsg = NULL;
1095+
}
1096+
1097+
t->codec.free(t->codec.ctx);
1098+
t->codec.ctx = NULL;
1099+
}
1100+
1101+
enum tt_result
1102+
tp_transcode_complete(tp_transcode_t *t, size_t *complete_msg_size)
1103+
{
1104+
assert(t);
1105+
assert(t->codec.ctx);
1106+
*complete_msg_size = 0;
1107+
return t->codec.complete(t->codec.ctx, complete_msg_size);
1108+
}
1109+
1110+
enum tt_result
1111+
tp_transcode(tp_transcode_t *t, const char *b, size_t s)
1112+
{
1113+
assert(t);
1114+
assert(t->codec.ctx);
1115+
return t->codec.transcode(t->codec.ctx, b, s);
1116+
}
1117+
1118+
bool
1119+
tp_dump(char *output, size_t output_size,
1120+
const char *input, size_t input_size)
1121+
{
1122+
tp_transcode_t t;
1123+
if (tp_transcode_init(&t, output, output_size, TP_TO_JSON, NULL)
1124+
== TP_TRANSCODE_ERROR)
1125+
return false;
1126+
1127+
if (tp_transcode(&t, input, input_size) == TP_TRANSCODE_ERROR) {
1128+
tp_transcode_free(&t);
1129+
return false;
1130+
}
1131+
1132+
size_t complete_msg_size = 0;
1133+
tp_transcode_complete(&t, &complete_msg_size);
1134+
output[complete_msg_size] = '0';
1135+
1136+
tp_transcode_free(&t);
1137+
1138+
return complete_msg_size > 0;
1139+
}
1140+
10801141
ssize_t
10811142
tp_read_payload(const char * const buf, const char * const end)
10821143
{

tp_transcode.h

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -96,82 +96,30 @@ enum tt_result tp_transcode_init(tp_transcode_t *t,
9696

9797
/** Free struct tp_transcode
9898
*/
99-
static inline void
100-
tp_transcode_free(tp_transcode_t *t);
99+
void tp_transcode_free(tp_transcode_t *t);
101100

102101
/** Feed transcoder (see tp_transcode_init).
103102
*
104103
* Returns TP_TRANSCODE_OK if bytes enought for finish transcoding
105104
* Returns TP_TRANSCODE_ERROR if error occurred
106105
*/
107-
static inline enum tt_result
108-
tp_transcode(tp_transcode_t *t, const char *b, size_t s);
106+
enum tt_result tp_transcode(tp_transcode_t *t, const char *b, size_t s);
109107

110108
/** Finalize transcoding.
111109
*
112110
* Returns TP_TRANSCODE_OK if transcoding done
113111
* Returns TP_TRANSCODE_ERROR if error occurred
114112
*/
115-
static inline enum tt_result
113+
enum tt_result
116114
tp_transcode_complete(tp_transcode_t *t, size_t *complete_msg_size);
117115

118116
/**
119117
* Dump Tarantool message to output in JSON format
120118
* Returns true, false
121119
*/
122-
static inline bool
120+
bool
123121
tp_dump(char *output, size_t output_size,
124-
const char *input, size_t input_size);
125-
126-
static inline void
127-
tp_transcode_free(tp_transcode_t *t)
128-
{
129-
assert(t);
130-
assert(t->codec.ctx);
131-
if (t->errmsg)
132-
t->mf.free(t->mf.ctx, t->errmsg);
133-
t->codec.free(t->codec.ctx);
134-
}
135-
136-
static inline enum tt_result
137-
tp_transcode_complete(tp_transcode_t *t, size_t *complete_msg_size)
138-
{
139-
assert(t);
140-
assert(t->codec.ctx);
141-
*complete_msg_size = 0;
142-
return t->codec.complete(t->codec.ctx, complete_msg_size);
143-
}
144-
145-
static inline enum tt_result
146-
tp_transcode(tp_transcode_t *t, const char *b, size_t s)
147-
{
148-
assert(t);
149-
assert(t->codec.ctx);
150-
return t->codec.transcode(t->codec.ctx, b, s);
151-
}
152-
153-
static inline bool
154-
tp_dump(char *output, size_t output_size,
155-
const char *input, size_t input_size)
156-
{
157-
tp_transcode_t t;
158-
if (tp_transcode_init(&t, output, output_size, TP_TO_JSON, NULL)
159-
== TP_TRANSCODE_ERROR)
160-
return false;
161-
162-
if (tp_transcode(&t, input, input_size) == TP_TRANSCODE_ERROR) {
163-
tp_transcode_free(&t);
164-
return false;
165-
}
166-
167-
size_t complete_msg_size = 0;
168-
tp_transcode_complete(&t, &complete_msg_size);
169-
output[complete_msg_size] = '0';
170-
171-
tp_transcode_free(&t);
172-
173-
return complete_msg_size > 0;
174-
}
122+
const char *input, size_t input_size);
175123

176124
#ifdef __cplusplus
177125
} /* extern "C" */

0 commit comments

Comments
 (0)