├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── benchmark ├── Makefile ├── run_benchmarks.py └── src │ ├── bench_alloc_free.cpp │ ├── bench_alloc_free_alloc.cpp │ ├── bench_fastalloc.cpp │ ├── bench_linkedlist.cpp │ ├── bench_recovery.cpp │ ├── common.cpp │ └── common.h ├── src ├── arena.c ├── arena.h ├── chunk.c ├── chunk.h ├── nvm_malloc.c ├── nvm_malloc.h ├── object_table.c ├── object_table.h ├── types.h ├── util.c └── util.h └── ulib-svn ├── AUTHORS ├── CHANGELOG ├── Makefile ├── README ├── VERSION ├── include └── ulib │ ├── bfilter.h │ ├── bitmap.h │ ├── crypt_aes.h │ ├── crypt_md5.h │ ├── crypt_rc4.h │ ├── crypt_sha1.h │ ├── crypt_sha256.h │ ├── hash_chain.h │ ├── hash_chain_prot.h │ ├── hash_chain_r.h │ ├── hash_func.h │ ├── hash_multi_r.h │ ├── hash_open.h │ ├── hash_open_prot.h │ ├── heap_prot.h │ ├── list.h │ ├── math_bit.h │ ├── math_bn.h │ ├── math_comb.h │ ├── math_factorial.h │ ├── math_gcd.h │ ├── math_lcm.h │ ├── math_rand_prot.h │ ├── math_rng_gamma.h │ ├── math_rng_normal.h │ ├── math_rng_zipf.h │ ├── mr_dataset.h │ ├── mr_engine.h │ ├── mr_interm.h │ ├── os_atomic_intel64.h │ ├── os_rdtsc.h │ ├── os_regionlock.h │ ├── os_spinlock.h │ ├── os_thread.h │ ├── os_typelock.h │ ├── search_line.h │ ├── sort_heap_prot.h │ ├── sort_list.h │ ├── sort_median_prot.h │ ├── str_util.h │ ├── tree.h │ ├── tree_util.h │ ├── ulib_ver.h │ ├── util_algo.h │ ├── util_argv.h │ ├── util_class.h │ ├── util_console.h │ ├── util_hexdump.h │ ├── util_log.h │ └── util_timer.h ├── lib ├── Makefile ├── __.SYMDEF SORTED └── makedll.bat ├── perf ├── Makefile ├── avl │ ├── libavl │ │ ├── avl.c │ │ ├── avl.h │ │ ├── avl_bench.cpp │ │ └── result │ ├── solaris │ │ ├── avl.c │ │ ├── avl.h │ │ ├── avl_bench.cpp │ │ ├── avl_impl.h │ │ └── result │ └── ulib │ │ └── result ├── hashmap │ ├── Makefile │ └── hash_perf.cpp └── mapreduce │ ├── Makefile │ └── mr_bench.cpp ├── rules.mk ├── src ├── Makefile ├── base │ ├── Makefile │ ├── bitmap.c │ ├── bitmap.h │ ├── crypt_aes.c │ ├── crypt_aes.h │ ├── crypt_md5.c │ ├── crypt_md5.h │ ├── crypt_rc4.c │ ├── crypt_rc4.h │ ├── crypt_sha1.c │ ├── crypt_sha1.h │ ├── crypt_sha256.c │ ├── crypt_sha256.h │ ├── hash_chain_prot.h │ ├── hash_func.c │ ├── hash_func.h │ ├── hash_open_prot.h │ ├── heap_prot.h │ ├── list.h │ ├── math_bit.h │ ├── math_bn.c │ ├── math_bn.h │ ├── math_factorial.h │ ├── math_gcd.c │ ├── math_gcd.h │ ├── math_lcm.c │ ├── math_lcm.h │ ├── math_rand_prot.h │ ├── search_line.c │ ├── search_line.h │ ├── sort_heap_prot.h │ ├── sort_list.c │ ├── sort_list.h │ ├── sort_median_prot.h │ ├── str_util.c │ ├── str_util.h │ ├── tree.c │ ├── tree.h │ ├── tree_util.c │ ├── tree_util.h │ ├── ulib_ver.c │ ├── ulib_ver.h │ ├── util_algo.h │ ├── util_hexdump.c │ ├── util_hexdump.h │ ├── util_log.h │ └── util_timer.h ├── ext1 │ ├── Makefile │ ├── bloom_filter │ │ ├── Makefile │ │ ├── bfilter.c │ │ └── bfilter.h │ ├── c++ │ │ ├── Makefile │ │ ├── hash_chain.h │ │ ├── hash_open.h │ │ └── util_class.h │ ├── comb │ │ ├── Makefile │ │ ├── math_comb.c │ │ └── math_comb.h │ ├── console │ │ ├── Makefile │ │ ├── util_argv.c │ │ ├── util_argv.h │ │ ├── util_console.c │ │ └── util_console.h │ └── rng │ │ ├── Makefile │ │ ├── math_rng_gamma.c │ │ ├── math_rng_gamma.h │ │ ├── math_rng_normal.c │ │ ├── math_rng_normal.h │ │ ├── math_rng_zipf.c │ │ └── math_rng_zipf.h └── ext2 │ ├── Makefile │ ├── mapreduce │ ├── Makefile │ ├── mr_dataset.cpp │ ├── mr_dataset.h │ ├── mr_engine.h │ └── mr_interm.h │ ├── osdep │ ├── Makefile │ ├── os_atomic_intel64.h │ ├── os_rdtsc.h │ └── os_spinlock.h │ ├── reentrant │ ├── Makefile │ ├── hash_chain_r.h │ ├── hash_multi_r.h │ ├── os_regionlock.h │ └── os_typelock.h │ └── thread │ ├── Makefile │ ├── os_thread.cpp │ └── os_thread.h └── test ├── Makefile ├── aes.cpp ├── atomic.cpp ├── avl_bench.cpp ├── bfilter.cpp ├── bfilter_bench.cpp ├── bit.cpp ├── bitmap.cpp ├── bn.cpp ├── comb.cpp ├── console.cpp ├── factorial.cpp ├── fpr.cpp ├── gamma_rng.cpp ├── gcd.cpp ├── hash_chain.cpp ├── hash_chain_bench.cpp ├── hash_chain_prot.cpp ├── hash_chain_prot_bench.cpp ├── hash_chain_r.cpp ├── hash_chain_r1.cpp ├── hash_chain_r_bench.cpp ├── hash_chain_r_workload_shared.cpp ├── hash_chain_workload_indep.cpp ├── hash_func.cpp ├── hash_func_perf.cpp ├── hash_multi_r_workload_shared.cpp ├── hash_multihash_r.cpp ├── hash_multihash_r_bench.cpp ├── hash_open.cpp ├── hash_open_prot_bench.cpp ├── heapsort.cpp ├── hweight.cpp ├── hweight_mult.cpp ├── hweight_perf.cpp ├── hweight_perf_mult.cpp ├── int_hash_perf.cpp ├── lcm.cpp ├── list.cpp ├── listsort.cpp ├── log.cpp ├── md5.cpp ├── median.cpp ├── normal_rng.cpp ├── rand.cpp ├── rand_inv.cpp ├── rc4.cpp ├── rdtsc.cpp ├── run_all_test.sh ├── search_line.cpp ├── search_line.data ├── set_bench.cpp ├── sha1.cpp ├── sha256.cpp ├── spinlock.cpp ├── splay_bench.cpp ├── splay_np_bench.cpp ├── str_util.cpp ├── thread.cpp ├── tree.cpp ├── version.cpp └── zipf_rng.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.o 3 | *.so 4 | *.a 5 | *.test 6 | objects/ 7 | benchmark/build 8 | benchmark/cached 9 | benchmark/plots 10 | 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | debug: CFLAGS := -O0 -ggdb -fpic -Wall -I. -Iulib-svn/include 3 | release: CFLAGS := -O3 -fpic -Wall -I. -Iulib-svn/include 4 | LDFLAGS := -lpthread 5 | 6 | SRCDIR := src 7 | OBJDIR := objects 8 | OBJECTS := util.o chunk.o object_table.o arena.o nvm_malloc.o 9 | LIBNAME := libnvmmalloc.so 10 | 11 | release: $(LIBNAME) libnvmmallocnoflush.so libnvmmallocnofence.so libnvmmallocnone.so 12 | 13 | debug: $(LIBNAME) 14 | 15 | $(LIBNAME): ulib-svn/lib/libulib.a $(addprefix $(OBJDIR)/, $(OBJECTS)) 16 | $(CC) $(CFLAGS) -shared -o $@ $(LDFLAGS) $(addprefix $(OBJDIR)/, $(OBJECTS)) ulib-svn/lib/libulib.a 17 | 18 | libnvmmallocnoflush.so: $(SRCDIR)/*.c ulib-svn/lib/libulib.a 19 | $(CC) $(CFLAGS) -shared -o $@ $(LDFLAGS) -DNOFLUSH $+ ulib-svn/lib/libulib.a 20 | 21 | libnvmmallocnofence.so: $(SRCDIR)/*.c ulib-svn/lib/libulib.a 22 | $(CC) $(CFLAGS) -shared -o $@ $(LDFLAGS) -DNOFENCE $+ ulib-svn/lib/libulib.a 23 | 24 | libnvmmallocnone.so: $(SRCDIR)/*.c ulib-svn/lib/libulib.a 25 | $(CC) $(CFLAGS) -shared -o $@ $(LDFLAGS) -DNOFLUSH -DNOFENCE $+ ulib-svn/lib/libulib.a 26 | 27 | $(OBJDIR)/%.o: $(SRCDIR)/%.c $(SRCDIR)/*.h 28 | @mkdir -p $(OBJDIR) 29 | $(CC) $(CFLAGS) -c -o $@ $< 30 | 31 | ulib-svn/lib/libulib.a: 32 | cd ulib-svn; make release 33 | 34 | clean: 35 | @rm -f $(LIBNAME) 36 | @rm -rf $(OBJDIR) 37 | 38 | .PHONY: test debug release 39 | -------------------------------------------------------------------------------- /benchmark/Makefile: -------------------------------------------------------------------------------- 1 | CXX ?= g++ 2 | CXXFLAGS := -std=c++11 -Wall -Isrc -I../src 3 | debug: CXXFLAGS += -O0 -ggdb 4 | release: CXXFLAGS += -O3 5 | LDFLAGS := -L.. -lpthread -ltbb -lnvmmalloc 6 | 7 | SRCDIR := src 8 | BUILDDIR := build 9 | BINARIES := bench_fastalloc bench_linkedlist bench_recovery bench_alloc_free bench_alloc_free_alloc 10 | TARGETS := $(addprefix $(BUILDDIR)/, $(BINARIES)) 11 | 12 | release: $(TARGETS) 13 | 14 | debug: $(TARGETS) 15 | 16 | $(BUILDDIR)/bench_recovery: $(SRCDIR)/bench_recovery.cpp $(SRCDIR)/common.h $(SRCDIR)/common.cpp 17 | @mkdir -p $(BUILDDIR) 18 | $(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -o $@ $(LDFLAGS) $< $(SRCDIR)/common.cpp 19 | 20 | $(BUILDDIR)/%: $(SRCDIR)/%.cpp $(SRCDIR)/common.h $(SRCDIR)/common.cpp 21 | @mkdir -p $(BUILDDIR) 22 | $(CXX) $(CXXFLAGS) -DUSE_MALLOC -o $@ $(LDFLAGS) $< $(SRCDIR)/common.cpp 23 | $(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -o $@_nvm $(LDFLAGS) $< $(SRCDIR)/common.cpp 24 | #$(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -DHAS_CLFLUSHOPT -o $@_nvm_clflushopt $(LDFLAGS) $< $(SRCDIR)/common.cpp 25 | #$(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -DHAS_CLWB -o $@_nvm_clwb $(LDFLAGS) $< $(SRCDIR)/common.cpp 26 | $(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -o $@_nvm_noflush $(LDFLAGS)noflush $< $(SRCDIR)/common.cpp 27 | $(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -o $@_nvm_nofence $(LDFLAGS)nofence $< $(SRCDIR)/common.cpp 28 | $(CXX) $(CXXFLAGS) -DUSE_NVM_MALLOC -o $@_nvm_none $(LDFLAGS)none $< $(SRCDIR)/common.cpp 29 | 30 | clean: 31 | @rm -rf $(BUILDDIR) 32 | 33 | .PHONY: clean 34 | -------------------------------------------------------------------------------- /benchmark/src/bench_alloc_free.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | #include 4 | #include 5 | 6 | std::vector workerTimes; 7 | uint64_t allocation_size_min = 64; 8 | uint64_t allocation_size_max = 64; 9 | 10 | void worker(int id) { 11 | volatile char* pointerlist[100000]; 12 | nvb::timer timer; 13 | std::default_random_engine generator; 14 | std::uniform_int_distribution distribution(allocation_size_min, allocation_size_max); 15 | auto randomSize = std::bind(distribution, generator); 16 | 17 | timer.start(); 18 | for (int i=0; i<100000; ++i) { 19 | pointerlist[i] = (volatile char*) nvb::reserve(randomSize()); 20 | memset((void*)pointerlist[i], 5, 64); 21 | nvb::activate((void*) pointerlist[i]); 22 | } 23 | for (int i=0; i<100000; ++i) { 24 | nvb::free((void*) pointerlist[i]); 25 | } 26 | 27 | 28 | // save result 29 | workerTimes[id] = timer.stop(); 30 | } 31 | 32 | int main(int argc, char **argv) { 33 | if (argc < 3 || argc > 4) { 34 | std::cout << "usage: " << argv[0] << " [allocation_size_max]" << std::endl; 35 | return -1; 36 | } 37 | size_t n_threads = atoi(argv[1]); 38 | allocation_size_min = atoi(argv[2]); 39 | if (allocation_size_min < 64) { 40 | std::cout << "WARNING: specified min allocation size was less than minimum, using 64 bytes instead" << std::endl; 41 | allocation_size_min = 64; 42 | } 43 | if (argc == 4) { 44 | allocation_size_max = atoi(argv[3]); 45 | if (allocation_size_max < allocation_size_min) { 46 | std::cout << "WARNING: max allocation size was less than min, using min instead" << std::endl; 47 | allocation_size_max = allocation_size_min; 48 | } 49 | } else { 50 | allocation_size_max = allocation_size_min; 51 | } 52 | workerTimes.resize(n_threads, 0); 53 | nvb::initialize("/mnt/pmfs/nvb", 0); 54 | nvb::execute_in_pool(worker, n_threads); 55 | uint64_t avg = 0; 56 | for (auto t : workerTimes) 57 | avg += t; 58 | avg /= n_threads; 59 | std::cout << avg << std::endl; 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /benchmark/src/bench_alloc_free_alloc.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | #include 4 | #include 5 | 6 | std::vector workerTimes; 7 | uint64_t allocation_size_min = 64; 8 | uint64_t allocation_size_max = 64; 9 | 10 | void worker(int id) { 11 | volatile char* pointerlist[100000]; 12 | nvb::timer timer; 13 | std::default_random_engine generator; 14 | std::uniform_int_distribution distribution(allocation_size_min, allocation_size_max); 15 | auto randomSize = std::bind(distribution, generator); 16 | 17 | timer.start(); 18 | for (int i=0; i<100000; ++i) { 19 | pointerlist[i] = (volatile char*) nvb::reserve(randomSize()); 20 | memset((void*)pointerlist[i], 5, 64); 21 | nvb::activate((void*) pointerlist[i]); 22 | } 23 | for (int i=0; i<100000; ++i) { 24 | nvb::free((void*) pointerlist[i]); 25 | } 26 | for (int i=0; i<100000; ++i) { 27 | pointerlist[i] = (volatile char*) nvb::reserve(randomSize()); 28 | memset((void*)pointerlist[i], 5, 64); 29 | nvb::activate((void*) pointerlist[i]); 30 | } 31 | 32 | // save result 33 | workerTimes[id] = timer.stop(); 34 | } 35 | 36 | int main(int argc, char **argv) { 37 | if (argc < 3 || argc > 4) { 38 | std::cout << "usage: " << argv[0] << " [allocation_size_max]" << std::endl; 39 | return -1; 40 | } 41 | size_t n_threads = atoi(argv[1]); 42 | allocation_size_min = atoi(argv[2]); 43 | if (allocation_size_min < 64) { 44 | std::cout << "WARNING: specified min allocation size was less than minimum, using 64 bytes instead" << std::endl; 45 | allocation_size_min = 64; 46 | } 47 | if (argc == 4) { 48 | allocation_size_max = atoi(argv[3]); 49 | if (allocation_size_max < allocation_size_min) { 50 | std::cout << "WARNING: max allocation size was less than min, using min instead" << std::endl; 51 | allocation_size_max = allocation_size_min; 52 | } 53 | } else { 54 | allocation_size_max = allocation_size_min; 55 | } 56 | workerTimes.resize(n_threads, 0); 57 | nvb::initialize("/mnt/pmfs/nvb", 0); 58 | nvb::execute_in_pool(worker, n_threads); 59 | uint64_t avg = 0; 60 | for (auto t : workerTimes) 61 | avg += t; 62 | avg /= n_threads; 63 | std::cout << avg << std::endl; 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /benchmark/src/bench_fastalloc.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | #include 4 | #include 5 | 6 | std::vector workerTimes; 7 | uint64_t allocation_size_min = 64; 8 | uint64_t allocation_size_max = 64; 9 | 10 | void worker(int id) { 11 | volatile char* pointerlist[100000]; 12 | nvb::timer timer; 13 | std::default_random_engine generator; 14 | std::uniform_int_distribution distribution(allocation_size_min, allocation_size_max); 15 | auto randomSize = std::bind(distribution, generator); 16 | 17 | timer.start(); 18 | for (int i=0; i<100000; ++i) { 19 | pointerlist[i] = (volatile char*) nvb::reserve(randomSize()); 20 | memset((void*)pointerlist[i], 5, 64); 21 | nvb::activate((void*) pointerlist[i]); 22 | } 23 | 24 | // save result 25 | workerTimes[id] = timer.stop(); 26 | } 27 | 28 | int main(int argc, char **argv) { 29 | if (argc < 3 || argc > 4) { 30 | std::cout << "usage: " << argv[0] << " [allocation_size_max]" << std::endl; 31 | return -1; 32 | } 33 | size_t n_threads = atoi(argv[1]); 34 | allocation_size_min = atoi(argv[2]); 35 | if (allocation_size_min < 64) { 36 | std::cout << "WARNING: specified min allocation size was less than minimum, using 64 bytes instead" << std::endl; 37 | allocation_size_min = 64; 38 | } 39 | if (argc == 4) { 40 | allocation_size_max = atoi(argv[3]); 41 | if (allocation_size_max < allocation_size_min) { 42 | std::cout << "WARNING: max allocation size was less than min, using min instead" << std::endl; 43 | allocation_size_max = allocation_size_min; 44 | } 45 | } else { 46 | allocation_size_max = allocation_size_min; 47 | } 48 | workerTimes.resize(n_threads, 0); 49 | nvb::initialize("/mnt/pmfs/nvb", 0); 50 | nvb::execute_in_pool(worker, n_threads); 51 | uint64_t avg = 0; 52 | for (auto t : workerTimes) 53 | avg += t; 54 | avg /= n_threads; 55 | std::cout << avg << std::endl; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /benchmark/src/bench_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | std::vector workerTimes; 9 | uint64_t payload_size_min = 64; 10 | uint64_t payload_size_max = 64; 11 | 12 | struct node_t { 13 | void *next; 14 | void *prev; 15 | void *data; 16 | char __padding[40]; // so the whole struct is 64 byte for fairness 17 | }; 18 | 19 | void worker(int id) { 20 | uint64_t idx = 0; 21 | volatile node_t *root = nullptr; 22 | nvb::timer timer; 23 | std::default_random_engine generator; 24 | std::uniform_int_distribution distribution(payload_size_min, payload_size_max); 25 | auto randomPayload = std::bind(distribution, generator); 26 | 27 | // run the benchmark 28 | timer.start(); 29 | for (idx=0; idx<100000; ++idx) { 30 | // allocate new node 31 | volatile node_t *new_node = (volatile node_t*) nvb::reserve(sizeof(node_t)); 32 | new_node->next = nvb::rel((void*)root); 33 | new_node->prev = nullptr; 34 | new_node->data = nullptr; 35 | nvb::persist((void*)new_node, sizeof(node_t)); 36 | if (root) 37 | nvb::activate((void*)new_node, (void**) &root->prev, (void*) new_node); 38 | else 39 | nvb::activate((void*)new_node); 40 | 41 | // allocate payload 42 | volatile void *payload = nvb::reserve(randomPayload()); 43 | memset((void*) payload, 5, 64); 44 | nvb::activate((void*)payload, (void**) &new_node->data, (void*) payload); 45 | 46 | // set new node as root 47 | root = new_node; 48 | } 49 | 50 | // store result 51 | workerTimes[id] = timer.stop(); 52 | } 53 | 54 | int main(int argc, char **argv) { 55 | if (argc < 3 || argc > 4) { 56 | std::cout << "usage: " << argv[0] << " []" << std::endl; 57 | return -1; 58 | } 59 | size_t n_threads = atoi(argv[1]); 60 | payload_size_min = atoi(argv[2]); 61 | if (payload_size_min < 64) { 62 | std::cout << "WARNING: specified allocation size was less than minimum, using 64 bytes instead" << std::endl; 63 | payload_size_min = 64; 64 | } 65 | if (argc == 4) { 66 | payload_size_max = atoi(argv[3]); 67 | } else { 68 | payload_size_max = payload_size_min; 69 | } 70 | workerTimes.resize(n_threads, 0); 71 | nvb::initialize("/mnt/pmfs/nvb", 0); 72 | nvb::execute_in_pool(worker, n_threads); 73 | uint64_t avg = 0; 74 | for (auto t : workerTimes) 75 | avg += t; 76 | avg /= n_threads; 77 | std::cout << avg << std::endl; 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /benchmark/src/common.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | namespace nvb { 4 | 5 | #ifdef USE_MALLOC 6 | object_table_t _object_table; 7 | #endif 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/arena.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014 Tim Berning */ 2 | 3 | #ifndef ARENA_H_ 4 | #define ARENA_H_ 5 | 6 | #include "types.h" 7 | 8 | #include 9 | 10 | void arena_init(arena_t *arena, uint32_t id, nvm_chunk_header_t *first_chunk, int create_initial_block); 11 | 12 | void* arena_allocate(arena_t *arena, uint32_t n_bytes); 13 | 14 | void arena_free(void *ptr, void **link_ptr1, void *target1, void **link_ptr2, void *target2); 15 | 16 | arena_run_t* arena_create_run_header(nvm_run_header_t *nvm_run); 17 | arena_block_t* arena_create_block_header(nvm_block_header_t *nvm_block); 18 | 19 | int run_node_compare(const void *_a, const void *_b); 20 | 21 | int block_node_compare(const void *_a, const void *_b); 22 | 23 | void arena_teardown(arena_t *arena); 24 | 25 | #endif /* ARENA_H_ */ 26 | -------------------------------------------------------------------------------- /src/chunk.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014 Tim Berning */ 2 | 3 | #ifndef CHUNK_H_ 4 | #define CHUNK_H_ 5 | 6 | #include 7 | 8 | void* initalize_nvm_space(const char *workspace_path, uint64_t max_num_chunks); 9 | 10 | void initialize_chunks(); 11 | 12 | uint64_t recover_chunks(); 13 | 14 | void* activate_more_chunks(uint64_t n_chunks); 15 | 16 | void teardown_nvm_space(); 17 | 18 | #endif /* CHUNK_H_ */ 19 | -------------------------------------------------------------------------------- /src/nvm_malloc.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014 Tim Berning */ 2 | 3 | #ifndef NVMALLOC_H_ 4 | #define NVMALLOC_H_ 5 | 6 | #include 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #define NVM_ABS_TO_REL(base, ptr) ((uintptr_t)ptr - (uintptr_t)base) 13 | #define NVM_REL_TO_ABS(base, ptr) (void*)((uintptr_t)base + (uintptr_t)ptr) 14 | 15 | extern void* nvm_initialize(const char *workspace_path, int recover_if_possible); 16 | 17 | extern void* nvm_reserve(uint64_t n_bytes); 18 | 19 | extern void* nvm_reserve_id(const char *id, uint64_t n_bytes); 20 | 21 | extern void nvm_activate(void *ptr, void **link_ptr1, void *target1, void **link_ptr2, void *target2); 22 | 23 | extern void nvm_activate_id(const char *id); 24 | 25 | extern void* nvm_get_id(const char *id); 26 | 27 | extern void nvm_free(void *ptr, void **link_ptr1, void *target1, void **link_ptr2, void *target2); 28 | 29 | extern void nvm_free_id(const char *id); 30 | 31 | extern void nvm_persist(const void *ptr, uint64_t n_bytes); 32 | 33 | extern void* nvm_abs(void *rel_ptr); 34 | 35 | extern void* nvm_rel(void *abs_ptr); 36 | 37 | extern void nvm_teardown(); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | #endif /* NVMALLOC_H_ */ 44 | -------------------------------------------------------------------------------- /src/object_table.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014 Tim Berning */ 2 | 3 | #ifndef OBJECT_TABLE_H_ 4 | #define OBJECT_TABLE_H_ 5 | 6 | #include "types.h" 7 | 8 | #define OT_OK 0 9 | #define OT_FAIL 1 10 | #define OT_DUPLICATE 2 11 | 12 | void ot_init(void *nvm_start); 13 | 14 | void ot_recover(void *nvm_start); 15 | 16 | int ot_insert(const char *id, void *data_ptr); 17 | 18 | object_table_entry_t* ot_get(const char *id); 19 | 20 | int ot_remove(const char *id); 21 | 22 | void ot_teardown(); 23 | 24 | #endif /* OBJECT_TABLE_H_ */ 25 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014 Tim Berning */ 2 | 3 | #ifndef UTIL_H_ 4 | #define UTIL_H_ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | inline uint64_t round_up(uint64_t num, uint64_t multiple); 11 | inline char identify_usage(void *ptr); 12 | 13 | inline void clflush(const void *ptr); 14 | inline void clflush_range(const void *ptr, uint64_t len); 15 | 16 | #ifdef HAS_CLFLUSHOPT 17 | inline void clflushopt(const void *ptr); 18 | inline void clflushopt_range(const void *ptr, uint64_t len); 19 | #endif 20 | 21 | #ifdef HAS_CLWB 22 | inline void clwb(const void *ptr); 23 | inline void clwb_range(const void *ptr, uint64_t len); 24 | #endif 25 | 26 | inline void sfence(); 27 | inline void mfence(); 28 | 29 | /* macros for persistency depending on instruction availability */ 30 | #ifdef NOFLUSH 31 | /* Completely disable flushes */ 32 | #define PERSIST(ptr) do { } while (0) 33 | #define PERSIST_RANGE(ptr, len) do { } while (0) 34 | #elif HAS_CLWB 35 | /* CLWB is the preferred instruction, not invalidating any cache lines */ 36 | #define PERSIST(ptr) do { sfence(); clwb(ptr); sfence(); } while (0) 37 | #define PERSIST_RANGE(ptr, len) do { sfence(); clwb_range(ptr, len); sfence(); } while (0) 38 | #elif HAS_CLFLUSHOPT 39 | /* CLFLUSHOPT is preferred over CLFLUSH as only dirty cache lines will be evicted */ 40 | #define PERSIST(ptr) do { sfence(); clflushopt(ptr); sfence(); } while (0) 41 | #define PERSIST_RANGE(ptr, len) do { sfence(); clflushopt_range(ptr, len); sfence(); } while (0) 42 | #else 43 | /* If neither CLWB nor CLFLUSHOPT are available, default to CLFLUSH */ 44 | #define PERSIST(ptr) do { mfence(); clflush(ptr); mfence(); } while (0) 45 | #define PERSIST_RANGE(ptr, len) do { mfence(); clflush_range(ptr, len); mfence(); } while (0) 46 | #endif 47 | 48 | #endif /* UTIL_H_ */ 49 | -------------------------------------------------------------------------------- /ulib-svn/AUTHORS: -------------------------------------------------------------------------------- 1 | Zilong Tan (eric.zltan@gmail.com) 2 | -------------------------------------------------------------------------------- /ulib-svn/CHANGELOG: -------------------------------------------------------------------------------- 1 | 05/17/2013 2 | 1. Fixed a bug in the iterator of align_hash_map/set: operator++() 3 | might read a byte beyond the flag array. 4 | 05/16/2013 5 | 1. Fixed a bug in os_regionlock.h potentially affecting all 6 | concurrent items, e.g., hash_chain_r. 7 | 04/09/2013 - 2.0.1 beta: 8 | 1. Fixed a compiling problem for old G++ versions. 9 | -------------------------------------------------------------------------------- /ulib-svn/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean release test 2 | 3 | all: 4 | make -C src/ 5 | make -C lib/ 6 | make -C test/ 7 | make -C perf/ 8 | 9 | release: 10 | make DEBUG=-DUNDEBUG -C src/ 11 | make -C lib/ release 12 | make DEBUG=-DUNDEBUG -C test/ 13 | 14 | clean: 15 | make -C src/ clean 16 | make -C test/ clean 17 | make -C lib/ clean 18 | make -C perf/ clean 19 | @find . -name "*~" | xargs rm -rf 20 | 21 | test: 22 | make -C test/ test 23 | -------------------------------------------------------------------------------- /ulib-svn/VERSION: -------------------------------------------------------------------------------- 1 | 2.0.1 2 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/bfilter.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_BLOOM_FILTER_H 27 | #define _ULIB_BLOOM_FILTER_H 28 | 29 | #include 30 | 31 | struct bloom_filter { 32 | unsigned long *bitmap; 33 | unsigned long nbits; 34 | unsigned long nelem; /* estimated number of elements */ 35 | int nfunc; /* number of hash functions */ 36 | uint64_t *seeds; /* seeds for hash functions */ 37 | }; 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | int bfilter_create(struct bloom_filter *bf, unsigned long nbits, unsigned long nelem); 44 | void bfilter_destroy(struct bloom_filter *bf); 45 | void bfilter_zero(struct bloom_filter *bf); 46 | void bfilter_set(struct bloom_filter *bf, const void *buf, unsigned long buflen); 47 | int bfilter_get(struct bloom_filter *bf, const void *buf, unsigned long buflen); 48 | void bfilter_set_hash(struct bloom_filter *bf, unsigned long hash); 49 | int bfilter_get_hash(struct bloom_filter *bf, unsigned long hash); 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/crypt_rc4.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_CRYPT_RC4_H 27 | #define _ULIB_CRYPT_RC4_H 28 | 29 | #include 30 | #include 31 | 32 | typedef struct rc4_ks_t { 33 | uint8_t state[256]; 34 | uint8_t x; 35 | uint8_t y; 36 | } rc4_ks_t; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | void rc4_setks(const uint8_t *kbuf, size_t klen, rc4_ks_t *ks); 43 | 44 | // in-place encryption/decryption 45 | void rc4_crypt(uint8_t *buf, size_t len, rc4_ks_t *ks); 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif /* _ULIB_CRYPT_RC4_H */ 52 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/crypt_sha256.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_CRYPT_SHA256_H 27 | #define _ULIB_CRYPT_SHA256_H 28 | 29 | #include 30 | #include 31 | 32 | #define SHA256_HASH_SIZE 32 /* 256 bit */ 33 | #define SHA256_HASH_WORDS 8 34 | 35 | typedef struct { 36 | uint64_t totalLength; 37 | uint32_t hash[SHA256_HASH_WORDS]; 38 | uint32_t bufferLength; 39 | union { 40 | uint32_t words[16]; 41 | uint8_t bytes[64]; 42 | } buffer; 43 | } sha256_ctx_t; 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | void sha256_init(sha256_ctx_t * sc); 50 | void sha256_update(sha256_ctx_t * sc, const void *data, size_t len); 51 | void sha256_finalize(sha256_ctx_t * sc, uint8_t hash[SHA256_HASH_SIZE]); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* _ULIB_CRYPT_SHA256_H */ 58 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_bn.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_BN_H 27 | #define _ULIB_MATH_BN_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* compute a^b % m */ 34 | unsigned long mpower(unsigned long a, unsigned long b, unsigned long m); 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | 40 | #endif /* _ULIB_MATH_BN_H */ 41 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_comb.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_COMB_H 27 | #define _ULIB_MATH_COMB_H 28 | 29 | #include 30 | 31 | typedef uint64_t comb_t; 32 | 33 | typedef struct { 34 | comb_t max; 35 | comb_t cur; 36 | } combiter_t; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * comb_begin - begin the combination iteration by initializing @iter 44 | * @m: total number of elements 45 | * @n: number of elements to choose 46 | * @iter: generated combination iterator 47 | * Note: the max @m supported is 64 48 | */ 49 | int comb_begin(int m, int n, combiter_t *iter); 50 | 51 | /** 52 | * comb_next - generate next iterator 53 | * @iter: combination iterator 54 | */ 55 | int comb_next(combiter_t *iter); 56 | 57 | /** 58 | * comb_get - get combination from iterator 59 | * @iter: iterator for the combination 60 | * @comb: output combination 61 | */ 62 | int comb_get(combiter_t *iter, comb_t *comb); 63 | 64 | /** 65 | * comb_elem - get and remove an element from the combination 66 | * @comb: input & output combination 67 | * Note: the elements are numbered starting from 1 68 | */ 69 | int comb_elem(comb_t *comb); 70 | 71 | #ifdef __cplusplus 72 | } 73 | #endif 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_factorial.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_FACTORIAL_H 27 | #define _ULIB_MATH_FACTORIAL_H 28 | 29 | #include 30 | 31 | #define PI 3.1415926 32 | 33 | /* Gosper's approximation for ln(n!) */ 34 | static inline double 35 | ln_factorial(unsigned int n) 36 | { 37 | if (n == 0) 38 | return 0; 39 | return log((2*n + 1/3.0) * PI)/2 + n*log(n) - n; 40 | } 41 | 42 | /* Gosper's approximation for n! */ 43 | static inline double 44 | factorial(unsigned int n) 45 | { 46 | return sqrt((2*n + 1/3.0) * PI) * pow(n, n) * exp(-(double)n); 47 | } 48 | 49 | /* Routines based on Feller's approximation for n! */ 50 | static inline double 51 | ln_comb(unsigned int n, unsigned int r) 52 | { 53 | if (r == n || r == 0) 54 | return 0; 55 | return (n - r) * log((double)n / (n - r)) + r * log((double)n / r) - 56 | 0.5 * log(2 * PI * r * (n - r) / n) + 57 | 1.0 / 12 * (1.0 / n - 1.0 / (n - r) - 1.0 / r); 58 | } 59 | 60 | static inline double 61 | comb(unsigned int n, unsigned int r) 62 | { 63 | if (r > n) 64 | return 0; 65 | if (n == 0 || r == 0 || n == r) 66 | return 1; 67 | /* won't calculate directly since it may cause overflow */ 68 | return exp(ln_comb(n, r)); 69 | } 70 | 71 | #endif /* _ULIB_MATH_FACTORIAL_H */ 72 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_gcd.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_GCD_H 27 | #define _ULIB_MATH_GCD_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | unsigned long gcd(unsigned long a, unsigned long b); 34 | 35 | /* compute x,y st. ax + by = 1 */ 36 | void egcd(unsigned long a, unsigned long b, long *x, long *y); 37 | 38 | /* computes a st. a * b = 1 mod m */ 39 | unsigned long invert(unsigned long m, unsigned long b); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* _ULIB_MATH_GCD_H */ 46 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_lcm.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_LCM_H 27 | #define _ULIB_MATH_LCM_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* Lowest common multiple */ 34 | unsigned long lcm(unsigned long a, unsigned long b); 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | 40 | #endif /* _ULIB_MATH_LCM_H */ 41 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_rng_gamma.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | Copyright (C) 2009, 2011 by Attractive Chaos 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #ifndef _ULIB_MATH_RNG_GAMMA_H 28 | #define _ULIB_MATH_RNG_GAMMA_H 29 | 30 | #include 31 | #include "math_rng_normal.h" 32 | 33 | typedef struct normal_rng gamma_rng_t; 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | void gamma_rng_init(gamma_rng_t *rng); 40 | 41 | /* g(x;a,b) = x^{a-1} b^a e^{-bx} / \Gamma(a) */ 42 | double gamma_rng_next(gamma_rng_t *rng, double alpha, double beta); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* _ULIB_MATH_RNG_GAMMA_H */ 49 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_rng_normal.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | Copyright (C) 2009, 2011 by Attractive Chaos 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #ifndef _ULIB_MATH_RNG_NORMAL_H 28 | #define _ULIB_MATH_RNG_NORMAL_H 29 | 30 | #include 31 | 32 | struct normal_rng { 33 | uint64_t u, v, w; /* NR rng context */ 34 | }; 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | void normal_rng_init(struct normal_rng *rng); 41 | 42 | /* Ziggurat method */ 43 | double normal_rng_next(struct normal_rng *rng); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif /* _ULIB_MATH_RNG_NORMAL_H */ 50 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/math_rng_zipf.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_RNG_ZIPF_H 27 | #define _ULIB_RNG_ZIPF_H 28 | 29 | #include 30 | 31 | struct zipf_rng { 32 | int range; 33 | float sum; 34 | float s; 35 | uint64_t u, v, w; /* int rng context */ 36 | }; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * zipf_rng_init - create a zipf rng with density function: 44 | * p(x) = 1/x^s, where x = 1,2,...,range. 45 | * @rng: rng context 46 | * @range: random integer range 47 | * @s: zipf distribution parameter 48 | */ 49 | void zipf_rng_init(struct zipf_rng *rng, int range, float s); 50 | 51 | /** 52 | * zipf_rng_next - generate a random integer 53 | * @rng: rng context 54 | */ 55 | int zipf_rng_next(struct zipf_rng *rng); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* _ULIB_RNG_ZIPF_H */ 62 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/os_rdtsc.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 2005 kazutomo (kazutomo@mcs.anl.gov) 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the "Software"), to deal in the Software without 9 | restriction, including without limitation the rights to use, copy, 10 | modify, merge, publish, distribute, sublicense, and/or sell copies 11 | of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #ifndef __ULIB_RDTSC_H 28 | #define __ULIB_RDTSC_H 29 | 30 | #include 31 | 32 | #if defined(__i386__) 33 | 34 | static __inline__ uint64_t rdtsc(void) 35 | { 36 | uint64_t x; 37 | __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x)); 38 | return x; 39 | } 40 | 41 | #elif defined(__x86_64__) 42 | 43 | static __inline__ uint64_t rdtsc(void) 44 | { 45 | unsigned hi, lo; 46 | __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); 47 | return ((uint64_t)lo) | (((uint64_t)hi) << 32); 48 | } 49 | 50 | #elif defined(__powerpc__) 51 | 52 | static __inline__ uint64_t rdtsc(void) 53 | { 54 | uint64_t result = 0; 55 | unsigned long upper, lower, tmp; 56 | 57 | __asm__ volatile( 58 | "0: \n" 59 | "\tmftbu %0 \n" 60 | "\tmftb %1 \n" 61 | "\tmftbu %2 \n" 62 | "\tcmpw %2,%0 \n" 63 | "\tbne 0b \n" 64 | : "=r"(upper),"=r"(lower),"=r"(tmp) 65 | ); 66 | 67 | result = upper; 68 | result = result << 32; 69 | result = result | lower; 70 | return result; 71 | } 72 | 73 | #else 74 | 75 | #error "No tick counter is available!" 76 | 77 | #endif 78 | 79 | #endif /* __ULIB_RDSTC_H */ 80 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/search_line.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | /* This file implements the binary search of text lines. The input 27 | * text file must be sorted in ascending order according to the 28 | * compare function provided by the user. */ 29 | 30 | #ifndef _ULIB_SEARCH_LINE_H 31 | #define _ULIB_SEARCH_LINE_H 32 | 33 | #include 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* Find a matching line and return the file offset to the line. 40 | * @comp: a line compare function that takes two parameters: a test 41 | * line and a user provided parameter. 42 | * @param: the user-specified parameter for comp, e.g., the target 43 | * line) 44 | * @maxlen: max line length, including the '\n' 45 | * 46 | * Note: lines in fd must be sorted in ascending order with respect to 47 | * @comp. 48 | * 49 | * Return -1 if the line was not found, or offset to the line 50 | * otherwise. 51 | */ 52 | ssize_t findline(int fd, int (*comp) (const char *, void *), 53 | void *param, int maxlen); 54 | 55 | /* find the first matching line */ 56 | ssize_t findfirstline(int fd, int (*comp) (const char *, void *), 57 | void *param, int maxlen); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | #endif /* _ULIB_SEARCH_LINE_H */ 64 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/sort_heap_prot.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_SORT_HEAP_PROT_H 27 | #define _ULIB_SORT_HEAP_PROT_H 28 | 29 | #include "heap_prot.h" 30 | 31 | #define DEFINE_HEAPSORT(name, type, lt) \ 32 | DEFINE_HEAP(name, type, lt) \ 33 | \ 34 | static inline void \ 35 | heapsort_##name(type *base, type *last) \ 36 | { \ 37 | heap_init_##name(base, last); \ 38 | heap_sort_##name(base, last); \ 39 | } 40 | 41 | #endif /* _ULIB_SORT_HEAP_PROT_H */ 42 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/sort_median_prot.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_SORT_MEDIAN_PROT_H 27 | #define _ULIB_SORT_MEDIAN_PROT_H 28 | 29 | #include "util_algo.h" 30 | 31 | /** 32 | * median - partition an array into tree parts: 33 | * array[median] 34 | */ 35 | #define DEFINE_MEDIAN(name, type, lt) \ 36 | static inline void \ 37 | median_##name(type *base, type *median, type *last) \ 38 | { \ 39 | register type *s, *t, *p, *q, *m; \ 40 | type e; \ 41 | \ 42 | s = base; \ 43 | t = last - 1; \ 44 | while (s < t) { \ 45 | p = s; \ 46 | q = t; \ 47 | m = p + (q - p) / 2; \ 48 | if (lt(m, p)) \ 49 | _swap(*m, *p); \ 50 | if (lt(q, m)) { \ 51 | _swap(*q, *m); \ 52 | if (lt(m, p)) \ 53 | _swap(*m, *p); \ 54 | } \ 55 | e = *m; \ 56 | for (;;) { \ 57 | do ++p; while (lt(p, &e)); \ 58 | do --q; while (lt(&e, q)); \ 59 | if (p >= q) \ 60 | break; \ 61 | _swap(*p, *q); \ 62 | } \ 63 | if (p > median) \ 64 | t = p - 1; \ 65 | else \ 66 | s = p; \ 67 | } \ 68 | } 69 | 70 | #endif /* _ULIB_SORT_MEDIAN_PROT_H */ 71 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/str_util.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012, 2013 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_STR_UTIL_H 27 | #define _ULIB_STR_UTIL_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* Return the start of the next line (deliminated by either '\n' or 34 | * '\0') and set the delimitor of the next line, if any, to '\0'. NULL 35 | * will be returned if no next line exists. */ 36 | char *nextline(char *buf, long size); 37 | 38 | /* Get the id-th field (0-based) delimited by @delim. If @field is not 39 | * NULL, @field will hold a copy of up to @flen bytes of the field 40 | * including an appended '\0' in the end. */ 41 | const char *getfield(const char *from, const char *end, 42 | int id, char *field, int flen, int delim); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* _ULIB_STR_UTIL_H */ 49 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/tree_util.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_TREE_UTIL_H 27 | #define _ULIB_TREE_UTIL_H 28 | 29 | #include 30 | #include "tree.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | int tree_height(struct tree_root_np *root); 37 | 38 | #define TREE_HEIGHT(root) \ 39 | tree_height((struct tree_root_np*)(root)) 40 | 41 | /* consistency check: 42 | * 1. left < parent < right; 43 | * 2. parent pointers are correct; */ 44 | int tree_verify(struct tree_root *root, 45 | int (*compare) (const void *, const void *)); 46 | 47 | #define TREE_VERIFY(root, comp) \ 48 | tree_verify((struct tree_root*)(root), \ 49 | (int (*)(const void *, const void *))(comp)) 50 | 51 | /* recursively count the number of nodes */ 52 | size_t tree_count(struct tree_root_np *root); 53 | 54 | #define TREE_COUNT(root) \ 55 | tree_count((struct tree_root_np*)(root)) 56 | 57 | /* print each path of the tree */ 58 | void tree_print(struct tree_root *root, 59 | void (*callback)(struct tree_root *)); 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif /* _ULIB_TREE_UTIL_H */ 66 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/ulib_ver.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_VER_H 27 | #define _ULIB_VER_H 28 | 29 | #define ULIB_VERSION_MAJOR 2 30 | #define ULIB_VERSION_MINOR 0 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | const char *ulib_version(); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif /* _ULIB_VER_H */ 43 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/util_argv.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_ARGV_H 27 | #define _ULIB_ARGV_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /** 34 | * argv_free - free an argv 35 | * @argv - the argument vector to be freed 36 | * 37 | * Frees an argv and the strings it points to. 38 | */ 39 | void argv_free(char **argv); 40 | 41 | /** 42 | * argv_split - split a string at whitespace, returning an argv 43 | * @str: the string to be split 44 | * @argcp: returned argument count 45 | * 46 | * Returns an array of pointers to strings which are split out from 47 | * @str. This is performed by strictly splitting on white-space; no 48 | * quote processing is performed. Multiple whitespace characters are 49 | * considered to be a single argument separator. The returned array 50 | * is always NULL-terminated. Returns NULL on memory allocation 51 | * failure. 52 | */ 53 | char **argv_split(const char *str, int *argcp); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/util_class.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_CLASS_H 27 | #define _ULIB_UTIL_CLASS_H 28 | 29 | #include 30 | 31 | namespace ulib { 32 | 33 | struct ulib_except : public std::exception { 34 | virtual 35 | ~ulib_except() throw() { } 36 | }; 37 | 38 | template 39 | struct do_nothing_combiner { 40 | virtual void 41 | operator() (T &, const T &) const { } 42 | }; 43 | 44 | } // namespace ulib 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/util_console.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_CONSOLE_H 27 | #define _ULIB_UTIL_CONSOLE_H 28 | 29 | #define DEF_PROMPT "ULIB CMD> " 30 | 31 | /* 32 | * cmdlet function prototype 33 | */ 34 | typedef int (*console_fcn_t) (int argc, const char *argv[]); 35 | 36 | typedef struct { 37 | void * idx; 38 | char * pmpt; 39 | char * rbuf; 40 | int rfd; 41 | int rbuflen; 42 | } console_t; 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | int console_init(console_t *ctx); 49 | int console_pmpt(console_t *ctx, const char *pmpt); 50 | int console_bind(console_t *ctx, const char *cmdlet, console_fcn_t f); 51 | int console_exec(console_t *ctx, const char *cmd); 52 | 53 | /* enter command processing loop 54 | * @count: how many commands to process, -1 for infinite 55 | * @term: terminating command */ 56 | int console_loop(console_t *ctx, int count, const char *term); 57 | 58 | void console_destroy(console_t *ctx); 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif /* _ULIB_UTIL_CONSOLE_H */ 65 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/util_log.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_LOG_H 27 | #define _ULIB_UTIL_LOG_H 28 | 29 | #include 30 | 31 | #define ULIB_LOG(level, fmt, ...) \ 32 | fprintf(level, "[%s:%d]\t" fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) 33 | 34 | /* debug log can be utterly removed in compiling time by defining 35 | * UNDEBUG */ 36 | #ifdef UNDEBUG 37 | #define ULIB_DEBUG(fmt, ...) 38 | #else 39 | #define ULIB_DEBUG(fmt, ...) \ 40 | ULIB_LOG(stdout, "[D] " fmt, ##__VA_ARGS__) 41 | #endif 42 | 43 | #define ULIB_NOTICE(fmt, ...) \ 44 | ULIB_LOG(stdout, "[I] " fmt, ##__VA_ARGS__) 45 | 46 | #define ULIB_WARNING(fmt, ...) \ 47 | ULIB_LOG(stderr, "[W] " fmt, ##__VA_ARGS__) 48 | 49 | #define ULIB_FATAL(fmt, ...) \ 50 | ULIB_LOG(stderr, "[E] " fmt, ##__VA_ARGS__) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /ulib-svn/include/ulib/util_timer.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_TIMER_H 27 | #define _ULIB_UTIL_TIMER_H 28 | 29 | #include 30 | 31 | typedef struct timespec ulib_timer_t; 32 | 33 | static inline void timer_start(ulib_timer_t *ts) 34 | { 35 | clock_gettime(CLOCK_MONOTONIC, ts); 36 | } 37 | 38 | static inline double timer_stop(const ulib_timer_t *ts) 39 | { 40 | ulib_timer_t tsnow; 41 | 42 | clock_gettime(CLOCK_MONOTONIC, &tsnow); 43 | 44 | return (tsnow.tv_sec - ts->tv_sec + 45 | (tsnow.tv_nsec - ts->tv_nsec) / 1000000000.0); 46 | } 47 | 48 | #endif /* _ULIB_UTIL_TIMER_H */ 49 | -------------------------------------------------------------------------------- /ulib-svn/lib/Makefile: -------------------------------------------------------------------------------- 1 | QUIET ?= @ 2 | 3 | TARGET = libulib.a 4 | 5 | $(TARGET): 6 | $(QUIET)for lib in $(wildcard *.a); do $(AR) x $${lib}; done; 7 | $(QUIET)$(AR) csr $(TARGET) *.o 8 | $(QUIET)rm -rf *.o 9 | 10 | debug: $(TARGET) 11 | 12 | release: $(TARGET) 13 | $(QUIET)strip --strip-debug --strip-unneeded -w -K \* $(TARGET) 14 | 15 | clean: 16 | $(QUIET)rm -rf *.a $(TARGET) 17 | 18 | .PHONY: debug release clean 19 | -------------------------------------------------------------------------------- /ulib-svn/lib/__.SYMDEF SORTED: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyrise/nvm_malloc/dbbd19c02204e193b2476711d23da3e72d9ab171/ulib-svn/lib/__.SYMDEF SORTED -------------------------------------------------------------------------------- /ulib-svn/lib/makedll.bat: -------------------------------------------------------------------------------- 1 | rem ======================--------=========================== 2 | rem Make Windows DLL 3 | rem Copyright (C) 2010-2013 Zilong Tan 4 | rem ========================================================= 5 | 6 | @echo off 7 | 8 | @cls 9 | 10 | @if exist *.o del *.o 11 | 12 | @for /R %%a in (*.a) do echo Extracting %%a ... && ar x %%a 13 | 14 | @echo Building ULIB.DLL ... 15 | 16 | @dllwrap.exe --driver-name x86_64-w64-mingw32-c++ --export-all-symbols 17 | -static -mtune=native -march=native -msse2 -mfpmath=sse -LC:/MinGW/lib -LC:/MinGW64/lib --add-stdcall-alias -mthreads -O3 -o ulib.dll --implib ulib.lib libulib.a -lpthread 18 | @if errorlevel 1 goto dll_error 19 | 20 | @del *.o 21 | 22 | goto done 23 | 24 | :dll_error 25 | @echo "dllwrap failed." 26 | @goto done 27 | 28 | :done 29 | @pause 30 | -------------------------------------------------------------------------------- /ulib-svn/perf/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean 2 | 3 | all: 4 | make -C mapreduce/ 5 | 6 | clean: 7 | make -C mapreduce/ clean 8 | -------------------------------------------------------------------------------- /ulib-svn/perf/avl/libavl/avl_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "avl.h" 10 | 11 | uint64_t u, v, w; 12 | #define myrand() RAND_NR_NEXT(u, v, w) 13 | 14 | struct node { 15 | uint64_t key; 16 | }; 17 | 18 | static inline int node_cmp(const void *a, const void *b, void *) 19 | { 20 | return generic_compare( 21 | ((struct node *)a)->key, 22 | ((struct node *)b)->key); 23 | } 24 | 25 | static inline void node_destroy(void *t, void *) 26 | { 27 | delete (node *)t; 28 | } 29 | 30 | int main(int argc, char *argv[]) 31 | { 32 | int num = 1000000; 33 | avl_table *root; 34 | 35 | if (argc > 1) 36 | num = atoi(argv[1]); 37 | 38 | uint64_t seed = time(NULL); 39 | RAND_NR_INIT(u, v, w, seed); 40 | 41 | root = avl_create(node_cmp, NULL, &avl_allocator_default); 42 | 43 | ulib_timer_t timer; 44 | timer_start(&timer); 45 | for (int i = 0; i < num; ++i) { 46 | node *t = new node; 47 | t->key = myrand(); 48 | avl_insert(root, t); 49 | } 50 | printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); 51 | 52 | timer_start(&timer); 53 | for (int i = 0; i < 1000000; ++i) { 54 | node t; 55 | t.key = myrand(); 56 | avl_find(root, &t); 57 | } 58 | printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); 59 | 60 | timer_start(&timer); 61 | avl_destroy(root, node_destroy); 62 | printf("Deleting 1M elems elapsed: %f\n", timer_stop(&timer)); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /ulib-svn/perf/avl/libavl/result: -------------------------------------------------------------------------------- 1 | Inserting 1M elems elapsed: 1.388314 2 | Searching 10M elems elapsed: 1.394957 3 | Deleting 1M elems elapsed: 0.213274 4 | -------------------------------------------------------------------------------- /ulib-svn/perf/avl/solaris/avl_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "avl.h" 10 | 11 | uint64_t u, v, w; 12 | #define myrand() RAND_NR_NEXT(u, v, w) 13 | 14 | struct node { 15 | avl_node_t link; 16 | uint64_t key; 17 | }; 18 | 19 | static inline int node_cmp(const void *a, const void *b) 20 | { 21 | return generic_compare( 22 | ((struct node *)a)->key, 23 | ((struct node *)b)->key); 24 | } 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | int num = 1000000; 29 | avl_tree_t root; 30 | 31 | if (argc > 1) 32 | num = atoi(argv[1]); 33 | 34 | uint64_t seed = time(NULL); 35 | RAND_NR_INIT(u, v, w, seed); 36 | 37 | avl_create(&root, node_cmp, sizeof(node), 0); 38 | 39 | ulib_timer_t timer; 40 | timer_start(&timer); 41 | for (int i = 0; i < num; ++i) { 42 | node *t = new node; 43 | t->key = myrand(); 44 | avl_index_t where; 45 | avl_find(&root, t, &where); 46 | avl_insert(&root, t, where); 47 | } 48 | printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); 49 | 50 | timer_start(&timer); 51 | for (int i = 0; i < 1000000; ++i) { 52 | node t; 53 | t.key = myrand(); 54 | avl_find(&root, &t.link, NULL); 55 | } 56 | printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); 57 | 58 | timer_start(&timer); 59 | for (node *p = (node*)avl_first(&root); p != NULL; 60 | p = (node*)AVL_NEXT(&root, p)) { 61 | avl_remove(&root, p); 62 | delete p; 63 | } 64 | printf("Deleting 1M elems elapsed: %f\n", timer_stop(&timer)); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /ulib-svn/perf/avl/solaris/result: -------------------------------------------------------------------------------- 1 | Inserting 1M elems elapsed: 0.960035 2 | Searching 10M elems elapsed: 1.069714 3 | Deleting 1M elems elapsed: 0.183905 4 | -------------------------------------------------------------------------------- /ulib-svn/perf/avl/ulib/result: -------------------------------------------------------------------------------- 1 | Inserting 1M elems elapsed: 0.893157 2 | Height: 24 3 | Searching 10M elems elapsed: 1.004864 4 | Deleting 1M elems elapsed: 0.169711 5 | 6 | -------------------------------------------------------------------------------- /ulib-svn/perf/hashmap/Makefile: -------------------------------------------------------------------------------- 1 | QUIET ?= @ 2 | 3 | SPARSEHASH_INC = ../../../sparsehash/include 4 | GPERFTOOLS_INC = ../../../gperftools/include 5 | GPERFTOOLS_LIB = ../../../gperftools/lib 6 | EASTL_INC = ../../../eastl/include 7 | EASTL_LIB = ../../../eastl/lib 8 | RDESTL_INC = ../../../rdestl 9 | ULIB_INC = ../../include 10 | 11 | INC = -I$(SPARSEHASH_INC) -I$(GPERFTOOLS_INC) -I$(EASTL_INC) -I$(RDESTL_INC) -I$(ULIB_INC) 12 | LIB = -L$(GPERFTOOLS_LIB) -L$(EASTL_LIB) 13 | 14 | CXXFLAGS ?=-O2 -W -Wall $(INC) 15 | LDFLAGS ?=$(LIB) -lEASTL -ltcmalloc -lrt -lpthread -static 16 | 17 | .cpp.o: 18 | $(QUIET)echo -e " CXX "$< 19 | $(QUIET)$(CXX) -c $(CXXFLAGS) $< -o $@ 20 | 21 | hash_perf: hash_perf.o 22 | $(QUIET)echo -e " CXX "hash_perf 23 | $(QUIET)$(CXX) -o hash_perf hash_perf.o $(LDFLAGS) 24 | 25 | clean: 26 | $(QUIET)rm -rf hash_perf hash_perf.o *~ 27 | 28 | .PHONY: clean 29 | -------------------------------------------------------------------------------- /ulib-svn/perf/mapreduce/Makefile: -------------------------------------------------------------------------------- 1 | QUIET ?= @ 2 | INCPATH = ../../include 3 | LIBPATH = ../../lib 4 | 5 | CXXFLAGS ?=-O2 -W -Wall -I$(INCPATH) 6 | #PROFILER =-L../../../gperftools/lib -lprofiler 7 | #TCMALLOC =-L../../../gperftools/lib -ltcmalloc 8 | LDFLAGS =-L$(LIBPATH) $(PROFILER) $(TCMALLOC) -lulib -lpthread -lrt 9 | 10 | APP = $(addsuffix .test, $(basename $(wildcard *.cpp))) 11 | 12 | %.test: %.cpp 13 | $(QUIET)echo -e " GEN "$@ 14 | $(QUIET)$(CXX) $(CXXFLAGS) -o $@ $< $(LDFLAGS) 15 | 16 | all: $(APP) 17 | 18 | clean: 19 | $(QUIET)rm -rf $(APP) *~ 20 | 21 | .PHONY: all clean 22 | -------------------------------------------------------------------------------- /ulib-svn/rules.mk: -------------------------------------------------------------------------------- 1 | QUIET ?= @ 2 | PREFIX ?= . 3 | INCPATH ?= $(PREFIX)/include/ulib 4 | LIBPATH ?= $(PREFIX)/lib 5 | LIBNAME ?= undef 6 | 7 | CFLAGS ?= -g3 -O2 -Wall -W -pipe -c -fPIC 8 | CXXFLAGS ?= -g3 -O2 -Wall -W -pipe -c -fPIC 9 | DEBUG ?= 10 | 11 | OBJS = \ 12 | $(addsuffix .o, $(basename $(wildcard *.c))) \ 13 | $(addsuffix .o, $(basename $(wildcard *.cpp))) 14 | 15 | HEADERS = $(wildcard *.h) 16 | 17 | .c.o: 18 | $(QUIET)echo -e " CC "$< 19 | $(QUIET)$(CC) $(CFLAGS) -I$(INCPATH) $(DEBUG) $< -o $@ 20 | 21 | .cpp.o: 22 | $(QUIET)echo -e " CXX "$< 23 | $(QUIET)$(CXX) $(CXXFLAGS) -I$(INCPATH) $(DEBUG) $< -o $@ 24 | 25 | .PHONY: install_headers install_libs \ 26 | uninstall_headers uninstall_libs \ 27 | clean 28 | 29 | clean: uninstall_headers uninstall_libs 30 | $(QUIET)rm -rf $(OBJS) 31 | $(QUIET)find . -name "*~" | xargs rm -rf 32 | 33 | install_headers: 34 | $(QUIET)mkdir -p $(INCPATH) 35 | $(QUIET)cp $(HEADERS) $(INCPATH)/ 36 | 37 | install_libs: $(OBJS) 38 | $(QUIET)mkdir -p $(LIBPATH) 39 | $(QUIET)echo " AR "$(LIBPATH)/$(LIBNAME) 40 | $(QUIET)ar csr $(LIBPATH)/$(LIBNAME) $(OBJS) 41 | 42 | uninstall_headers: 43 | $(QUIET)for file in $(HEADERS); do rm -rf $(INCPATH)/$$file; done 44 | 45 | uninstall_libs: 46 | $(QUIET)rm -rf $(LIBPATH)/$(LIBNAME) 47 | -------------------------------------------------------------------------------- /ulib-svn/src/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean 2 | 3 | all: 4 | make -C base/ 5 | make -C ext1/ 6 | make -C ext2/ 7 | 8 | clean: 9 | make -C base/ clean 10 | make -C ext1/ clean 11 | make -C ext2/ clean 12 | -------------------------------------------------------------------------------- /ulib-svn/src/base/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../.. 2 | LIBNAME = libbase.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/base/crypt_rc4.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include "crypt_rc4.h" 27 | #include "util_algo.h" 28 | 29 | void rc4_setks(const uint8_t *buf, size_t len, rc4_ks_t *ks) 30 | { 31 | uint8_t j = 0; 32 | uint8_t *state = ks->state; 33 | int i; 34 | 35 | for (i = 0; i < 256; ++i) 36 | state[i] = i; 37 | 38 | ks->x = 0; 39 | ks->y = 0; 40 | 41 | for (i = 0; i < 256; ++i) { 42 | j = j + state[i] + buf[i % len]; 43 | _swap(state[i], state[j]); 44 | } 45 | } 46 | 47 | void rc4_crypt(uint8_t *buf, size_t len, rc4_ks_t *ks) 48 | { 49 | uint8_t x; 50 | uint8_t y; 51 | uint8_t *state = ks->state; 52 | unsigned int i; 53 | 54 | x = ks->x; 55 | y = ks->y; 56 | 57 | for (i = 0; i < len; i++) { 58 | y = y + state[++x]; 59 | _swap(state[x], state[y]); 60 | buf[i] ^= state[(state[x] + state[y]) & 0xff]; 61 | } 62 | 63 | ks->x = x; 64 | ks->y = y; 65 | } 66 | -------------------------------------------------------------------------------- /ulib-svn/src/base/crypt_rc4.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_CRYPT_RC4_H 27 | #define _ULIB_CRYPT_RC4_H 28 | 29 | #include 30 | #include 31 | 32 | typedef struct rc4_ks_t { 33 | uint8_t state[256]; 34 | uint8_t x; 35 | uint8_t y; 36 | } rc4_ks_t; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | void rc4_setks(const uint8_t *kbuf, size_t klen, rc4_ks_t *ks); 43 | 44 | // in-place encryption/decryption 45 | void rc4_crypt(uint8_t *buf, size_t len, rc4_ks_t *ks); 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif /* _ULIB_CRYPT_RC4_H */ 52 | -------------------------------------------------------------------------------- /ulib-svn/src/base/crypt_sha256.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_CRYPT_SHA256_H 27 | #define _ULIB_CRYPT_SHA256_H 28 | 29 | #include 30 | #include 31 | 32 | #define SHA256_HASH_SIZE 32 /* 256 bit */ 33 | #define SHA256_HASH_WORDS 8 34 | 35 | typedef struct { 36 | uint64_t totalLength; 37 | uint32_t hash[SHA256_HASH_WORDS]; 38 | uint32_t bufferLength; 39 | union { 40 | uint32_t words[16]; 41 | uint8_t bytes[64]; 42 | } buffer; 43 | } sha256_ctx_t; 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | void sha256_init(sha256_ctx_t * sc); 50 | void sha256_update(sha256_ctx_t * sc, const void *data, size_t len); 51 | void sha256_finalize(sha256_ctx_t * sc, uint8_t hash[SHA256_HASH_SIZE]); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* _ULIB_CRYPT_SHA256_H */ 58 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_bn.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include "math_bn.h" 27 | 28 | unsigned long mpower(unsigned long a, unsigned long b, unsigned long m) 29 | { 30 | unsigned long r = 1; 31 | 32 | while (b) { 33 | if (b & 1) 34 | r = r * a % m; 35 | a = a * a % m; 36 | b >>= 1; 37 | } 38 | 39 | return r; 40 | } 41 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_bn.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_BN_H 27 | #define _ULIB_MATH_BN_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* compute a^b % m */ 34 | unsigned long mpower(unsigned long a, unsigned long b, unsigned long m); 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | 40 | #endif /* _ULIB_MATH_BN_H */ 41 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_factorial.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_FACTORIAL_H 27 | #define _ULIB_MATH_FACTORIAL_H 28 | 29 | #include 30 | 31 | #define PI 3.1415926 32 | 33 | /* Gosper's approximation for ln(n!) */ 34 | static inline double 35 | ln_factorial(unsigned int n) 36 | { 37 | if (n == 0) 38 | return 0; 39 | return log((2*n + 1/3.0) * PI)/2 + n*log(n) - n; 40 | } 41 | 42 | /* Gosper's approximation for n! */ 43 | static inline double 44 | factorial(unsigned int n) 45 | { 46 | return sqrt((2*n + 1/3.0) * PI) * pow(n, n) * exp(-(double)n); 47 | } 48 | 49 | /* Routines based on Feller's approximation for n! */ 50 | static inline double 51 | ln_comb(unsigned int n, unsigned int r) 52 | { 53 | if (r == n || r == 0) 54 | return 0; 55 | return (n - r) * log((double)n / (n - r)) + r * log((double)n / r) - 56 | 0.5 * log(2 * PI * r * (n - r) / n) + 57 | 1.0 / 12 * (1.0 / n - 1.0 / (n - r) - 1.0 / r); 58 | } 59 | 60 | static inline double 61 | comb(unsigned int n, unsigned int r) 62 | { 63 | if (r > n) 64 | return 0; 65 | if (n == 0 || r == 0 || n == r) 66 | return 1; 67 | /* won't calculate directly since it may cause overflow */ 68 | return exp(ln_comb(n, r)); 69 | } 70 | 71 | #endif /* _ULIB_MATH_FACTORIAL_H */ 72 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_gcd.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include "math_gcd.h" 27 | 28 | unsigned long gcd(unsigned long a, unsigned long b) 29 | { 30 | unsigned long r; 31 | 32 | while (b) { 33 | r = a % b; 34 | a = b; 35 | b = r; 36 | } 37 | return a; 38 | } 39 | 40 | void egcd(unsigned long a, unsigned long b, long *x, long *y) 41 | { 42 | long x1, x2, x3, y1, y2, y3; 43 | unsigned long r, q; 44 | 45 | x1 = 1; 46 | x2 = 0; 47 | y1 = 0; 48 | y2 = 1; 49 | 50 | while (b != 0) { 51 | q = a / b; 52 | r = a % b; 53 | a = b; 54 | b = r; 55 | x3 = x1 - q * x2; 56 | x1 = x2; 57 | x2 = x3; 58 | y3 = y1 - q * y2; 59 | y1 = y2; 60 | y2 = y3; 61 | } 62 | *x = x1; 63 | *y = y1; 64 | } 65 | 66 | unsigned long invert(unsigned long m, unsigned long b) 67 | { 68 | long s, t; 69 | 70 | egcd(m, b, &t, &s); 71 | 72 | if (s < 0) 73 | return m + s; 74 | return s; 75 | } 76 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_gcd.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_GCD_H 27 | #define _ULIB_MATH_GCD_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | unsigned long gcd(unsigned long a, unsigned long b); 34 | 35 | /* compute x,y st. ax + by = 1 */ 36 | void egcd(unsigned long a, unsigned long b, long *x, long *y); 37 | 38 | /* computes a st. a * b = 1 mod m */ 39 | unsigned long invert(unsigned long m, unsigned long b); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* _ULIB_MATH_GCD_H */ 46 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_lcm.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include "math_gcd.h" 27 | #include "math_lcm.h" 28 | 29 | unsigned long lcm(unsigned long a, unsigned long b) 30 | { 31 | if (a && b) 32 | return (a * b) / gcd(a, b); 33 | else if (b) 34 | return b; 35 | 36 | return a; 37 | } 38 | -------------------------------------------------------------------------------- /ulib-svn/src/base/math_lcm.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_LCM_H 27 | #define _ULIB_MATH_LCM_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* Lowest common multiple */ 34 | unsigned long lcm(unsigned long a, unsigned long b); 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | 40 | #endif /* _ULIB_MATH_LCM_H */ 41 | -------------------------------------------------------------------------------- /ulib-svn/src/base/search_line.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | /* This file implements the binary search of text lines. The input 27 | * text file must be sorted in ascending order according to the 28 | * compare function provided by the user. */ 29 | 30 | #ifndef _ULIB_SEARCH_LINE_H 31 | #define _ULIB_SEARCH_LINE_H 32 | 33 | #include 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* Find a matching line and return the file offset to the line. 40 | * @comp: a line compare function that takes two parameters: a test 41 | * line and a user provided parameter. 42 | * @param: the user-specified parameter for comp, e.g., the target 43 | * line) 44 | * @maxlen: max line length, including the '\n' 45 | * 46 | * Note: lines in fd must be sorted in ascending order with respect to 47 | * @comp. 48 | * 49 | * Return -1 if the line was not found, or offset to the line 50 | * otherwise. 51 | */ 52 | ssize_t findline(int fd, int (*comp) (const char *, void *), 53 | void *param, int maxlen); 54 | 55 | /* find the first matching line */ 56 | ssize_t findfirstline(int fd, int (*comp) (const char *, void *), 57 | void *param, int maxlen); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | #endif /* _ULIB_SEARCH_LINE_H */ 64 | -------------------------------------------------------------------------------- /ulib-svn/src/base/sort_heap_prot.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_SORT_HEAP_PROT_H 27 | #define _ULIB_SORT_HEAP_PROT_H 28 | 29 | #include "heap_prot.h" 30 | 31 | #define DEFINE_HEAPSORT(name, type, lt) \ 32 | DEFINE_HEAP(name, type, lt) \ 33 | \ 34 | static inline void \ 35 | heapsort_##name(type *base, type *last) \ 36 | { \ 37 | heap_init_##name(base, last); \ 38 | heap_sort_##name(base, last); \ 39 | } 40 | 41 | #endif /* _ULIB_SORT_HEAP_PROT_H */ 42 | -------------------------------------------------------------------------------- /ulib-svn/src/base/sort_median_prot.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_SORT_MEDIAN_PROT_H 27 | #define _ULIB_SORT_MEDIAN_PROT_H 28 | 29 | #include "util_algo.h" 30 | 31 | /** 32 | * median - partition an array into tree parts: 33 | * array[median] 34 | */ 35 | #define DEFINE_MEDIAN(name, type, lt) \ 36 | static inline void \ 37 | median_##name(type *base, type *median, type *last) \ 38 | { \ 39 | register type *s, *t, *p, *q, *m; \ 40 | type e; \ 41 | \ 42 | s = base; \ 43 | t = last - 1; \ 44 | while (s < t) { \ 45 | p = s; \ 46 | q = t; \ 47 | m = p + (q - p) / 2; \ 48 | if (lt(m, p)) \ 49 | _swap(*m, *p); \ 50 | if (lt(q, m)) { \ 51 | _swap(*q, *m); \ 52 | if (lt(m, p)) \ 53 | _swap(*m, *p); \ 54 | } \ 55 | e = *m; \ 56 | for (;;) { \ 57 | do ++p; while (lt(p, &e)); \ 58 | do --q; while (lt(&e, q)); \ 59 | if (p >= q) \ 60 | break; \ 61 | _swap(*p, *q); \ 62 | } \ 63 | if (p > median) \ 64 | t = p - 1; \ 65 | else \ 66 | s = p; \ 67 | } \ 68 | } 69 | 70 | #endif /* _ULIB_SORT_MEDIAN_PROT_H */ 71 | -------------------------------------------------------------------------------- /ulib-svn/src/base/str_util.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012, 2013 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include "util_algo.h" 29 | #include "str_util.h" 30 | 31 | char *nextline(char *buf, long size) 32 | { 33 | char *end = buf + size; 34 | 35 | while (buf < end && *buf != '\n' && *buf != '\0') 36 | buf++; 37 | if (buf >= end) 38 | return NULL; 39 | *buf++ = '\0'; 40 | return (buf >= end? NULL: buf); 41 | } 42 | 43 | const char *getfield(const char *from, const char *end, 44 | int id, char *field, int flen, int delim) 45 | { 46 | while (id-- > 0 && from < end) { 47 | from = memchr(from, delim, end - from); 48 | if (from == NULL) 49 | return end; 50 | ++from; 51 | } 52 | if (from < end && field && flen > 0) { 53 | ptrdiff_t len = _min(end - from, (ptrdiff_t)(flen - 1)); 54 | const char *next = (const char *)memchr(from, delim, len); 55 | if (next == NULL) { 56 | memcpy(field, from, len); 57 | field[len] = 0; 58 | } else { 59 | memcpy(field, from, next - from); 60 | field[next - from] = 0; 61 | } 62 | } 63 | return from; 64 | } 65 | -------------------------------------------------------------------------------- /ulib-svn/src/base/str_util.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012, 2013 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_STR_UTIL_H 27 | #define _ULIB_STR_UTIL_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* Return the start of the next line (deliminated by either '\n' or 34 | * '\0') and set the delimitor of the next line, if any, to '\0'. NULL 35 | * will be returned if no next line exists. */ 36 | char *nextline(char *buf, long size); 37 | 38 | /* Get the id-th field (0-based) delimited by @delim. If @field is not 39 | * NULL, @field will hold a copy of up to @flen bytes of the field 40 | * including an appended '\0' in the end. */ 41 | const char *getfield(const char *from, const char *end, 42 | int id, char *field, int flen, int delim); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* _ULIB_STR_UTIL_H */ 49 | -------------------------------------------------------------------------------- /ulib-svn/src/base/tree_util.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_TREE_UTIL_H 27 | #define _ULIB_TREE_UTIL_H 28 | 29 | #include 30 | #include "tree.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | int tree_height(struct tree_root_np *root); 37 | 38 | #define TREE_HEIGHT(root) \ 39 | tree_height((struct tree_root_np*)(root)) 40 | 41 | /* consistency check: 42 | * 1. left < parent < right; 43 | * 2. parent pointers are correct; */ 44 | int tree_verify(struct tree_root *root, 45 | int (*compare) (const void *, const void *)); 46 | 47 | #define TREE_VERIFY(root, comp) \ 48 | tree_verify((struct tree_root*)(root), \ 49 | (int (*)(const void *, const void *))(comp)) 50 | 51 | /* recursively count the number of nodes */ 52 | size_t tree_count(struct tree_root_np *root); 53 | 54 | #define TREE_COUNT(root) \ 55 | tree_count((struct tree_root_np*)(root)) 56 | 57 | /* print each path of the tree */ 58 | void tree_print(struct tree_root *root, 59 | void (*callback)(struct tree_root *)); 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif /* _ULIB_TREE_UTIL_H */ 66 | -------------------------------------------------------------------------------- /ulib-svn/src/base/ulib_ver.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include "ulib_ver.h" 28 | 29 | const char *ulib_version() 30 | { 31 | static char version[256] = { 0 }; 32 | 33 | snprintf( 34 | version, sizeof(version), 35 | "ULIB %d.%d\n" 36 | "Copyright (C) 2009-2012 Zilong Tan (eric.zltan@gmail.com)\n", 37 | ULIB_VERSION_MAJOR, 38 | ULIB_VERSION_MINOR 39 | ); 40 | 41 | return version; 42 | } 43 | -------------------------------------------------------------------------------- /ulib-svn/src/base/ulib_ver.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_VER_H 27 | #define _ULIB_VER_H 28 | 29 | #define ULIB_VERSION_MAJOR 2 30 | #define ULIB_VERSION_MINOR 0 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | const char *ulib_version(); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif /* _ULIB_VER_H */ 43 | -------------------------------------------------------------------------------- /ulib-svn/src/base/util_log.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_LOG_H 27 | #define _ULIB_UTIL_LOG_H 28 | 29 | #include 30 | 31 | #define ULIB_LOG(level, fmt, ...) \ 32 | fprintf(level, "[%s:%d]\t" fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__) 33 | 34 | /* debug log can be utterly removed in compiling time by defining 35 | * UNDEBUG */ 36 | #ifdef UNDEBUG 37 | #define ULIB_DEBUG(fmt, ...) 38 | #else 39 | #define ULIB_DEBUG(fmt, ...) \ 40 | ULIB_LOG(stdout, "[D] " fmt, ##__VA_ARGS__) 41 | #endif 42 | 43 | #define ULIB_NOTICE(fmt, ...) \ 44 | ULIB_LOG(stdout, "[I] " fmt, ##__VA_ARGS__) 45 | 46 | #define ULIB_WARNING(fmt, ...) \ 47 | ULIB_LOG(stderr, "[W] " fmt, ##__VA_ARGS__) 48 | 49 | #define ULIB_FATAL(fmt, ...) \ 50 | ULIB_LOG(stderr, "[E] " fmt, ##__VA_ARGS__) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /ulib-svn/src/base/util_timer.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_TIMER_H 27 | #define _ULIB_UTIL_TIMER_H 28 | 29 | #include 30 | 31 | typedef struct timespec ulib_timer_t; 32 | 33 | static inline void timer_start(ulib_timer_t *ts) 34 | { 35 | clock_gettime(CLOCK_MONOTONIC, ts); 36 | } 37 | 38 | static inline double timer_stop(const ulib_timer_t *ts) 39 | { 40 | ulib_timer_t tsnow; 41 | 42 | clock_gettime(CLOCK_MONOTONIC, &tsnow); 43 | 44 | return (tsnow.tv_sec - ts->tv_sec + 45 | (tsnow.tv_nsec - ts->tv_nsec) / 1000000000.0); 46 | } 47 | 48 | #endif /* _ULIB_UTIL_TIMER_H */ 49 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean 2 | 3 | all: 4 | make -C c++/ 5 | make -C bloom_filter/ 6 | make -C comb/ 7 | make -C console/ 8 | make -C rng/ 9 | 10 | clean: 11 | make -C c++/ clean 12 | make -C bloom_filter/ clean 13 | make -C comb/ clean 14 | make -C console/ clean 15 | make -C rng/ clean 16 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/bloom_filter/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | LIBNAME = libbfilter.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/bloom_filter/bfilter.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_BLOOM_FILTER_H 27 | #define _ULIB_BLOOM_FILTER_H 28 | 29 | #include 30 | 31 | struct bloom_filter { 32 | unsigned long *bitmap; 33 | unsigned long nbits; 34 | unsigned long nelem; /* estimated number of elements */ 35 | int nfunc; /* number of hash functions */ 36 | uint64_t *seeds; /* seeds for hash functions */ 37 | }; 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | int bfilter_create(struct bloom_filter *bf, unsigned long nbits, unsigned long nelem); 44 | void bfilter_destroy(struct bloom_filter *bf); 45 | void bfilter_zero(struct bloom_filter *bf); 46 | void bfilter_set(struct bloom_filter *bf, const void *buf, unsigned long buflen); 47 | int bfilter_get(struct bloom_filter *bf, const void *buf, unsigned long buflen); 48 | void bfilter_set_hash(struct bloom_filter *bf, unsigned long hash); 49 | int bfilter_get_hash(struct bloom_filter *bf, unsigned long hash); 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/c++/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | 3 | .PHONY: all 4 | all: install_headers 5 | 6 | include $(PREFIX)/rules.mk 7 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/c++/util_class.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_CLASS_H 27 | #define _ULIB_UTIL_CLASS_H 28 | 29 | #include 30 | 31 | namespace ulib { 32 | 33 | struct ulib_except : public std::exception { 34 | virtual 35 | ~ulib_except() throw() { } 36 | }; 37 | 38 | template 39 | struct do_nothing_combiner { 40 | virtual void 41 | operator() (T &, const T &) const { } 42 | }; 43 | 44 | } // namespace ulib 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/comb/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | LIBNAME = libcomb.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/comb/math_comb.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include "math_comb.h" 29 | 30 | int comb_begin(int m, int n, combiter_t *iter) 31 | { 32 | if (iter == NULL || n > m || n < 0 || m > 64) 33 | return -1; 34 | 35 | iter->cur = (1ULL << n) - 1; 36 | iter->max = iter->cur << (m - n); 37 | 38 | return 0; 39 | } 40 | 41 | int comb_next(combiter_t *iter) 42 | { 43 | if (iter == NULL) 44 | return -1; 45 | if (iter->cur == 0 || iter->cur >= iter->max) 46 | return -1; 47 | 48 | iter->cur = hweight_next64(iter->cur); 49 | 50 | return 0; 51 | } 52 | 53 | int comb_get(combiter_t *iter, comb_t *comb) 54 | { 55 | if (iter == NULL || comb == NULL) 56 | return -1; 57 | if (iter->cur == 0) 58 | return -1; 59 | *comb = iter->cur; 60 | return 0; 61 | } 62 | 63 | int comb_elem(comb_t *comb) 64 | { 65 | int idx; 66 | 67 | if (comb == NULL) 68 | return -1; 69 | if (*comb == 0) 70 | return -1; 71 | 72 | idx = ffs64(*comb); 73 | *comb &= *comb - 1; 74 | 75 | return idx; 76 | } 77 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/comb/math_comb.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_MATH_COMB_H 27 | #define _ULIB_MATH_COMB_H 28 | 29 | #include 30 | 31 | typedef uint64_t comb_t; 32 | 33 | typedef struct { 34 | comb_t max; 35 | comb_t cur; 36 | } combiter_t; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * comb_begin - begin the combination iteration by initializing @iter 44 | * @m: total number of elements 45 | * @n: number of elements to choose 46 | * @iter: generated combination iterator 47 | * Note: the max @m supported is 64 48 | */ 49 | int comb_begin(int m, int n, combiter_t *iter); 50 | 51 | /** 52 | * comb_next - generate next iterator 53 | * @iter: combination iterator 54 | */ 55 | int comb_next(combiter_t *iter); 56 | 57 | /** 58 | * comb_get - get combination from iterator 59 | * @iter: iterator for the combination 60 | * @comb: output combination 61 | */ 62 | int comb_get(combiter_t *iter, comb_t *comb); 63 | 64 | /** 65 | * comb_elem - get and remove an element from the combination 66 | * @comb: input & output combination 67 | * Note: the elements are numbered starting from 1 68 | */ 69 | int comb_elem(comb_t *comb); 70 | 71 | #ifdef __cplusplus 72 | } 73 | #endif 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/console/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | LIBNAME = libconsole.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/console/util_argv.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #define _GNU_SOURCE /* strndup for earlier glibc */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include "util_argv.h" 32 | 33 | static const char *skip_arg(const char *cp) 34 | { 35 | while (*cp && !isspace(*cp)) 36 | cp++; 37 | 38 | return cp; 39 | } 40 | 41 | static const char *skip_spaces(const char *cp) 42 | { 43 | while (isspace(*cp)) 44 | cp++; 45 | 46 | return cp; 47 | } 48 | 49 | static int count_argc(const char *str) 50 | { 51 | int count = 0; 52 | 53 | while (*str) { 54 | str = skip_spaces(str); 55 | if (*str) { 56 | count++; 57 | str = skip_arg(str); 58 | } 59 | } 60 | 61 | return count; 62 | } 63 | 64 | void argv_free(char **argv) 65 | { 66 | char **p; 67 | for (p = argv; *p; p++) 68 | free(*p); 69 | 70 | free(argv); 71 | } 72 | 73 | char **argv_split(const char *str, int *argcp) 74 | { 75 | int argc = count_argc(str); 76 | char **argv = malloc(sizeof(*argv) * (argc+1)); 77 | char **argvp; 78 | 79 | if (argv == NULL) 80 | goto out; 81 | 82 | if (argcp) 83 | *argcp = argc; 84 | 85 | argvp = argv; 86 | 87 | while (*str) { 88 | str = skip_spaces(str); 89 | 90 | if (*str) { 91 | const char *p = str; 92 | char *t; 93 | 94 | str = skip_arg(str); 95 | 96 | t = strndup(p, str-p); 97 | if (t == NULL) 98 | goto fail; 99 | *argvp++ = t; 100 | } 101 | } 102 | *argvp = NULL; 103 | 104 | out: 105 | return argv; 106 | 107 | fail: 108 | argv_free(argv); 109 | return NULL; 110 | } 111 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/console/util_argv.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_ARGV_H 27 | #define _ULIB_ARGV_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /** 34 | * argv_free - free an argv 35 | * @argv - the argument vector to be freed 36 | * 37 | * Frees an argv and the strings it points to. 38 | */ 39 | void argv_free(char **argv); 40 | 41 | /** 42 | * argv_split - split a string at whitespace, returning an argv 43 | * @str: the string to be split 44 | * @argcp: returned argument count 45 | * 46 | * Returns an array of pointers to strings which are split out from 47 | * @str. This is performed by strictly splitting on white-space; no 48 | * quote processing is performed. Multiple whitespace characters are 49 | * considered to be a single argument separator. The returned array 50 | * is always NULL-terminated. Returns NULL on memory allocation 51 | * failure. 52 | */ 53 | char **argv_split(const char *str, int *argcp); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/console/util_console.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2011, 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_UTIL_CONSOLE_H 27 | #define _ULIB_UTIL_CONSOLE_H 28 | 29 | #define DEF_PROMPT "ULIB CMD> " 30 | 31 | /* 32 | * cmdlet function prototype 33 | */ 34 | typedef int (*console_fcn_t) (int argc, const char *argv[]); 35 | 36 | typedef struct { 37 | void * idx; 38 | char * pmpt; 39 | char * rbuf; 40 | int rfd; 41 | int rbuflen; 42 | } console_t; 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | int console_init(console_t *ctx); 49 | int console_pmpt(console_t *ctx, const char *pmpt); 50 | int console_bind(console_t *ctx, const char *cmdlet, console_fcn_t f); 51 | int console_exec(console_t *ctx, const char *cmd); 52 | 53 | /* enter command processing loop 54 | * @count: how many commands to process, -1 for infinite 55 | * @term: terminating command */ 56 | int console_loop(console_t *ctx, int count, const char *term); 57 | 58 | void console_destroy(console_t *ctx); 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif /* _ULIB_UTIL_CONSOLE_H */ 65 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/rng/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | LIBNAME = librng.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/rng/math_rng_gamma.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | Copyright (C) 2009, 2011 by Attractive Chaos 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include "math_rng_gamma.h" 31 | 32 | void gamma_rng_init(gamma_rng_t *rng) 33 | { 34 | normal_rng_init(rng); 35 | } 36 | 37 | double gamma_rng_next(gamma_rng_t *rng, double a, double b) 38 | { 39 | if (a < 1.0) { 40 | double u; 41 | do { 42 | u = RAND_NR_DOUBLE(RAND_NR_NEXT(rng->u, rng->v, rng->w)); 43 | } while (u == 0.0); 44 | return gamma_rng_next(rng, 1.0 + a, b) * pow(u, 1.0 / a); 45 | } 46 | 47 | double x, v, u; 48 | double d = a - 1.0 / 3.0; 49 | double c = (1.0 / 3.0) / sqrt(d); 50 | 51 | for (;;) { 52 | do { 53 | x = normal_rng_next(rng); 54 | v = 1.0 + c * x; 55 | } while (v <= 0.0); 56 | v = v * v * v; 57 | do { 58 | u = RAND_NR_DOUBLE(RAND_NR_NEXT(rng->u, rng->v, rng->w)); 59 | } while (u == 0.0); 60 | if (u < 1.0 - 0.0331 * x * x * x * x) 61 | break; 62 | if (log(u) < 0.5 * x * x + d * (1 - v + log(v))) 63 | break; 64 | } 65 | return b * d * v; 66 | } 67 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/rng/math_rng_gamma.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | Copyright (C) 2009, 2011 by Attractive Chaos 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #ifndef _ULIB_MATH_RNG_GAMMA_H 28 | #define _ULIB_MATH_RNG_GAMMA_H 29 | 30 | #include 31 | #include "math_rng_normal.h" 32 | 33 | typedef struct normal_rng gamma_rng_t; 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | void gamma_rng_init(gamma_rng_t *rng); 40 | 41 | /* g(x;a,b) = x^{a-1} b^a e^{-bx} / \Gamma(a) */ 42 | double gamma_rng_next(gamma_rng_t *rng, double alpha, double beta); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* _ULIB_MATH_RNG_GAMMA_H */ 49 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/rng/math_rng_normal.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2013 Zilong Tan (eric.zltan@gmail.com) 4 | Copyright (C) 2009, 2011 by Attractive Chaos 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #ifndef _ULIB_MATH_RNG_NORMAL_H 28 | #define _ULIB_MATH_RNG_NORMAL_H 29 | 30 | #include 31 | 32 | struct normal_rng { 33 | uint64_t u, v, w; /* NR rng context */ 34 | }; 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | void normal_rng_init(struct normal_rng *rng); 41 | 42 | /* Ziggurat method */ 43 | double normal_rng_next(struct normal_rng *rng); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif /* _ULIB_MATH_RNG_NORMAL_H */ 50 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/rng/math_rng_zipf.c: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include "math_rng_zipf.h" 31 | 32 | static inline int isequalf(float a, float b) 33 | { 34 | return fabs(a - b) < 0.000001; 35 | } 36 | 37 | void zipf_rng_init(struct zipf_rng *rng, int range, float s) 38 | { 39 | uint64_t seed = (uint64_t) time(NULL); 40 | 41 | rng->range = range++; 42 | rng->s = s; 43 | 44 | if (isequalf(s, 1.0)) 45 | rng->sum = log(range); 46 | else 47 | rng->sum = (pow(range, 1.0 - s) - 1.0)/(1.0 - s); 48 | 49 | RAND_NR_INIT(rng->u, rng->v, rng->w, seed); 50 | } 51 | 52 | int zipf_rng_next(struct zipf_rng *rng) 53 | { 54 | double m = RAND_NR_DOUBLE(RAND_NR_NEXT(rng->u, rng->v, rng->w)); 55 | 56 | if (isequalf(rng->s, 1.0)) 57 | return (int)exp(m * rng->sum); 58 | 59 | return (int)pow(m * rng->sum * (1.0 - rng->s) + 1.0, 1.0/(1.0 - rng->s)); 60 | } 61 | -------------------------------------------------------------------------------- /ulib-svn/src/ext1/rng/math_rng_zipf.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #ifndef _ULIB_RNG_ZIPF_H 27 | #define _ULIB_RNG_ZIPF_H 28 | 29 | #include 30 | 31 | struct zipf_rng { 32 | int range; 33 | float sum; 34 | float s; 35 | uint64_t u, v, w; /* int rng context */ 36 | }; 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * zipf_rng_init - create a zipf rng with density function: 44 | * p(x) = 1/x^s, where x = 1,2,...,range. 45 | * @rng: rng context 46 | * @range: random integer range 47 | * @s: zipf distribution parameter 48 | */ 49 | void zipf_rng_init(struct zipf_rng *rng, int range, float s); 50 | 51 | /** 52 | * zipf_rng_next - generate a random integer 53 | * @rng: rng context 54 | */ 55 | int zipf_rng_next(struct zipf_rng *rng); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* _ULIB_RNG_ZIPF_H */ 62 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean 2 | 3 | all: 4 | make -C thread 5 | make -C osdep 6 | make -C reentrant 7 | make -C mapreduce 8 | 9 | clean: 10 | make -C thread/ clean 11 | make -C osdep/ clean 12 | make -C reentrant/ clean 13 | make -C mapreduce/ clean 14 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/mapreduce/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | LIBNAME = libmr.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/mapreduce/mr_dataset.cpp: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include "os_rdtsc.h" 29 | #include "math_rand_prot.h" 30 | #include "mr_dataset.h" 31 | 32 | namespace ulib { 33 | 34 | namespace mapreduce { 35 | 36 | dataset_zipf::dataset_zipf(size_t size, size_t range, float s) 37 | : _buf(new int[size]), _size(size) 38 | { 39 | zipf_rng rng; 40 | zipf_rng_init(&rng, range, s); 41 | for (size_t i = 0; i < size; ++i) 42 | _buf[i] = zipf_rng_next(&rng); 43 | } 44 | 45 | dataset_zipf::~dataset_zipf() 46 | { delete [] _buf; } 47 | 48 | dataset_random::dataset_random(size_t size, size_t range) 49 | : _buf(new int[size]), _size(size) 50 | { 51 | uint64_t t = rdtsc(); 52 | for (size_t i = 0; i < size; ++i) { 53 | uint64_t r = i + t; 54 | RAND_INT4_MIX64(r); 55 | _buf[i] = r % range; 56 | } 57 | } 58 | 59 | dataset_random::~dataset_random() 60 | { delete [] _buf; } 61 | 62 | } // namespace mapreduce 63 | 64 | } // namespace ulib 65 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/osdep/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | 3 | .PHONY: all 4 | all: install_headers 5 | 6 | include $(PREFIX)/rules.mk 7 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/osdep/os_rdtsc.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 2005 kazutomo (kazutomo@mcs.anl.gov) 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the "Software"), to deal in the Software without 9 | restriction, including without limitation the rights to use, copy, 10 | modify, merge, publish, distribute, sublicense, and/or sell copies 11 | of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | */ 26 | 27 | #ifndef __ULIB_RDTSC_H 28 | #define __ULIB_RDTSC_H 29 | 30 | #include 31 | 32 | #if defined(__i386__) 33 | 34 | static __inline__ uint64_t rdtsc(void) 35 | { 36 | uint64_t x; 37 | __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x)); 38 | return x; 39 | } 40 | 41 | #elif defined(__x86_64__) 42 | 43 | static __inline__ uint64_t rdtsc(void) 44 | { 45 | unsigned hi, lo; 46 | __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); 47 | return ((uint64_t)lo) | (((uint64_t)hi) << 32); 48 | } 49 | 50 | #elif defined(__powerpc__) 51 | 52 | static __inline__ uint64_t rdtsc(void) 53 | { 54 | uint64_t result = 0; 55 | unsigned long upper, lower, tmp; 56 | 57 | __asm__ volatile( 58 | "0: \n" 59 | "\tmftbu %0 \n" 60 | "\tmftb %1 \n" 61 | "\tmftbu %2 \n" 62 | "\tcmpw %2,%0 \n" 63 | "\tbne 0b \n" 64 | : "=r"(upper),"=r"(lower),"=r"(tmp) 65 | ); 66 | 67 | result = upper; 68 | result = result << 32; 69 | result = result | lower; 70 | return result; 71 | } 72 | 73 | #else 74 | 75 | #error "No tick counter is available!" 76 | 77 | #endif 78 | 79 | #endif /* __ULIB_RDSTC_H */ 80 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/reentrant/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | 3 | .PHONY: all 4 | all: install_headers 5 | 6 | include $(PREFIX)/rules.mk 7 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/thread/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = ../../.. 2 | LIBNAME = libthread.a 3 | 4 | .PHONY: all 5 | all: install_headers install_libs 6 | 7 | include $(PREFIX)/rules.mk 8 | -------------------------------------------------------------------------------- /ulib-svn/src/ext2/thread/os_thread.cpp: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, copy, 9 | modify, merge, publish, distribute, sublicense, and/or sell copies 10 | of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | #include 27 | #include "os_thread.h" 28 | 29 | namespace ulib { 30 | 31 | void *thread::_thread(void *param) 32 | { 33 | thread *ctx = (thread *) param; 34 | 35 | int ret = ctx->setup(); 36 | if (ret) { 37 | ULIB_DEBUG("thread initialization failed, setup() returned %d", ret); 38 | return (void *)(unsigned long)-1; 39 | } 40 | ret = ctx->run(); 41 | ctx->cleanup(); 42 | return NULL; 43 | } 44 | 45 | } // namespace ulib 46 | -------------------------------------------------------------------------------- /ulib-svn/test/Makefile: -------------------------------------------------------------------------------- 1 | QUIET ?= @ 2 | 3 | INCPATH = ../include 4 | LIBPATH = ../lib 5 | 6 | EXTRALIB ?= 7 | 8 | CXXFLAGS ?= -g3 -O2 -W -Wall 9 | LDFLAGS ?= -lulib $(EXTRALIB) -lpthread -lrt 10 | DEBUG ?= 11 | 12 | TARGET = $(addsuffix .test, $(basename $(wildcard *.cpp))) 13 | 14 | %.test: %.cpp 15 | $(QUIET)echo -e " GEN "$@; 16 | $(QUIET)$(CXX) -I $(INCPATH) $(CXXFLAGS) $(DEBUG) $< -o $@ -L $(LIBPATH) $(LDFLAGS); 17 | 18 | all: $(TARGET) 19 | 20 | clean: 21 | $(QUIET)rm -rf $(TARGET) *~ 22 | 23 | test: 24 | $(QUIET)./run_all_test.sh 25 | 26 | .PHONY: all clean test 27 | -------------------------------------------------------------------------------- /ulib-svn/test/atomic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | atomic_barrier(); 8 | 9 | uint64_t a = 0; 10 | 11 | assert(atomic_cmpswp64(&a, 1, 2) == 0); 12 | assert(a == 0); 13 | assert(atomic_cmpswp64(&a, 0, 2) == 0); 14 | assert(a == 2); 15 | assert(atomic_cmpswp16(&a, 0, 2) == 2); 16 | assert(atomic_cmpswp16(&a, 2, 0) == 2); 17 | assert(atomic_cmpswp16(&a, 0, 2) == 0); 18 | assert(a == 2); 19 | assert(atomic_fetchadd64(&a, 1) == 2); 20 | assert(a == 3); 21 | assert(atomic_fetchadd64(&a, -1) == 3); 22 | assert(a == 2); 23 | assert(atomic_fetchstore64(&a, 5) == 2); 24 | assert(a == 5); 25 | assert(atomic_test_and_set_bit64(&a, 0) == -1); 26 | assert(a == 5); 27 | assert(atomic_test_and_set_bit64(&a, 1) == 0); 28 | assert(a == 7); 29 | atomic_and64(&a, ~7ul); 30 | assert(a == 0); 31 | atomic_or64(&a, (1ull << 63)); 32 | assert(a == (1ull << 63)); 33 | assert(atomic_test_and_set_bit64(&a, 63) == -1); 34 | atomic_or8(&a, 1); 35 | assert(a == ((1ull << 63) | 1)); 36 | atomic_and8(&a, (int8_t)~1u); 37 | assert(a == (1ull << 63)); 38 | atomic_and64(&a, ~(1ull << 63)); 39 | assert(a == 0); 40 | atomic_inc64(&a); 41 | assert(a == 1); 42 | atomic_dec64(&a); 43 | assert(a == 0); 44 | atomic_add64(&a, -1); 45 | assert(a == (uint64_t)-1); 46 | atomic_cpu_relax(); 47 | 48 | printf("passed\n"); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /ulib-svn/test/avl_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | uint64_t u, v, w; 13 | #define myrand() RAND_NR_NEXT(u, v, w) 14 | 15 | struct avl_node { 16 | struct avl_root link; 17 | int key; 18 | }; 19 | 20 | static inline int avl_node_cmp(const void *a, const void *b) 21 | { 22 | return generic_compare( 23 | ((struct avl_node *)a)->key, 24 | ((struct avl_node *)b)->key); 25 | } 26 | 27 | int main(int argc, char *argv[]) 28 | { 29 | int num = 1000000; 30 | avl_root *root = NULL; 31 | 32 | if (argc > 1) 33 | num = atoi(argv[1]); 34 | 35 | uint64_t seed = time(NULL); 36 | RAND_NR_INIT(u, v, w, seed); 37 | 38 | ulib_timer_t timer; 39 | timer_start(&timer); 40 | for (int i = 0; i < num; ++i) { 41 | avl_node *t = new avl_node; 42 | t->key = myrand(); 43 | if (&t->link != avl_map(&t->link, avl_node_cmp, &root)) 44 | delete t; 45 | } 46 | printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); 47 | 48 | printf("Height: %d\n", TREE_HEIGHT(root)); 49 | 50 | timer_start(&timer); 51 | for (int i = 0; i < 1000000; ++i) { 52 | avl_node t; 53 | t.key = myrand(); 54 | TREE_SEARCH(&t.link, avl_node_cmp, root); 55 | } 56 | printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); 57 | 58 | avl_node *pos, *tmp; 59 | timer_start(&timer); 60 | avl_for_each_entry_safe(pos, tmp, root, link) { 61 | avl_del(&pos->link, &root); 62 | delete pos; 63 | } 64 | printf("Deleting 1M elems elapsed: %f\n", timer_stop(&timer)); 65 | 66 | 67 | printf("passed\n"); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /ulib-svn/test/bfilter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | struct bloom_filter bf; 8 | 9 | assert(bfilter_create(&bf, 1000, 100) == 0); 10 | bfilter_zero(&bf); 11 | 12 | assert(bfilter_get(&bf, "xyz", 3) == 0); 13 | bfilter_set(&bf, "xyz", 3); 14 | assert(bfilter_get(&bf, "xyz", 3) == 1); 15 | 16 | bfilter_destroy(&bf); 17 | 18 | printf("passed\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ulib-svn/test/bfilter_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint64_t u, v, w; 11 | #define myrand() RAND_NR_NEXT(u, v, w) 12 | 13 | const char *usage = 14 | "%s [ins] [get]\n"; 15 | 16 | volatile long counter = 0; 17 | 18 | static void sig_alarm_handler(int) 19 | { 20 | printf("%ld per sec\n", counter); 21 | counter = 0; 22 | alarm(1); 23 | } 24 | 25 | void register_sig_handler() 26 | { 27 | struct sigaction sigact; 28 | 29 | sigact.sa_handler = sig_alarm_handler; 30 | sigact.sa_flags = 0; 31 | if (sigaction(SIGALRM, &sigact, NULL)) { 32 | perror("sigaction"); 33 | exit(-1); 34 | } 35 | alarm(1); 36 | } 37 | 38 | void constant_insert(long ins, long get) 39 | { 40 | long t; 41 | 42 | struct bloom_filter bf; 43 | 44 | if (bfilter_create(&bf, 4 * ins, ins)) { 45 | fprintf(stderr, "alloc failed\n"); 46 | return; 47 | } 48 | bfilter_zero(&bf); 49 | 50 | for (t = 0; t < ins; t++) { 51 | uint64_t num = myrand(); 52 | bfilter_set(&bf, &num, sizeof(num)); 53 | ++counter; 54 | } 55 | 56 | printf("insertion done\n"); 57 | 58 | for (t = 0; t < get; t++) { 59 | uint64_t num = myrand(); 60 | bfilter_get(&bf, &num, sizeof(num)); 61 | ++counter; 62 | } 63 | 64 | printf("all done\n"); 65 | } 66 | 67 | int main(int argc, char *argv[]) 68 | { 69 | long ins = 5000000; 70 | long get = 50000000; 71 | uint64_t seed = time(NULL); 72 | 73 | if (argc > 1) 74 | ins = atol(argv[1]); 75 | if (argc > 2) 76 | get = atol(argv[2]); 77 | 78 | RAND_NR_INIT(u, v, w, seed); 79 | 80 | register_sig_handler(); 81 | 82 | constant_insert(ins, get); 83 | 84 | printf("passed\n"); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /ulib-svn/test/bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | uint64_t u, v, w; 10 | uint64_t seed = time(0); 11 | 12 | RAND_NR_INIT(u, v, w, seed); 13 | 14 | for (int i = 0; i < 100; i++) 15 | printf("rand number = %llx\n", (unsigned long long)RAND_NR_NEXT(u, v, w)); 16 | 17 | uint64_t r = RAND_NR_NEXT(u, v, w); 18 | uint64_t s = BIN_TO_GRAYCODE(r); 19 | uint64_t t = BIN_TO_GRAYCODE(r + 1); 20 | assert(hweight64(t ^ s) == 1); 21 | GRAYCODE_TO_BIN64(s); 22 | 23 | assert(rev8(5) == 160); 24 | assert(rev8_hakmem(5) == 160); 25 | 26 | uint64_t hi, lo; 27 | MULQ(0x1234567887654321ul, 0x77665544332211fful, lo, hi); 28 | assert(hi == 611815671993850618UL); 29 | assert(lo == 14353276178066116319UL); 30 | 31 | if (s != r) 32 | fprintf(stderr, "expected %016llx, acutal %016llx\n", 33 | (unsigned long long)r, 34 | (unsigned long long)s); 35 | else 36 | printf("passed\n"); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /ulib-svn/test/bitmap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | DEFINE_BITMAP(bm, 1000); 8 | 9 | bitmap_zero(bm, 1000); 10 | bitmap_set(bm, 3, 2); 11 | assert(!test_bit(2, bm)); 12 | assert(test_bit(3, bm)); 13 | assert(test_bit(4, bm)); 14 | assert(!test_bit(5, bm)); 15 | 16 | int bit; 17 | int start = 3; 18 | for_each_set_bit(bit, bm, 1000) 19 | assert(bit == start++); 20 | 21 | printf("passed\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ulib-svn/test/bn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | assert(mpower(2, 0, 3) == 1); 8 | assert(mpower(3, 100, 4) == 1); 9 | assert(mpower(3, 10000000, 4) == 1); 10 | assert(mpower(5, 1000, 17) == 16); 11 | 12 | printf("passed\n"); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /ulib-svn/test/comb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | combiter_t ci; 9 | 10 | assert(comb_begin(3, 2, &ci) == 0); 11 | 12 | // index of loop 13 | int s = 0; 14 | 15 | do { 16 | comb_t comb; 17 | // get current combination 18 | if (comb_get(&ci, &comb)) 19 | break; 20 | 21 | // enumerates all elements contained in this combination 22 | int elem; 23 | int t = 0; 24 | while ((elem = comb_elem(&comb)) != -1) { 25 | switch (s) { 26 | case 0: // first round 011 27 | switch (t) { 28 | case 0: assert(elem == 1); 29 | break; 30 | case 1: assert(elem == 2); 31 | break; 32 | default: fprintf(stderr, "elem error\n"); 33 | exit(EXIT_FAILURE); 34 | } 35 | break; 36 | case 1: // second round 101 37 | switch (t) { 38 | case 0: assert(elem == 1); 39 | break; 40 | case 1: assert(elem == 3); 41 | break; 42 | default: fprintf(stderr, "elem error\n"); 43 | exit(EXIT_FAILURE); 44 | } 45 | break; 46 | case 2: // third round 110 47 | switch (t) { 48 | case 0: assert(elem == 2); 49 | break; 50 | case 1: assert(elem == 3); 51 | break; 52 | default: fprintf(stderr, "elem error\n"); 53 | exit(EXIT_FAILURE); 54 | } 55 | break; 56 | default: // no more rounds 57 | fprintf(stderr, "combination error\n"); 58 | exit(EXIT_FAILURE); 59 | } 60 | t++; 61 | } 62 | assert(t == 2); 63 | s++; 64 | } while (!comb_next(&ci)); 65 | 66 | // exactly three combinations for choosing 2 from 3 elements 67 | assert(s == 3); 68 | 69 | printf("passed\n"); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /ulib-svn/test/console.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int g_ok = 0; 7 | 8 | int cmd_set_ok(int, const char **) 9 | { 10 | g_ok = 1; 11 | 12 | return 0; 13 | } 14 | 15 | int cmd_return_error(int, const char **) 16 | { 17 | return -1; 18 | } 19 | 20 | int cmd_param_ok(int argc, const char **argv) 21 | { 22 | if (argc == 2 && strcmp(argv[1], "hello") == 0) 23 | return 0; 24 | 25 | return -1; 26 | } 27 | 28 | int main() 29 | { 30 | console_t con; 31 | 32 | assert(console_init(&con) == 0); 33 | assert(console_bind(&con, "set_ok", cmd_set_ok) == 0); 34 | assert(console_bind(&con, "err", cmd_return_error) == 0); 35 | assert(console_bind(&con, "param", cmd_param_ok) == 0); 36 | 37 | // verify that cmd_set_ok is called 38 | assert(console_exec(&con, "set_ok") == 0); 39 | assert(g_ok == 1); 40 | 41 | // verify that cmd_return_error is called 42 | // should return -1 since cmdfunc would fail 43 | assert(console_exec(&con, "err") == -1); 44 | 45 | // calling nonexisted command returns -1 46 | assert(console_exec(&con, "nonexist") == -1); 47 | 48 | // verify that parameter is parsed correctly 49 | assert(console_exec(&con, "param hello") == 0); 50 | 51 | console_destroy(&con); 52 | 53 | printf("passed\n"); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /ulib-svn/test/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define FLOAT_EXACT_EQUAL(x,y) (fabs((x) - (y)) < 0.01) 7 | #define FLOAT_APPRO_EQUAL(x,y) (fabs((x) - (y)) < 0.1) 8 | 9 | int main() 10 | { 11 | assert(ln_factorial(0) == 0); 12 | assert(FLOAT_EXACT_EQUAL(1.7917595, ln_factorial(3))); 13 | assert(FLOAT_EXACT_EQUAL(8.5251614, ln_factorial(7))); 14 | assert(FLOAT_EXACT_EQUAL(42.335616, ln_factorial(20))); 15 | assert(FLOAT_EXACT_EQUAL(863.23199, ln_factorial(200))); 16 | 17 | assert(FLOAT_APPRO_EQUAL(1, factorial(0))); 18 | assert(FLOAT_APPRO_EQUAL(1, factorial(1))); 19 | assert(FLOAT_APPRO_EQUAL(24, factorial(4))); 20 | assert(FLOAT_APPRO_EQUAL(120, factorial(5))); 21 | 22 | assert(FLOAT_EXACT_EQUAL(0, ln_comb(0, 0))); 23 | assert(FLOAT_EXACT_EQUAL(0, ln_comb(1, 1))); 24 | assert(FLOAT_EXACT_EQUAL(3.555348, ln_comb(7, 3))); 25 | assert(FLOAT_EXACT_EQUAL(1.609438, ln_comb(5, 4))); 26 | assert(FLOAT_EXACT_EQUAL(14.48910, ln_comb(24, 10))); 27 | 28 | assert(FLOAT_EXACT_EQUAL(1, comb(0, 0))); 29 | assert(FLOAT_EXACT_EQUAL(1, comb(1, 1))); 30 | assert(FLOAT_EXACT_EQUAL(1, comb(1, 0))); 31 | assert(FLOAT_EXACT_EQUAL(0, comb(1, 2))); 32 | assert(FLOAT_EXACT_EQUAL(35, comb(7, 3))); 33 | assert(FLOAT_EXACT_EQUAL(6, comb(4, 2))); 34 | 35 | printf("passed\n"); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /ulib-svn/test/fpr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | struct bloom_filter bf; 11 | int m = argc > 1? atoi(argv[1]): 100; 12 | int n = argc > 2? atoi(argv[2]): 1000; 13 | int t = argc > 3? atoi(argv[3]): 1000000; 14 | int i, s; 15 | uint64_t u, v, w, k; 16 | 17 | if (bfilter_create(&bf, n, m)) { 18 | fprintf(stderr, "create bloom filter failed\n"); 19 | exit(EXIT_FAILURE); 20 | } 21 | 22 | for (i = 0; i < m; ++i) 23 | bfilter_set(&bf, &i, sizeof(i)); 24 | 25 | k = rdtsc(); 26 | RAND_NR_INIT(u, v, w, k); 27 | 28 | s = 0; 29 | for (i = 0; i < t; ++i) { 30 | k = RAND_NR_NEXT(u, v, w); 31 | RAND_INT3_MIX64(k); 32 | s += bfilter_get(&bf, &k, sizeof(k)); 33 | } 34 | bfilter_destroy(&bf); 35 | 36 | printf("fpr:%f\n", (float)s / t); 37 | 38 | printf("passed\n"); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /ulib-svn/test/gamma_rng.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int i; 8 | int t = 10; 9 | double alpha = 2.0; 10 | double beta = 1.0; 11 | 12 | gamma_rng_t rng; 13 | 14 | if (argc > 1) 15 | t = atoi(argv[1]); 16 | 17 | gamma_rng_init(&rng); 18 | 19 | for (i = 0; i < t; i++) 20 | printf("%f\n", gamma_rng_next(&rng, alpha, beta)); 21 | 22 | printf("passed\n"); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /ulib-svn/test/gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | assert(7 == gcd(21, 14)); 8 | assert(7 == gcd(14, 21)); 9 | 10 | long x, y; 11 | egcd(3, 2, &x, &y); 12 | assert(x == 1 && y == -1); 13 | egcd(2, 3, &x, &y); 14 | assert(x == -1 && y == 1); 15 | 16 | assert(3 == invert(8, 3)); 17 | 18 | printf("passed\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_chain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace ulib; 6 | 7 | int main() 8 | { 9 | chain_hash_map ch(100); 10 | 11 | ch.find_or_insert(1,2); 12 | 13 | ch[1] = 2; 14 | chain_hash_map::const_iterator it = 15 | ch.find(1); 16 | assert(it != ch.end()); 17 | assert(it.key() == 1); 18 | assert(it.value() == 2); 19 | assert(ch[1] == 2); 20 | assert(ch[2] == 0); 21 | 22 | ch[40910] = 1; 23 | ch[1001] = 4; 24 | 25 | for (it = ch.begin(); it != ch.end(); ++it) 26 | printf("%d\t%d\n", it.key(), it.value()); 27 | 28 | for (chain_hash_map::iterator t = ch.begin(); t != ch.end(); t++) 29 | printf("%d\t%d\n", t.key(), t.value()); 30 | 31 | printf("sort without snapshot:\n"); 32 | ch.sort(); 33 | for (chain_hash_map::iterator t = ch.begin(); t != ch.end(); t++) 34 | printf("%d\t%d\n", t.key(), t.value()); 35 | 36 | printf("sort after snapshot:\n"); 37 | ch.snap(); 38 | ch.sort(); 39 | for (chain_hash_map::iterator t = ch.begin(); t != ch.end(); t++) 40 | printf("%d\t%d\n", t.key(), t.value()); 41 | 42 | printf("inserting an element after snapshot:\n"); 43 | ch[999] = 10; 44 | for (chain_hash_map::iterator t = ch.begin(); t != ch.end(); t++) 45 | printf("%d\t%d\n", t.key(), t.value()); 46 | 47 | printf("inserting an element after snapshot, and re-snap:\n"); 48 | ch.snap(); 49 | for (chain_hash_map::iterator t = ch.begin(); t != ch.end(); t++) 50 | printf("%d\t%d\n", t.key(), t.value()); 51 | 52 | printf("when copied:\n"); 53 | chain_hash_map new_ch = ch; 54 | for (chain_hash_map::iterator t = new_ch.begin(); t != new_ch.end(); t++) 55 | printf("%d\t%d\n", t.key(), t.value()); 56 | 57 | ch.clear(); 58 | 59 | for (chain_hash_map::iterator t = ch.begin(); t != ch.end(); t++) 60 | printf("%d\t%d\n", t.key(), t.value()); 61 | 62 | printf("passed\n"); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_chain_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace ulib; 11 | 12 | uint64_t u, v, w; 13 | #define myrand() RAND_NR_NEXT(u, v, w) 14 | 15 | const char *usage = 16 | "%s [ins] [get]\n"; 17 | 18 | volatile long counter = 0; 19 | 20 | static void sig_alarm_handler(int) 21 | { 22 | printf("%ld per sec\n", counter); 23 | counter = 0; 24 | alarm(1); 25 | } 26 | 27 | void register_sig_handler() 28 | { 29 | struct sigaction sigact; 30 | 31 | sigact.sa_handler = sig_alarm_handler; 32 | sigact.sa_flags = 0; 33 | if (sigaction(SIGALRM, &sigact, NULL)) { 34 | perror("sigaction"); 35 | exit(-1); 36 | } 37 | alarm(1); 38 | } 39 | 40 | void constant_insert(long ins, long get) 41 | { 42 | long t; 43 | 44 | chain_hash_map map(ins * 5 / 4); 45 | 46 | for (t = 0; t < ins; t++) { 47 | map[myrand()] = t; 48 | counter++; 49 | } 50 | 51 | printf("insertion done\n"); 52 | 53 | for (t = 0; t < get; t++) { 54 | map.find(myrand()); 55 | counter++; 56 | } 57 | 58 | printf("all done\n"); 59 | } 60 | 61 | int main(int argc, char *argv[]) 62 | { 63 | long ins = 1000000; 64 | long get = 50000000; 65 | uint64_t seed = time(NULL); 66 | 67 | if (argc > 1) 68 | ins = atol(argv[1]); 69 | if (argc > 2) 70 | get = atol(argv[2]); 71 | 72 | RAND_NR_INIT(u, v, w, seed); 73 | 74 | register_sig_handler(); 75 | 76 | constant_insert(ins, get); 77 | 78 | printf("passed\n"); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_chain_prot_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint64_t u, v, w; 11 | #define myrand() RAND_NR_NEXT(u, v, w) 12 | 13 | const char *usage = 14 | "%s [ins] [get]\n"; 15 | 16 | volatile long counter = 0; 17 | 18 | DEFINE_CHAINHASH(myhash, uint64_t, uint64_t, 1, chainhash_hashfn, chainhash_equalfn, chainhash_cmpfn) 19 | 20 | static void sig_alarm_handler(int) 21 | { 22 | printf("%ld per sec\n", counter); 23 | counter = 0; 24 | alarm(1); 25 | } 26 | 27 | void register_sig_handler() 28 | { 29 | struct sigaction sigact; 30 | 31 | sigact.sa_handler = sig_alarm_handler; 32 | sigact.sa_flags = 0; 33 | if (sigaction(SIGALRM, &sigact, NULL)) { 34 | perror("sigaction"); 35 | exit(-1); 36 | } 37 | alarm(1); 38 | } 39 | 40 | void constant_insert(long ins, long get) 41 | { 42 | long t; 43 | 44 | chainhash_t(myhash) *my = chainhash_init(myhash, ins); 45 | 46 | if (my == NULL) { 47 | fprintf(stderr, "alloc failed\n"); 48 | return; 49 | } 50 | 51 | for (t = 0; t < ins; t++) { 52 | chainhash_itr_t(myhash) itr = 53 | chainhash_set(myhash, my, myrand()); 54 | if (!chainhash_end(itr)) 55 | chainhash_value(myhash, itr) = t; 56 | counter++; 57 | } 58 | 59 | printf("insertion done\n"); 60 | 61 | for (t = 0; t < get; t++) { 62 | chainhash_get(myhash, my, myrand()); 63 | counter++; 64 | } 65 | 66 | chainhash_destroy(myhash, my); 67 | 68 | printf("all done\n"); 69 | } 70 | 71 | int main(int argc, char *argv[]) 72 | { 73 | long ins = 1000000; 74 | long get = 50000000; 75 | uint64_t seed = time(NULL); 76 | 77 | if (argc > 1) 78 | ins = atol(argv[1]); 79 | if (argc > 2) 80 | get = atol(argv[2]); 81 | 82 | RAND_NR_INIT(u, v, w, seed); 83 | 84 | register_sig_handler(); 85 | 86 | constant_insert(ins, get); 87 | 88 | printf("passed\n"); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_chain_r1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace ulib; 6 | 7 | int main() 8 | { 9 | chain_hash_map_r ch(100, 10); 10 | 11 | ch.find_or_insert(1,2); 12 | 13 | ch[1] = 2; 14 | chain_hash_map_r::const_iterator it = 15 | ch.find(1); 16 | assert(it != ch.end()); 17 | assert(it.key() == 1); 18 | assert(it.value() == 2); 19 | assert(ch[1] == 2); 20 | assert(ch[2] == 0); 21 | 22 | ch[40910] = 1; 23 | ch[1001] = 4; 24 | 25 | for (it = ch.begin(); it != ch.end(); ++it) 26 | printf("%d\t%d\n", it.key(), it.value()); 27 | 28 | for (chain_hash_map_r::iterator t = ch.begin(); t != ch.end(); t++) 29 | printf("%d\t%d\n", t.key(), t.value()); 30 | 31 | printf("sort without snapshot:\n"); 32 | ch.sort(); 33 | for (chain_hash_map_r::iterator t = ch.begin(); t != ch.end(); t++) 34 | printf("%d\t%d\n", t.key(), t.value()); 35 | 36 | printf("sort after snapshot:\n"); 37 | ch.snap(); 38 | ch.sort(); 39 | for (chain_hash_map_r::iterator t = ch.begin(); t != ch.end(); t++) 40 | printf("%d\t%d\n", t.key(), t.value()); 41 | 42 | printf("inserting an element after snapshot:\n"); 43 | ch[999] = 10; 44 | for (chain_hash_map_r::iterator t = ch.begin(); t != ch.end(); t++) 45 | printf("%d\t%d\n", t.key(), t.value()); 46 | 47 | printf("inserting an element after snapshot, and re-snap:\n"); 48 | ch.snap(); 49 | for (chain_hash_map_r::iterator t = ch.begin(); t != ch.end(); t++) 50 | printf("%d\t%d\n", t.key(), t.value()); 51 | 52 | printf("when copied:\n"); 53 | chain_hash_map_r new_ch = ch; 54 | for (chain_hash_map_r::iterator t = new_ch.begin(); t != new_ch.end(); t++) 55 | printf("%d\t%d\n", t.key(), t.value()); 56 | 57 | ch.clear(); 58 | 59 | for (chain_hash_map_r::iterator t = ch.begin(); t != ch.end(); t++) 60 | printf("%d\t%d\n", t.key(), t.value()); 61 | 62 | printf("passed\n"); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_chain_r_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace ulib; 11 | 12 | uint64_t u, v, w; 13 | #define myrand() RAND_NR_NEXT(u, v, w) 14 | 15 | const char *usage = 16 | "%s [ins] [get]\n"; 17 | 18 | volatile long counter = 0; 19 | 20 | static void sig_alarm_handler(int) 21 | { 22 | printf("%ld per sec\n", counter); 23 | counter = 0; 24 | alarm(1); 25 | } 26 | 27 | void register_sig_handler() 28 | { 29 | struct sigaction sigact; 30 | 31 | sigact.sa_handler = sig_alarm_handler; 32 | sigact.sa_flags = 0; 33 | if (sigaction(SIGALRM, &sigact, NULL)) { 34 | perror("sigaction"); 35 | exit(-1); 36 | } 37 | alarm(1); 38 | } 39 | 40 | void constant_insert(long ins, long get) 41 | { 42 | long t; 43 | 44 | chain_hash_map_r map(ins * 5 / 4, 1000); 45 | 46 | for (t = 0; t < ins; t++) { 47 | map[myrand()] = t; 48 | counter++; 49 | } 50 | 51 | printf("insertion done\n"); 52 | 53 | for (t = 0; t < get; t++) { 54 | chain_hash_map_r::const_iterator it = 55 | map.find(myrand()); 56 | counter++; 57 | } 58 | 59 | printf("all done\n"); 60 | } 61 | 62 | int main(int argc, char *argv[]) 63 | { 64 | long ins = 1000000; 65 | long get = 50000000; 66 | uint64_t seed = time(NULL); 67 | 68 | if (argc > 1) 69 | ins = atol(argv[1]); 70 | if (argc > 2) 71 | get = atol(argv[2]); 72 | 73 | RAND_NR_INIT(u, v, w, seed); 74 | 75 | register_sig_handler(); 76 | 77 | constant_insert(ins, get); 78 | 79 | printf("passed\n"); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_chain_r_workload_shared.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace ulib; 13 | 14 | #define R_TH_NUM 5 15 | #define W_TH_NUM 5 16 | #define MASK 0xfffff 17 | 18 | // working time in seconds 19 | #define WORKING_TIME 5 20 | 21 | pid_t gettid() 22 | { return syscall( __NR_gettid ); } 23 | 24 | chain_hash_map_r shared_map(10000000, 256); 25 | 26 | class writer : public thread { 27 | public: 28 | int 29 | run() 30 | { 31 | while (is_running()) { 32 | uint64_t r = _seed++; 33 | shared_map.combine(RAND_INT4_MIX64(r) & MASK, r); 34 | ++_cnt; 35 | } 36 | return 0; 37 | } 38 | 39 | writer() 40 | : _cnt(0) 41 | { 42 | _seed = gettid(); 43 | RAND_INT4_MIX64(_seed); 44 | _seed += time(NULL); 45 | } 46 | 47 | ~writer() 48 | { stop_and_join(); } 49 | 50 | uint64_t 51 | count() const 52 | { return _cnt; } 53 | 54 | private: 55 | uint64_t _cnt; 56 | uint64_t _seed; 57 | }; 58 | 59 | class reader : public thread { 60 | public: 61 | int 62 | run() 63 | { 64 | while (is_running()) { 65 | uint64_t r = _seed++; 66 | shared_map.find(RAND_INT4_MIX64(r) & MASK); 67 | ++_cnt; 68 | } 69 | return 0; 70 | } 71 | 72 | reader() 73 | : _cnt(0) 74 | { 75 | _seed = gettid(); 76 | RAND_INT4_MIX64(_seed); 77 | _seed += time(NULL); 78 | } 79 | 80 | ~reader() 81 | { stop_and_join(); } 82 | 83 | uint64_t 84 | count() const 85 | { return _cnt; } 86 | 87 | private: 88 | uint64_t _cnt; 89 | uint64_t _seed; 90 | }; 91 | 92 | int main() 93 | { 94 | reader r[R_TH_NUM]; 95 | writer w[W_TH_NUM]; 96 | 97 | for (int i = 0; i < R_TH_NUM; ++i) 98 | r[i].start(); 99 | for (int i = 0; i < W_TH_NUM; ++i) 100 | w[i].start(); 101 | 102 | ulib_timer_t timer; 103 | timer_start(&timer); 104 | 105 | sleep(WORKING_TIME); 106 | 107 | for (int i = 0; i < R_TH_NUM; ++i) 108 | r[i].stop_and_join(); 109 | for (int i = 0; i < W_TH_NUM; ++i) 110 | w[i].stop_and_join(); 111 | 112 | float elapsed = timer_stop(&timer); 113 | 114 | uint64_t r_cnt = 0; 115 | uint64_t w_cnt = 0; 116 | for (int i = 0; i < R_TH_NUM; ++i) 117 | r_cnt += r[i].count(); 118 | for (int i = 0; i < W_TH_NUM; ++i) 119 | w_cnt += w[i].count(); 120 | 121 | printf("total ops :%lu read, %lu write\n", (unsigned long)r_cnt, (unsigned long)w_cnt); 122 | printf("ns_per_read :%10lu op/s\n", (unsigned long)(r_cnt / elapsed)); 123 | printf("ns_per_write:%10lu op/s\n", (unsigned long)(w_cnt / elapsed)); 124 | 125 | printf("passed\n"); 126 | 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define SIZE (100 * 1024 * 1024 + 3) 10 | 11 | using namespace ulib; 12 | 13 | int main() 14 | { 15 | char buf[4096] = { 0 }; 16 | open_hash_set hashes; 17 | 18 | for (unsigned s = 0; s < 1000; ++s) { 19 | for (unsigned i = 0; i < sizeof(buf); ++i) { 20 | uint64_t hash = hash_fast64(buf, i, s); 21 | if (hashes.contain(hash)) { 22 | fprintf(stderr, "%016llx already exists\n", 23 | (unsigned long long) hash); 24 | exit(EXIT_FAILURE); 25 | } else 26 | hashes.insert(hash); 27 | } 28 | } 29 | 30 | uint64_t nil = 0; 31 | 32 | printf("0 fasthash64: %016llx\n", (unsigned long long)hash_fast64(&nil, sizeof(nil), 0)); 33 | printf("1 fasthash64: %016llx\n", (unsigned long long)hash_fast64(&nil, sizeof(nil), 1)); 34 | printf("0 fasthash32: %08x\n", hash_fast32(&nil, sizeof(nil), 0)); 35 | printf("1 fasthash32: %08x\n", hash_fast32(&nil, sizeof(nil), 1)); 36 | 37 | // Test vectors: 38 | // A1, A2, A3 = 12983ffe21252f81, 884fc614fc2fa70e, d9712048288274b2 39 | // B1, B2, B3 = f3e342c9aea341d9, f91881833784ba2f, 3696ebaba379d7d2 40 | // a1, a2, a3 = 0e8cef83, 73dfe0fa, 4f11546a 41 | // b1, b2, b3 = babfff10, 3e6c38ac, 6ce2ec27 42 | 43 | char *buf1 = new char [SIZE]; 44 | memset(buf1, 0x00, SIZE); 45 | assert(hash_fast64(buf1, SIZE, 0) == 0x12983ffe21252f81ul); 46 | assert(hash_ferm64(buf1, SIZE, 0) == 0xf3e342c9aea341d9ul); 47 | assert(hash_fast32(buf1, SIZE, 0) == 0x0e8cef83u); 48 | assert(hash_ferm32(buf1, SIZE, 0) == 0xbabfff10u); 49 | memset(buf1, 0x00, SIZE); 50 | assert(hash_fast64(buf1, SIZE, 0xfeedbeef) == 0x884fc614fc2fa70eul); 51 | assert(hash_ferm64(buf1, SIZE, 0xfeedbeef) == 0xf91881833784ba2ful); 52 | assert(hash_fast32(buf1, SIZE, 0xfeedbeef) == 0x73dfe0fau); 53 | assert(hash_ferm32(buf1, SIZE, 0xfeedbeef) == 0x3e6c38acu); 54 | memset(buf1, 0xA5, SIZE); 55 | assert(hash_fast64(buf1, SIZE, 0) == 0xd9712048288274b2ul); 56 | assert(hash_ferm64(buf1, SIZE, 0) == 0x3696ebaba379d7d2ul); 57 | assert(hash_fast32(buf1, SIZE, 0) == 0x4f11546au); 58 | assert(hash_ferm32(buf1, SIZE, 0) == 0x6ce2ec27u); 59 | 60 | printf("passed\n"); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_func_perf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define SIZE (100 * 1024 * 1024 + 3) 7 | 8 | #define TIME(st) ({ \ 9 | ulib_timer_t _timer; \ 10 | timer_start(&_timer); \ 11 | volatile uint64_t __r = st; \ 12 | printf(#st " elapsed: %f\n", timer_stop(&_timer)); \ 13 | __r; \ 14 | }) 15 | 16 | int main() 17 | { 18 | uint64_t A1, A2, A3; 19 | uint32_t a1, a2, a3; 20 | uint64_t B1, B2, B3; 21 | uint32_t b1, b2, b3; 22 | 23 | char *buf = new char [SIZE]; 24 | memset(buf, 0x00, SIZE); 25 | A1 = TIME(hash_fast64(buf, SIZE, 0)); 26 | B1 = TIME(hash_ferm64(buf, SIZE, 0)); 27 | a1 = TIME(hash_fast32(buf, SIZE, 0)); 28 | b1 = TIME(hash_ferm32(buf, SIZE, 0)); 29 | memset(buf, 0x00, SIZE); 30 | A2 = TIME(hash_fast64(buf, SIZE, 0xfeedbeef)); 31 | B2 = TIME(hash_ferm64(buf, SIZE, 0xfeedbeef)); 32 | a2 = TIME(hash_fast32(buf, SIZE, 0xfeedbeef)); 33 | b2 = TIME(hash_ferm32(buf, SIZE, 0xfeedbeef)); 34 | memset(buf, 0xA5, SIZE); 35 | A3 = TIME(hash_fast64(buf, SIZE, 0)); 36 | B3 = TIME(hash_ferm64(buf, SIZE, 0)); 37 | a3 = TIME(hash_fast32(buf, SIZE, 0)); 38 | b3 = TIME(hash_ferm32(buf, SIZE, 0)); 39 | 40 | printf("A1, A2, A3 = %016lx, %016lx, %016lx\n", A1, A2, A3); 41 | printf("B1, B2, B3 = %016lx, %016lx, %016lx\n", B1, B2, B3); 42 | printf("a1, a2, a3 = %08x, %08x, %08x\n", a1, a2, a3); 43 | printf("b1, b2, b3 = %08x, %08x, %08x\n", b1, b2, b3); 44 | 45 | printf("passed\n"); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_multihash_r_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace ulib; 11 | 12 | uint64_t u, v, w; 13 | #define myrand() RAND_NR_NEXT(u, v, w) 14 | 15 | const char *usage = 16 | "%s [ins] [get]\n"; 17 | 18 | volatile long counter = 0; 19 | 20 | static void sig_alarm_handler(int) 21 | { 22 | printf("%ld per sec\n", counter); 23 | counter = 0; 24 | alarm(1); 25 | } 26 | 27 | void register_sig_handler() 28 | { 29 | struct sigaction sigact; 30 | 31 | sigact.sa_handler = sig_alarm_handler; 32 | sigact.sa_flags = 0; 33 | if (sigaction(SIGALRM, &sigact, NULL)) { 34 | perror("sigaction"); 35 | exit(-1); 36 | } 37 | alarm(1); 38 | } 39 | 40 | void constant_insert(long ins, long get) 41 | { 42 | long t; 43 | 44 | multi_hash_map map(4); 45 | 46 | for (t = 0; t < ins; t++) { 47 | map[myrand()] = t; 48 | counter++; 49 | } 50 | 51 | printf("insertion done\n"); 52 | 53 | for (t = 0; t < get; t++) { 54 | map.find(myrand()); 55 | counter++; 56 | } 57 | 58 | printf("all done\n"); 59 | } 60 | 61 | int main(int argc, char *argv[]) 62 | { 63 | long ins = 1000000; 64 | long get = 50000000; 65 | uint64_t seed = time(NULL); 66 | 67 | if (argc > 1) 68 | ins = atol(argv[1]); 69 | if (argc > 2) 70 | get = atol(argv[2]); 71 | 72 | RAND_NR_INIT(u, v, w, seed); 73 | 74 | register_sig_handler(); 75 | 76 | constant_insert(ins, get); 77 | 78 | printf("passed\n"); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_open.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace ulib; 10 | 11 | struct str { 12 | const char *c_str; 13 | 14 | str(const char *s = 0) 15 | : c_str(s) { } 16 | 17 | operator size_t() const 18 | { return hash_fast64((const unsigned char *)c_str, strlen(c_str), 0); } 19 | 20 | bool operator==(const str &other) const 21 | { return strcmp(c_str, other.c_str) == 0; } 22 | }; 23 | 24 | int main() 25 | { 26 | open_hash_map months; 27 | 28 | months["january"] = 31; 29 | months["february"] = 28; 30 | months["march"] = 31; 31 | months["april"] = 30; 32 | months["may"] = 31; 33 | months["june"] = 30; 34 | months["july"] = 31; 35 | months["august"] = 31; 36 | months["september"] = 30; 37 | months["october"] = 31; 38 | months["november"] = 30; 39 | months["december"] = 31; 40 | 41 | assert(months["september"] == 30); 42 | assert(months["april"] == 30); 43 | assert(months["february"] == 28); 44 | assert(months["december"] == 31); 45 | 46 | open_hash_map map; 47 | 48 | map[1] = 2; 49 | map[2] = 1; 50 | map[3] = 3; 51 | 52 | open_hash_map copy1(map); 53 | assert(copy1[1] == 2); 54 | assert(copy1[2] == 1); 55 | assert(copy1[3] == 3); 56 | 57 | open_hash_map copy2 = map; 58 | assert(copy2[1] == 2); 59 | assert(copy2[2] == 1); 60 | assert(copy2[3] == 3); 61 | 62 | assert(copy1.size() == 3); 63 | assert(copy1.size() == copy2.size()); 64 | copy2 = copy1; 65 | assert(copy1.size() == copy2.size()); 66 | copy2 = copy2; 67 | assert(copy2[1] == 2); 68 | assert(copy2[2] == 1); 69 | assert(copy2[3] == 3); 70 | assert(copy1.size() == copy2.size()); 71 | 72 | uint64_t seed = time(NULL); 73 | uint64_t num = seed; 74 | 75 | // insert 100000 random numbers 76 | for (int i = 0; i < 100000; ++i) { 77 | RAND_XORSHIFT(num, 7, 5, 47); 78 | map[num] = -1; 79 | } 80 | // check if all random numbers can be found 81 | num = seed; 82 | for (int i = 0; i < 100000; ++i) { 83 | RAND_XORSHIFT(num, 7, 5, 47); 84 | assert(map[num] == -1); 85 | } 86 | // then remove all random numbers 87 | num = seed; 88 | for (int i = 0; i < 100000; ++i) { 89 | RAND_XORSHIFT(num, 7, 5, 47); 90 | map.erase(num); 91 | } 92 | assert(map.size() == 3); 93 | // ensure all random numbers were erased 94 | num = seed; 95 | for (int i = 0; i < 100000; ++i) { 96 | RAND_XORSHIFT(num, 7, 5, 47); 97 | assert(map[num] == 0); // default value for new elemnets is zero 98 | } 99 | 100 | printf("passed\n"); 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /ulib-svn/test/hash_open_prot_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint64_t u, v, w; 11 | #define myrand() RAND_NR_NEXT(u, v, w) 12 | 13 | const char *usage = 14 | "%s [ins] [get]\n"; 15 | 16 | volatile long counter = 0; 17 | 18 | DEFINE_OPENHASH(myhash, uint64_t, uint64_t, 1, openhash_hashfn, openhash_equalfn) 19 | 20 | static void sig_alarm_handler(int) 21 | { 22 | printf("%ld per sec\n", counter); 23 | counter = 0; 24 | alarm(1); 25 | } 26 | 27 | void register_sig_handler() 28 | { 29 | struct sigaction sigact; 30 | 31 | sigact.sa_handler = sig_alarm_handler; 32 | sigact.sa_flags = 0; 33 | if (sigaction(SIGALRM, &sigact, NULL)) { 34 | perror("sigaction"); 35 | exit(-1); 36 | } 37 | alarm(1); 38 | } 39 | 40 | void constant_insert(long ins, long get) 41 | { 42 | long t; 43 | int ret; 44 | 45 | openhash_t(myhash) *my = openhash_init(myhash); 46 | 47 | if (my == NULL) { 48 | fprintf(stderr, "alloc failed\n"); 49 | return; 50 | } 51 | 52 | for (t = 0; t < ins; t++) { 53 | oh_iter_t itr = openhash_set(myhash, my, myrand(), &ret); 54 | if (openhash_end(my) != itr) 55 | openhash_value(my, itr) = t; 56 | counter++; 57 | } 58 | 59 | printf("insertion done\n"); 60 | 61 | for (t = 0; t < get; t++) { 62 | openhash_get(myhash, my, myrand()); 63 | counter++; 64 | } 65 | 66 | openhash_destroy(myhash, my); 67 | 68 | printf("all done\n"); 69 | } 70 | 71 | int main(int argc, char *argv[]) 72 | { 73 | long ins = 1000000; 74 | long get = 50000000; 75 | uint64_t seed = time(NULL); 76 | 77 | if (argc > 1) 78 | ins = atol(argv[1]); 79 | if (argc > 2) 80 | get = atol(argv[2]); 81 | 82 | RAND_NR_INIT(u, v, w, seed); 83 | 84 | register_sig_handler(); 85 | 86 | constant_insert(ins, get); 87 | 88 | printf("passed\n"); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /ulib-svn/test/heapsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define LESSTHAN(x, y) ((x) < (y)) 6 | 7 | DEFINE_HEAPSORT(test, int, LESSTHAN); 8 | 9 | int main() 10 | { 11 | int data[] = { 0, -1, 3, 100, 8 }; 12 | 13 | heapsort_test(data, data + sizeof(data)/sizeof(data[0])); 14 | 15 | assert(data[0] == -1); 16 | assert(data[1] == 0); 17 | assert(data[2] == 3); 18 | assert(data[3] == 8); 19 | assert(data[4] == 100); 20 | 21 | printf("passed\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ulib-svn/test/hweight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | uint64_t u, v, w; 9 | 10 | void hweight64_test() 11 | { 12 | for (int i = 0; i < 100000; ++i) { 13 | uint64_t r = RAND_NR_NEXT(u, v, w); 14 | assert(__builtin_popcountll(r) == hweight64(r)); 15 | } 16 | } 17 | 18 | void hweight32_test() 19 | { 20 | for (int i = 0; i < 100000; ++i) { 21 | uint32_t r = RAND_NR_NEXT(u, v, w); 22 | assert(__builtin_popcountl(r) == hweight32(r)); 23 | } 24 | } 25 | 26 | void hweight32_hakmem_test() 27 | { 28 | for (int i = 0; i < 100000; ++i) { 29 | uint32_t r = RAND_NR_NEXT(u, v, w); 30 | assert(__builtin_popcountl(r) == hweight32_hakmem(r)); 31 | } 32 | } 33 | 34 | void hweight16_test() 35 | { 36 | for (int i = 0; i < 100000; ++i) { 37 | uint16_t r = RAND_NR_NEXT(u, v, w); 38 | assert(__builtin_popcount(r) == hweight16(r)); 39 | } 40 | } 41 | 42 | void hweight15_test() 43 | { 44 | for (int i = 0; i < 100000; ++i) { 45 | uint16_t r = RAND_NR_NEXT(u, v, w) & 0x7fff; 46 | if (r == 0x7fffu) 47 | --r; 48 | assert(__builtin_popcount(r) == hweight15(r)); 49 | } 50 | } 51 | 52 | int main() 53 | { 54 | uint64_t seed = rdtsc(); 55 | RAND_NR_INIT(u, v, w, seed); 56 | hweight64_test(); 57 | hweight32_test(); 58 | hweight32_hakmem_test(); 59 | hweight16_test(); 60 | hweight15_test(); 61 | 62 | printf("passed\n"); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /ulib-svn/test/hweight_mult.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define BIT_HAS_FAST_MULT 5 | #include 6 | #include 7 | #include 8 | 9 | uint64_t u, v, w; 10 | 11 | void hweight64_test() 12 | { 13 | for (int i = 0; i < 100000; ++i) { 14 | uint64_t r = RAND_NR_NEXT(u, v, w); 15 | assert(__builtin_popcountll(r) == hweight64(r)); 16 | } 17 | } 18 | 19 | void hweight32_test() 20 | { 21 | for (int i = 0; i < 100000; ++i) { 22 | uint32_t r = RAND_NR_NEXT(u, v, w); 23 | assert(__builtin_popcountl(r) == hweight32(r)); 24 | } 25 | } 26 | 27 | void hweight32_hakmem_test() 28 | { 29 | for (int i = 0; i < 100000; ++i) { 30 | uint32_t r = RAND_NR_NEXT(u, v, w); 31 | assert(__builtin_popcountl(r) == hweight32_hakmem(r)); 32 | } 33 | } 34 | 35 | void hweight16_test() 36 | { 37 | for (int i = 0; i < 100000; ++i) { 38 | uint16_t r = RAND_NR_NEXT(u, v, w); 39 | assert(__builtin_popcount(r) == hweight16(r)); 40 | } 41 | } 42 | 43 | void hweight15_test() 44 | { 45 | for (int i = 0; i < 100000; ++i) { 46 | uint16_t r = RAND_NR_NEXT(u, v, w) & 0x7fff; 47 | if (r == 0x7fffu) 48 | --r; 49 | assert(__builtin_popcount(r) == hweight15(r)); 50 | } 51 | } 52 | 53 | int main() 54 | { 55 | uint64_t seed = rdtsc(); 56 | RAND_NR_INIT(u, v, w, seed); 57 | hweight64_test(); 58 | hweight32_test(); 59 | hweight32_hakmem_test(); 60 | hweight16_test(); 61 | hweight15_test(); 62 | 63 | printf("passed\n"); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /ulib-svn/test/hweight_perf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define TIME(st) ({ \ 8 | ulib_timer_t _timer; \ 9 | timer_start(&_timer); \ 10 | volatile unsigned int _r = st; \ 11 | printf(#st " elapsed: %f\n", timer_stop(&_timer)); \ 12 | _r; \ 13 | }) 14 | 15 | uint64_t u, v, w; 16 | 17 | unsigned int hweight64_time() 18 | { 19 | unsigned int r = 0; 20 | 21 | for (uint64_t i = 0; i < 100000000; ++i) 22 | r += hweight64(i); 23 | return r; 24 | } 25 | 26 | unsigned int hweight32_time() 27 | { 28 | unsigned int r = 0; 29 | 30 | for (uint32_t i = 0; i < 100000000; ++i) 31 | r += hweight32(i); 32 | return r; 33 | } 34 | 35 | unsigned int popcount_time() 36 | { 37 | unsigned int r = 0; 38 | 39 | for (uint32_t i = 0; i < 100000000; ++i) 40 | r += __builtin_popcount(i); 41 | return r; 42 | } 43 | 44 | unsigned int popcountl_time() 45 | { 46 | unsigned int r = 0; 47 | 48 | for (uint64_t i = 0; i < 100000000; ++i) 49 | r += __builtin_popcountl(i); 50 | return r; 51 | } 52 | 53 | unsigned int hweight32_hakmem_time() 54 | { 55 | unsigned int r = 0; 56 | 57 | for (uint32_t i = 0; i < 100000000; ++i) 58 | r += hweight32_hakmem(i); 59 | return r; 60 | } 61 | 62 | unsigned int hweight16_time() 63 | { 64 | unsigned int r = 0; 65 | 66 | for (uint32_t i = 0; i < 100000000; ++i) 67 | r += hweight16(i); 68 | return r; 69 | } 70 | 71 | unsigned int hweight15_time() 72 | { 73 | unsigned int r = 0; 74 | 75 | for (uint32_t i = 0; i < 100000000; ++i) 76 | r += hweight15(i); 77 | return r; 78 | } 79 | 80 | int main() 81 | { 82 | TIME(hweight64_time()); 83 | TIME(hweight32_time()); 84 | TIME(hweight32_hakmem_time()); 85 | TIME(hweight16_time()); 86 | TIME(hweight15_time()); 87 | TIME(popcount_time()); 88 | TIME(popcountl_time()); 89 | printf("passed\n"); 90 | return 0; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /ulib-svn/test/hweight_perf_mult.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define BIT_HAS_FAST_MULT 6 | #include 7 | 8 | #include 9 | 10 | #define TIME(st) ({ \ 11 | ulib_timer_t _timer; \ 12 | timer_start(&_timer); \ 13 | volatile unsigned int _r = st; \ 14 | printf(#st " elapsed: %f\n", timer_stop(&_timer)); \ 15 | _r; \ 16 | }) 17 | 18 | uint64_t u, v, w; 19 | 20 | unsigned int hweight64_time() 21 | { 22 | unsigned int r = 0; 23 | 24 | for (uint64_t i = 0; i < 100000000; ++i) 25 | r += hweight64(i); 26 | return r; 27 | } 28 | 29 | unsigned int hweight32_time() 30 | { 31 | unsigned int r = 0; 32 | 33 | for (uint32_t i = 0; i < 100000000; ++i) 34 | r += hweight32(i); 35 | return r; 36 | } 37 | 38 | int main() 39 | { 40 | TIME(hweight64_time()); 41 | TIME(hweight32_time()); 42 | 43 | printf("passed\n"); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /ulib-svn/test/int_hash_perf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define TIME(st) ({ \ 6 | ulib_timer_t _timer; \ 7 | timer_start(&_timer); \ 8 | volatile uint64_t _r = st; \ 9 | printf(#st " elapsed: %f\n", timer_stop(&_timer)); \ 10 | _r; \ 11 | }) 12 | 13 | uint64_t rand_int_mix64_time() 14 | { 15 | int i = 0; 16 | uint64_t k = 1; 17 | 18 | for (i = 0; i < 100000000; ++i) 19 | RAND_INT_MIX64(k); 20 | return k; 21 | } 22 | 23 | uint64_t rand_int2_mix64_time() 24 | { 25 | int i = 0; 26 | uint64_t k = 1; 27 | 28 | for (i = 0; i < 100000000; ++i) 29 | RAND_INT2_MIX64(k); 30 | return k; 31 | } 32 | 33 | uint64_t rand_int3_mix64_time() 34 | { 35 | int i = 0; 36 | uint64_t k = 1; 37 | 38 | for (i = 0; i < 100000000; ++i) 39 | RAND_INT3_MIX64(k); 40 | return k; 41 | } 42 | 43 | uint64_t rand_int4_mix64_time() 44 | { 45 | int i = 0; 46 | uint64_t k = 1; 47 | 48 | for (i = 0; i < 100000000; ++i) 49 | RAND_INT4_MIX64(k); 50 | return k; 51 | } 52 | 53 | uint64_t fer_mix64_time() 54 | { 55 | int i = 0; 56 | uint64_t k = 1; 57 | 58 | for (i = 0; i < 100000000; ++i) 59 | FER_MIX64(k); 60 | return k; 61 | } 62 | 63 | int main() 64 | { 65 | TIME(rand_int_mix64_time()); 66 | TIME(rand_int2_mix64_time()); 67 | TIME(rand_int3_mix64_time()); 68 | TIME(rand_int4_mix64_time()); 69 | TIME(fer_mix64_time()); 70 | printf("passed\n"); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /ulib-svn/test/lcm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | assert(42 == lcm(21, 14)); 8 | assert(42 == lcm(14, 21)); 9 | 10 | printf("passed\n"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /ulib-svn/test/list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct list_node { 6 | // this member is required for list node 7 | struct list_head link; 8 | // associated data 9 | int data; 10 | }; 11 | 12 | int main() 13 | { 14 | // declare an empty list 15 | LIST_HEAD(head); 16 | struct list_node *node; 17 | int nnode = 0; 18 | 19 | // add some nodes to list 20 | for (int i = 0; i < 100; i++) { 21 | node = new list_node; 22 | node->data = i; 23 | list_add_tail(&node->link, &head); 24 | } 25 | 26 | // walk all nodes 27 | list_for_each_entry(node, &head, link) { 28 | printf("list node data %d\n", node->data); 29 | ++nnode; 30 | } 31 | 32 | assert(nnode == 100); 33 | 34 | // free the list 35 | struct list_node *tmp; 36 | nnode = 0; 37 | list_for_each_entry_safe(node, tmp, &head, link) { 38 | delete node; 39 | ++nnode; 40 | } 41 | 42 | assert(nnode == 100); 43 | 44 | printf("passed\n"); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /ulib-svn/test/listsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | struct list_node { 9 | struct list_head link; 10 | int data; 11 | }; 12 | 13 | struct list_node_forward { 14 | struct list_node_forward *next; 15 | int data; 16 | }; 17 | 18 | int comp_list_node(void *, const void *x, const void *y) 19 | { 20 | return generic_compare(((const list_node *)x)->data, ((const list_node *)y)->data); 21 | } 22 | 23 | int comp_list_node_forward(void *, const void *x, const void *y) 24 | { 25 | return generic_compare(((const list_node_forward *)x)->data, ((const list_node_forward *)y)->data); 26 | } 27 | 28 | int main() 29 | { 30 | LIST_HEAD(head); 31 | struct list_node_forward fhead, *fn = &fhead; 32 | struct list_node *node; 33 | srand((int)time(NULL)); 34 | 35 | 36 | // insert some nodes 37 | for (int i = 0; i < 10000; i++) { 38 | node = new list_node; 39 | node->data = rand(); 40 | list_add_tail(&node->link, &head); 41 | fn->next = new list_node_forward; 42 | fn->next->data = rand(); 43 | fn = fn->next; 44 | } 45 | 46 | fn->next = NULL; 47 | 48 | list_sort(NULL, &head, comp_list_node); 49 | list_sort_forward(NULL, (list_head_forward *)&fhead, comp_list_node_forward); 50 | 51 | // verify and sorted list 52 | struct list_node *tmp; 53 | list_for_each_entry_safe(node, tmp, &head, link) { 54 | if (&tmp->link != &head) 55 | assert(comp_list_node(NULL, node, tmp) <= 0); 56 | delete node; 57 | } 58 | 59 | for (fn = fhead.next; fn;) { 60 | list_node_forward *next = fn->next? fn->next: fn; 61 | assert(comp_list_node_forward(NULL, fn, next) <= 0); 62 | delete fn; 63 | if (fn == next) 64 | break; 65 | fn = next; 66 | } 67 | 68 | printf("passed\n"); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /ulib-svn/test/log.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | ULIB_DEBUG("debug log"); 6 | ULIB_WARNING("warning log"); 7 | ULIB_NOTICE("notice log"); 8 | ULIB_FATAL("fatal log"); 9 | 10 | printf("passed\n"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /ulib-svn/test/md5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | md5_ctx_t ctx; 8 | 9 | md5_init(&ctx); 10 | md5_update(&ctx, (const unsigned char*)"", 0); 11 | md5_finalize(&ctx); 12 | 13 | uint8_t *digest = MD5_DIGEST(&ctx); 14 | assert(*(uint64_t *)&digest[0] == 0x04b2008fd98c1dd4ull); 15 | assert(*(uint64_t *)&digest[8] == 0x7e42f8ec980980e9ull); 16 | 17 | md5_init(&ctx); 18 | md5_update(&ctx, (const unsigned char*)"MD5 hello world string message", 30); 19 | md5_finalize(&ctx); 20 | assert(*(uint64_t *)&digest[0] == 0x8cfe3ef35b47f0d6ull); 21 | assert(*(uint64_t *)&digest[8] == 0x4caec3658551bc8full); 22 | 23 | printf("passed\n"); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ulib-svn/test/median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define LESSTHAN(x, y) (*(x) < *(y)) 8 | 9 | DEFINE_MEDIAN(test, int, LESSTHAN); 10 | 11 | int main() 12 | { 13 | srand((int)time(0)); 14 | 15 | for (int j = 0; j < 100; ++j) { 16 | int ne = rand() % 67321 + 1; 17 | int k = rand() % ne; 18 | int m; 19 | int *data = new int [ne]; 20 | 21 | printf("number of testing numbers: %d\n", ne); 22 | printf("median: %d\n", k); 23 | 24 | for (int i = 0; i < ne; ++i) 25 | data[i] = rand(); 26 | 27 | median_test(data, data + k, data + ne); 28 | m = data[k]; 29 | 30 | // verification 31 | std::sort(data, data + ne); 32 | assert(m == data[k]); 33 | delete [] data; 34 | } 35 | 36 | printf("passed\n"); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /ulib-svn/test/normal_rng.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int i; 8 | int t = 10; 9 | struct normal_rng rng; 10 | 11 | if (argc > 1) 12 | t = atoi(argv[1]); 13 | 14 | normal_rng_init(&rng); 15 | 16 | for (i = 0; i < t; i++) 17 | printf("%f\n", normal_rng_next(&rng)); 18 | 19 | printf("passed\n"); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ulib-svn/test/rand.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | uint64_t u, v, w; 9 | uint64_t seed = time(0); 10 | 11 | RAND_NR_INIT(u, v, w, seed); 12 | 13 | for (int i = 0; i < 100; i++) 14 | printf("rand number = %llx\n", (unsigned long long)RAND_NR_NEXT(u, v, w)); 15 | 16 | uint64_t h = 0; 17 | 18 | printf("rand int mix1 = %llx\n", (unsigned long long)RAND_INT_MIX64(h)); 19 | printf("rand int mix2 = %llx\n", (unsigned long long)RAND_INT2_MIX64(h)); 20 | printf("rand int mix3 = %llx\n", (unsigned long long)RAND_INT3_MIX64(h)); 21 | printf("rand int mix4 = %llx\n", (unsigned long long)RAND_INT4_MIX64(h)); 22 | 23 | uint64_t r = RAND_NR_NEXT(u, v, w); 24 | uint64_t s = r; 25 | RAND_INT4_MIX64(s); 26 | RAND_INT4_MIX64_INV(s); 27 | 28 | if (s != r) 29 | fprintf(stderr, "expected %016llx, acutal %016llx\n", 30 | (unsigned long long)r, 31 | (unsigned long long)s); 32 | else 33 | printf("passed\n"); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /ulib-svn/test/rand_inv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | uint64_t h = 0x1234567887654321ull; 9 | RAND_INT3_MIX64(h); 10 | RAND_INT3_MIX64_INV(h); 11 | assert(h == 0x1234567887654321ull); 12 | printf("passed\n"); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /ulib-svn/test/rc4.cpp: -------------------------------------------------------------------------------- 1 | #include // put it before stdio.h for independence test 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | const unsigned char test_key1[] = 8 | "Key"; 9 | // key stream for ASCII key "Key" 10 | const unsigned char test_vec1[] = 11 | { 0xeb, 0x9f, 0x77, 0x81, 0xb7, 0x34 }; 12 | 13 | const unsigned char test_key2[] = 14 | "Wiki"; 15 | // key stream for ASCII key "Wiki" 16 | const unsigned char test_vec2[] = 17 | { 0x60, 0x44, 0xdb, 0x6d, 0x41, 0xb7 }; 18 | 19 | const unsigned char test_key3[] = 20 | "Secret"; 21 | // key stream for ASCII key "Secret" 22 | const unsigned char test_vec3[] = 23 | { 0x04, 0xd4, 0x6b, 0x05, 0x3c, 0xa8 }; 24 | 25 | int main() 26 | { 27 | rc4_ks_t key; 28 | 29 | rc4_setks(test_key1, strlen((const char *)test_key1), &key); 30 | unsigned char output1[sizeof(test_vec1)] = { 0 }; 31 | print_hex_dump_bytes("OUT\t", DUMP_PREFIX_OFFSET, output1, sizeof(output1)); 32 | rc4_crypt(output1, sizeof(output1), &key); 33 | print_hex_dump_bytes("VEC\t", DUMP_PREFIX_OFFSET, output1, sizeof(output1)); 34 | assert(memcmp(test_vec1, output1, sizeof(test_vec1)) == 0); 35 | 36 | rc4_setks(test_key2, strlen((const char *)test_key2), &key); 37 | unsigned char output2[sizeof(test_vec1)] = { 0 }; 38 | print_hex_dump_bytes("OUT\t", DUMP_PREFIX_OFFSET, output2, sizeof(output2)); 39 | rc4_crypt(output2, sizeof(output2), &key); 40 | print_hex_dump_bytes("VEC\t", DUMP_PREFIX_OFFSET, output2, sizeof(output2)); 41 | assert(memcmp(test_vec2, output2, sizeof(test_vec2)) == 0); 42 | 43 | rc4_setks(test_key3, strlen((const char *)test_key3), &key); 44 | unsigned char output3[sizeof(test_vec1)] = { 0 }; 45 | print_hex_dump_bytes("OUT\t", DUMP_PREFIX_OFFSET, output3, sizeof(output3)); 46 | rc4_crypt(output3, sizeof(output3), &key); 47 | print_hex_dump_bytes("VEC\t", DUMP_PREFIX_OFFSET, output3, sizeof(output3)); 48 | assert(memcmp(test_vec3, output3, sizeof(test_vec3)) == 0); 49 | 50 | printf("passed\n"); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /ulib-svn/test/rdtsc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | uint64_t s = rdtsc(); 8 | for (int i = 0; i < 1000; ++i) 9 | ; 10 | uint64_t t = rdtsc(); 11 | assert(s != t); 12 | 13 | printf("1000 cycle costs %lu\n", (unsigned long)(t - s)); 14 | 15 | printf("passed\n"); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /ulib-svn/test/run_all_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | FILES=`/bin/ls -l *.test 2>/dev/null` 4 | 5 | if [ -z "$FILES" ]; then 6 | echo no test to perform 7 | else 8 | echo `for i in *.test; do ./$i; done | grep passed | wc -l` success 9 | echo `/bin/ls -l *.test | wc -l` in all 10 | fi 11 | -------------------------------------------------------------------------------- /ulib-svn/test/search_line.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int comp_line(const char *line, void *param) 10 | { 11 | return strcmp(line, (const char *)param); 12 | } 13 | 14 | int main() 15 | { 16 | int fd = open("./search_line.data", O_RDONLY); 17 | 18 | assert(fd != -1); 19 | assert(findfirstline(fd, comp_line, (void *)"world", 1024) == 18); 20 | // can't find 21 | assert(findfirstline(fd, comp_line, (void *)"world hello", 1024) == -1); 22 | 23 | close(fd); 24 | 25 | printf("passed\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ulib-svn/test/search_line.data: -------------------------------------------------------------------------------- 1 | hello 2 | hello world 3 | world 4 | -------------------------------------------------------------------------------- /ulib-svn/test/set_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | uint64_t u, v, w; 14 | #define myrand() RAND_NR_NEXT(u, v, w) 15 | 16 | struct set_elem { 17 | int key; 18 | 19 | bool operator<(const set_elem &other) const; 20 | }; 21 | 22 | static inline int set_elem_cmp(const void *a, const void *b) 23 | { 24 | return generic_compare( 25 | ((struct set_elem *)a)->key, 26 | ((struct set_elem *)b)->key); 27 | } 28 | 29 | bool set_elem::operator<(const set_elem &other) const 30 | { 31 | return set_elem_cmp(this, &other) < 0; 32 | } 33 | 34 | int main(int argc, char *argv[]) 35 | { 36 | int num = 1000000; 37 | set myset; 38 | 39 | if (argc > 1) 40 | num = atoi(argv[1]); 41 | 42 | uint64_t seed = time(NULL); 43 | RAND_NR_INIT(u, v, w, seed); 44 | 45 | ulib_timer_t timer; 46 | timer_start(&timer); 47 | for (int i = 0; i < num; ++i) { 48 | set_elem t; 49 | t.key = myrand(); 50 | myset.insert(t); 51 | } 52 | printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); 53 | 54 | timer_start(&timer); 55 | for (int i = 0; i < 1000000; ++i) { 56 | set_elem t; 57 | t.key = myrand(); 58 | myset.find(t); 59 | } 60 | printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); 61 | 62 | printf("passed\n"); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /ulib-svn/test/sha1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | sha1_ctx_t ctx; 8 | 9 | sha1_init(&ctx); 10 | sha1_update(&ctx, (const unsigned char*)"SHA1 hello world string message", 31); 11 | sha1_finalize(&ctx); 12 | 13 | uint8_t *digest = SHA1_DIGEST(&ctx); 14 | 15 | assert(*(uint32_t *)&digest[0] == 0x0f38349au); 16 | assert(*(uint32_t *)&digest[4] == 0x9053606du); 17 | assert(*(uint32_t *)&digest[8] == 0xcbe578cau); 18 | assert(*(uint32_t *)&digest[12] == 0x7a8fae05u); 19 | assert(*(uint32_t *)&digest[16] == 0x109db0e1u); 20 | 21 | printf("passed\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ulib-svn/test/sha256.cpp: -------------------------------------------------------------------------------- 1 | #include // put it at the beginning for independence test 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | const unsigned char test_key1[] = 8 | "hello\n"; 9 | const unsigned char test_vec1[32] = 10 | { 0x58, 0x91, 0xb5, 0xb5, 0x22, 0xd5, 0xdf, 0x08, 0x6d, 0x0f, 0xf0, 0xb1, 0x10, 0xfb, 0xd9, 0xd2, 11 | 0x1b, 0xb4, 0xfc, 0x71, 0x63, 0xaf, 0x34, 0xd0, 0x82, 0x86, 0xa2, 0xe8, 0x46, 0xf6, 0xbe, 0x03 }; 12 | 13 | int main() 14 | { 15 | sha256_ctx_t ctx; 16 | 17 | unsigned char hash[32] = { 0 }; 18 | 19 | sha256_init(&ctx); 20 | sha256_update(&ctx, test_key1, strlen((const char *)test_key1)); 21 | sha256_finalize(&ctx, hash); 22 | 23 | print_hex_dump_bytes("SHA256KEY\t", DUMP_PREFIX_OFFSET, test_key1, strlen((const char *)test_key1)); 24 | print_hex_dump_bytes("SHA256SUM\t", DUMP_PREFIX_OFFSET, hash, sizeof(hash)); 25 | 26 | assert(memcmp(test_vec1, hash, sizeof(test_vec1)) == 0); 27 | 28 | printf("passed\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /ulib-svn/test/splay_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | uint64_t u, v, w; 12 | #define myrand() RAND_NR_NEXT(u, v, w) 13 | 14 | struct tree_node { 15 | struct tree_root link; 16 | int key; 17 | }; 18 | 19 | static inline int tree_node_cmp(const void *a, const void *b) 20 | { 21 | return generic_compare( 22 | ((struct tree_node *)a)->key, 23 | ((struct tree_node *)b)->key); 24 | } 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | int num = 1000000; 29 | tree_root *root = NULL; 30 | 31 | if (argc > 1) 32 | num = atoi(argv[1]); 33 | 34 | uint64_t seed = time(NULL); 35 | RAND_NR_INIT(u, v, w, seed); 36 | 37 | ulib_timer_t timer; 38 | timer_start(&timer); 39 | for (int i = 0; i < num; ++i) { 40 | tree_node *t = new tree_node; 41 | t->key = myrand(); 42 | if (&t->link != splay_map(&t->link, tree_node_cmp, &root)) 43 | delete t; 44 | } 45 | printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); 46 | 47 | timer_start(&timer); 48 | for (int i = 0; i < 1000000; ++i) { 49 | tree_node t; 50 | t.key = myrand(); 51 | splay_search(&t.link, tree_node_cmp, &root); 52 | } 53 | printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); 54 | 55 | tree_node *pos, *tmp; 56 | timer_start(&timer); 57 | tree_for_each_entry_safe(pos, tmp, root, link) { 58 | tree_del(&pos->link, &root); 59 | delete pos; 60 | } 61 | printf("Deleting 1M elems elapsed: %f\n", timer_stop(&timer)); 62 | 63 | 64 | printf("passed\n"); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /ulib-svn/test/splay_np_bench.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | uint64_t u, v, w; 12 | #define myrand() RAND_NR_NEXT(u, v, w) 13 | 14 | struct tree_node { 15 | struct tree_root_np link; 16 | int key; 17 | }; 18 | 19 | static inline int tree_node_cmp(const void *a, const void *b) 20 | { 21 | return generic_compare( 22 | ((struct tree_node *)a)->key, 23 | ((struct tree_node *)b)->key); 24 | } 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | int num = 1000000; 29 | tree_root_np *root = NULL; 30 | 31 | if (argc > 1) 32 | num = atoi(argv[1]); 33 | 34 | uint64_t seed = time(NULL); 35 | RAND_NR_INIT(u, v, w, seed); 36 | 37 | ulib_timer_t timer; 38 | timer_start(&timer); 39 | for (int i = 0; i < num; ++i) { 40 | tree_node *t = new tree_node; 41 | t->key = myrand(); 42 | if (&t->link != splay_map_np(&t->link, tree_node_cmp, &root)) 43 | delete t; 44 | } 45 | printf("Inserting 1M elems elapsed: %f\n", timer_stop(&timer)); 46 | 47 | timer_start(&timer); 48 | for (int i = 0; i < 1000000; ++i) { 49 | tree_node t; 50 | t.key = myrand(); 51 | splay_search_np(&t.link, tree_node_cmp, &root); 52 | } 53 | printf("Searching 10M elems elapsed: %f\n", timer_stop(&timer)); 54 | 55 | printf("passed\n"); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /ulib-svn/test/str_util.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | const char *str = 9 | " hello world string ___GARBAGE___"; 10 | char field[6]; 11 | char large[100]; 12 | assert(getfield(str, str, 0, field, 0, ' ') == str); 13 | assert(getfield(str, str + strlen(str), 0, NULL, 0, ' ') == str); 14 | assert(getfield(str, str + strlen(str), 1, NULL, sizeof(field), ' ') == str + 1); 15 | assert(getfield(str, str + strlen(str), 1, field, sizeof(field), ' ') == str + 1); 16 | assert(strcmp(field, "hello") == 0); 17 | assert(getfield(str, str + strlen(str), 2, field, sizeof(field), ' ') == str + 7); 18 | assert(field[0] == 0); 19 | assert(getfield(str, str + strlen(str), 4, field, sizeof(field), ' ') == str + 14); 20 | assert(strcmp(field, "strin") == 0); // yes, "strin" without 'g' 21 | assert(getfield(str, str + strlen(str), 5, field, sizeof(field), ' ') == str + 21); 22 | assert(getfield(str, str + strlen(str), 5, large, sizeof(large), ' ') == str + 21); 23 | assert(strcmp(large, "___GARBAGE___") == 0); 24 | assert(getfield(str, str + strlen(str), 6, field, sizeof(field), ' ') == str + strlen(str)); 25 | printf("passed\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ulib-svn/test/thread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | volatile static bool success = false; 7 | 8 | class mythread : public ulib::thread { 9 | public: 10 | int run() { 11 | for (; _once || is_running();) { 12 | _once = false; 13 | success = true; 14 | sleep(1); 15 | } 16 | return 0; 17 | } 18 | 19 | static volatile bool _once; 20 | }; 21 | 22 | volatile bool mythread::_once = true; 23 | 24 | int main() 25 | { 26 | mythread thd; 27 | 28 | assert(thd.start() == 0); 29 | assert(thd.stop_and_join() == 0); 30 | if (success) 31 | printf("passed\n"); 32 | else 33 | printf("failure\n"); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /ulib-svn/test/tree.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // This file demonstrates the use of trees declared in tree.h. The 3 | // following example takes binary search tree (struct tree_root) as an 4 | // exmaple. The use of splay tree and AVL tree are similar. 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include // generic_compare() 12 | #include 13 | 14 | struct tree_node { 15 | // this member is required for tree node structure 16 | struct tree_root link; 17 | // associated data for the node 18 | int data; 19 | }; 20 | 21 | // function comparing two tree nodes 22 | int comp_tree_node(const void *x, const void *y) 23 | { 24 | // retrive the containers of node x and y 25 | tree_node *node_x = tree_entry(x, struct tree_node, link); 26 | tree_node *node_y = tree_entry(y, struct tree_node, link); 27 | 28 | // for tree_node struct, since the tree_root member is the 29 | // first member of the structure, we may alternatively convert 30 | // the pointer x and y directly to node_x and node_y through 31 | // type cast. This method is faster. 32 | 33 | return generic_compare(node_x->data, node_y->data); 34 | } 35 | 36 | int main() 37 | { 38 | // define an empty tree 39 | struct tree_root *root = 0; 40 | struct tree_node *node; 41 | 42 | srand((int)time(NULL)); 43 | 44 | // insert several nodes 45 | for (int i = 0; i < 100;) { 46 | node = new tree_node; 47 | node->data = rand(); 48 | if (&node->link != tree_map(&node->link, comp_tree_node, &root)) { 49 | // a node with the same data already exists, no need to add 50 | delete node; 51 | } else { 52 | // new node is successfully added 53 | ++i; 54 | } 55 | } 56 | 57 | // walk all the nodes 58 | int nnode = 0; 59 | tree_for_each_entry(node, root, link) { 60 | printf("data for current node is %d\n", node->data); 61 | ++nnode; 62 | } 63 | 64 | assert(nnode == 100); 65 | 66 | // free the tree, use the 'safe' version of iteration 67 | nnode = 0; 68 | struct tree_node *tmp; 69 | tree_for_each_entry_safe(node, tmp, root, link) { 70 | // NOTE: first delete this node before freeing it 71 | tree_del(&node->link, &root); 72 | delete node; 73 | ++nnode; 74 | } 75 | 76 | assert(nnode == 100); 77 | 78 | printf("passed\n"); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /ulib-svn/test/version.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("%s\n", ulib_version()); 7 | 8 | printf("passed\n"); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /ulib-svn/test/zipf_rng.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int n = 10; 8 | int t = 10; 9 | int i; 10 | float s = 1.0; 11 | struct zipf_rng rng; 12 | 13 | if (argc > 3) { 14 | n = atoi(argv[1]); 15 | s = atof(argv[2]); 16 | t = atoi(argv[3]); 17 | } 18 | 19 | zipf_rng_init(&rng, n, s); 20 | 21 | for (i = 0; i < t; i++) 22 | printf("%d\n", zipf_rng_next(&rng)); 23 | 24 | printf("passed\n"); 25 | 26 | return 0; 27 | } 28 | --------------------------------------------------------------------------------