Do not interleave code and declarations in C files

Avoid C99 style to interleave code and declarations in order to compile msgpackc with Visual Studion < 2013
This commit is contained in:
tbeu 2015-03-26 21:48:03 +01:00
parent 7bee573a72
commit 8921f9dcfc
9 changed files with 514 additions and 478 deletions

View File

@ -52,6 +52,7 @@ void unpack(receiver* r) {
msgpack_unpack_return ret;
char* buf;
size_t recv_len;
int recv_count = 0;
msgpack_unpacked_init(&result);
if (msgpack_unpacker_buffer_capacity(unp) < EACH_RECV_SIZE) {
@ -64,7 +65,6 @@ void unpack(receiver* r) {
msgpack_unpacker_buffer_consumed(unp, recv_len);
int recv_count = 0;
while (recv_len > 0) {
int i = 0;
printf("receive count: %d %zd bytes received.:\n", recv_count++, recv_len);

View File

@ -3,12 +3,15 @@
int main(void)
{
/* msgpack::sbuffer is a simple buffer implementation. */
msgpack_sbuffer sbuf;
msgpack_packer pk;
msgpack_zone mempool;
msgpack_object deserialized;
/* msgpack::sbuffer is a simple buffer implementation. */
msgpack_sbuffer_init(&sbuf);
/* serialize values into the buffer using msgpack_sbuffer_write callback function. */
msgpack_packer pk;
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
msgpack_pack_array(&pk, 3);
@ -19,10 +22,8 @@ int main(void)
/* deserialize the buffer into msgpack_object instance. */
/* deserialized object is valid during the msgpack_zone instance alive. */
msgpack_zone mempool;
msgpack_zone_init(&mempool, 2048);
msgpack_object deserialized;
msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);
/* print the deserialized object. */

View File

@ -1,13 +1,16 @@
#include <msgpack.h>
#include <assert.h>
void test()
{
size_t size = 10000000;
msgpack_sbuffer buf;
msgpack_packer * pk;
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_sbuffer_init(&buf);
msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
msgpack_pack_array(pk, size);
{
@ -17,9 +20,6 @@ void test()
}
msgpack_packer_free(pk);
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) {

View File

@ -1,14 +1,17 @@
#include <msgpack.h>
#include <assert.h>
void test()
{
uint64_t test_u64 = 0xFFF0000000000001LL;
size_t size = 10000000;
msgpack_sbuffer buf;
msgpack_packer * pk;
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_sbuffer_init(&buf);
msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
msgpack_pack_array(pk, size);
{
@ -18,9 +21,6 @@ void test()
}
msgpack_packer_free(pk);
size_t upk_pos = 0;
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) {

View File

@ -97,7 +97,7 @@ msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
{
assert(len >= *off);
{
const unsigned char* p = (unsigned char*)data + *off;
const unsigned char* const pe = (unsigned char*)data + len;
const void* n = NULL;
@ -457,7 +457,7 @@ _end:
return ret;
}
}
#undef msgpack_unpack_func
#undef msgpack_unpack_callback

View File

@ -71,8 +71,10 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
case MSGPACK_OBJECT_ARRAY:
{
int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; }
if(ret < 0) {
return ret;
}
else {
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
@ -82,12 +84,15 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
return 0;
}
}
case MSGPACK_OBJECT_MAP:
{
int ret = msgpack_pack_map(pk, d.via.map.size);
if(ret < 0) { return ret; }
if(ret < 0) {
return ret;
}
else {
msgpack_object_kv* kv = d.via.map.ptr;
msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size;
for(; kv != kvend; ++kv) {
@ -99,6 +104,7 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
return 0;
}
}
default:
return -1;
@ -172,9 +178,9 @@ void msgpack_object_print(FILE* out, msgpack_object o)
fprintf(out, "[");
if(o.via.array.size != 0) {
msgpack_object* p = o.via.array.ptr;
msgpack_object* const pend = o.via.array.ptr + o.via.array.size;
msgpack_object_print(out, *p);
++p;
msgpack_object* const pend = o.via.array.ptr + o.via.array.size;
for(; p < pend; ++p) {
fprintf(out, ", ");
msgpack_object_print(out, *p);
@ -187,11 +193,11 @@ void msgpack_object_print(FILE* out, msgpack_object o)
fprintf(out, "{");
if(o.via.map.size != 0) {
msgpack_object_kv* p = o.via.map.ptr;
msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size;
msgpack_object_print(out, p->key);
fprintf(out, "=>");
msgpack_object_print(out, p->val);
++p;
msgpack_object_kv* const pend = o.via.map.ptr + o.via.map.size;
for(; p < pend; ++p) {
fprintf(out, ", ");
msgpack_object_print(out, p->key);

View File

@ -58,8 +58,8 @@ static int template_execute(
static inline msgpack_object template_callback_root(unpack_user* u)
{
MSGPACK_UNUSED(u);
msgpack_object o;
MSGPACK_UNUSED(u);
o.type = MSGPACK_OBJECT_NIL;
return o;
}
@ -306,26 +306,28 @@ static inline _msgpack_atomic_counter_t get_count(void* buffer)
return *(volatile _msgpack_atomic_counter_t*)buffer;
}
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size)
{
char* buffer;
void* ctx;
msgpack_zone* z;
if(initial_buffer_size < COUNTER_SIZE) {
initial_buffer_size = COUNTER_SIZE;
}
char* buffer = (char*)malloc(initial_buffer_size);
buffer = (char*)malloc(initial_buffer_size);
if(buffer == NULL) {
return false;
}
void* ctx = malloc(sizeof(template_context));
ctx = malloc(sizeof(template_context));
if(ctx == NULL) {
free(buffer);
return false;
}
msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(z == NULL) {
free(ctx);
free(buffer);
@ -357,7 +359,6 @@ void msgpack_unpacker_destroy(msgpack_unpacker* mpac)
decr_count(mpac->buffer);
}
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size)
{
msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker));
@ -394,6 +395,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
}
if(mpac->off == COUNTER_SIZE) {
char* tmp;
size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE
while(next_size < size + mpac->used) {
size_t tmp_next_size = next_size * 2;
@ -404,7 +406,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
next_size = tmp_next_size;
}
char* tmp = (char*)realloc(mpac->buffer, next_size);
tmp = (char*)realloc(mpac->buffer, next_size);
if(tmp == NULL) {
return false;
}
@ -413,6 +415,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
mpac->free = next_size - mpac->used;
} else {
char* tmp;
size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE
size_t not_parsed = mpac->used - mpac->off;
while(next_size < size + not_parsed + COUNTER_SIZE) {
@ -424,7 +427,7 @@ bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
next_size = tmp_next_size;
}
char* tmp = (char*)malloc(next_size);
tmp = (char*)malloc(next_size);
if(tmp == NULL) {
return false;
}
@ -470,16 +473,19 @@ msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
{
msgpack_zone* r;
msgpack_zone* old;
if(!msgpack_unpacker_flush_zone(mpac)) {
return NULL;
}
msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
if(r == NULL) {
return NULL;
}
msgpack_zone* old = mpac->z;
old = mpac->z;
mpac->z = r;
CTX_CAST(mpac->ctx)->user.z = mpac->z;
@ -514,9 +520,11 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac)
msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result)
{
int ret;
msgpack_unpacked_destroy(result);
int ret = msgpack_unpacker_execute(mpac);
ret = msgpack_unpacker_execute(mpac);
if(ret < 0) {
result->zone = NULL;
@ -546,14 +554,15 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
// FIXME
return MSGPACK_UNPACK_CONTINUE;
}
else {
int e;
template_context ctx;
template_init(&ctx);
ctx.user.z = result_zone;
ctx.user.referenced = false;
int e = template_execute(&ctx, data, len, &noff);
e = template_execute(&ctx, data, len, &noff);
if(e < 0) {
return MSGPACK_UNPACK_PARSE_ERROR;
}
@ -572,14 +581,15 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
return MSGPACK_UNPACK_SUCCESS;
}
}
msgpack_unpack_return
msgpack_unpack_next(msgpack_unpacked* result,
const char* data, size_t len, size_t* off)
{
size_t noff = 0;
msgpack_unpacked_destroy(result);
size_t noff = 0;
if(off != NULL) { noff = *off; }
if(len <= noff) {
@ -593,14 +603,15 @@ msgpack_unpack_next(msgpack_unpacked* result,
if (!result->zone) {
return MSGPACK_UNPACK_NOMEM_ERROR;
}
else {
int e;
template_context ctx;
template_init(&ctx);
ctx.user.z = result->zone;
ctx.user.referenced = false;
int e = template_execute(&ctx, data, len, &noff);
e = template_execute(&ctx, data, len, &noff);
if(e < 0) {
msgpack_zone_free(result->zone);
result->zone = NULL;
@ -617,6 +628,7 @@ msgpack_unpack_next(msgpack_unpacked* result,
return MSGPACK_UNPACK_SUCCESS;
}
}
#if defined(MSGPACK_OLD_COMPILER_BUS_ERROR_WORKAROUND)
// FIXME: Dirty hack to avoid a bus error caused by OS X's old gcc.

View File

@ -29,15 +29,19 @@ struct msgpack_vrefbuffer_chunk {
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
size_t ref_size, size_t chunk_size)
{
size_t nfirst;
struct iovec* array;
msgpack_vrefbuffer_chunk* chunk;
vbuf->chunk_size = chunk_size;
vbuf->ref_size =
ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ?
ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ;
size_t nfirst = (sizeof(struct iovec) < 72/2) ?
nfirst = (sizeof(struct iovec) < 72/2) ?
72 / sizeof(struct iovec) : 8;
struct iovec* array = (struct iovec*)malloc(
array = (struct iovec*)malloc(
sizeof(struct iovec) * nfirst);
if(array == NULL) {
return false;
@ -47,13 +51,13 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
vbuf->end = array + nfirst;
vbuf->array = array;
msgpack_vrefbuffer_chunk* chunk = (msgpack_vrefbuffer_chunk*)malloc(
chunk = (msgpack_vrefbuffer_chunk*)malloc(
sizeof(msgpack_vrefbuffer_chunk) + chunk_size);
if(chunk == NULL) {
free(array);
return false;
}
else {
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
ib->free = chunk_size;
@ -63,6 +67,7 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
return true;
}
}
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
{
@ -89,6 +94,7 @@ void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf)
c = n;
}
{
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
msgpack_vrefbuffer_chunk* chunk = ib->head;
chunk->next = NULL;
@ -97,6 +103,7 @@ void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf)
vbuf->tail = vbuf->array;
}
}
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
const char* buf, size_t len)
@ -127,14 +134,16 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
const char* buf, size_t len)
{
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
char* m;
if(ib->free < len) {
msgpack_vrefbuffer_chunk* chunk;
size_t sz = vbuf->chunk_size;
if(sz < len) {
sz = len;
}
msgpack_vrefbuffer_chunk* chunk = (msgpack_vrefbuffer_chunk*)malloc(
chunk = (msgpack_vrefbuffer_chunk*)malloc(
sizeof(msgpack_vrefbuffer_chunk) + sz);
if(chunk == NULL) {
return -1;
@ -146,7 +155,7 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
}
char* m = ib->ptr;
m = ib->ptr;
memcpy(m, buf, len);
ib->free -= len;
ib->ptr += len;
@ -172,9 +181,10 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
empty->next = NULL;
{
const size_t nused = (size_t)(vbuf->tail - vbuf->array);
if(to->tail + nused < vbuf->end) {
struct iovec* nvec;
const size_t tosize = (size_t)(to->tail - to->array);
const size_t reqsize = nused + tosize;
size_t nnext = (size_t)(to->end - to->array) * 2;
@ -187,7 +197,7 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
nnext = tmp_nnext;
}
struct iovec* nvec = (struct iovec*)realloc(
nvec = (struct iovec*)realloc(
to->array, sizeof(struct iovec)*nnext);
if(nvec == NULL) {
free(empty);
@ -204,7 +214,7 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
to->tail += nused;
vbuf->tail = vbuf->array;
{
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
msgpack_vrefbuffer_inner_buffer* const toib = &to->inner_buffer;
@ -223,6 +233,8 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
ib->head = empty;
ib->free = sz;
ib->ptr = ((char*)empty) + sizeof(msgpack_vrefbuffer_chunk);
}
}
return 0;
}

View File

@ -75,6 +75,7 @@ static inline void clear_chunk_list(msgpack_zone_chunk_list* cl, size_t chunk_si
void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
{
msgpack_zone_chunk_list* const cl = &zone->chunk_list;
msgpack_zone_chunk* chunk;
size_t sz = zone->chunk_size;
@ -87,9 +88,12 @@ void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
sz = tmp_sz;
}
msgpack_zone_chunk* chunk = (msgpack_zone_chunk*)malloc(
chunk = (msgpack_zone_chunk*)malloc(
sizeof(msgpack_zone_chunk) + sz);
if (chunk == NULL) return NULL;
if (chunk == NULL) {
return NULL;
}
else {
char* ptr = ((char*)chunk) + sizeof(msgpack_zone_chunk);
chunk->next = cl->head;
cl->head = chunk;
@ -98,6 +102,7 @@ void* msgpack_zone_malloc_expand(msgpack_zone* zone, size_t size)
return ptr;
}
}
static inline void init_finalizer_array(msgpack_zone_finalizer_array* fa)
@ -131,6 +136,7 @@ bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
void (*func)(void* data), void* data)
{
msgpack_zone_finalizer_array* const fa = &zone->finalizer_array;
msgpack_zone_finalizer* tmp;
const size_t nused = (size_t)(fa->end - fa->array);
@ -143,8 +149,7 @@ bool msgpack_zone_push_finalizer_expand(msgpack_zone* zone,
nnext = nused * 2;
}
msgpack_zone_finalizer* tmp =
(msgpack_zone_finalizer*)realloc(fa->array,
tmp = (msgpack_zone_finalizer*)realloc(fa->array,
sizeof(msgpack_zone_finalizer) * nnext);
if(tmp == NULL) {
return false;