diff --git a/Makefile.am b/Makefile.am index 9d1282d8..d79f7eb3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = c cpp nobase_include_HEADERS = \ - msgpack/pack/inline_impl.h \ - msgpack/unpack/inline_context.h \ - msgpack/unpack/inline_impl.h + msgpack/pack_template.h \ + msgpack/unpack_define.h \ + msgpack/unpack_template.h diff --git a/c/Makefile.am b/c/Makefile.am index af8f3adb..f1c57e9b 100644 --- a/c/Makefile.am +++ b/c/Makefile.am @@ -2,17 +2,12 @@ lib_LTLIBRARIES = libmsgpackc.la libmsgpackc_la_SOURCES = \ pack.c \ - unpack.c \ - unpack_inline.c + unpack.c nobase_include_HEADERS = \ msgpack.h \ msgpack/pack.h \ msgpack/unpack.h -noinst_HEADERS = \ - pack_inline.h \ - unpack_context.h - libmsgpackc_la_LDFLAGS = -version-info 0:0:0 diff --git a/c/bench.c b/c/bench.c index 6b8466f2..fa717c0a 100644 --- a/c/bench.c +++ b/c/bench.c @@ -16,14 +16,15 @@ void reset_timer() gettimeofday(&g_timer, NULL); } -double show_timer() +void show_timer(size_t bufsz) { struct timeval endtime; gettimeofday(&endtime, NULL); double sec = (endtime.tv_sec - g_timer.tv_sec) + (double)(endtime.tv_usec - g_timer.tv_usec) / 1000 / 1000; printf("%f sec\n", sec); - return sec; + printf("%f MB\n", ((double)bufsz)/1024/1024); + printf("%f Mbps\n", ((double)bufsz)*8/sec/1000/1000); } @@ -38,32 +39,33 @@ static int reformat_start_array(void * ctx) { return 1; } static int reformat_end_array(void * ctx) { return 1; } -static void* unpack_unsigned_int_8(void* data, uint8_t d) { return NULL; } -static void* unpack_unsigned_int_16(void* data, uint16_t d) { return NULL; } -static void* unpack_unsigned_int_32(void* data, uint32_t d) { return NULL; } -static void* unpack_unsigned_int_64(void* data, uint64_t d) { return NULL; } -static void* unpack_signed_int_8(void* data, int8_t d) { return NULL; } -static void* unpack_signed_int_16(void* data, int16_t d) { return NULL; } -static void* unpack_signed_int_32(void* data, int32_t d) { return NULL; } -static void* unpack_signed_int_64(void* data, int64_t d) { return NULL; } +static void* unpack_uint8(void* data, uint8_t d) { return NULL; } +static void* unpack_uint16(void* data, uint16_t d) { return NULL; } +static void* unpack_uint32(void* data, uint32_t d) { return NULL; } +static void* unpack_uint64(void* data, uint64_t d) { return NULL; } +static void* unpack_int8(void* data, int8_t d) { return NULL; } +static void* unpack_int16(void* data, int16_t d) { return NULL; } +static void* unpack_int32(void* data, int32_t d) { return NULL; } +static void* unpack_int64(void* data, int64_t d) { return NULL; } static void* unpack_float(void* data, float d) { return NULL; } static void* unpack_double(void* data, double d) { return NULL; } static void* unpack_nil(void* data) { return NULL; } static void* unpack_true(void* data) { return NULL; } static void* unpack_false(void* data) { return NULL; } -static void* unpack_array_start(void* data, unsigned int n) { return NULL; } +static void* unpack_array(void* data, unsigned int n) { return NULL; } static void unpack_array_item(void* data, void* c, void* o) { } -static void* unpack_map_start(void* data, unsigned int n) { return NULL; } +static void* unpack_map(void* data, unsigned int n) { return NULL; } static void unpack_map_item(void* data, void* c, void* k, void* v) { } static void* unpack_raw(void* data, const char* b, const char* p, unsigned int l) { /*printf("unpack raw %p %lu\n",p,l);*/ return NULL; } + typedef struct { size_t allocated; size_t length; char* buffer; } pack_buffer; -static const size_t PACK_INITIAL_BUFFER_SIZE = 512; +static const size_t PACK_INITIAL_BUFFER_SIZE = 32*1024; static void pack_buffer_init(pack_buffer* data) { @@ -84,7 +86,7 @@ static void pack_buffer_free(pack_buffer* data) free(data->buffer); } -static void pack_append_buffer(void* user, const unsigned char* b, unsigned int l) +static void pack_append_buffer(void* user, const char* b, unsigned int l) { pack_buffer* data = (pack_buffer*)user; if(data->allocated - data->length < l) { @@ -128,7 +130,6 @@ void bench_json(void) yajl_handle h = yajl_alloc(&callbacks, &hcfg, NULL); - double sec; const unsigned char * buf; unsigned int len; @@ -143,11 +144,9 @@ void bench_json(void) } yajl_gen_array_close(g); } - sec = show_timer(); + show_timer(len); yajl_gen_get_buf(g, &buf, &len); - printf("%u KB\n", len / 1024); - printf("%f MB/s\n", len / sec / 1024 / 1024); puts("----"); puts("parse integer"); @@ -159,9 +158,7 @@ void bench_json(void) fprintf(stderr, (const char *) str); } } - sec = show_timer(); - - printf("%f MB/s\n", len / sec / 1024 / 1024); + show_timer(len); //yajl_gen_clear(g); @@ -182,11 +179,9 @@ void bench_json(void) } yajl_gen_array_close(g); } - sec = show_timer(); + show_timer(len); yajl_gen_get_buf(g, &buf, &len); - printf("%u KB\n", len / 1024); - printf("%f MB/s\n", len / sec / 1024 / 1024); puts("----"); puts("parse string"); @@ -198,15 +193,14 @@ void bench_json(void) fprintf(stderr, (const char *) str); } } - sec = show_timer(); - - printf("%f MB/s\n", len / sec / 1024 / 1024); + show_timer(len); yajl_gen_free(g); yajl_free(h); } + void bench_msgpack(void) { puts("== MessagePack =="); @@ -214,33 +208,33 @@ void bench_msgpack(void) pack_buffer mpkbuf; pack_buffer_init(&mpkbuf); + msgpack_pack_t* mpk = msgpack_pack_new( &mpkbuf, pack_append_buffer); msgpack_unpack_callback cb = { - unpack_unsigned_int_8, - unpack_unsigned_int_16, - unpack_unsigned_int_32, - unpack_unsigned_int_64, - unpack_signed_int_8, - unpack_signed_int_16, - unpack_signed_int_32, - unpack_signed_int_64, + unpack_uint8, + unpack_uint16, + unpack_uint32, + unpack_uint64, + unpack_int8, + unpack_int16, + unpack_int32, + unpack_int64, unpack_float, unpack_double, unpack_nil, unpack_true, unpack_false, - unpack_array_start, + unpack_array, unpack_array_item, - unpack_map_start, + unpack_map, unpack_map_item, unpack_raw, }; msgpack_unpack_t* mupk = msgpack_unpack_new(NULL, &cb); - double sec; size_t len; const char* buf; @@ -254,12 +248,10 @@ void bench_msgpack(void) msgpack_pack_unsigned_int(mpk, i); } } - sec = show_timer(); + show_timer(mpkbuf.length); len = mpkbuf.length; buf = mpkbuf.buffer; - printf("%lu KB\n", len / 1024); - printf("%f MB/s\n", len / sec / 1024 / 1024); puts("----"); puts("unpack integer"); @@ -273,9 +265,7 @@ void bench_msgpack(void) fprintf(stderr, "Not finished.\n"); } } - sec = show_timer(); - - printf("%f MB/s\n", len / sec / 1024 / 1024); + show_timer(mpkbuf.length); pack_buffer_reset(&mpkbuf); @@ -292,12 +282,10 @@ void bench_msgpack(void) msgpack_pack_raw(mpk, TASK_STR_PTR, i); } } - sec = show_timer(); + show_timer(mpkbuf.length); len = mpkbuf.length; buf = mpkbuf.buffer; - printf("%lu KB\n", len / 1024); - printf("%f MB/s\n", len / sec / 1024 / 1024); puts("----"); puts("unpack string"); @@ -311,9 +299,7 @@ void bench_msgpack(void) fprintf(stderr, "Not finished.\n"); } } - sec = show_timer(); - - printf("%f MB/s\n", len / sec / 1024 / 1024); + show_timer(mpkbuf.length); msgpack_unpack_free(mupk); diff --git a/c/bench.mk b/c/bench.mk new file mode 100644 index 00000000..c765e312 --- /dev/null +++ b/c/bench.mk @@ -0,0 +1,9 @@ + +CFLAGS += -Wall -g -I. -I.. -O4 +LDFLAGS += -lyajl + +all: bench + +bench: bench.o pack.o unpack.o pack.h unpack.h + $(CC) bench.o pack.o unpack.o $(CFLAGS) $(LDFLAGS) -o $@ + diff --git a/c/bench_inline.c b/c/bench_inline.c deleted file mode 100644 index bb3c4316..00000000 --- a/c/bench_inline.c +++ /dev/null @@ -1,325 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include -#include - - -static struct timeval g_timer; - -void reset_timer() -{ - gettimeofday(&g_timer, NULL); -} - -double show_timer() -{ - struct timeval endtime; - gettimeofday(&endtime, NULL); - double sec = (endtime.tv_sec - g_timer.tv_sec) - + (double)(endtime.tv_usec - g_timer.tv_usec) / 1000 / 1000; - printf("%f sec\n", sec); - return sec; -} - - -static int reformat_null(void * ctx) { return 1; } -static int reformat_boolean(void * ctx, int boolean) { return 1; } -static int reformat_number(void * ctx, const char * s, unsigned int l) { return 1; } -static int reformat_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { return 1; } -static int reformat_map_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { return 1; } -static int reformat_start_map(void * ctx) { return 1; } -static int reformat_end_map(void * ctx) { return 1; } -static int reformat_start_array(void * ctx) { return 1; } -static int reformat_end_array(void * ctx) { return 1; } - - -typedef void* msgpack_object; - -typedef struct { -} msgpack_unpack_context; - -#include "msgpack/unpack/inline_context.h" - -static inline void* msgpack_unpack_init(msgpack_unpack_context* x) { return NULL; } -static inline void* msgpack_unpack_unsigned_int_8(msgpack_unpack_context* x, uint8_t d) { return NULL; } -static inline void* msgpack_unpack_unsigned_int_16(msgpack_unpack_context* x, uint16_t d) { return NULL; } -static inline void* msgpack_unpack_unsigned_int_32(msgpack_unpack_context* x, uint32_t d) { return NULL; } -static inline void* msgpack_unpack_unsigned_int_64(msgpack_unpack_context* x, uint64_t d) { return NULL; } -static inline void* msgpack_unpack_signed_int_8(msgpack_unpack_context* x, int8_t d) { return NULL; } -static inline void* msgpack_unpack_signed_int_16(msgpack_unpack_context* x, int16_t d) { return NULL; } -static inline void* msgpack_unpack_signed_int_32(msgpack_unpack_context* x, int32_t d) { return NULL; } -static inline void* msgpack_unpack_signed_int_64(msgpack_unpack_context* x, int64_t d) { return NULL; } -static inline void* msgpack_unpack_float(msgpack_unpack_context* x, float d) { return NULL; } -static inline void* msgpack_unpack_double(msgpack_unpack_context* x, double d) { return NULL; } -static inline void* msgpack_unpack_nil(msgpack_unpack_context* x) { return NULL; } -static inline void* msgpack_unpack_true(msgpack_unpack_context* x) { return NULL; } -static inline void* msgpack_unpack_false(msgpack_unpack_context* x) { return NULL; } -static inline void* msgpack_unpack_array_start(msgpack_unpack_context* x, unsigned int n) { return NULL; } -static inline void msgpack_unpack_array_item(msgpack_unpack_context* x, void* c, void* o) { } -static inline void* msgpack_unpack_map_start(msgpack_unpack_context* x, unsigned int n) { return NULL; } -static inline void msgpack_unpack_map_item(msgpack_unpack_context* x, void* c, void* k, void* v) { } -static inline void* msgpack_unpack_raw(msgpack_unpack_context* x, const void* b, const void* p, size_t l) { return NULL; } - -#include "msgpack/unpack/inline_impl.h" - -typedef struct { - size_t allocated; - size_t length; - char* buffer; -} pack_buffer; - -static const size_t PACK_INITIAL_BUFFER_SIZE = 512; - -static void pack_buffer_init(pack_buffer* data) -{ - data->buffer = malloc(PACK_INITIAL_BUFFER_SIZE); - data->length = 0; - data->allocated = PACK_INITIAL_BUFFER_SIZE; -} - -static void pack_buffer_reset(pack_buffer* data) -{ - data->buffer = realloc(data->buffer, PACK_INITIAL_BUFFER_SIZE); - data->allocated = PACK_INITIAL_BUFFER_SIZE; - data->length = 0; -} - -static void pack_buffer_free(pack_buffer* data) -{ - free(data->buffer); -} - -static void pack_append_buffer(void* user, const unsigned char* b, unsigned int l) -{ - pack_buffer* data = (pack_buffer*)user; - if(data->allocated - data->length < l) { - data->buffer = realloc(data->buffer, data->allocated*2); - data->allocated *= 2; - } - memcpy(data->buffer + data->length, b, l); - data->length += l; -} - - -static const unsigned int TASK_INT_NUM = 1<<24; -static const unsigned int TASK_STR_LEN = 1<<15; -//static const unsigned int TASK_INT_NUM = 1<<20; -//static const unsigned int TASK_STR_LEN = 1<<12; -static const char* TASK_STR_PTR; - - -void bench_json(void) -{ - puts("== JSON =="); - - - yajl_gen_config gcfg = {0, NULL}; - yajl_gen g = yajl_gen_alloc(&gcfg); - - yajl_parser_config hcfg = { 0, 0 }; - yajl_callbacks callbacks = { - reformat_null, - reformat_boolean, - NULL, - NULL, - reformat_number, - reformat_start_map, - reformat_map_key, - reformat_end_map, - reformat_start_array, - reformat_end_array - }; - yajl_handle h = yajl_alloc(&callbacks, &hcfg, NULL); - - - double sec; - const unsigned char * buf; - unsigned int len; - - - puts("generate integer"); - reset_timer(); - { - unsigned int i; - yajl_gen_array_open(g); - for(i=0; i < TASK_INT_NUM; ++i) { - yajl_gen_integer(g, i); - } - yajl_gen_array_close(g); - } - sec = show_timer(); - - yajl_gen_get_buf(g, &buf, &len); - printf("%u KB\n", len / 1024); - printf("%f Mbps\n", len / sec / 1024 / 1024); - - puts("----"); - puts("parse integer"); - reset_timer(); - { - yajl_status stat = yajl_parse(h, buf, len); - if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { - unsigned char * str = yajl_get_error(h, 1, buf, len); - fprintf(stderr, (const char *) str); - } - } - sec = show_timer(); - - printf("%f Mbps\n", len / sec / 1024 / 1024); - - - //yajl_gen_clear(g); - yajl_gen_free(g); - g = yajl_gen_alloc(&gcfg); - yajl_free(h); - h = yajl_alloc(&callbacks, &hcfg, NULL); - - - puts("----"); - puts("generate string"); - reset_timer(); - { - unsigned int i; - yajl_gen_array_open(g); - for(i=0; i < TASK_STR_LEN; ++i) { - yajl_gen_string(g, (const unsigned char*)TASK_STR_PTR, i); - } - yajl_gen_array_close(g); - } - sec = show_timer(); - - yajl_gen_get_buf(g, &buf, &len); - printf("%u KB\n", len / 1024); - printf("%f Mbps\n", len / sec / 1024 / 1024); - - puts("----"); - puts("parse string"); - reset_timer(); - { - yajl_status stat = yajl_parse(h, buf, len); - if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { - unsigned char * str = yajl_get_error(h, 1, buf, len); - fprintf(stderr, (const char *) str); - } - } - sec = show_timer(); - - printf("%f Mbps\n", len / sec / 1024 / 1024); - - - yajl_gen_free(g); - yajl_free(h); -} - -void bench_msgpack(void) -{ - puts("== MessagePack =="); - - - pack_buffer mpkbuf; - pack_buffer_init(&mpkbuf); - msgpack_pack_t* mpk = msgpack_pack_new( - &mpkbuf, pack_append_buffer); - - msgpack_unpacker mupk; - msgpack_unpacker_init(&mupk); - - double sec; - size_t len; - const char* buf; - - - puts("pack integer"); - reset_timer(); - { - unsigned int i; - msgpack_pack_array(mpk, TASK_INT_NUM); - for(i=0; i < TASK_INT_NUM; ++i) { - msgpack_pack_unsigned_int(mpk, i); - } - } - sec = show_timer(); - - len = mpkbuf.length; - buf = mpkbuf.buffer; - printf("%lu KB\n", len / 1024); - printf("%f Mbps\n", len / sec / 1024 / 1024); - - puts("----"); - puts("unpack integer"); - reset_timer(); - { - size_t off = 0; - int ret = msgpack_unpacker_execute(&mupk, buf, len, &off); - if(ret < 0) { - fprintf(stderr, "Parse error.\n"); - } else if(ret == 0) { - fprintf(stderr, "Not finished.\n"); - } - } - sec = show_timer(); - - printf("%f Mbps\n", len / sec / 1024 / 1024); - - - pack_buffer_reset(&mpkbuf); - msgpack_unpacker_init(&mupk); - - - puts("----"); - puts("pack string"); - reset_timer(); - { - unsigned int i; - msgpack_pack_array(mpk, TASK_STR_LEN); - for(i=0; i < TASK_STR_LEN; ++i) { - msgpack_pack_raw(mpk, TASK_STR_PTR, i); - } - } - sec = show_timer(); - - len = mpkbuf.length; - buf = mpkbuf.buffer; - printf("%lu KB\n", len / 1024); - printf("%f Mbps\n", len / sec / 1024 / 1024); - - puts("----"); - puts("unpack string"); - reset_timer(); - { - size_t off = 0; - int ret = msgpack_unpacker_execute(&mupk, buf, len, &off); - if(ret < 0) { - fprintf(stderr, "Parse error.\n"); - } else if(ret == 0) { - fprintf(stderr, "Not finished.\n"); - } - } - sec = show_timer(); - - printf("%f Mbps\n", len / sec / 1024 / 1024); - - - msgpack_pack_free(mpk); - pack_buffer_free(&mpkbuf); -} - -int main(int argc, char* argv[]) -{ - char* str = malloc(TASK_STR_LEN); - memset(str, 'a', TASK_STR_LEN); - TASK_STR_PTR = str; - - bench_msgpack(); - //bench_json(); - - return 0; -} - - - diff --git a/c/pack.c b/c/pack.c index 05bd38d6..766a9d17 100644 --- a/c/pack.c +++ b/c/pack.c @@ -15,11 +15,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "pack_inline.h" +#include "msgpack/pack.h" #include -#include -msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_callback_t callback) + +#define msgpack_pack_inline_func(name) \ + void msgpack_pack_##name + +#define msgpack_pack_user msgpack_pack_t* + +#define msgpack_pack_append_buffer(user, buf, len) \ + (*(user)->callback)((user)->data, (const char*)buf, len) + +#include "msgpack/pack_template.h" + +msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_append_buffer_t callback) { msgpack_pack_t* ctx = calloc(1, sizeof(msgpack_pack_t)); if(!ctx) { return NULL; } @@ -33,8 +43,3 @@ void msgpack_pack_free(msgpack_pack_t* ctx) free(ctx); } -static inline void msgpack_pack_append_buffer(msgpack_pack_t* ctx, const unsigned char* b, unsigned int l) -{ - ctx->callback(ctx->data, b, l); -} - diff --git a/c/pack.h b/c/pack.h index 3144f37d..c6cadf40 100644 --- a/c/pack.h +++ b/c/pack.h @@ -21,26 +21,32 @@ #include #include -typedef void (*msgpack_pack_callback_t)(void* data, const unsigned char* b, unsigned int i); +#ifdef __cplusplus +extern "C" { +#endif + + +typedef void (*msgpack_pack_append_buffer_t)(void* data, const char* b, unsigned int i); typedef struct { void* data; - msgpack_pack_callback_t callback; + msgpack_pack_append_buffer_t callback; } msgpack_pack_t; -msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_callback_t callback); +msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_append_buffer_t callback); + void msgpack_pack_free(msgpack_pack_t* ctx); void msgpack_pack_int(msgpack_pack_t* ctx, int d); void msgpack_pack_unsigned_int(msgpack_pack_t* ctx, unsigned int d); -void msgpack_pack_unsigned_int_8(msgpack_pack_t* ctx, uint8_t d); -void msgpack_pack_unsigned_int_16(msgpack_pack_t* ctx, uint16_t d); -void msgpack_pack_unsigned_int_32(msgpack_pack_t* ctx, uint32_t d); -void msgpack_pack_unsigned_int_64(msgpack_pack_t* ctx, uint64_t d); -void msgpack_pack_signed_int_8(msgpack_pack_t* ctx, int8_t d); -void msgpack_pack_signed_int_16(msgpack_pack_t* ctx, int16_t d); -void msgpack_pack_signed_int_32(msgpack_pack_t* ctx, int32_t d); -void msgpack_pack_signed_int_64(msgpack_pack_t* ctx, int64_t d); +void msgpack_pack_uint8(msgpack_pack_t* ctx, uint8_t d); +void msgpack_pack_uint16(msgpack_pack_t* ctx, uint16_t d); +void msgpack_pack_uint32(msgpack_pack_t* ctx, uint32_t d); +void msgpack_pack_uint64(msgpack_pack_t* ctx, uint64_t d); +void msgpack_pack_int8(msgpack_pack_t* ctx, int8_t d); +void msgpack_pack_int16(msgpack_pack_t* ctx, int16_t d); +void msgpack_pack_int32(msgpack_pack_t* ctx, int32_t d); +void msgpack_pack_int64(msgpack_pack_t* ctx, int64_t d); void msgpack_pack_float(msgpack_pack_t* ctx, float d); void msgpack_pack_double(msgpack_pack_t* ctx, double d); void msgpack_pack_nil(msgpack_pack_t* ctx); @@ -51,5 +57,10 @@ void msgpack_pack_map(msgpack_pack_t* ctx, unsigned int n); void msgpack_pack_string(msgpack_pack_t* ctx, const char* b); void msgpack_pack_raw(msgpack_pack_t* ctx, const void* b, size_t l); + +#ifdef __cplusplus +} +#endif + #endif /* msgpack/pack.h */ diff --git a/c/pack_inline.h b/c/pack_inline.h deleted file mode 100644 index dd43a20e..00000000 --- a/c/pack_inline.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * MessagePack packing routine for C - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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. - */ -#ifndef PACK_INLINE_H__ -#define PACK_INLINE_H__ - -#include "msgpack/pack.h" - -typedef msgpack_pack_t* msgpack_pack_context; - -static inline void msgpack_pack_append_buffer(msgpack_pack_t* x, const unsigned char* b, unsigned int l); - -#include -#include /* __BYTE_ORDER */ -#include "msgpack/pack/inline_impl.h" - -#endif /* pack_inline.h */ - diff --git a/c/unpack.c b/c/unpack.c index a2fc0660..3058427f 100644 --- a/c/unpack.c +++ b/c/unpack.c @@ -16,15 +16,101 @@ * limitations under the License. */ #include "msgpack/unpack.h" -#include "unpack_context.h" +#include "msgpack/unpack_define.h" #include + +#define msgpack_unpack_struct(name) \ + struct template_##name + +#define msgpack_unpack_func(ret, name) \ + ret template_func_##name + +#define msgpack_unpack_callback(name) \ + template_callback_##name + +#define msgpack_unpack_object void* + +#define msgpack_unpack_user msgpack_unpack_t + + +struct template_context; + +static void template_func_init(struct template_context* ctx); + +static void* template_func_data(struct template_context* ctx); + +static int template_func_execute(struct template_context* ctx, + const char* data, size_t len, size_t* off); + + +static inline void* template_callback_init(msgpack_unpack_t* x) +{ return NULL; } + +static inline void* template_callback_uint8(msgpack_unpack_t* x, uint8_t d) +{ return x->callback.unpack_uint8(x->data, d); } + +static inline void* template_callback_uint16(msgpack_unpack_t* x, uint16_t d) +{ return x->callback.unpack_uint16(x->data, d); } + +static inline void* template_callback_uint32(msgpack_unpack_t* x, uint32_t d) +{ return x->callback.unpack_uint32(x->data, d); } + +static inline void* template_callback_uint64(msgpack_unpack_t* x, uint64_t d) +{ return x->callback.unpack_uint64(x->data, d); } + +static inline void* template_callback_int8(msgpack_unpack_t* x, int8_t d) +{ return x->callback.unpack_int8(x->data, d); } + +static inline void* template_callback_int16(msgpack_unpack_t* x, int16_t d) +{ return x->callback.unpack_int16(x->data, d); } + +static inline void* template_callback_int32(msgpack_unpack_t* x, int32_t d) +{ return x->callback.unpack_int32(x->data, d); } + +static inline void* template_callback_int64(msgpack_unpack_t* x, int64_t d) +{ return x->callback.unpack_int64(x->data, d); } + +static inline void* template_callback_float(msgpack_unpack_t* x, float d) +{ return x->callback.unpack_float(x->data, d); } + +static inline void* template_callback_double(msgpack_unpack_t* x, double d) +{ return x->callback.unpack_double(x->data, d); } + +static inline void* template_callback_nil(msgpack_unpack_t* x) +{ return x->callback.unpack_nil(x->data); } + +static inline void* template_callback_true(msgpack_unpack_t* x) +{ return x->callback.unpack_true(x->data); } + +static inline void* template_callback_false(msgpack_unpack_t* x) +{ return x->callback.unpack_false(x->data); } + +static inline void* template_callback_array(msgpack_unpack_t* x, unsigned int n) +{ return x->callback.unpack_array(x->data, n); } + +static inline void template_callback_array_item(msgpack_unpack_t* x, void* c, void* o) +{ x->callback.unpack_array_item(x->data, c, o); } + +static inline void* template_callback_map(msgpack_unpack_t* x, unsigned int n) +{ return x->callback.unpack_map(x->data, n); } + +static inline void template_callback_map_item(msgpack_unpack_t* x, void* c, void* k, void* v) +{ x->callback.unpack_map_item(x->data, c, k, v); } + +static inline void* template_callback_raw(msgpack_unpack_t* x, const char* b, const char* p, unsigned int l) +{ return x->callback.unpack_raw(x->data, b, p, l); } + + +#include "msgpack/unpack_template.h" + + msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callback) { - msgpack_unpacker* ctx; - ctx = (msgpack_unpacker*)calloc(1, sizeof(msgpack_unpacker)); + struct template_context* ctx; + ctx = (struct template_context*)calloc(1, sizeof(struct template_context)); if(ctx == NULL) { return NULL; } - msgpack_unpacker_init(ctx); + template_func_init(ctx); ((msgpack_unpack_t*)ctx)->data = data; ((msgpack_unpack_t*)ctx)->callback = *callback; return (msgpack_unpack_t*)ctx; @@ -32,25 +118,27 @@ msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callba void msgpack_unpack_free(msgpack_unpack_t* ctx) { - free((msgpack_unpacker*)ctx); + free((struct template_context*)ctx); } void* msgpack_unpack_data(msgpack_unpack_t* ctx) { - return msgpack_unpacker_data((msgpack_unpacker*)ctx); + return template_func_data((struct template_context*)ctx); } void msgpack_unpack_reset(msgpack_unpack_t* ctx) { - msgpack_unpack_t x = ((msgpack_unpacker*)ctx)->user; - msgpack_unpacker_init((msgpack_unpacker*)ctx); - ((msgpack_unpacker*)ctx)->user = x; + msgpack_unpack_t x = ((struct template_context*)ctx)->user; + template_func_init((struct template_context*)ctx); + ((struct template_context*)ctx)->user = x; } -int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off) +int msgpack_unpack_execute(msgpack_unpack_t* ctx, + const char* data, size_t len, size_t* off) { - return msgpack_unpacker_execute( - (msgpack_unpacker*)ctx, + return template_func_execute( + (struct template_context*)ctx, data, len, off); } + diff --git a/c/unpack.h b/c/unpack.h index 63674390..c1cacaba 100644 --- a/c/unpack.h +++ b/c/unpack.h @@ -21,24 +21,29 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct { - void* (*unpack_unsigned_int_8)(void* data, uint8_t d); - void* (*unpack_unsigned_int_16)(void* data, uint16_t d); - void* (*unpack_unsigned_int_32)(void* data, uint32_t d); - void* (*unpack_unsigned_int_64)(void* data, uint64_t d); - void* (*unpack_signed_int_8)(void* data, int8_t d); - void* (*unpack_signed_int_16)(void* data, int16_t d); - void* (*unpack_signed_int_32)(void* data, int32_t d); - void* (*unpack_signed_int_64)(void* data, int64_t d); + void* (*unpack_uint8)(void* data, uint8_t d); + void* (*unpack_uint16)(void* data, uint16_t d); + void* (*unpack_uint32)(void* data, uint32_t d); + void* (*unpack_uint64)(void* data, uint64_t d); + void* (*unpack_int8)(void* data, int8_t d); + void* (*unpack_int16)(void* data, int16_t d); + void* (*unpack_int32)(void* data, int32_t d); + void* (*unpack_int64)(void* data, int64_t d); void* (*unpack_float)(void* data, float d); void* (*unpack_double)(void* data, double d); void* (*unpack_nil)(void* data); void* (*unpack_true)(void* data); void* (*unpack_false)(void* data); - void* (*unpack_array_start)(void* data, unsigned int n); + void* (*unpack_array)(void* data, unsigned int n); void (*unpack_array_item)(void* data, void* c, void* o); - void* (*unpack_map_start)(void* data, unsigned int n); - void (*unpack_map_item)(void* data, void* c, void* k, void* v); + void* (*unpack_map)(void* data, unsigned int n); + void (*unpack_map_item)(void* data, void* c, void* k, void* v); void* (*unpack_raw)(void* data, const char* b, const char* p, unsigned int l); } msgpack_unpack_callback; @@ -51,8 +56,14 @@ msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callba void msgpack_unpack_free(msgpack_unpack_t* ctx); void msgpack_unpack_reset(msgpack_unpack_t* ctx); -int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off); +int msgpack_unpack_execute(msgpack_unpack_t* ctx, + const char* data, size_t len, size_t* off); void* msgpack_unpack_data(msgpack_unpack_t* ctx); + +#ifdef __cplusplus +} +#endif + #endif /* msgpack/unpack.h */ diff --git a/c/unpack_context.h b/c/unpack_context.h deleted file mode 100644 index d7b03883..00000000 --- a/c/unpack_context.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * MessagePack unpacking routine for C - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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. - */ -#ifndef UNPACK_CONTEXT_H__ -#define UNPACK_CONTEXT_H__ - -#include "msgpack/unpack.h" - -typedef void* msgpack_object; - -typedef msgpack_unpack_t msgpack_unpack_context; - -#include "msgpack/unpack/inline_context.h" - -#endif /* unpack_context.h */ - diff --git a/c/unpack_inline.c b/c/unpack_inline.c deleted file mode 100644 index 52822825..00000000 --- a/c/unpack_inline.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * MessagePack unpacking routine for C - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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 "unpack_context.h" - -static inline void* msgpack_unpack_init(msgpack_unpack_t* x) -{ return NULL; } - -static inline void* msgpack_unpack_unsigned_int_8(msgpack_unpack_t* x, uint8_t d) -{ return x->callback.unpack_unsigned_int_8(x->data, d); } - -static inline void* msgpack_unpack_unsigned_int_16(msgpack_unpack_t* x, uint16_t d) -{ return x->callback.unpack_unsigned_int_16(x->data, d); } - -static inline void* msgpack_unpack_unsigned_int_32(msgpack_unpack_t* x, uint32_t d) -{ return x->callback.unpack_unsigned_int_32(x->data, d); } - -static inline void* msgpack_unpack_unsigned_int_64(msgpack_unpack_t* x, uint64_t d) -{ return x->callback.unpack_unsigned_int_64(x->data, d); } - -static inline void* msgpack_unpack_signed_int_8(msgpack_unpack_t* x, int8_t d) -{ return x->callback.unpack_signed_int_8(x->data, d); } - -static inline void* msgpack_unpack_signed_int_16(msgpack_unpack_t* x, int16_t d) -{ return x->callback.unpack_signed_int_16(x->data, d); } - -static inline void* msgpack_unpack_signed_int_32(msgpack_unpack_t* x, int32_t d) -{ return x->callback.unpack_signed_int_32(x->data, d); } - -static inline void* msgpack_unpack_signed_int_64(msgpack_unpack_t* x, int64_t d) -{ return x->callback.unpack_signed_int_64(x->data, d); } - -static inline void* msgpack_unpack_float(msgpack_unpack_t* x, float d) -{ return x->callback.unpack_float(x->data, d); } - -static inline void* msgpack_unpack_double(msgpack_unpack_t* x, double d) -{ return x->callback.unpack_double(x->data, d); } - -static inline void* msgpack_unpack_nil(msgpack_unpack_t* x) -{ return x->callback.unpack_nil(x->data); } - -static inline void* msgpack_unpack_true(msgpack_unpack_t* x) -{ return x->callback.unpack_true(x->data); } - -static inline void* msgpack_unpack_false(msgpack_unpack_t* x) -{ return x->callback.unpack_false(x->data); } - -static inline void* msgpack_unpack_array_start(msgpack_unpack_t* x, unsigned int n) -{ return x->callback.unpack_array_start(x->data, n); } - -static inline void msgpack_unpack_array_item(msgpack_unpack_t* x, void* c, void* o) -{ x->callback.unpack_array_item(x->data, c, o); } - -static inline void* msgpack_unpack_map_start(msgpack_unpack_t* x, unsigned int n) -{ return x->callback.unpack_map_start(x->data, n); } - -static inline void msgpack_unpack_map_item(msgpack_unpack_t* x, void* c, void* k, void* v) -{ x->callback.unpack_map_item(x->data, c, k, v); } - -static inline void* msgpack_unpack_raw(msgpack_unpack_t* x, const char* b, const char* p, unsigned int l) -{ return x->callback.unpack_raw(x->data, b, p, l); } - - -#include "msgpack/unpack/inline_impl.h" - diff --git a/cpp/Makefile.am b/cpp/Makefile.am index 08ea21f4..aa22cc70 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -3,7 +3,6 @@ lib_LTLIBRARIES = libmsgpack.la libmsgpack_la_SOURCES = \ object.cpp \ unpack.cpp \ - unpack_inline.cpp \ zone.cpp nobase_include_HEADERS = \ @@ -14,13 +13,11 @@ nobase_include_HEADERS = \ msgpack/zone.hpp noinst_HEADERS = \ - unpack_context.hpp \ msgpack/zone.hpp.erb # FIXME object.lo: msgpack/zone.hpp unpack.lo: msgpack/zone.hpp -unpack_context.lo: msgpack/zone.hpp zone.lo: msgpack/zone.hpp msgpack/zone.hpp: msgpack/zone.hpp.erb diff --git a/cpp/bench.cpp b/cpp/bench.cpp index 4133a2e9..517a8708 100644 --- a/cpp/bench.cpp +++ b/cpp/bench.cpp @@ -6,10 +6,10 @@ #include #include -static const unsigned int TASK_INT_NUM = 1<<24; -static const unsigned int TASK_STR_LEN = 1<<15; -//static const unsigned int TASK_INT_NUM = 1<<23; -//static const unsigned int TASK_STR_LEN = 1<<14; +//static const unsigned int TASK_INT_NUM = 1<<24; +//static const unsigned int TASK_STR_LEN = 1<<15; +static const unsigned int TASK_INT_NUM = 1<<22; +static const unsigned int TASK_STR_LEN = 1<<13; static const char* TASK_STR_PTR; @@ -24,7 +24,7 @@ public: + (double)(endtime.tv_usec - m_timeval.tv_usec) / 1000 / 1000; std::cout << sec << " sec" << std::endl; std::cout << (double(bufsz)/1024/1024) << " MB" << std::endl; - std::cout << (bufsz/sec/1024/1024*8) << " Mbps" << std::endl; + std::cout << (bufsz/sec/1000/1000*8) << " Mbps" << std::endl; } private: timeval m_timeval; diff --git a/cpp/bench.mk b/cpp/bench.mk new file mode 100644 index 00000000..52a4b80d --- /dev/null +++ b/cpp/bench.mk @@ -0,0 +1,9 @@ + +CXXFLAGS += -Wall -g -I. -I.. -O4 +LDFLAGS += + +all: bench + +bench: bench.o unpack.o zone.o object.o pack.hpp unpack.hpp zone.hpp object.hpp + $(CXX) bench.o unpack.o zone.o object.o $(CFLAGS) $(LDFLAGS) -o $@ + diff --git a/cpp/object.cpp b/cpp/object.cpp index 7bb98419..ef2a68c9 100644 --- a/cpp/object.cpp +++ b/cpp/object.cpp @@ -155,35 +155,35 @@ inline void numeric_pack(dynamic_packer& p, V v); template <> inline void numeric_pack(dynamic_packer& p, uint8_t v) - { p.pack_unsigned_int_8(v); } + { p.pack_uint8(v); } template <> inline void numeric_pack(dynamic_packer& p, uint16_t v) - { p.pack_unsigned_int_16(v); } + { p.pack_uint16(v); } template <> inline void numeric_pack(dynamic_packer& p, uint32_t v) - { p.pack_unsigned_int_32(v); } + { p.pack_uint32(v); } template <> inline void numeric_pack(dynamic_packer& p, uint64_t v) - { p.pack_unsigned_int_64(v); } + { p.pack_uint64(v); } template <> inline void numeric_pack(dynamic_packer& p, int8_t v) - { p.pack_unsigned_int_8(v); } + { p.pack_int8(v); } template <> inline void numeric_pack(dynamic_packer& p, int16_t v) - { p.pack_unsigned_int_16(v); } + { p.pack_int16(v); } template <> inline void numeric_pack(dynamic_packer& p, int32_t v) - { p.pack_unsigned_int_32(v); } + { p.pack_int32(v); } template <> inline void numeric_pack(dynamic_packer& p, int64_t v) - { p.pack_unsigned_int_64(v); } + { p.pack_int64(v); } template <> inline void numeric_pack(dynamic_packer& p, float v) diff --git a/cpp/pack.hpp b/cpp/pack.hpp index 95806792..f3eeb34b 100644 --- a/cpp/pack.hpp +++ b/cpp/pack.hpp @@ -34,14 +34,14 @@ public: public: void pack_int(int d) { pack_int_impl(m_stream, d); } void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); } - void pack_unsigned_int_8(uint8_t d) { pack_unsigned_int_8_impl(m_stream, d); } - void pack_unsigned_int_16(uint16_t d) { pack_unsigned_int_16_impl(m_stream, d); } - void pack_unsigned_int_32(uint32_t d) { pack_unsigned_int_32_impl(m_stream, d); } - void pack_unsigned_int_64(uint64_t d) { pack_unsigned_int_64_impl(m_stream, d); } - void pack_signed_int_8(uint8_t d) { pack_signed_int_8_impl(m_stream, d); } - void pack_signed_int_16(uint16_t d) { pack_signed_int_16_impl(m_stream, d); } - void pack_signed_int_32(uint32_t d) { pack_signed_int_32_impl(m_stream, d); } - void pack_signed_int_64(uint64_t d) { pack_signed_int_64_impl(m_stream, d); } + void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); } + void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); } + void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); } + void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); } + void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); } + void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); } + void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); } + void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); } void pack_float(float d) { pack_float_impl(m_stream, d); } void pack_double(double d) { pack_double_impl(m_stream, d); } void pack_nil() { pack_nil(m_stream); } @@ -49,20 +49,19 @@ public: void pack_false() { pack_false(m_stream); } void pack_array(unsigned int n) { pack_array_impl(m_stream, n); } void pack_map(unsigned int n) { pack_map_impl(m_stream, n); } - void pack_string(const char* b) { pack_string_impl(m_stream, b); } void pack_raw(const char* b, size_t l) { pack_raw_impl(m_stream, (const void*)b, l); } private: static void pack_int_impl(Stream& x, int d); static void pack_unsigned_int_impl(Stream& x, unsigned int d); - static void pack_unsigned_int_8_impl(Stream& x, uint8_t d); - static void pack_unsigned_int_16_impl(Stream& x, uint16_t d); - static void pack_unsigned_int_32_impl(Stream& x, uint32_t d); - static void pack_unsigned_int_64_impl(Stream& x, uint64_t d); - static void pack_signed_int_8_impl(Stream& x, int8_t d); - static void pack_signed_int_16_impl(Stream& x, int16_t d); - static void pack_signed_int_32_impl(Stream& x, int32_t d); - static void pack_signed_int_64_impl(Stream& x, int64_t d); + static void pack_uint8_impl(Stream& x, uint8_t d); + static void pack_uint16_impl(Stream& x, uint16_t d); + static void pack_uint32_impl(Stream& x, uint32_t d); + static void pack_uint64_impl(Stream& x, uint64_t d); + static void pack_int8_impl(Stream& x, int8_t d); + static void pack_int16_impl(Stream& x, int16_t d); + static void pack_int32_impl(Stream& x, int32_t d); + static void pack_int64_impl(Stream& x, int64_t d); static void pack_float_impl(Stream& x, float d); static void pack_double_impl(Stream& x, double d); static void pack_nil_impl(Stream& x); @@ -70,7 +69,6 @@ private: static void pack_false_impl(Stream& x); static void pack_array_impl(Stream& x, unsigned int n); static void pack_map_impl(Stream& x, unsigned int n); - static void pack_string_impl(Stream& x, const char* b); static void pack_raw_impl(Stream& x, const void* b, size_t l); static void append_buffer(Stream& x, const unsigned char* buf, unsigned int len) { x.write((const char*)buf, len); } @@ -85,11 +83,10 @@ private: #define msgpack_pack_inline_func(name) \ template \ inline void packer::pack_ ## name ## _impl -#define msgpack_pack_context Stream& +#define msgpack_pack_user Stream& #define msgpack_pack_append_buffer append_buffer -#include "msgpack/pack/inline_impl.h" -#undef msgpack_pack_context -#undef msgpack_pack_append_buffer +#include "msgpack/pack_template.h" + template packer::packer(Stream& s) : m_stream(s) { } @@ -118,14 +115,14 @@ public: public: void pack_int(int d) { pack_int_impl(m_stream, d); } void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); } - void pack_unsigned_int_8(uint8_t d) { pack_unsigned_int_8_impl(m_stream, d); } - void pack_unsigned_int_16(uint16_t d) { pack_unsigned_int_16_impl(m_stream, d); } - void pack_unsigned_int_32(uint32_t d) { pack_unsigned_int_32_impl(m_stream, d); } - void pack_unsigned_int_64(uint64_t d) { pack_unsigned_int_64_impl(m_stream, d); } - void pack_signed_int_8(uint8_t d) { pack_signed_int_8_impl(m_stream, d); } - void pack_signed_int_16(uint16_t d) { pack_signed_int_16_impl(m_stream, d); } - void pack_signed_int_32(uint32_t d) { pack_signed_int_32_impl(m_stream, d); } - void pack_signed_int_64(uint64_t d) { pack_signed_int_64_impl(m_stream, d); } + void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); } + void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); } + void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); } + void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); } + void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); } + void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); } + void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); } + void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); } void pack_float(float d) { pack_float_impl(m_stream, d); } void pack_double(double d) { pack_double_impl(m_stream, d); } void pack_nil() { pack_nil_impl(m_stream); } @@ -139,14 +136,14 @@ public: private: static void pack_int_impl(dynamic_stream& x, int d); static void pack_unsigned_int_impl(dynamic_stream& x, unsigned int d); - static void pack_unsigned_int_8_impl(dynamic_stream& x, uint8_t d); - static void pack_unsigned_int_16_impl(dynamic_stream& x, uint16_t d); - static void pack_unsigned_int_32_impl(dynamic_stream& x, uint32_t d); - static void pack_unsigned_int_64_impl(dynamic_stream& x, uint64_t d); - static void pack_signed_int_8_impl(dynamic_stream& x, int8_t d); - static void pack_signed_int_16_impl(dynamic_stream& x, int16_t d); - static void pack_signed_int_32_impl(dynamic_stream& x, int32_t d); - static void pack_signed_int_64_impl(dynamic_stream& x, int64_t d); + static void pack_uint8_impl(dynamic_stream& x, uint8_t d); + static void pack_uint16_impl(dynamic_stream& x, uint16_t d); + static void pack_uint32_impl(dynamic_stream& x, uint32_t d); + static void pack_uint64_impl(dynamic_stream& x, uint64_t d); + static void pack_int8_impl(dynamic_stream& x, int8_t d); + static void pack_int16_impl(dynamic_stream& x, int16_t d); + static void pack_int32_impl(dynamic_stream& x, int32_t d); + static void pack_int64_impl(dynamic_stream& x, int64_t d); static void pack_float_impl(dynamic_stream& x, float d); static void pack_double_impl(dynamic_stream& x, double d); static void pack_nil_impl(dynamic_stream& x); @@ -166,14 +163,11 @@ private: dynamic_packer(); }; -#undef MSGPACK_PACK_INLINE_IMPL_H__ #define msgpack_pack_inline_func(name) \ inline void dynamic_packer::pack_ ## name ## _impl -#define msgpack_pack_context dynamic_stream& +#define msgpack_pack_user dynamic_stream& #define msgpack_pack_append_buffer append_buffer -#include "msgpack/pack/inline_impl.h" -#undef msgpack_pack_context -#undef msgpack_pack_append_buffer +#include "msgpack/pack_template.h" template dynamic_packer::dynamic_packer(Stream& s) : m_stream(s) { } diff --git a/cpp/unpack.cpp b/cpp/unpack.cpp index fe7f4b51..873d3dab 100644 --- a/cpp/unpack.cpp +++ b/cpp/unpack.cpp @@ -16,12 +16,97 @@ // limitations under the License. // #include "msgpack/unpack.hpp" -#include "unpack_context.hpp" +#include "msgpack/unpack_define.h" #include namespace msgpack { +#define msgpack_unpack_struct(name) \ + struct msgpack_unpacker_##name + +#define msgpack_unpack_func(ret, name) \ + ret msgpack_unpacker_##name + +#define msgpack_unpack_callback(name) \ + msgpack_unpack_##name + +#define msgpack_unpack_object object_class* + +#define msgpack_unpack_user zone* + + +struct msgpack_unpacker_context; + +static void msgpack_unpacker_init(struct msgpack_unpacker_context* ctx); + +static object_class* msgpack_unpacker_data(struct msgpack_unpacker_context* ctx); + +static int msgpack_unpacker_execute(struct msgpack_unpacker_context* ctx, + const char* data, size_t len, size_t* off); + + +static inline object_class* msgpack_unpack_init(zone** z) +{ return NULL; } + +static inline object_class* msgpack_unpack_uint8(zone** z, uint8_t d) +{ return (*z)->nu8(d); } + +static inline object_class* msgpack_unpack_uint16(zone** z, uint16_t d) +{ return (*z)->nu16(d); } + +static inline object_class* msgpack_unpack_uint32(zone** z, uint32_t d) +{ return (*z)->nu32(d); } + +static inline object_class* msgpack_unpack_uint64(zone** z, uint64_t d) +{ return (*z)->nu64(d); } + +static inline object_class* msgpack_unpack_int8(zone** z, int8_t d) +{ return (*z)->ni8(d); } + +static inline object_class* msgpack_unpack_int16(zone** z, int16_t d) +{ return (*z)->ni16(d); } + +static inline object_class* msgpack_unpack_int32(zone** z, int32_t d) +{ return (*z)->ni32(d); } + +static inline object_class* msgpack_unpack_int64(zone** z, int64_t d) +{ return (*z)->ni64(d); } + +static inline object_class* msgpack_unpack_float(zone** z, float d) +{ return (*z)->nfloat(d); } + +static inline object_class* msgpack_unpack_double(zone** z, double d) +{ return (*z)->ndouble(d); } + +static inline object_class* msgpack_unpack_nil(zone** z) +{ return (*z)->nnil(); } + +static inline object_class* msgpack_unpack_true(zone** z) +{ return (*z)->ntrue(); } + +static inline object_class* msgpack_unpack_false(zone** z) +{ return (*z)->nfalse(); } + +static inline object_class* msgpack_unpack_array(zone** z, unsigned int n) +{ return (*z)->narray(n); } + +static inline void msgpack_unpack_array_item(zone** z, object_class* c, object_class* o) +{ reinterpret_cast(c)->push_back(o); } + +static inline object_class* msgpack_unpack_map(zone** z, unsigned int n) +{ return (*z)->nmap(); } + +static inline void msgpack_unpack_map_item(zone** z, object_class* c, object_class* k, object_class* v) +{ reinterpret_cast(c)->store(k, v); } + +static inline object_class* msgpack_unpack_raw(zone** z, const char* b, const char* p, unsigned int l) +{ return (*z)->nraw_ref(p, l); } + + +#include "msgpack/unpack_template.h" + + struct unpacker::context { context(zone* z) { @@ -65,7 +150,7 @@ struct unpacker::context { } private: - msgpack_unpacker m_ctx; + msgpack_unpacker_context m_ctx; private: context(); @@ -171,17 +256,26 @@ void unpacker::reset() } -object unpacker::unpack(const char* data, size_t len, zone& z) +object unpacker::unpack(const char* data, size_t len, zone& z, size_t* off) { context ctx(&z); - size_t off = 0; - int ret = ctx.execute(data, len, &off); - if(ret < 0) { - throw unpack_error("parse error"); - } else if(ret == 0) { - throw unpack_error("insufficient bytes"); - } else if(off < len) { - throw unpack_error("extra bytes"); + if(off) { + int ret = ctx.execute(data, len, off); + if(ret < 0) { + throw unpack_error("parse error"); + } else if(ret == 0) { + throw unpack_error("insufficient bytes"); + } + } else { + size_t noff = 0; + int ret = ctx.execute(data, len, &noff); + if(ret < 0) { + throw unpack_error("parse error"); + } else if(ret == 0) { + throw unpack_error("insufficient bytes"); + } else if(noff < len) { + throw unpack_error("extra bytes"); + } } return ctx.data(); } diff --git a/cpp/unpack.hpp b/cpp/unpack.hpp index 473e8e9e..86cfb6e9 100644 --- a/cpp/unpack.hpp +++ b/cpp/unpack.hpp @@ -100,7 +100,7 @@ private: unpacker(const unpacker&); public: - static object unpack(const char* data, size_t len, zone& z); + static object unpack(const char* data, size_t len, zone& z, size_t* off = NULL); }; @@ -136,9 +136,9 @@ inline void unpacker::remove_nonparsed_buffer() { m_used = m_off; } -inline object unpack(const char* data, size_t len, zone& z) +inline object unpack(const char* data, size_t len, zone& z, size_t* off = NULL) { - return unpacker::unpack(data, len, z); + return unpacker::unpack(data, len, z, off); } diff --git a/cpp/unpack_context.hpp b/cpp/unpack_context.hpp deleted file mode 100644 index 59bd8720..00000000 --- a/cpp/unpack_context.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// -// MessagePack for C++ deserializing routine -// -// Copyright (C) 2008 FURUHASHI Sadayuki -// -// Licensed 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. -// -#ifndef UNPACK_CONTEXT_HPP__ -#define UNPACK_CONTEXT_HPP__ - -#include "msgpack/zone.hpp" -#include "msgpack/object.hpp" - -typedef msgpack::object_class* msgpack_object; - -typedef msgpack::zone* msgpack_unpack_context; - -#include "msgpack/unpack/inline_context.h" - -#endif /* unpack_context.h */ - diff --git a/cpp/unpack_inline.cpp b/cpp/unpack_inline.cpp deleted file mode 100644 index 40f27699..00000000 --- a/cpp/unpack_inline.cpp +++ /dev/null @@ -1,86 +0,0 @@ -// -// MessagePack for C++ deserializing routine -// -// Copyright (C) 2008 FURUHASHI Sadayuki -// -// Licensed 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 "unpack_context.hpp" - - -extern "C" { -using namespace msgpack; - - -static inline object_class* msgpack_unpack_init(zone** z) -{ return NULL; } - -static inline object_class* msgpack_unpack_unsigned_int_8(zone** z, uint8_t d) -{ return (*z)->nu8(d); } - -static inline object_class* msgpack_unpack_unsigned_int_16(zone** z, uint16_t d) -{ return (*z)->nu16(d); } - -static inline object_class* msgpack_unpack_unsigned_int_32(zone** z, uint32_t d) -{ return (*z)->nu32(d); } - -static inline object_class* msgpack_unpack_unsigned_int_64(zone** z, uint64_t d) -{ return (*z)->nu64(d); } - -static inline object_class* msgpack_unpack_signed_int_8(zone** z, int8_t d) -{ return (*z)->ni8(d); } - -static inline object_class* msgpack_unpack_signed_int_16(zone** z, int16_t d) -{ return (*z)->ni16(d); } - -static inline object_class* msgpack_unpack_signed_int_32(zone** z, int32_t d) -{ return (*z)->ni32(d); } - -static inline object_class* msgpack_unpack_signed_int_64(zone** z, int64_t d) -{ return (*z)->ni64(d); } - -static inline object_class* msgpack_unpack_float(zone** z, float d) -{ return (*z)->nfloat(d); } - -static inline object_class* msgpack_unpack_double(zone** z, double d) -{ return (*z)->ndouble(d); } - -static inline object_class* msgpack_unpack_nil(zone** z) -{ return (*z)->nnil(); } - -static inline object_class* msgpack_unpack_true(zone** z) -{ return (*z)->ntrue(); } - -static inline object_class* msgpack_unpack_false(zone** z) -{ return (*z)->nfalse(); } - -static inline object_class* msgpack_unpack_array_start(zone** z, unsigned int n) -{ return (*z)->narray(n); } - -static inline void msgpack_unpack_array_item(zone** z, object_class* c, object_class* o) -{ reinterpret_cast(c)->push_back(o); } - -static inline object_class* msgpack_unpack_map_start(zone** z, unsigned int n) -{ return (*z)->nmap(); } - -static inline void msgpack_unpack_map_item(zone** z, object_class* c, object_class* k, object_class* v) -{ reinterpret_cast(c)->store(k, v); } - -static inline object_class* msgpack_unpack_raw(zone** z, const char* b, const char* p, unsigned int l) -{ return (*z)->nraw_ref(p, l); } - - -} // extern "C" - -#include "msgpack/unpack/inline_impl.h" - diff --git a/msgpack/pack/inline_impl.h b/msgpack/pack_template.h similarity index 77% rename from msgpack/pack/inline_impl.h rename to msgpack/pack_template.h index 635b6974..22d99119 100644 --- a/msgpack/pack/inline_impl.h +++ b/msgpack/pack_template.h @@ -1,5 +1,5 @@ /* - * MessagePack packing routine + * MessagePack packing routine template * * Copyright (C) 2008 FURUHASHI Sadayuki * @@ -15,8 +15,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MSGPACK_PACK_INLINE_IMPL_H__ -#define MSGPACK_PACK_INLINE_IMPL_H__ #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) #if __BYTE_ORDER == __LITTLE_ENDIAN @@ -52,17 +50,26 @@ #endif + #ifndef msgpack_pack_inline_func -#define msgpack_pack_inline_func(name) \ - inline void msgpack_pack_##name +#error msgpack_pack_inline_func template is not defined #endif +#ifndef msgpack_pack_user +#error msgpack_pack_user type is not defined +#endif + +#ifndef msgpack_pack_append_buffer +#error msgpack_pack_append_buffer callback is not defined +#endif + + /* * Integer */ // wrapper -msgpack_pack_inline_func(int)(msgpack_pack_context x, int d) +msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) { if(d < -32) { if(d < -32768) { // signed 32 @@ -95,7 +102,7 @@ msgpack_pack_inline_func(int)(msgpack_pack_context x, int d) } // wrapper -msgpack_pack_inline_func(unsigned_int)(msgpack_pack_context x, unsigned int d) +msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d) { if(d < 128) { // fixnum @@ -115,7 +122,7 @@ msgpack_pack_inline_func(unsigned_int)(msgpack_pack_context x, unsigned int d) } } -msgpack_pack_inline_func(unsigned_int_8)(msgpack_pack_context x, uint8_t d) +msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d) { if(d < 128) { msgpack_pack_append_buffer(x, &d, 1); @@ -125,26 +132,26 @@ msgpack_pack_inline_func(unsigned_int_8)(msgpack_pack_context x, uint8_t d) } } -msgpack_pack_inline_func(unsigned_int_16)(msgpack_pack_context x, uint16_t d) +msgpack_pack_inline_func(uint16)(msgpack_pack_user x, uint16_t d) { const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); } -msgpack_pack_inline_func(unsigned_int_32)(msgpack_pack_context x, uint32_t d) +msgpack_pack_inline_func(uint32)(msgpack_pack_user x, uint32_t d) { const unsigned char buf[5] = {0xce, STORE_BE32(d)}; msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func(unsigned_int_64)(msgpack_pack_context x, uint64_t d) +msgpack_pack_inline_func(uint64)(msgpack_pack_user x, uint64_t d) { // FIXME optimization const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; msgpack_pack_append_buffer(x, buf, 9); } -msgpack_pack_inline_func(signed_int_8)(msgpack_pack_context x, int8_t d) +msgpack_pack_inline_func(int8)(msgpack_pack_user x, int8_t d) { if(d > 0) { msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); @@ -156,19 +163,19 @@ msgpack_pack_inline_func(signed_int_8)(msgpack_pack_context x, int8_t d) } } -msgpack_pack_inline_func(signed_int_16)(msgpack_pack_context x, int16_t d) +msgpack_pack_inline_func(int16)(msgpack_pack_user x, int16_t d) { const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); } -msgpack_pack_inline_func(signed_int_32)(msgpack_pack_context x, int32_t d) +msgpack_pack_inline_func(int32)(msgpack_pack_user x, int32_t d) { const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func(signed_int_64)(msgpack_pack_context x, int64_t d) +msgpack_pack_inline_func(int64)(msgpack_pack_user x, int64_t d) { // FIXME optimization const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; @@ -180,14 +187,14 @@ msgpack_pack_inline_func(signed_int_64)(msgpack_pack_context x, int64_t d) * Float */ -msgpack_pack_inline_func(float)(msgpack_pack_context x, float d) +msgpack_pack_inline_func(float)(msgpack_pack_user x, float d) { uint32_t n = *((uint32_t*)&d); // FIXME const unsigned char buf[5] = {0xca, STORE_BE32(n)}; msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func(double)(msgpack_pack_context x, double d) +msgpack_pack_inline_func(double)(msgpack_pack_user x, double d) { uint64_t n = *((uint64_t*)&d); // FIXME const unsigned char buf[9] = {0xcb, STORE_BE64(n)}; @@ -199,7 +206,7 @@ msgpack_pack_inline_func(double)(msgpack_pack_context x, double d) * Nil */ -msgpack_pack_inline_func(nil)(msgpack_pack_context x) +msgpack_pack_inline_func(nil)(msgpack_pack_user x) { static const unsigned char d = 0xc0; msgpack_pack_append_buffer(x, &d, 1); @@ -210,13 +217,13 @@ msgpack_pack_inline_func(nil)(msgpack_pack_context x) * Boolean */ -msgpack_pack_inline_func(true)(msgpack_pack_context x) +msgpack_pack_inline_func(true)(msgpack_pack_user x) { static const unsigned char d = 0xc3; msgpack_pack_append_buffer(x, &d, 1); } -msgpack_pack_inline_func(false)(msgpack_pack_context x) +msgpack_pack_inline_func(false)(msgpack_pack_user x) { static const unsigned char d = 0xc2; msgpack_pack_append_buffer(x, &d, 1); @@ -227,7 +234,7 @@ msgpack_pack_inline_func(false)(msgpack_pack_context x) * Array */ -msgpack_pack_inline_func(array)(msgpack_pack_context x, unsigned int n) +msgpack_pack_inline_func(array)(msgpack_pack_user x, unsigned int n) { if(n < 16) { unsigned char d = 0x90 | n; @@ -248,7 +255,7 @@ msgpack_pack_inline_func(array)(msgpack_pack_context x, unsigned int n) * Map */ -msgpack_pack_inline_func(map)(msgpack_pack_context x, unsigned int n) +msgpack_pack_inline_func(map)(msgpack_pack_user x, unsigned int n) { if(n < 16) { unsigned char d = 0x80 | n; @@ -269,13 +276,7 @@ msgpack_pack_inline_func(map)(msgpack_pack_context x, unsigned int n) * Raw */ -msgpack_pack_inline_func(string)(msgpack_pack_context x, const char* b) -{ - uint32_t l = strlen(b); - msgpack_pack_append_buffer(x, (const unsigned char*)b, l+1); -} - -msgpack_pack_inline_func(raw)(msgpack_pack_context x, const void* b, size_t l) +msgpack_pack_inline_func(raw)(msgpack_pack_user x, const void* b, size_t l) { if(l < 32) { unsigned char d = 0xa0 | l; @@ -294,10 +295,10 @@ msgpack_pack_inline_func(raw)(msgpack_pack_context x, const void* b, size_t l) #undef msgpack_pack_inline_func +#undef msgpack_pack_user +#undef msgpack_pack_append_buffer #undef STORE_BE16 #undef STORE_BE32 #undef STORE_BE64 -#endif /* msgpack/pack/inline_impl.h */ - diff --git a/msgpack/unpack/callback.h b/msgpack/unpack/callback.h deleted file mode 100644 index 315bb9e6..00000000 --- a/msgpack/unpack/callback.h +++ /dev/null @@ -1,24 +0,0 @@ - -msgpack_object msgpack_unpack_init(msgpack_unpack_context* x); -msgpack_object msgpack_unpack_unsigned_int_8(msgpack_unpack_context* x, uint8_t d); -msgpack_object msgpack_unpack_unsigned_int_16(msgpack_unpack_context* x, uint16_t d); -msgpack_object msgpack_unpack_unsigned_int_32(msgpack_unpack_context* x, uint32_t d); -msgpack_object msgpack_unpack_unsigned_int_64(msgpack_unpack_context* x, uint64_t d); -msgpack_object msgpack_unpack_signed_int_8(msgpack_unpack_context* x, int8_t d); -msgpack_object msgpack_unpack_signed_int_16(msgpack_unpack_context* x, int16_t d); -msgpack_object msgpack_unpack_signed_int_32(msgpack_unpack_context* x, int32_t d); -msgpack_object msgpack_unpack_signed_int_64(msgpack_unpack_context* x, int64_t d); -msgpack_object msgpack_unpack_float(msgpack_unpack_context* x, float d); -msgpack_object msgpack_unpack_double(msgpack_unpack_context* x, double d); -msgpack_object msgpack_unpack_big_int(msgpack_unpack_context* x, const void* b, unsigned int l); -msgpack_object msgpack_unpack_big_float(msgpack_unpack_context* x, const void* b, unsigned int l); -msgpack_object msgpack_unpack_nil(msgpack_unpack_context* x); -msgpack_object msgpack_unpack_true(msgpack_unpack_context* x); -msgpack_object msgpack_unpack_false(msgpack_unpack_context* x); -msgpack_object msgpack_unpack_array_start(msgpack_unpack_context* x, unsigned int n); - void msgpack_unpack_array_item(msgpack_unpack_context* x, msgpack_object c, msgpack_object o); -msgpack_object msgpack_unpack_map_start(msgpack_unpack_context* x, unsigned int n); - void msgpack_unpack_map_item(msgpack_unpack_context* x, msgpack_object c, msgpack_object k, msgpack_object v); -msgpack_object msgpack_unpack_string(msgpack_unpack_context* x, const void* b, size_t l); -msgpack_object msgpack_unpack_raw(msgpack_unpack_context* x, const char* b, const char* p, unsigned int l); - diff --git a/msgpack/unpack/inline_context.h b/msgpack/unpack/inline_context.h deleted file mode 100644 index e764c092..00000000 --- a/msgpack/unpack/inline_context.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * MessagePack unpacking routine - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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. - */ -#ifndef MSGPACK_UNPACK_INLINE_CONTEXT_H__ -#define MSGPACK_UNPACK_INLINE_CONTEXT_H__ - -#include -#include - -#ifndef MSG_STACK_SIZE -#define MSG_STACK_SIZE 16 -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - msgpack_object obj; - size_t count; - unsigned int ct; - union { - /*const unsigned char* terminal_trail_start;*/ - msgpack_object map_key; - } tmp; -} msgpack_unpacker_stack; - -typedef struct { - msgpack_unpack_context user; // must be first - unsigned int cs; - unsigned int trail; - unsigned int top; - msgpack_unpacker_stack stack[MSG_STACK_SIZE]; -} msgpack_unpacker; - -void msgpack_unpacker_init(msgpack_unpacker* ctx); -int msgpack_unpacker_execute(msgpack_unpacker* ctx, const char* data, size_t len, size_t* off); -#define msgpack_unpacker_data(unpacker) (unpacker)->stack[0].obj - -#ifdef __cplusplus -} -#endif - -#endif /* msgpack/unpack/inline_context.h */ - diff --git a/msgpack/unpack/inline_impl.h b/msgpack/unpack/inline_impl.h deleted file mode 100644 index a6557f9a..00000000 --- a/msgpack/unpack/inline_impl.h +++ /dev/null @@ -1,465 +0,0 @@ -/* - * MessagePack unpacking routine - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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. - */ -#ifndef MSGPACK_UNPACK_INLINE_IMPL_H__ -#define MSGPACK_UNPACK_INLINE_IMPL_H__ - -#include -#include -#include -/*#include */ - -#ifdef __cplusplus -extern "C" { -#endif - -// Positive FixNum 0xxxxxxx 0x00 - 0x7f -// Negative FixNum 111xxxxx 0xe0 - 0xff -// Variable 110xxxxx 0xc0 - 0xdf -// nil 00000 0xc0 -// string 00001 0xc1 -// false 00010 0xc2 -// true 00011 0xc3 -// (?) 00100 0xc4 -// (?) 00101 0xc5 -// (?) 00110 0xc6 -// (?) 00111 0xc7 -// (?) 01000 0xc8 -// (?) 01001 0xc9 -// float 01010 0xca -// double 01011 0xcb -// uint 8 01100 0xcc -// uint 16 01101 0xcd -// uint 32 01110 0xce -// uint 64 01111 0xcf -// int 8 10000 0xd0 -// int 16 10001 0xd1 -// int 32 10010 0xd2 -// int 64 10011 0xd3 -// (?) 10100 0xd4 -// (?) 10101 0xd5 -// (big float 16) 10110 0xd6 -// (big float 32) 10111 0xd7 -// (big integer 16) 11000 0xd8 -// (big integer 32) 11001 0xd9 -// raw 16 11010 0xda -// raw 32 11011 0xdb -// array 16 11100 0xdc -// array 32 11101 0xdd -// map 16 11110 0xde -// map 32 11111 0xdf -// FixRaw 101xxxxx 0xa0 - 0xbf -// FixArray 1001xxxx 0x90 - 0x9f -// FixMap 1000xxxx 0x80 - 0x8f - - -#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define __LITTLE_ENDIAN__ -#elif __BYTE_ORDER == __BIG_ENDIAN -#define __BIG_ENDIAN__ -#endif -#endif - -#define betoh16(x) ntohs(x) -#define betoh32(x) ntohl(x) - -#ifdef __LITTLE_ENDIAN__ -#if defined(__bswap_64) -# define betoh64(x) __bswap_64(x) -#elif defined(__DARWIN_OSSwapInt64) -# define betoh64(x) __DARWIN_OSSwapInt64(x) -#else -static inline uint64_t betoh64(uint64_t x) { - return ((x << 56) & 0xff00000000000000ULL ) | - ((x << 40) & 0x00ff000000000000ULL ) | - ((x << 24) & 0x0000ff0000000000ULL ) | - ((x << 8) & 0x000000ff00000000ULL ) | - ((x >> 8) & 0x00000000ff000000ULL ) | - ((x >> 24) & 0x0000000000ff0000ULL ) | - ((x >> 40) & 0x000000000000ff00ULL ) | - ((x >> 56) & 0x00000000000000ffULL ) ; -} -#endif -#else -#define betoh64(x) (x) -#endif - - -typedef enum { - CS_HEADER = 0x00, // nil - - //CS_STRING = 0x01, - //CS_ = 0x02, // false - //CS_ = 0x03, // true - - //CS_ = 0x04, - //CS_ = 0x05, - //CS_ = 0x06, - //CS_ = 0x07, - - //CS_ = 0x08, - //CS_ = 0x09, - CS_FLOAT = 0x0a, - CS_DOUBLE = 0x0b, - CS_UNSIGNED_INT_8 = 0x0c, - CS_UNSIGNED_INT_16 = 0x0d, - CS_UNSIGNED_INT_32 = 0x0e, - CS_UNSIGNED_INT_64 = 0x0f, - CS_SIGNED_INT_8 = 0x10, - CS_SIGNED_INT_16 = 0x11, - CS_SIGNED_INT_32 = 0x12, - CS_SIGNED_INT_64 = 0x13, - - //CS_ = 0x14, - //CS_ = 0x15, - //CS_BIG_INT_16 = 0x16, - //CS_BIG_INT_32 = 0x17, - //CS_BIG_FLOAT_16 = 0x18, - //CS_BIG_FLOAT_32 = 0x19, - CS_RAW_16 = 0x1a, - CS_RAW_32 = 0x1b, - CS_ARRAY_16 = 0x1c, - CS_ARRAY_32 = 0x1d, - CS_MAP_16 = 0x1e, - CS_MAP_32 = 0x1f, - - //ACS_BIG_INT_VALUE, - //ACS_BIG_FLOAT_VALUE, - ACS_RAW_VALUE, -} current_state_t; - - -typedef enum { - CT_ARRAY_ITEM, - CT_MAP_KEY, - CT_MAP_VALUE, -} container_type_t; - - -void msgpack_unpacker_init(msgpack_unpacker* ctx) -{ - memset(ctx, 0, sizeof(msgpack_unpacker)); // FIXME init ctx->user? - ctx->cs = CS_HEADER; - ctx->trail = 0; - ctx->top = 0; - ctx->stack[0].obj = msgpack_unpack_init(&ctx->user); -} - -int msgpack_unpacker_execute(msgpack_unpacker* 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; - - unsigned int trail = ctx->trail; - unsigned int cs = ctx->cs; - unsigned int top = ctx->top; - msgpack_unpacker_stack* stack = ctx->stack; - msgpack_unpack_context* user = &ctx->user; - - msgpack_object obj; - msgpack_unpacker_stack* c = NULL; - - int ret; - -#define push_simple_value(func) \ - obj = func(user); \ - /*printf("obj %d\n",obj);*/ \ - goto _push -#define push_fixed_value(func, arg) \ - obj = func(user, arg); \ - /*printf("obj %d\n",obj);*/ \ - goto _push -#define push_variable_value(func, base, pos, len) \ - obj = func(user, (const char*)base, (const char*)pos, len); \ - /*printf("obj %d\n",obj);*/ \ - goto _push - -/* -#define again_terminal_trail(_cs, from) \ - cs = _cs; \ - stack[top].tmp.terminal_trail_start = from; \ - goto _terminal_trail_again -*/ -#define again_fixed_trail(_cs, trail_len) \ - trail = trail_len; \ - cs = _cs; \ - goto _fixed_trail_again -#define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \ - trail = trail_len; \ - if(trail == 0) { goto ifzero; } \ - cs = _cs; \ - goto _fixed_trail_again - -#define start_container(func, count_, ct_) \ - stack[top].obj = func(user, count_); \ - if((count_) == 0) { obj = stack[top].obj; goto _push; } \ - if(top >= MSG_STACK_SIZE) { goto _failed; } \ - stack[top].ct = ct_; \ - stack[top].count = count_; \ - /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \ - /*printf("stack push %d\n", top);*/ \ - ++top; \ - goto _header_again - -#define NEXT_CS(p) \ - ((unsigned int)*p & 0x1f) - -#define PTR_CAST_8(ptr) (*(uint8_t*)ptr) -#define PTR_CAST_16(ptr) betoh16(*(uint16_t*)ptr) -#define PTR_CAST_32(ptr) betoh32(*(uint32_t*)ptr) -#define PTR_CAST_64(ptr) betoh64(*(uint64_t*)ptr) - - if(p == pe) { goto _out; } - do { - switch(cs) { - case CS_HEADER: - switch(*p) { - case 0x00 ... 0x7f: // Positive Fixnum - push_fixed_value(msgpack_unpack_unsigned_int_8, *(uint8_t*)p); - case 0xe0 ... 0xff: // Negative Fixnum - push_fixed_value(msgpack_unpack_signed_int_8, *(int8_t*)p); - case 0xc0 ... 0xdf: // Variable - switch(*p) { - case 0xc0: // nil - push_simple_value(msgpack_unpack_nil); - //case 0xc1: // string - // again_terminal_trail(NEXT_CS(p), p+1); - case 0xc2: // false - push_simple_value(msgpack_unpack_false); - case 0xc3: // true - push_simple_value(msgpack_unpack_true); - //case 0xc4: - //case 0xc5: - //case 0xc6: - //case 0xc7: - //case 0xc8: - //case 0xc9: - case 0xca: // float - case 0xcb: // double - case 0xcc: // unsigned int 8 - case 0xcd: // unsigned int 16 - case 0xce: // unsigned int 32 - case 0xcf: // unsigned int 64 - case 0xd0: // signed int 8 - case 0xd1: // signed int 16 - case 0xd2: // signed int 32 - case 0xd3: // signed int 64 - again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03)); - //case 0xd4: - //case 0xd5: - //case 0xd6: // big integer 16 - //case 0xd7: // big integer 32 - //case 0xd8: // big float 16 - //case 0xd9: // big float 32 - case 0xda: // raw 16 - case 0xdb: // raw 32 - case 0xdc: // array 16 - case 0xdd: // array 32 - case 0xde: // map 16 - case 0xdf: // map 32 - again_fixed_trail(NEXT_CS(p), 2 << (((unsigned int)*p) & 0x01)); - default: - goto _failed; - } - case 0xa0 ... 0xbf: // FixRaw - again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero); - case 0x90 ... 0x9f: // FixArray - start_container(msgpack_unpack_array_start, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM); - case 0x80 ... 0x8f: // FixMap - start_container(msgpack_unpack_map_start, ((unsigned int)*p) & 0x0f, CT_MAP_KEY); - - default: - goto _failed; - } - // end CS_HEADER - - - //_terminal_trail_again: - // ++p; - - //case CS_STRING: - // if(*p == 0) { - // const unsigned char* start = stack[top].tmp.terminal_trail_start; - // obj = msgpack_unpack_string(user, start, p-start); - // goto _push; - // } - // goto _terminal_trail_again; - - - _fixed_trail_again: - ++p; - - default: - if((size_t)(pe - p) < trail) { goto _out; } - n = p; p += trail - 1; - switch(cs) { - //case CS_ - //case CS_ - case CS_FLOAT: { - uint32_t x = PTR_CAST_32(n); // FIXME - push_fixed_value(msgpack_unpack_float, *((float*)&x)); } - case CS_DOUBLE: { - uint64_t x = PTR_CAST_64(n); // FIXME - push_fixed_value(msgpack_unpack_double, *((double*)&x)); } - case CS_UNSIGNED_INT_8: - push_fixed_value(msgpack_unpack_unsigned_int_8, (uint8_t)PTR_CAST_8(n)); - case CS_UNSIGNED_INT_16: - push_fixed_value(msgpack_unpack_unsigned_int_16, (uint16_t)PTR_CAST_16(n)); - case CS_UNSIGNED_INT_32: - push_fixed_value(msgpack_unpack_unsigned_int_32, (uint32_t)PTR_CAST_32(n)); - case CS_UNSIGNED_INT_64: - push_fixed_value(msgpack_unpack_unsigned_int_64, (uint64_t)PTR_CAST_64(n)); - - case CS_SIGNED_INT_8: - push_fixed_value(msgpack_unpack_signed_int_8, (int8_t)PTR_CAST_8(n)); - case CS_SIGNED_INT_16: - push_fixed_value(msgpack_unpack_signed_int_16, (int16_t)PTR_CAST_16(n)); - case CS_SIGNED_INT_32: - push_fixed_value(msgpack_unpack_signed_int_32, (int32_t)PTR_CAST_32(n)); - case CS_SIGNED_INT_64: - push_fixed_value(msgpack_unpack_signed_int_64, (int64_t)PTR_CAST_64(n)); - - //case CS_ - //case CS_ - //case CS_BIG_INT_16: - // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint16_t)PTR_CAST_16(n), _big_int_zero); - //case CS_BIG_INT_32: - // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint32_t)PTR_CAST_32(n), _big_int_zero); - //case ACS_BIG_INT_VALUE: - //_big_int_zero: - // // FIXME - // push_variable_value(msgpack_unpack_big_int, data, n, trail); - - //case CS_BIG_FLOAT_16: - // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint16_t)PTR_CAST_16(n), _big_float_zero); - //case CS_BIG_FLOAT_32: - // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint32_t)PTR_CAST_32(n), _big_float_zero); - //case ACS_BIG_FLOAT_VALUE: - //_big_float_zero: - // // FIXME - // push_variable_value(msgpack_unpack_big_float, data, n, trail); - - case CS_RAW_16: - again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint16_t)PTR_CAST_16(n), _raw_zero); - case CS_RAW_32: - again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint32_t)PTR_CAST_32(n), _raw_zero); - case ACS_RAW_VALUE: - _raw_zero: - push_variable_value(msgpack_unpack_raw, data, n, trail); - - case CS_ARRAY_16: - start_container(msgpack_unpack_array_start, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM); - case CS_ARRAY_32: - start_container(msgpack_unpack_array_start, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM); - - case CS_MAP_16: - start_container(msgpack_unpack_map_start, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY); - case CS_MAP_32: - start_container(msgpack_unpack_map_start, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY); - - default: - goto _failed; - } - } - -_push: - if(top == 0) { goto _finish; } - c = &stack[top-1]; - switch(c->ct) { - case CT_ARRAY_ITEM: - msgpack_unpack_array_item(user, c->obj, obj); - if(--c->count == 0) { - obj = c->obj; - --top; - /*printf("stack pop %d\n", top);*/ - goto _push; - } - goto _header_again; - case CT_MAP_KEY: - c->tmp.map_key = obj; - c->ct = CT_MAP_VALUE; - goto _header_again; - case CT_MAP_VALUE: - msgpack_unpack_map_item(user, c->obj, c->tmp.map_key, obj); - if(--c->count == 0) { - obj = c->obj; - --top; - /*printf("stack pop %d\n", top);*/ - goto _push; - } - c->ct = CT_MAP_KEY; - goto _header_again; - - default: - goto _failed; - } - -_header_again: - cs = CS_HEADER; - ++p; - } while(p != pe); - goto _out; - - -_finish: - stack[0].obj = obj; - ++p; - ret = 1; - /*printf("-- finish --\n"); */ - goto _end; - -_failed: - /*printf("** FAILED **\n"); */ - ret = -1; - goto _end; - -_out: - ret = 0; - goto _end; - -_end: - ctx->cs = cs; - ctx->trail = trail; - ctx->top = top; - *off = p - (const unsigned char*)data; - - return ret; -} - - -#ifdef betoh16 -#undef betoh16 -#endif - -#ifdef betoh32 -#undef betoh32 -#endif - -#ifdef betoh64 -#undef betoh64 -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* msgpack/unpack/inline_impl.h */ - diff --git a/msgpack/unpack_define.h b/msgpack/unpack_define.h new file mode 100644 index 00000000..1d9db19f --- /dev/null +++ b/msgpack/unpack_define.h @@ -0,0 +1,127 @@ +/* + * MessagePack unpacking routine template + * + * Copyright (C) 2008 FURUHASHI Sadayuki + * + * Licensed 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. + */ +#ifndef MSGPACK_UNPACK_DEFINE_H__ +#define MSGPACK_UNPACK_DEFINE_H__ + +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifndef MSGPACK_MAX_STACK_SIZE +#define MSGPACK_MAX_STACK_SIZE 16 +#endif + + +#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define __LITTLE_ENDIAN__ +#elif __BYTE_ORDER == __BIG_ENDIAN +#define __BIG_ENDIAN__ +#endif +#endif + +#define msgpack_betoh16(x) ntohs(x) +#define msgpack_betoh32(x) ntohl(x) + +#ifdef __LITTLE_ENDIAN__ +#if defined(__bswap_64) +# define msgpack_betoh64(x) __bswap_64(x) +#elif defined(__DARWIN_OSSwapInt64) +# define msgpack_betoh64(x) __DARWIN_OSSwapInt64(x) +#else +static inline uint64_t msgpack_betoh64(uint64_t x) { + return ((x << 56) & 0xff00000000000000ULL ) | + ((x << 40) & 0x00ff000000000000ULL ) | + ((x << 24) & 0x0000ff0000000000ULL ) | + ((x << 8) & 0x000000ff00000000ULL ) | + ((x >> 8) & 0x00000000ff000000ULL ) | + ((x >> 24) & 0x0000000000ff0000ULL ) | + ((x >> 40) & 0x000000000000ff00ULL ) | + ((x >> 56) & 0x00000000000000ffULL ) ; +} +#endif +#else +#define msgpack_betoh64(x) (x) +#endif + + +typedef enum { + CS_HEADER = 0x00, // nil + + //CS_ = 0x01, + //CS_ = 0x02, // false + //CS_ = 0x03, // true + + //CS_ = 0x04, + //CS_ = 0x05, + //CS_ = 0x06, + //CS_ = 0x07, + + //CS_ = 0x08, + //CS_ = 0x09, + CS_FLOAT = 0x0a, + CS_DOUBLE = 0x0b, + CS_UINT_8 = 0x0c, + CS_UINT_16 = 0x0d, + CS_UINT_32 = 0x0e, + CS_UINT_64 = 0x0f, + CS_INT_8 = 0x10, + CS_INT_16 = 0x11, + CS_INT_32 = 0x12, + CS_INT_64 = 0x13, + + //CS_ = 0x14, + //CS_ = 0x15, + //CS_BIG_INT_16 = 0x16, + //CS_BIG_INT_32 = 0x17, + //CS_BIG_FLOAT_16 = 0x18, + //CS_BIG_FLOAT_32 = 0x19, + CS_RAW_16 = 0x1a, + CS_RAW_32 = 0x1b, + CS_ARRAY_16 = 0x1c, + CS_ARRAY_32 = 0x1d, + CS_MAP_16 = 0x1e, + CS_MAP_32 = 0x1f, + + //ACS_BIG_INT_VALUE, + //ACS_BIG_FLOAT_VALUE, + ACS_RAW_VALUE, +} msgpack_unpack_state; + + +typedef enum { + CT_ARRAY_ITEM, + CT_MAP_KEY, + CT_MAP_VALUE, +} msgpack_container_type; + + +#ifdef __cplusplus +} +#endif + +#endif /* msgpack/unpack_define.h */ + diff --git a/msgpack/unpack_template.h b/msgpack/unpack_template.h new file mode 100644 index 00000000..8197fa41 --- /dev/null +++ b/msgpack/unpack_template.h @@ -0,0 +1,360 @@ +/* + * MessagePack unpacking routine template + * + * Copyright (C) 2008 FURUHASHI Sadayuki + * + * Licensed 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. + */ + +#ifndef msgpack_unpack_func +#error msgpack_unpack_func template is not defined +#endif + +#ifndef msgpack_unpack_callback +#error msgpack_unpack_callback template is not defined +#endif + +#ifndef msgpack_unpack_struct +#error msgpack_unpack_struct template is not defined +#endif + +#ifndef msgpack_unpack_struct_decl +#define msgpack_unpack_struct_decl(name) msgpack_unpack_struct(name) +#endif + +#ifndef msgpack_unpack_object +#error msgpack_unpack_object type is not defined +#endif + +#ifndef msgpack_unpack_user +#error msgpack_unpack_user type is not defined +#endif + + +msgpack_unpack_struct_decl(stack) { + msgpack_unpack_object obj; + size_t count; + unsigned int ct; + msgpack_unpack_object map_key; +}; + +msgpack_unpack_struct_decl(context) { + msgpack_unpack_user user; // must be first + unsigned int cs; + unsigned int trail; + unsigned int top; + msgpack_unpack_struct(stack) stack[MSGPACK_MAX_STACK_SIZE]; +}; + + +msgpack_unpack_func(void, init)(msgpack_unpack_struct(context)* ctx) +{ + /*memset(ctx, 0, sizeof( msgpack_unpack_struct(context) )); FIXME needed? */ + ctx->cs = CS_HEADER; + ctx->trail = 0; + ctx->top = 0; + ctx->stack[0].obj = msgpack_unpack_callback(init)(&ctx->user); +} + +msgpack_unpack_func(msgpack_unpack_object, data)(msgpack_unpack_struct(context)* unpacker) +{ + return (unpacker)->stack[0].obj; +} + + +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; + + unsigned int trail = ctx->trail; + unsigned int cs = ctx->cs; + unsigned int top = ctx->top; + msgpack_unpack_struct(stack)* stack = ctx->stack; + msgpack_unpack_user* user = &ctx->user; + + msgpack_unpack_object obj; + msgpack_unpack_struct(stack)* c = NULL; + + int ret; + +#define push_simple_value(func) \ + obj = msgpack_unpack_callback(func)(user); \ + /*printf("obj %d\n",obj);*/ \ + goto _push +#define push_fixed_value(func, arg) \ + obj = msgpack_unpack_callback(func)(user, arg); \ + /*printf("obj %d\n",obj);*/ \ + goto _push +#define push_variable_value(func, base, pos, len) \ + obj = msgpack_unpack_callback(func)(user, (const char*)base, (const char*)pos, len); \ + /*printf("obj %d\n",obj);*/ \ + goto _push + +#define again_fixed_trail(_cs, trail_len) \ + trail = trail_len; \ + cs = _cs; \ + goto _fixed_trail_again +#define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \ + trail = trail_len; \ + if(trail == 0) { goto ifzero; } \ + cs = _cs; \ + goto _fixed_trail_again + +#define start_container(func, count_, ct_) \ + stack[top].obj = msgpack_unpack_callback(func)(user, count_); \ + if((count_) == 0) { obj = stack[top].obj; goto _push; } \ + if(top >= MSGPACK_MAX_STACK_SIZE) { goto _failed; } \ + stack[top].ct = ct_; \ + stack[top].count = count_; \ + /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \ + /*printf("stack push %d\n", top);*/ \ + ++top; \ + goto _header_again + +#define NEXT_CS(p) \ + ((unsigned int)*p & 0x1f) + +#define PTR_CAST_8(ptr) (*(uint8_t*)ptr) +#define PTR_CAST_16(ptr) msgpack_betoh16(*(uint16_t*)ptr) +#define PTR_CAST_32(ptr) msgpack_betoh32(*(uint32_t*)ptr) +#define PTR_CAST_64(ptr) msgpack_betoh64(*(uint64_t*)ptr) + + if(p == pe) { goto _out; } + do { + switch(cs) { + case CS_HEADER: + switch(*p) { + case 0x00 ... 0x7f: // Positive Fixnum + push_fixed_value(uint8, *(uint8_t*)p); + case 0xe0 ... 0xff: // Negative Fixnum + push_fixed_value(int8, *(int8_t*)p); + case 0xc0 ... 0xdf: // Variable + switch(*p) { + case 0xc0: // nil + push_simple_value(nil); + //case 0xc1: // string + // again_terminal_trail(NEXT_CS(p), p+1); + case 0xc2: // false + push_simple_value(false); + case 0xc3: // true + push_simple_value(true); + //case 0xc4: + //case 0xc5: + //case 0xc6: + //case 0xc7: + //case 0xc8: + //case 0xc9: + case 0xca: // float + case 0xcb: // double + case 0xcc: // unsigned int 8 + case 0xcd: // unsigned int 16 + case 0xce: // unsigned int 32 + case 0xcf: // unsigned int 64 + case 0xd0: // signed int 8 + case 0xd1: // signed int 16 + case 0xd2: // signed int 32 + case 0xd3: // signed int 64 + again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03)); + //case 0xd4: + //case 0xd5: + //case 0xd6: // big integer 16 + //case 0xd7: // big integer 32 + //case 0xd8: // big float 16 + //case 0xd9: // big float 32 + case 0xda: // raw 16 + case 0xdb: // raw 32 + case 0xdc: // array 16 + case 0xdd: // array 32 + case 0xde: // map 16 + case 0xdf: // map 32 + again_fixed_trail(NEXT_CS(p), 2 << (((unsigned int)*p) & 0x01)); + default: + goto _failed; + } + case 0xa0 ... 0xbf: // FixRaw + again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero); + case 0x90 ... 0x9f: // FixArray + start_container(array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM); + case 0x80 ... 0x8f: // FixMap + start_container(map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY); + + default: + goto _failed; + } + // end CS_HEADER + + + _fixed_trail_again: + ++p; + + default: + if((size_t)(pe - p) < trail) { goto _out; } + n = p; p += trail - 1; + switch(cs) { + //case CS_ + //case CS_ + case CS_FLOAT: { + uint32_t x = PTR_CAST_32(n); // FIXME + push_fixed_value(float, *((float*)&x)); } + case CS_DOUBLE: { + uint64_t x = PTR_CAST_64(n); // FIXME + push_fixed_value(double, *((double*)&x)); } + case CS_UINT_8: + push_fixed_value(uint8, (uint8_t)PTR_CAST_8(n)); + case CS_UINT_16: + push_fixed_value(uint16, (uint16_t)PTR_CAST_16(n)); + case CS_UINT_32: + push_fixed_value(uint32, (uint32_t)PTR_CAST_32(n)); + case CS_UINT_64: + push_fixed_value(uint64, (uint64_t)PTR_CAST_64(n)); + + case CS_INT_8: + push_fixed_value(int8, (int8_t)PTR_CAST_8(n)); + case CS_INT_16: + push_fixed_value(int16, (int16_t)PTR_CAST_16(n)); + case CS_INT_32: + push_fixed_value(int32, (int32_t)PTR_CAST_32(n)); + case CS_INT_64: + push_fixed_value(int64, (int64_t)PTR_CAST_64(n)); + + //case CS_ + //case CS_ + //case CS_BIG_INT_16: + // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint16_t)PTR_CAST_16(n), _big_int_zero); + //case CS_BIG_INT_32: + // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint32_t)PTR_CAST_32(n), _big_int_zero); + //case ACS_BIG_INT_VALUE: + //_big_int_zero: + // // FIXME + // push_variable_value(big_int, data, n, trail); + + //case CS_BIG_FLOAT_16: + // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint16_t)PTR_CAST_16(n), _big_float_zero); + //case CS_BIG_FLOAT_32: + // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint32_t)PTR_CAST_32(n), _big_float_zero); + //case ACS_BIG_FLOAT_VALUE: + //_big_float_zero: + // // FIXME + // push_variable_value(big_float, data, n, trail); + + case CS_RAW_16: + again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint16_t)PTR_CAST_16(n), _raw_zero); + case CS_RAW_32: + again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint32_t)PTR_CAST_32(n), _raw_zero); + case ACS_RAW_VALUE: + _raw_zero: + push_variable_value(raw, data, n, trail); + + case CS_ARRAY_16: + start_container(array, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM); + case CS_ARRAY_32: + start_container(array, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM); + + case CS_MAP_16: + start_container(map, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY); + case CS_MAP_32: + start_container(map, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY); + + default: + goto _failed; + } + } + +_push: + if(top == 0) { goto _finish; } + c = &stack[top-1]; + switch(c->ct) { + case CT_ARRAY_ITEM: + msgpack_unpack_callback(array_item)(user, c->obj, obj); + if(--c->count == 0) { + obj = c->obj; + --top; + /*printf("stack pop %d\n", top);*/ + goto _push; + } + goto _header_again; + case CT_MAP_KEY: + c->map_key = obj; + c->ct = CT_MAP_VALUE; + goto _header_again; + case CT_MAP_VALUE: + msgpack_unpack_callback(map_item)(user, c->obj, c->map_key, obj); + if(--c->count == 0) { + obj = c->obj; + --top; + /*printf("stack pop %d\n", top);*/ + goto _push; + } + c->ct = CT_MAP_KEY; + goto _header_again; + + default: + goto _failed; + } + +_header_again: + cs = CS_HEADER; + ++p; + } while(p != pe); + goto _out; + + +_finish: + stack[0].obj = obj; + ++p; + ret = 1; + /*printf("-- finish --\n"); */ + goto _end; + +_failed: + /*printf("** FAILED **\n"); */ + ret = -1; + goto _end; + +_out: + ret = 0; + goto _end; + +_end: + ctx->cs = cs; + ctx->trail = trail; + ctx->top = top; + *off = p - (const unsigned char*)data; + + return ret; +} + + +#undef msgpack_unpack_func +#undef msgpack_unpack_callback +#undef msgpack_unpack_struct +#undef msgpack_unpack_object +#undef msgpack_unpack_user + +#undef push_simple_value +#undef push_fixed_value +#undef push_variable_value +#undef again_fixed_trail +#undef again_fixed_trail_if_zero +#undef start_container + +#undef NEXT_CS +#undef PTR_CAST_8 +#undef PTR_CAST_16 +#undef PTR_CAST_32 +#undef PTR_CAST_64 + diff --git a/ruby/gem/Manifest.txt b/ruby/gem/Manifest.txt index 299a4e02..388135e8 100644 --- a/ruby/gem/Manifest.txt +++ b/ruby/gem/Manifest.txt @@ -7,15 +7,12 @@ config/requirements.rb ext/extconf.rb ext/pack.c ext/pack.h -ext/pack_inline.h ext/rbinit.c ext/unpack.c ext/unpack.h -ext/unpack_context.h -ext/unpack_inline.c -msgpack/pack/inline_impl.h -msgpack/unpack/inline_context.h -msgpack/unpack/inline_impl.h +msgpack/pack_template.h +msgpack/unpack_define.h +msgpack/unpack_template.h lib/msgpack/version.rb script/console script/destroy diff --git a/ruby/gem/lib/msgpack/version.rb b/ruby/gem/lib/msgpack/version.rb index c65972f4..b2a5db65 100644 --- a/ruby/gem/lib/msgpack/version.rb +++ b/ruby/gem/lib/msgpack/version.rb @@ -1,7 +1,7 @@ module MessagePack module VERSION #:nodoc: MAJOR = 0 - MINOR = 1 + MINOR = 2 TINY = 0 STRING = [MAJOR, MINOR, TINY].join('.') diff --git a/ruby/gengem.sh b/ruby/gengem.sh index 4f8623cc..c8abcc2a 100755 --- a/ruby/gengem.sh +++ b/ruby/gengem.sh @@ -3,16 +3,13 @@ cp extconf.rb gem/ext/ cp pack.c gem/ext/ cp pack.h gem/ext/ -cp pack_inline.h gem/ext/ cp rbinit.c gem/ext/ cp unpack.c gem/ext/ cp unpack.h gem/ext/ -cp unpack_context.h gem/ext/ -cp unpack_inline.c gem/ext/ cp ../README gem/README.txt -cp ../msgpack/pack/inline_impl.h gem/msgpack/pack/ -cp ../msgpack/unpack/inline_context.h gem/msgpack/unpack/ -cp ../msgpack/unpack/inline_impl.h gem/msgpack/unpack/ +cp ../msgpack/pack_template.h gem/msgpack/ +cp ../msgpack/unpack_define.h gem/msgpack/ +cp ../msgpack/unpack_template.h gem/msgpack/ cd gem && rake --trace package diff --git a/ruby/pack.c b/ruby/pack.c index 3d71776d..54f610ce 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -15,7 +15,41 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "pack_inline.h" +#include "ruby.h" +#include +#include + +#define msgpack_pack_inline_func(name) \ + static void msgpack_pack_##name + +#define msgpack_pack_user VALUE + +#define msgpack_pack_append_buffer(user, buf, len) \ + rb_str_buf_cat(user, (const void*)buf, len) + +/* +static void msgpack_pack_int(VALUE x, int d); +static void msgpack_pack_unsigned_int(VALUE x, unsigned int d); +static void msgpack_pack_uint8(VALUE x, uint8_t d); +static void msgpack_pack_uint16(VALUE x, uint16_t d); +static void msgpack_pack_uint32(VALUE x, uint32_t d); +static void msgpack_pack_uint64(VALUE x, uint64_t d); +static void msgpack_pack_int8(VALUE x, int8_t d); +static void msgpack_pack_int16(VALUE x, int16_t d); +static void msgpack_pack_int32(VALUE x, int32_t d); +static void msgpack_pack_int64(VALUE x, int64_t d); +static void msgpack_pack_float(VALUE x, float d); +static void msgpack_pack_double(VALUE x, double d); +static void msgpack_pack_nil(VALUE x); +static void msgpack_pack_true(VALUE x); +static void msgpack_pack_false(VALUE x); +static void msgpack_pack_array(VALUE x, unsigned int n); +static void msgpack_pack_map(VALUE x, unsigned int n); +static void msgpack_pack_raw(VALUE x, const void* b, size_t l); +*/ + +#include "msgpack/pack_template.h" + #ifndef RUBY_VM #include "st.h" // ruby hash @@ -72,9 +106,9 @@ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) ARG_BUFFER(out, argc, argv); // FIXME bignum if(RBIGNUM_SIGN(self)) { // positive - msgpack_pack_unsigned_int_64(out, rb_big2ull(self)); + msgpack_pack_uint64(out, rb_big2ull(self)); } else { // negative - msgpack_pack_signed_int_64(out, rb_big2ll(self)); + msgpack_pack_int64(out, rb_big2ll(self)); } return out; } diff --git a/ruby/pack_inline.h b/ruby/pack_inline.h deleted file mode 100644 index bda74c31..00000000 --- a/ruby/pack_inline.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * MessagePack packing routine for Ruby - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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. - */ -#ifndef PACK_INLINE_H__ -#define PACK_INLINE_H__ - -#include "ruby.h" - -typedef VALUE msgpack_pack_context; - -static inline void msgpack_pack_append_buffer(VALUE x, const unsigned char* b, unsigned int l) -{ - rb_str_buf_cat(x, (const void*)b, l); -} - -#include -#include /* __BYTE_ORDER */ -#include "msgpack/pack/inline_impl.h" - -#endif /* pack_inline.h */ - diff --git a/ruby/unpack.c b/ruby/unpack.c index fa2996d0..8439c028 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -16,8 +16,99 @@ * limitations under the License. */ #include "ruby.h" -#include "unpack_context.h" -#include +#include "msgpack/unpack_define.h" + + +typedef struct { + int finished; +} msgpack_unpack_context; + + +#define msgpack_unpack_struct(name) \ + struct msgpack_unpacker_##name + +#define msgpack_unpack_func(ret, name) \ + ret msgpack_unpacker_##name + +#define msgpack_unpack_callback(name) \ + template_callback_##name + +#define msgpack_unpack_object VALUE + +#define msgpack_unpack_user msgpack_unpack_context + + +struct msgpack_unpacker_context; +typedef struct msgpack_unpacker_context msgpack_unpacker; + +static void msgpack_unpacker_init(msgpack_unpacker* ctx); + +static VALUE msgpack_unpacker_data(msgpack_unpacker* ctx); + +static int msgpack_unpacker_execute(msgpack_unpacker* ctx, + const char* data, size_t len, size_t* off); + + +static inline VALUE template_callback_init(msgpack_unpack_context* x) +{ return Qnil; } + +static inline VALUE template_callback_uint8(msgpack_unpack_context* x, uint8_t d) +{ return INT2FIX(d); } + +static inline VALUE template_callback_uint16(msgpack_unpack_context* x, uint16_t d) +{ return INT2FIX(d); } + +static inline VALUE template_callback_uint32(msgpack_unpack_context* x, uint32_t d) +{ return UINT2NUM(d); } + +static inline VALUE template_callback_uint64(msgpack_unpack_context* x, uint64_t d) +{ return rb_ull2inum(d); } + +static inline VALUE template_callback_int8(msgpack_unpack_context* x, int8_t d) +{ return INT2FIX((long)d); } + +static inline VALUE template_callback_int16(msgpack_unpack_context* x, int16_t d) +{ return INT2FIX((long)d); } + +static inline VALUE template_callback_int32(msgpack_unpack_context* x, int32_t d) +{ return INT2NUM((long)d); } + +static inline VALUE template_callback_int64(msgpack_unpack_context* x, int64_t d) +{ return rb_ll2inum(d); } + +static inline VALUE template_callback_float(msgpack_unpack_context* x, float d) +{ return rb_float_new(d); } + +static inline VALUE template_callback_double(msgpack_unpack_context* x, double d) +{ return rb_float_new(d); } + +static inline VALUE template_callback_nil(msgpack_unpack_context* x) +{ return Qnil; } + +static inline VALUE template_callback_true(msgpack_unpack_context* x) +{ return Qtrue; } + +static inline VALUE template_callback_false(msgpack_unpack_context* x) +{ return Qfalse; } + +static inline VALUE template_callback_array(msgpack_unpack_context* x, unsigned int n) +{ return rb_ary_new2(n); } + +static inline void template_callback_array_item(msgpack_unpack_context* x, VALUE c, VALUE o) +{ rb_ary_push(c, o); } // FIXME set value directry RARRAY_PTR(obj)[RARRAY_LEN(obj)++] + +static inline VALUE template_callback_map(msgpack_unpack_context* x, unsigned int n) +{ return rb_hash_new(); } + +static inline void template_callback_map_item(msgpack_unpack_context* x, VALUE c, VALUE k, VALUE v) +{ rb_hash_aset(c, k, v); } + +static inline VALUE template_callback_raw(msgpack_unpack_context* x, const char* b, const char* p, unsigned int l) +{ return rb_str_new(p, l); } + + +#include "msgpack/unpack_template.h" + #define UNPACKER(from, name) \ msgpack_unpacker *name = NULL; \ @@ -45,7 +136,7 @@ static void MessagePack_Unpacker_mark(msgpack_unpacker *mp) unsigned int i; for(i=0; i < mp->top; ++i) { rb_gc_mark(mp->stack[i].obj); - rb_gc_mark(mp->stack[i].tmp.map_key); + rb_gc_mark(mp->stack[i].map_key); } } @@ -61,7 +152,7 @@ static VALUE MessagePack_Unpacker_alloc(VALUE klass) static VALUE MessagePack_Unpacker_reset(VALUE self) { UNPACKER(self, mp); - mp->user.finished = false; + mp->user.finished = 0; msgpack_unpacker_init(mp); return self; } @@ -93,10 +184,10 @@ static VALUE MessagePack_Unpacker_execute_impl(VALUE args) if(ret < 0) { rb_raise(eUnpackError, "Parse error."); } else if(ret > 0) { - mp->user.finished = true; + mp->user.finished = 1; return ULONG2NUM(from); } else { - mp->user.finished = false; + mp->user.finished = 0; return ULONG2NUM(from); } } diff --git a/ruby/unpack_context.h b/ruby/unpack_context.h deleted file mode 100644 index 35e01324..00000000 --- a/ruby/unpack_context.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * MessagePack unpacking routine for Ruby - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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. - */ -#ifndef UNPACK_CONTEXT_H__ -#define UNPACK_CONTEXT_H__ - -#include "ruby.h" -#include -#include - -typedef VALUE msgpack_object; - -typedef struct { - bool finished; -} msgpack_unpack_context; - - -#include "msgpack/unpack/inline_context.h" - -#endif /* unpack_context.h */ - diff --git a/ruby/unpack_inline.c b/ruby/unpack_inline.c deleted file mode 100644 index 849441c4..00000000 --- a/ruby/unpack_inline.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * MessagePack unpacking routine for Ruby - * - * Copyright (C) 2008 FURUHASHI Sadayuki - * - * Licensed 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 "unpack_context.h" - -static inline VALUE msgpack_unpack_init(msgpack_unpack_context* x) -{ return Qnil; } - -static inline VALUE msgpack_unpack_unsigned_int_8(msgpack_unpack_context* x, uint8_t d) -{ return INT2FIX(d); } - -static inline VALUE msgpack_unpack_unsigned_int_16(msgpack_unpack_context* x, uint16_t d) -{ return INT2FIX(d); } - -static inline VALUE msgpack_unpack_unsigned_int_32(msgpack_unpack_context* x, uint32_t d) -{ return UINT2NUM(d); } - -static inline VALUE msgpack_unpack_unsigned_int_64(msgpack_unpack_context* x, uint64_t d) -{ return rb_ull2inum(d); } - -static inline VALUE msgpack_unpack_signed_int_8(msgpack_unpack_context* x, int8_t d) -{ return INT2FIX((long)d); } - -static inline VALUE msgpack_unpack_signed_int_16(msgpack_unpack_context* x, int16_t d) -{ return INT2FIX((long)d); } - -static inline VALUE msgpack_unpack_signed_int_32(msgpack_unpack_context* x, int32_t d) -{ return INT2NUM((long)d); } - -static inline VALUE msgpack_unpack_signed_int_64(msgpack_unpack_context* x, int64_t d) -{ return rb_ll2inum(d); } - -static inline VALUE msgpack_unpack_float(msgpack_unpack_context* x, float d) -{ return rb_float_new(d); } - -static inline VALUE msgpack_unpack_double(msgpack_unpack_context* x, double d) -{ return rb_float_new(d); } - -static inline VALUE msgpack_unpack_nil(msgpack_unpack_context* x) -{ return Qnil; } - -static inline VALUE msgpack_unpack_true(msgpack_unpack_context* x) -{ return Qtrue; } - -static inline VALUE msgpack_unpack_false(msgpack_unpack_context* x) -{ return Qfalse; } - -static inline VALUE msgpack_unpack_array_start(msgpack_unpack_context* x, unsigned int n) -{ return rb_ary_new2(n); } - -static inline void msgpack_unpack_array_item(msgpack_unpack_context* x, VALUE c, VALUE o) -{ rb_ary_push(c, o); } // FIXME set value directry RARRAY_PTR(obj)[RARRAY_LEN(obj)++] - -static inline VALUE msgpack_unpack_map_start(msgpack_unpack_context* x, unsigned int n) -{ return rb_hash_new(); } - -static inline void msgpack_unpack_map_item(msgpack_unpack_context* x, VALUE c, VALUE k, VALUE v) -{ rb_hash_aset(c, k, v); } - -static inline VALUE msgpack_unpack_raw(msgpack_unpack_context* x, const char* b, const char* p, unsigned int l) -{ return rb_str_new(p, l); } - -#include "msgpack/unpack/inline_impl.h" -