├── log └── .gitignore ├── test ├── unit │ ├── __init__.py │ ├── test_protocol.py │ └── test_basic.py ├── Makefile ├── conf │ └── control.sh └── lib │ └── utils.py ├── deps ├── jemalloc │ ├── config.stamp.in │ ├── test │ │ ├── bitmap.exp │ │ ├── mremap.exp │ │ ├── rallocm.exp │ │ ├── ALLOCM_ARENA.exp │ │ ├── allocated.exp │ │ ├── thread_arena.exp │ │ ├── thread_tcache_enabled.exp │ │ ├── allocm.exp │ │ ├── aligned_alloc.exp │ │ ├── posix_memalign.exp │ │ ├── mremap.c │ │ ├── ALLOCM_ARENA.c │ │ ├── thread_arena.c │ │ ├── thread_tcache_enabled.c │ │ ├── allocated.c │ │ ├── posix_memalign.c │ │ ├── aligned_alloc.c │ │ └── rallocm.c │ ├── VERSION │ ├── src │ │ ├── mb.c │ │ ├── hash.c │ │ ├── atomic.c │ │ ├── extent.c │ │ ├── rtree.c │ │ ├── tsd.c │ │ ├── bitmap.c │ │ └── base.c │ ├── doc │ │ ├── jemalloc.html │ │ ├── html.xsl.in │ │ ├── manpages.xsl.in │ │ └── stylesheet.xsl │ ├── bin │ │ └── jemalloc.sh.in │ ├── autogen.sh │ ├── include │ │ ├── msvc_compat │ │ │ ├── stdbool.h │ │ │ └── strings.h │ │ └── jemalloc │ │ │ └── internal │ │ │ ├── chunk_mmap.h │ │ │ ├── quarantine.h │ │ │ ├── base.h │ │ │ ├── chunk_dss.h │ │ │ ├── extent.h │ │ │ ├── huge.h │ │ │ ├── hash.h │ │ │ ├── prng.h │ │ │ ├── chunk.h │ │ │ ├── qr.h │ │ │ ├── ql.h │ │ │ ├── ckh.h │ │ │ ├── mutex.h │ │ │ └── mb.h │ ├── .gitignore │ ├── README │ └── COPYING ├── lua │ ├── doc │ │ ├── logo.gif │ │ ├── cover.png │ │ ├── manual.css │ │ ├── readme.html │ │ └── lua.css │ ├── etc │ │ ├── lua.ico │ │ ├── lua.hpp │ │ ├── lua.pc │ │ ├── all.c │ │ ├── min.c │ │ ├── Makefile │ │ ├── strict.lua │ │ ├── README │ │ ├── luavs.bat │ │ └── noparser.c │ ├── test │ │ ├── life.lua │ │ ├── echo.lua │ │ ├── hello.lua │ │ ├── printf.lua │ │ ├── env.lua │ │ ├── luac.lua │ │ ├── fibfor.lua │ │ ├── readonly.lua │ │ ├── table.lua │ │ ├── cf.lua │ │ ├── xd.lua │ │ ├── globals.lua │ │ ├── bisect.lua │ │ ├── fib.lua │ │ ├── factorial.lua │ │ ├── sieve.lua │ │ ├── trace-calls.lua │ │ ├── trace-globals.lua │ │ ├── README │ │ └── sort.lua │ ├── src │ │ ├── lapi.h │ │ ├── linit.c │ │ ├── lstring.h │ │ ├── lundump.h │ │ ├── ldebug.h │ │ ├── ltm.h │ │ ├── lualib.h │ │ ├── lfunc.h │ │ ├── lvm.h │ │ ├── ltable.h │ │ ├── lmem.h │ │ ├── lzio.h │ │ ├── ltm.c │ │ ├── lzio.c │ │ ├── ldo.h │ │ ├── llex.h │ │ ├── lmem.c │ │ ├── lparser.h │ │ ├── llimits.h │ │ ├── lcode.h │ │ └── lopcodes.c │ ├── README │ └── COPYRIGHT ├── leveldb-1.15.0.tar.gz ├── snappy-1.1.2.tar.gz ├── hiredis │ ├── .gitignore │ ├── zmalloc.h │ ├── fmacros.h │ ├── CHANGELOG.md │ ├── bench1.c │ ├── bench3.c │ ├── example-libev.c │ ├── example-libevent.c │ ├── COPYING │ ├── example-ae.c │ ├── bench2.c │ ├── example.c │ └── net.h └── Makefile ├── Makefile ├── src ├── test │ ├── test_event.c │ ├── test_repl.c │ ├── test_conf.c │ └── testhelp.h ├── util │ ├── nc_conf.h │ ├── nc_md5.h │ ├── nc_signal.h │ ├── nc_server.h │ ├── nc_file.h │ ├── nc_rbtree.h │ ├── nc_array.h │ ├── nc_mbuf.h │ ├── nc_util.h │ ├── nc_connection.h │ ├── nc_string.c │ ├── nc_string.h │ └── nc_event.h ├── ndb_command.h ├── ndb_message.h ├── ndb_job.h ├── ndb_cursor.h ├── ndb_stat.h ├── ndb.h ├── ndb_repl.h ├── ndb_leveldb.h ├── ndb_stat.c └── Makefile ├── README.rst ├── .gitignore └── conf └── ndb.conf /log/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | -------------------------------------------------------------------------------- /test/unit/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /deps/jemalloc/config.stamp.in: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /deps/jemalloc/test/bitmap.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/test/mremap.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/test/rallocm.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/test/ALLOCM_ARENA.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/test/allocated.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/test/thread_arena.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/test/thread_tcache_enabled.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /deps/jemalloc/VERSION: -------------------------------------------------------------------------------- 1 | 3.2.0-0-g87499f6748ebe4817571e817e9f680ccb5bf54a9 2 | -------------------------------------------------------------------------------- /deps/lua/doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/lua/doc/logo.gif -------------------------------------------------------------------------------- /deps/lua/etc/lua.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/lua/etc/lua.ico -------------------------------------------------------------------------------- /deps/lua/doc/cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/lua/doc/cover.png -------------------------------------------------------------------------------- /deps/lua/test/life.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/lua/test/life.lua -------------------------------------------------------------------------------- /deps/leveldb-1.15.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/leveldb-1.15.0.tar.gz -------------------------------------------------------------------------------- /deps/snappy-1.1.2.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/snappy-1.1.2.tar.gz -------------------------------------------------------------------------------- /deps/jemalloc/src/mb.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MB_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | -------------------------------------------------------------------------------- /deps/hiredis/.gitignore: -------------------------------------------------------------------------------- 1 | /hiredis-test 2 | /hiredis-example* 3 | /*.o 4 | /*.so 5 | /*.dylib 6 | /*.a 7 | -------------------------------------------------------------------------------- /deps/jemalloc/doc/jemalloc.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/idning/ndb/HEAD/deps/jemalloc/doc/jemalloc.html -------------------------------------------------------------------------------- /deps/jemalloc/src/hash.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_HASH_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | -------------------------------------------------------------------------------- /deps/jemalloc/src/atomic.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_ATOMIC_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | -------------------------------------------------------------------------------- /deps/lua/test/echo.lua: -------------------------------------------------------------------------------- 1 | -- echo command line arguments 2 | 3 | for i=0,table.getn(arg) do 4 | print(i,arg[i]) 5 | end 6 | -------------------------------------------------------------------------------- /deps/lua/test/hello.lua: -------------------------------------------------------------------------------- 1 | -- the first program in every language 2 | 3 | io.write("Hello world, from ",_VERSION,"!\n") 4 | -------------------------------------------------------------------------------- /deps/lua/test/printf.lua: -------------------------------------------------------------------------------- 1 | -- an implementation of printf 2 | 3 | function printf(...) 4 | io.write(string.format(...)) 5 | end 6 | 7 | printf("Hello %s from %s on %s\n",os.getenv"USER" or "there",_VERSION,os.date()) 8 | -------------------------------------------------------------------------------- /deps/jemalloc/bin/jemalloc.sh.in: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | prefix=@prefix@ 4 | exec_prefix=@exec_prefix@ 5 | libdir=@libdir@ 6 | 7 | @LD_PRELOAD_VAR@=${libdir}/libjemalloc.@SOREV@ 8 | export @LD_PRELOAD_VAR@ 9 | exec "$@" 10 | -------------------------------------------------------------------------------- /deps/lua/test/env.lua: -------------------------------------------------------------------------------- 1 | -- read environment variables as if they were global variables 2 | 3 | local f=function (t,i) return os.getenv(i) end 4 | setmetatable(getfenv(),{__index=f}) 5 | 6 | -- an example 7 | print(a,USER,PATH) 8 | -------------------------------------------------------------------------------- /deps/jemalloc/doc/html.xsl.in: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /deps/jemalloc/doc/manpages.xsl.in: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /deps/lua/etc/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /deps/lua/test/luac.lua: -------------------------------------------------------------------------------- 1 | -- bare-bones luac in Lua 2 | -- usage: lua luac.lua file.lua 3 | 4 | assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua") 5 | f=assert(io.open("luac.out","wb")) 6 | assert(f:write(string.dump(assert(loadfile(arg[1]))))) 7 | assert(f:close()) 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: dep 2 | cd src && $(MAKE) $@ 3 | 4 | dep: 5 | cd deps && $(MAKE) 6 | 7 | test: 8 | cd test && $(MAKE) test 9 | 10 | clean: 11 | cd deps && $(MAKE) $@ 12 | cd src && $(MAKE) $@ 13 | find . -name core | xargs rm -f 14 | rm -f tags cscope.* 15 | rm -rf db 16 | 17 | .PHONY: all clean test 18 | -------------------------------------------------------------------------------- /deps/lua/test/fibfor.lua: -------------------------------------------------------------------------------- 1 | -- example of for with generator functions 2 | 3 | function generatefib (n) 4 | return coroutine.wrap(function () 5 | local a,b = 1, 1 6 | while a <= n do 7 | coroutine.yield(a) 8 | a, b = b, a+b 9 | end 10 | end) 11 | end 12 | 13 | for i in generatefib(1000) do print(i) end 14 | -------------------------------------------------------------------------------- /deps/lua/test/readonly.lua: -------------------------------------------------------------------------------- 1 | -- make global variables readonly 2 | 3 | local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end 4 | local g={} 5 | local G=getfenv() 6 | setmetatable(g,{__index=G,__newindex=f}) 7 | setfenv(1,g) 8 | 9 | -- an example 10 | rawset(g,"x",3) 11 | x=2 12 | y=1 -- cannot redefine `y' 13 | -------------------------------------------------------------------------------- /src/test/test_event.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file : test_event.c 3 | * author : ning 4 | * date : 2014-06-30 17:26:31 5 | */ 6 | 7 | #include "nc_util.h" 8 | #include "testhelp.h" 9 | 10 | 11 | 12 | int 13 | main() 14 | { 15 | 16 | test_report(); 17 | return 0; 18 | } 19 | 20 | /* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ 21 | -------------------------------------------------------------------------------- /deps/hiredis/zmalloc.h: -------------------------------------------------------------------------------- 1 | /* Drop in replacement for zmalloc.h in order to just use libc malloc without 2 | * any wrappering. */ 3 | 4 | #ifndef ZMALLOC_H 5 | #define ZMALLOC_H 6 | 7 | #define zmalloc malloc 8 | #define zrealloc realloc 9 | #define zcalloc(x) calloc(x,1) 10 | #define zfree free 11 | #define zstrdup strdup 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /deps/jemalloc/doc/stylesheet.xsl: -------------------------------------------------------------------------------- 1 | 2 | ansi 3 | 4 | 5 | "" 6 | 7 | 8 | -------------------------------------------------------------------------------- /deps/lua/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /deps/lua/test/table.lua: -------------------------------------------------------------------------------- 1 | -- make table, grouping all data for the same item 2 | -- input is 2 columns (item, data) 3 | 4 | local A 5 | while 1 do 6 | local l=io.read() 7 | if l==nil then break end 8 | local _,_,a,b=string.find(l,'"?([_%w]+)"?%s*(.*)$') 9 | if a~=A then A=a io.write("\n",a,":") end 10 | io.write(" ",b) 11 | end 12 | io.write("\n") 13 | -------------------------------------------------------------------------------- /deps/jemalloc/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | for i in autoconf; do 4 | echo "$i" 5 | $i 6 | if [ $? -ne 0 ]; then 7 | echo "Error $? in $i" 8 | exit 1 9 | fi 10 | done 11 | 12 | echo "./configure --enable-autogen $@" 13 | ./configure --enable-autogen $@ 14 | if [ $? -ne 0 ]; then 15 | echo "Error $? in ./configure" 16 | exit 1 17 | fi 18 | -------------------------------------------------------------------------------- /deps/lua/test/cf.lua: -------------------------------------------------------------------------------- 1 | -- temperature conversion table (celsius to farenheit) 2 | 3 | for c0=-20,50-1,10 do 4 | io.write("C ") 5 | for c=c0,c0+10-1 do 6 | io.write(string.format("%3.0f ",c)) 7 | end 8 | io.write("\n") 9 | 10 | io.write("F ") 11 | for c=c0,c0+10-1 do 12 | f=(9/5)*c+32 13 | io.write(string.format("%3.0f ",f)) 14 | end 15 | io.write("\n\n") 16 | end 17 | -------------------------------------------------------------------------------- /deps/hiredis/fmacros.h: -------------------------------------------------------------------------------- 1 | #ifndef __HIREDIS_FMACRO_H 2 | #define __HIREDIS_FMACRO_H 3 | 4 | #if !defined(_BSD_SOURCE) 5 | #define _BSD_SOURCE 6 | #endif 7 | 8 | #if defined(__sun__) 9 | #define _POSIX_C_SOURCE 200112L 10 | #elif defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) 11 | #define _XOPEN_SOURCE 600 12 | #else 13 | #define _XOPEN_SOURCE 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ndb 2 | === 3 | 4 | - redis protocol 5 | - store with leveldb 6 | - config with lua 7 | - got 50k/s on GET/SET with redis-benchmark no pipeline. (less then 10,000,000 records -- data in mem) 8 | - got 100K SET, 300K GET /s on small data with pipeline=1000 (more then ) 9 | 10 | performance 11 | =========== 12 | 13 | - data: 2G/20G/200G 14 | - pipeline: 1/1000 15 | - media: ssd/hdd 16 | 17 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | nosetests -v 3 | 4 | debug: 5 | export T_VERBOSE=9; nosetests --nocapture --nologcapture -v 6 | 7 | large: 8 | export T_VERBOSE=3; export T_LARGE=102; nosetests --nocapture --nologcapture -v 9 | 10 | case: 11 | nosetests --nocapture --nologcapture -v unit.test_protocol:test_badreq 12 | 13 | wait: 14 | export T_VERBOSE=9; nosetests --nocapture --nologcapture -v unit.test_repl:repl_wait 15 | -------------------------------------------------------------------------------- /deps/jemalloc/include/msvc_compat/stdbool.h: -------------------------------------------------------------------------------- 1 | #ifndef stdbool_h 2 | #define stdbool_h 3 | 4 | #include 5 | 6 | /* MSVC doesn't define _Bool or bool in C, but does have BOOL */ 7 | /* Note this doesn't pass autoconf's test because (bool) 0.5 != true */ 8 | typedef BOOL _Bool; 9 | 10 | #define bool _Bool 11 | #define true 1 12 | #define false 0 13 | 14 | #define __bool_true_false_are_defined 1 15 | 16 | #endif /* stdbool_h */ 17 | -------------------------------------------------------------------------------- /deps/lua/test/xd.lua: -------------------------------------------------------------------------------- 1 | -- hex dump 2 | -- usage: lua xd.lua < file 3 | 4 | local offset=0 5 | while true do 6 | local s=io.read(16) 7 | if s==nil then return end 8 | io.write(string.format("%08X ",offset)) 9 | string.gsub(s,"(.)", 10 | function (c) io.write(string.format("%02X ",string.byte(c))) end) 11 | io.write(string.rep(" ",3*(16-string.len(s)))) 12 | io.write(" ",string.gsub(s,"%c","."),"\n") 13 | offset=offset+16 14 | end 15 | -------------------------------------------------------------------------------- /deps/lua/doc/manual.css: -------------------------------------------------------------------------------- 1 | h3 code { 2 | font-family: inherit ; 3 | font-size: inherit ; 4 | } 5 | 6 | pre, code { 7 | font-size: 12pt ; 8 | } 9 | 10 | span.apii { 11 | float: right ; 12 | font-family: inherit ; 13 | font-style: normal ; 14 | font-size: small ; 15 | color: gray ; 16 | } 17 | 18 | p+h1, ul+h1 { 19 | padding-top: 0.4em ; 20 | padding-bottom: 0.4em ; 21 | padding-left: 30px ; 22 | margin-left: -30px ; 23 | background-color: #E0E0FF ; 24 | } 25 | -------------------------------------------------------------------------------- /deps/jemalloc/.gitignore: -------------------------------------------------------------------------------- 1 | /autom4te.cache/ 2 | /config.stamp 3 | /config.log 4 | /config.status 5 | /doc/html.xsl 6 | /doc/manpages.xsl 7 | /doc/jemalloc.xml 8 | /lib/ 9 | /Makefile 10 | /include/jemalloc/internal/jemalloc_internal\.h 11 | /include/jemalloc/internal/size_classes\.h 12 | /include/jemalloc/jemalloc\.h 13 | /include/jemalloc/jemalloc_defs\.h 14 | /src/*.[od] 15 | /test/*.[od] 16 | /test/*.out 17 | /test/[a-zA-Z_]* 18 | !test/*.c 19 | !test/*.exp 20 | /bin/jemalloc.sh 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | 25 | tags 26 | 27 | 28 | #bins 29 | ndb 30 | core 31 | 32 | 33 | deps/leveldb/ 34 | deps/lua/src/lua 35 | deps/lua/src/luac 36 | deps/snappy/ 37 | 38 | *.pyc 39 | *.log 40 | /src/test_* 41 | -------------------------------------------------------------------------------- /deps/lua/test/globals.lua: -------------------------------------------------------------------------------- 1 | -- reads luac listings and reports global variable usage 2 | -- lines where a global is written to are marked with "*" 3 | -- typical usage: luac -p -l file.lua | lua globals.lua | sort | lua table.lua 4 | 5 | while 1 do 6 | local s=io.read() 7 | if s==nil then break end 8 | local ok,_,l,op,g=string.find(s,"%[%-?(%d*)%]%s*([GS])ETGLOBAL.-;%s+(.*)$") 9 | if ok then 10 | if op=="S" then op="*" else op="" end 11 | io.write(g,"\t",l,op,"\n") 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /deps/jemalloc/include/msvc_compat/strings.h: -------------------------------------------------------------------------------- 1 | #ifndef strings_h 2 | #define strings_h 3 | 4 | /* MSVC doesn't define ffs/ffsl. This dummy strings.h header is provided 5 | * for both */ 6 | #include 7 | #pragma intrinsic(_BitScanForward) 8 | static __forceinline int ffsl(long x) 9 | { 10 | unsigned long i; 11 | 12 | if (_BitScanForward(&i, x)) 13 | return (i + 1); 14 | return (0); 15 | } 16 | 17 | static __forceinline int ffs(int x) 18 | { 19 | 20 | return (ffsl(x)); 21 | } 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /conf/ndb.conf: -------------------------------------------------------------------------------- 1 | -- vim: ft=lua 2 | 3 | -- listen = '0.0.0.0:5527' 4 | listen = 5527 5 | daemonize = 0 6 | loglevel = 3 7 | logfile = 'log/ndb.log' 8 | 9 | leveldb = { 10 | 11 | dbpath = 'db', 12 | cache_size = 500 *1024*1024, 13 | block_size = 32 *1024, 14 | write_buffer_size = 64 *1024*1024, 15 | 16 | -- TODO: not implemented 17 | -- compaction_speed = 1000*1024*1024, 18 | 19 | compression = 0, 20 | } 21 | 22 | -- 23 | --blacklist = { 24 | 25 | --} 26 | 27 | -------------------------------------------------------------------------------- /deps/Makefile: -------------------------------------------------------------------------------- 1 | all: snappy leveldb lua hiredis 2 | 3 | leveldb: leveldb-1.15.0.tar.gz 4 | tar vzxf leveldb-1.15.0.tar.gz 5 | mv leveldb-1.15.0 leveldb 6 | cd leveldb && make -j8 7 | 8 | snappy: snappy-1.1.2.tar.gz 9 | tar vzxf snappy-1.1.2.tar.gz 10 | mv snappy-1.1.2 snappy 11 | cd snappy && ./configure && make -j8 12 | 13 | .PHONY: lua hiredis 14 | 15 | hiredis: 16 | cd hiredis && make 17 | 18 | lua: 19 | cd lua && make linux 20 | 21 | clean: 22 | cd lua && make clean 23 | rm -rf snappy 24 | rm -rf leveldb 25 | -------------------------------------------------------------------------------- /deps/jemalloc/test/allocm.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Alignment: 8 3 | Alignment: 16 4 | Alignment: 32 5 | Alignment: 64 6 | Alignment: 128 7 | Alignment: 256 8 | Alignment: 512 9 | Alignment: 1024 10 | Alignment: 2048 11 | Alignment: 4096 12 | Alignment: 8192 13 | Alignment: 16384 14 | Alignment: 32768 15 | Alignment: 65536 16 | Alignment: 131072 17 | Alignment: 262144 18 | Alignment: 524288 19 | Alignment: 1048576 20 | Alignment: 2097152 21 | Alignment: 4194304 22 | Alignment: 8388608 23 | Alignment: 16777216 24 | Alignment: 33554432 25 | Test end 26 | -------------------------------------------------------------------------------- /deps/jemalloc/test/aligned_alloc.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Alignment: 8 3 | Alignment: 16 4 | Alignment: 32 5 | Alignment: 64 6 | Alignment: 128 7 | Alignment: 256 8 | Alignment: 512 9 | Alignment: 1024 10 | Alignment: 2048 11 | Alignment: 4096 12 | Alignment: 8192 13 | Alignment: 16384 14 | Alignment: 32768 15 | Alignment: 65536 16 | Alignment: 131072 17 | Alignment: 262144 18 | Alignment: 524288 19 | Alignment: 1048576 20 | Alignment: 2097152 21 | Alignment: 4194304 22 | Alignment: 8388608 23 | Alignment: 16777216 24 | Alignment: 33554432 25 | Test end 26 | -------------------------------------------------------------------------------- /deps/jemalloc/test/posix_memalign.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Alignment: 8 3 | Alignment: 16 4 | Alignment: 32 5 | Alignment: 64 6 | Alignment: 128 7 | Alignment: 256 8 | Alignment: 512 9 | Alignment: 1024 10 | Alignment: 2048 11 | Alignment: 4096 12 | Alignment: 8192 13 | Alignment: 16384 14 | Alignment: 32768 15 | Alignment: 65536 16 | Alignment: 131072 17 | Alignment: 262144 18 | Alignment: 524288 19 | Alignment: 1048576 20 | Alignment: 2097152 21 | Alignment: 4194304 22 | Alignment: 8388608 23 | Alignment: 16777216 24 | Alignment: 33554432 25 | Test end 26 | -------------------------------------------------------------------------------- /deps/hiredis/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### 0.10.1 2 | 3 | * Makefile overhaul. Important to check out if you override one or more 4 | variables using environment variables or via arguments to the "make" tool. 5 | 6 | * Issue #45: Fix potential memory leak for a multi bulk reply with 0 elements 7 | being created by the default reply object functions. 8 | 9 | * Issue #43: Don't crash in an asynchronous context when Redis returns an error 10 | reply after the connection has been made (this happens when the maximum 11 | number of connections is reached). 12 | 13 | ### 0.10.0 14 | 15 | * See commit log. 16 | 17 | -------------------------------------------------------------------------------- /test/conf/control.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | start() 4 | { 5 | stop 6 | ulimit -c unlimited 7 | 8 | pushd . > /dev/null 9 | 10 | cd `dirname $$0` 11 | ${startcmd} 12 | popd 13 | } 14 | 15 | stop() 16 | { 17 | pkill -f '${runcmd}' 18 | } 19 | 20 | case C"$$1" in 21 | C) 22 | echo "Usage: $$0 {start|stop}" 23 | ;; 24 | Cstart) 25 | start 26 | echo "Done!" 27 | ;; 28 | Cstop) 29 | stop 30 | echo "Done!" 31 | ;; 32 | C*) 33 | echo "Usage: $$0 {start|stop}" 34 | ;; 35 | esac 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /deps/hiredis/bench1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "hiredis.h" 6 | 7 | int main(void) { 8 | unsigned int i; 9 | redisContext *c; 10 | redisReply *reply; 11 | 12 | struct timeval timeout = { 1, 500000 }; // 1.5 seconds 13 | c = redisConnectWithTimeout((char*)"127.0.0.5", 22000, timeout); 14 | if (c->err) { 15 | printf("Connection error: %s\n", c->errstr); 16 | exit(1); 17 | } 18 | for(i=0; i<100*1000; i++){ 19 | reply = redisCommand(c,"SET %s %s", "foo", "hello world"); 20 | freeReplyObject(reply); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /src/util/nc_conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : nc_conf.h 3 | * author : ning 4 | * date : 2014-06-29 21:01:29 5 | */ 6 | 7 | #ifndef _NC_LUACONF_H_ 8 | #define _NC_LUACONF_H_ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "nc_util.h" 15 | 16 | typedef struct nc_conf_s { 17 | lua_State *L; 18 | const char *conf; 19 | } nc_conf_t; 20 | 21 | rstatus_t nc_conf_init(nc_conf_t *conf, const char *filename); 22 | rstatus_t nc_conf_deinit(nc_conf_t *conf); 23 | 24 | char *nc_conf_get_str(nc_conf_t *conf, const char *name, char *default_v); 25 | int nc_conf_get_num(nc_conf_t *conf, const char *name, int default_v); 26 | 27 | #endif 28 | /* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ 29 | -------------------------------------------------------------------------------- /src/ndb_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : ndb_cmd.h 3 | * author : ning 4 | * date : 2014-08-02 10:03:51 5 | */ 6 | 7 | #ifndef _NDB_COMMAND_H_ 8 | #define _NDB_COMMAND_H_ 9 | 10 | #include "ndb.h" 11 | 12 | typedef rstatus_t (*cmd_process_t)(struct conn*, msg_t *msg); 13 | 14 | typedef struct command_s { 15 | char *name; 16 | int argc; 17 | char *sflags; /* flags as string representation, one char per flag. */ 18 | cmd_process_t proc; 19 | } command_t; 20 | 21 | rstatus_t command_init(); 22 | rstatus_t command_deinit(); 23 | rstatus_t command_process(struct conn* conn, msg_t *msg); 24 | 25 | rstatus_t ndb_conn_recv_done(struct conn *conn); 26 | rstatus_t ndb_conn_send_done(struct conn *conn); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /deps/lua/test/bisect.lua: -------------------------------------------------------------------------------- 1 | -- bisection method for solving non-linear equations 2 | 3 | delta=1e-6 -- tolerance 4 | 5 | function bisect(f,a,b,fa,fb) 6 | local c=(a+b)/2 7 | io.write(n," c=",c," a=",a," b=",b,"\n") 8 | if c==a or c==b or math.abs(a-b)>> ",string.rep(" ",level)) 9 | if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end 10 | t=debug.getinfo(2) 11 | if event=="call" then 12 | level=level+1 13 | else 14 | level=level-1 if level<0 then level=0 end 15 | end 16 | if t.what=="main" then 17 | if event=="call" then 18 | io.write("begin ",t.short_src) 19 | else 20 | io.write("end ",t.short_src) 21 | end 22 | elseif t.what=="Lua" then 23 | -- table.foreach(t,print) 24 | io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") 25 | else 26 | io.write(event," ",t.name or "(C)"," [",t.what,"] ") 27 | end 28 | io.write("\n") 29 | end 30 | 31 | debug.sethook(hook,"cr") 32 | level=0 33 | -------------------------------------------------------------------------------- /deps/lua/test/trace-globals.lua: -------------------------------------------------------------------------------- 1 | -- trace assigments to global variables 2 | 3 | do 4 | -- a tostring that quotes strings. note the use of the original tostring. 5 | local _tostring=tostring 6 | local tostring=function(a) 7 | if type(a)=="string" then 8 | return string.format("%q",a) 9 | else 10 | return _tostring(a) 11 | end 12 | end 13 | 14 | local log=function (name,old,new) 15 | local t=debug.getinfo(3,"Sl") 16 | local line=t.currentline 17 | io.write(t.short_src) 18 | if line>=0 then io.write(":",line) end 19 | io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") 20 | end 21 | 22 | local g={} 23 | local set=function (t,name,value) 24 | log(name,g[name],value) 25 | g[name]=value 26 | end 27 | setmetatable(getfenv(),{__index=g,__newindex=set}) 28 | end 29 | 30 | -- an example 31 | 32 | a=1 33 | b=2 34 | a=10 35 | b=20 36 | b=nil 37 | b=200 38 | print(a,b,c) 39 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/chunk_mmap.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | bool pages_purge(void *addr, size_t length); 13 | 14 | void *chunk_alloc_mmap(size_t size, size_t alignment, bool *zero); 15 | bool chunk_dealloc_mmap(void *chunk, size_t size); 16 | 17 | #endif /* JEMALLOC_H_EXTERNS */ 18 | /******************************************************************************/ 19 | #ifdef JEMALLOC_H_INLINES 20 | 21 | #endif /* JEMALLOC_H_INLINES */ 22 | /******************************************************************************/ 23 | -------------------------------------------------------------------------------- /deps/lua/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Initialization of libraries for lua.c 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | #include "lua.h" 12 | 13 | #include "lualib.h" 14 | #include "lauxlib.h" 15 | 16 | 17 | static const luaL_Reg lualibs[] = { 18 | {"", luaopen_base}, 19 | {LUA_LOADLIBNAME, luaopen_package}, 20 | {LUA_TABLIBNAME, luaopen_table}, 21 | {LUA_IOLIBNAME, luaopen_io}, 22 | {LUA_OSLIBNAME, luaopen_os}, 23 | {LUA_STRLIBNAME, luaopen_string}, 24 | {LUA_MATHLIBNAME, luaopen_math}, 25 | {LUA_DBLIBNAME, luaopen_debug}, 26 | {NULL, NULL} 27 | }; 28 | 29 | 30 | LUALIB_API void luaL_openlibs (lua_State *L) { 31 | const luaL_Reg *lib = lualibs; 32 | for (; lib->func; lib++) { 33 | lua_pushcfunction(L, lib->func); 34 | lua_pushstring(L, lib->name); 35 | lua_call(L, 1, 0); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/quarantine.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | /* Default per thread quarantine size if valgrind is enabled. */ 5 | #define JEMALLOC_VALGRIND_QUARANTINE_DEFAULT (ZU(1) << 24) 6 | 7 | #endif /* JEMALLOC_H_TYPES */ 8 | /******************************************************************************/ 9 | #ifdef JEMALLOC_H_STRUCTS 10 | 11 | #endif /* JEMALLOC_H_STRUCTS */ 12 | /******************************************************************************/ 13 | #ifdef JEMALLOC_H_EXTERNS 14 | 15 | void quarantine(void *ptr); 16 | bool quarantine_boot(void); 17 | 18 | #endif /* JEMALLOC_H_EXTERNS */ 19 | /******************************************************************************/ 20 | #ifdef JEMALLOC_H_INLINES 21 | 22 | #endif /* JEMALLOC_H_INLINES */ 23 | /******************************************************************************/ 24 | 25 | -------------------------------------------------------------------------------- /deps/lua/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lgc.h" 12 | #include "lobject.h" 13 | #include "lstate.h" 14 | 15 | 16 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 17 | 18 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 19 | 20 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 25 | 26 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 27 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 28 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/ndb_stat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : ndb_stat.h 3 | * author : ning 4 | * date : 2014-10-13 15:21:18 5 | */ 6 | 7 | #ifndef _NDB_STAT_H_ 8 | #define _NDB_STAT_H_ 9 | 10 | #include "nc_util.h" 11 | 12 | #define NDB_OPS_SEC_SAMPLES 16 13 | 14 | typedef struct stat_s { 15 | void *owner; /* instance */ 16 | uint64_t numconnections; 17 | uint64_t start_time; /* start time in us */ 18 | uint64_t ops; 19 | 20 | uint64_t last_aggregate_ops; 21 | uint64_t last_aggregate_ts; 22 | uint64_t aggregate_idx; 23 | 24 | uint64_t ops_sec_samples[NDB_OPS_SEC_SAMPLES]; 25 | } stat_t; 26 | 27 | rstatus_t stat_init(void *owner, stat_t *stat); 28 | rstatus_t stat_deinit(stat_t *stat); 29 | 30 | rstatus_t stat_inc(stat_t *stat, bool w, bool hit); 31 | sds stat_info(stat_t *stat); 32 | rstatus_t stat_cron(stat_t *stat); 33 | 34 | #endif 35 | /* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ 36 | 37 | -------------------------------------------------------------------------------- /deps/lua/etc/min.c: -------------------------------------------------------------------------------- 1 | /* 2 | * min.c -- a minimal Lua interpreter 3 | * loads stdin only with minimal error handling. 4 | * no interaction, and no standard library, only a "print" function. 5 | */ 6 | 7 | #include 8 | 9 | #include "lua.h" 10 | #include "lauxlib.h" 11 | 12 | static int print(lua_State *L) 13 | { 14 | int n=lua_gettop(L); 15 | int i; 16 | for (i=1; i<=n; i++) 17 | { 18 | if (i>1) printf("\t"); 19 | if (lua_isstring(L,i)) 20 | printf("%s",lua_tostring(L,i)); 21 | else if (lua_isnil(L,i)) 22 | printf("%s","nil"); 23 | else if (lua_isboolean(L,i)) 24 | printf("%s",lua_toboolean(L,i) ? "true" : "false"); 25 | else 26 | printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i)); 27 | } 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | int main(void) 33 | { 34 | lua_State *L=lua_open(); 35 | lua_register(L,"print",print); 36 | if (luaL_dofile(L,NULL)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1)); 37 | lua_close(L); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /deps/lua/doc/readme.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lua documentation 4 | 5 | 6 | 7 | 8 | 9 |
10 |

11 | Lua 12 | Documentation 13 |

14 | 15 | This is the documentation included in the source distribution of Lua 5.1.5. 16 | 17 | 25 | 26 | Lua's 27 | official web site 28 | contains updated documentation, 29 | especially the 30 | reference manual. 31 |

32 | 33 |


34 | 35 | Last update: 36 | Fri Feb 3 09:44:42 BRST 2012 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/base.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | void *base_alloc(size_t size); 13 | void *base_calloc(size_t number, size_t size); 14 | extent_node_t *base_node_alloc(void); 15 | void base_node_dealloc(extent_node_t *node); 16 | bool base_boot(void); 17 | void base_prefork(void); 18 | void base_postfork_parent(void); 19 | void base_postfork_child(void); 20 | 21 | #endif /* JEMALLOC_H_EXTERNS */ 22 | /******************************************************************************/ 23 | #ifdef JEMALLOC_H_INLINES 24 | 25 | #endif /* JEMALLOC_H_INLINES */ 26 | /******************************************************************************/ 27 | -------------------------------------------------------------------------------- /deps/lua/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (char* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | #ifdef luac_c 23 | /* print one chunk; from print.c */ 24 | LUAI_FUNC void luaU_print (const Proto* f, int full); 25 | #endif 26 | 27 | /* for header of binary files -- this is Lua 5.1 */ 28 | #define LUAC_VERSION 0x51 29 | 30 | /* for header of binary files -- this is the official format */ 31 | #define LUAC_FORMAT 0 32 | 33 | /* size of header of binary files */ 34 | #define LUAC_HEADERSIZE 12 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/test/test_repl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file : test_oplog.c 3 | * author : ning 4 | * date : 2014-09-24 10:05:34 5 | */ 6 | 7 | #include 8 | 9 | #include "nc_util.h" 10 | #include "testhelp.h" 11 | #include "../ndb_repl.c" 12 | 13 | /* 14 | * parse master info comman and get oplog.last 15 | */ 16 | static void 17 | test_repl_parse_master_info() 18 | { 19 | sds buf = sdsnew("#repl\r\n" \ 20 | "oplog.first:3\r\n" \ 21 | "oplog.last:5\r\n" ); 22 | 23 | sds value = repl_parse_master_info(buf, "oplog.last"); 24 | TEST_ASSERT("parse master info", 25 | strcmp(value, "5") == 0); 26 | sdsfree(value); 27 | 28 | value = repl_parse_master_info(buf, "oplog.last2"); 29 | TEST_ASSERT("parse master info", 30 | value == NULL); 31 | sdsfree(value); 32 | 33 | sdsfree(buf); 34 | } 35 | 36 | int 37 | main(int argc, const char **argv) 38 | { 39 | log_init(LOG_DEBUG, NULL); 40 | 41 | test_repl_parse_master_info(); 42 | 43 | test_report(); 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/util/nc_signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_SIGNAL_H_ 19 | #define _NC_SIGNAL_H_ 20 | 21 | #include "nc_util.h" 22 | 23 | struct signal { 24 | int signo; 25 | char *signame; 26 | int flags; 27 | void (*handler)(int signo); 28 | }; 29 | 30 | rstatus_t signal_init(void); 31 | void signal_deinit(void); 32 | void signal_handler(int signo); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /deps/lua/etc/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua etc 2 | 3 | TOP= .. 4 | LIB= $(TOP)/src 5 | INC= $(TOP)/src 6 | BIN= $(TOP)/src 7 | SRC= $(TOP)/src 8 | TST= $(TOP)/test 9 | 10 | CC= gcc 11 | CFLAGS= -O2 -Wall -I$(INC) $(MYCFLAGS) 12 | MYCFLAGS= 13 | MYLDFLAGS= -Wl,-E 14 | MYLIBS= -lm 15 | #MYLIBS= -lm -Wl,-E -ldl -lreadline -lhistory -lncurses 16 | RM= rm -f 17 | 18 | default: 19 | @echo 'Please choose a target: min noparser one strict clean' 20 | 21 | min: min.c 22 | $(CC) $(CFLAGS) $@.c -L$(LIB) -llua $(MYLIBS) 23 | echo 'print"Hello there!"' | ./a.out 24 | 25 | noparser: noparser.o 26 | $(CC) noparser.o $(SRC)/lua.o -L$(LIB) -llua $(MYLIBS) 27 | $(BIN)/luac $(TST)/hello.lua 28 | -./a.out luac.out 29 | -./a.out -e'a=1' 30 | 31 | one: 32 | $(CC) $(CFLAGS) all.c $(MYLIBS) 33 | ./a.out $(TST)/hello.lua 34 | 35 | strict: 36 | -$(BIN)/lua -e 'print(a);b=2' 37 | -$(BIN)/lua -lstrict -e 'print(a)' 38 | -$(BIN)/lua -e 'function f() b=2 end f()' 39 | -$(BIN)/lua -lstrict -e 'function f() b=2 end f()' 40 | 41 | clean: 42 | $(RM) a.out core core.* *.o luac.out 43 | 44 | .PHONY: default min noparser one strict clean 45 | -------------------------------------------------------------------------------- /deps/lua/etc/strict.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- strict.lua 3 | -- checks uses of undeclared global variables 4 | -- All global variables must be 'declared' through a regular assignment 5 | -- (even assigning nil will do) in a main chunk before being used 6 | -- anywhere or assigned to inside a function. 7 | -- 8 | 9 | local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget 10 | 11 | local mt = getmetatable(_G) 12 | if mt == nil then 13 | mt = {} 14 | setmetatable(_G, mt) 15 | end 16 | 17 | mt.__declared = {} 18 | 19 | local function what () 20 | local d = getinfo(3, "S") 21 | return d and d.what or "C" 22 | end 23 | 24 | mt.__newindex = function (t, n, v) 25 | if not mt.__declared[n] then 26 | local w = what() 27 | if w ~= "main" and w ~= "C" then 28 | error("assign to undeclared variable '"..n.."'", 2) 29 | end 30 | mt.__declared[n] = true 31 | end 32 | rawset(t, n, v) 33 | end 34 | 35 | mt.__index = function (t, n) 36 | if not mt.__declared[n] and what() ~= "C" then 37 | error("variable '"..n.."' is not declared", 2) 38 | end 39 | return rawget(t, n) 40 | end 41 | 42 | -------------------------------------------------------------------------------- /deps/lua/etc/README: -------------------------------------------------------------------------------- 1 | This directory contains some useful files and code. 2 | Unlike the code in ../src, everything here is in the public domain. 3 | 4 | If any of the makes fail, you're probably not using the same libraries 5 | used to build Lua. Set MYLIBS in Makefile accordingly. 6 | 7 | all.c 8 | Full Lua interpreter in a single file. 9 | Do "make one" for a demo. 10 | 11 | lua.hpp 12 | Lua header files for C++ using 'extern "C"'. 13 | 14 | lua.ico 15 | A Lua icon for Windows (and web sites: save as favicon.ico). 16 | Drawn by hand by Markus Gritsch . 17 | 18 | lua.pc 19 | pkg-config data for Lua 20 | 21 | luavs.bat 22 | Script to build Lua under "Visual Studio .NET Command Prompt". 23 | Run it from the toplevel as etc\luavs.bat. 24 | 25 | min.c 26 | A minimal Lua interpreter. 27 | Good for learning and for starting your own. 28 | Do "make min" for a demo. 29 | 30 | noparser.c 31 | Linking with noparser.o avoids loading the parsing modules in lualib.a. 32 | Do "make noparser" for a demo. 33 | 34 | strict.lua 35 | Traps uses of undeclared global variables. 36 | Do "make strict" for a demo. 37 | 38 | -------------------------------------------------------------------------------- /deps/jemalloc/src/extent.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_EXTENT_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | 6 | static inline int 7 | extent_szad_comp(extent_node_t *a, extent_node_t *b) 8 | { 9 | int ret; 10 | size_t a_size = a->size; 11 | size_t b_size = b->size; 12 | 13 | ret = (a_size > b_size) - (a_size < b_size); 14 | if (ret == 0) { 15 | uintptr_t a_addr = (uintptr_t)a->addr; 16 | uintptr_t b_addr = (uintptr_t)b->addr; 17 | 18 | ret = (a_addr > b_addr) - (a_addr < b_addr); 19 | } 20 | 21 | return (ret); 22 | } 23 | 24 | /* Generate red-black tree functions. */ 25 | rb_gen(, extent_tree_szad_, extent_tree_t, extent_node_t, link_szad, 26 | extent_szad_comp) 27 | 28 | static inline int 29 | extent_ad_comp(extent_node_t *a, extent_node_t *b) 30 | { 31 | uintptr_t a_addr = (uintptr_t)a->addr; 32 | uintptr_t b_addr = (uintptr_t)b->addr; 33 | 34 | return ((a_addr > b_addr) - (a_addr < b_addr)); 35 | } 36 | 37 | /* Generate red-black tree functions. */ 38 | rb_gen(, extent_tree_ad_, extent_tree_t, extent_node_t, link_ad, 39 | extent_ad_comp) 40 | -------------------------------------------------------------------------------- /src/ndb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : ndb.h 3 | * author : ning 4 | * date : 2014-07-31 16:49:17 5 | */ 6 | 7 | #ifndef _NDB_H_ 8 | #define _NDB_H_ 9 | 10 | typedef struct instance_s instance_t; 11 | 12 | #include "nc_util.h" 13 | #include "ndb_message.h" 14 | #include "ndb_leveldb.h" 15 | #include "ndb_oplog.h" 16 | #include "ndb_stat.h" 17 | #include "ndb_repl.h" 18 | #include "ndb_command.h" 19 | #include "ndb_job.h" 20 | #include "ndb_cursor.h" 21 | 22 | struct instance_s { 23 | server_t srv; 24 | store_t store; 25 | oplog_t oplog; 26 | stat_t stat; 27 | repl_t repl; 28 | 29 | bool daemonize; 30 | int loglevel; /* log level */ 31 | char *logfile; /* log filename */ 32 | pid_t pid; /* process id */ 33 | 34 | char *configfile; /* configuration filename */ 35 | nc_conf_t conf; 36 | 37 | uint32_t cronloops; /* number of times the cron function run */ 38 | }; 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /deps/lua/etc/luavs.bat: -------------------------------------------------------------------------------- 1 | @rem Script to build Lua under "Visual Studio .NET Command Prompt". 2 | @rem Do not run from this directory; run it from the toplevel: etc\luavs.bat . 3 | @rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src. 4 | @rem (contributed by David Manura and Mike Pall) 5 | 6 | @setlocal 7 | @set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE 8 | @set MYLINK=link /nologo 9 | @set MYMT=mt /nologo 10 | 11 | cd src 12 | %MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c 13 | del lua.obj luac.obj 14 | %MYLINK% /DLL /out:lua51.dll l*.obj 15 | if exist lua51.dll.manifest^ 16 | %MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2 17 | %MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c 18 | %MYLINK% /out:lua.exe lua.obj lua51.lib 19 | if exist lua.exe.manifest^ 20 | %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe 21 | %MYCOMPILE% l*.c print.c 22 | del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^ 23 | loslib.obj ltablib.obj lstrlib.obj loadlib.obj 24 | %MYLINK% /out:luac.exe *.obj 25 | if exist luac.exe.manifest^ 26 | %MYMT% -manifest luac.exe.manifest -outputresource:luac.exe 27 | del *.obj *.manifest 28 | cd .. 29 | -------------------------------------------------------------------------------- /deps/lua/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg (lua_State *L); 30 | LUAI_FUNC int luaG_checkcode (const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop (Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /deps/lua/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /deps/lua/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* Key to file-handle type */ 15 | #define LUA_FILEHANDLE "FILE*" 16 | 17 | 18 | #define LUA_COLIBNAME "coroutine" 19 | LUALIB_API int (luaopen_base) (lua_State *L); 20 | 21 | #define LUA_TABLIBNAME "table" 22 | LUALIB_API int (luaopen_table) (lua_State *L); 23 | 24 | #define LUA_IOLIBNAME "io" 25 | LUALIB_API int (luaopen_io) (lua_State *L); 26 | 27 | #define LUA_OSLIBNAME "os" 28 | LUALIB_API int (luaopen_os) (lua_State *L); 29 | 30 | #define LUA_STRLIBNAME "string" 31 | LUALIB_API int (luaopen_string) (lua_State *L); 32 | 33 | #define LUA_MATHLIBNAME "math" 34 | LUALIB_API int (luaopen_math) (lua_State *L); 35 | 36 | #define LUA_DBLIBNAME "debug" 37 | LUALIB_API int (luaopen_debug) (lua_State *L); 38 | 39 | #define LUA_LOADLIBNAME "package" 40 | LUALIB_API int (luaopen_package) (lua_State *L); 41 | 42 | 43 | /* open all previous libraries */ 44 | LUALIB_API void (luaL_openlibs) (lua_State *L); 45 | 46 | 47 | 48 | #ifndef lua_assert 49 | #define lua_assert(x) ((void)0) 50 | #endif 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /deps/lua/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /deps/lua/test/README: -------------------------------------------------------------------------------- 1 | These are simple tests for Lua. Some of them contain useful code. 2 | They are meant to be run to make sure Lua is built correctly and also 3 | to be read, to see how Lua programs look. 4 | 5 | Here is a one-line summary of each program: 6 | 7 | bisect.lua bisection method for solving non-linear equations 8 | cf.lua temperature conversion table (celsius to farenheit) 9 | echo.lua echo command line arguments 10 | env.lua environment variables as automatic global variables 11 | factorial.lua factorial without recursion 12 | fib.lua fibonacci function with cache 13 | fibfor.lua fibonacci numbers with coroutines and generators 14 | globals.lua report global variable usage 15 | hello.lua the first program in every language 16 | life.lua Conway's Game of Life 17 | luac.lua bare-bones luac 18 | printf.lua an implementation of printf 19 | readonly.lua make global variables readonly 20 | sieve.lua the sieve of of Eratosthenes programmed with coroutines 21 | sort.lua two implementations of a sort function 22 | table.lua make table, grouping all data for the same item 23 | trace-calls.lua trace calls 24 | trace-globals.lua trace assigments to global variables 25 | xd.lua hex dump 26 | 27 | -------------------------------------------------------------------------------- /src/util/nc_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : nc_server.h 3 | * author : ning 4 | * date : 2014-07-29 15:30:55 5 | */ 6 | 7 | #ifndef _NC_SERVER_H_ 8 | #define _NC_SERVER_H_ 9 | 10 | #include "nc_util.h" 11 | 12 | /* TODO: this file need modify */ 13 | 14 | typedef struct server_s { 15 | void *owner; /* owner */ 16 | char *listen; /* listen host:port */ 17 | int backlog; 18 | struct event_base *evb; /* event base */ 19 | int ev_timeout; /* timeout for event_wait */ 20 | int mbuf_size; /* mbuf_size, TOOD: not used yet */ 21 | struct conn *conn; /* listen conn */ 22 | 23 | conn_callback_t recv_done; /* recv done handler */ 24 | conn_callback_t send_done; /* send done handler */ 25 | cron_callback_t cron_callback; /* cron handler */ 26 | } server_t; 27 | 28 | rstatus_t server_init(void *owner, server_t *srv, 29 | conn_callback_t recv_done, conn_callback_t send_done, cron_callback_t cron_callback); 30 | rstatus_t server_deinit(server_t *srv); 31 | rstatus_t server_run(server_t *srv); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /deps/lua/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 26 | LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 27 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 28 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 29 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 30 | StkId val); 31 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 32 | StkId val); 33 | LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 34 | LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /deps/lua/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.nk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 22 | LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 28 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 29 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 30 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 31 | LUAI_FUNC int luaH_getn (Table *t); 32 | 33 | 34 | #if defined(LUA_DEBUG) 35 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 36 | LUAI_FUNC int luaH_isdummy (Node *n); 37 | #endif 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/chunk_dss.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef enum { 5 | dss_prec_disabled = 0, 6 | dss_prec_primary = 1, 7 | dss_prec_secondary = 2, 8 | 9 | dss_prec_limit = 3 10 | } dss_prec_t ; 11 | #define DSS_PREC_DEFAULT dss_prec_secondary 12 | #define DSS_DEFAULT "secondary" 13 | 14 | #endif /* JEMALLOC_H_TYPES */ 15 | /******************************************************************************/ 16 | #ifdef JEMALLOC_H_STRUCTS 17 | 18 | extern const char *dss_prec_names[]; 19 | 20 | #endif /* JEMALLOC_H_STRUCTS */ 21 | /******************************************************************************/ 22 | #ifdef JEMALLOC_H_EXTERNS 23 | 24 | dss_prec_t chunk_dss_prec_get(void); 25 | bool chunk_dss_prec_set(dss_prec_t dss_prec); 26 | void *chunk_alloc_dss(size_t size, size_t alignment, bool *zero); 27 | bool chunk_in_dss(void *chunk); 28 | bool chunk_dss_boot(void); 29 | void chunk_dss_prefork(void); 30 | void chunk_dss_postfork_parent(void); 31 | void chunk_dss_postfork_child(void); 32 | 33 | #endif /* JEMALLOC_H_EXTERNS */ 34 | /******************************************************************************/ 35 | #ifdef JEMALLOC_H_INLINES 36 | 37 | #endif /* JEMALLOC_H_INLINES */ 38 | /******************************************************************************/ 39 | -------------------------------------------------------------------------------- /deps/jemalloc/test/mremap.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | int 5 | main(void) 6 | { 7 | int ret, err; 8 | size_t sz, lg_chunk, chunksize, i; 9 | char *p, *q; 10 | 11 | malloc_printf("Test begin\n"); 12 | 13 | sz = sizeof(lg_chunk); 14 | if ((err = mallctl("opt.lg_chunk", &lg_chunk, &sz, NULL, 0))) { 15 | assert(err != ENOENT); 16 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 17 | strerror(err)); 18 | ret = 1; 19 | goto label_return; 20 | } 21 | chunksize = ((size_t)1U) << lg_chunk; 22 | 23 | p = (char *)malloc(chunksize); 24 | if (p == NULL) { 25 | malloc_printf("malloc(%zu) --> %p\n", chunksize, p); 26 | ret = 1; 27 | goto label_return; 28 | } 29 | memset(p, 'a', chunksize); 30 | 31 | q = (char *)realloc(p, chunksize * 2); 32 | if (q == NULL) { 33 | malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize * 2, 34 | q); 35 | ret = 1; 36 | goto label_return; 37 | } 38 | for (i = 0; i < chunksize; i++) { 39 | assert(q[i] == 'a'); 40 | } 41 | 42 | p = q; 43 | 44 | q = (char *)realloc(p, chunksize); 45 | if (q == NULL) { 46 | malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize, q); 47 | ret = 1; 48 | goto label_return; 49 | } 50 | for (i = 0; i < chunksize; i++) { 51 | assert(q[i] == 'a'); 52 | } 53 | 54 | free(q); 55 | 56 | ret = 0; 57 | label_return: 58 | malloc_printf("Test end\n"); 59 | return (ret); 60 | } 61 | -------------------------------------------------------------------------------- /deps/hiredis/bench3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "hiredis.h" 7 | 8 | /*int tread(int fd, char*buf, int len){*/ 9 | /*int readed = 0;*/ 10 | /*while(readederr) { 34 | printf("Connection error: %s\n", c->errstr); 35 | exit(1); 36 | } 37 | 38 | char *cmd = "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$9\r\nbarbarbar\r\n"; 39 | int len = strlen(cmd); 40 | 41 | char buf[1024]; 42 | for(i=0; i<100*1000; i++){ 43 | ret = twrite(c->fd, cmd, len); 44 | assert(len == ret); 45 | 46 | /*fprintf(stderr, "read\n");*/ 47 | ret = tread(c->fd, buf, 5); 48 | assert(5 == ret); 49 | 50 | buf[5] = 0; 51 | /*fprintf(stderr, "%d: %s\n", i, buf);*/ 52 | /*assert(0 == strcmp(buf, "+OK\r\n"));*/ 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /deps/lua/etc/noparser.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The code below can be used to make a Lua core that does not contain the 3 | * parsing modules (lcode, llex, lparser), which represent 35% of the total core. 4 | * You'll only be able to load binary files and strings, precompiled with luac. 5 | * (Of course, you'll have to build luac with the original parsing modules!) 6 | * 7 | * To use this module, simply compile it ("make noparser" does that) and list 8 | * its object file before the Lua libraries. The linker should then not load 9 | * the parsing modules. To try it, do "make luab". 10 | * 11 | * If you also want to avoid the dump module (ldump.o), define NODUMP. 12 | * #define NODUMP 13 | */ 14 | 15 | #define LUA_CORE 16 | 17 | #include "llex.h" 18 | #include "lparser.h" 19 | #include "lzio.h" 20 | 21 | LUAI_FUNC void luaX_init (lua_State *L) { 22 | UNUSED(L); 23 | } 24 | 25 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { 26 | UNUSED(z); 27 | UNUSED(buff); 28 | UNUSED(name); 29 | lua_pushliteral(L,"parser not loaded"); 30 | lua_error(L); 31 | return NULL; 32 | } 33 | 34 | #ifdef NODUMP 35 | #include "lundump.h" 36 | 37 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) { 38 | UNUSED(f); 39 | UNUSED(w); 40 | UNUSED(data); 41 | UNUSED(strip); 42 | #if 1 43 | UNUSED(L); 44 | return 0; 45 | #else 46 | lua_pushliteral(L,"dumper not loaded"); 47 | lua_error(L); 48 | #endif 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /deps/lua/README: -------------------------------------------------------------------------------- 1 | README for Lua 5.1 2 | 3 | See INSTALL for installation instructions. 4 | See HISTORY for a summary of changes since the last released version. 5 | 6 | * What is Lua? 7 | ------------ 8 | Lua is a powerful, light-weight programming language designed for extending 9 | applications. Lua is also frequently used as a general-purpose, stand-alone 10 | language. Lua is free software. 11 | 12 | For complete information, visit Lua's web site at http://www.lua.org/ . 13 | For an executive summary, see http://www.lua.org/about.html . 14 | 15 | Lua has been used in many different projects around the world. 16 | For a short list, see http://www.lua.org/uses.html . 17 | 18 | * Availability 19 | ------------ 20 | Lua is freely available for both academic and commercial purposes. 21 | See COPYRIGHT and http://www.lua.org/license.html for details. 22 | Lua can be downloaded at http://www.lua.org/download.html . 23 | 24 | * Installation 25 | ------------ 26 | Lua is implemented in pure ANSI C, and compiles unmodified in all known 27 | platforms that have an ANSI C compiler. In most Unix-like platforms, simply 28 | do "make" with a suitable target. See INSTALL for detailed instructions. 29 | 30 | * Origin 31 | ------ 32 | Lua is developed at Lua.org, a laboratory of the Department of Computer 33 | Science of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro 34 | in Brazil). 35 | For more information about the authors, see http://www.lua.org/authors.html . 36 | 37 | (end of README) 38 | -------------------------------------------------------------------------------- /src/ndb_repl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : ndb_repl.h 3 | * author : ning 4 | * date : 2014-09-26 12:49:26 5 | */ 6 | 7 | #ifndef _NDB_REPL_H_ 8 | #define _NDB_REPL_H_ 9 | 10 | #include "nc_util.h" 11 | #include "hiredis.h" 12 | 13 | typedef struct repl_s { 14 | void *owner; /* instance */ 15 | 16 | redisContext *conn; /* connection to master */ 17 | char *master; /* master info: host:port */ 18 | uint64_t repl_opid; /* replcation pos (the current last opid fetched from master) */ 19 | uint32_t connect_timeout; /* timeout in ms */ 20 | uint32_t connect_retry; /* connect retry */ 21 | uint32_t sleep_time; /* in ms */ 22 | 23 | /* TODO: we may do not need a file, we can save it in leveldb, 24 | * it's easy to write to leveldb, 25 | * but it's not readable by human */ 26 | char *info_path; /* file to save repl info */ 27 | } repl_t; 28 | 29 | typedef enum repl_role_s { 30 | REPL_ROLE_MASTER, 31 | REPL_ROLE_SLAVE, 32 | } repl_role_t; 33 | 34 | rstatus_t repl_init(void *owner, repl_t *repl); 35 | rstatus_t repl_deinit(repl_t *repl); 36 | 37 | rstatus_t repl_start(repl_t *repl); 38 | rstatus_t repl_stop(repl_t *repl); 39 | rstatus_t repl_set_master(repl_t *repl, char *master); 40 | rstatus_t repl_info_flush(repl_t *repl); 41 | rstatus_t repl_run(repl_t *repl); 42 | repl_role_t repl_role(repl_t *repl); 43 | 44 | #endif 45 | /* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ 46 | 47 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/extent.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct extent_node_s extent_node_t; 5 | 6 | #endif /* JEMALLOC_H_TYPES */ 7 | /******************************************************************************/ 8 | #ifdef JEMALLOC_H_STRUCTS 9 | 10 | /* Tree of extents. */ 11 | struct extent_node_s { 12 | /* Linkage for the size/address-ordered tree. */ 13 | rb_node(extent_node_t) link_szad; 14 | 15 | /* Linkage for the address-ordered tree. */ 16 | rb_node(extent_node_t) link_ad; 17 | 18 | /* Profile counters, used for huge objects. */ 19 | prof_ctx_t *prof_ctx; 20 | 21 | /* Pointer to the extent that this tree node is responsible for. */ 22 | void *addr; 23 | 24 | /* Total region size. */ 25 | size_t size; 26 | 27 | /* True if zero-filled; used by chunk recycling code. */ 28 | bool zeroed; 29 | }; 30 | typedef rb_tree(extent_node_t) extent_tree_t; 31 | 32 | #endif /* JEMALLOC_H_STRUCTS */ 33 | /******************************************************************************/ 34 | #ifdef JEMALLOC_H_EXTERNS 35 | 36 | rb_proto(, extent_tree_szad_, extent_tree_t, extent_node_t) 37 | 38 | rb_proto(, extent_tree_ad_, extent_tree_t, extent_node_t) 39 | 40 | #endif /* JEMALLOC_H_EXTERNS */ 41 | /******************************************************************************/ 42 | #ifdef JEMALLOC_H_INLINES 43 | 44 | #endif /* JEMALLOC_H_INLINES */ 45 | /******************************************************************************/ 46 | 47 | -------------------------------------------------------------------------------- /deps/hiredis/example-libev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "hiredis.h" 6 | #include "async.h" 7 | #include "adapters/libev.h" 8 | 9 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 10 | redisReply *reply = r; 11 | if (reply == NULL) return; 12 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 13 | 14 | /* Disconnect after receiving the reply to GET */ 15 | redisAsyncDisconnect(c); 16 | } 17 | 18 | void connectCallback(const redisAsyncContext *c, int status) { 19 | if (status != REDIS_OK) { 20 | printf("Error: %s\n", c->errstr); 21 | return; 22 | } 23 | printf("Connected...\n"); 24 | } 25 | 26 | void disconnectCallback(const redisAsyncContext *c, int status) { 27 | if (status != REDIS_OK) { 28 | printf("Error: %s\n", c->errstr); 29 | return; 30 | } 31 | printf("Disconnected...\n"); 32 | } 33 | 34 | int main (int argc, char **argv) { 35 | signal(SIGPIPE, SIG_IGN); 36 | 37 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 38 | if (c->err) { 39 | /* Let *c leak for now... */ 40 | printf("Error: %s\n", c->errstr); 41 | return 1; 42 | } 43 | 44 | redisLibevAttach(EV_DEFAULT_ c); 45 | redisAsyncSetConnectCallback(c,connectCallback); 46 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 47 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 48 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 49 | ev_loop(EV_DEFAULT_ 0); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/huge.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | /* Huge allocation statistics. */ 13 | extern uint64_t huge_nmalloc; 14 | extern uint64_t huge_ndalloc; 15 | extern size_t huge_allocated; 16 | 17 | /* Protects chunk-related data structures. */ 18 | extern malloc_mutex_t huge_mtx; 19 | 20 | void *huge_malloc(size_t size, bool zero); 21 | void *huge_palloc(size_t size, size_t alignment, bool zero); 22 | void *huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, 23 | size_t extra); 24 | void *huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra, 25 | size_t alignment, bool zero, bool try_tcache_dalloc); 26 | void huge_dalloc(void *ptr, bool unmap); 27 | size_t huge_salloc(const void *ptr); 28 | prof_ctx_t *huge_prof_ctx_get(const void *ptr); 29 | void huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx); 30 | bool huge_boot(void); 31 | void huge_prefork(void); 32 | void huge_postfork_parent(void); 33 | void huge_postfork_child(void); 34 | 35 | #endif /* JEMALLOC_H_EXTERNS */ 36 | /******************************************************************************/ 37 | #ifdef JEMALLOC_H_INLINES 38 | 39 | #endif /* JEMALLOC_H_INLINES */ 40 | /******************************************************************************/ 41 | -------------------------------------------------------------------------------- /deps/lua/COPYRIGHT: -------------------------------------------------------------------------------- 1 | Lua License 2 | ----------- 3 | 4 | Lua is licensed under the terms of the MIT license reproduced below. 5 | This means that Lua is free software and can be used for both academic 6 | and commercial purposes at absolutely no cost. 7 | 8 | For details and rationale, see http://www.lua.org/license.html . 9 | 10 | =============================================================================== 11 | 12 | Copyright (C) 1994-2012 Lua.org, PUC-Rio. 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy 15 | of this software and associated documentation files (the "Software"), to deal 16 | in the Software without restriction, including without limitation the rights 17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | copies of the Software, and to permit persons to whom the Software is 19 | furnished to do so, subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in 22 | all copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | THE SOFTWARE. 31 | 32 | =============================================================================== 33 | 34 | (end of COPYRIGHT) 35 | -------------------------------------------------------------------------------- /deps/jemalloc/test/ALLOCM_ARENA.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define NTHREADS 10 5 | 6 | void * 7 | je_thread_start(void *arg) 8 | { 9 | unsigned thread_ind = (unsigned)(uintptr_t)arg; 10 | unsigned arena_ind; 11 | int r; 12 | void *p; 13 | size_t rsz, sz; 14 | 15 | sz = sizeof(arena_ind); 16 | if (mallctl("arenas.extend", &arena_ind, &sz, NULL, 0) 17 | != 0) { 18 | malloc_printf("Error in arenas.extend\n"); 19 | abort(); 20 | } 21 | 22 | if (thread_ind % 4 != 3) { 23 | size_t mib[3]; 24 | size_t miblen = sizeof(mib) / sizeof(size_t); 25 | const char *dss_precs[] = {"disabled", "primary", "secondary"}; 26 | const char *dss = dss_precs[thread_ind % 4]; 27 | if (mallctlnametomib("arena.0.dss", mib, &miblen) != 0) { 28 | malloc_printf("Error in mallctlnametomib()\n"); 29 | abort(); 30 | } 31 | mib[1] = arena_ind; 32 | if (mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss, 33 | sizeof(const char *))) { 34 | malloc_printf("Error in mallctlbymib()\n"); 35 | abort(); 36 | } 37 | } 38 | 39 | r = allocm(&p, &rsz, 1, ALLOCM_ARENA(arena_ind)); 40 | if (r != ALLOCM_SUCCESS) { 41 | malloc_printf("Unexpected allocm() error\n"); 42 | abort(); 43 | } 44 | 45 | return (NULL); 46 | } 47 | 48 | int 49 | main(void) 50 | { 51 | je_thread_t threads[NTHREADS]; 52 | unsigned i; 53 | 54 | malloc_printf("Test begin\n"); 55 | 56 | for (i = 0; i < NTHREADS; i++) { 57 | je_thread_create(&threads[i], je_thread_start, 58 | (void *)(uintptr_t)i); 59 | } 60 | 61 | for (i = 0; i < NTHREADS; i++) 62 | je_thread_join(threads[i], NULL); 63 | 64 | malloc_printf("Test end\n"); 65 | return (0); 66 | } 67 | -------------------------------------------------------------------------------- /deps/lua/doc/lua.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: #000000 ; 3 | background-color: #FFFFFF ; 4 | font-family: Helvetica, Arial, sans-serif ; 5 | text-align: justify ; 6 | margin-right: 30px ; 7 | margin-left: 30px ; 8 | } 9 | 10 | h1, h2, h3, h4 { 11 | font-family: Verdana, Geneva, sans-serif ; 12 | font-weight: normal ; 13 | font-style: italic ; 14 | } 15 | 16 | h2 { 17 | padding-top: 0.4em ; 18 | padding-bottom: 0.4em ; 19 | padding-left: 30px ; 20 | padding-right: 30px ; 21 | margin-left: -30px ; 22 | background-color: #E0E0FF ; 23 | } 24 | 25 | h3 { 26 | padding-left: 0.5em ; 27 | border-left: solid #E0E0FF 1em ; 28 | } 29 | 30 | table h3 { 31 | padding-left: 0px ; 32 | border-left: none ; 33 | } 34 | 35 | a:link { 36 | color: #000080 ; 37 | background-color: inherit ; 38 | text-decoration: none ; 39 | } 40 | 41 | a:visited { 42 | background-color: inherit ; 43 | text-decoration: none ; 44 | } 45 | 46 | a:link:hover, a:visited:hover { 47 | color: #000080 ; 48 | background-color: #E0E0FF ; 49 | } 50 | 51 | a:link:active, a:visited:active { 52 | color: #FF0000 ; 53 | } 54 | 55 | hr { 56 | border: 0 ; 57 | height: 1px ; 58 | color: #a0a0a0 ; 59 | background-color: #a0a0a0 ; 60 | } 61 | 62 | :target { 63 | background-color: #F8F8F8 ; 64 | padding: 8px ; 65 | border: solid #a0a0a0 2px ; 66 | } 67 | 68 | .footer { 69 | color: gray ; 70 | font-size: small ; 71 | } 72 | 73 | input[type=text] { 74 | border: solid #a0a0a0 2px ; 75 | border-radius: 2em ; 76 | -moz-border-radius: 2em ; 77 | background-image: url('images/search.png') ; 78 | background-repeat: no-repeat; 79 | background-position: 4px center ; 80 | padding-left: 20px ; 81 | height: 2em ; 82 | } 83 | 84 | -------------------------------------------------------------------------------- /deps/hiredis/example-libevent.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "hiredis.h" 6 | #include "async.h" 7 | #include "adapters/libevent.h" 8 | 9 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 10 | redisReply *reply = r; 11 | if (reply == NULL) return; 12 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 13 | 14 | /* Disconnect after receiving the reply to GET */ 15 | redisAsyncDisconnect(c); 16 | } 17 | 18 | void connectCallback(const redisAsyncContext *c, int status) { 19 | if (status != REDIS_OK) { 20 | printf("Error: %s\n", c->errstr); 21 | return; 22 | } 23 | printf("Connected...\n"); 24 | } 25 | 26 | void disconnectCallback(const redisAsyncContext *c, int status) { 27 | if (status != REDIS_OK) { 28 | printf("Error: %s\n", c->errstr); 29 | return; 30 | } 31 | printf("Disconnected...\n"); 32 | } 33 | 34 | int main (int argc, char **argv) { 35 | signal(SIGPIPE, SIG_IGN); 36 | struct event_base *base = event_base_new(); 37 | 38 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 39 | if (c->err) { 40 | /* Let *c leak for now... */ 41 | printf("Error: %s\n", c->errstr); 42 | return 1; 43 | } 44 | 45 | redisLibeventAttach(c,base); 46 | redisAsyncSetConnectCallback(c,connectCallback); 47 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 48 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 49 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 50 | event_base_dispatch(base); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /deps/hiredis/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2011, Salvatore Sanfilippo 2 | Copyright (c) 2010-2011, Pieter Noordhuis 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of Redis nor the names of its contributors may be used 17 | to endorse or promote products derived from this software without specific 18 | prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 24 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 27 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /deps/lua/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig (lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /src/ndb_leveldb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : ndb_leveldb.h 3 | * author : ning 4 | * date : 2014-08-01 09:00:10 5 | */ 6 | 7 | #ifndef _NDB_LEVELDB_H_ 8 | #define _NDB_LEVELDB_H_ 9 | 10 | #include "nc_util.h" 11 | #include 12 | 13 | typedef struct store_s { 14 | void *owner; /* instance */ 15 | char *dbpath; 16 | size_t block_size; 17 | size_t cache_size; 18 | size_t write_buffer_size; 19 | 20 | int compression; 21 | int read_verify_checksum; 22 | int write_sync; 23 | 24 | leveldb_t *db; 25 | leveldb_comparator_t *cmp; 26 | leveldb_cache_t *cache; 27 | leveldb_env_t *env; 28 | leveldb_options_t *options; 29 | leveldb_readoptions_t *roptions; 30 | leveldb_writeoptions_t *woptions; 31 | } store_t; 32 | 33 | rstatus_t store_init(void *owner, store_t *s); 34 | rstatus_t store_deinit(store_t *s); 35 | 36 | rstatus_t store_get(store_t *s, sds key, sds *val, int64_t *expire); 37 | rstatus_t store_set(store_t *s, sds key, sds val, int64_t expire); 38 | rstatus_t store_del(store_t *s, sds key); 39 | rstatus_t store_compact(store_t *s); 40 | rstatus_t store_drop(store_t *s); 41 | sds store_info(store_t *s); 42 | 43 | typedef rstatus_t (*scan_callback_t)(store_t *s, sds key, sds val, uint64_t expire); 44 | rstatus_t store_scan(store_t *s, scan_callback_t callback); 45 | rstatus_t store_eliminate(store_t *s); 46 | 47 | #define STORE_DEFAULT_EXPIRE 0 48 | #define STORE_NS_KV "S" 49 | 50 | #endif 51 | 52 | -------------------------------------------------------------------------------- /deps/jemalloc/src/rtree.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_RTREE_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | rtree_t * 5 | rtree_new(unsigned bits) 6 | { 7 | rtree_t *ret; 8 | unsigned bits_per_level, height, i; 9 | 10 | bits_per_level = ffs(pow2_ceil((RTREE_NODESIZE / sizeof(void *)))) - 1; 11 | height = bits / bits_per_level; 12 | if (height * bits_per_level != bits) 13 | height++; 14 | assert(height * bits_per_level >= bits); 15 | 16 | ret = (rtree_t*)base_alloc(offsetof(rtree_t, level2bits) + 17 | (sizeof(unsigned) * height)); 18 | if (ret == NULL) 19 | return (NULL); 20 | memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) * 21 | height)); 22 | 23 | if (malloc_mutex_init(&ret->mutex)) { 24 | /* Leak the rtree. */ 25 | return (NULL); 26 | } 27 | ret->height = height; 28 | if (bits_per_level * height > bits) 29 | ret->level2bits[0] = bits % bits_per_level; 30 | else 31 | ret->level2bits[0] = bits_per_level; 32 | for (i = 1; i < height; i++) 33 | ret->level2bits[i] = bits_per_level; 34 | 35 | ret->root = (void**)base_alloc(sizeof(void *) << ret->level2bits[0]); 36 | if (ret->root == NULL) { 37 | /* 38 | * We leak the rtree here, since there's no generic base 39 | * deallocation. 40 | */ 41 | return (NULL); 42 | } 43 | memset(ret->root, 0, sizeof(void *) << ret->level2bits[0]); 44 | 45 | return (ret); 46 | } 47 | 48 | void 49 | rtree_prefork(rtree_t *rtree) 50 | { 51 | 52 | malloc_mutex_prefork(&rtree->mutex); 53 | } 54 | 55 | void 56 | rtree_postfork_parent(rtree_t *rtree) 57 | { 58 | 59 | malloc_mutex_postfork_parent(&rtree->mutex); 60 | } 61 | 62 | void 63 | rtree_postfork_child(rtree_t *rtree) 64 | { 65 | 66 | malloc_mutex_postfork_child(&rtree->mutex); 67 | } 68 | -------------------------------------------------------------------------------- /deps/hiredis/example-ae.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "hiredis.h" 6 | #include "async.h" 7 | #include "adapters/ae.h" 8 | 9 | /* Put event loop in the global scope, so it can be explicitly stopped */ 10 | static aeEventLoop *loop; 11 | 12 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 13 | redisReply *reply = r; 14 | if (reply == NULL) return; 15 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 16 | 17 | /* Disconnect after receiving the reply to GET */ 18 | redisAsyncDisconnect(c); 19 | } 20 | 21 | void connectCallback(const redisAsyncContext *c, int status) { 22 | if (status != REDIS_OK) { 23 | printf("Error: %s\n", c->errstr); 24 | return; 25 | } 26 | printf("Connected...\n"); 27 | } 28 | 29 | void disconnectCallback(const redisAsyncContext *c, int status) { 30 | if (status != REDIS_OK) { 31 | printf("Error: %s\n", c->errstr); 32 | return; 33 | } 34 | printf("Disconnected...\n"); 35 | } 36 | 37 | int main (int argc, char **argv) { 38 | signal(SIGPIPE, SIG_IGN); 39 | 40 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 41 | if (c->err) { 42 | /* Let *c leak for now... */ 43 | printf("Error: %s\n", c->errstr); 44 | return 1; 45 | } 46 | 47 | loop = aeCreateEventLoop(); 48 | redisAeAttach(loop, c); 49 | redisAsyncSetConnectCallback(c,connectCallback); 50 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 51 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 52 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 53 | aeMain(loop); 54 | return 0; 55 | } 56 | 57 | -------------------------------------------------------------------------------- /src/util/nc_file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * file : file.h 3 | * author : ning 4 | * date : 2014-04-24 09:32:02 5 | */ 6 | 7 | #ifndef _FILE_H_ 8 | #define _FILE_H_ 9 | 10 | #include "nc_util.h" 11 | #include "sds.h" 12 | 13 | static inline bool 14 | fs_exists(const char *filename) 15 | { 16 | struct stat st; 17 | 18 | return stat(filename, &st) == 0; 19 | } 20 | 21 | static inline bool 22 | fs_is_dir(const char *filename) 23 | { 24 | struct stat st; 25 | 26 | if (stat(filename, &st) == -1) { 27 | return false; 28 | } 29 | return (bool)S_ISDIR(st.st_mode); 30 | } 31 | 32 | static inline bool 33 | fs_is_file(const char *filename){ 34 | struct stat st; 35 | 36 | if (stat(filename, &st) == -1) { 37 | return false; 38 | } 39 | 40 | return (bool)S_ISREG(st.st_mode); 41 | } 42 | 43 | static inline off_t 44 | fs_file_size(const char *filename){ 45 | struct stat st; 46 | 47 | if (stat(filename, &st) == -1) { 48 | return -1; 49 | } 50 | 51 | return st.st_size; 52 | } 53 | 54 | static inline sds 55 | fs_file_content(const char *filename){ 56 | sds content; 57 | off_t size; 58 | off_t readed = 0; 59 | off_t n; 60 | int fd; 61 | 62 | size = fs_file_size(filename); 63 | if (size < 0) { 64 | return NULL; 65 | } 66 | 67 | fd = open(filename, O_RDONLY); 68 | if (fd < 0) { 69 | log_error("open(%s) failed: %s", filename, strerror(errno)); 70 | return NULL; 71 | } 72 | 73 | content = sdsnewlen(NULL, size); 74 | 75 | while (readed < size) { 76 | n = read(fd, content + readed, size - readed); 77 | if (n < 0) { 78 | sdsfree(content); 79 | return NULL; 80 | } 81 | readed += n; 82 | } 83 | 84 | return content; 85 | } 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /deps/lua/test/sort.lua: -------------------------------------------------------------------------------- 1 | -- two implementations of a sort function 2 | -- this is an example only. Lua has now a built-in function "sort" 3 | 4 | -- extracted from Programming Pearls, page 110 5 | function qsort(x,l,u,f) 6 | if ly end) 58 | show("after reverse selection sort",x) 59 | qsort(x,1,n,function (x,y) return x 2 | #include 3 | #include 4 | #include 5 | #include "hiredis.h" 6 | #include "async.h" 7 | #include "adapters/ae.h" 8 | 9 | /* Put event loop in the global scope, so it can be explicitly stopped */ 10 | static aeEventLoop *loop; 11 | 12 | void setCallback(redisAsyncContext *c, void *r, void *privdata) { 13 | redisReply *reply = r; 14 | if (reply == NULL) return; 15 | int * pi = (int*) privdata; 16 | 17 | /*printf("argv[%d]: %s\n", *pi, reply->str);*/ 18 | 19 | (*pi)++; 20 | if (*pi > 100*1000) 21 | exit(0); 22 | 23 | redisAsyncCommand(c, setCallback, (char*)pi, "SET thekey %s", "xxxxxxxxxxxxxx"); 24 | } 25 | 26 | void connectCallback(const redisAsyncContext *c, int status) { 27 | if (status != REDIS_OK) { 28 | printf("Error: %s\n", c->errstr); 29 | return; 30 | } 31 | printf("Connected...\n"); 32 | } 33 | 34 | void disconnectCallback(const redisAsyncContext *c, int status) { 35 | if (status != REDIS_OK) { 36 | printf("Error: %s\n", c->errstr); 37 | return; 38 | } 39 | printf("Disconnected...\n"); 40 | } 41 | 42 | int main() { 43 | signal(SIGPIPE, SIG_IGN); 44 | 45 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 22000); 46 | if (c->err) { 47 | /* Let *c leak for now... */ 48 | printf("Error: %s\n", c->errstr); 49 | return 1; 50 | } 51 | 52 | loop = aeCreateEventLoop(1000); 53 | redisAeAttach(loop, c); 54 | redisAsyncSetConnectCallback(c,connectCallback); 55 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 56 | 57 | int i = 0; 58 | redisAsyncCommand(c, setCallback, (char*)&i, "SET thekey %s", "xxxxxxxxxxxxxx"); 59 | aeMain(loop); 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /deps/jemalloc/COPYING: -------------------------------------------------------------------------------- 1 | Unless otherwise specified, files in the jemalloc source distribution are 2 | subject to the following license: 3 | -------------------------------------------------------------------------------- 4 | Copyright (C) 2002-2012 Jason Evans . 5 | All rights reserved. 6 | Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved. 7 | Copyright (C) 2009-2012 Facebook, Inc. All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions are met: 11 | 1. Redistributions of source code must retain the above copyright notice(s), 12 | this list of conditions and the following disclaimer. 13 | 2. Redistributions in binary form must reproduce the above copyright notice(s), 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS 18 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- 28 | -------------------------------------------------------------------------------- /deps/lua/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define char2int(c) cast(int, cast(unsigned char, (c))) 21 | 22 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/util/nc_rbtree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_RBTREE_ 19 | #define _NC_RBTREE_ 20 | 21 | #define rbtree_red(_node) ((_node)->color = 1) 22 | #define rbtree_black(_node) ((_node)->color = 0) 23 | #define rbtree_is_red(_node) ((_node)->color) 24 | #define rbtree_is_black(_node) (!rbtree_is_red(_node)) 25 | #define rbtree_copy_color(_n1, _n2) ((_n1)->color = (_n2)->color) 26 | 27 | struct rbnode { 28 | struct rbnode *left; /* left link */ 29 | struct rbnode *right; /* right link */ 30 | struct rbnode *parent; /* parent link */ 31 | int64_t key; /* key for ordering */ 32 | void *data; /* opaque data */ 33 | uint8_t color; /* red | black */ 34 | }; 35 | 36 | struct rbtree { 37 | struct rbnode *root; /* root node */ 38 | struct rbnode *sentinel; /* nil node */ 39 | }; 40 | 41 | void rbtree_node_init(struct rbnode *node); 42 | void rbtree_init(struct rbtree *tree, struct rbnode *node); 43 | struct rbnode *rbtree_min(struct rbtree *tree); 44 | void rbtree_insert(struct rbtree *tree, struct rbnode *node); 45 | void rbtree_delete(struct rbtree *tree, struct rbnode *node); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /deps/jemalloc/test/thread_arena.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define NTHREADS 10 5 | 6 | void * 7 | je_thread_start(void *arg) 8 | { 9 | unsigned main_arena_ind = *(unsigned *)arg; 10 | void *p; 11 | unsigned arena_ind; 12 | size_t size; 13 | int err; 14 | 15 | p = malloc(1); 16 | if (p == NULL) { 17 | malloc_printf("%s(): Error in malloc()\n", __func__); 18 | return (void *)1; 19 | } 20 | 21 | size = sizeof(arena_ind); 22 | if ((err = mallctl("thread.arena", &arena_ind, &size, &main_arena_ind, 23 | sizeof(main_arena_ind)))) { 24 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 25 | strerror(err)); 26 | return (void *)1; 27 | } 28 | 29 | size = sizeof(arena_ind); 30 | if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 31 | 0))) { 32 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 33 | strerror(err)); 34 | return (void *)1; 35 | } 36 | assert(arena_ind == main_arena_ind); 37 | 38 | return (NULL); 39 | } 40 | 41 | int 42 | main(void) 43 | { 44 | int ret = 0; 45 | void *p; 46 | unsigned arena_ind; 47 | size_t size; 48 | int err; 49 | je_thread_t threads[NTHREADS]; 50 | unsigned i; 51 | 52 | malloc_printf("Test begin\n"); 53 | 54 | p = malloc(1); 55 | if (p == NULL) { 56 | malloc_printf("%s(): Error in malloc()\n", __func__); 57 | ret = 1; 58 | goto label_return; 59 | } 60 | 61 | size = sizeof(arena_ind); 62 | if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) { 63 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 64 | strerror(err)); 65 | ret = 1; 66 | goto label_return; 67 | } 68 | 69 | for (i = 0; i < NTHREADS; i++) { 70 | je_thread_create(&threads[i], je_thread_start, 71 | (void *)&arena_ind); 72 | } 73 | 74 | for (i = 0; i < NTHREADS; i++) 75 | je_thread_join(threads[i], (void *)&ret); 76 | 77 | label_return: 78 | malloc_printf("Test end\n"); 79 | return (ret); 80 | } 81 | -------------------------------------------------------------------------------- /test/unit/test_protocol.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from common import * 3 | 4 | def get_conn(): 5 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | s.connect((ndb.host(), ndb.port())) 7 | # s.connect((ndb.host(), 2000)) 8 | s.settimeout(.3) 9 | return s 10 | 11 | def _test(req, resp, sleep=0): 12 | s = get_conn() 13 | 14 | for i in req: 15 | s.sendall(i) 16 | time.sleep(sleep) 17 | 18 | s.settimeout(.3) 19 | 20 | data = s.recv(10000) 21 | assert(data == resp) 22 | 23 | def test_slow(): 24 | req = '*1\r\n$4\r\nPING\r\n' 25 | resp = '+PONG\r\n' 26 | 27 | if T_LARGE > T_LARGE_DEFAULT: 28 | sleep = 1 29 | else: 30 | sleep = .1 31 | 32 | _test(req, resp, sleep) 33 | 34 | def test_pingpong(): 35 | req = '*1\r\n$4\r\nPING\r\n' 36 | resp = '+PONG\r\n' 37 | _test(req, resp) 38 | 39 | def _test_bad(req): 40 | s = get_conn() 41 | 42 | s.sendall(req) 43 | data = s.recv(10000) 44 | print data 45 | 46 | assert('' == s.recv(1000)) # peer is closed 47 | assert(data.startswith('-ERR Protocol error')) 48 | 49 | def test_badreq(): 50 | reqs = [ 51 | # '*1\r\n$3\r\nPING\r\n', 52 | '\r\n', 53 | # '*3abcdefg\r\n', 54 | '*3\r\n*abcde\r\n', 55 | # '*3\r\n$abcde\r\n', 56 | # '*3\r\n$3abcde\r\n', 57 | # '*3\r\n$3\r\nabcde\r\n', 58 | ] 59 | 60 | for req in reqs: 61 | _test_bad(req) 62 | 63 | 64 | def test_wrong_argc(): 65 | s = get_conn() 66 | 67 | s.sendall('*1\r\n$3\r\nGET\r\n') 68 | data = s.recv(10000) 69 | print data 70 | 71 | # assert('' == s.recv(1000)) # peer is closed 72 | assert(data.startswith('-ERR wrong number of arguments')) 73 | 74 | s.sendall('*3\r\n$3\r\nSET\r\n$5\r\nkkkkk\r\n$5\r\nvvvvv\r\n') 75 | # s.sendall('*2\r\n$3\r\nGET\r\n$5\r\nkkkkk\r\n') 76 | data = s.recv(10000) 77 | assert(data == '+OK\r\n') 78 | 79 | -------------------------------------------------------------------------------- /deps/lua/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | 23 | const char *const luaT_typenames[] = { 24 | "nil", "boolean", "userdata", "number", 25 | "string", "table", "function", "userdata", "thread", 26 | "proto", "upval" 27 | }; 28 | 29 | 30 | void luaT_init (lua_State *L) { 31 | static const char *const luaT_eventname[] = { /* ORDER TM */ 32 | "__index", "__newindex", 33 | "__gc", "__mode", "__eq", 34 | "__add", "__sub", "__mul", "__div", "__mod", 35 | "__pow", "__unm", "__len", "__lt", "__le", 36 | "__concat", "__call" 37 | }; 38 | int i; 39 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 41 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 42 | } 43 | } 44 | 45 | 46 | /* 47 | ** function to be used with macro "fasttm": optimized for absence of 48 | ** tag methods 49 | */ 50 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 51 | const TValue *tm = luaH_getstr(events, ename); 52 | lua_assert(event <= TM_EQ); 53 | if (ttisnil(tm)) { /* no tag method? */ 54 | events->flags |= cast_byte(1u<metatable; 66 | break; 67 | case LUA_TUSERDATA: 68 | mt = uvalue(o)->metatable; 69 | break; 70 | default: 71 | mt = G(L)->mt[ttype(o)]; 72 | } 73 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 74 | } 75 | 76 | -------------------------------------------------------------------------------- /deps/lua/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) return EOZ; 29 | z->n = size - 1; 30 | z->p = buff; 31 | return char2int(*(z->p++)); 32 | } 33 | 34 | 35 | int luaZ_lookahead (ZIO *z) { 36 | if (z->n == 0) { 37 | if (luaZ_fill(z) == EOZ) 38 | return EOZ; 39 | else { 40 | z->n++; /* luaZ_fill removed first byte; put back it */ 41 | z->p--; 42 | } 43 | } 44 | return char2int(*z->p); 45 | } 46 | 47 | 48 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 49 | z->L = L; 50 | z->reader = reader; 51 | z->data = data; 52 | z->n = 0; 53 | z->p = NULL; 54 | } 55 | 56 | 57 | /* --------------------------------------------------------------- read --- */ 58 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 59 | while (n) { 60 | size_t m; 61 | if (luaZ_lookahead(z) == EOZ) 62 | return n; /* return number of missing bytes */ 63 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 64 | memcpy(b, z->p, m); 65 | z->n -= m; 66 | z->p += m; 67 | b = (char *)b + m; 68 | n -= m; 69 | } 70 | return 0; 71 | } 72 | 73 | /* ------------------------------------------------------------------------ */ 74 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 75 | if (n > buff->buffsize) { 76 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 77 | luaZ_resizebuffer(L, buff, n); 78 | } 79 | return buff->buffer; 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /deps/lua/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | 36 | 37 | /* type of protected functions, to be ran by `runprotected' */ 38 | typedef void (*Pfunc) (lua_State *L, void *ud); 39 | 40 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 41 | LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 45 | ptrdiff_t oldtop, ptrdiff_t ef); 46 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 47 | LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 48 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 49 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 50 | 51 | LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 52 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 53 | 54 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /deps/jemalloc/test/thread_tcache_enabled.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | void * 5 | je_thread_start(void *arg) 6 | { 7 | int err; 8 | size_t sz; 9 | bool e0, e1; 10 | 11 | sz = sizeof(bool); 12 | if ((err = mallctl("thread.tcache.enabled", &e0, &sz, NULL, 0))) { 13 | if (err == ENOENT) { 14 | #ifdef JEMALLOC_TCACHE 15 | assert(false); 16 | #endif 17 | } 18 | goto label_return; 19 | } 20 | 21 | if (e0) { 22 | e1 = false; 23 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) 24 | == 0); 25 | assert(e0); 26 | } 27 | 28 | e1 = true; 29 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 30 | assert(e0 == false); 31 | 32 | e1 = true; 33 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 34 | assert(e0); 35 | 36 | e1 = false; 37 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 38 | assert(e0); 39 | 40 | e1 = false; 41 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 42 | assert(e0 == false); 43 | 44 | free(malloc(1)); 45 | e1 = true; 46 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 47 | assert(e0 == false); 48 | 49 | free(malloc(1)); 50 | e1 = true; 51 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 52 | assert(e0); 53 | 54 | free(malloc(1)); 55 | e1 = false; 56 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 57 | assert(e0); 58 | 59 | free(malloc(1)); 60 | e1 = false; 61 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 62 | assert(e0 == false); 63 | 64 | free(malloc(1)); 65 | label_return: 66 | return (NULL); 67 | } 68 | 69 | int 70 | main(void) 71 | { 72 | int ret = 0; 73 | je_thread_t thread; 74 | 75 | malloc_printf("Test begin\n"); 76 | 77 | je_thread_start(NULL); 78 | 79 | je_thread_create(&thread, je_thread_start, NULL); 80 | je_thread_join(thread, (void *)&ret); 81 | 82 | je_thread_start(NULL); 83 | 84 | je_thread_create(&thread, je_thread_start, NULL); 85 | je_thread_join(thread, (void *)&ret); 86 | 87 | je_thread_start(NULL); 88 | 89 | malloc_printf("Test end\n"); 90 | return (ret); 91 | } 92 | -------------------------------------------------------------------------------- /deps/hiredis/example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "hiredis.h" 6 | 7 | int main(void) { 8 | unsigned int j; 9 | redisContext *c; 10 | redisReply *reply; 11 | 12 | struct timeval timeout = { 1, 500000 }; // 1.5 seconds 13 | c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout); 14 | if (c->err) { 15 | printf("Connection error: %s\n", c->errstr); 16 | exit(1); 17 | } 18 | 19 | /* PING server */ 20 | reply = redisCommand(c,"PING"); 21 | printf("PING: %s\n", reply->str); 22 | freeReplyObject(reply); 23 | 24 | /* Set a key */ 25 | reply = redisCommand(c,"SET %s %s", "foo", "hello world"); 26 | printf("SET: %s\n", reply->str); 27 | freeReplyObject(reply); 28 | 29 | /* Set a key using binary safe API */ 30 | reply = redisCommand(c,"SET %b %b", "bar", 3, "hello", 5); 31 | printf("SET (binary API): %s\n", reply->str); 32 | freeReplyObject(reply); 33 | 34 | /* Try a GET and two INCR */ 35 | reply = redisCommand(c,"GET foo"); 36 | printf("GET foo: %s\n", reply->str); 37 | freeReplyObject(reply); 38 | 39 | reply = redisCommand(c,"INCR counter"); 40 | printf("INCR counter: %lld\n", reply->integer); 41 | freeReplyObject(reply); 42 | /* again ... */ 43 | reply = redisCommand(c,"INCR counter"); 44 | printf("INCR counter: %lld\n", reply->integer); 45 | freeReplyObject(reply); 46 | 47 | /* Create a list of numbers, from 0 to 9 */ 48 | reply = redisCommand(c,"DEL mylist"); 49 | freeReplyObject(reply); 50 | for (j = 0; j < 10; j++) { 51 | char buf[64]; 52 | 53 | snprintf(buf,64,"%d",j); 54 | reply = redisCommand(c,"LPUSH mylist element-%s", buf); 55 | freeReplyObject(reply); 56 | } 57 | 58 | /* Let's check what we have inside the list */ 59 | reply = redisCommand(c,"LRANGE mylist 0 -1"); 60 | if (reply->type == REDIS_REPLY_ARRAY) { 61 | for (j = 0; j < reply->elements; j++) { 62 | printf("%u) %s\n", j, reply->element[j]->str); 63 | } 64 | } 65 | freeReplyObject(reply); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /src/util/nc_array.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_ARRAY_H_ 19 | #define _NC_ARRAY_H_ 20 | 21 | #include "nc_util.h" 22 | 23 | typedef int (*array_compare_t)(const void *, const void *); 24 | typedef rstatus_t (*array_each_t)(void *, void *); 25 | 26 | typedef struct array_s { 27 | uint32_t nelem; /* # element */ 28 | void *elem; /* element */ 29 | size_t size; /* element size */ 30 | uint32_t nalloc; /* # allocated element */ 31 | } array_t; 32 | 33 | #define null_array { 0, NULL, 0, 0 } 34 | 35 | static inline void 36 | array_null(array_t *a) 37 | { 38 | a->nelem = 0; 39 | a->elem = NULL; 40 | a->size = 0; 41 | a->nalloc = 0; 42 | } 43 | 44 | static inline void 45 | array_set(array_t *a, void *elem, size_t size, uint32_t nalloc) 46 | { 47 | a->nelem = 0; 48 | a->elem = elem; 49 | a->size = size; 50 | a->nalloc = nalloc; 51 | } 52 | 53 | static inline uint32_t 54 | array_n(const array_t *a) 55 | { 56 | return a->nelem; 57 | } 58 | 59 | array_t *array_create(uint32_t n, size_t size); 60 | void array_destroy(array_t *a); 61 | 62 | uint32_t array_idx(array_t *a, void *elem); 63 | void *array_push(array_t *a); 64 | void *array_pop(array_t *a); 65 | void *array_get(array_t *a, uint32_t idx); 66 | void *array_top(array_t *a); 67 | void array_swap(array_t *a, array_t *b); 68 | void array_sort(array_t *a, array_compare_t compare); 69 | rstatus_t array_each(array_t *a, array_each_t func, void *data); 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/hash.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | #endif /* JEMALLOC_H_EXTERNS */ 13 | /******************************************************************************/ 14 | #ifdef JEMALLOC_H_INLINES 15 | 16 | #ifndef JEMALLOC_ENABLE_INLINE 17 | uint64_t hash(const void *key, size_t len, uint64_t seed); 18 | #endif 19 | 20 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_HASH_C_)) 21 | /* 22 | * The following hash function is based on MurmurHash64A(), placed into the 23 | * public domain by Austin Appleby. See http://murmurhash.googlepages.com/ for 24 | * details. 25 | */ 26 | JEMALLOC_INLINE uint64_t 27 | hash(const void *key, size_t len, uint64_t seed) 28 | { 29 | const uint64_t m = UINT64_C(0xc6a4a7935bd1e995); 30 | const int r = 47; 31 | uint64_t h = seed ^ (len * m); 32 | const uint64_t *data = (const uint64_t *)key; 33 | const uint64_t *end = data + (len/8); 34 | const unsigned char *data2; 35 | 36 | assert(((uintptr_t)key & 0x7) == 0); 37 | 38 | while(data != end) { 39 | uint64_t k = *data++; 40 | 41 | k *= m; 42 | k ^= k >> r; 43 | k *= m; 44 | 45 | h ^= k; 46 | h *= m; 47 | } 48 | 49 | data2 = (const unsigned char *)data; 50 | switch(len & 7) { 51 | case 7: h ^= ((uint64_t)(data2[6])) << 48; 52 | case 6: h ^= ((uint64_t)(data2[5])) << 40; 53 | case 5: h ^= ((uint64_t)(data2[4])) << 32; 54 | case 4: h ^= ((uint64_t)(data2[3])) << 24; 55 | case 3: h ^= ((uint64_t)(data2[2])) << 16; 56 | case 2: h ^= ((uint64_t)(data2[1])) << 8; 57 | case 1: h ^= ((uint64_t)(data2[0])); 58 | h *= m; 59 | } 60 | 61 | h ^= h >> r; 62 | h *= m; 63 | h ^= h >> r; 64 | 65 | return (h); 66 | } 67 | #endif 68 | 69 | #endif /* JEMALLOC_H_INLINES */ 70 | /******************************************************************************/ 71 | -------------------------------------------------------------------------------- /src/test/test_conf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file : test_conf.c 3 | * author : ning 4 | * date : 2014-06-30 16:24:20 5 | */ 6 | 7 | #include "nc_util.h" 8 | #include "testhelp.h" 9 | 10 | static const char *config_file = "/tmp/test_conf.conf"; 11 | 12 | static void 13 | clean() 14 | { 15 | char buf[1024]; 16 | 17 | nc_scnprintf(buf, sizeof(buf), "rm %s", config_file); 18 | system(buf); 19 | } 20 | 21 | static void 22 | test_normal_conf() 23 | { 24 | FILE *f = fopen(config_file, "w"); 25 | nc_conf_t conf; 26 | 27 | fprintf(f, "ip = '127.0.0.5'"); 28 | fprintf(f, "port = 9527"); 29 | fclose(f); 30 | 31 | if (NC_ERROR == nc_conf_init(&conf, config_file)) { 32 | TEST_ASSERT("conf_init", 0); 33 | return; 34 | } 35 | 36 | TEST_ASSERT("ip", 37 | !strcmp("127.0.0.5", nc_conf_get_str(&conf, "ip", "0.0.0.0"))); 38 | TEST_ASSERT("ipx", 39 | !strcmp("0.0.0.0", nc_conf_get_str(&conf, "ipx", "0.0.0.0"))); 40 | 41 | TEST_ASSERT("port", 42 | 9527 == nc_conf_get_num(&conf, "port", 0)); 43 | TEST_ASSERT("portx", 44 | 0 == nc_conf_get_num(&conf, "portx", 0)); 45 | TEST_ASSERT("port.x", 46 | 302 == nc_conf_get_num(&conf, "port.x", 302)); 47 | 48 | clean(); 49 | } 50 | 51 | static void 52 | test_empty_conf() 53 | { 54 | FILE *f = fopen(config_file, "w"); 55 | nc_conf_t conf; 56 | 57 | fclose(f); 58 | 59 | if (NC_ERROR == nc_conf_init(&conf, config_file)) { 60 | TEST_ASSERT("conf_init", 0); 61 | return; 62 | } 63 | 64 | TEST_ASSERT("ipx", 65 | !strcmp("0.0.0.0", nc_conf_get_str(&conf, "ipx", "0.0.0.0"))); 66 | TEST_ASSERT("portx", 67 | 0 == nc_conf_get_num(&conf, "portx", 0)); 68 | clean(); 69 | } 70 | 71 | static void 72 | test_notexist_conf() 73 | { 74 | nc_conf_t conf; 75 | 76 | TEST_ASSERT("notexist", NC_ERROR == nc_conf_init(&conf, config_file)); 77 | } 78 | 79 | int 80 | main(int argc, const char **argv) 81 | { 82 | test_normal_conf(); 83 | test_empty_conf(); 84 | 85 | test_notexist_conf(); 86 | 87 | test_report(); 88 | return 0; 89 | } 90 | 91 | /* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ 92 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/prng.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | /* 5 | * Simple linear congruential pseudo-random number generator: 6 | * 7 | * prng(y) = (a*x + c) % m 8 | * 9 | * where the following constants ensure maximal period: 10 | * 11 | * a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4. 12 | * c == Odd number (relatively prime to 2^n). 13 | * m == 2^32 14 | * 15 | * See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints. 16 | * 17 | * This choice of m has the disadvantage that the quality of the bits is 18 | * proportional to bit position. For example. the lowest bit has a cycle of 2, 19 | * the next has a cycle of 4, etc. For this reason, we prefer to use the upper 20 | * bits. 21 | * 22 | * Macro parameters: 23 | * uint32_t r : Result. 24 | * unsigned lg_range : (0..32], number of least significant bits to return. 25 | * uint32_t state : Seed value. 26 | * const uint32_t a, c : See above discussion. 27 | */ 28 | #define prng32(r, lg_range, state, a, c) do { \ 29 | assert(lg_range > 0); \ 30 | assert(lg_range <= 32); \ 31 | \ 32 | r = (state * (a)) + (c); \ 33 | state = r; \ 34 | r >>= (32 - lg_range); \ 35 | } while (false) 36 | 37 | /* Same as prng32(), but 64 bits of pseudo-randomness, using uint64_t. */ 38 | #define prng64(r, lg_range, state, a, c) do { \ 39 | assert(lg_range > 0); \ 40 | assert(lg_range <= 64); \ 41 | \ 42 | r = (state * (a)) + (c); \ 43 | state = r; \ 44 | r >>= (64 - lg_range); \ 45 | } while (false) 46 | 47 | #endif /* JEMALLOC_H_TYPES */ 48 | /******************************************************************************/ 49 | #ifdef JEMALLOC_H_STRUCTS 50 | 51 | #endif /* JEMALLOC_H_STRUCTS */ 52 | /******************************************************************************/ 53 | #ifdef JEMALLOC_H_EXTERNS 54 | 55 | #endif /* JEMALLOC_H_EXTERNS */ 56 | /******************************************************************************/ 57 | #ifdef JEMALLOC_H_INLINES 58 | 59 | #endif /* JEMALLOC_H_INLINES */ 60 | /******************************************************************************/ 61 | -------------------------------------------------------------------------------- /src/util/nc_mbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_MBUF_H_ 19 | #define _NC_MBUF_H_ 20 | 21 | typedef void (*mbuf_copy_t)(struct mbuf *, void *); 22 | 23 | struct mbuf { 24 | uint32_t magic; /* mbuf magic (const) */ 25 | STAILQ_ENTRY(mbuf) next; /* next mbuf */ 26 | uint8_t *pos; /* read marker */ 27 | uint8_t *last; /* write marker */ 28 | uint8_t *start; /* start of buffer (const) */ 29 | uint8_t *end; /* end of buffer (const) */ 30 | }; 31 | 32 | STAILQ_HEAD(mhdr, mbuf); 33 | 34 | #define MBUF_MAGIC 0xdeadbeef 35 | #define MBUF_MIN_SIZE 512 36 | #define MBUF_MAX_SIZE 16777216 37 | #define MBUF_SIZE 16384 38 | #define MBUF_HSIZE sizeof(struct mbuf) 39 | 40 | static inline bool 41 | mbuf_empty(struct mbuf *mbuf) 42 | { 43 | return mbuf->pos == mbuf->last ? true : false; 44 | } 45 | 46 | static inline bool 47 | mbuf_full(struct mbuf *mbuf) 48 | { 49 | return mbuf->last == mbuf->end ? true : false; 50 | } 51 | 52 | void mbuf_init(size_t size); 53 | void mbuf_deinit(void); 54 | struct mbuf *mbuf_get(void); 55 | void mbuf_put(struct mbuf *mbuf); 56 | void mbuf_rewind(struct mbuf *mbuf); 57 | uint32_t mbuf_length(struct mbuf *mbuf); 58 | uint32_t mbuf_size(struct mbuf *mbuf); 59 | size_t mbuf_data_size(void); 60 | void mbuf_insert(struct mhdr *mhdr, struct mbuf *mbuf); 61 | void mbuf_remove(struct mhdr *mhdr, struct mbuf *mbuf); 62 | void mbuf_copy(struct mbuf *mbuf, uint8_t *pos, size_t n); 63 | struct mbuf *mbuf_split(struct mhdr *h, uint8_t *pos, mbuf_copy_t cb, void *cbarg); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/chunk.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | /* 5 | * Size and alignment of memory chunks that are allocated by the OS's virtual 6 | * memory system. 7 | */ 8 | #define LG_CHUNK_DEFAULT 22 9 | 10 | /* Return the chunk address for allocation address a. */ 11 | #define CHUNK_ADDR2BASE(a) \ 12 | ((void *)((uintptr_t)(a) & ~chunksize_mask)) 13 | 14 | /* Return the chunk offset of address a. */ 15 | #define CHUNK_ADDR2OFFSET(a) \ 16 | ((size_t)((uintptr_t)(a) & chunksize_mask)) 17 | 18 | /* Return the smallest chunk multiple that is >= s. */ 19 | #define CHUNK_CEILING(s) \ 20 | (((s) + chunksize_mask) & ~chunksize_mask) 21 | 22 | #endif /* JEMALLOC_H_TYPES */ 23 | /******************************************************************************/ 24 | #ifdef JEMALLOC_H_STRUCTS 25 | 26 | #endif /* JEMALLOC_H_STRUCTS */ 27 | /******************************************************************************/ 28 | #ifdef JEMALLOC_H_EXTERNS 29 | 30 | extern size_t opt_lg_chunk; 31 | extern const char *opt_dss; 32 | 33 | /* Protects stats_chunks; currently not used for any other purpose. */ 34 | extern malloc_mutex_t chunks_mtx; 35 | /* Chunk statistics. */ 36 | extern chunk_stats_t stats_chunks; 37 | 38 | extern rtree_t *chunks_rtree; 39 | 40 | extern size_t chunksize; 41 | extern size_t chunksize_mask; /* (chunksize - 1). */ 42 | extern size_t chunk_npages; 43 | extern size_t map_bias; /* Number of arena chunk header pages. */ 44 | extern size_t arena_maxclass; /* Max size class for arenas. */ 45 | 46 | void *chunk_alloc(size_t size, size_t alignment, bool base, bool *zero, 47 | dss_prec_t dss_prec); 48 | void chunk_unmap(void *chunk, size_t size); 49 | void chunk_dealloc(void *chunk, size_t size, bool unmap); 50 | bool chunk_boot(void); 51 | void chunk_prefork(void); 52 | void chunk_postfork_parent(void); 53 | void chunk_postfork_child(void); 54 | 55 | #endif /* JEMALLOC_H_EXTERNS */ 56 | /******************************************************************************/ 57 | #ifdef JEMALLOC_H_INLINES 58 | 59 | #endif /* JEMALLOC_H_INLINES */ 60 | /******************************************************************************/ 61 | 62 | #include "jemalloc/internal/chunk_dss.h" 63 | #include "jemalloc/internal/chunk_mmap.h" 64 | -------------------------------------------------------------------------------- /deps/hiredis/net.h: -------------------------------------------------------------------------------- 1 | /* Extracted from anet.c to work properly with Hiredis error reporting. 2 | * 3 | * Copyright (c) 2006-2011, Salvatore Sanfilippo 4 | * Copyright (c) 2010-2011, Pieter Noordhuis 5 | * 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * * Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * * Neither the name of Redis nor the names of its contributors may be used 17 | * to endorse or promote products derived from this software without 18 | * specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | * POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #ifndef __NET_H 34 | #define __NET_H 35 | 36 | #include "hiredis.h" 37 | 38 | #if defined(__sun) 39 | #define AF_LOCAL AF_UNIX 40 | #endif 41 | 42 | int redisCheckSocketError(redisContext *c, int fd); 43 | int redisContextSetTimeout(redisContext *c, struct timeval tv); 44 | int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout); 45 | int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/ndb_stat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file : ndb_stat.c 3 | * author : ning 4 | * date : 2014-10-13 15:21:16 5 | */ 6 | 7 | #include "ndb.h" 8 | #include "ndb_stat.h" 9 | 10 | rstatus_t 11 | stat_init(void *owner, stat_t *stat) 12 | { 13 | stat->owner = owner; 14 | 15 | stat->numconnections = 0; 16 | stat->start_time = nc_msec_now(); 17 | 18 | stat->ops = 0; 19 | stat->last_aggregate_ts = 0; 20 | stat->last_aggregate_ops = 0; 21 | stat->aggregate_idx = 0; 22 | 23 | memset(stat->ops_sec_samples, 0, sizeof(stat->ops_sec_samples)); 24 | 25 | return NC_OK; 26 | } 27 | 28 | rstatus_t 29 | stat_deinit(stat_t *stat) 30 | { 31 | return NC_OK; 32 | } 33 | 34 | rstatus_t 35 | stat_inc(stat_t *stat, bool w, bool hit) 36 | { 37 | stat->ops++; 38 | 39 | return NC_OK; 40 | } 41 | 42 | static uint64_t 43 | stat_average(uint64_t *arr, uint32_t cnt) 44 | { 45 | uint32_t i; 46 | uint32_t used = 0; 47 | uint64_t sum = 0; 48 | 49 | for (i = 0; i < cnt; i++) { 50 | log_debug("arr[%d] = %"PRIu64"", i, arr[i]); 51 | 52 | if (arr[i]) { 53 | used ++; 54 | sum += arr[i]; 55 | } 56 | } 57 | 58 | if (used == 0) { 59 | return 0; 60 | } 61 | 62 | return sum / used; 63 | } 64 | 65 | sds 66 | stat_info(stat_t *stat) 67 | { 68 | sds info = sdsempty(); 69 | info = sdscatprintf(info, "uptime:%"PRIu64"\r\n", 70 | (nc_msec_now() - stat->start_time) / 1000); 71 | 72 | info = sdscatprintf(info, "ops:%"PRIu64"\r\n", 73 | stat_average(stat->ops_sec_samples, NDB_OPS_SEC_SAMPLES)); 74 | 75 | return info; 76 | } 77 | 78 | rstatus_t 79 | stat_cron(stat_t *stat) 80 | { 81 | uint64_t ts = nc_msec_now(); 82 | uint64_t diff = ts - stat->last_aggregate_ts; 83 | uint64_t qps = 0; 84 | 85 | log_debug("stat: stat->ops: %"PRIu64" stat->last_aggregate_ops: %"PRIu64" st:%"PRIu64"", 86 | stat->ops, stat->last_aggregate_ops, ts); 87 | if (diff) { 88 | qps = (stat->ops - stat->last_aggregate_ops) * 1000 / diff; 89 | } 90 | 91 | stat->last_aggregate_ts = ts; 92 | stat->last_aggregate_ops = stat->ops; 93 | 94 | stat->ops_sec_samples[stat->aggregate_idx] = qps; 95 | 96 | stat->aggregate_idx = (stat->aggregate_idx + 1) % NDB_OPS_SEC_SAMPLES; 97 | 98 | return NC_OK; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /deps/lua/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 24 | enum RESERVED { 25 | /* terminal symbols denoted by reserved words */ 26 | TK_AND = FIRST_RESERVED, TK_BREAK, 27 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 28 | TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 29 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 30 | /* other terminal symbols */ 31 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 32 | TK_NAME, TK_STRING, TK_EOS 33 | }; 34 | 35 | /* number of reserved words */ 36 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 37 | 38 | 39 | /* array with token `names' */ 40 | LUAI_DATA const char *const luaX_tokens []; 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | TString *ts; 46 | } SemInfo; /* semantics information */ 47 | 48 | 49 | typedef struct Token { 50 | int token; 51 | SemInfo seminfo; 52 | } Token; 53 | 54 | 55 | typedef struct LexState { 56 | int current; /* current character (charint) */ 57 | int linenumber; /* input line counter */ 58 | int lastline; /* line of last token `consumed' */ 59 | Token t; /* current token */ 60 | Token lookahead; /* look ahead token */ 61 | struct FuncState *fs; /* `FuncState' is private to the parser */ 62 | struct lua_State *L; 63 | ZIO *z; /* input stream */ 64 | Mbuffer *buff; /* buffer for tokens */ 65 | TString *source; /* current source name */ 66 | char decpoint; /* locale decimal point */ 67 | } LexState; 68 | 69 | 70 | LUAI_FUNC void luaX_init (lua_State *L); 71 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 72 | TString *source); 73 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 74 | LUAI_FUNC void luaX_next (LexState *ls); 75 | LUAI_FUNC void luaX_lookahead (LexState *ls); 76 | LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 77 | LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 78 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 79 | 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /deps/jemalloc/src/tsd.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_TSD_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | /* Data. */ 6 | 7 | static unsigned ncleanups; 8 | static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX]; 9 | 10 | /******************************************************************************/ 11 | 12 | void * 13 | malloc_tsd_malloc(size_t size) 14 | { 15 | 16 | /* Avoid choose_arena() in order to dodge bootstrapping issues. */ 17 | return (arena_malloc(arenas[0], size, false, false)); 18 | } 19 | 20 | void 21 | malloc_tsd_dalloc(void *wrapper) 22 | { 23 | 24 | idalloc(wrapper); 25 | } 26 | 27 | void 28 | malloc_tsd_no_cleanup(void *arg) 29 | { 30 | 31 | not_reached(); 32 | } 33 | 34 | #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32) 35 | #ifndef _WIN32 36 | JEMALLOC_EXPORT 37 | #endif 38 | void 39 | _malloc_thread_cleanup(void) 40 | { 41 | bool pending[MALLOC_TSD_CLEANUPS_MAX], again; 42 | unsigned i; 43 | 44 | for (i = 0; i < ncleanups; i++) 45 | pending[i] = true; 46 | 47 | do { 48 | again = false; 49 | for (i = 0; i < ncleanups; i++) { 50 | if (pending[i]) { 51 | pending[i] = cleanups[i](); 52 | if (pending[i]) 53 | again = true; 54 | } 55 | } 56 | } while (again); 57 | } 58 | #endif 59 | 60 | void 61 | malloc_tsd_cleanup_register(bool (*f)(void)) 62 | { 63 | 64 | assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX); 65 | cleanups[ncleanups] = f; 66 | ncleanups++; 67 | } 68 | 69 | void 70 | malloc_tsd_boot(void) 71 | { 72 | 73 | ncleanups = 0; 74 | } 75 | 76 | #ifdef _WIN32 77 | static BOOL WINAPI 78 | _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 79 | { 80 | 81 | switch (fdwReason) { 82 | #ifdef JEMALLOC_LAZY_LOCK 83 | case DLL_THREAD_ATTACH: 84 | isthreaded = true; 85 | break; 86 | #endif 87 | case DLL_THREAD_DETACH: 88 | _malloc_thread_cleanup(); 89 | break; 90 | default: 91 | break; 92 | } 93 | return (true); 94 | } 95 | 96 | #ifdef _MSC_VER 97 | # ifdef _M_IX86 98 | # pragma comment(linker, "/INCLUDE:__tls_used") 99 | # else 100 | # pragma comment(linker, "/INCLUDE:_tls_used") 101 | # endif 102 | # pragma section(".CRT$XLY",long,read) 103 | #endif 104 | JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used) 105 | static const BOOL (WINAPI *tls_callback)(HINSTANCE hinstDLL, 106 | DWORD fdwReason, LPVOID lpvReserved) = _tls_callback; 107 | #endif 108 | -------------------------------------------------------------------------------- /deps/lua/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | /* 24 | ** About the realloc function: 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26 | ** (`osize' is the old size, `nsize' is the new size) 27 | ** 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). 29 | ** 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL). 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *errormsg) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, errormsg); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | void *luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | return NULL; /* to avoid warnings */ 69 | } 70 | 71 | 72 | 73 | /* 74 | ** generic allocation routine. 75 | */ 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 77 | global_State *g = G(L); 78 | lua_assert((osize == 0) == (block == NULL)); 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); 80 | if (block == NULL && nsize > 0) 81 | luaD_throw(L, LUA_ERRMEM); 82 | lua_assert((nsize == 0) == (block == NULL)); 83 | g->totalbytes = (g->totalbytes - osize) + nsize; 84 | return block; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/qr.h: -------------------------------------------------------------------------------- 1 | /* Ring definitions. */ 2 | #define qr(a_type) \ 3 | struct { \ 4 | a_type *qre_next; \ 5 | a_type *qre_prev; \ 6 | } 7 | 8 | /* Ring functions. */ 9 | #define qr_new(a_qr, a_field) do { \ 10 | (a_qr)->a_field.qre_next = (a_qr); \ 11 | (a_qr)->a_field.qre_prev = (a_qr); \ 12 | } while (0) 13 | 14 | #define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next) 15 | 16 | #define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev) 17 | 18 | #define qr_before_insert(a_qrelm, a_qr, a_field) do { \ 19 | (a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev; \ 20 | (a_qr)->a_field.qre_next = (a_qrelm); \ 21 | (a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr); \ 22 | (a_qrelm)->a_field.qre_prev = (a_qr); \ 23 | } while (0) 24 | 25 | #define qr_after_insert(a_qrelm, a_qr, a_field) \ 26 | do \ 27 | { \ 28 | (a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next; \ 29 | (a_qr)->a_field.qre_prev = (a_qrelm); \ 30 | (a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr); \ 31 | (a_qrelm)->a_field.qre_next = (a_qr); \ 32 | } while (0) 33 | 34 | #define qr_meld(a_qr_a, a_qr_b, a_field) do { \ 35 | void *t; \ 36 | (a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b); \ 37 | (a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a); \ 38 | t = (a_qr_a)->a_field.qre_prev; \ 39 | (a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev; \ 40 | (a_qr_b)->a_field.qre_prev = t; \ 41 | } while (0) 42 | 43 | /* qr_meld() and qr_split() are functionally equivalent, so there's no need to 44 | * have two copies of the code. */ 45 | #define qr_split(a_qr_a, a_qr_b, a_field) \ 46 | qr_meld((a_qr_a), (a_qr_b), a_field) 47 | 48 | #define qr_remove(a_qr, a_field) do { \ 49 | (a_qr)->a_field.qre_prev->a_field.qre_next \ 50 | = (a_qr)->a_field.qre_next; \ 51 | (a_qr)->a_field.qre_next->a_field.qre_prev \ 52 | = (a_qr)->a_field.qre_prev; \ 53 | (a_qr)->a_field.qre_next = (a_qr); \ 54 | (a_qr)->a_field.qre_prev = (a_qr); \ 55 | } while (0) 56 | 57 | #define qr_foreach(var, a_qr, a_field) \ 58 | for ((var) = (a_qr); \ 59 | (var) != NULL; \ 60 | (var) = (((var)->a_field.qre_next != (a_qr)) \ 61 | ? (var)->a_field.qre_next : NULL)) 62 | 63 | #define qr_reverse_foreach(var, a_qr, a_field) \ 64 | for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL; \ 65 | (var) != NULL; \ 66 | (var) = (((var) != (a_qr)) \ 67 | ? (var)->a_field.qre_prev : NULL)) 68 | -------------------------------------------------------------------------------- /deps/lua/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VLOCAL, /* info = local register */ 27 | VUPVAL, /* info = index of upvalue in `upvalues' */ 28 | VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 29 | VINDEXED, /* info = table register; aux = index register (or `k') */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VNONRELOC, /* info = result register */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | typedef struct expdesc { 38 | expkind k; 39 | union { 40 | struct { int info, aux; } s; 41 | lua_Number nval; 42 | } u; 43 | int t; /* patch list of `exit when true' */ 44 | int f; /* patch list of `exit when false' */ 45 | } expdesc; 46 | 47 | 48 | typedef struct upvaldesc { 49 | lu_byte k; 50 | lu_byte info; 51 | } upvaldesc; 52 | 53 | 54 | struct BlockCnt; /* defined in lparser.c */ 55 | 56 | 57 | /* state needed to generate code for a given function */ 58 | typedef struct FuncState { 59 | Proto *f; /* current function header */ 60 | Table *h; /* table to find (and reuse) elements in `k' */ 61 | struct FuncState *prev; /* enclosing function */ 62 | struct LexState *ls; /* lexical state */ 63 | struct lua_State *L; /* copy of the Lua state */ 64 | struct BlockCnt *bl; /* chain of current blocks */ 65 | int pc; /* next position to code (equivalent to `ncode') */ 66 | int lasttarget; /* `pc' of last `jump target' */ 67 | int jpc; /* list of pending jumps to `pc' */ 68 | int freereg; /* first free register */ 69 | int nk; /* number of elements in `k' */ 70 | int np; /* number of elements in `p' */ 71 | short nlocvars; /* number of elements in `locvars' */ 72 | lu_byte nactvar; /* number of active local variables */ 73 | upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 74 | unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 75 | } FuncState; 76 | 77 | 78 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 79 | const char *name); 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | # we have to use g++ to static link libsnappy.a 2 | LD=g++ 3 | 4 | #basic 5 | CFLAGS=-std=c99 -pedantic -Wall -O0 -g -ggdb -D_POSIX_C_SOURCE -D_GNU_SOURCE -DNC_DEBUG_LOG -DHAVE_ASSERT_PANIC 6 | CFLAGS+= -I./util/ 7 | LDLIBS= -lm -ldl 8 | LDFLAGS=-g -ggdb -rdynamic 9 | 10 | #lua 11 | LDLIBS+= ../deps/lua/src/liblua.a 12 | CFLAGS+= -I../deps/lua/src 13 | 14 | #leveldb 15 | CFLAGS+= -I../deps/leveldb/include/ 16 | LDLIBS+= ../deps/leveldb/libleveldb.a ../deps/snappy/.libs/libsnappy.a -lpthread 17 | #LDLIBS+= ../deps/leveldb/libleveldb.a -lsnappy -lpthread # this work with gcc 18 | 19 | #hiredis 20 | CFLAGS+= -I ../deps/hiredis/ 21 | LDLIBS+= ../deps/hiredis/libhiredis.a 22 | 23 | UTIL_SRC=$(wildcard util/*.c) 24 | UTIL_OBJ=$(patsubst %.c,%.o,$(UTIL_SRC) ) 25 | 26 | all: bin 27 | @echo "done" 28 | @echo "run 'make test'" 29 | 30 | %.o: %.c *.h util/*.h 31 | $(CC) -c -o $@ $(CFLAGS) $< 32 | 33 | clean: 34 | rm -f $(ALL_BIN) 35 | rm -f *.o *.gcda *.gcno *.gcov 36 | find . -name *.o | xargs rm -f 37 | find . -name *~ | xargs rm -f 38 | #rm -f cscope.* 39 | 40 | #ndb-server 41 | KV_BIN=ndb 42 | KV_OBJ=$(UTIL_OBJ) ndb.o ndb_message.o ndb_leveldb.o ndb_command.o ndb_job.o ndb_cursor.o ndb_oplog.o ndb_repl.o ndb_stat.o 43 | $(KV_BIN): $(KV_OBJ) 44 | $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) 45 | 46 | TEST_CONF_BIN=test_conf 47 | TEST_CONF_OBJ=$(UTIL_OBJ) test/test_conf.o 48 | $(TEST_CONF_BIN): $(TEST_CONF_OBJ) 49 | $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) 50 | 51 | #test-misc 52 | TEST_ARRAY_BIN=test_misc 53 | TEST_ARRAY_OBJ=$(UTIL_OBJ) test/test_misc.o 54 | $(TEST_ARRAY_BIN): $(TEST_ARRAY_OBJ) 55 | $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) 56 | 57 | #test-event 58 | TEST_EVENT_BIN=test_event 59 | TEST_EVENT_OBJ=$(UTIL_OBJ) test/test_event.o 60 | $(TEST_EVENT_BIN): $(TEST_EVENT_OBJ) 61 | $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) 62 | 63 | #test-oplog 64 | TEST_OPLOG_BIN=test_oplog 65 | TEST_OPLOG_OBJ=$(UTIL_OBJ) test/test_oplog.o 66 | $(TEST_OPLOG_BIN): $(TEST_OPLOG_OBJ) 67 | $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) 68 | test/test_oplog.o: ndb_oplog.c test/test_oplog.c 69 | 70 | #test-repl 71 | TEST_REPL_BIN=test_repl 72 | TEST_REPL_OBJ=$(UTIL_OBJ) test/test_repl.o ndb_leveldb.o ndb_oplog.o ndb_job.o ndb_stat.o 73 | $(TEST_REPL_BIN): $(TEST_REPL_OBJ) 74 | $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) 75 | test/test_repl.o: ndb_repl.c test/test_repl.c 76 | 77 | #summary 78 | ALL_BIN=$(KV_BIN) $(TEST_CONF_BIN) $(TEST_ARRAY_BIN) $(TEST_EVENT_BIN) $(TEST_OPLOG_BIN) $(TEST_REPL_BIN) 79 | bin: $(ALL_BIN) 80 | 81 | test: bin 82 | for number in $(ALL_BIN) ; do \ 83 | echo $$number ; \ 84 | ./$$number 2>&1 | grep tests ; \ 85 | done 86 | 87 | .PHONY: test 88 | -------------------------------------------------------------------------------- /src/test/testhelp.h: -------------------------------------------------------------------------------- 1 | /* This is a really minimal testing framework for C. 2 | * 3 | * Example: 4 | * 5 | * test_cond("Check if 1 == 1", 1==1) 6 | * test_cond("Check if 5 > 10", 5 > 10) 7 | * test_report() 8 | * 9 | * ---------------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2010-2012, Salvatore Sanfilippo 12 | * All rights reserved. 13 | * 14 | * Redistribution and use in source and binary forms, with or without 15 | * modification, are permitted provided that the following conditions are met: 16 | * 17 | * * Redistributions of source code must retain the above copyright notice, 18 | * this list of conditions and the following disclaimer. 19 | * * Redistributions in binary form must reproduce the above copyright 20 | * notice, this list of conditions and the following disclaimer in the 21 | * documentation and/or other materials provided with the distribution. 22 | * * Neither the name of Redis nor the names of its contributors may be used 23 | * to endorse or promote products derived from this software without 24 | * specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 30 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 | * POSSIBILITY OF SUCH DAMAGE. 37 | */ 38 | 39 | #ifndef __TESTHELP_H 40 | #define __TESTHELP_H 41 | 42 | int __failed_tests = 0; 43 | int __test_num = 0; 44 | #define test_cond(descr,_c) do { \ 45 | __test_num++; printf("=== %d - %s: ", __test_num, descr); \ 46 | if(_c) printf("PASSED\n"); else {printf("FAILED\n"); __failed_tests++;} \ 47 | } while(0); 48 | 49 | #define TEST_ASSERT test_cond 50 | 51 | #define test_report() do { \ 52 | printf("%d tests, %d passed, %d failed \n", __test_num, \ 53 | __test_num-__failed_tests, __failed_tests); \ 54 | if (__failed_tests) { \ 55 | printf("\033[91m=== WARNING === We have failed tests here...\033[0m\n"); \ 56 | exit(1); \ 57 | } \ 58 | } while(0); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/ql.h: -------------------------------------------------------------------------------- 1 | /* 2 | * List definitions. 3 | */ 4 | #define ql_head(a_type) \ 5 | struct { \ 6 | a_type *qlh_first; \ 7 | } 8 | 9 | #define ql_head_initializer(a_head) {NULL} 10 | 11 | #define ql_elm(a_type) qr(a_type) 12 | 13 | /* List functions. */ 14 | #define ql_new(a_head) do { \ 15 | (a_head)->qlh_first = NULL; \ 16 | } while (0) 17 | 18 | #define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field) 19 | 20 | #define ql_first(a_head) ((a_head)->qlh_first) 21 | 22 | #define ql_last(a_head, a_field) \ 23 | ((ql_first(a_head) != NULL) \ 24 | ? qr_prev(ql_first(a_head), a_field) : NULL) 25 | 26 | #define ql_next(a_head, a_elm, a_field) \ 27 | ((ql_last(a_head, a_field) != (a_elm)) \ 28 | ? qr_next((a_elm), a_field) : NULL) 29 | 30 | #define ql_prev(a_head, a_elm, a_field) \ 31 | ((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field) \ 32 | : NULL) 33 | 34 | #define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do { \ 35 | qr_before_insert((a_qlelm), (a_elm), a_field); \ 36 | if (ql_first(a_head) == (a_qlelm)) { \ 37 | ql_first(a_head) = (a_elm); \ 38 | } \ 39 | } while (0) 40 | 41 | #define ql_after_insert(a_qlelm, a_elm, a_field) \ 42 | qr_after_insert((a_qlelm), (a_elm), a_field) 43 | 44 | #define ql_head_insert(a_head, a_elm, a_field) do { \ 45 | if (ql_first(a_head) != NULL) { \ 46 | qr_before_insert(ql_first(a_head), (a_elm), a_field); \ 47 | } \ 48 | ql_first(a_head) = (a_elm); \ 49 | } while (0) 50 | 51 | #define ql_tail_insert(a_head, a_elm, a_field) do { \ 52 | if (ql_first(a_head) != NULL) { \ 53 | qr_before_insert(ql_first(a_head), (a_elm), a_field); \ 54 | } \ 55 | ql_first(a_head) = qr_next((a_elm), a_field); \ 56 | } while (0) 57 | 58 | #define ql_remove(a_head, a_elm, a_field) do { \ 59 | if (ql_first(a_head) == (a_elm)) { \ 60 | ql_first(a_head) = qr_next(ql_first(a_head), a_field); \ 61 | } \ 62 | if (ql_first(a_head) != (a_elm)) { \ 63 | qr_remove((a_elm), a_field); \ 64 | } else { \ 65 | ql_first(a_head) = NULL; \ 66 | } \ 67 | } while (0) 68 | 69 | #define ql_head_remove(a_head, a_type, a_field) do { \ 70 | a_type *t = ql_first(a_head); \ 71 | ql_remove((a_head), t, a_field); \ 72 | } while (0) 73 | 74 | #define ql_tail_remove(a_head, a_type, a_field) do { \ 75 | a_type *t = ql_last(a_head, a_field); \ 76 | ql_remove((a_head), t, a_field); \ 77 | } while (0) 78 | 79 | #define ql_foreach(a_var, a_head, a_field) \ 80 | qr_foreach((a_var), ql_first(a_head), a_field) 81 | 82 | #define ql_reverse_foreach(a_var, a_head, a_field) \ 83 | qr_reverse_foreach((a_var), ql_first(a_head), a_field) 84 | -------------------------------------------------------------------------------- /src/util/nc_util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_UTIL_H_ 19 | #define _NC_UTIL_H_ 20 | 21 | #ifdef HAVE_DEBUG_LOG 22 | # define NC_DEBUG_LOG 1 23 | #endif 24 | 25 | #ifdef HAVE_ASSERT_PANIC 26 | # define NC_ASSERT_PANIC 1 27 | #endif 28 | 29 | #ifdef HAVE_ASSERT_LOG 30 | # define NC_ASSERT_LOG 1 31 | #endif 32 | 33 | #ifdef HAVE_BACKTRACE 34 | # define NC_HAVE_BACKTRACE 1 35 | #endif 36 | 37 | #define _FILE_OFFSET_BITS 64 38 | 39 | typedef enum rstatus_s { 40 | NC_OK = 0, 41 | NC_ERROR = -1, 42 | NC_EAGAIN = -2, 43 | NC_ENOMEM = -3, 44 | } rstatus_t; 45 | 46 | typedef int err_t; /* error type */ 47 | 48 | struct array; 49 | struct string; 50 | struct context; 51 | struct conn; 52 | struct conn_tqh; 53 | struct mbuf; 54 | struct mhdr; 55 | struct conf; 56 | struct stats; 57 | 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | 65 | #include 66 | #include 67 | 68 | #include 69 | #include 70 | #include 71 | #include 72 | #include 73 | #include 74 | #include 75 | #include 76 | #include 77 | #include 78 | 79 | #include 80 | #include 81 | #include 82 | #include 83 | #include 84 | #include 85 | #include 86 | #include 87 | #include 88 | #include 89 | 90 | #include 91 | #include 92 | 93 | #include "nc_array.h" 94 | #include "nc_md5.h" 95 | #include "nc_rbtree.h" 96 | #include "nc_string.h" 97 | #include "nc_queue.h" 98 | #include "nc_log.h" 99 | #include "nc_file.h" 100 | #include "nc_util.h" 101 | 102 | #include "nc_event.h" 103 | #include "nc_mbuf.h" 104 | #include "nc_connection.h" 105 | #include "nc_server.h" 106 | 107 | #include "nc_conf.h" 108 | #include "nc_signal.h" 109 | #include "nc_misc.h" 110 | 111 | #include "sds.h" 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /deps/jemalloc/src/bitmap.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_BITMAP_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | /* Function prototypes for non-inline static functions. */ 6 | 7 | static size_t bits2groups(size_t nbits); 8 | 9 | /******************************************************************************/ 10 | 11 | static size_t 12 | bits2groups(size_t nbits) 13 | { 14 | 15 | return ((nbits >> LG_BITMAP_GROUP_NBITS) + 16 | !!(nbits & BITMAP_GROUP_NBITS_MASK)); 17 | } 18 | 19 | void 20 | bitmap_info_init(bitmap_info_t *binfo, size_t nbits) 21 | { 22 | unsigned i; 23 | size_t group_count; 24 | 25 | assert(nbits > 0); 26 | assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS)); 27 | 28 | /* 29 | * Compute the number of groups necessary to store nbits bits, and 30 | * progressively work upward through the levels until reaching a level 31 | * that requires only one group. 32 | */ 33 | binfo->levels[0].group_offset = 0; 34 | group_count = bits2groups(nbits); 35 | for (i = 1; group_count > 1; i++) { 36 | assert(i < BITMAP_MAX_LEVELS); 37 | binfo->levels[i].group_offset = binfo->levels[i-1].group_offset 38 | + group_count; 39 | group_count = bits2groups(group_count); 40 | } 41 | binfo->levels[i].group_offset = binfo->levels[i-1].group_offset 42 | + group_count; 43 | binfo->nlevels = i; 44 | binfo->nbits = nbits; 45 | } 46 | 47 | size_t 48 | bitmap_info_ngroups(const bitmap_info_t *binfo) 49 | { 50 | 51 | return (binfo->levels[binfo->nlevels].group_offset << LG_SIZEOF_BITMAP); 52 | } 53 | 54 | size_t 55 | bitmap_size(size_t nbits) 56 | { 57 | bitmap_info_t binfo; 58 | 59 | bitmap_info_init(&binfo, nbits); 60 | return (bitmap_info_ngroups(&binfo)); 61 | } 62 | 63 | void 64 | bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo) 65 | { 66 | size_t extra; 67 | unsigned i; 68 | 69 | /* 70 | * Bits are actually inverted with regard to the external bitmap 71 | * interface, so the bitmap starts out with all 1 bits, except for 72 | * trailing unused bits (if any). Note that each group uses bit 0 to 73 | * correspond to the first logical bit in the group, so extra bits 74 | * are the most significant bits of the last group. 75 | */ 76 | memset(bitmap, 0xffU, binfo->levels[binfo->nlevels].group_offset << 77 | LG_SIZEOF_BITMAP); 78 | extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK)) 79 | & BITMAP_GROUP_NBITS_MASK; 80 | if (extra != 0) 81 | bitmap[binfo->levels[1].group_offset - 1] >>= extra; 82 | for (i = 1; i < binfo->nlevels; i++) { 83 | size_t group_count = binfo->levels[i].group_offset - 84 | binfo->levels[i-1].group_offset; 85 | extra = (BITMAP_GROUP_NBITS - (group_count & 86 | BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK; 87 | if (extra != 0) 88 | bitmap[binfo->levels[i+1].group_offset - 1] >>= extra; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /deps/jemalloc/test/allocated.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | void * 5 | je_thread_start(void *arg) 6 | { 7 | int err; 8 | void *p; 9 | uint64_t a0, a1, d0, d1; 10 | uint64_t *ap0, *ap1, *dp0, *dp1; 11 | size_t sz, usize; 12 | 13 | sz = sizeof(a0); 14 | if ((err = mallctl("thread.allocated", &a0, &sz, NULL, 0))) { 15 | if (err == ENOENT) { 16 | #ifdef JEMALLOC_STATS 17 | assert(false); 18 | #endif 19 | goto label_return; 20 | } 21 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 22 | strerror(err)); 23 | exit(1); 24 | } 25 | sz = sizeof(ap0); 26 | if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) { 27 | if (err == ENOENT) { 28 | #ifdef JEMALLOC_STATS 29 | assert(false); 30 | #endif 31 | goto label_return; 32 | } 33 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 34 | strerror(err)); 35 | exit(1); 36 | } 37 | assert(*ap0 == a0); 38 | 39 | sz = sizeof(d0); 40 | if ((err = mallctl("thread.deallocated", &d0, &sz, NULL, 0))) { 41 | if (err == ENOENT) { 42 | #ifdef JEMALLOC_STATS 43 | assert(false); 44 | #endif 45 | goto label_return; 46 | } 47 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 48 | strerror(err)); 49 | exit(1); 50 | } 51 | sz = sizeof(dp0); 52 | if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) { 53 | if (err == ENOENT) { 54 | #ifdef JEMALLOC_STATS 55 | assert(false); 56 | #endif 57 | goto label_return; 58 | } 59 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 60 | strerror(err)); 61 | exit(1); 62 | } 63 | assert(*dp0 == d0); 64 | 65 | p = malloc(1); 66 | if (p == NULL) { 67 | malloc_printf("%s(): Error in malloc()\n", __func__); 68 | exit(1); 69 | } 70 | 71 | sz = sizeof(a1); 72 | mallctl("thread.allocated", &a1, &sz, NULL, 0); 73 | sz = sizeof(ap1); 74 | mallctl("thread.allocatedp", &ap1, &sz, NULL, 0); 75 | assert(*ap1 == a1); 76 | assert(ap0 == ap1); 77 | 78 | usize = malloc_usable_size(p); 79 | assert(a0 + usize <= a1); 80 | 81 | free(p); 82 | 83 | sz = sizeof(d1); 84 | mallctl("thread.deallocated", &d1, &sz, NULL, 0); 85 | sz = sizeof(dp1); 86 | mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0); 87 | assert(*dp1 == d1); 88 | assert(dp0 == dp1); 89 | 90 | assert(d0 + usize <= d1); 91 | 92 | label_return: 93 | return (NULL); 94 | } 95 | 96 | int 97 | main(void) 98 | { 99 | int ret = 0; 100 | je_thread_t thread; 101 | 102 | malloc_printf("Test begin\n"); 103 | 104 | je_thread_start(NULL); 105 | 106 | je_thread_create(&thread, je_thread_start, NULL); 107 | je_thread_join(thread, (void *)&ret); 108 | 109 | je_thread_start(NULL); 110 | 111 | je_thread_create(&thread, je_thread_start, NULL); 112 | je_thread_join(thread, (void *)&ret); 113 | 114 | je_thread_start(NULL); 115 | 116 | malloc_printf("Test end\n"); 117 | return (ret); 118 | } 119 | -------------------------------------------------------------------------------- /deps/lua/src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /deps/lua/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_NE, OPR_EQ, 30 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 46 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 47 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 48 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 49 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 50 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 51 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 52 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 53 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 54 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 55 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 56 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 59 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 60 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 61 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 62 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 63 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 64 | LUAI_FUNC int luaK_jump (FuncState *fs); 65 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 66 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 67 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 68 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 69 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 70 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 71 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 72 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 73 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/util/nc_connection.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_CONNECTION_H_ 19 | #define _NC_CONNECTION_H_ 20 | 21 | #include "nc_util.h" 22 | 23 | typedef rstatus_t (*conn_callback_t)(struct conn *); 24 | typedef rstatus_t (*cron_callback_t)(void *arg); /* arg: instance */ 25 | 26 | /* TODO: this file need modify */ 27 | struct conn { 28 | TAILQ_ENTRY(conn) conn_tqe; /* link in server_pool / server / free q */ 29 | void *owner; /* connection owner - srv */ 30 | void *data; /* msg in parse */ 31 | 32 | int fd; /* socket fd */ 33 | 34 | struct mhdr recv_queue; /* recv mbuf list */ 35 | size_t recv_queue_bytes; /* bytes in recv_queue */ 36 | struct mhdr send_queue; /* send mbuf list */ 37 | size_t send_queue_bytes; /* received (read) bytes */ 38 | 39 | conn_callback_t recv; /* recv (read) handler */ 40 | conn_callback_t send; /* send (write) handler */ 41 | conn_callback_t close; /* close handler */ 42 | 43 | size_t recv_bytes; /* received (read) bytes */ 44 | size_t send_bytes; /* sent (written) bytes */ 45 | 46 | uint32_t events; /* connection io events */ 47 | err_t err; /* connection errno */ 48 | unsigned recv_active:1; /* recv active? */ 49 | unsigned recv_ready:1; /* recv ready? */ 50 | unsigned send_active:1; /* send active? */ 51 | unsigned send_ready:1; /* send ready? */ 52 | 53 | unsigned listen:1; /* listen socket? */ 54 | unsigned eof:1; /* eof? aka passive close? */ 55 | unsigned done:1; /* done? aka close? */ 56 | }; 57 | 58 | TAILQ_HEAD(conn_tqh, conn); 59 | 60 | struct conn *conn_get(void *owner); 61 | void conn_put(struct conn *conn); 62 | 63 | void conn_init(void); 64 | void conn_deinit(void); 65 | 66 | rstatus_t conn_recv(struct conn *conn); 67 | rstatus_t conn_send(struct conn *conn); 68 | rstatus_t conn_add_out(struct conn *conn); 69 | rstatus_t conn_del_out(struct conn *conn); 70 | rstatus_t conn_sendq_append(struct conn *conn, char *pos, size_t n); 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/ckh.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct ckh_s ckh_t; 5 | typedef struct ckhc_s ckhc_t; 6 | 7 | /* Typedefs to allow easy function pointer passing. */ 8 | typedef void ckh_hash_t (const void *, unsigned, size_t *, size_t *); 9 | typedef bool ckh_keycomp_t (const void *, const void *); 10 | 11 | /* Maintain counters used to get an idea of performance. */ 12 | /* #define CKH_COUNT */ 13 | /* Print counter values in ckh_delete() (requires CKH_COUNT). */ 14 | /* #define CKH_VERBOSE */ 15 | 16 | /* 17 | * There are 2^LG_CKH_BUCKET_CELLS cells in each hash table bucket. Try to fit 18 | * one bucket per L1 cache line. 19 | */ 20 | #define LG_CKH_BUCKET_CELLS (LG_CACHELINE - LG_SIZEOF_PTR - 1) 21 | 22 | #endif /* JEMALLOC_H_TYPES */ 23 | /******************************************************************************/ 24 | #ifdef JEMALLOC_H_STRUCTS 25 | 26 | /* Hash table cell. */ 27 | struct ckhc_s { 28 | const void *key; 29 | const void *data; 30 | }; 31 | 32 | struct ckh_s { 33 | #ifdef CKH_COUNT 34 | /* Counters used to get an idea of performance. */ 35 | uint64_t ngrows; 36 | uint64_t nshrinks; 37 | uint64_t nshrinkfails; 38 | uint64_t ninserts; 39 | uint64_t nrelocs; 40 | #endif 41 | 42 | /* Used for pseudo-random number generation. */ 43 | #define CKH_A 1103515241 44 | #define CKH_C 12347 45 | uint32_t prng_state; 46 | 47 | /* Total number of items. */ 48 | size_t count; 49 | 50 | /* 51 | * Minimum and current number of hash table buckets. There are 52 | * 2^LG_CKH_BUCKET_CELLS cells per bucket. 53 | */ 54 | unsigned lg_minbuckets; 55 | unsigned lg_curbuckets; 56 | 57 | /* Hash and comparison functions. */ 58 | ckh_hash_t *hash; 59 | ckh_keycomp_t *keycomp; 60 | 61 | /* Hash table with 2^lg_curbuckets buckets. */ 62 | ckhc_t *tab; 63 | }; 64 | 65 | #endif /* JEMALLOC_H_STRUCTS */ 66 | /******************************************************************************/ 67 | #ifdef JEMALLOC_H_EXTERNS 68 | 69 | bool ckh_new(ckh_t *ckh, size_t minitems, ckh_hash_t *hash, 70 | ckh_keycomp_t *keycomp); 71 | void ckh_delete(ckh_t *ckh); 72 | size_t ckh_count(ckh_t *ckh); 73 | bool ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data); 74 | bool ckh_insert(ckh_t *ckh, const void *key, const void *data); 75 | bool ckh_remove(ckh_t *ckh, const void *searchkey, void **key, 76 | void **data); 77 | bool ckh_search(ckh_t *ckh, const void *seachkey, void **key, void **data); 78 | void ckh_string_hash(const void *key, unsigned minbits, size_t *hash1, 79 | size_t *hash2); 80 | bool ckh_string_keycomp(const void *k1, const void *k2); 81 | void ckh_pointer_hash(const void *key, unsigned minbits, size_t *hash1, 82 | size_t *hash2); 83 | bool ckh_pointer_keycomp(const void *k1, const void *k2); 84 | 85 | #endif /* JEMALLOC_H_EXTERNS */ 86 | /******************************************************************************/ 87 | #ifdef JEMALLOC_H_INLINES 88 | 89 | #endif /* JEMALLOC_H_INLINES */ 90 | /******************************************************************************/ 91 | -------------------------------------------------------------------------------- /test/unit/test_basic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding: utf-8 3 | 4 | from common import * 5 | 6 | def test_setget(kv = {'k': 'v'}): 7 | conn = get_conn() 8 | 9 | for k, v in kv.items(): 10 | rst = conn.set(k, v) 11 | getv = conn.get(k) 12 | assert(len(getv) == len(v)) 13 | assert(getv == v) 14 | 15 | rst = conn.delete(k) 16 | assert(conn.get(k) == None) 17 | 18 | def test_set_get_large(): 19 | kv = {} 20 | for i in range(T_LARGE): 21 | k = os.urandom(10 * T_LARGE) 22 | if T_LARGE > T_LARGE_DEFAULT: 23 | v = os.urandom(1024*1024*16+1024) #16M 24 | else: 25 | v = os.urandom(1024*16+1024) #16K 26 | kv[k] = v 27 | 28 | test_setget(kv) 29 | 30 | def test_set_get_sp(): 31 | kv = { 32 | 'a': '', 33 | 'b': '\n', 34 | 'c': '\r', 35 | 'd': '\r\n', 36 | } 37 | 38 | test_setget(kv) 39 | 40 | def test_expire(): 41 | conn = get_conn() 42 | 43 | rst = conn.set('k', 'v') 44 | 45 | assert(conn.ttl('k') == None) #TODO: why this is None 46 | 47 | rst = conn.expire('k', 2) 48 | time.sleep(.5) 49 | assert(conn.ttl('k') == 1) 50 | 51 | time.sleep(2) 52 | 53 | assert(conn.ttl('k') == -2) 54 | assert(conn.get('k') == None) 55 | 56 | def test_scan(): 57 | conn = get_conn() 58 | 59 | kv = {'kkk-%s' % i : 'vvv-%s' % i for i in range(12)} 60 | for k, v in kv.items(): 61 | conn.set(k, v) 62 | 63 | all_keys = [] 64 | cursor = '0' 65 | while True: 66 | cursor, keys = conn.scan(cursor) 67 | all_keys = all_keys + keys 68 | 69 | if '0' == cursor: 70 | break 71 | assert set(all_keys) == set(kv.keys()) 72 | 73 | def test_vscan(): 74 | conn = get_conn() 75 | 76 | kv = {'kkk-%s' % i : 'vvv-%s' % i for i in range(12)} 77 | for k, v in kv.items(): 78 | conn.set(k, v) 79 | conn.expire(k, 3) 80 | 81 | all_keys = [] 82 | cursor = '0' 83 | while True: 84 | cursor, arr = conn.vscan(cursor) 85 | for k, v, e in [arr[i*3:i*3+3] for i in range(len(arr)/3)]: 86 | print k, v, e 87 | all_keys.append(k) 88 | assert(v == kv[k]) 89 | # print 1000 * (time.time() + 3), e 90 | assert(abs(1000 * (time.time() + 3) - int(e)) < 50) # TODO: assert expire 91 | 92 | if '0' == cursor: 93 | break 94 | assert set(all_keys) == set(kv.keys()) 95 | 96 | def test_compact_and_eliminate(): 97 | if T_LARGE == T_LARGE_DEFAULT: 98 | return 99 | 100 | conn = get_conn() 101 | 102 | kv = {'kkk-%s' % i : 'vvv-%s' % i for i in range(10000*10)} 103 | for k, v in kv.items(): 104 | conn.set(k, v) 105 | conn.expire(k, 1) 106 | if k.endswith('0000'): 107 | print conn.info() 108 | 109 | time.sleep(1) 110 | conn.eliminate() 111 | conn.compact() 112 | print conn.info() 113 | 114 | def just_wait(): 115 | time.sleep(60*60) 116 | 117 | -------------------------------------------------------------------------------- /deps/jemalloc/test/posix_memalign.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define CHUNK 0x400000 5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ 6 | #define MAXALIGN ((size_t)0x2000000LU) 7 | #define NITER 4 8 | 9 | int 10 | main(void) 11 | { 12 | size_t alignment, size, total; 13 | unsigned i; 14 | int err; 15 | void *p, *ps[NITER]; 16 | 17 | malloc_printf("Test begin\n"); 18 | 19 | /* Test error conditions. */ 20 | for (alignment = 0; alignment < sizeof(void *); alignment++) { 21 | err = posix_memalign(&p, alignment, 1); 22 | if (err != EINVAL) { 23 | malloc_printf( 24 | "Expected error for invalid alignment %zu\n", 25 | alignment); 26 | } 27 | } 28 | 29 | for (alignment = sizeof(size_t); alignment < MAXALIGN; 30 | alignment <<= 1) { 31 | err = posix_memalign(&p, alignment + 1, 1); 32 | if (err == 0) { 33 | malloc_printf( 34 | "Expected error for invalid alignment %zu\n", 35 | alignment + 1); 36 | } 37 | } 38 | 39 | #if LG_SIZEOF_PTR == 3 40 | alignment = UINT64_C(0x8000000000000000); 41 | size = UINT64_C(0x8000000000000000); 42 | #else 43 | alignment = 0x80000000LU; 44 | size = 0x80000000LU; 45 | #endif 46 | err = posix_memalign(&p, alignment, size); 47 | if (err == 0) { 48 | malloc_printf( 49 | "Expected error for posix_memalign(&p, %zu, %zu)\n", 50 | alignment, size); 51 | } 52 | 53 | #if LG_SIZEOF_PTR == 3 54 | alignment = UINT64_C(0x4000000000000000); 55 | size = UINT64_C(0x8400000000000001); 56 | #else 57 | alignment = 0x40000000LU; 58 | size = 0x84000001LU; 59 | #endif 60 | err = posix_memalign(&p, alignment, size); 61 | if (err == 0) { 62 | malloc_printf( 63 | "Expected error for posix_memalign(&p, %zu, %zu)\n", 64 | alignment, size); 65 | } 66 | 67 | alignment = 0x10LU; 68 | #if LG_SIZEOF_PTR == 3 69 | size = UINT64_C(0xfffffffffffffff0); 70 | #else 71 | size = 0xfffffff0LU; 72 | #endif 73 | err = posix_memalign(&p, alignment, size); 74 | if (err == 0) { 75 | malloc_printf( 76 | "Expected error for posix_memalign(&p, %zu, %zu)\n", 77 | alignment, size); 78 | } 79 | 80 | for (i = 0; i < NITER; i++) 81 | ps[i] = NULL; 82 | 83 | for (alignment = 8; 84 | alignment <= MAXALIGN; 85 | alignment <<= 1) { 86 | total = 0; 87 | malloc_printf("Alignment: %zu\n", alignment); 88 | for (size = 1; 89 | size < 3 * alignment && size < (1U << 31); 90 | size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { 91 | for (i = 0; i < NITER; i++) { 92 | err = posix_memalign(&ps[i], 93 | alignment, size); 94 | if (err) { 95 | malloc_printf( 96 | "Error for size %zu (%#zx): %s\n", 97 | size, size, strerror(err)); 98 | exit(1); 99 | } 100 | total += malloc_usable_size(ps[i]); 101 | if (total >= (MAXALIGN << 1)) 102 | break; 103 | } 104 | for (i = 0; i < NITER; i++) { 105 | if (ps[i] != NULL) { 106 | free(ps[i]); 107 | ps[i] = NULL; 108 | } 109 | } 110 | } 111 | } 112 | 113 | malloc_printf("Test end\n"); 114 | return (0); 115 | } 116 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/mutex.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct malloc_mutex_s malloc_mutex_t; 5 | 6 | #ifdef _WIN32 7 | # define MALLOC_MUTEX_INITIALIZER 8 | #elif (defined(JEMALLOC_OSSPIN)) 9 | # define MALLOC_MUTEX_INITIALIZER {0} 10 | #elif (defined(JEMALLOC_MUTEX_INIT_CB)) 11 | # define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, NULL} 12 | #else 13 | # if (defined(PTHREAD_MUTEX_ADAPTIVE_NP) && \ 14 | defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)) 15 | # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP 16 | # define MALLOC_MUTEX_INITIALIZER {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP} 17 | # else 18 | # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT 19 | # define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER} 20 | # endif 21 | #endif 22 | 23 | #endif /* JEMALLOC_H_TYPES */ 24 | /******************************************************************************/ 25 | #ifdef JEMALLOC_H_STRUCTS 26 | 27 | struct malloc_mutex_s { 28 | #ifdef _WIN32 29 | CRITICAL_SECTION lock; 30 | #elif (defined(JEMALLOC_OSSPIN)) 31 | OSSpinLock lock; 32 | #elif (defined(JEMALLOC_MUTEX_INIT_CB)) 33 | pthread_mutex_t lock; 34 | malloc_mutex_t *postponed_next; 35 | #else 36 | pthread_mutex_t lock; 37 | #endif 38 | }; 39 | 40 | #endif /* JEMALLOC_H_STRUCTS */ 41 | /******************************************************************************/ 42 | #ifdef JEMALLOC_H_EXTERNS 43 | 44 | #ifdef JEMALLOC_LAZY_LOCK 45 | extern bool isthreaded; 46 | #else 47 | # undef isthreaded /* Undo private_namespace.h definition. */ 48 | # define isthreaded true 49 | #endif 50 | 51 | bool malloc_mutex_init(malloc_mutex_t *mutex); 52 | void malloc_mutex_prefork(malloc_mutex_t *mutex); 53 | void malloc_mutex_postfork_parent(malloc_mutex_t *mutex); 54 | void malloc_mutex_postfork_child(malloc_mutex_t *mutex); 55 | bool mutex_boot(void); 56 | 57 | #endif /* JEMALLOC_H_EXTERNS */ 58 | /******************************************************************************/ 59 | #ifdef JEMALLOC_H_INLINES 60 | 61 | #ifndef JEMALLOC_ENABLE_INLINE 62 | void malloc_mutex_lock(malloc_mutex_t *mutex); 63 | void malloc_mutex_unlock(malloc_mutex_t *mutex); 64 | #endif 65 | 66 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_)) 67 | JEMALLOC_INLINE void 68 | malloc_mutex_lock(malloc_mutex_t *mutex) 69 | { 70 | 71 | if (isthreaded) { 72 | #ifdef _WIN32 73 | EnterCriticalSection(&mutex->lock); 74 | #elif (defined(JEMALLOC_OSSPIN)) 75 | OSSpinLockLock(&mutex->lock); 76 | #else 77 | pthread_mutex_lock(&mutex->lock); 78 | #endif 79 | } 80 | } 81 | 82 | JEMALLOC_INLINE void 83 | malloc_mutex_unlock(malloc_mutex_t *mutex) 84 | { 85 | 86 | if (isthreaded) { 87 | #ifdef _WIN32 88 | LeaveCriticalSection(&mutex->lock); 89 | #elif (defined(JEMALLOC_OSSPIN)) 90 | OSSpinLockUnlock(&mutex->lock); 91 | #else 92 | pthread_mutex_unlock(&mutex->lock); 93 | #endif 94 | } 95 | } 96 | #endif 97 | 98 | #endif /* JEMALLOC_H_INLINES */ 99 | /******************************************************************************/ 100 | -------------------------------------------------------------------------------- /deps/jemalloc/include/jemalloc/internal/mb.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | #endif /* JEMALLOC_H_EXTERNS */ 13 | /******************************************************************************/ 14 | #ifdef JEMALLOC_H_INLINES 15 | 16 | #ifndef JEMALLOC_ENABLE_INLINE 17 | void mb_write(void); 18 | #endif 19 | 20 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MB_C_)) 21 | #ifdef __i386__ 22 | /* 23 | * According to the Intel Architecture Software Developer's Manual, current 24 | * processors execute instructions in order from the perspective of other 25 | * processors in a multiprocessor system, but 1) Intel reserves the right to 26 | * change that, and 2) the compiler's optimizer could re-order instructions if 27 | * there weren't some form of barrier. Therefore, even if running on an 28 | * architecture that does not need memory barriers (everything through at least 29 | * i686), an "optimizer barrier" is necessary. 30 | */ 31 | JEMALLOC_INLINE void 32 | mb_write(void) 33 | { 34 | 35 | # if 0 36 | /* This is a true memory barrier. */ 37 | asm volatile ("pusha;" 38 | "xor %%eax,%%eax;" 39 | "cpuid;" 40 | "popa;" 41 | : /* Outputs. */ 42 | : /* Inputs. */ 43 | : "memory" /* Clobbers. */ 44 | ); 45 | #else 46 | /* 47 | * This is hopefully enough to keep the compiler from reordering 48 | * instructions around this one. 49 | */ 50 | asm volatile ("nop;" 51 | : /* Outputs. */ 52 | : /* Inputs. */ 53 | : "memory" /* Clobbers. */ 54 | ); 55 | #endif 56 | } 57 | #elif (defined(__amd64__) || defined(__x86_64__)) 58 | JEMALLOC_INLINE void 59 | mb_write(void) 60 | { 61 | 62 | asm volatile ("sfence" 63 | : /* Outputs. */ 64 | : /* Inputs. */ 65 | : "memory" /* Clobbers. */ 66 | ); 67 | } 68 | #elif defined(__powerpc__) 69 | JEMALLOC_INLINE void 70 | mb_write(void) 71 | { 72 | 73 | asm volatile ("eieio" 74 | : /* Outputs. */ 75 | : /* Inputs. */ 76 | : "memory" /* Clobbers. */ 77 | ); 78 | } 79 | #elif defined(__sparc64__) 80 | JEMALLOC_INLINE void 81 | mb_write(void) 82 | { 83 | 84 | asm volatile ("membar #StoreStore" 85 | : /* Outputs. */ 86 | : /* Inputs. */ 87 | : "memory" /* Clobbers. */ 88 | ); 89 | } 90 | #elif defined(__tile__) 91 | JEMALLOC_INLINE void 92 | mb_write(void) 93 | { 94 | 95 | __sync_synchronize(); 96 | } 97 | #else 98 | /* 99 | * This is much slower than a simple memory barrier, but the semantics of mutex 100 | * unlock make this work. 101 | */ 102 | JEMALLOC_INLINE void 103 | mb_write(void) 104 | { 105 | malloc_mutex_t mtx; 106 | 107 | malloc_mutex_init(&mtx); 108 | malloc_mutex_lock(&mtx); 109 | malloc_mutex_unlock(&mtx); 110 | } 111 | #endif 112 | #endif 113 | 114 | #endif /* JEMALLOC_H_INLINES */ 115 | /******************************************************************************/ 116 | -------------------------------------------------------------------------------- /deps/jemalloc/test/aligned_alloc.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define CHUNK 0x400000 5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ 6 | #define MAXALIGN ((size_t)0x2000000LU) 7 | #define NITER 4 8 | 9 | int 10 | main(void) 11 | { 12 | size_t alignment, size, total; 13 | unsigned i; 14 | void *p, *ps[NITER]; 15 | 16 | malloc_printf("Test begin\n"); 17 | 18 | /* Test error conditions. */ 19 | alignment = 0; 20 | set_errno(0); 21 | p = aligned_alloc(alignment, 1); 22 | if (p != NULL || get_errno() != EINVAL) { 23 | malloc_printf( 24 | "Expected error for invalid alignment %zu\n", alignment); 25 | } 26 | 27 | for (alignment = sizeof(size_t); alignment < MAXALIGN; 28 | alignment <<= 1) { 29 | set_errno(0); 30 | p = aligned_alloc(alignment + 1, 1); 31 | if (p != NULL || get_errno() != EINVAL) { 32 | malloc_printf( 33 | "Expected error for invalid alignment %zu\n", 34 | alignment + 1); 35 | } 36 | } 37 | 38 | #if LG_SIZEOF_PTR == 3 39 | alignment = UINT64_C(0x8000000000000000); 40 | size = UINT64_C(0x8000000000000000); 41 | #else 42 | alignment = 0x80000000LU; 43 | size = 0x80000000LU; 44 | #endif 45 | set_errno(0); 46 | p = aligned_alloc(alignment, size); 47 | if (p != NULL || get_errno() != ENOMEM) { 48 | malloc_printf( 49 | "Expected error for aligned_alloc(%zu, %zu)\n", 50 | alignment, size); 51 | } 52 | 53 | #if LG_SIZEOF_PTR == 3 54 | alignment = UINT64_C(0x4000000000000000); 55 | size = UINT64_C(0x8400000000000001); 56 | #else 57 | alignment = 0x40000000LU; 58 | size = 0x84000001LU; 59 | #endif 60 | set_errno(0); 61 | p = aligned_alloc(alignment, size); 62 | if (p != NULL || get_errno() != ENOMEM) { 63 | malloc_printf( 64 | "Expected error for aligned_alloc(%zu, %zu)\n", 65 | alignment, size); 66 | } 67 | 68 | alignment = 0x10LU; 69 | #if LG_SIZEOF_PTR == 3 70 | size = UINT64_C(0xfffffffffffffff0); 71 | #else 72 | size = 0xfffffff0LU; 73 | #endif 74 | set_errno(0); 75 | p = aligned_alloc(alignment, size); 76 | if (p != NULL || get_errno() != ENOMEM) { 77 | malloc_printf( 78 | "Expected error for aligned_alloc(&p, %zu, %zu)\n", 79 | alignment, size); 80 | } 81 | 82 | for (i = 0; i < NITER; i++) 83 | ps[i] = NULL; 84 | 85 | for (alignment = 8; 86 | alignment <= MAXALIGN; 87 | alignment <<= 1) { 88 | total = 0; 89 | malloc_printf("Alignment: %zu\n", alignment); 90 | for (size = 1; 91 | size < 3 * alignment && size < (1U << 31); 92 | size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { 93 | for (i = 0; i < NITER; i++) { 94 | ps[i] = aligned_alloc(alignment, size); 95 | if (ps[i] == NULL) { 96 | char buf[BUFERROR_BUF]; 97 | 98 | buferror(buf, sizeof(buf)); 99 | malloc_printf( 100 | "Error for size %zu (%#zx): %s\n", 101 | size, size, buf); 102 | exit(1); 103 | } 104 | total += malloc_usable_size(ps[i]); 105 | if (total >= (MAXALIGN << 1)) 106 | break; 107 | } 108 | for (i = 0; i < NITER; i++) { 109 | if (ps[i] != NULL) { 110 | free(ps[i]); 111 | ps[i] = NULL; 112 | } 113 | } 114 | } 115 | } 116 | 117 | malloc_printf("Test end\n"); 118 | return (0); 119 | } 120 | -------------------------------------------------------------------------------- /deps/lua/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADBOOL", 20 | "LOADNIL", 21 | "GETUPVAL", 22 | "GETGLOBAL", 23 | "GETTABLE", 24 | "SETGLOBAL", 25 | "SETUPVAL", 26 | "SETTABLE", 27 | "NEWTABLE", 28 | "SELF", 29 | "ADD", 30 | "SUB", 31 | "MUL", 32 | "DIV", 33 | "MOD", 34 | "POW", 35 | "UNM", 36 | "NOT", 37 | "LEN", 38 | "CONCAT", 39 | "JMP", 40 | "EQ", 41 | "LT", 42 | "LE", 43 | "TEST", 44 | "TESTSET", 45 | "CALL", 46 | "TAILCALL", 47 | "RETURN", 48 | "FORLOOP", 49 | "FORPREP", 50 | "TFORLOOP", 51 | "SETLIST", 52 | "CLOSE", 53 | "CLOSURE", 54 | "VARARG", 55 | NULL 56 | }; 57 | 58 | 59 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 60 | 61 | const lu_byte luaP_opmodes[NUM_OPCODES] = { 62 | /* T A B C mode opcode */ 63 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 64 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 65 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 66 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 67 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 68 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 69 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 70 | ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 71 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 72 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 73 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 74 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 75 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 76 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 77 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 81 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 82 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 83 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 84 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 85 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 86 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 87 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 88 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 89 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 90 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 91 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 92 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 93 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 94 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 95 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 96 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 97 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 98 | ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 99 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 100 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 101 | }; 102 | 103 | -------------------------------------------------------------------------------- /src/util/nc_string.c: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #include "nc_util.h" 19 | 20 | /* 21 | * String (struct string) is a sequence of unsigned char objects terminated 22 | * by the null character '\0'. The length of the string is pre-computed and 23 | * made available explicitly as an additional field. This means that we don't 24 | * have to walk the entire character sequence until the null terminating 25 | * character everytime that the length of the String is requested 26 | * 27 | * The only way to create a String is to initialize it using, string_init() 28 | * and duplicate an existing String - string_duplicate() or copy an existing 29 | * raw sequence of character bytes - string_copy(). Such String's must be 30 | * freed using string_deinit() 31 | * 32 | * We can also create String as reference to raw string - string_set_raw() 33 | * or to text string - string_set_text() or string(). Such String don't have 34 | * to be freed. 35 | */ 36 | 37 | void 38 | string_init(struct string *str) 39 | { 40 | str->len = 0; 41 | str->data = NULL; 42 | } 43 | 44 | void 45 | string_deinit(struct string *str) 46 | { 47 | ASSERT((str->len == 0 && str->data == NULL) || 48 | (str->len != 0 && str->data != NULL)); 49 | 50 | if (str->data != NULL) { 51 | nc_free(str->data); 52 | string_init(str); 53 | } 54 | } 55 | 56 | bool 57 | string_empty(const struct string *str) 58 | { 59 | ASSERT((str->len == 0 && str->data == NULL) || 60 | (str->len != 0 && str->data != NULL)); 61 | return str->len == 0 ? true : false; 62 | } 63 | 64 | rstatus_t 65 | string_duplicate(struct string *dst, const struct string *src) 66 | { 67 | ASSERT(dst->len == 0 && dst->data == NULL); 68 | ASSERT(src->len != 0 && src->data != NULL); 69 | 70 | dst->data = nc_strndup(src->data, src->len + 1); 71 | if (dst->data == NULL) { 72 | return NC_ENOMEM; 73 | } 74 | 75 | dst->len = src->len; 76 | dst->data[dst->len] = '\0'; 77 | 78 | return NC_OK; 79 | } 80 | 81 | rstatus_t 82 | string_copy(struct string *dst, const uint8_t *src, uint32_t srclen) 83 | { 84 | ASSERT(dst->len == 0 && dst->data == NULL); 85 | ASSERT(src != NULL && srclen != 0); 86 | 87 | dst->data = nc_strndup(src, srclen + 1); 88 | if (dst->data == NULL) { 89 | return NC_ENOMEM; 90 | } 91 | 92 | dst->len = srclen; 93 | dst->data[dst->len] = '\0'; 94 | 95 | return NC_OK; 96 | } 97 | 98 | int 99 | string_compare(const struct string *s1, const struct string *s2) 100 | { 101 | if (s1->len != s2->len) { 102 | return s1->len > s2->len ? 1 : -1; 103 | } 104 | 105 | return nc_strncmp(s1->data, s2->data, s1->len); 106 | } 107 | -------------------------------------------------------------------------------- /deps/jemalloc/test/rallocm.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | int 5 | main(void) 6 | { 7 | size_t pagesize; 8 | void *p, *q; 9 | size_t sz, tsz; 10 | int r; 11 | 12 | malloc_printf("Test begin\n"); 13 | 14 | /* Get page size. */ 15 | { 16 | #ifdef _WIN32 17 | SYSTEM_INFO si; 18 | GetSystemInfo(&si); 19 | pagesize = (size_t)si.dwPageSize; 20 | #else 21 | long result = sysconf(_SC_PAGESIZE); 22 | assert(result != -1); 23 | pagesize = (size_t)result; 24 | #endif 25 | } 26 | 27 | r = allocm(&p, &sz, 42, 0); 28 | if (r != ALLOCM_SUCCESS) { 29 | malloc_printf("Unexpected allocm() error\n"); 30 | abort(); 31 | } 32 | 33 | q = p; 34 | r = rallocm(&q, &tsz, sz, 0, ALLOCM_NO_MOVE); 35 | if (r != ALLOCM_SUCCESS) 36 | malloc_printf("Unexpected rallocm() error\n"); 37 | if (q != p) 38 | malloc_printf("Unexpected object move\n"); 39 | if (tsz != sz) { 40 | malloc_printf("Unexpected size change: %zu --> %zu\n", 41 | sz, tsz); 42 | } 43 | 44 | q = p; 45 | r = rallocm(&q, &tsz, sz, 5, ALLOCM_NO_MOVE); 46 | if (r != ALLOCM_SUCCESS) 47 | malloc_printf("Unexpected rallocm() error\n"); 48 | if (q != p) 49 | malloc_printf("Unexpected object move\n"); 50 | if (tsz != sz) { 51 | malloc_printf("Unexpected size change: %zu --> %zu\n", 52 | sz, tsz); 53 | } 54 | 55 | q = p; 56 | r = rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE); 57 | if (r != ALLOCM_ERR_NOT_MOVED) 58 | malloc_printf("Unexpected rallocm() result\n"); 59 | if (q != p) 60 | malloc_printf("Unexpected object move\n"); 61 | if (tsz != sz) { 62 | malloc_printf("Unexpected size change: %zu --> %zu\n", 63 | sz, tsz); 64 | } 65 | 66 | q = p; 67 | r = rallocm(&q, &tsz, sz + 5, 0, 0); 68 | if (r != ALLOCM_SUCCESS) 69 | malloc_printf("Unexpected rallocm() error\n"); 70 | if (q == p) 71 | malloc_printf("Expected object move\n"); 72 | if (tsz == sz) { 73 | malloc_printf("Expected size change: %zu --> %zu\n", 74 | sz, tsz); 75 | } 76 | p = q; 77 | sz = tsz; 78 | 79 | r = rallocm(&q, &tsz, pagesize*2, 0, 0); 80 | if (r != ALLOCM_SUCCESS) 81 | malloc_printf("Unexpected rallocm() error\n"); 82 | if (q == p) 83 | malloc_printf("Expected object move\n"); 84 | if (tsz == sz) { 85 | malloc_printf("Expected size change: %zu --> %zu\n", 86 | sz, tsz); 87 | } 88 | p = q; 89 | sz = tsz; 90 | 91 | r = rallocm(&q, &tsz, pagesize*4, 0, 0); 92 | if (r != ALLOCM_SUCCESS) 93 | malloc_printf("Unexpected rallocm() error\n"); 94 | if (tsz == sz) { 95 | malloc_printf("Expected size change: %zu --> %zu\n", 96 | sz, tsz); 97 | } 98 | p = q; 99 | sz = tsz; 100 | 101 | r = rallocm(&q, &tsz, pagesize*2, 0, ALLOCM_NO_MOVE); 102 | if (r != ALLOCM_SUCCESS) 103 | malloc_printf("Unexpected rallocm() error\n"); 104 | if (q != p) 105 | malloc_printf("Unexpected object move\n"); 106 | if (tsz == sz) { 107 | malloc_printf("Expected size change: %zu --> %zu\n", 108 | sz, tsz); 109 | } 110 | sz = tsz; 111 | 112 | r = rallocm(&q, &tsz, pagesize*4, 0, ALLOCM_NO_MOVE); 113 | if (r != ALLOCM_SUCCESS) 114 | malloc_printf("Unexpected rallocm() error\n"); 115 | if (q != p) 116 | malloc_printf("Unexpected object move\n"); 117 | if (tsz == sz) { 118 | malloc_printf("Expected size change: %zu --> %zu\n", 119 | sz, tsz); 120 | } 121 | sz = tsz; 122 | 123 | dallocm(p, 0); 124 | 125 | malloc_printf("Test end\n"); 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /test/lib/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | import sys 4 | import time 5 | import copy 6 | import thread 7 | import socket 8 | import threading 9 | import logging 10 | import inspect 11 | import argparse 12 | import telnetlib 13 | import redis 14 | import random 15 | import redis 16 | import json 17 | import glob 18 | import commands 19 | 20 | from collections import defaultdict 21 | from argparse import RawTextHelpFormatter 22 | 23 | from string import Template 24 | 25 | PWD = os.path.dirname(os.path.realpath(__file__)) 26 | WORKDIR = os.path.join(PWD, '../') 27 | 28 | 29 | def getenv(key, default): 30 | if key in os.environ: 31 | return os.environ[key] 32 | return default 33 | 34 | logfile = getenv('TEST_LOGFILE', 't.log') 35 | if logfile == '-': 36 | logging.basicConfig(format="%(asctime)-15s [%(threadName)s] [%(levelname)s] %(message)s", level=logging.DEBUG) 37 | else: 38 | logging.basicConfig(filename=logfile, format="%(asctime)-15s [%(threadName)s] [%(levelname)s] %(message)s", level=logging.DEBUG) 39 | 40 | logging.info("test running!!!!!!") 41 | 42 | def strstr(s1, s2): 43 | return s1.find(s2) != -1 44 | 45 | def lets_sleep(SLEEP_TIME = 0.1): 46 | time.sleep(SLEEP_TIME) 47 | 48 | def TT(template, args): #todo: modify all 49 | return Template(template).substitute(args) 50 | 51 | def nothrow(ExceptionToCheck=Exception, logger=None): 52 | def deco_retry(f): 53 | def f_retry(*args, **kwargs): 54 | try: 55 | return f(*args, **kwargs) 56 | except ExceptionToCheck, e: 57 | if logger: 58 | logger.info(e) 59 | else: 60 | print str(e) 61 | return f_retry # true decorator 62 | return deco_retry 63 | 64 | @nothrow(Exception) 65 | def test_nothrow(): 66 | raise Exception('exception: xx') 67 | 68 | def json_encode(j): 69 | return json.dumps(j, indent=4, cls=MyEncoder) 70 | 71 | def json_decode(j): 72 | return json.loads(j) 73 | 74 | #commands dose not work on windows.. 75 | def system(cmd, log_fun=logging.info): 76 | if log_fun: log_fun(cmd) 77 | r = commands.getoutput(cmd) 78 | return r 79 | 80 | def shorten(s, l=80): 81 | if len(s)<=l: 82 | return s 83 | return s[:l-3] + '...' 84 | 85 | def assert_true(a): 86 | assert a, 'assert fail: except true, got %s' % a 87 | 88 | def assert_equal(a, b): 89 | assert a == b, 'assert fail: %s vs %s' % (shorten(str(a)), shorten(str(b))) 90 | 91 | def assert_raises(exception_cls, callable, *args, **kwargs): 92 | try: 93 | callable(*args, **kwargs) 94 | except exception_cls as e: 95 | return e 96 | except Exception as e: 97 | assert False, 'assert_raises %s but raised: %s' % (exception_cls, e) 98 | assert False, 'assert_raises %s but nothing raise' % (exception_cls) 99 | 100 | def assert_fail(err_response, callable, *args, **kwargs): 101 | try: 102 | callable(*args, **kwargs) 103 | assert False, 'assert_fail %s but nothing raise' % (err_response) 104 | except Exception as e: 105 | #assert strstr(str(e), err_response), 'assert "%s" but got "%s"' % (err_response, e) 106 | assert re.search(err_response, str(e)), 'assert "%s" but got "%s"' % (err_response, e) 107 | 108 | if __name__ == "__main__": 109 | test_nothrow() 110 | 111 | # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 112 | -------------------------------------------------------------------------------- /deps/jemalloc/src/base.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_BASE_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | /* Data. */ 6 | 7 | static malloc_mutex_t base_mtx; 8 | 9 | /* 10 | * Current pages that are being used for internal memory allocations. These 11 | * pages are carved up in cacheline-size quanta, so that there is no chance of 12 | * false cache line sharing. 13 | */ 14 | static void *base_pages; 15 | static void *base_next_addr; 16 | static void *base_past_addr; /* Addr immediately past base_pages. */ 17 | static extent_node_t *base_nodes; 18 | 19 | /******************************************************************************/ 20 | /* Function prototypes for non-inline static functions. */ 21 | 22 | static bool base_pages_alloc(size_t minsize); 23 | 24 | /******************************************************************************/ 25 | 26 | static bool 27 | base_pages_alloc(size_t minsize) 28 | { 29 | size_t csize; 30 | bool zero; 31 | 32 | assert(minsize != 0); 33 | csize = CHUNK_CEILING(minsize); 34 | zero = false; 35 | base_pages = chunk_alloc(csize, chunksize, true, &zero, 36 | chunk_dss_prec_get()); 37 | if (base_pages == NULL) 38 | return (true); 39 | base_next_addr = base_pages; 40 | base_past_addr = (void *)((uintptr_t)base_pages + csize); 41 | 42 | return (false); 43 | } 44 | 45 | void * 46 | base_alloc(size_t size) 47 | { 48 | void *ret; 49 | size_t csize; 50 | 51 | /* Round size up to nearest multiple of the cacheline size. */ 52 | csize = CACHELINE_CEILING(size); 53 | 54 | malloc_mutex_lock(&base_mtx); 55 | /* Make sure there's enough space for the allocation. */ 56 | if ((uintptr_t)base_next_addr + csize > (uintptr_t)base_past_addr) { 57 | if (base_pages_alloc(csize)) { 58 | malloc_mutex_unlock(&base_mtx); 59 | return (NULL); 60 | } 61 | } 62 | /* Allocate. */ 63 | ret = base_next_addr; 64 | base_next_addr = (void *)((uintptr_t)base_next_addr + csize); 65 | malloc_mutex_unlock(&base_mtx); 66 | 67 | return (ret); 68 | } 69 | 70 | void * 71 | base_calloc(size_t number, size_t size) 72 | { 73 | void *ret = base_alloc(number * size); 74 | 75 | if (ret != NULL) 76 | memset(ret, 0, number * size); 77 | 78 | return (ret); 79 | } 80 | 81 | extent_node_t * 82 | base_node_alloc(void) 83 | { 84 | extent_node_t *ret; 85 | 86 | malloc_mutex_lock(&base_mtx); 87 | if (base_nodes != NULL) { 88 | ret = base_nodes; 89 | base_nodes = *(extent_node_t **)ret; 90 | malloc_mutex_unlock(&base_mtx); 91 | } else { 92 | malloc_mutex_unlock(&base_mtx); 93 | ret = (extent_node_t *)base_alloc(sizeof(extent_node_t)); 94 | } 95 | 96 | return (ret); 97 | } 98 | 99 | void 100 | base_node_dealloc(extent_node_t *node) 101 | { 102 | 103 | malloc_mutex_lock(&base_mtx); 104 | *(extent_node_t **)node = base_nodes; 105 | base_nodes = node; 106 | malloc_mutex_unlock(&base_mtx); 107 | } 108 | 109 | bool 110 | base_boot(void) 111 | { 112 | 113 | base_nodes = NULL; 114 | if (malloc_mutex_init(&base_mtx)) 115 | return (true); 116 | 117 | return (false); 118 | } 119 | 120 | void 121 | base_prefork(void) 122 | { 123 | 124 | malloc_mutex_prefork(&base_mtx); 125 | } 126 | 127 | void 128 | base_postfork_parent(void) 129 | { 130 | 131 | malloc_mutex_postfork_parent(&base_mtx); 132 | } 133 | 134 | void 135 | base_postfork_child(void) 136 | { 137 | 138 | malloc_mutex_postfork_child(&base_mtx); 139 | } 140 | -------------------------------------------------------------------------------- /src/util/nc_string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_STRING_H_ 19 | #define _NC_STRING_H_ 20 | 21 | #include 22 | #include 23 | 24 | struct string { 25 | uint32_t len; /* string length */ 26 | uint8_t *data; /* string data */ 27 | }; 28 | 29 | #define string(_str) { sizeof(_str) - 1, (uint8_t *)(_str) } 30 | #define null_string { 0, NULL } 31 | 32 | #define string_set_text(_str, _text) do { \ 33 | (_str)->len = (uint32_t)(sizeof(_text) - 1);\ 34 | (_str)->data = (uint8_t *)(_text); \ 35 | } while (0); 36 | 37 | #define string_set_raw(_str, _raw) do { \ 38 | (_str)->len = (uint32_t)(nc_strlen(_raw)); \ 39 | (_str)->data = (uint8_t *)(_raw); \ 40 | } while (0); 41 | 42 | void string_init(struct string *str); 43 | void string_deinit(struct string *str); 44 | bool string_empty(const struct string *str); 45 | rstatus_t string_duplicate(struct string *dst, const struct string *src); 46 | rstatus_t string_copy(struct string *dst, const uint8_t *src, uint32_t srclen); 47 | int string_compare(const struct string *s1, const struct string *s2); 48 | 49 | /* 50 | * Wrapper around common routines for manipulating C character 51 | * strings 52 | */ 53 | #define nc_memcpy(_d, _c, _n) \ 54 | memcpy(_d, _c, (size_t)(_n)) 55 | 56 | #define nc_memmove(_d, _c, _n) \ 57 | memmove(_d, _c, (size_t)(_n)) 58 | 59 | #define nc_memchr(_d, _c, _n) \ 60 | memchr(_d, _c, (size_t)(_n)) 61 | 62 | #define nc_strlen(_s) \ 63 | strlen((char *)(_s)) 64 | 65 | #define nc_strncmp(_s1, _s2, _n) \ 66 | strncmp((char *)(_s1), (char *)(_s2), (size_t)(_n)) 67 | 68 | #define nc_strchr(_p, _l, _c) \ 69 | _nc_strchr((uint8_t *)(_p), (uint8_t *)(_l), (uint8_t)(_c)) 70 | 71 | #define nc_strrchr(_p, _s, _c) \ 72 | _nc_strrchr((uint8_t *)(_p),(uint8_t *)(_s), (uint8_t)(_c)) 73 | 74 | #define nc_strndup(_s, _n) \ 75 | (uint8_t *)strndup((char *)(_s), (size_t)(_n)); 76 | 77 | #define nc_scnprintf(_s, _n, ...) \ 78 | _scnprintf((char *)(_s), (size_t)(_n), __VA_ARGS__) 79 | 80 | #define nc_vscnprintf(_s, _n, _f, _a) \ 81 | _vscnprintf((char *)(_s), (size_t)(_n), _f, _a) 82 | 83 | static inline uint8_t * 84 | _nc_strchr(uint8_t *p, uint8_t *last, uint8_t c) 85 | { 86 | while (p < last) { 87 | if (*p == c) { 88 | return p; 89 | } 90 | p++; 91 | } 92 | 93 | return NULL; 94 | } 95 | 96 | static inline uint8_t * 97 | _nc_strrchr(uint8_t *p, uint8_t *start, uint8_t c) 98 | { 99 | while (p >= start) { 100 | if (*p == c) { 101 | return p; 102 | } 103 | p--; 104 | } 105 | 106 | return NULL; 107 | } 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /src/util/nc_event.h: -------------------------------------------------------------------------------- 1 | /* 2 | * twemproxy - A fast and lightweight proxy for memcached protocol. 3 | * Copyright (C) 2011 Twitter, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _NC_EVENT_H_ 19 | #define _NC_EVENT_H_ 20 | 21 | #include "nc_util.h" 22 | 23 | /* select event implement */ 24 | #ifdef __linux__ 25 | # define NC_HAVE_EPOLL 1 26 | #elif (defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined (__NetBSD__) 27 | # define NC_HAVE_KQUEUE 1 28 | #elif defined(__sun) 29 | # define NC_HAVE_EVENT_PORTS 1 30 | #else 31 | # error missing scalable I/O event notification mechanism 32 | #endif 33 | 34 | #define EVENT_SIZE 1024 35 | 36 | #define EVENT_READ 0x0000ff 37 | #define EVENT_WRITE 0x00ff00 38 | #define EVENT_ERR 0xff0000 39 | 40 | typedef int (*event_cb_t)(void *, uint32_t); 41 | typedef void (*event_stats_cb_t)(void *, void *); 42 | 43 | #ifdef NC_HAVE_KQUEUE 44 | 45 | struct event_base { 46 | int kq; /* kernel event queue descriptor */ 47 | 48 | struct kevent *change; /* change[] - events we want to monitor */ 49 | int nchange; /* # change */ 50 | 51 | struct kevent *event; /* event[] - events that were triggered */ 52 | int nevent; /* # event */ 53 | int nreturned; /* # event placed in event[] */ 54 | int nprocessed; /* # event processed from event[] */ 55 | 56 | event_cb_t cb; /* event callback */ 57 | }; 58 | 59 | #elif NC_HAVE_EPOLL 60 | 61 | struct event_base { 62 | int ep; /* epoll descriptor */ 63 | 64 | struct epoll_event *event; /* event[] - events that were triggered */ 65 | int nevent; /* # event */ 66 | 67 | event_cb_t cb; /* event callback */ 68 | }; 69 | 70 | #elif NC_HAVE_EVENT_PORTS 71 | 72 | #include 73 | 74 | struct event_base { 75 | int evp; /* event port descriptor */ 76 | 77 | port_event_t *event; /* event[] - events that were triggered */ 78 | int nevent; /* # event */ 79 | 80 | event_cb_t cb; /* event callback */ 81 | }; 82 | 83 | #else 84 | # error missing scalable I/O event notification mechanism 85 | #endif 86 | 87 | struct event_base *event_base_create(int size, event_cb_t cb); 88 | void event_base_destroy(struct event_base *evb); 89 | 90 | int event_add_in(struct event_base *evb, struct conn *c); 91 | int event_del_in(struct event_base *evb, struct conn *c); 92 | int event_add_out(struct event_base *evb, struct conn *c); 93 | int event_del_out(struct event_base *evb, struct conn *c); 94 | int event_add_conn(struct event_base *evb, struct conn *c); 95 | int event_del_conn(struct event_base *evb, struct conn *c); 96 | int event_wait(struct event_base *evb, int timeout); 97 | void event_loop_stats(event_stats_cb_t cb, void *arg); 98 | 99 | #endif /* _NC_EVENT_H */ 100 | --------------------------------------------------------------------------------