lang/c/msgpack: c-macro based template

git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@66 5a5092ae-2292-43ba-b2d5-dcab9c1a2731
This commit is contained in:
frsyuki
2009-02-15 09:09:57 +00:00
parent 76dda6d36e
commit 1222466a1c
35 changed files with 1034 additions and 1507 deletions

View File

@@ -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

View File

@@ -1,7 +1,7 @@
module MessagePack
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
MINOR = 2
TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.')

View File

@@ -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

View File

@@ -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 <stddef.h>
#include <stdint.h>
#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;
}

View File

@@ -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 <string.h>
#include <arpa/inet.h> /* __BYTE_ORDER */
#include "msgpack/pack/inline_impl.h"
#endif /* pack_inline.h */

View File

@@ -16,8 +16,99 @@
* limitations under the License.
*/
#include "ruby.h"
#include "unpack_context.h"
#include <stdio.h>
#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);
}
}

View File

@@ -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 <stddef.h>
#include <stdbool.h>
typedef VALUE msgpack_object;
typedef struct {
bool finished;
} msgpack_unpack_context;
#include "msgpack/unpack/inline_context.h"
#endif /* unpack_context.h */

View File

@@ -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"