diff options
Diffstat (limited to 'c')
-rw-r--r-- | c/Makefile | 17 | ||||
-rw-r--r-- | c/bench.c | 338 | ||||
-rw-r--r-- | c/bench_inline.c | 329 | ||||
l--------- | c/msgpack | 1 | ||||
-rw-r--r-- | c/pack.c | 40 | ||||
-rw-r--r-- | c/pack.h | 38 | ||||
-rw-r--r-- | c/pack_inline.h | 14 | ||||
-rw-r--r-- | c/unpack.c | 56 | ||||
-rw-r--r-- | c/unpack.h | 59 | ||||
-rw-r--r-- | c/unpack_context.h | 30 | ||||
-rw-r--r-- | c/unpack_inline.c | 82 |
11 files changed, 1004 insertions, 0 deletions
diff --git a/c/Makefile b/c/Makefile new file mode 100644 index 0000000..d4b2226 --- /dev/null +++ b/c/Makefile @@ -0,0 +1,17 @@ + +CFLAGS = -I.. -Wall -g -O4 +LDFLAGS = -L. + +all: bench + +bench: pack.o unpack.o unpack_inline.o bench.o pack.h pack_inline.h unpack.h unpack_context.h + $(CC) $(LDFLAGS) unpack.o unpack_inline.o pack.o bench.o -lyajl_s -o $@ + +bench_inline: pack.o bench_inline.o pack.h pack_inline.h + $(CC) $(LDFLAGS) pack.o bench_inline.o -lyajl_s -o $@ + +.PHONY: clean +clean: + $(RM) unpack.o unpack_inline.o pack.o test.o bench.o bench_inline.o + $(RM) bench bench_inline + diff --git a/c/bench.c b/c/bench.c new file mode 100644 index 0000000..f27350a --- /dev/null +++ b/c/bench.c @@ -0,0 +1,338 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/time.h> + +#include <msgpack/pack.h> +#include <msgpack/unpack.h> +#include <yajl/yajl_parse.h> +#include <yajl/yajl_gen.h> + + +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; } + + +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_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_item(void* data, void* c, void* o) { } +static void* unpack_map_start(void* data, unsigned int n) { return NULL; } +static void unpack_map_item(void* data, void* c, void* k, void* v) { } +static void* unpack_string(void* data, const void* b, size_t l) { return NULL; } +static void* unpack_raw(void* data, const void* b, size_t l) { /*printf("unpack raw %p %lu\n",b,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 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_string, + 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 MB/s\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 MB/s\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 MB/s\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 MB/s\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_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_float, + unpack_double, + unpack_nil, + unpack_true, + unpack_false, + unpack_array_start, + unpack_array_item, + unpack_map_start, + unpack_map_item, + unpack_string, + unpack_raw, + }; + msgpack_unpack_t* mupk = msgpack_unpack_new(NULL, &cb); + + + 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 MB/s\n", len / sec / 1024 / 1024); + + puts("----"); + puts("unpack integer"); + reset_timer(); + { + size_t off = 0; + int ret = msgpack_unpack_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 MB/s\n", len / sec / 1024 / 1024); + + + pack_buffer_reset(&mpkbuf); + msgpack_unpack_reset(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 MB/s\n", len / sec / 1024 / 1024); + + puts("----"); + puts("unpack string"); + reset_timer(); + { + size_t off = 0; + int ret = msgpack_unpack_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 MB/s\n", len / sec / 1024 / 1024); + + + msgpack_unpack_free(mupk); + 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/bench_inline.c b/c/bench_inline.c new file mode 100644 index 0000000..2901508 --- /dev/null +++ b/c/bench_inline.c @@ -0,0 +1,329 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/time.h> + +#include <msgpack/pack.h> +#include <msgpack/unpack.h> +#include <yajl/yajl_parse.h> +#include <yajl/yajl_gen.h> + + +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_big_int(msgpack_unpack_context* x, const void* b, unsigned int l) { return NULL; } +static inline void* msgpack_unpack_big_float(msgpack_unpack_context* x, const void* b, unsigned int l) { 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_string(msgpack_unpack_context* x, const void* b, size_t l) { return NULL; } +static inline void* msgpack_unpack_raw(msgpack_unpack_context* x, const void* b, 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_string, + 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/msgpack b/c/msgpack new file mode 120000 index 0000000..945c9b4 --- /dev/null +++ b/c/msgpack @@ -0,0 +1 @@ +.
\ No newline at end of file diff --git a/c/pack.c b/c/pack.c new file mode 100644 index 0000000..05bd38d --- /dev/null +++ b/c/pack.c @@ -0,0 +1,40 @@ +/* + * 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. + */ +#include "pack_inline.h" +#include <stdlib.h> +#include <stdio.h> + +msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_callback_t callback) +{ + msgpack_pack_t* ctx = calloc(1, sizeof(msgpack_pack_t)); + if(!ctx) { return NULL; } + ctx->data = data; + ctx->callback = callback; + return ctx; +} + +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 new file mode 100644 index 0000000..8107d9f --- /dev/null +++ b/c/pack.h @@ -0,0 +1,38 @@ +#ifndef MSGPACK_PACK_H__ +#define MSGPACK_PACK_H__ + +#include <stddef.h> +#include <stdint.h> + +typedef void (*msgpack_pack_callback_t)(void* data, const unsigned char* b, unsigned int i); + +typedef struct { + void* data; + msgpack_pack_callback_t callback; +} msgpack_pack_t; + +msgpack_pack_t* msgpack_pack_new(void* data, msgpack_pack_callback_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_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); +void msgpack_pack_true(msgpack_pack_t* ctx); +void msgpack_pack_false(msgpack_pack_t* ctx); +void msgpack_pack_array(msgpack_pack_t* ctx, unsigned int n); +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); + +#endif /* msgpack/pack.h */ + diff --git a/c/pack_inline.h b/c/pack_inline.h new file mode 100644 index 0000000..d943464 --- /dev/null +++ b/c/pack_inline.h @@ -0,0 +1,14 @@ +#ifndef PACK_INLINE_H__ +#define PACK_INLINE_H__ + +#include "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 "msgpack/pack/inline_impl.h" + + +#endif /* pack_inline.h */ + diff --git a/c/unpack.c b/c/unpack.c new file mode 100644 index 0000000..9fe3695 --- /dev/null +++ b/c/unpack.c @@ -0,0 +1,56 @@ +/* + * 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. + */ +#include "unpack.h" +#include "unpack_context.h" +#include <stdlib.h> + +msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callback) +{ + msgpack_unpacker* ctx; + ctx = (msgpack_unpacker*)calloc(1, sizeof(msgpack_unpacker)); + if(ctx == NULL) { return NULL; } + msgpack_unpacker_init(ctx); + ((msgpack_unpack_t*)ctx)->data = data; + ((msgpack_unpack_t*)ctx)->callback = *callback; + return (msgpack_unpack_t*)ctx; +} + +void msgpack_unpack_free(msgpack_unpack_t* ctx) +{ + free((msgpack_unpacker*)ctx); +} + +void* msgpack_unpack_data(msgpack_unpack_t* ctx) +{ + return msgpack_unpacker_data((msgpack_unpacker*)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; +} + +int msgpack_unpack_execute(msgpack_unpack_t* ctx, const char* data, size_t len, size_t* off) +{ + return msgpack_unpacker_execute( + (msgpack_unpacker*)ctx, + data, len, off); +} + diff --git a/c/unpack.h b/c/unpack.h new file mode 100644 index 0000000..3230a0d --- /dev/null +++ b/c/unpack.h @@ -0,0 +1,59 @@ +/* + * 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 MSGPACK_UNPACK_H__ +#define MSGPACK_UNPACK_H__ + +#include <stdint.h> +#include <stddef.h> + +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_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_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_string)(void* data, const void* b, size_t l); + void* (*unpack_raw)(void* data, const void* b, size_t l); +} msgpack_unpack_callback; + +typedef struct { + void* data; + msgpack_unpack_callback callback; +} msgpack_unpack_t; + +msgpack_unpack_t* msgpack_unpack_new(void* data, msgpack_unpack_callback* callback); +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); +void* msgpack_unpack_data(msgpack_unpack_t* ctx); + +#endif /* msgpack/unpack.h */ + diff --git a/c/unpack_context.h b/c/unpack_context.h new file mode 100644 index 0000000..7337c9e --- /dev/null +++ b/c/unpack_context.h @@ -0,0 +1,30 @@ +/* + * 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 "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 new file mode 100644 index 0000000..3525468 --- /dev/null +++ b/c/unpack_inline.c @@ -0,0 +1,82 @@ +/* + * 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_string(msgpack_unpack_t* x, const void* b, size_t l) +{ return x->callback.unpack_string(x->data, b, l); } + +static inline void* msgpack_unpack_raw(msgpack_unpack_t* x, const void* b, size_t l) +{ return x->callback.unpack_raw(x->data, b, l); } + + +#include "msgpack/unpack/inline_impl.h" + |