From aa23fb110a7b7f6ea209942b3ee267c3a01b6241 Mon Sep 17 00:00:00 2001 From: Evan Weaver Date: Sun, 27 Feb 2011 16:09:35 -0800 Subject: [PATCH] No need for stats wrapper. --- ext/rlibmemcached.i | 24 ++++---------- ext/rlibmemcached_wrap.c | 67 +------------------------------------- lib/memcached/memcached.rb | 2 +- test/profile/exercise.rb | 4 +-- 4 files changed, 10 insertions(+), 87 deletions(-) diff --git a/ext/rlibmemcached.i b/ext/rlibmemcached.i index a668837..2ecd775 100644 --- a/ext/rlibmemcached.i +++ b/ext/rlibmemcached.i @@ -13,6 +13,8 @@ %include "typemaps.i" %include "libmemcached/visibility.h" +//// Memory management + // Register libmemcached's struct free function to prevent memory leaks %freefunc memcached_st "memcached_free"; %freefunc memcached_server_st "memcached_server_free"; @@ -21,6 +23,7 @@ %newobject memcached_server_by_key; %newobject memcached_create; %newobject memcached_clone; +%newobject memcached_stat_get_value; // %trackobjects; // Doesn't fix any interesting leaks @@ -111,13 +114,13 @@ $1 = string; $2 = &length; }; + +// Strings with lengths %typemap(argout) (char *key, size_t *key_length) { - // Pushes an empty string when *key_length == 0 rb_ary_push($result, rb_str_new($1, *$2)); } // Array of strings - %typemap(out) (char **) { int i; VALUE ary = rb_ary_new(); @@ -146,7 +149,8 @@ //// Manual wrappers // Single get. SWIG likes to use SWIG_FromCharPtr instead of SWIG_FromCharPtrAndSize because -// of the retval/argout split, so it truncates return values with \0 in them. +// of the retval/argout split, so it truncates return values with \0 in them. There is probably a better +// way to do this. VALUE memcached_get_rvalue(memcached_st *ptr, const char *key, size_t key_length, uint32_t *flags, memcached_return *error); %{ VALUE memcached_get_rvalue(memcached_st *ptr, const char *key, size_t key_length, uint32_t *flags, memcached_return *error) { @@ -198,20 +202,6 @@ VALUE memcached_fetch_rvalue(memcached_st *ptr, char *key, size_t *key_length, u }; %} -// We need to wrap this so it doesn't leak memory. SWIG doesn't want to automatically free. We could -// maybe use a 'ret' %typemap, but this is ok. -VALUE memcached_stat_get_rvalue(memcached_st *ptr, memcached_stat_st *stat, char *key, memcached_return *error); -%{ -VALUE memcached_stat_get_rvalue(memcached_st *ptr, memcached_stat_st *stat, char *key, memcached_return *error) { - char *str; - VALUE ret; - str = memcached_stat_get_value(ptr, stat, key, error); - ret = rb_str_new2(str); - free(str); - return ret; -}; -%} - // Ruby isn't aware that the pointer is an array... there is probably a better way to do this memcached_server_st *memcached_select_server_at(memcached_st *in_ptr, int index); %{ diff --git a/ext/rlibmemcached_wrap.c b/ext/rlibmemcached_wrap.c index 1bb73a3..b409857 100644 --- a/ext/rlibmemcached_wrap.c +++ b/ext/rlibmemcached_wrap.c @@ -2294,16 +2294,6 @@ VALUE memcached_fetch_rvalue(memcached_st *ptr, char *key, size_t *key_length, u }; -VALUE memcached_stat_get_rvalue(memcached_st *ptr, memcached_stat_st *stat, char *key, memcached_return *error) { - char *str; - VALUE ret; - str = memcached_stat_get_value(ptr, stat, key, error); - ret = rb_str_new2(str); - free(str); - return ret; -}; - - memcached_server_st *memcached_select_server_at(memcached_st *in_ptr, int index) { return &(in_ptr->hosts[index]); }; @@ -8190,6 +8180,7 @@ _wrap_memcached_stat_get_value(int argc, VALUE *argv, VALUE self) { vresult = SWIG_Ruby_AppendOutput(vresult, SWIG_NewPointerObj((void*)(arg4), SWIGTYPE_p_memcached_return, new_flags)); } if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + free((char*)result); return vresult; fail: if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); @@ -9101,7 +9092,6 @@ _wrap_memcached_fetch(int argc, VALUE *argv, VALUE self) { result = (char *)memcached_fetch(arg1,arg2,arg3,arg4,arg5,arg6); vresult = SWIG_FromCharPtr((const char *)result); { - // Pushes an empty string when *key_length == 0 rb_ary_push(vresult, rb_str_new(arg2, *arg3)); } if (SWIG_IsTmpObj(res4)) { @@ -12680,7 +12670,6 @@ _wrap_memcached_fetch_rvalue(int argc, VALUE *argv, VALUE self) { result = (VALUE)memcached_fetch_rvalue(arg1,arg2,arg3,arg4,arg5); vresult = result; { - // Pushes an empty string when *key_length == 0 rb_ary_push(vresult, rb_str_new(arg2, *arg3)); } if (SWIG_IsTmpObj(res4)) { @@ -12701,59 +12690,6 @@ fail: } -SWIGINTERN VALUE -_wrap_memcached_stat_get_rvalue(int argc, VALUE *argv, VALUE self) { - memcached_st *arg1 = (memcached_st *) 0 ; - memcached_stat_st *arg2 = (memcached_stat_st *) 0 ; - char *arg3 = (char *) 0 ; - memcached_return *arg4 = (memcached_return *) 0 ; - void *argp1 = 0 ; - int res1 = 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - memcached_return temp4 ; - int res4 = SWIG_TMPOBJ ; - VALUE result; - VALUE vresult = Qnil; - - arg4 = &temp4; - if ((argc < 3) || (argc > 3)) { - rb_raise(rb_eArgError, "wrong # of arguments(%d for 3)",argc); SWIG_fail; - } - res1 = SWIG_ConvertPtr(argv[0], &argp1,SWIGTYPE_p_memcached_st, 0 | 0 ); - if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), Ruby_Format_TypeError( "", "memcached_st *","memcached_stat_get_rvalue", 1, argv[0] )); - } - arg1 = (memcached_st *)(argp1); - res2 = SWIG_ConvertPtr(argv[1], &argp2,SWIGTYPE_p_memcached_stat_st, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), Ruby_Format_TypeError( "", "memcached_stat_st *","memcached_stat_get_rvalue", 2, argv[1] )); - } - arg2 = (memcached_stat_st *)(argp2); - res3 = SWIG_AsCharPtrAndSize(argv[2], &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), Ruby_Format_TypeError( "", "char *","memcached_stat_get_rvalue", 3, argv[2] )); - } - arg3 = (char *)(buf3); - result = (VALUE)memcached_stat_get_rvalue(arg1,arg2,arg3,arg4); - vresult = result; - if (SWIG_IsTmpObj(res4)) { - vresult = SWIG_Ruby_AppendOutput(vresult, SWIG_From_unsigned_SS_short((*arg4))); - } else { - int new_flags = SWIG_IsNewObj(res4) ? (SWIG_POINTER_OWN | 0 ) : 0 ; - vresult = SWIG_Ruby_AppendOutput(vresult, SWIG_NewPointerObj((void*)(arg4), SWIGTYPE_p_memcached_return, new_flags)); - } - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return vresult; -fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return Qnil; -} - - SWIGINTERN VALUE _wrap_memcached_select_server_at(int argc, VALUE *argv, VALUE self) { memcached_st *arg1 = (memcached_st *) 0 ; @@ -13716,7 +13652,6 @@ SWIGEXPORT void Init_rlibmemcached(void) { rb_define_module_function(mRlibmemcached, "memcached_get_len_rvalue", _wrap_memcached_get_len_rvalue, -1); rb_define_module_function(mRlibmemcached, "memcached_get_from_last_rvalue", _wrap_memcached_get_from_last_rvalue, -1); rb_define_module_function(mRlibmemcached, "memcached_fetch_rvalue", _wrap_memcached_fetch_rvalue, -1); - rb_define_module_function(mRlibmemcached, "memcached_stat_get_rvalue", _wrap_memcached_stat_get_rvalue, -1); rb_define_module_function(mRlibmemcached, "memcached_select_server_at", _wrap_memcached_select_server_at, -1); rb_define_module_function(mRlibmemcached, "memcached_select_stat_at", _wrap_memcached_select_stat_at, -1); rb_define_module_function(mRlibmemcached, "memcached_generate_hash_rvalue", _wrap_memcached_generate_hash_rvalue, -1); diff --git a/lib/memcached/memcached.rb b/lib/memcached/memcached.rb index 4ff3af1..90b2c84 100644 --- a/lib/memcached/memcached.rb +++ b/lib/memcached/memcached.rb @@ -559,7 +559,7 @@ Please note that when :no_block => true, update methods do not raise on keys.each do |key| server_structs.size.times do |index| - value, ret = Lib.memcached_stat_get_rvalue( + value, ret = Lib.memcached_stat_get_value( @struct, Lib.memcached_select_stat_at(@struct, stat_struct, index), key) diff --git a/test/profile/exercise.rb b/test/profile/exercise.rb index a52910f..9eb410c 100644 --- a/test/profile/exercise.rb +++ b/test/profile/exercise.rb @@ -106,9 +106,7 @@ class Worker @cache.delete @key2 rescue nil end when "stats" - @i.times do - @cache.stats - end + @cache.stats when "multiget" @i.times do @cache.get([@key1, @key2, @key3])