├── .gitignore ├── CHANGES ├── LICENSE ├── Makefile.am ├── README.md ├── build.sh ├── chaos ├── Makefile.am ├── async_method │ ├── async_method.h │ ├── async_method_base.h │ ├── async_method_bind_func.h │ ├── async_method_bind_obj.h │ └── async_method_inc.h ├── deps │ └── jemalloc │ │ ├── COPYING │ │ ├── ChangeLog │ │ ├── INSTALL │ │ ├── Makefile.in │ │ ├── README │ │ ├── autogen.sh │ │ ├── bin │ │ ├── jemalloc.sh.in │ │ └── pprof │ │ ├── config.guess │ │ ├── config.stamp.in │ │ ├── config.sub │ │ ├── configure.ac │ │ ├── doc │ │ ├── html.xsl.in │ │ ├── jemalloc.xml.in │ │ ├── manpages.xsl.in │ │ └── stylesheet.xsl │ │ ├── include │ │ ├── jemalloc │ │ │ ├── internal │ │ │ │ ├── arena.h │ │ │ │ ├── atomic.h │ │ │ │ ├── base.h │ │ │ │ ├── bitmap.h │ │ │ │ ├── chunk.h │ │ │ │ ├── chunk_dss.h │ │ │ │ ├── chunk_mmap.h │ │ │ │ ├── ckh.h │ │ │ │ ├── ctl.h │ │ │ │ ├── extent.h │ │ │ │ ├── hash.h │ │ │ │ ├── huge.h │ │ │ │ ├── jemalloc_internal.h.in │ │ │ │ ├── mb.h │ │ │ │ ├── mutex.h │ │ │ │ ├── private_namespace.h │ │ │ │ ├── prng.h │ │ │ │ ├── prof.h │ │ │ │ ├── ql.h │ │ │ │ ├── qr.h │ │ │ │ ├── quarantine.h │ │ │ │ ├── rb.h │ │ │ │ ├── rtree.h │ │ │ │ ├── size_classes.sh │ │ │ │ ├── stats.h │ │ │ │ ├── tcache.h │ │ │ │ ├── tsd.h │ │ │ │ └── util.h │ │ │ ├── jemalloc.h.in │ │ │ └── jemalloc_defs.h.in │ │ └── msvc_compat │ │ │ ├── inttypes.h │ │ │ ├── stdbool.h │ │ │ ├── stdint.h │ │ │ └── strings.h │ │ ├── install-sh │ │ ├── jemalloc.h │ │ ├── src │ │ ├── arena.c │ │ ├── atomic.c │ │ ├── base.c │ │ ├── bitmap.c │ │ ├── chunk.c │ │ ├── chunk_dss.c │ │ ├── chunk_mmap.c │ │ ├── ckh.c │ │ ├── ctl.c │ │ ├── extent.c │ │ ├── hash.c │ │ ├── huge.c │ │ ├── jemalloc.c │ │ ├── mb.c │ │ ├── mutex.c │ │ ├── prof.c │ │ ├── quarantine.c │ │ ├── rtree.c │ │ ├── stats.c │ │ ├── tcache.c │ │ ├── tsd.c │ │ ├── util.c │ │ └── zone.c │ │ └── test │ │ ├── aligned_alloc.c │ │ ├── aligned_alloc.exp │ │ ├── allocated.c │ │ ├── allocated.exp │ │ ├── allocm.c │ │ ├── allocm.exp │ │ ├── bitmap.c │ │ ├── bitmap.exp │ │ ├── jemalloc_test.h.in │ │ ├── mremap.c │ │ ├── mremap.exp │ │ ├── posix_memalign.c │ │ ├── posix_memalign.exp │ │ ├── rallocm.c │ │ ├── rallocm.exp │ │ ├── thread_arena.c │ │ ├── thread_arena.exp │ │ ├── thread_tcache_enabled.c │ │ └── thread_tcache_enabled.exp ├── heart_beat │ ├── heart_beat_element_mgr.h │ ├── heart_beat_inc.h │ ├── heart_beat_list.h │ └── heart_beat_service.h ├── log │ ├── Makefile.am │ ├── log.cpp │ ├── log.h │ ├── log_inc.h │ ├── log_misc.cpp │ └── log_misc.h ├── network │ ├── Makefile.am │ ├── acceptor_service.cpp │ ├── acceptor_service.h │ ├── active_connection.h │ ├── buffer_list.cpp │ ├── buffer_list.h │ ├── connection.cpp │ ├── connection.h │ ├── connector_service.h │ ├── default_conn_strategy.cpp │ ├── default_conn_strategy.h │ ├── msg_buffer.cpp │ ├── msg_buffer.h │ ├── network_config.h │ ├── network_inc.h │ ├── network_tool.h │ ├── serialize.h │ ├── tcp_service.cpp │ ├── tcp_service.h │ ├── work_service.cpp │ ├── work_service.h │ ├── work_service_group.cpp │ └── work_service_group.h ├── statistic │ ├── Makefile.am │ ├── statistic_inc.h │ ├── statistic_service.cpp │ └── statistic_service.h ├── task_service │ ├── Makefile.am │ ├── base_container.h │ ├── fast_msg_queue.h │ ├── io_multiplex_handler.cpp │ ├── io_multiplex_handler.h │ ├── ring_buffer.h │ ├── task_queue.h │ ├── task_service.cpp │ ├── task_service.h │ ├── task_service_define.h │ ├── task_service_group.cpp │ ├── task_service_group.h │ ├── task_service_inc.h │ ├── timer_container.h │ ├── timer_manager.cpp │ └── timer_manager.h ├── test.h ├── thread │ ├── Makefile.am │ ├── condition.h │ ├── mutex.h │ ├── rwlock.h │ ├── spinlock.h │ ├── thread.cpp │ ├── thread.h │ ├── thread_group.cpp │ ├── thread_group.h │ └── thread_inc.h └── utility │ ├── Makefile.am │ ├── arg_helper.cpp │ ├── arg_helper.h │ ├── atomic_val.h │ ├── construct.gen.cpp │ ├── construct.h │ ├── itoa.h │ ├── memory.h │ ├── misc.h │ ├── noncopyable.h │ ├── processor_helper.cpp │ ├── processor_helper.h │ ├── random.cpp │ ├── random.h │ ├── shared_ptr.h │ ├── signal_handler.cpp │ ├── signal_handler.h │ ├── singleton.h │ ├── time_util.h │ └── utility_inc.h ├── configure.ac ├── fixunix.sh ├── op_lvl.sh └── test ├── Makefile.am ├── arg_helper ├── Makefile.am ├── chaos.cpp └── test_arg_helper.h ├── async_method ├── Makefile.am ├── chaos.cpp └── test_async_method.h ├── echo_server ├── Makefile.am ├── chaos.cpp └── test_tcp_server.h ├── log ├── Makefile.am ├── chaos.cpp └── test_log.h ├── misc_def.h ├── misc_impl.cpp ├── msg_buffer ├── Makefile.am ├── chaos.cpp └── test_msg_buffer.h ├── perf ├── Makefile.am ├── chaos.cpp └── test_perf.h ├── press_client ├── Makefile.am ├── chaos.cpp ├── test_pressclient.cpp └── test_pressclient.h ├── task_service ├── Makefile.am ├── chaos.cpp └── test_task_service.h ├── thread ├── Makefile.am ├── chaos.cpp └── test_thread.h ├── throughput_client ├── Makefile.am ├── chaos.cpp └── test_tpclient.h ├── timer ├── Makefile.am ├── chaos.cpp └── test_timer.h └── utility ├── Makefile.am ├── chaos.cpp └── test_shared_ptr.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | 9 | # Compiled Static libraries 10 | *.lai 11 | *.la 12 | *.a 13 | 14 | # yunjie add 15 | *.swp 16 | *.in 17 | *.m4 18 | Makefile 19 | conf.h 20 | *.guess 21 | *.log 22 | *.status 23 | *.sub 24 | configure 25 | depcomp 26 | install-sh 27 | libtool 28 | ltmain.sh 29 | missing 30 | stamp-h1 31 | *.d 32 | .deps 33 | *.cache 34 | *.files 35 | *.out 36 | tags 37 | vigen.sh 38 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | 2 | Chaos 0.223-beta 2012.6.21 3 | *) 首发 4 | 5 | 6 | 7 | Chaos 0.24-beta 2012.7.15 8 | *) 增加统计组件 - statistic 9 | 10 | *) 增加connector_service 11 | 12 | *) 修复task_service_group_t::stop时由于内部 13 | service数据互相投递导致的崩溃问题 14 | 15 | *) 增加各模块的trace日志 16 | 17 | *) 增加network_config对socket选项的配置 18 | 19 | *) 修复各模块静态库打印不了warn级别以下日志 20 | 的问题 21 | 22 | 23 | 24 | Chaos 0.24-8-gba14387-beta 2012.7.29 25 | *) 优化了解析数据包进行拼接时的内存分配 26 | *) task_service主循环中增加异常捕获 27 | 28 | 29 | Chaos 0.3.25-beta 2012.8.6 30 | *) 解决了对高版本gcc/g++的兼容性问题 31 | 32 | 33 | Chaos 0.4.25-beta 2012.8.16 34 | *) 增加pipe,socketpair,eventfd三种线程通信 35 | 方式(编译期使用宏指定),默认使用pipe 36 | 37 | *) 完善chaos::task_service_t与boost::io_service 38 | 测试用例 39 | 40 | 41 | Chaos 0.4.26-beta 2012.8.22 42 | *) 修复post时日志造成递归溢出的错误,并加上提示注释 43 | *) 增加op_lvl脚本批量更改g++编译优化选项 44 | 45 | 46 | Chaos 0.5.26-beta 2012.8.26 47 | *) 预编译时加入内核版本信息,可自动根据内核版本选择 48 | 最优IPC方式 49 | 50 | *) 修改include文件方式,从附加目录中或系统目录中按层 51 | 次include头文件,而不是当前相对路径 52 | 53 | *) 重构automake目录结构,支持make install 54 | 55 | 56 | Chaos 0.5.30-beta 2012.9.10 57 | *) aysnc_method实现宏模板化 58 | 59 | *) 将bind_func, bind_memfunc改为全局模板函数 60 | 61 | *) 优化bind_func, bind_memfunc函数的参数传递 62 | 从两次拷贝减少为一次, 代价是如果参数是functor 63 | 则必须显示加上&符号 64 | 65 | *) connection增加userdata 66 | 67 | *) connection成员私有化, 部分成员函数也私有化 68 | 69 | *) 增加default_conn_strategy.cpp文件 70 | 71 | *) tcp_service, connector_service支持broadcast功能 72 | 73 | *) 提高connection_t数据的安全性 74 | 75 | *) 增加misc_impl.cpp 76 | 77 | *) press_client多线程处理 78 | 79 | *) 解决关闭时work_service提前delete造成的崩溃现象 80 | 81 | *) 修复test_pressclient checksum的bug 82 | 83 | 84 | Chaos 0.6.30-beta 2012.11.4 85 | *) 增加序列化类 86 | *) 修复发送缓冲崩溃的BUG 87 | *) 修复在一个io_callback中无法再次正确注册io event的bug 88 | *) 修复某些边界下on_write_complete没有被正确调用的BUG 89 | *) 解决pressclient发送的消息包过大导致栈溢出 90 | *) 统一bind_func和bind_memfunc为bindfunc 91 | *) bindfunc支持不同参数个数的函数重载 92 | *) 引入jemalloc 93 | *) 优化msg_buffer 94 | *) test目录增加Makefile层级 95 | *) build.sh模块编译 96 | *) 优化对send时EINTR情况的处理 97 | *) 增加基于jemalloc的chaos_new/chaos_delete 98 | *) test_pressclient增加对不同函数重载的测试 99 | *) 生成的静态库区分为包含jemalloc二进制与不包含的 100 | *) 增加基于jemalloc的construct/destroy 101 | *) 增加用于生成construct.h的construct.gen.cpp文件 102 | *) async_method内存分配使用construct/destroy 103 | *) connection的分配使用construct/destroy 104 | *) 加入共享指针类 105 | *) 使用非原子引用技术的shared ptr来包装connection,timer event保证数据安全性 106 | *) msg_buffer的拷贝策略进行修改, 255以下使用数据拷贝的方式, 255及255以上使用引用计数防止拷贝 107 | *) async_method使用值拷贝 108 | *) 修复connector_service在多线程并发下,m_service_index会被连续++导致溢出崩溃的问题 109 | 110 | 111 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012, Yunjie Lu. All rights reserved. 2 | // https://github.com/lyjdamzwf/chaos 3 | // 4 | // Redistribution and use in source and binary forms, with or without 5 | // modification, are permitted provided that the following conditions 6 | // are met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // * Redistributions in binary form must reproduce the above copyright 11 | // notice, this list of conditions and the following disclaimer in the 12 | // documentation and/or other materials provided with the distribution. 13 | // * The name of the author may not be used to endorse or promote 14 | // products derived from this software without specific prior written 15 | // permission. 16 | // 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS= chaos test 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Yunjie Lu. All rights reserved. 2 | 3 | Use of this source code is governed by a BSD-style 4 | license that can be found in the License file. 5 | 6 | 7 | chaos是一个采用c++开发, reactor模式的网络事件库, 是我个人对libevent, asio两款优秀的网络库进行理解之后, 基于个人的想法所开发的. 8 | 9 | 更多chaos库的资料请关注以下: 10 | github - https://github.com/lyjdamzwf/chaos 11 | 新浪weibo - http://weibo.com/crazyprogramerlyj 12 | 博客 - http://www.cppthinker.com 13 | 14 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | def_dir=/usr/local 2 | build_part="all" 3 | 4 | if [ -e "chaos/deps/jemalloc/lib/libjemalloc.a" ]; then 5 | build_part="chaos" 6 | fi 7 | 8 | if [ ! -z $1 ]; then 9 | build_part=$1 10 | fi 11 | 12 | if [ ! -z $2 ]; then 13 | def_dir=$2 14 | fi 15 | 16 | echo "install directory:"$def_dir 17 | 18 | if [ "$build_part" = "all" ]; then 19 | cd chaos/deps/jemalloc 20 | sh autogen.sh --with-jemalloc-prefix=je_ 21 | make 22 | cd ../../../ 23 | fi 24 | 25 | if [ "$build_part" = "all" ] || [ "$build_part" = "reconf" ]; then 26 | aclocal 27 | libtoolize 28 | autoconf 29 | autoheader 30 | automake --add-missing 31 | ./configure --prefix=$def_dir 32 | fi 33 | 34 | if [ "$build_part" = "all" ] || [ "$build_part" = "reconf" ] || [ "$build_part" = "chaos" ]; then 35 | make -C chaos 36 | cp ./chaos/utility/libchaos_utility.a ./chaos/utility/libchaos_utility_je.a 37 | cp ./chaos/libchaos.a ./chaos/libchaos_je.a 38 | ar -q ./chaos/utility/libchaos_utility_je.a chaos/deps/jemalloc/src/*.o 39 | ar -q ./chaos/libchaos_je.a chaos/deps/jemalloc/src/*.o 40 | fi 41 | 42 | 43 | if [ "$build_part" = "all" ] || [ "$build_part" = "reconf" ] || [ "$build_part" = "chaos" ] || [ "$build_part" = "test" ]; then 44 | make -C test 45 | fi 46 | -------------------------------------------------------------------------------- /chaos/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS= thread utility task_service log network statistic 2 | 3 | CXXFLAGS = -Wall -g -O2 -fPIC 4 | 5 | lib_LIBRARIES = libchaos.a 6 | 7 | ## 安装根目录 8 | chaosdir=$(prefix)/chaos-@chaos_ver@ 9 | 10 | ## 安装库目录 11 | libdir=$(chaosdir)/lib 12 | 13 | ## 安装头文件目录 14 | includedir=$(chaosdir)/include/chaos 15 | async_methoddir=$(includedir)/async_method 16 | heart_beatdir=$(includedir)/heart_beat 17 | logdir=$(includedir)/log 18 | networkdir=$(includedir)/network 19 | statisticdir=$(includedir)/statistic 20 | task_servicedir=$(includedir)/task_service 21 | threaddir=$(includedir)/thread 22 | utilitydir=$(includedir)/utility 23 | 24 | include_HEADERS= *.h 25 | async_method_HEADERS=async_method/*.h 26 | heart_beat_HEADERS=heart_beat/*.h 27 | log_HEADERS=log/*.h 28 | network_HEADERS=network/*.h 29 | statistic_HEADERS=statistic/*.h 30 | task_service_HEADERS=task_service/*.h 31 | thread_HEADERS=thread/*.h 32 | utility_HEADERS=utility/*.h 33 | 34 | libchaos_a_LIBFLAGS = 35 | libchaos_a_LIBADD = 36 | 37 | libchaos_a_SOURCES = \ 38 | log/log.cpp \ 39 | log/log_misc.cpp \ 40 | network/acceptor_service.cpp \ 41 | network/buffer_list.cpp \ 42 | network/connection.cpp \ 43 | network/msg_buffer.cpp \ 44 | network/tcp_service.cpp \ 45 | network/work_service.cpp \ 46 | network/work_service_group.cpp \ 47 | network/default_conn_strategy.cpp \ 48 | statistic/statistic_service.cpp \ 49 | task_service/io_multiplex_handler.cpp \ 50 | task_service/task_service.cpp \ 51 | task_service/task_service_group.cpp \ 52 | task_service/timer_manager.cpp \ 53 | thread/thread.cpp \ 54 | thread/thread_group.cpp \ 55 | utility/arg_helper.cpp \ 56 | utility/processor_helper.cpp \ 57 | utility/random.cpp \ 58 | utility/signal_handler.cpp 59 | 60 | -------------------------------------------------------------------------------- /chaos/async_method/async_method_bind_func.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_ASYNC_METHOD_BIND_FUNC_H_ 10 | #define _CHAOS_ASYNC_METHOD_BIND_FUNC_H_ 11 | 12 | /*! 13 | * @file async_method_bind_func.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.17 18 | * @brief bind class of static function 19 | * @last changed 2012.3.24 by yunjie 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace async_method 31 | { 32 | 33 | using namespace chaos::utility; 34 | 35 | #define BIND_FUNC_IMPL(num) \ 36 | template \ 37 | class async_method_bind_func_##num##_t : public async_method_base_t \ 38 | { \ 39 | public: \ 40 | async_method_bind_func_##num##_t(F f_ BIND_CTOR_ARG_##num) \ 41 | : m_func(f_) BIND_CTOR_LIST_##num \ 42 | {} \ 43 | void exec() \ 44 | { \ 45 | if (m_func) \ 46 | { \ 47 | (*m_func)(BIND_INVOKE_LIST_##num); \ 48 | } \ 49 | } \ 50 | async_method_base_t* clone() \ 51 | { \ 52 | return chaos::utility::construct(m_func COMMA_##num BIND_INVOKE_LIST_##num); \ 53 | } \ 54 | private: \ 55 | F m_func; \ 56 | BIND_MEMBER_LIST_##num \ 57 | }; 58 | 59 | BIND_FUNC_IMPL(0) 60 | BIND_FUNC_IMPL(1) 61 | BIND_FUNC_IMPL(2) 62 | BIND_FUNC_IMPL(3) 63 | BIND_FUNC_IMPL(4) 64 | BIND_FUNC_IMPL(5) 65 | BIND_FUNC_IMPL(6) 66 | BIND_FUNC_IMPL(7) 67 | BIND_FUNC_IMPL(8) 68 | BIND_FUNC_IMPL(9) 69 | 70 | } 71 | 72 | } 73 | 74 | #endif //! _CHAOS_ASYNC_METHOD_BIND_FUNC_H_ 75 | 76 | -------------------------------------------------------------------------------- /chaos/async_method/async_method_bind_obj.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_ASYNC_METHOD_BIND_OBJ_H_ 10 | #define _CHAOS_ASYNC_METHOD_BIND_OBJ_H_ 11 | 12 | /*! 13 | * @file async_method_obj.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.17 18 | * @brief bind class of obj 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace async_method 31 | { 32 | 33 | using namespace chaos::utility; 34 | 35 | #define BIND_OBJ_IMPL(num) \ 36 | template \ 37 | class async_method_bind_obj_##num##_t : public async_method_base_t \ 38 | { \ 39 | public: \ 40 | async_method_bind_obj_##num##_t(CLS_TYPE* obj_, F f_ BIND_CTOR_ARG_##num) \ 41 | : m_obj_ptr(obj_), m_func(f_) BIND_CTOR_LIST_##num \ 42 | {} \ 43 | void exec() \ 44 | { \ 45 | if (m_obj_ptr && m_func) \ 46 | { \ 47 | (m_obj_ptr->*m_func)(BIND_INVOKE_LIST_##num); \ 48 | } \ 49 | } \ 50 | async_method_base_t* clone() \ 51 | { \ 52 | return chaos::utility::construct(m_obj_ptr, m_func COMMA_##num BIND_INVOKE_LIST_##num); \ 53 | } \ 54 | private: \ 55 | CLS_TYPE* m_obj_ptr; \ 56 | F m_func; \ 57 | BIND_MEMBER_LIST_##num \ 58 | }; 59 | 60 | BIND_OBJ_IMPL(0); 61 | BIND_OBJ_IMPL(1); 62 | BIND_OBJ_IMPL(2); 63 | BIND_OBJ_IMPL(3); 64 | BIND_OBJ_IMPL(4); 65 | BIND_OBJ_IMPL(5); 66 | BIND_OBJ_IMPL(6); 67 | BIND_OBJ_IMPL(7); 68 | BIND_OBJ_IMPL(8); 69 | BIND_OBJ_IMPL(9); 70 | 71 | } 72 | 73 | } 74 | 75 | #endif //! _CHAOS_ASYNC_METHOD_BIND_OBJ_H_ 76 | -------------------------------------------------------------------------------- /chaos/async_method/async_method_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_ASYNC_METHOD_INC_H_ 10 | #define _CHAOS_ASYNC_METHOD_INC_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | #endif //! _CHAOS_ASYNC_METHOD_INC_H_ 18 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/COPYING: -------------------------------------------------------------------------------- 1 | Unless otherwise specified, files in the jemalloc source distribution are 2 | subject to the following license: 3 | -------------------------------------------------------------------------------- 4 | Copyright (C) 2002-2012 Jason Evans . 5 | All rights reserved. 6 | Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved. 7 | Copyright (C) 2009-2012 Facebook, Inc. All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions are met: 11 | 1. Redistributions of source code must retain the above copyright notice(s), 12 | this list of conditions and the following disclaimer. 13 | 2. Redistributions in binary form must reproduce the above copyright notice(s), 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS 18 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20 | EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- 28 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/README: -------------------------------------------------------------------------------- 1 | jemalloc is a general-purpose scalable concurrent malloc(3) implementation. 2 | This distribution is a "portable" implementation that currently targets 3 | FreeBSD, Linux, Apple OS X, and MinGW. jemalloc is included as the default 4 | allocator in the FreeBSD and NetBSD operating systems, and it is used by the 5 | Mozilla Firefox web browser on Microsoft Windows-related platforms. Depending 6 | on your needs, one of the other divergent versions may suit your needs better 7 | than this distribution. 8 | 9 | The COPYING file contains copyright and licensing information. 10 | 11 | The INSTALL file contains information on how to configure, build, and install 12 | jemalloc. 13 | 14 | The ChangeLog file contains a brief summary of changes for each release. 15 | 16 | URL: http://www.canonware.com/jemalloc/ 17 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | for i in autoconf; do 4 | echo "$i" 5 | $i 6 | if [ $? -ne 0 ]; then 7 | echo "Error $? in $i" 8 | exit 1 9 | fi 10 | done 11 | 12 | echo "./configure --enable-autogen $@" 13 | ./configure --enable-autogen $@ 14 | if [ $? -ne 0 ]; then 15 | echo "Error $? in ./configure" 16 | exit 1 17 | fi 18 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/bin/jemalloc.sh.in: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | prefix=@prefix@ 4 | exec_prefix=@exec_prefix@ 5 | libdir=@libdir@ 6 | 7 | @LD_PRELOAD_VAR@=${libdir}/libjemalloc.@SOREV@ 8 | export @LD_PRELOAD_VAR@ 9 | exec "$@" 10 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/config.stamp.in: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyjdamzwf/chaos/95dff372002c4a04ee45b62568d46a4f50d793e4/chaos/deps/jemalloc/config.stamp.in -------------------------------------------------------------------------------- /chaos/deps/jemalloc/doc/html.xsl.in: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/doc/manpages.xsl.in: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/doc/stylesheet.xsl: -------------------------------------------------------------------------------- 1 | 2 | ansi 3 | 4 | 5 | "" 6 | 7 | 8 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/base.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | void *base_alloc(size_t size); 13 | void *base_calloc(size_t number, size_t size); 14 | extent_node_t *base_node_alloc(void); 15 | void base_node_dealloc(extent_node_t *node); 16 | bool base_boot(void); 17 | void base_prefork(void); 18 | void base_postfork_parent(void); 19 | void base_postfork_child(void); 20 | 21 | #endif /* JEMALLOC_H_EXTERNS */ 22 | /******************************************************************************/ 23 | #ifdef JEMALLOC_H_INLINES 24 | 25 | #endif /* JEMALLOC_H_INLINES */ 26 | /******************************************************************************/ 27 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/chunk.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | /* 5 | * Size and alignment of memory chunks that are allocated by the OS's virtual 6 | * memory system. 7 | */ 8 | #define LG_CHUNK_DEFAULT 22 9 | 10 | /* Return the chunk address for allocation address a. */ 11 | #define CHUNK_ADDR2BASE(a) \ 12 | ((void *)((uintptr_t)(a) & ~chunksize_mask)) 13 | 14 | /* Return the chunk offset of address a. */ 15 | #define CHUNK_ADDR2OFFSET(a) \ 16 | ((size_t)((uintptr_t)(a) & chunksize_mask)) 17 | 18 | /* Return the smallest chunk multiple that is >= s. */ 19 | #define CHUNK_CEILING(s) \ 20 | (((s) + chunksize_mask) & ~chunksize_mask) 21 | 22 | #endif /* JEMALLOC_H_TYPES */ 23 | /******************************************************************************/ 24 | #ifdef JEMALLOC_H_STRUCTS 25 | 26 | #endif /* JEMALLOC_H_STRUCTS */ 27 | /******************************************************************************/ 28 | #ifdef JEMALLOC_H_EXTERNS 29 | 30 | extern size_t opt_lg_chunk; 31 | 32 | /* Protects stats_chunks; currently not used for any other purpose. */ 33 | extern malloc_mutex_t chunks_mtx; 34 | /* Chunk statistics. */ 35 | extern chunk_stats_t stats_chunks; 36 | 37 | extern rtree_t *chunks_rtree; 38 | 39 | extern size_t chunksize; 40 | extern size_t chunksize_mask; /* (chunksize - 1). */ 41 | extern size_t chunk_npages; 42 | extern size_t map_bias; /* Number of arena chunk header pages. */ 43 | extern size_t arena_maxclass; /* Max size class for arenas. */ 44 | 45 | void *chunk_alloc(size_t size, size_t alignment, bool base, bool *zero); 46 | void chunk_dealloc(void *chunk, size_t size, bool unmap); 47 | bool chunk_boot(void); 48 | 49 | #endif /* JEMALLOC_H_EXTERNS */ 50 | /******************************************************************************/ 51 | #ifdef JEMALLOC_H_INLINES 52 | 53 | #endif /* JEMALLOC_H_INLINES */ 54 | /******************************************************************************/ 55 | 56 | #include "jemalloc/internal/chunk_dss.h" 57 | #include "jemalloc/internal/chunk_mmap.h" 58 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/chunk_dss.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | void *chunk_alloc_dss(size_t size, size_t alignment, bool *zero); 13 | bool chunk_in_dss(void *chunk); 14 | bool chunk_dss_boot(void); 15 | void chunk_dss_prefork(void); 16 | void chunk_dss_postfork_parent(void); 17 | void chunk_dss_postfork_child(void); 18 | 19 | #endif /* JEMALLOC_H_EXTERNS */ 20 | /******************************************************************************/ 21 | #ifdef JEMALLOC_H_INLINES 22 | 23 | #endif /* JEMALLOC_H_INLINES */ 24 | /******************************************************************************/ 25 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/chunk_mmap.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | void pages_purge(void *addr, size_t length); 13 | 14 | void *chunk_alloc_mmap(size_t size, size_t alignment, bool *zero); 15 | bool chunk_dealloc_mmap(void *chunk, size_t size); 16 | 17 | #endif /* JEMALLOC_H_EXTERNS */ 18 | /******************************************************************************/ 19 | #ifdef JEMALLOC_H_INLINES 20 | 21 | #endif /* JEMALLOC_H_INLINES */ 22 | /******************************************************************************/ 23 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/ckh.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct ckh_s ckh_t; 5 | typedef struct ckhc_s ckhc_t; 6 | 7 | /* Typedefs to allow easy function pointer passing. */ 8 | typedef void ckh_hash_t (const void *, unsigned, size_t *, size_t *); 9 | typedef bool ckh_keycomp_t (const void *, const void *); 10 | 11 | /* Maintain counters used to get an idea of performance. */ 12 | /* #define CKH_COUNT */ 13 | /* Print counter values in ckh_delete() (requires CKH_COUNT). */ 14 | /* #define CKH_VERBOSE */ 15 | 16 | /* 17 | * There are 2^LG_CKH_BUCKET_CELLS cells in each hash table bucket. Try to fit 18 | * one bucket per L1 cache line. 19 | */ 20 | #define LG_CKH_BUCKET_CELLS (LG_CACHELINE - LG_SIZEOF_PTR - 1) 21 | 22 | #endif /* JEMALLOC_H_TYPES */ 23 | /******************************************************************************/ 24 | #ifdef JEMALLOC_H_STRUCTS 25 | 26 | /* Hash table cell. */ 27 | struct ckhc_s { 28 | const void *key; 29 | const void *data; 30 | }; 31 | 32 | struct ckh_s { 33 | #ifdef CKH_COUNT 34 | /* Counters used to get an idea of performance. */ 35 | uint64_t ngrows; 36 | uint64_t nshrinks; 37 | uint64_t nshrinkfails; 38 | uint64_t ninserts; 39 | uint64_t nrelocs; 40 | #endif 41 | 42 | /* Used for pseudo-random number generation. */ 43 | #define CKH_A 1103515241 44 | #define CKH_C 12347 45 | uint32_t prng_state; 46 | 47 | /* Total number of items. */ 48 | size_t count; 49 | 50 | /* 51 | * Minimum and current number of hash table buckets. There are 52 | * 2^LG_CKH_BUCKET_CELLS cells per bucket. 53 | */ 54 | unsigned lg_minbuckets; 55 | unsigned lg_curbuckets; 56 | 57 | /* Hash and comparison functions. */ 58 | ckh_hash_t *hash; 59 | ckh_keycomp_t *keycomp; 60 | 61 | /* Hash table with 2^lg_curbuckets buckets. */ 62 | ckhc_t *tab; 63 | }; 64 | 65 | #endif /* JEMALLOC_H_STRUCTS */ 66 | /******************************************************************************/ 67 | #ifdef JEMALLOC_H_EXTERNS 68 | 69 | bool ckh_new(ckh_t *ckh, size_t minitems, ckh_hash_t *hash, 70 | ckh_keycomp_t *keycomp); 71 | void ckh_delete(ckh_t *ckh); 72 | size_t ckh_count(ckh_t *ckh); 73 | bool ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data); 74 | bool ckh_insert(ckh_t *ckh, const void *key, const void *data); 75 | bool ckh_remove(ckh_t *ckh, const void *searchkey, void **key, 76 | void **data); 77 | bool ckh_search(ckh_t *ckh, const void *seachkey, void **key, void **data); 78 | void ckh_string_hash(const void *key, unsigned minbits, size_t *hash1, 79 | size_t *hash2); 80 | bool ckh_string_keycomp(const void *k1, const void *k2); 81 | void ckh_pointer_hash(const void *key, unsigned minbits, size_t *hash1, 82 | size_t *hash2); 83 | bool ckh_pointer_keycomp(const void *k1, const void *k2); 84 | 85 | #endif /* JEMALLOC_H_EXTERNS */ 86 | /******************************************************************************/ 87 | #ifdef JEMALLOC_H_INLINES 88 | 89 | #endif /* JEMALLOC_H_INLINES */ 90 | /******************************************************************************/ 91 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/ctl.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct ctl_node_s ctl_node_t; 5 | typedef struct ctl_named_node_s ctl_named_node_t; 6 | typedef struct ctl_indexed_node_s ctl_indexed_node_t; 7 | typedef struct ctl_arena_stats_s ctl_arena_stats_t; 8 | typedef struct ctl_stats_s ctl_stats_t; 9 | 10 | #endif /* JEMALLOC_H_TYPES */ 11 | /******************************************************************************/ 12 | #ifdef JEMALLOC_H_STRUCTS 13 | 14 | struct ctl_node_s { 15 | bool named; 16 | }; 17 | 18 | struct ctl_named_node_s { 19 | struct ctl_node_s node; 20 | const char *name; 21 | /* If (nchildren == 0), this is a terminal node. */ 22 | unsigned nchildren; 23 | const ctl_node_t *children; 24 | int (*ctl)(const size_t *, size_t, void *, size_t *, 25 | void *, size_t); 26 | }; 27 | 28 | struct ctl_indexed_node_s { 29 | struct ctl_node_s node; 30 | const ctl_named_node_t *(*index)(const size_t *, size_t, size_t); 31 | }; 32 | 33 | struct ctl_arena_stats_s { 34 | bool initialized; 35 | unsigned nthreads; 36 | size_t pactive; 37 | size_t pdirty; 38 | arena_stats_t astats; 39 | 40 | /* Aggregate stats for small size classes, based on bin stats. */ 41 | size_t allocated_small; 42 | uint64_t nmalloc_small; 43 | uint64_t ndalloc_small; 44 | uint64_t nrequests_small; 45 | 46 | malloc_bin_stats_t bstats[NBINS]; 47 | malloc_large_stats_t *lstats; /* nlclasses elements. */ 48 | }; 49 | 50 | struct ctl_stats_s { 51 | size_t allocated; 52 | size_t active; 53 | size_t mapped; 54 | struct { 55 | size_t current; /* stats_chunks.curchunks */ 56 | uint64_t total; /* stats_chunks.nchunks */ 57 | size_t high; /* stats_chunks.highchunks */ 58 | } chunks; 59 | struct { 60 | size_t allocated; /* huge_allocated */ 61 | uint64_t nmalloc; /* huge_nmalloc */ 62 | uint64_t ndalloc; /* huge_ndalloc */ 63 | } huge; 64 | ctl_arena_stats_t *arenas; /* (narenas + 1) elements. */ 65 | }; 66 | 67 | #endif /* JEMALLOC_H_STRUCTS */ 68 | /******************************************************************************/ 69 | #ifdef JEMALLOC_H_EXTERNS 70 | 71 | int ctl_byname(const char *name, void *oldp, size_t *oldlenp, void *newp, 72 | size_t newlen); 73 | int ctl_nametomib(const char *name, size_t *mibp, size_t *miblenp); 74 | 75 | int ctl_bymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, 76 | void *newp, size_t newlen); 77 | bool ctl_boot(void); 78 | 79 | #define xmallctl(name, oldp, oldlenp, newp, newlen) do { \ 80 | if (je_mallctl(name, oldp, oldlenp, newp, newlen) \ 81 | != 0) { \ 82 | malloc_printf( \ 83 | ": Failure in xmallctl(\"%s\", ...)\n", \ 84 | name); \ 85 | abort(); \ 86 | } \ 87 | } while (0) 88 | 89 | #define xmallctlnametomib(name, mibp, miblenp) do { \ 90 | if (je_mallctlnametomib(name, mibp, miblenp) != 0) { \ 91 | malloc_printf(": Failure in " \ 92 | "xmallctlnametomib(\"%s\", ...)\n", name); \ 93 | abort(); \ 94 | } \ 95 | } while (0) 96 | 97 | #define xmallctlbymib(mib, miblen, oldp, oldlenp, newp, newlen) do { \ 98 | if (je_mallctlbymib(mib, miblen, oldp, oldlenp, newp, \ 99 | newlen) != 0) { \ 100 | malloc_write( \ 101 | ": Failure in xmallctlbymib()\n"); \ 102 | abort(); \ 103 | } \ 104 | } while (0) 105 | 106 | #endif /* JEMALLOC_H_EXTERNS */ 107 | /******************************************************************************/ 108 | #ifdef JEMALLOC_H_INLINES 109 | 110 | #endif /* JEMALLOC_H_INLINES */ 111 | /******************************************************************************/ 112 | 113 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/extent.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct extent_node_s extent_node_t; 5 | 6 | #endif /* JEMALLOC_H_TYPES */ 7 | /******************************************************************************/ 8 | #ifdef JEMALLOC_H_STRUCTS 9 | 10 | /* Tree of extents. */ 11 | struct extent_node_s { 12 | /* Linkage for the size/address-ordered tree. */ 13 | rb_node(extent_node_t) link_szad; 14 | 15 | /* Linkage for the address-ordered tree. */ 16 | rb_node(extent_node_t) link_ad; 17 | 18 | /* Profile counters, used for huge objects. */ 19 | prof_ctx_t *prof_ctx; 20 | 21 | /* Pointer to the extent that this tree node is responsible for. */ 22 | void *addr; 23 | 24 | /* Total region size. */ 25 | size_t size; 26 | }; 27 | typedef rb_tree(extent_node_t) extent_tree_t; 28 | 29 | #endif /* JEMALLOC_H_STRUCTS */ 30 | /******************************************************************************/ 31 | #ifdef JEMALLOC_H_EXTERNS 32 | 33 | rb_proto(, extent_tree_szad_, extent_tree_t, extent_node_t) 34 | 35 | rb_proto(, extent_tree_ad_, extent_tree_t, extent_node_t) 36 | 37 | #endif /* JEMALLOC_H_EXTERNS */ 38 | /******************************************************************************/ 39 | #ifdef JEMALLOC_H_INLINES 40 | 41 | #endif /* JEMALLOC_H_INLINES */ 42 | /******************************************************************************/ 43 | 44 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/hash.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | #endif /* JEMALLOC_H_EXTERNS */ 13 | /******************************************************************************/ 14 | #ifdef JEMALLOC_H_INLINES 15 | 16 | #ifndef JEMALLOC_ENABLE_INLINE 17 | uint64_t hash(const void *key, size_t len, uint64_t seed); 18 | #endif 19 | 20 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_HASH_C_)) 21 | /* 22 | * The following hash function is based on MurmurHash64A(), placed into the 23 | * public domain by Austin Appleby. See http://murmurhash.googlepages.com/ for 24 | * details. 25 | */ 26 | JEMALLOC_INLINE uint64_t 27 | hash(const void *key, size_t len, uint64_t seed) 28 | { 29 | const uint64_t m = UINT64_C(0xc6a4a7935bd1e995); 30 | const int r = 47; 31 | uint64_t h = seed ^ (len * m); 32 | const uint64_t *data = (const uint64_t *)key; 33 | const uint64_t *end = data + (len/8); 34 | const unsigned char *data2; 35 | 36 | assert(((uintptr_t)key & 0x7) == 0); 37 | 38 | while(data != end) { 39 | uint64_t k = *data++; 40 | 41 | k *= m; 42 | k ^= k >> r; 43 | k *= m; 44 | 45 | h ^= k; 46 | h *= m; 47 | } 48 | 49 | data2 = (const unsigned char *)data; 50 | switch(len & 7) { 51 | case 7: h ^= ((uint64_t)(data2[6])) << 48; 52 | case 6: h ^= ((uint64_t)(data2[5])) << 40; 53 | case 5: h ^= ((uint64_t)(data2[4])) << 32; 54 | case 4: h ^= ((uint64_t)(data2[3])) << 24; 55 | case 3: h ^= ((uint64_t)(data2[2])) << 16; 56 | case 2: h ^= ((uint64_t)(data2[1])) << 8; 57 | case 1: h ^= ((uint64_t)(data2[0])); 58 | h *= m; 59 | } 60 | 61 | h ^= h >> r; 62 | h *= m; 63 | h ^= h >> r; 64 | 65 | return (h); 66 | } 67 | #endif 68 | 69 | #endif /* JEMALLOC_H_INLINES */ 70 | /******************************************************************************/ 71 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/huge.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | /* Huge allocation statistics. */ 13 | extern uint64_t huge_nmalloc; 14 | extern uint64_t huge_ndalloc; 15 | extern size_t huge_allocated; 16 | 17 | /* Protects chunk-related data structures. */ 18 | extern malloc_mutex_t huge_mtx; 19 | 20 | void *huge_malloc(size_t size, bool zero); 21 | void *huge_palloc(size_t size, size_t alignment, bool zero); 22 | void *huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, 23 | size_t extra); 24 | void *huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra, 25 | size_t alignment, bool zero); 26 | void huge_dalloc(void *ptr, bool unmap); 27 | size_t huge_salloc(const void *ptr); 28 | prof_ctx_t *huge_prof_ctx_get(const void *ptr); 29 | void huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx); 30 | bool huge_boot(void); 31 | void huge_prefork(void); 32 | void huge_postfork_parent(void); 33 | void huge_postfork_child(void); 34 | 35 | #endif /* JEMALLOC_H_EXTERNS */ 36 | /******************************************************************************/ 37 | #ifdef JEMALLOC_H_INLINES 38 | 39 | #endif /* JEMALLOC_H_INLINES */ 40 | /******************************************************************************/ 41 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/mb.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | #endif /* JEMALLOC_H_TYPES */ 5 | /******************************************************************************/ 6 | #ifdef JEMALLOC_H_STRUCTS 7 | 8 | #endif /* JEMALLOC_H_STRUCTS */ 9 | /******************************************************************************/ 10 | #ifdef JEMALLOC_H_EXTERNS 11 | 12 | #endif /* JEMALLOC_H_EXTERNS */ 13 | /******************************************************************************/ 14 | #ifdef JEMALLOC_H_INLINES 15 | 16 | #ifndef JEMALLOC_ENABLE_INLINE 17 | void mb_write(void); 18 | #endif 19 | 20 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MB_C_)) 21 | #ifdef __i386__ 22 | /* 23 | * According to the Intel Architecture Software Developer's Manual, current 24 | * processors execute instructions in order from the perspective of other 25 | * processors in a multiprocessor system, but 1) Intel reserves the right to 26 | * change that, and 2) the compiler's optimizer could re-order instructions if 27 | * there weren't some form of barrier. Therefore, even if running on an 28 | * architecture that does not need memory barriers (everything through at least 29 | * i686), an "optimizer barrier" is necessary. 30 | */ 31 | JEMALLOC_INLINE void 32 | mb_write(void) 33 | { 34 | 35 | # if 0 36 | /* This is a true memory barrier. */ 37 | asm volatile ("pusha;" 38 | "xor %%eax,%%eax;" 39 | "cpuid;" 40 | "popa;" 41 | : /* Outputs. */ 42 | : /* Inputs. */ 43 | : "memory" /* Clobbers. */ 44 | ); 45 | #else 46 | /* 47 | * This is hopefully enough to keep the compiler from reordering 48 | * instructions around this one. 49 | */ 50 | asm volatile ("nop;" 51 | : /* Outputs. */ 52 | : /* Inputs. */ 53 | : "memory" /* Clobbers. */ 54 | ); 55 | #endif 56 | } 57 | #elif (defined(__amd64__) || defined(__x86_64__)) 58 | JEMALLOC_INLINE void 59 | mb_write(void) 60 | { 61 | 62 | asm volatile ("sfence" 63 | : /* Outputs. */ 64 | : /* Inputs. */ 65 | : "memory" /* Clobbers. */ 66 | ); 67 | } 68 | #elif defined(__powerpc__) 69 | JEMALLOC_INLINE void 70 | mb_write(void) 71 | { 72 | 73 | asm volatile ("eieio" 74 | : /* Outputs. */ 75 | : /* Inputs. */ 76 | : "memory" /* Clobbers. */ 77 | ); 78 | } 79 | #elif defined(__sparc64__) 80 | JEMALLOC_INLINE void 81 | mb_write(void) 82 | { 83 | 84 | asm volatile ("membar #StoreStore" 85 | : /* Outputs. */ 86 | : /* Inputs. */ 87 | : "memory" /* Clobbers. */ 88 | ); 89 | } 90 | #elif defined(__tile__) 91 | JEMALLOC_INLINE void 92 | mb_write(void) 93 | { 94 | 95 | __sync_synchronize(); 96 | } 97 | #else 98 | /* 99 | * This is much slower than a simple memory barrier, but the semantics of mutex 100 | * unlock make this work. 101 | */ 102 | JEMALLOC_INLINE void 103 | mb_write(void) 104 | { 105 | malloc_mutex_t mtx; 106 | 107 | malloc_mutex_init(&mtx); 108 | malloc_mutex_lock(&mtx); 109 | malloc_mutex_unlock(&mtx); 110 | } 111 | #endif 112 | #endif 113 | 114 | #endif /* JEMALLOC_H_INLINES */ 115 | /******************************************************************************/ 116 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/mutex.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | typedef struct malloc_mutex_s malloc_mutex_t; 5 | 6 | #ifdef _WIN32 7 | # define MALLOC_MUTEX_INITIALIZER 8 | #elif (defined(JEMALLOC_OSSPIN)) 9 | # define MALLOC_MUTEX_INITIALIZER {0} 10 | #elif (defined(JEMALLOC_MUTEX_INIT_CB)) 11 | # define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, NULL} 12 | #else 13 | # if (defined(PTHREAD_MUTEX_ADAPTIVE_NP) && \ 14 | defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)) 15 | # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP 16 | # define MALLOC_MUTEX_INITIALIZER {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP} 17 | # else 18 | # define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT 19 | # define MALLOC_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER} 20 | # endif 21 | #endif 22 | 23 | #endif /* JEMALLOC_H_TYPES */ 24 | /******************************************************************************/ 25 | #ifdef JEMALLOC_H_STRUCTS 26 | 27 | struct malloc_mutex_s { 28 | #ifdef _WIN32 29 | CRITICAL_SECTION lock; 30 | #elif (defined(JEMALLOC_OSSPIN)) 31 | OSSpinLock lock; 32 | #elif (defined(JEMALLOC_MUTEX_INIT_CB)) 33 | pthread_mutex_t lock; 34 | malloc_mutex_t *postponed_next; 35 | #else 36 | pthread_mutex_t lock; 37 | #endif 38 | }; 39 | 40 | #endif /* JEMALLOC_H_STRUCTS */ 41 | /******************************************************************************/ 42 | #ifdef JEMALLOC_H_EXTERNS 43 | 44 | #ifdef JEMALLOC_LAZY_LOCK 45 | extern bool isthreaded; 46 | #else 47 | # undef isthreaded /* Undo private_namespace.h definition. */ 48 | # define isthreaded true 49 | #endif 50 | 51 | bool malloc_mutex_init(malloc_mutex_t *mutex); 52 | void malloc_mutex_prefork(malloc_mutex_t *mutex); 53 | void malloc_mutex_postfork_parent(malloc_mutex_t *mutex); 54 | void malloc_mutex_postfork_child(malloc_mutex_t *mutex); 55 | bool mutex_boot(void); 56 | 57 | #endif /* JEMALLOC_H_EXTERNS */ 58 | /******************************************************************************/ 59 | #ifdef JEMALLOC_H_INLINES 60 | 61 | #ifndef JEMALLOC_ENABLE_INLINE 62 | void malloc_mutex_lock(malloc_mutex_t *mutex); 63 | void malloc_mutex_unlock(malloc_mutex_t *mutex); 64 | #endif 65 | 66 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_)) 67 | JEMALLOC_INLINE void 68 | malloc_mutex_lock(malloc_mutex_t *mutex) 69 | { 70 | 71 | if (isthreaded) { 72 | #ifdef _WIN32 73 | EnterCriticalSection(&mutex->lock); 74 | #elif (defined(JEMALLOC_OSSPIN)) 75 | OSSpinLockLock(&mutex->lock); 76 | #else 77 | pthread_mutex_lock(&mutex->lock); 78 | #endif 79 | } 80 | } 81 | 82 | JEMALLOC_INLINE void 83 | malloc_mutex_unlock(malloc_mutex_t *mutex) 84 | { 85 | 86 | if (isthreaded) { 87 | #ifdef _WIN32 88 | LeaveCriticalSection(&mutex->lock); 89 | #elif (defined(JEMALLOC_OSSPIN)) 90 | OSSpinLockUnlock(&mutex->lock); 91 | #else 92 | pthread_mutex_unlock(&mutex->lock); 93 | #endif 94 | } 95 | } 96 | #endif 97 | 98 | #endif /* JEMALLOC_H_INLINES */ 99 | /******************************************************************************/ 100 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/prng.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | /* 5 | * Simple linear congruential pseudo-random number generator: 6 | * 7 | * prng(y) = (a*x + c) % m 8 | * 9 | * where the following constants ensure maximal period: 10 | * 11 | * a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4. 12 | * c == Odd number (relatively prime to 2^n). 13 | * m == 2^32 14 | * 15 | * See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints. 16 | * 17 | * This choice of m has the disadvantage that the quality of the bits is 18 | * proportional to bit position. For example. the lowest bit has a cycle of 2, 19 | * the next has a cycle of 4, etc. For this reason, we prefer to use the upper 20 | * bits. 21 | * 22 | * Macro parameters: 23 | * uint32_t r : Result. 24 | * unsigned lg_range : (0..32], number of least significant bits to return. 25 | * uint32_t state : Seed value. 26 | * const uint32_t a, c : See above discussion. 27 | */ 28 | #define prng32(r, lg_range, state, a, c) do { \ 29 | assert(lg_range > 0); \ 30 | assert(lg_range <= 32); \ 31 | \ 32 | r = (state * (a)) + (c); \ 33 | state = r; \ 34 | r >>= (32 - lg_range); \ 35 | } while (false) 36 | 37 | /* Same as prng32(), but 64 bits of pseudo-randomness, using uint64_t. */ 38 | #define prng64(r, lg_range, state, a, c) do { \ 39 | assert(lg_range > 0); \ 40 | assert(lg_range <= 64); \ 41 | \ 42 | r = (state * (a)) + (c); \ 43 | state = r; \ 44 | r >>= (64 - lg_range); \ 45 | } while (false) 46 | 47 | #endif /* JEMALLOC_H_TYPES */ 48 | /******************************************************************************/ 49 | #ifdef JEMALLOC_H_STRUCTS 50 | 51 | #endif /* JEMALLOC_H_STRUCTS */ 52 | /******************************************************************************/ 53 | #ifdef JEMALLOC_H_EXTERNS 54 | 55 | #endif /* JEMALLOC_H_EXTERNS */ 56 | /******************************************************************************/ 57 | #ifdef JEMALLOC_H_INLINES 58 | 59 | #endif /* JEMALLOC_H_INLINES */ 60 | /******************************************************************************/ 61 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/ql.h: -------------------------------------------------------------------------------- 1 | /* 2 | * List definitions. 3 | */ 4 | #define ql_head(a_type) \ 5 | struct { \ 6 | a_type *qlh_first; \ 7 | } 8 | 9 | #define ql_head_initializer(a_head) {NULL} 10 | 11 | #define ql_elm(a_type) qr(a_type) 12 | 13 | /* List functions. */ 14 | #define ql_new(a_head) do { \ 15 | (a_head)->qlh_first = NULL; \ 16 | } while (0) 17 | 18 | #define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field) 19 | 20 | #define ql_first(a_head) ((a_head)->qlh_first) 21 | 22 | #define ql_last(a_head, a_field) \ 23 | ((ql_first(a_head) != NULL) \ 24 | ? qr_prev(ql_first(a_head), a_field) : NULL) 25 | 26 | #define ql_next(a_head, a_elm, a_field) \ 27 | ((ql_last(a_head, a_field) != (a_elm)) \ 28 | ? qr_next((a_elm), a_field) : NULL) 29 | 30 | #define ql_prev(a_head, a_elm, a_field) \ 31 | ((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field) \ 32 | : NULL) 33 | 34 | #define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do { \ 35 | qr_before_insert((a_qlelm), (a_elm), a_field); \ 36 | if (ql_first(a_head) == (a_qlelm)) { \ 37 | ql_first(a_head) = (a_elm); \ 38 | } \ 39 | } while (0) 40 | 41 | #define ql_after_insert(a_qlelm, a_elm, a_field) \ 42 | qr_after_insert((a_qlelm), (a_elm), a_field) 43 | 44 | #define ql_head_insert(a_head, a_elm, a_field) do { \ 45 | if (ql_first(a_head) != NULL) { \ 46 | qr_before_insert(ql_first(a_head), (a_elm), a_field); \ 47 | } \ 48 | ql_first(a_head) = (a_elm); \ 49 | } while (0) 50 | 51 | #define ql_tail_insert(a_head, a_elm, a_field) do { \ 52 | if (ql_first(a_head) != NULL) { \ 53 | qr_before_insert(ql_first(a_head), (a_elm), a_field); \ 54 | } \ 55 | ql_first(a_head) = qr_next((a_elm), a_field); \ 56 | } while (0) 57 | 58 | #define ql_remove(a_head, a_elm, a_field) do { \ 59 | if (ql_first(a_head) == (a_elm)) { \ 60 | ql_first(a_head) = qr_next(ql_first(a_head), a_field); \ 61 | } \ 62 | if (ql_first(a_head) != (a_elm)) { \ 63 | qr_remove((a_elm), a_field); \ 64 | } else { \ 65 | ql_first(a_head) = NULL; \ 66 | } \ 67 | } while (0) 68 | 69 | #define ql_head_remove(a_head, a_type, a_field) do { \ 70 | a_type *t = ql_first(a_head); \ 71 | ql_remove((a_head), t, a_field); \ 72 | } while (0) 73 | 74 | #define ql_tail_remove(a_head, a_type, a_field) do { \ 75 | a_type *t = ql_last(a_head, a_field); \ 76 | ql_remove((a_head), t, a_field); \ 77 | } while (0) 78 | 79 | #define ql_foreach(a_var, a_head, a_field) \ 80 | qr_foreach((a_var), ql_first(a_head), a_field) 81 | 82 | #define ql_reverse_foreach(a_var, a_head, a_field) \ 83 | qr_reverse_foreach((a_var), ql_first(a_head), a_field) 84 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/qr.h: -------------------------------------------------------------------------------- 1 | /* Ring definitions. */ 2 | #define qr(a_type) \ 3 | struct { \ 4 | a_type *qre_next; \ 5 | a_type *qre_prev; \ 6 | } 7 | 8 | /* Ring functions. */ 9 | #define qr_new(a_qr, a_field) do { \ 10 | (a_qr)->a_field.qre_next = (a_qr); \ 11 | (a_qr)->a_field.qre_prev = (a_qr); \ 12 | } while (0) 13 | 14 | #define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next) 15 | 16 | #define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev) 17 | 18 | #define qr_before_insert(a_qrelm, a_qr, a_field) do { \ 19 | (a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev; \ 20 | (a_qr)->a_field.qre_next = (a_qrelm); \ 21 | (a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr); \ 22 | (a_qrelm)->a_field.qre_prev = (a_qr); \ 23 | } while (0) 24 | 25 | #define qr_after_insert(a_qrelm, a_qr, a_field) \ 26 | do \ 27 | { \ 28 | (a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next; \ 29 | (a_qr)->a_field.qre_prev = (a_qrelm); \ 30 | (a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr); \ 31 | (a_qrelm)->a_field.qre_next = (a_qr); \ 32 | } while (0) 33 | 34 | #define qr_meld(a_qr_a, a_qr_b, a_field) do { \ 35 | void *t; \ 36 | (a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b); \ 37 | (a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a); \ 38 | t = (a_qr_a)->a_field.qre_prev; \ 39 | (a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev; \ 40 | (a_qr_b)->a_field.qre_prev = t; \ 41 | } while (0) 42 | 43 | /* qr_meld() and qr_split() are functionally equivalent, so there's no need to 44 | * have two copies of the code. */ 45 | #define qr_split(a_qr_a, a_qr_b, a_field) \ 46 | qr_meld((a_qr_a), (a_qr_b), a_field) 47 | 48 | #define qr_remove(a_qr, a_field) do { \ 49 | (a_qr)->a_field.qre_prev->a_field.qre_next \ 50 | = (a_qr)->a_field.qre_next; \ 51 | (a_qr)->a_field.qre_next->a_field.qre_prev \ 52 | = (a_qr)->a_field.qre_prev; \ 53 | (a_qr)->a_field.qre_next = (a_qr); \ 54 | (a_qr)->a_field.qre_prev = (a_qr); \ 55 | } while (0) 56 | 57 | #define qr_foreach(var, a_qr, a_field) \ 58 | for ((var) = (a_qr); \ 59 | (var) != NULL; \ 60 | (var) = (((var)->a_field.qre_next != (a_qr)) \ 61 | ? (var)->a_field.qre_next : NULL)) 62 | 63 | #define qr_reverse_foreach(var, a_qr, a_field) \ 64 | for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL; \ 65 | (var) != NULL; \ 66 | (var) = (((var) != (a_qr)) \ 67 | ? (var)->a_field.qre_prev : NULL)) 68 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/quarantine.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | #ifdef JEMALLOC_H_TYPES 3 | 4 | /* Default per thread quarantine size if valgrind is enabled. */ 5 | #define JEMALLOC_VALGRIND_QUARANTINE_DEFAULT (ZU(1) << 24) 6 | 7 | #endif /* JEMALLOC_H_TYPES */ 8 | /******************************************************************************/ 9 | #ifdef JEMALLOC_H_STRUCTS 10 | 11 | #endif /* JEMALLOC_H_STRUCTS */ 12 | /******************************************************************************/ 13 | #ifdef JEMALLOC_H_EXTERNS 14 | 15 | void quarantine(void *ptr); 16 | bool quarantine_boot(void); 17 | 18 | #endif /* JEMALLOC_H_EXTERNS */ 19 | /******************************************************************************/ 20 | #ifdef JEMALLOC_H_INLINES 21 | 22 | #endif /* JEMALLOC_H_INLINES */ 23 | /******************************************************************************/ 24 | 25 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/jemalloc/internal/size_classes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # The following limits are chosen such that they cover all supported platforms. 4 | 5 | # Range of quanta. 6 | lg_qmin=3 7 | lg_qmax=4 8 | 9 | # The range of tiny size classes is [2^lg_tmin..2^(lg_q-1)]. 10 | lg_tmin=3 11 | 12 | # Range of page sizes. 13 | lg_pmin=12 14 | lg_pmax=16 15 | 16 | pow2() { 17 | e=$1 18 | pow2_result=1 19 | while [ ${e} -gt 0 ] ; do 20 | pow2_result=$((${pow2_result} + ${pow2_result})) 21 | e=$((${e} - 1)) 22 | done 23 | } 24 | 25 | cat < 255) 102 | # error "Too many small size classes" 103 | #endif 104 | 105 | #endif /* JEMALLOC_H_TYPES */ 106 | /******************************************************************************/ 107 | #ifdef JEMALLOC_H_STRUCTS 108 | 109 | 110 | #endif /* JEMALLOC_H_STRUCTS */ 111 | /******************************************************************************/ 112 | #ifdef JEMALLOC_H_EXTERNS 113 | 114 | 115 | #endif /* JEMALLOC_H_EXTERNS */ 116 | /******************************************************************************/ 117 | #ifdef JEMALLOC_H_INLINES 118 | 119 | 120 | #endif /* JEMALLOC_H_INLINES */ 121 | /******************************************************************************/ 122 | EOF 123 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/msvc_compat/stdbool.h: -------------------------------------------------------------------------------- 1 | #ifndef stdbool_h 2 | #define stdbool_h 3 | 4 | #include 5 | 6 | /* MSVC doesn't define _Bool or bool in C, but does have BOOL */ 7 | /* Note this doesn't pass autoconf's test because (bool) 0.5 != true */ 8 | typedef BOOL _Bool; 9 | 10 | #define bool _Bool 11 | #define true 1 12 | #define false 0 13 | 14 | #define __bool_true_false_are_defined 1 15 | 16 | #endif /* stdbool_h */ 17 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/include/msvc_compat/strings.h: -------------------------------------------------------------------------------- 1 | #ifndef strings_h 2 | #define strings_h 3 | 4 | /* MSVC doesn't define ffs/ffsl. This dummy strings.h header is provided 5 | * for both */ 6 | #include 7 | #pragma intrinsic(_BitScanForward) 8 | static __forceinline int ffsl(long x) 9 | { 10 | unsigned long i; 11 | 12 | if (_BitScanForward(&i, x)) 13 | return (i + 1); 14 | return (0); 15 | } 16 | 17 | static __forceinline int ffs(int x) 18 | { 19 | 20 | return (ffsl(x)); 21 | } 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/jemalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_JEMALLOC_H_ 2 | #define _CHAOS_JEMALLOC_H_ 3 | 4 | #include 5 | 6 | 7 | #endif //! _CHAOS_JEMALLOC_H_ 8 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/atomic.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_ATOMIC_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/base.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_BASE_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | /* Data. */ 6 | 7 | static malloc_mutex_t base_mtx; 8 | 9 | /* 10 | * Current pages that are being used for internal memory allocations. These 11 | * pages are carved up in cacheline-size quanta, so that there is no chance of 12 | * false cache line sharing. 13 | */ 14 | static void *base_pages; 15 | static void *base_next_addr; 16 | static void *base_past_addr; /* Addr immediately past base_pages. */ 17 | static extent_node_t *base_nodes; 18 | 19 | /******************************************************************************/ 20 | /* Function prototypes for non-inline static functions. */ 21 | 22 | static bool base_pages_alloc(size_t minsize); 23 | 24 | /******************************************************************************/ 25 | 26 | static bool 27 | base_pages_alloc(size_t minsize) 28 | { 29 | size_t csize; 30 | bool zero; 31 | 32 | assert(minsize != 0); 33 | csize = CHUNK_CEILING(minsize); 34 | zero = false; 35 | base_pages = chunk_alloc(csize, chunksize, true, &zero); 36 | if (base_pages == NULL) 37 | return (true); 38 | base_next_addr = base_pages; 39 | base_past_addr = (void *)((uintptr_t)base_pages + csize); 40 | 41 | return (false); 42 | } 43 | 44 | void * 45 | base_alloc(size_t size) 46 | { 47 | void *ret; 48 | size_t csize; 49 | 50 | /* Round size up to nearest multiple of the cacheline size. */ 51 | csize = CACHELINE_CEILING(size); 52 | 53 | malloc_mutex_lock(&base_mtx); 54 | /* Make sure there's enough space for the allocation. */ 55 | if ((uintptr_t)base_next_addr + csize > (uintptr_t)base_past_addr) { 56 | if (base_pages_alloc(csize)) { 57 | malloc_mutex_unlock(&base_mtx); 58 | return (NULL); 59 | } 60 | } 61 | /* Allocate. */ 62 | ret = base_next_addr; 63 | base_next_addr = (void *)((uintptr_t)base_next_addr + csize); 64 | malloc_mutex_unlock(&base_mtx); 65 | 66 | return (ret); 67 | } 68 | 69 | void * 70 | base_calloc(size_t number, size_t size) 71 | { 72 | void *ret = base_alloc(number * size); 73 | 74 | if (ret != NULL) 75 | memset(ret, 0, number * size); 76 | 77 | return (ret); 78 | } 79 | 80 | extent_node_t * 81 | base_node_alloc(void) 82 | { 83 | extent_node_t *ret; 84 | 85 | malloc_mutex_lock(&base_mtx); 86 | if (base_nodes != NULL) { 87 | ret = base_nodes; 88 | base_nodes = *(extent_node_t **)ret; 89 | malloc_mutex_unlock(&base_mtx); 90 | } else { 91 | malloc_mutex_unlock(&base_mtx); 92 | ret = (extent_node_t *)base_alloc(sizeof(extent_node_t)); 93 | } 94 | 95 | return (ret); 96 | } 97 | 98 | void 99 | base_node_dealloc(extent_node_t *node) 100 | { 101 | 102 | malloc_mutex_lock(&base_mtx); 103 | *(extent_node_t **)node = base_nodes; 104 | base_nodes = node; 105 | malloc_mutex_unlock(&base_mtx); 106 | } 107 | 108 | bool 109 | base_boot(void) 110 | { 111 | 112 | base_nodes = NULL; 113 | if (malloc_mutex_init(&base_mtx)) 114 | return (true); 115 | 116 | return (false); 117 | } 118 | 119 | void 120 | base_prefork(void) 121 | { 122 | 123 | malloc_mutex_prefork(&base_mtx); 124 | } 125 | 126 | void 127 | base_postfork_parent(void) 128 | { 129 | 130 | malloc_mutex_postfork_parent(&base_mtx); 131 | } 132 | 133 | void 134 | base_postfork_child(void) 135 | { 136 | 137 | malloc_mutex_postfork_child(&base_mtx); 138 | } 139 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/bitmap.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_BITMAP_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | /* Function prototypes for non-inline static functions. */ 6 | 7 | static size_t bits2groups(size_t nbits); 8 | 9 | /******************************************************************************/ 10 | 11 | static size_t 12 | bits2groups(size_t nbits) 13 | { 14 | 15 | return ((nbits >> LG_BITMAP_GROUP_NBITS) + 16 | !!(nbits & BITMAP_GROUP_NBITS_MASK)); 17 | } 18 | 19 | void 20 | bitmap_info_init(bitmap_info_t *binfo, size_t nbits) 21 | { 22 | unsigned i; 23 | size_t group_count; 24 | 25 | assert(nbits > 0); 26 | assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS)); 27 | 28 | /* 29 | * Compute the number of groups necessary to store nbits bits, and 30 | * progressively work upward through the levels until reaching a level 31 | * that requires only one group. 32 | */ 33 | binfo->levels[0].group_offset = 0; 34 | group_count = bits2groups(nbits); 35 | for (i = 1; group_count > 1; i++) { 36 | assert(i < BITMAP_MAX_LEVELS); 37 | binfo->levels[i].group_offset = binfo->levels[i-1].group_offset 38 | + group_count; 39 | group_count = bits2groups(group_count); 40 | } 41 | binfo->levels[i].group_offset = binfo->levels[i-1].group_offset 42 | + group_count; 43 | binfo->nlevels = i; 44 | binfo->nbits = nbits; 45 | } 46 | 47 | size_t 48 | bitmap_info_ngroups(const bitmap_info_t *binfo) 49 | { 50 | 51 | return (binfo->levels[binfo->nlevels].group_offset << LG_SIZEOF_BITMAP); 52 | } 53 | 54 | size_t 55 | bitmap_size(size_t nbits) 56 | { 57 | bitmap_info_t binfo; 58 | 59 | bitmap_info_init(&binfo, nbits); 60 | return (bitmap_info_ngroups(&binfo)); 61 | } 62 | 63 | void 64 | bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo) 65 | { 66 | size_t extra; 67 | unsigned i; 68 | 69 | /* 70 | * Bits are actually inverted with regard to the external bitmap 71 | * interface, so the bitmap starts out with all 1 bits, except for 72 | * trailing unused bits (if any). Note that each group uses bit 0 to 73 | * correspond to the first logical bit in the group, so extra bits 74 | * are the most significant bits of the last group. 75 | */ 76 | memset(bitmap, 0xffU, binfo->levels[binfo->nlevels].group_offset << 77 | LG_SIZEOF_BITMAP); 78 | extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK)) 79 | & BITMAP_GROUP_NBITS_MASK; 80 | if (extra != 0) 81 | bitmap[binfo->levels[1].group_offset - 1] >>= extra; 82 | for (i = 1; i < binfo->nlevels; i++) { 83 | size_t group_count = binfo->levels[i].group_offset - 84 | binfo->levels[i-1].group_offset; 85 | extra = (BITMAP_GROUP_NBITS - (group_count & 86 | BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK; 87 | if (extra != 0) 88 | bitmap[binfo->levels[i+1].group_offset - 1] >>= extra; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/extent.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_EXTENT_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | 6 | static inline int 7 | extent_szad_comp(extent_node_t *a, extent_node_t *b) 8 | { 9 | int ret; 10 | size_t a_size = a->size; 11 | size_t b_size = b->size; 12 | 13 | ret = (a_size > b_size) - (a_size < b_size); 14 | if (ret == 0) { 15 | uintptr_t a_addr = (uintptr_t)a->addr; 16 | uintptr_t b_addr = (uintptr_t)b->addr; 17 | 18 | ret = (a_addr > b_addr) - (a_addr < b_addr); 19 | } 20 | 21 | return (ret); 22 | } 23 | 24 | /* Generate red-black tree functions. */ 25 | rb_gen(, extent_tree_szad_, extent_tree_t, extent_node_t, link_szad, 26 | extent_szad_comp) 27 | 28 | static inline int 29 | extent_ad_comp(extent_node_t *a, extent_node_t *b) 30 | { 31 | uintptr_t a_addr = (uintptr_t)a->addr; 32 | uintptr_t b_addr = (uintptr_t)b->addr; 33 | 34 | return ((a_addr > b_addr) - (a_addr < b_addr)); 35 | } 36 | 37 | /* Generate red-black tree functions. */ 38 | rb_gen(, extent_tree_ad_, extent_tree_t, extent_node_t, link_ad, 39 | extent_ad_comp) 40 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/hash.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_HASH_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/mb.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MB_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/mutex.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MUTEX_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | #if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32) 5 | #include 6 | #endif 7 | 8 | #ifndef _CRT_SPINCOUNT 9 | #define _CRT_SPINCOUNT 4000 10 | #endif 11 | 12 | /******************************************************************************/ 13 | /* Data. */ 14 | 15 | #ifdef JEMALLOC_LAZY_LOCK 16 | bool isthreaded = false; 17 | #endif 18 | #ifdef JEMALLOC_MUTEX_INIT_CB 19 | static bool postpone_init = true; 20 | static malloc_mutex_t *postponed_mutexes = NULL; 21 | #endif 22 | 23 | #if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32) 24 | static void pthread_create_once(void); 25 | #endif 26 | 27 | /******************************************************************************/ 28 | /* 29 | * We intercept pthread_create() calls in order to toggle isthreaded if the 30 | * process goes multi-threaded. 31 | */ 32 | 33 | #if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32) 34 | static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *, 35 | void *(*)(void *), void *__restrict); 36 | 37 | static void 38 | pthread_create_once(void) 39 | { 40 | 41 | pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create"); 42 | if (pthread_create_fptr == NULL) { 43 | malloc_write(": Error in dlsym(RTLD_NEXT, " 44 | "\"pthread_create\")\n"); 45 | abort(); 46 | } 47 | 48 | isthreaded = true; 49 | } 50 | 51 | JEMALLOC_EXPORT int 52 | pthread_create(pthread_t *__restrict thread, 53 | const pthread_attr_t *__restrict attr, void *(*start_routine)(void *), 54 | void *__restrict arg) 55 | { 56 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; 57 | 58 | pthread_once(&once_control, pthread_create_once); 59 | 60 | return (pthread_create_fptr(thread, attr, start_routine, arg)); 61 | } 62 | #endif 63 | 64 | /******************************************************************************/ 65 | 66 | #ifdef JEMALLOC_MUTEX_INIT_CB 67 | int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, 68 | void *(calloc_cb)(size_t, size_t)); 69 | #endif 70 | 71 | bool 72 | malloc_mutex_init(malloc_mutex_t *mutex) 73 | { 74 | 75 | #ifdef _WIN32 76 | if (!InitializeCriticalSectionAndSpinCount(&mutex->lock, 77 | _CRT_SPINCOUNT)) 78 | return (true); 79 | #elif (defined(JEMALLOC_OSSPIN)) 80 | mutex->lock = 0; 81 | #elif (defined(JEMALLOC_MUTEX_INIT_CB)) 82 | if (postpone_init) { 83 | mutex->postponed_next = postponed_mutexes; 84 | postponed_mutexes = mutex; 85 | } else { 86 | if (_pthread_mutex_init_calloc_cb(&mutex->lock, base_calloc) != 87 | 0) 88 | return (true); 89 | } 90 | #else 91 | pthread_mutexattr_t attr; 92 | 93 | if (pthread_mutexattr_init(&attr) != 0) 94 | return (true); 95 | pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE); 96 | if (pthread_mutex_init(&mutex->lock, &attr) != 0) { 97 | pthread_mutexattr_destroy(&attr); 98 | return (true); 99 | } 100 | pthread_mutexattr_destroy(&attr); 101 | #endif 102 | return (false); 103 | } 104 | 105 | void 106 | malloc_mutex_prefork(malloc_mutex_t *mutex) 107 | { 108 | 109 | malloc_mutex_lock(mutex); 110 | } 111 | 112 | void 113 | malloc_mutex_postfork_parent(malloc_mutex_t *mutex) 114 | { 115 | 116 | malloc_mutex_unlock(mutex); 117 | } 118 | 119 | void 120 | malloc_mutex_postfork_child(malloc_mutex_t *mutex) 121 | { 122 | 123 | #ifdef JEMALLOC_MUTEX_INIT_CB 124 | malloc_mutex_unlock(mutex); 125 | #else 126 | if (malloc_mutex_init(mutex)) { 127 | malloc_printf(": Error re-initializing mutex in " 128 | "child\n"); 129 | if (opt_abort) 130 | abort(); 131 | } 132 | #endif 133 | } 134 | 135 | bool 136 | mutex_boot(void) 137 | { 138 | 139 | #ifdef JEMALLOC_MUTEX_INIT_CB 140 | postpone_init = false; 141 | while (postponed_mutexes != NULL) { 142 | if (_pthread_mutex_init_calloc_cb(&postponed_mutexes->lock, 143 | base_calloc) != 0) 144 | return (true); 145 | postponed_mutexes = postponed_mutexes->postponed_next; 146 | } 147 | #endif 148 | return (false); 149 | } 150 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/rtree.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_RTREE_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | rtree_t * 5 | rtree_new(unsigned bits) 6 | { 7 | rtree_t *ret; 8 | unsigned bits_per_level, height, i; 9 | 10 | bits_per_level = ffs(pow2_ceil((RTREE_NODESIZE / sizeof(void *)))) - 1; 11 | height = bits / bits_per_level; 12 | if (height * bits_per_level != bits) 13 | height++; 14 | assert(height * bits_per_level >= bits); 15 | 16 | ret = (rtree_t*)base_alloc(offsetof(rtree_t, level2bits) + 17 | (sizeof(unsigned) * height)); 18 | if (ret == NULL) 19 | return (NULL); 20 | memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) * 21 | height)); 22 | 23 | if (malloc_mutex_init(&ret->mutex)) { 24 | /* Leak the rtree. */ 25 | return (NULL); 26 | } 27 | ret->height = height; 28 | if (bits_per_level * height > bits) 29 | ret->level2bits[0] = bits % bits_per_level; 30 | else 31 | ret->level2bits[0] = bits_per_level; 32 | for (i = 1; i < height; i++) 33 | ret->level2bits[i] = bits_per_level; 34 | 35 | ret->root = (void**)base_alloc(sizeof(void *) << ret->level2bits[0]); 36 | if (ret->root == NULL) { 37 | /* 38 | * We leak the rtree here, since there's no generic base 39 | * deallocation. 40 | */ 41 | return (NULL); 42 | } 43 | memset(ret->root, 0, sizeof(void *) << ret->level2bits[0]); 44 | 45 | return (ret); 46 | } 47 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/src/tsd.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_TSD_C_ 2 | #include "jemalloc/internal/jemalloc_internal.h" 3 | 4 | /******************************************************************************/ 5 | /* Data. */ 6 | 7 | static unsigned ncleanups; 8 | static malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX]; 9 | 10 | /******************************************************************************/ 11 | 12 | void * 13 | malloc_tsd_malloc(size_t size) 14 | { 15 | 16 | /* Avoid choose_arena() in order to dodge bootstrapping issues. */ 17 | return (arena_malloc(arenas[0], size, false, false)); 18 | } 19 | 20 | void 21 | malloc_tsd_dalloc(void *wrapper) 22 | { 23 | 24 | idalloc(wrapper); 25 | } 26 | 27 | void 28 | malloc_tsd_no_cleanup(void *arg) 29 | { 30 | 31 | not_reached(); 32 | } 33 | 34 | #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32) 35 | #ifndef _WIN32 36 | JEMALLOC_EXPORT 37 | #endif 38 | void 39 | _malloc_thread_cleanup(void) 40 | { 41 | bool pending[MALLOC_TSD_CLEANUPS_MAX], again; 42 | unsigned i; 43 | 44 | for (i = 0; i < ncleanups; i++) 45 | pending[i] = true; 46 | 47 | do { 48 | again = false; 49 | for (i = 0; i < ncleanups; i++) { 50 | if (pending[i]) { 51 | pending[i] = cleanups[i](); 52 | if (pending[i]) 53 | again = true; 54 | } 55 | } 56 | } while (again); 57 | } 58 | #endif 59 | 60 | void 61 | malloc_tsd_cleanup_register(bool (*f)(void)) 62 | { 63 | 64 | assert(ncleanups < MALLOC_TSD_CLEANUPS_MAX); 65 | cleanups[ncleanups] = f; 66 | ncleanups++; 67 | } 68 | 69 | void 70 | malloc_tsd_boot(void) 71 | { 72 | 73 | ncleanups = 0; 74 | } 75 | 76 | #ifdef _WIN32 77 | static BOOL WINAPI 78 | _tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 79 | { 80 | 81 | switch (fdwReason) { 82 | #ifdef JEMALLOC_LAZY_LOCK 83 | case DLL_THREAD_ATTACH: 84 | isthreaded = true; 85 | break; 86 | #endif 87 | case DLL_THREAD_DETACH: 88 | _malloc_thread_cleanup(); 89 | break; 90 | default: 91 | break; 92 | } 93 | return (true); 94 | } 95 | 96 | #ifdef _MSC_VER 97 | # ifdef _M_IX86 98 | # pragma comment(linker, "/INCLUDE:__tls_used") 99 | # else 100 | # pragma comment(linker, "/INCLUDE:_tls_used") 101 | # endif 102 | # pragma section(".CRT$XLY",long,read) 103 | #endif 104 | JEMALLOC_SECTION(".CRT$XLY") JEMALLOC_ATTR(used) 105 | static const BOOL (WINAPI *tls_callback)(HINSTANCE hinstDLL, 106 | DWORD fdwReason, LPVOID lpvReserved) = _tls_callback; 107 | #endif 108 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/aligned_alloc.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define CHUNK 0x400000 5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ 6 | #define MAXALIGN ((size_t)0x2000000LU) 7 | #define NITER 4 8 | 9 | int 10 | main(void) 11 | { 12 | size_t alignment, size, total; 13 | unsigned i; 14 | void *p, *ps[NITER]; 15 | 16 | malloc_printf("Test begin\n"); 17 | 18 | /* Test error conditions. */ 19 | alignment = 0; 20 | set_errno(0); 21 | p = aligned_alloc(alignment, 1); 22 | if (p != NULL || get_errno() != EINVAL) { 23 | malloc_printf( 24 | "Expected error for invalid alignment %zu\n", alignment); 25 | } 26 | 27 | for (alignment = sizeof(size_t); alignment < MAXALIGN; 28 | alignment <<= 1) { 29 | set_errno(0); 30 | p = aligned_alloc(alignment + 1, 1); 31 | if (p != NULL || get_errno() != EINVAL) { 32 | malloc_printf( 33 | "Expected error for invalid alignment %zu\n", 34 | alignment + 1); 35 | } 36 | } 37 | 38 | #if LG_SIZEOF_PTR == 3 39 | alignment = UINT64_C(0x8000000000000000); 40 | size = UINT64_C(0x8000000000000000); 41 | #else 42 | alignment = 0x80000000LU; 43 | size = 0x80000000LU; 44 | #endif 45 | set_errno(0); 46 | p = aligned_alloc(alignment, size); 47 | if (p != NULL || get_errno() != ENOMEM) { 48 | malloc_printf( 49 | "Expected error for aligned_alloc(%zu, %zu)\n", 50 | alignment, size); 51 | } 52 | 53 | #if LG_SIZEOF_PTR == 3 54 | alignment = UINT64_C(0x4000000000000000); 55 | size = UINT64_C(0x8400000000000001); 56 | #else 57 | alignment = 0x40000000LU; 58 | size = 0x84000001LU; 59 | #endif 60 | set_errno(0); 61 | p = aligned_alloc(alignment, size); 62 | if (p != NULL || get_errno() != ENOMEM) { 63 | malloc_printf( 64 | "Expected error for aligned_alloc(%zu, %zu)\n", 65 | alignment, size); 66 | } 67 | 68 | alignment = 0x10LU; 69 | #if LG_SIZEOF_PTR == 3 70 | size = UINT64_C(0xfffffffffffffff0); 71 | #else 72 | size = 0xfffffff0LU; 73 | #endif 74 | set_errno(0); 75 | p = aligned_alloc(alignment, size); 76 | if (p != NULL || get_errno() != ENOMEM) { 77 | malloc_printf( 78 | "Expected error for aligned_alloc(&p, %zu, %zu)\n", 79 | alignment, size); 80 | } 81 | 82 | for (i = 0; i < NITER; i++) 83 | ps[i] = NULL; 84 | 85 | for (alignment = 8; 86 | alignment <= MAXALIGN; 87 | alignment <<= 1) { 88 | total = 0; 89 | malloc_printf("Alignment: %zu\n", alignment); 90 | for (size = 1; 91 | size < 3 * alignment && size < (1U << 31); 92 | size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { 93 | for (i = 0; i < NITER; i++) { 94 | ps[i] = aligned_alloc(alignment, size); 95 | if (ps[i] == NULL) { 96 | char buf[BUFERROR_BUF]; 97 | 98 | buferror(buf, sizeof(buf)); 99 | malloc_printf( 100 | "Error for size %zu (%#zx): %s\n", 101 | size, size, buf); 102 | exit(1); 103 | } 104 | total += malloc_usable_size(ps[i]); 105 | if (total >= (MAXALIGN << 1)) 106 | break; 107 | } 108 | for (i = 0; i < NITER; i++) { 109 | if (ps[i] != NULL) { 110 | free(ps[i]); 111 | ps[i] = NULL; 112 | } 113 | } 114 | } 115 | } 116 | 117 | malloc_printf("Test end\n"); 118 | return (0); 119 | } 120 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/aligned_alloc.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Alignment: 8 3 | Alignment: 16 4 | Alignment: 32 5 | Alignment: 64 6 | Alignment: 128 7 | Alignment: 256 8 | Alignment: 512 9 | Alignment: 1024 10 | Alignment: 2048 11 | Alignment: 4096 12 | Alignment: 8192 13 | Alignment: 16384 14 | Alignment: 32768 15 | Alignment: 65536 16 | Alignment: 131072 17 | Alignment: 262144 18 | Alignment: 524288 19 | Alignment: 1048576 20 | Alignment: 2097152 21 | Alignment: 4194304 22 | Alignment: 8388608 23 | Alignment: 16777216 24 | Alignment: 33554432 25 | Test end 26 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/allocated.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | void * 5 | je_thread_start(void *arg) 6 | { 7 | int err; 8 | void *p; 9 | uint64_t a0, a1, d0, d1; 10 | uint64_t *ap0, *ap1, *dp0, *dp1; 11 | size_t sz, usize; 12 | 13 | sz = sizeof(a0); 14 | if ((err = mallctl("thread.allocated", &a0, &sz, NULL, 0))) { 15 | if (err == ENOENT) { 16 | #ifdef JEMALLOC_STATS 17 | assert(false); 18 | #endif 19 | goto label_return; 20 | } 21 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 22 | strerror(err)); 23 | exit(1); 24 | } 25 | sz = sizeof(ap0); 26 | if ((err = mallctl("thread.allocatedp", &ap0, &sz, NULL, 0))) { 27 | if (err == ENOENT) { 28 | #ifdef JEMALLOC_STATS 29 | assert(false); 30 | #endif 31 | goto label_return; 32 | } 33 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 34 | strerror(err)); 35 | exit(1); 36 | } 37 | assert(*ap0 == a0); 38 | 39 | sz = sizeof(d0); 40 | if ((err = mallctl("thread.deallocated", &d0, &sz, NULL, 0))) { 41 | if (err == ENOENT) { 42 | #ifdef JEMALLOC_STATS 43 | assert(false); 44 | #endif 45 | goto label_return; 46 | } 47 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 48 | strerror(err)); 49 | exit(1); 50 | } 51 | sz = sizeof(dp0); 52 | if ((err = mallctl("thread.deallocatedp", &dp0, &sz, NULL, 0))) { 53 | if (err == ENOENT) { 54 | #ifdef JEMALLOC_STATS 55 | assert(false); 56 | #endif 57 | goto label_return; 58 | } 59 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 60 | strerror(err)); 61 | exit(1); 62 | } 63 | assert(*dp0 == d0); 64 | 65 | p = malloc(1); 66 | if (p == NULL) { 67 | malloc_printf("%s(): Error in malloc()\n", __func__); 68 | exit(1); 69 | } 70 | 71 | sz = sizeof(a1); 72 | mallctl("thread.allocated", &a1, &sz, NULL, 0); 73 | sz = sizeof(ap1); 74 | mallctl("thread.allocatedp", &ap1, &sz, NULL, 0); 75 | assert(*ap1 == a1); 76 | assert(ap0 == ap1); 77 | 78 | usize = malloc_usable_size(p); 79 | assert(a0 + usize <= a1); 80 | 81 | free(p); 82 | 83 | sz = sizeof(d1); 84 | mallctl("thread.deallocated", &d1, &sz, NULL, 0); 85 | sz = sizeof(dp1); 86 | mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0); 87 | assert(*dp1 == d1); 88 | assert(dp0 == dp1); 89 | 90 | assert(d0 + usize <= d1); 91 | 92 | label_return: 93 | return (NULL); 94 | } 95 | 96 | int 97 | main(void) 98 | { 99 | int ret = 0; 100 | je_thread_t thread; 101 | 102 | malloc_printf("Test begin\n"); 103 | 104 | je_thread_start(NULL); 105 | 106 | je_thread_create(&thread, je_thread_start, NULL); 107 | je_thread_join(thread, (void *)&ret); 108 | 109 | je_thread_start(NULL); 110 | 111 | je_thread_create(&thread, je_thread_start, NULL); 112 | je_thread_join(thread, (void *)&ret); 113 | 114 | je_thread_start(NULL); 115 | 116 | malloc_printf("Test end\n"); 117 | return (ret); 118 | } 119 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/allocated.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/allocm.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Alignment: 8 3 | Alignment: 16 4 | Alignment: 32 5 | Alignment: 64 6 | Alignment: 128 7 | Alignment: 256 8 | Alignment: 512 9 | Alignment: 1024 10 | Alignment: 2048 11 | Alignment: 4096 12 | Alignment: 8192 13 | Alignment: 16384 14 | Alignment: 32768 15 | Alignment: 65536 16 | Alignment: 131072 17 | Alignment: 262144 18 | Alignment: 524288 19 | Alignment: 1048576 20 | Alignment: 2097152 21 | Alignment: 4194304 22 | Alignment: 8388608 23 | Alignment: 16777216 24 | Alignment: 33554432 25 | Test end 26 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/bitmap.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #if (LG_BITMAP_MAXBITS > 12) 5 | # define MAXBITS 4500 6 | #else 7 | # define MAXBITS (1U << LG_BITMAP_MAXBITS) 8 | #endif 9 | 10 | static void 11 | test_bitmap_size(void) 12 | { 13 | size_t i, prev_size; 14 | 15 | prev_size = 0; 16 | for (i = 1; i <= MAXBITS; i++) { 17 | size_t size = bitmap_size(i); 18 | assert(size >= prev_size); 19 | prev_size = size; 20 | } 21 | } 22 | 23 | static void 24 | test_bitmap_init(void) 25 | { 26 | size_t i; 27 | 28 | for (i = 1; i <= MAXBITS; i++) { 29 | bitmap_info_t binfo; 30 | bitmap_info_init(&binfo, i); 31 | { 32 | size_t j; 33 | bitmap_t *bitmap = malloc(sizeof(bitmap_t) * 34 | bitmap_info_ngroups(&binfo)); 35 | bitmap_init(bitmap, &binfo); 36 | 37 | for (j = 0; j < i; j++) 38 | assert(bitmap_get(bitmap, &binfo, j) == false); 39 | free(bitmap); 40 | 41 | } 42 | } 43 | } 44 | 45 | static void 46 | test_bitmap_set(void) 47 | { 48 | size_t i; 49 | 50 | for (i = 1; i <= MAXBITS; i++) { 51 | bitmap_info_t binfo; 52 | bitmap_info_init(&binfo, i); 53 | { 54 | size_t j; 55 | bitmap_t *bitmap = malloc(sizeof(bitmap_t) * 56 | bitmap_info_ngroups(&binfo)); 57 | bitmap_init(bitmap, &binfo); 58 | 59 | for (j = 0; j < i; j++) 60 | bitmap_set(bitmap, &binfo, j); 61 | assert(bitmap_full(bitmap, &binfo)); 62 | free(bitmap); 63 | } 64 | } 65 | } 66 | 67 | static void 68 | test_bitmap_unset(void) 69 | { 70 | size_t i; 71 | 72 | for (i = 1; i <= MAXBITS; i++) { 73 | bitmap_info_t binfo; 74 | bitmap_info_init(&binfo, i); 75 | { 76 | size_t j; 77 | bitmap_t *bitmap = malloc(sizeof(bitmap_t) * 78 | bitmap_info_ngroups(&binfo)); 79 | bitmap_init(bitmap, &binfo); 80 | 81 | for (j = 0; j < i; j++) 82 | bitmap_set(bitmap, &binfo, j); 83 | assert(bitmap_full(bitmap, &binfo)); 84 | for (j = 0; j < i; j++) 85 | bitmap_unset(bitmap, &binfo, j); 86 | for (j = 0; j < i; j++) 87 | bitmap_set(bitmap, &binfo, j); 88 | assert(bitmap_full(bitmap, &binfo)); 89 | free(bitmap); 90 | } 91 | } 92 | } 93 | 94 | static void 95 | test_bitmap_sfu(void) 96 | { 97 | size_t i; 98 | 99 | for (i = 1; i <= MAXBITS; i++) { 100 | bitmap_info_t binfo; 101 | bitmap_info_init(&binfo, i); 102 | { 103 | ssize_t j; 104 | bitmap_t *bitmap = malloc(sizeof(bitmap_t) * 105 | bitmap_info_ngroups(&binfo)); 106 | bitmap_init(bitmap, &binfo); 107 | 108 | /* Iteratively set bits starting at the beginning. */ 109 | for (j = 0; j < i; j++) 110 | assert(bitmap_sfu(bitmap, &binfo) == j); 111 | assert(bitmap_full(bitmap, &binfo)); 112 | 113 | /* 114 | * Iteratively unset bits starting at the end, and 115 | * verify that bitmap_sfu() reaches the unset bits. 116 | */ 117 | for (j = i - 1; j >= 0; j--) { 118 | bitmap_unset(bitmap, &binfo, j); 119 | assert(bitmap_sfu(bitmap, &binfo) == j); 120 | bitmap_unset(bitmap, &binfo, j); 121 | } 122 | assert(bitmap_get(bitmap, &binfo, 0) == false); 123 | 124 | /* 125 | * Iteratively set bits starting at the beginning, and 126 | * verify that bitmap_sfu() looks past them. 127 | */ 128 | for (j = 1; j < i; j++) { 129 | bitmap_set(bitmap, &binfo, j - 1); 130 | assert(bitmap_sfu(bitmap, &binfo) == j); 131 | bitmap_unset(bitmap, &binfo, j); 132 | } 133 | assert(bitmap_sfu(bitmap, &binfo) == i - 1); 134 | assert(bitmap_full(bitmap, &binfo)); 135 | free(bitmap); 136 | } 137 | } 138 | } 139 | 140 | int 141 | main(void) 142 | { 143 | malloc_printf("Test begin\n"); 144 | 145 | test_bitmap_size(); 146 | test_bitmap_init(); 147 | test_bitmap_set(); 148 | test_bitmap_unset(); 149 | test_bitmap_sfu(); 150 | 151 | malloc_printf("Test end\n"); 152 | return (0); 153 | } 154 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/bitmap.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/jemalloc_test.h.in: -------------------------------------------------------------------------------- 1 | /* 2 | * This header should be included by tests, rather than directly including 3 | * jemalloc/jemalloc.h, because --with-install-suffix may cause the header to 4 | * have a different name. 5 | */ 6 | #include "jemalloc/jemalloc@install_suffix@.h" 7 | #include "jemalloc/internal/jemalloc_internal.h" 8 | 9 | /* Abstraction layer for threading in tests */ 10 | #ifdef _WIN32 11 | #include 12 | 13 | typedef HANDLE je_thread_t; 14 | 15 | void 16 | je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg) 17 | { 18 | LPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc; 19 | *thread = CreateThread(NULL, 0, routine, arg, 0, NULL); 20 | if (*thread == NULL) { 21 | malloc_printf("Error in CreateThread()\n"); 22 | exit(1); 23 | } 24 | } 25 | 26 | void 27 | je_thread_join(je_thread_t thread, void **ret) 28 | { 29 | WaitForSingleObject(thread, INFINITE); 30 | } 31 | 32 | #else 33 | #include 34 | 35 | typedef pthread_t je_thread_t; 36 | 37 | void 38 | je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg) 39 | { 40 | 41 | if (pthread_create(thread, NULL, proc, arg) != 0) { 42 | malloc_printf("Error in pthread_create()\n"); 43 | exit(1); 44 | } 45 | } 46 | 47 | void 48 | je_thread_join(je_thread_t thread, void **ret) 49 | { 50 | 51 | pthread_join(thread, ret); 52 | } 53 | #endif 54 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/mremap.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | int 5 | main(void) 6 | { 7 | int ret, err; 8 | size_t sz, lg_chunk, chunksize, i; 9 | char *p, *q; 10 | 11 | malloc_printf("Test begin\n"); 12 | 13 | sz = sizeof(lg_chunk); 14 | if ((err = mallctl("opt.lg_chunk", &lg_chunk, &sz, NULL, 0))) { 15 | assert(err != ENOENT); 16 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 17 | strerror(err)); 18 | ret = 1; 19 | goto label_return; 20 | } 21 | chunksize = ((size_t)1U) << lg_chunk; 22 | 23 | p = (char *)malloc(chunksize); 24 | if (p == NULL) { 25 | malloc_printf("malloc(%zu) --> %p\n", chunksize, p); 26 | ret = 1; 27 | goto label_return; 28 | } 29 | memset(p, 'a', chunksize); 30 | 31 | q = (char *)realloc(p, chunksize * 2); 32 | if (q == NULL) { 33 | malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize * 2, 34 | q); 35 | ret = 1; 36 | goto label_return; 37 | } 38 | for (i = 0; i < chunksize; i++) { 39 | assert(q[i] == 'a'); 40 | } 41 | 42 | p = q; 43 | 44 | q = (char *)realloc(p, chunksize); 45 | if (q == NULL) { 46 | malloc_printf("realloc(%p, %zu) --> %p\n", p, chunksize, q); 47 | ret = 1; 48 | goto label_return; 49 | } 50 | for (i = 0; i < chunksize; i++) { 51 | assert(q[i] == 'a'); 52 | } 53 | 54 | free(q); 55 | 56 | ret = 0; 57 | label_return: 58 | malloc_printf("Test end\n"); 59 | return (ret); 60 | } 61 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/mremap.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/posix_memalign.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define CHUNK 0x400000 5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ 6 | #define MAXALIGN ((size_t)0x2000000LU) 7 | #define NITER 4 8 | 9 | int 10 | main(void) 11 | { 12 | size_t alignment, size, total; 13 | unsigned i; 14 | int err; 15 | void *p, *ps[NITER]; 16 | 17 | malloc_printf("Test begin\n"); 18 | 19 | /* Test error conditions. */ 20 | for (alignment = 0; alignment < sizeof(void *); alignment++) { 21 | err = posix_memalign(&p, alignment, 1); 22 | if (err != EINVAL) { 23 | malloc_printf( 24 | "Expected error for invalid alignment %zu\n", 25 | alignment); 26 | } 27 | } 28 | 29 | for (alignment = sizeof(size_t); alignment < MAXALIGN; 30 | alignment <<= 1) { 31 | err = posix_memalign(&p, alignment + 1, 1); 32 | if (err == 0) { 33 | malloc_printf( 34 | "Expected error for invalid alignment %zu\n", 35 | alignment + 1); 36 | } 37 | } 38 | 39 | #if LG_SIZEOF_PTR == 3 40 | alignment = UINT64_C(0x8000000000000000); 41 | size = UINT64_C(0x8000000000000000); 42 | #else 43 | alignment = 0x80000000LU; 44 | size = 0x80000000LU; 45 | #endif 46 | err = posix_memalign(&p, alignment, size); 47 | if (err == 0) { 48 | malloc_printf( 49 | "Expected error for posix_memalign(&p, %zu, %zu)\n", 50 | alignment, size); 51 | } 52 | 53 | #if LG_SIZEOF_PTR == 3 54 | alignment = UINT64_C(0x4000000000000000); 55 | size = UINT64_C(0x8400000000000001); 56 | #else 57 | alignment = 0x40000000LU; 58 | size = 0x84000001LU; 59 | #endif 60 | err = posix_memalign(&p, alignment, size); 61 | if (err == 0) { 62 | malloc_printf( 63 | "Expected error for posix_memalign(&p, %zu, %zu)\n", 64 | alignment, size); 65 | } 66 | 67 | alignment = 0x10LU; 68 | #if LG_SIZEOF_PTR == 3 69 | size = UINT64_C(0xfffffffffffffff0); 70 | #else 71 | size = 0xfffffff0LU; 72 | #endif 73 | err = posix_memalign(&p, alignment, size); 74 | if (err == 0) { 75 | malloc_printf( 76 | "Expected error for posix_memalign(&p, %zu, %zu)\n", 77 | alignment, size); 78 | } 79 | 80 | for (i = 0; i < NITER; i++) 81 | ps[i] = NULL; 82 | 83 | for (alignment = 8; 84 | alignment <= MAXALIGN; 85 | alignment <<= 1) { 86 | total = 0; 87 | malloc_printf("Alignment: %zu\n", alignment); 88 | for (size = 1; 89 | size < 3 * alignment && size < (1U << 31); 90 | size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { 91 | for (i = 0; i < NITER; i++) { 92 | err = posix_memalign(&ps[i], 93 | alignment, size); 94 | if (err) { 95 | malloc_printf( 96 | "Error for size %zu (%#zx): %s\n", 97 | size, size, strerror(err)); 98 | exit(1); 99 | } 100 | total += malloc_usable_size(ps[i]); 101 | if (total >= (MAXALIGN << 1)) 102 | break; 103 | } 104 | for (i = 0; i < NITER; i++) { 105 | if (ps[i] != NULL) { 106 | free(ps[i]); 107 | ps[i] = NULL; 108 | } 109 | } 110 | } 111 | } 112 | 113 | malloc_printf("Test end\n"); 114 | return (0); 115 | } 116 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/posix_memalign.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Alignment: 8 3 | Alignment: 16 4 | Alignment: 32 5 | Alignment: 64 6 | Alignment: 128 7 | Alignment: 256 8 | Alignment: 512 9 | Alignment: 1024 10 | Alignment: 2048 11 | Alignment: 4096 12 | Alignment: 8192 13 | Alignment: 16384 14 | Alignment: 32768 15 | Alignment: 65536 16 | Alignment: 131072 17 | Alignment: 262144 18 | Alignment: 524288 19 | Alignment: 1048576 20 | Alignment: 2097152 21 | Alignment: 4194304 22 | Alignment: 8388608 23 | Alignment: 16777216 24 | Alignment: 33554432 25 | Test end 26 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/rallocm.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | int 5 | main(void) 6 | { 7 | size_t pagesize; 8 | void *p, *q; 9 | size_t sz, tsz; 10 | int r; 11 | 12 | malloc_printf("Test begin\n"); 13 | 14 | /* Get page size. */ 15 | { 16 | #ifdef _WIN32 17 | SYSTEM_INFO si; 18 | GetSystemInfo(&si); 19 | pagesize = (size_t)si.dwPageSize; 20 | #else 21 | long result = sysconf(_SC_PAGESIZE); 22 | assert(result != -1); 23 | pagesize = (size_t)result; 24 | #endif 25 | } 26 | 27 | r = allocm(&p, &sz, 42, 0); 28 | if (r != ALLOCM_SUCCESS) { 29 | malloc_printf("Unexpected allocm() error\n"); 30 | abort(); 31 | } 32 | 33 | q = p; 34 | r = rallocm(&q, &tsz, sz, 0, ALLOCM_NO_MOVE); 35 | if (r != ALLOCM_SUCCESS) 36 | malloc_printf("Unexpected rallocm() error\n"); 37 | if (q != p) 38 | malloc_printf("Unexpected object move\n"); 39 | if (tsz != sz) { 40 | malloc_printf("Unexpected size change: %zu --> %zu\n", 41 | sz, tsz); 42 | } 43 | 44 | q = p; 45 | r = rallocm(&q, &tsz, sz, 5, ALLOCM_NO_MOVE); 46 | if (r != ALLOCM_SUCCESS) 47 | malloc_printf("Unexpected rallocm() error\n"); 48 | if (q != p) 49 | malloc_printf("Unexpected object move\n"); 50 | if (tsz != sz) { 51 | malloc_printf("Unexpected size change: %zu --> %zu\n", 52 | sz, tsz); 53 | } 54 | 55 | q = p; 56 | r = rallocm(&q, &tsz, sz + 5, 0, ALLOCM_NO_MOVE); 57 | if (r != ALLOCM_ERR_NOT_MOVED) 58 | malloc_printf("Unexpected rallocm() result\n"); 59 | if (q != p) 60 | malloc_printf("Unexpected object move\n"); 61 | if (tsz != sz) { 62 | malloc_printf("Unexpected size change: %zu --> %zu\n", 63 | sz, tsz); 64 | } 65 | 66 | q = p; 67 | r = rallocm(&q, &tsz, sz + 5, 0, 0); 68 | if (r != ALLOCM_SUCCESS) 69 | malloc_printf("Unexpected rallocm() error\n"); 70 | if (q == p) 71 | malloc_printf("Expected object move\n"); 72 | if (tsz == sz) { 73 | malloc_printf("Expected size change: %zu --> %zu\n", 74 | sz, tsz); 75 | } 76 | p = q; 77 | sz = tsz; 78 | 79 | r = rallocm(&q, &tsz, pagesize*2, 0, 0); 80 | if (r != ALLOCM_SUCCESS) 81 | malloc_printf("Unexpected rallocm() error\n"); 82 | if (q == p) 83 | malloc_printf("Expected object move\n"); 84 | if (tsz == sz) { 85 | malloc_printf("Expected size change: %zu --> %zu\n", 86 | sz, tsz); 87 | } 88 | p = q; 89 | sz = tsz; 90 | 91 | r = rallocm(&q, &tsz, pagesize*4, 0, 0); 92 | if (r != ALLOCM_SUCCESS) 93 | malloc_printf("Unexpected rallocm() error\n"); 94 | if (tsz == sz) { 95 | malloc_printf("Expected size change: %zu --> %zu\n", 96 | sz, tsz); 97 | } 98 | p = q; 99 | sz = tsz; 100 | 101 | r = rallocm(&q, &tsz, pagesize*2, 0, ALLOCM_NO_MOVE); 102 | if (r != ALLOCM_SUCCESS) 103 | malloc_printf("Unexpected rallocm() error\n"); 104 | if (q != p) 105 | malloc_printf("Unexpected object move\n"); 106 | if (tsz == sz) { 107 | malloc_printf("Expected size change: %zu --> %zu\n", 108 | sz, tsz); 109 | } 110 | sz = tsz; 111 | 112 | r = rallocm(&q, &tsz, pagesize*4, 0, ALLOCM_NO_MOVE); 113 | if (r != ALLOCM_SUCCESS) 114 | malloc_printf("Unexpected rallocm() error\n"); 115 | if (q != p) 116 | malloc_printf("Unexpected object move\n"); 117 | if (tsz == sz) { 118 | malloc_printf("Expected size change: %zu --> %zu\n", 119 | sz, tsz); 120 | } 121 | sz = tsz; 122 | 123 | dallocm(p, 0); 124 | 125 | malloc_printf("Test end\n"); 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/rallocm.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/thread_arena.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | #define NTHREADS 10 5 | 6 | void * 7 | je_thread_start(void *arg) 8 | { 9 | unsigned main_arena_ind = *(unsigned *)arg; 10 | void *p; 11 | unsigned arena_ind; 12 | size_t size; 13 | int err; 14 | 15 | p = malloc(1); 16 | if (p == NULL) { 17 | malloc_printf("%s(): Error in malloc()\n", __func__); 18 | return (void *)1; 19 | } 20 | 21 | size = sizeof(arena_ind); 22 | if ((err = mallctl("thread.arena", &arena_ind, &size, &main_arena_ind, 23 | sizeof(main_arena_ind)))) { 24 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 25 | strerror(err)); 26 | return (void *)1; 27 | } 28 | 29 | size = sizeof(arena_ind); 30 | if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 31 | 0))) { 32 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 33 | strerror(err)); 34 | return (void *)1; 35 | } 36 | assert(arena_ind == main_arena_ind); 37 | 38 | return (NULL); 39 | } 40 | 41 | int 42 | main(void) 43 | { 44 | int ret = 0; 45 | void *p; 46 | unsigned arena_ind; 47 | size_t size; 48 | int err; 49 | je_thread_t threads[NTHREADS]; 50 | unsigned i; 51 | 52 | malloc_printf("Test begin\n"); 53 | 54 | p = malloc(1); 55 | if (p == NULL) { 56 | malloc_printf("%s(): Error in malloc()\n", __func__); 57 | ret = 1; 58 | goto label_return; 59 | } 60 | 61 | size = sizeof(arena_ind); 62 | if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) { 63 | malloc_printf("%s(): Error in mallctl(): %s\n", __func__, 64 | strerror(err)); 65 | ret = 1; 66 | goto label_return; 67 | } 68 | 69 | for (i = 0; i < NTHREADS; i++) 70 | je_thread_create(&threads[i], je_thread_start, (void *)&arena_ind); 71 | 72 | for (i = 0; i < NTHREADS; i++) 73 | je_thread_join(threads[i], (void *)&ret); 74 | 75 | label_return: 76 | malloc_printf("Test end\n"); 77 | return (ret); 78 | } 79 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/thread_arena.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/thread_tcache_enabled.c: -------------------------------------------------------------------------------- 1 | #define JEMALLOC_MANGLE 2 | #include "jemalloc_test.h" 3 | 4 | void * 5 | je_thread_start(void *arg) 6 | { 7 | int err; 8 | size_t sz; 9 | bool e0, e1; 10 | 11 | sz = sizeof(bool); 12 | if ((err = mallctl("thread.tcache.enabled", &e0, &sz, NULL, 0))) { 13 | if (err == ENOENT) { 14 | #ifdef JEMALLOC_TCACHE 15 | assert(false); 16 | #endif 17 | } 18 | goto label_return; 19 | } 20 | 21 | if (e0) { 22 | e1 = false; 23 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) 24 | == 0); 25 | assert(e0); 26 | } 27 | 28 | e1 = true; 29 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 30 | assert(e0 == false); 31 | 32 | e1 = true; 33 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 34 | assert(e0); 35 | 36 | e1 = false; 37 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 38 | assert(e0); 39 | 40 | e1 = false; 41 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 42 | assert(e0 == false); 43 | 44 | free(malloc(1)); 45 | e1 = true; 46 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 47 | assert(e0 == false); 48 | 49 | free(malloc(1)); 50 | e1 = true; 51 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 52 | assert(e0); 53 | 54 | free(malloc(1)); 55 | e1 = false; 56 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 57 | assert(e0); 58 | 59 | free(malloc(1)); 60 | e1 = false; 61 | assert(mallctl("thread.tcache.enabled", &e0, &sz, &e1, sz) == 0); 62 | assert(e0 == false); 63 | 64 | free(malloc(1)); 65 | label_return: 66 | return (NULL); 67 | } 68 | 69 | int 70 | main(void) 71 | { 72 | int ret = 0; 73 | je_thread_t thread; 74 | 75 | malloc_printf("Test begin\n"); 76 | 77 | je_thread_start(NULL); 78 | 79 | je_thread_create(&thread, je_thread_start, NULL); 80 | je_thread_join(thread, (void *)&ret); 81 | 82 | je_thread_start(NULL); 83 | 84 | je_thread_create(&thread, je_thread_start, NULL); 85 | je_thread_join(thread, (void *)&ret); 86 | 87 | je_thread_start(NULL); 88 | 89 | malloc_printf("Test end\n"); 90 | return (ret); 91 | } 92 | -------------------------------------------------------------------------------- /chaos/deps/jemalloc/test/thread_tcache_enabled.exp: -------------------------------------------------------------------------------- 1 | Test begin 2 | Test end 3 | -------------------------------------------------------------------------------- /chaos/heart_beat/heart_beat_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_HEART_BEAT_INC_H_ 10 | #define _CHAOS_HEART_BEAT_INC_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #endif //! _CHAOS_HEART_BEAT_INC_H_ 17 | 18 | -------------------------------------------------------------------------------- /chaos/log/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -Wall -g -O2 -fPIC 2 | 3 | ## 安装根目录 4 | chaosdir=$(prefix)/chaos-@chaos_ver@ 5 | 6 | ## 安装库目录 7 | libdir=$(chaosdir)/lib 8 | 9 | lib_LIBRARIES = libchaos_log.a 10 | libchaos_log_a_LIBFLAGS = 11 | libchaos_log_a_LIBADD = 12 | 13 | libchaos_log_a_SOURCES = \ 14 | log_misc.cpp \ 15 | log.cpp 16 | 17 | -------------------------------------------------------------------------------- /chaos/log/log_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_LOG_INC_H_ 10 | #define _CHAOS_LOG_INC_H_ 11 | 12 | #include 13 | #include 14 | 15 | 16 | #endif //! _CHAOS_LOG_INC_H_ 17 | -------------------------------------------------------------------------------- /chaos/log/log_misc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | /*! 10 | * @file log_misc.cpp 11 | * @author yunjie.lu 12 | * @email lyjdamzwf@gmail.com 13 | * @weibo http://weibo.com/crazyprogramerlyj 14 | * @date 2012.4.6 15 | * @brief log misc 16 | * @last changed 17 | * 18 | */ 19 | 20 | #include 21 | 22 | namespace chaos 23 | { 24 | 25 | namespace log 26 | { 27 | 28 | int init_log(const std::string& path, 29 | const std::string& file, 30 | const int flag_print_file, 31 | const int flag_print_screen, 32 | const int flag_log_level, 33 | const std::vector& modules, 34 | log_t::print_screen_callback_t screen_callback_, 35 | log_t::print_file_callback_t file_callback_, 36 | const int max_line, 37 | const int maz_size) 38 | { 39 | singleton_t::instance().set_path(path.c_str()); 40 | singleton_t::instance().set_filename(file.c_str()); 41 | singleton_t::instance().set_maxline(max_line); 42 | singleton_t::instance().set_maxsize(maz_size); 43 | if(flag_print_file) singleton_t::instance().enable_print_file(true); 44 | if(flag_print_screen) singleton_t::instance().enable_print_screen(true); 45 | if( flag_log_level >=1 ) singleton_t::instance().enable_log_level(LOG_FLAG(LF_FATAL), true); 46 | if( flag_log_level >=2 ) singleton_t::instance().enable_log_level(LOG_FLAG(LF_ERROR), true); 47 | if( flag_log_level >=3 ) singleton_t::instance().enable_log_level(LOG_FLAG(LF_WARN), true); 48 | if( flag_log_level >=4 ) singleton_t::instance().enable_log_level(LOG_FLAG(LF_INFO), true); 49 | if( flag_log_level >=5 ) singleton_t::instance().enable_log_level(LOG_FLAG(LF_TRACE), true); 50 | if( flag_log_level >=6 ) singleton_t::instance().enable_log_level(LOG_FLAG(LF_DEBUG), true); 51 | singleton_t::instance().set_print_screen_callback(screen_callback_); 52 | singleton_t::instance().set_print_file_callback(file_callback_); 53 | for(std::size_t i = 0 ; i < modules.size() ; i++) 54 | { 55 | singleton_t::instance().enable_log_module(modules[i].c_str(), true); 56 | } 57 | return singleton_t::instance().open(); 58 | } 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /chaos/log/log_misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_LOG_MISC_H_ 10 | #define _CHAOS_LOG_MISC_H_ 11 | 12 | /*! 13 | * @file log_misc.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.4.6 18 | * @brief log misc 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | #ifdef HAVE_CONFIG_H 27 | #include 28 | #endif 29 | 30 | #include 31 | #include 32 | 33 | /** 34 | #define ENABLE_LOG_FATAL 35 | #define ENABLE_LOG_ERROR 36 | #define ENABLE_LOG_WARN 37 | #define ENABLE_LOG_INFO 38 | #define ENABLE_LOG_TRACE 39 | #define ENABLE_LOG_DEBUG 40 | */ 41 | 42 | //! yunjie: log modules begin 43 | #ifndef THREAD_MODULE 44 | #define THREAD_MODULE "THREAD_MODULE" 45 | #endif 46 | 47 | #ifndef TASK_SERVICE_MODULE 48 | #define TASK_SERVICE_MODULE "TASK_SERVICE_MODULE" 49 | #endif 50 | 51 | #ifndef TIMER_MANAGER_MODULE 52 | #define TIMER_MANAGER_MODULE "TIMER_MANAGER_MODULE" 53 | #endif 54 | 55 | #ifndef IO_MULTIPLEX_MODULE 56 | #define IO_MULTIPLEX_MODULE "IO_MULTIPLEX_MODULE" 57 | #endif 58 | 59 | #ifndef ACCEPTOR_SERVICE_MODULE 60 | #define ACCEPTOR_SERVICE_MODULE "ACCEPTOR_SERVICE_MODULE" 61 | #endif 62 | 63 | #ifndef WORK_SERVICE_MODULE 64 | #define WORK_SERVICE_MODULE "WORK_SERVICE_MODULE" 65 | #endif 66 | 67 | #ifndef TCP_SERVICE_MODULE 68 | #define TCP_SERVICE_MODULE "TCP_SERVICE_MODULE" 69 | #endif 70 | 71 | #ifndef CONNECTION_MODULE 72 | #define CONNECTION_MODULE "CONNECTION_MODULE" 73 | #endif 74 | 75 | #ifndef HEART_BEAT_MOUDLE 76 | #define HEART_BEAT_MOUDLE "HEART_BEAT_MOUDLE" 77 | #endif 78 | 79 | #ifndef STATISTIC_MOUDLE 80 | #define STATISTIC_MOUDLE "STATISTIC_MOUDLE" 81 | #endif 82 | 83 | #ifndef CONNECTOR_SERVICE 84 | #define CONNECTOR_SERVICE "CONNECTOR_SERVICE" 85 | #endif 86 | 87 | //! yunjie: log modules end 88 | 89 | 90 | namespace chaos 91 | { 92 | 93 | namespace log 94 | { 95 | 96 | using namespace std; 97 | using namespace chaos::utility; 98 | 99 | #ifdef ENABLE_LOG_FATAL 100 | #define LOGFATAL(x) singleton_t::instance()._logfatal x 101 | #else 102 | #define LOGFATAL(x) {} 103 | #endif 104 | 105 | #ifdef ENABLE_LOG_ERROR 106 | #define LOGERROR(x) singleton_t::instance()._logerror x 107 | #else 108 | #define LOGERROR(x) {} 109 | #endif 110 | 111 | #ifdef ENABLE_LOG_WARN 112 | #define LOGWARN(x) singleton_t::instance()._logwarn x 113 | #else 114 | #define LOGWARN(x) {} 115 | #endif 116 | 117 | #ifdef ENABLE_LOG_INFO 118 | #define LOGINFO(x) singleton_t::instance()._loginfo x 119 | #else 120 | #define LOGINFO(x) {} 121 | #endif 122 | 123 | #ifdef ENABLE_LOG_TRACE 124 | #define LOGTRACE(x) singleton_t::instance()._logtrace x 125 | #else 126 | #define LOGTRACE(x) {} 127 | #endif 128 | 129 | #ifdef ENABLE_LOG_DEBUG 130 | #define LOGDEBUG(x) singleton_t::instance()._logdebug x 131 | #else 132 | #define LOGDEBUG(x) {} 133 | #endif 134 | 135 | 136 | int init_log(const std::string& path, 137 | const std::string& file, 138 | const int flag_print_file, 139 | const int flag_print_screen, 140 | const int flag_log_level, 141 | const std::vector& modules, 142 | log_t::print_screen_callback_t screen_callback_ = NULL, 143 | log_t::print_file_callback_t file_callback = NULL, 144 | const int max_line = 100000, 145 | const int maz_size = 5000000); 146 | 147 | } 148 | 149 | } 150 | 151 | #endif //! _CHAOS_LOG_MISC_H_ 152 | -------------------------------------------------------------------------------- /chaos/network/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -Wall -g -O2 -fPIC 2 | 3 | ## 安装根目录 4 | chaosdir=$(prefix)/chaos-@chaos_ver@ 5 | 6 | ## 安装库目录 7 | libdir=$(chaosdir)/lib 8 | 9 | lib_LIBRARIES = libchaos_network.a 10 | libchaos_network_a_LIBFLAGS = 11 | libchaos_network_a_LIBADD = 12 | 13 | libchaos_network_a_SOURCES = \ 14 | acceptor_service.cpp \ 15 | work_service_group.cpp \ 16 | connection.cpp \ 17 | tcp_service.cpp \ 18 | work_service.cpp \ 19 | msg_buffer.cpp \ 20 | buffer_list.cpp \ 21 | default_conn_strategy.cpp 22 | -------------------------------------------------------------------------------- /chaos/network/acceptor_service.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #include 10 | 11 | /*! 12 | * @file acceptor_service.cpp 13 | * @author yunjie.lu 14 | * @email lyjdamzwf@gmail.com 15 | * @weibo http://weibo.com/crazyprogramerlyj 16 | * @date 2012.4.11 17 | * @brief acceptor service 18 | * @last changed 19 | * 20 | */ 21 | 22 | namespace chaos 23 | { 24 | 25 | namespace network 26 | { 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /chaos/network/buffer_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_BUFFER_LIST_H_ 10 | #define _CHAOS_BUFFER_LIST_H_ 11 | 12 | /*! 13 | * @file buffer_list.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.6.23 18 | * @brief buffer list 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | using namespace std; 25 | 26 | #include 27 | 28 | #ifndef BUF_STAT 29 | #define BUF_STAT 1 30 | #endif 31 | 32 | namespace chaos 33 | { 34 | 35 | namespace network 36 | { 37 | 38 | #define DEFAULT_MAX_MOVE_BYTES (16*1024) 39 | 40 | class buffer_list_t 41 | { 42 | public: 43 | typedef list::iterator iterator_t; 44 | typedef list::const_iterator const_iterator_t; 45 | 46 | public: 47 | buffer_list_t(); 48 | 49 | //! yunjie: 当前read iterator指向的buffer数据地址 50 | const char* data() const 51 | { 52 | return m_read_it->data(); 53 | } 54 | 55 | //! yunjie: 当前read iterator指向的buffer的数据字节数 56 | uint32_t size() const 57 | { 58 | return m_read_it->size(); 59 | } 60 | 61 | //! yunjie: 当前read iterator指向的buffer的剩余可使 62 | //! 用空间字节数 63 | uint32_t remain_tail_capacity() const 64 | { 65 | return m_read_it->remain_tail_capacity(); 66 | } 67 | 68 | //! yunjie: 当前read iterator指向的buffer是否已经 69 | //! 没有任何可用空间 70 | bool is_full() const 71 | { 72 | return m_read_it->is_full(); 73 | } 74 | 75 | void set_buffer_max_limit(uint32_t size_limit_); 76 | 77 | int append( 78 | const char* data_, 79 | uint32_t size_ 80 | ); 81 | int recv_to_buffer(fd_t fd_, int& recv_ret_); 82 | void set_max_move_bytes(uint32_t bytes_); 83 | uint32_t drain_size(uint32_t size_); 84 | void clear(); 85 | 86 | string format_buffer_list_info() const; 87 | 88 | private: 89 | list m_buffer_list; 90 | iterator_t m_read_it; 91 | iterator_t m_write_it; 92 | uint32_t m_max_move_bytes; 93 | uint32_t m_buffer_max_limit; 94 | 95 | //! yunjie: 统计信息 96 | uint32_t m_the_biggest_buffer_size; 97 | uint32_t m_the_biggest_list; 98 | }; 99 | 100 | } 101 | 102 | } 103 | 104 | #endif //! _CHAOS_BUFFER_LIST_H_ 105 | -------------------------------------------------------------------------------- /chaos/network/default_conn_strategy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_DEFAULT_CONN_STRATEGY_H_ 10 | #define _CHAOS_DEFAULT_CONN_STRATEGY_H_ 11 | 12 | /*! 13 | * @file default_conn_strategy.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.4.17 18 | * @brief default conn strategy 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | namespace chaos 26 | { 27 | 28 | namespace network 29 | { 30 | 31 | typedef uint16_t header_cmd_t; 32 | typedef uint16_t header_ext_t; 33 | typedef uint32_t header_datalen_t; 34 | 35 | #define HEADER_SIZE (sizeof(header_cmd_t) + sizeof(header_ext_t) + sizeof(header_datalen_t)) 36 | 37 | struct packet_header_t 38 | { 39 | packet_header_t() 40 | : 41 | cmd(0), 42 | ext(0), 43 | data_len(0) 44 | { 45 | } 46 | 47 | header_cmd_t cmd; 48 | header_ext_t ext; 49 | header_datalen_t data_len; 50 | }; 51 | 52 | class default_conn_strategy_t : public connection_t 53 | { 54 | public: 55 | default_conn_strategy_t(); 56 | virtual ~default_conn_strategy_t(); 57 | 58 | protected: 59 | void on_read_complete(buffer_list_t& buffer_); 60 | 61 | void on_write_complete(uint32_t transferred_size_); 62 | 63 | int parse_packet(buffer_list_t& buffer_); 64 | 65 | virtual void handle_packet( 66 | const packet_header_t& packet_header_, 67 | const char* data_ptr_, 68 | uint32_t data_size_ 69 | ) = 0; 70 | 71 | void clear(); 72 | 73 | protected: 74 | char m_packet_header_buffer[HEADER_SIZE]; 75 | uint16_t m_header_readed_bytes; 76 | 77 | //! yunjie: 数据包是否被分割在两个buffer中 78 | bool m_is_split; 79 | vector m_packet_body_buffer; 80 | uint32_t m_body_readed_bytes; 81 | }; 82 | 83 | 84 | } 85 | 86 | } 87 | 88 | 89 | #endif //! _CHAOS_DEFAULT_CONN_STRATEGY_H_ 90 | -------------------------------------------------------------------------------- /chaos/network/network_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_NETWORK_CONFIG_H_ 10 | #define _CHAOS_NETWORK_CONFIG_H_ 11 | 12 | #include 13 | 14 | /*! 15 | * @file network_config.cpp 16 | * @author yunjie.lu 17 | * @email lyjdamzwf@gmail.com 18 | * @weibo http://weibo.com/crazyprogramerlyj 19 | * @date 2012.7.14 20 | * @brief network config 21 | * @last changed 22 | * 23 | */ 24 | 25 | using namespace chaos::utility; 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace network 31 | { 32 | 33 | struct network_config_t 34 | { 35 | network_config_t() 36 | : 37 | tcp_sndbuf_size(64 * 1024), 38 | tcp_rcvbuf_size(64 * 1024), 39 | max_send_buffer_size(16 * 1024), 40 | max_read_buffer_size(16 * 1024), 41 | is_enable_tcp_nodelay(false) 42 | { 43 | } 44 | 45 | //! yunjie: tcp协议栈的发送/接收缓冲区大小 46 | int32_t tcp_sndbuf_size; 47 | int32_t tcp_rcvbuf_size; 48 | 49 | //! yunjie: chaos应用层的发送/接收缓冲区最大限制 50 | int32_t max_send_buffer_size; 51 | int32_t max_read_buffer_size; 52 | 53 | //! yunjie: 是否打开TCP_NODELAY选项(关闭nagle) 54 | bool is_enable_tcp_nodelay; 55 | }; 56 | 57 | static network_config_t default_config; 58 | 59 | struct config_holder_t : private noncopyable_t 60 | { 61 | config_holder_t() 62 | : 63 | config_ptr(NULL) 64 | { 65 | } 66 | 67 | ~config_holder_t() 68 | { 69 | } 70 | 71 | void set_config(network_config_t* config_) 72 | { 73 | config_ptr = config_; 74 | } 75 | 76 | const network_config_t& operator *() 77 | { 78 | if (NULL == config_ptr) 79 | { 80 | return default_config; 81 | } 82 | else 83 | { 84 | return *config_ptr; 85 | } 86 | } 87 | 88 | private: 89 | network_config_t* config_ptr; 90 | }; 91 | 92 | } 93 | 94 | } 95 | 96 | #endif //! _CHAOS_NETWORK_CONFIG_H_ 97 | -------------------------------------------------------------------------------- /chaos/network/network_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_NETWORK_INC_H_ 10 | #define _CHAOS_NETWORK_INC_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #endif //! _CHAOS_NETWORK_INC_H_ 26 | -------------------------------------------------------------------------------- /chaos/network/network_tool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_NETWORK_TOOL_H_ 10 | #define _CHAOS_NETWORK_TOOL_H_ 11 | 12 | /*! 13 | * @file network_tool.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.4.11 18 | * @brief network tool 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace chaos 30 | { 31 | 32 | namespace network 33 | { 34 | 35 | class network_tool_t 36 | { 37 | public: 38 | static uint32_t ipv4_string_to_int(const string& ipv4_str_) 39 | { 40 | uint32_t ret = 0; 41 | ret = ntohl(inet_addr(ipv4_str_.c_str())); //! yunjie: inet_addr的返回值是已经经过网络字节顺序处理了 42 | 43 | return ret; 44 | } 45 | 46 | static int ipv4_int_to_string(uint32_t ipv4_int_, string& out_) 47 | { 48 | struct in_addr addr; 49 | memset(&addr, 0, sizeof(in_addr)); 50 | addr.s_addr = htonl(ipv4_int_); //! yunjie: 转换成网络字节序的大端模式 51 | out_ = inet_ntoa(addr); 52 | 53 | return 0; 54 | } 55 | 56 | static int make_socket_nonblocking(int socket_) 57 | { 58 | if (fcntl(socket_, F_SETFL, O_NONBLOCK) == -1) 59 | { 60 | return -1; 61 | } 62 | 63 | return 0; 64 | } 65 | }; 66 | 67 | } 68 | 69 | } 70 | 71 | #endif //! _CHAOS_NETWORK_TOOL_H_ 72 | -------------------------------------------------------------------------------- /chaos/network/tcp_service.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #include 10 | 11 | /*! 12 | * @file tcp_service.cpp 13 | * @author yunjie.lu 14 | * @email lyjdamzwf@gmail.com 15 | * @weibo http://weibo.com/crazyprogramerlyj 16 | * @date 2012.4.17 17 | * @brief tcp service 18 | * @last changed 19 | * 20 | */ 21 | 22 | namespace chaos 23 | { 24 | 25 | namespace network 26 | { 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /chaos/network/work_service_group.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #include 10 | 11 | /*! 12 | * @file work_service_group.cpp 13 | * @author yunjie.lu 14 | * @email lyjdamzwf@gmail.com 15 | * @weibo http://weibo.com/crazyprogramerlyj 16 | * @date 2012.4.14 17 | * @brief work service 18 | * @last changed 19 | * 20 | */ 21 | 22 | namespace chaos 23 | { 24 | 25 | namespace network 26 | { 27 | 28 | work_service_group_t::work_service_group_t() 29 | : 30 | task_service_group_t("work_service") //! yunjie: 调用父类构造, 初始化service name 31 | { 32 | 33 | } 34 | 35 | work_service_group_t::~work_service_group_t() 36 | { 37 | } 38 | 39 | void work_service_group_t::enable_conn_heart_beat(const conn_heart_beat_param_t& param_) 40 | { 41 | m_conn_heart_beat_param = param_; 42 | } 43 | 44 | int work_service_group_t::start( 45 | int task_service_num_, 46 | int thread_num_per_service_ 47 | ) 48 | { 49 | int ret = 0; 50 | if ((ret = task_service_group_t::start(task_service_num_, thread_num_per_service_))) 51 | { 52 | return ret; 53 | } 54 | 55 | if ( 56 | m_conn_heart_beat_param.timeout_flag 57 | && m_conn_heart_beat_param.timeout 58 | ) 59 | { 60 | for ( 61 | task_service_container_t::iterator it = m_task_service_group.begin(); 62 | it != m_task_service_group.end(); 63 | ++it 64 | ) 65 | { 66 | work_service_t* work_ptr = (work_service_t*)(*it); 67 | if (NULL != work_ptr) 68 | { 69 | work_ptr->start_heart_beat_service(m_conn_heart_beat_param); 70 | } 71 | } 72 | } 73 | 74 | return ret; 75 | } 76 | 77 | task_service_t* work_service_group_t::new_service() 78 | { 79 | return new work_service_t(m_service_name); 80 | } 81 | 82 | int work_service_group_t::async_broadcast( 83 | const packet_wrapper_t& msg_, 84 | broadcast_filter_t filter_ 85 | ) 86 | { 87 | for ( 88 | task_service_container_t::iterator it = m_task_service_group.begin(); 89 | it != m_task_service_group.end(); 90 | ++it 91 | ) 92 | { 93 | work_service_t* work_ptr = (work_service_t*)(*it); 94 | 95 | if (NULL != work_ptr) 96 | { 97 | work_ptr->async_broadcast(msg_, filter_); 98 | } 99 | } 100 | 101 | return 0; 102 | } 103 | 104 | int work_service_group_t::async_broadcast( 105 | const char* msg_, 106 | uint32_t size_, 107 | broadcast_filter_t filter_ 108 | ) 109 | { 110 | for ( 111 | task_service_container_t::iterator it = m_task_service_group.begin(); 112 | it != m_task_service_group.end(); 113 | ++it 114 | ) 115 | { 116 | work_service_t* work_ptr = (work_service_t*)(*it); 117 | if (NULL != work_ptr) 118 | { 119 | work_ptr->async_broadcast(msg_, size_, filter_); 120 | } 121 | } 122 | 123 | return 0; 124 | } 125 | 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /chaos/network/work_service_group.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_WORK_SERVICE_GROUP_H_ 10 | #define _CHAOS_WORK_SERVICE_GROUP_H_ 11 | 12 | /*! 13 | * @file work_service_group.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.4.14 18 | * @brief work service 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | namespace chaos 26 | { 27 | 28 | namespace network 29 | { 30 | 31 | class work_service_group_t : public task_service_group_t 32 | { 33 | public: 34 | work_service_group_t(); 35 | virtual ~work_service_group_t(); 36 | 37 | //! yunjie: enable_conn_heart_beat要在start之前调用才会生效 38 | void enable_conn_heart_beat(const conn_heart_beat_param_t& param_); 39 | 40 | int start( 41 | int task_service_num_, 42 | int thread_num_per_service_ = DEFAULT_THREAD_NUM_PER_SERVICE 43 | ); 44 | 45 | int async_broadcast( 46 | const packet_wrapper_t& msg_, 47 | broadcast_filter_t filter_ = NULL 48 | ); 49 | int async_broadcast( 50 | const char* msg_, 51 | uint32_t size_, 52 | broadcast_filter_t filter_ = NULL 53 | ); 54 | 55 | protected: 56 | //! yunjie: 父类的virtual方法 57 | task_service_t* new_service(); 58 | 59 | private: 60 | conn_heart_beat_param_t m_conn_heart_beat_param; 61 | }; 62 | 63 | } 64 | 65 | } 66 | 67 | #endif //! _CHAOS_WORK_SERVICE_GROUP_H_ 68 | -------------------------------------------------------------------------------- /chaos/statistic/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -Wall -g -O2 -fPIC 2 | 3 | chaosdir=$(prefix)/chaos-@chaos_ver@ 4 | libdir=$(chaosdir)/lib 5 | 6 | lib_LIBRARIES = libchaos_statistic.a 7 | libchaos_statistic_a_LIBFLAGS = 8 | libchaos_statistic_a_LIBADD = 9 | 10 | libchaos_statistic_a_SOURCES = \ 11 | statistic_service.cpp 12 | -------------------------------------------------------------------------------- /chaos/statistic/statistic_inc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_STATISTIC_INC_H_ 2 | #define _CHAOS_STATISTIC_INC_H_ 3 | 4 | #include 5 | 6 | #endif //! _CHAOS_STATISTIC_INC_H_ 7 | 8 | -------------------------------------------------------------------------------- /chaos/task_service/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -Wall -g -O2 -fPIC 2 | 3 | ## 安装根目录 4 | chaosdir=$(prefix)/chaos-@chaos_ver@ 5 | 6 | ## 安装库目录 7 | libdir=$(chaosdir)/lib 8 | 9 | lib_LIBRARIES = libchaos_task_service.a 10 | libchaos_task_service_a_LIBFLAGS = 11 | libchaos_task_service_a_LIBADD = 12 | 13 | libchaos_task_service_a_SOURCES = \ 14 | task_service.cpp \ 15 | timer_manager.cpp \ 16 | io_multiplex_handler.cpp \ 17 | task_service_group.cpp 18 | -------------------------------------------------------------------------------- /chaos/task_service/base_container.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_BASE_CONTAINER_H_ 10 | #define _CHAOS_BASE_CONTAINER_H_ 11 | 12 | /*! 13 | * @file base_container.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.4.2 18 | * @brief base container 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | namespace chaos 27 | { 28 | 29 | namespace task_service 30 | { 31 | 32 | using namespace chaos::thread; 33 | 34 | enum cond_failed_e 35 | { 36 | FETCH_BREAK = 0, 37 | FETCH_RETURN, 38 | FETCH_CONTINUE 39 | }; 40 | 41 | //! yunjie: CONTAINER模板参数是嵌套模板参数, 用class声明 42 | template class CONTAINER > 43 | class base_container_t 44 | { 45 | public: 46 | typedef CONTAINER container_t; 47 | typedef bool (*fetch_condition_t) (const ELEMENT&, void* ext_data_); 48 | 49 | struct cond_checker_t 50 | { 51 | cond_checker_t() 52 | : 53 | cond_func(NULL), 54 | ext_data(NULL), 55 | failed_op(FETCH_BREAK) 56 | { 57 | } 58 | 59 | cond_checker_t(fetch_condition_t func_, 60 | void* ext_data_, 61 | cond_failed_e failed_op_ 62 | ) 63 | : 64 | cond_func(func_), 65 | ext_data(ext_data_), 66 | failed_op(failed_op_) 67 | { 68 | } 69 | 70 | fetch_condition_t cond_func; 71 | void* ext_data; 72 | cond_failed_e failed_op; 73 | }; 74 | 75 | public: 76 | base_container_t() 77 | : 78 | m_inited(false), 79 | m_is_lock(false) 80 | { 81 | } 82 | 83 | virtual ~base_container_t() 84 | { 85 | } 86 | 87 | int initialize(bool is_lock_) 88 | { 89 | if (m_inited) 90 | { 91 | return -1; 92 | } 93 | 94 | m_is_lock = is_lock_; 95 | 96 | return 0; 97 | } 98 | 99 | virtual int fetch_task(CONTAINER& out_task_, 100 | uint32_t& out_all_task_num_, 101 | uint32_t fetch_count_, 102 | cond_checker_t* checker_ = NULL, 103 | task_prior_e prior_ = TASK_PRIOR_NORMAL 104 | ) = 0; 105 | 106 | virtual void clear(task_prior_e prior_ = TASK_PRIOR_NORMAL) = 0; 107 | 108 | bool is_lock() const 109 | { 110 | return m_is_lock; 111 | } 112 | 113 | protected: 114 | bool m_inited; 115 | bool m_is_lock; 116 | mutable mutex_t m_mutex; 117 | CONTAINER m_container_arr[TASK_PRIOR_COUNT]; 118 | }; 119 | 120 | } 121 | 122 | } 123 | 124 | #endif //! _CHAOS_TIMER_CONTAINER_H_ 125 | 126 | -------------------------------------------------------------------------------- /chaos/task_service/fast_msg_queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_FAST_MSG_QUEUE_H_ 10 | #define _CHAOS_FAST_MSG_QUEUE_H_ 11 | 12 | /*! 13 | * @file fast_msg_queue.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.25 18 | * @brief fast msg queue 19 | * 还存在BUG, 没有投入使用 20 | * @last changed 21 | * 22 | */ 23 | 24 | #include 25 | 26 | #include 27 | 28 | #include 29 | 30 | namespace chaos 31 | { 32 | 33 | namespace task_service 34 | { 35 | 36 | using namespace chaos::thread; 37 | 38 | template 39 | class fast_msg_queue_t 40 | { 41 | public: 42 | fast_msg_queue_t() 43 | { 44 | } 45 | 46 | virtual ~fast_msg_queue_t() 47 | { 48 | } 49 | 50 | public: 51 | void push(const T& element_) 52 | { 53 | if (m_ring_buffer.writeable()) 54 | { 55 | m_ring_buffer.put(element_); 56 | } 57 | else 58 | printf("can not put\n"); 59 | 60 | } 61 | 62 | T pop() 63 | { 64 | if (empty()) 65 | { 66 | return T(); 67 | } 68 | 69 | return m_ring_buffer.get(); 70 | } 71 | 72 | bool empty() const 73 | { 74 | return !m_ring_buffer.readable(); 75 | } 76 | 77 | int size() const 78 | { 79 | return m_ring_buffer.size(); 80 | } 81 | 82 | void clear() 83 | { 84 | m_ring_buffer.reset(); 85 | } 86 | 87 | private: 88 | ring_buffer_t m_ring_buffer; 89 | }; 90 | 91 | } 92 | 93 | } 94 | 95 | #endif //! _CHAOS_FAST_MSG_QUEUE_H_ 96 | -------------------------------------------------------------------------------- /chaos/task_service/ring_buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_RING_BUFFER_H_ 10 | #define _CHAOS_RING_BUFFER_H_ 11 | 12 | /*! 13 | * @file ring_buffer.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.25 18 | * @brief ring buffer 19 | * 仍存在BUG, 尚未投入使用 20 | * @last changed 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | namespace chaos 27 | { 28 | 29 | namespace task_service 30 | { 31 | 32 | using namespace chaos::thread; 33 | using namespace chaos::utility; 34 | 35 | #define RING_BUFFER_ELEMENT_COUNT (1000 * 100) 36 | 37 | template 38 | class ring_buffer_t : noncopyable_t 39 | { 40 | public: 41 | ring_buffer_t() 42 | : m_write_offset(0), 43 | m_read_offset(0), 44 | m_is_same_round(true) 45 | { 46 | } 47 | 48 | virtual ~ring_buffer_t() 49 | { 50 | } 51 | 52 | public: 53 | int put(const ElementType& element_) 54 | { 55 | if (!writeable()) 56 | { 57 | return -1; 58 | } 59 | 60 | m_buffer[m_write_offset] = element_; 61 | if (m_write_offset + 1 == RING_BUFFER_ELEMENT_COUNT) 62 | { 63 | m_write_offset = 0; 64 | m_is_same_round = false; 65 | } 66 | else 67 | { 68 | ++m_write_offset; 69 | } 70 | 71 | return 0; 72 | } 73 | 74 | ElementType get() 75 | { 76 | if (!readable()) 77 | { 78 | return ElementType(); 79 | } 80 | 81 | ElementType& element = m_buffer[m_read_offset]; 82 | if (m_read_offset + 1 == RING_BUFFER_ELEMENT_COUNT) 83 | { 84 | m_read_offset = 0; 85 | m_is_same_round = true; 86 | } 87 | else 88 | { 89 | ++m_read_offset; 90 | } 91 | 92 | return element; 93 | } 94 | 95 | bool writeable() const 96 | { 97 | return (m_is_same_round && m_write_offset >= m_read_offset) || (!m_is_same_round && m_write_offset < m_read_offset); 98 | } 99 | 100 | bool readable() const 101 | { 102 | return (m_is_same_round && m_read_offset < m_write_offset) || (!m_is_same_round); 103 | } 104 | 105 | int size() const 106 | { 107 | if (m_is_same_round) 108 | { 109 | return m_write_offset - m_read_offset; 110 | } 111 | else 112 | { 113 | return RING_BUFFER_ELEMENT_COUNT - m_read_offset + m_write_offset; 114 | } 115 | } 116 | 117 | void reset() 118 | { 119 | m_write_offset = 0; 120 | m_read_offset = 0; 121 | m_is_same_round = true; 122 | } 123 | 124 | private: 125 | int m_write_offset; 126 | int m_read_offset; 127 | bool m_is_same_round; 128 | ElementType m_buffer[RING_BUFFER_ELEMENT_COUNT]; 129 | }; 130 | 131 | } 132 | 133 | } 134 | 135 | 136 | #endif //! _CHAOS_RING_BUFFER_H_ 137 | -------------------------------------------------------------------------------- /chaos/task_service/task_service_define.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_TASK_SERVICE_DEFINE_H_ 10 | #define _CHAOS_TASK_SERVICE_DEFINE_H_ 11 | 12 | /*! 13 | * @file task_serviec_define.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.17 18 | * @brief task service define 19 | * @last changed 20 | * 21 | */ 22 | 23 | namespace chaos 24 | { 25 | 26 | namespace task_service 27 | { 28 | 29 | using namespace async_method; 30 | 31 | #define CHECK_LOCK(lock_flag, mutex) \ 32 | scope_mutex_holder_t mutex_holder; \ 33 | if (lock_flag) \ 34 | { \ 35 | mutex_holder.set_ptr_and_lock(&mutex); \ 36 | } 37 | 38 | enum task_prior_e 39 | { 40 | TASK_PRIOR_LOW = 0, 41 | TASK_PRIOR_NORMAL, 42 | TASK_PRIOR_HIGH, 43 | TASK_PRIOR_COUNT 44 | }; 45 | 46 | typedef async_method_t time_event_callback_t; 47 | 48 | } 49 | 50 | } 51 | 52 | 53 | #endif //! _CHAOS_TASK_SERVICE_DEFINE_H_ 54 | -------------------------------------------------------------------------------- /chaos/task_service/task_service_group.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_TASK_SERVICE_GROUP_H_ 10 | #define _CHAOS_TASK_SERVICE_GROUP_H_ 11 | 12 | /*! 13 | * @file task_service_group.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.4.15 18 | * @brief task service group 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace task_service 31 | { 32 | 33 | using namespace std; 34 | 35 | #define DEFAULT_THREAD_NUM_PER_SERVICE 1 36 | 37 | class task_service_group_t : private noncopyable_t 38 | { 39 | public: 40 | typedef vector task_service_container_t; 41 | 42 | task_service_group_t(const string& service_name_ = DEFAULT_SERVICE_NAME); 43 | virtual ~task_service_group_t(); 44 | 45 | 46 | virtual int start( 47 | int task_service_num_, 48 | int thread_num_per_service_ = DEFAULT_THREAD_NUM_PER_SERVICE 49 | ); 50 | virtual int stop(); 51 | task_service_t* operator[](uint32_t index_); 52 | int size() const 53 | { 54 | return m_task_service_group.size(); 55 | } 56 | 57 | //! yunjie: ext_data可以给子类实现用来判断post到哪一个task_service队列中去 58 | //! virtual int post(const async_method_t& async_method_, void* ext_data_ = NULL, task_prior_e prior_ = TASK_PRIOR_NORMAL, bool allow_exec_local_ = true) = 0; 59 | 60 | protected: 61 | //! yunjie: 子类可实现自己的new_service, 返回子类对象 62 | virtual task_service_t* new_service(); 63 | 64 | protected: 65 | bool m_started; 66 | string m_service_name; 67 | task_service_container_t m_task_service_group; 68 | 69 | //! yunjie: 被移除的service ptr容器, 会在析构函数时进行delete 70 | task_service_container_t m_removed_service_group; 71 | }; 72 | 73 | } 74 | 75 | } 76 | 77 | #endif //! _CHAOS_TASK_SERVICE_GROUP_H_ 78 | -------------------------------------------------------------------------------- /chaos/task_service/task_service_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_TASK_SERVICE_INC_H_ 10 | #define _CHAOS_TASK_SERVICE_INC_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #endif //! _CHAOS_TASK_SERVICE_INC_H_ 21 | -------------------------------------------------------------------------------- /chaos/task_service/timer_manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_TIME_MANAGER_H_ 10 | #define _CHAOS_TIME_MANAGER_H_ 11 | 12 | /*! 13 | * @file time_event.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.29 18 | * @brief time manager 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace chaos 34 | { 35 | 36 | namespace task_service 37 | { 38 | 39 | using namespace std; 40 | using namespace chaos::utility; 41 | using namespace chaos::thread; 42 | using namespace chaos::async_method; 43 | 44 | class timer_manager_t : private noncopyable_t 45 | { 46 | public: 47 | typedef timer_container_t time_heap_t; 48 | 49 | public: 50 | timer_manager_t(); 51 | virtual ~timer_manager_t(); 52 | 53 | public: 54 | int initialize(bool lock_ = false); 55 | 56 | void register_timer( 57 | uint32_t interval_, 58 | const time_event_callback_t& callback_, 59 | bool persist_ = false, 60 | time_t start_time_ = 0 61 | ); 62 | 63 | void exec(); 64 | 65 | void clear(); 66 | 67 | struct timeval get_cached_time(); 68 | 69 | void flush_time(); 70 | 71 | uint32_t size(); 72 | 73 | private: 74 | /** yunjie: 私有方法都不会涉及到锁竞争 */ 75 | 76 | void register_timer_i(const time_event_sptr_t& event_); 77 | 78 | void clear_i(); 79 | 80 | struct timeval get_cached_time_i(); 81 | 82 | void flush_time_i(); 83 | 84 | private: 85 | bool m_inited; 86 | time_heap_t m_time_heap; 87 | struct timeval m_cached_time; 88 | bool m_is_lock; 89 | mutex_t m_mutex; //! yunjie: 目前只用来在多线程驱动下的task_service中保护m_cached_time 90 | }; 91 | 92 | } 93 | 94 | } 95 | 96 | 97 | #endif //! _CHAOS_TIME_MANAGER_H_ 98 | -------------------------------------------------------------------------------- /chaos/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H_INCLUDED 2 | #define TEST_H_INCLUDED 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | template 9 | void loop_2_printf(vector& vector_) 10 | { 11 | for (typename vector::iterator it = vector_.begin(); it != vector_.end(); it++) 12 | { 13 | std::cout << *it << std::endl; 14 | } 15 | } 16 | 17 | #endif // TEST_H_INCLUDED 18 | -------------------------------------------------------------------------------- /chaos/thread/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -Wall -g -O2 -fPIC 2 | 3 | ## 安装根目录 4 | chaosdir=$(prefix)/chaos-@chaos_ver@ 5 | 6 | ## 安装库目录 7 | libdir=$(chaosdir)/lib 8 | 9 | lib_LIBRARIES = libchaos_thread.a 10 | libchaos_thread_a_LIBFLAGS = 11 | libchaos_thread_a_LIBADD = 12 | 13 | libchaos_thread_a_SOURCES = \ 14 | thread.cpp \ 15 | thread_group.cpp 16 | -------------------------------------------------------------------------------- /chaos/thread/condition.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyjdamzwf/chaos/95dff372002c4a04ee45b62568d46a4f50d793e4/chaos/thread/condition.h -------------------------------------------------------------------------------- /chaos/thread/mutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_MUTEX_H_ 10 | #define _CHAOS_MUTEX_H_ 11 | 12 | /*! 13 | * @file mutex.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.19 18 | * @brief mutex 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace thread 31 | { 32 | 33 | using namespace chaos::utility; 34 | 35 | class mutex_t : private noncopyable_t 36 | { 37 | public: 38 | mutex_t(int kind_ = PTHREAD_MUTEX_FAST_NP) 39 | { 40 | pthread_mutexattr_t attr; 41 | ::pthread_mutexattr_init(&attr); 42 | ::pthread_mutexattr_settype(&attr, kind_); 43 | ::pthread_mutex_init(&m_mutex, &attr); 44 | } 45 | 46 | ~mutex_t() 47 | { 48 | ::pthread_mutex_destroy(&m_mutex); 49 | } 50 | 51 | inline void lock() 52 | { 53 | ::pthread_mutex_lock(&m_mutex); 54 | } 55 | 56 | inline void unlock() 57 | { 58 | ::pthread_mutex_unlock(&m_mutex); 59 | } 60 | 61 | pthread_mutex_t * get_mutex() 62 | { 63 | return &m_mutex; 64 | } 65 | 66 | private: 67 | pthread_mutex_t m_mutex; 68 | }; 69 | 70 | class scope_mutex_lock_t : private noncopyable_t 71 | { 72 | public: 73 | 74 | scope_mutex_lock_t(mutex_t &m_) : m_lock(m_) 75 | { 76 | m_lock.lock(); 77 | } 78 | 79 | ~scope_mutex_lock_t() 80 | { 81 | m_lock.unlock(); 82 | } 83 | 84 | private: 85 | mutex_t& m_lock; 86 | }; 87 | 88 | class scope_mutex_holder_t : private noncopyable_t 89 | { 90 | public: 91 | scope_mutex_holder_t() 92 | : 93 | m_lock_ptr(NULL) 94 | { 95 | } 96 | 97 | ~scope_mutex_holder_t() 98 | { 99 | if (NULL != m_lock_ptr) 100 | { 101 | m_lock_ptr->unlock(); 102 | } 103 | } 104 | 105 | void set_ptr_and_lock(mutex_t* ptr_) 106 | { 107 | if (NULL == ptr_) 108 | { 109 | return; 110 | } 111 | 112 | if (NULL != m_lock_ptr) 113 | { 114 | m_lock_ptr->unlock(); 115 | } 116 | 117 | m_lock_ptr = ptr_; 118 | m_lock_ptr->lock(); 119 | } 120 | 121 | void reset_and_unlock() 122 | { 123 | if (NULL == m_lock_ptr) 124 | { 125 | return; 126 | } 127 | 128 | m_lock_ptr->unlock(); 129 | m_lock_ptr = NULL; 130 | } 131 | 132 | bool is_lock() const 133 | { 134 | return (NULL != m_lock_ptr); 135 | } 136 | 137 | private: 138 | mutex_t* m_lock_ptr; 139 | 140 | }; 141 | 142 | } 143 | 144 | } 145 | 146 | #endif //! _CHAOS_MUTEX_H_ 147 | 148 | -------------------------------------------------------------------------------- /chaos/thread/rwlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_RWLOCK_H_ 10 | #define _CHAOS_RWLOCK_H_ 11 | 12 | /*! 13 | * @file rwlock.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.19 18 | * @brief rwlock 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace thread 31 | { 32 | 33 | class rwlock_t : private noncopyable_t 34 | { 35 | public: 36 | unsigned int rd_count; 37 | unsigned int wr_count; 38 | 39 | rwlock_t() : rd_count(0), wr_count(0) 40 | { 41 | ::pthread_rwlock_init(&m_rwlock, NULL); 42 | } 43 | 44 | ~rwlock_t() 45 | { 46 | ::pthread_rwlock_destroy(&m_rwlock); 47 | } 48 | 49 | void rdlock() 50 | { 51 | ::pthread_rwlock_rdlock(&m_rwlock); 52 | rd_count++; 53 | } 54 | 55 | void wrlock() 56 | { 57 | ::pthread_rwlock_wrlock(&m_rwlock); 58 | rd_count++; 59 | wr_count++; 60 | } 61 | 62 | void unlock() 63 | { 64 | ::pthread_rwlock_unlock(&m_rwlock); 65 | rd_count--; 66 | } 67 | 68 | private: 69 | pthread_rwlock_t m_rwlock; 70 | }; 71 | 72 | class scope_rdlock_t : private noncopyable_t 73 | { 74 | public: 75 | scope_rdlock_t(rwlock_t &lock_) : m_rwlock(lock_) 76 | { 77 | m_rwlock.rdlock(); 78 | } 79 | 80 | ~scope_rdlock_t() 81 | { 82 | m_rwlock.unlock(); 83 | } 84 | private: 85 | rwlock_t& m_rwlock; 86 | }; 87 | 88 | class scope_wrlock_t : private noncopyable_t 89 | { 90 | public: 91 | scope_wrlock_t(rwlock_t &lock_) : m_rwlock(lock_) 92 | { 93 | m_rwlock.wrlock(); 94 | } 95 | 96 | ~scope_wrlock_t() 97 | { 98 | m_rwlock.unlock(); 99 | } 100 | private: 101 | rwlock_t& m_rwlock; 102 | }; 103 | 104 | } 105 | 106 | } 107 | 108 | #endif //! _CHAOS_RWLOCK_H_ 109 | 110 | -------------------------------------------------------------------------------- /chaos/thread/spinlock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_SPINLOCK_H_ 10 | #define _CHAOS_SPINLOCK_H_ 11 | 12 | /*! 13 | * @file spinlock.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.19 18 | * @brief spinlock 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace thread 31 | { 32 | 33 | using namespace chaos::utility; 34 | 35 | class spin_lock_t : private noncopyable_t 36 | { 37 | public: 38 | spin_lock_t() 39 | { 40 | //! pthread_spinlinattr_t attr; 41 | //! ::pthread_spinattr_init(&attr); 42 | //! ::pthread_mutexattr_settype(&attr, kind_); 43 | ::pthread_spin_init(&m_spinlock, 0); 44 | } 45 | 46 | ~spin_lock_t() 47 | { 48 | ::pthread_spin_destroy(&m_spinlock); 49 | } 50 | 51 | inline void lock() 52 | { 53 | ::pthread_spin_lock(&m_spinlock); 54 | } 55 | 56 | inline void unlock() 57 | { 58 | ::pthread_spin_unlock(&m_spinlock); 59 | } 60 | 61 | pthread_spinlock_t* get_spinlock() 62 | { 63 | return &m_spinlock; 64 | } 65 | 66 | private: 67 | pthread_spinlock_t m_spinlock; 68 | }; 69 | 70 | class scope_spin_lock_t : private noncopyable_t 71 | { 72 | public: 73 | 74 | scope_spin_lock_t(spin_lock_t& m_) : m_lock(m_) 75 | { 76 | m_lock.lock(); 77 | } 78 | 79 | ~scope_spin_lock_t() 80 | { 81 | m_lock.unlock(); 82 | } 83 | 84 | private: 85 | spin_lock_t& m_lock; 86 | }; 87 | 88 | } 89 | 90 | } 91 | 92 | #endif //! _CHAOS_SPINLOCK_H_ 93 | 94 | 95 | -------------------------------------------------------------------------------- /chaos/thread/thread.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyjdamzwf/chaos/95dff372002c4a04ee45b62568d46a4f50d793e4/chaos/thread/thread.h -------------------------------------------------------------------------------- /chaos/thread/thread_group.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | /*! 17 | * @file thread_group.cpp 18 | * @author yunjie.lu 19 | * @email lyjdamzwf@gmail.com 20 | * @weibo http://weibo.com/crazyprogramerlyj 21 | * @date 2012.3.18 22 | * @brief thread group 23 | * @last changed 24 | * 25 | */ 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace thread 31 | { 32 | 33 | thread_group_t::thread_group_t() : m_thd_vt() 34 | { 35 | } 36 | 37 | thread_group_t::~thread_group_t() 38 | { 39 | join_all(); 40 | } 41 | 42 | void thread_group_t::add(thread_t* thread_) 43 | { 44 | LOGTRACE((THREAD_MODULE, "thread_grout_t::add thread-[%] begin.", thread_->get_thread_name().c_str())); 45 | 46 | container_t::iterator it = std::find(m_thd_vt.begin(), m_thd_vt.end(), thread_); 47 | if (it == m_thd_vt.end()) 48 | m_thd_vt.push_back(thread_); 49 | 50 | LOGTRACE((THREAD_MODULE, "thread_grout_t::add thread-[%] end.", thread_->get_thread_name().c_str())); 51 | } 52 | 53 | thread_t* thread_group_t::get_by_index(size_type_t idx_) 54 | { 55 | if (idx_ >= m_thd_vt.size()) 56 | return NULL; 57 | else 58 | return m_thd_vt[idx_]; 59 | } 60 | 61 | thread_t* thread_group_t::operator[](size_type_t idx_) 62 | { 63 | if (idx_ >= m_thd_vt.size()) 64 | return NULL; 65 | else 66 | return m_thd_vt[idx_]; 67 | } 68 | 69 | void thread_group_t::join_all() 70 | { 71 | LOGTRACE((THREAD_MODULE, "thread_group_t::join_all() begin")); 72 | 73 | while (!m_thd_vt.empty()) 74 | { 75 | thread_t* thread = m_thd_vt.back(); 76 | if (thread) 77 | { 78 | thread->final(); 79 | thread->join(); 80 | SAFE_DELETE(thread); 81 | } 82 | m_thd_vt.pop_back(); 83 | } 84 | 85 | LOGTRACE((THREAD_MODULE, "thread_group_t::join_all() end")); 86 | } 87 | 88 | bool thread_group_t::check_is_self(pthread_t pid_) 89 | { 90 | for (container_t::iterator it = m_thd_vt.begin(); 91 | it != m_thd_vt.end(); 92 | ++it 93 | ) 94 | { 95 | if ((*it)->get_thread_id() == pid_) 96 | { 97 | return true; 98 | } 99 | } 100 | 101 | return false; 102 | } 103 | 104 | void thread_group_t::exec_all(callback_t &cb_) 105 | { 106 | for(container_t::iterator it = m_thd_vt.begin(); it != m_thd_vt.end(); it++) 107 | { 108 | cb_.exec(*it); 109 | } 110 | } 111 | 112 | } 113 | 114 | } 115 | 116 | -------------------------------------------------------------------------------- /chaos/thread/thread_group.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_THREAD_GROUP_H_ 10 | #define _CHAOS_THREAD_GROUP_H_ 11 | 12 | /*! 13 | * @file thread_group.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.3.18 18 | * @brief thread group 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | namespace chaos 26 | { 27 | 28 | namespace thread 29 | { 30 | 31 | class thread_group_t: private noncopyable_t 32 | { 33 | public: 34 | struct callback_t 35 | { 36 | virtual void exec(thread_t* thd_) = 0; 37 | virtual ~callback_t(){}; 38 | }; 39 | 40 | thread_group_t(); 41 | virtual ~thread_group_t(); 42 | 43 | typedef std::vector container_t; 44 | typedef container_t::size_type size_type_t; 45 | 46 | void add(thread_t *t); 47 | thread_t* get_by_index(size_type_t idx_); 48 | thread_t* operator[](size_type_t idx_); 49 | 50 | void join_all(); 51 | void exec_all(callback_t& cb_); 52 | 53 | bool check_is_self(pthread_t pid_); 54 | 55 | const size_type_t size() const 56 | { 57 | return m_thd_vt.size(); 58 | } 59 | 60 | private: 61 | container_t m_thd_vt; //! 线程向量 62 | }; 63 | 64 | } 65 | 66 | } 67 | 68 | #endif //! _CHAOS_THREAD_GROUP_H_ 69 | -------------------------------------------------------------------------------- /chaos/thread/thread_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_THREAD_INC_H_ 10 | #define _CHAOS_THREAD_INC_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | 20 | #endif //! _CHAOS_THREAD_INC_H_ 21 | -------------------------------------------------------------------------------- /chaos/utility/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS = -Wall -g -O2 -fPIC 2 | 3 | ## 安装根目录 4 | chaosdir=$(prefix)/chaos-@chaos_ver@ 5 | 6 | ## 安装库目录 7 | libdir=$(chaosdir)/lib 8 | 9 | lib_LIBRARIES = libchaos_utility.a 10 | libchaos_utility_a_LIBFLAGS = 11 | libchaos_utility_a_LIBADD = 12 | 13 | libchaos_utility_a_SOURCES = \ 14 | processor_helper.cpp \ 15 | random.cpp \ 16 | signal_handler.cpp \ 17 | arg_helper.cpp 18 | -------------------------------------------------------------------------------- /chaos/utility/arg_helper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | /*! 15 | * @file arg_helper.cpp 16 | * @author yunjie.lu 17 | * @email lyjdamzwf@gmail.com 18 | * @weibo http://weibo.com/crazyprogramerlyj 19 | * @date 2012.6.2 20 | * @brief arg helper 21 | * @last changed 22 | * 23 | */ 24 | 25 | namespace chaos 26 | { 27 | 28 | namespace utility 29 | { 30 | 31 | int arg_helper_t::parse_arg( 32 | arg_option_t* options_, 33 | int option_count_, 34 | char** argv_, 35 | int argc_, 36 | arg_pair_t** ret_arr_, 37 | int* ret_count_ 38 | ) 39 | { 40 | int ret = 0; 41 | 42 | if (NULL == options_) 43 | { 44 | goto PARSE_ERR; 45 | } 46 | 47 | m_arg_options_ptr = options_; 48 | *ret_arr_ = m_pair_ret; 49 | *ret_count_ = 0; 50 | 51 | //! yunjie: argv_[0] 是二进制文件名 52 | for (int i = 1; i < argc_; ++i) 53 | { 54 | bool is_match = false; 55 | char* cur_arg = argv_[i]; 56 | int cur_arg_index = i; 57 | 58 | //! yunjie: 进行匹配 59 | for (int j = 0 ; j < option_count_; ++j) 60 | { 61 | arg_option_t cur_option = options_[j]; 62 | 63 | if (strlen(cur_arg) <= 1) 64 | { 65 | goto PARSE_ERR; 66 | } 67 | 68 | if ('-' != cur_arg[0]) 69 | { 70 | goto PARSE_ERR; 71 | } 72 | 73 | //! yunjie: 去掉参数前的'-' 74 | if (0 == strcmp(&cur_arg[1], cur_option.name)) 75 | { 76 | if (cur_option.has_val) 77 | { 78 | if (cur_arg_index + 1 >= argc_) 79 | { 80 | goto PARSE_ERR; 81 | } 82 | 83 | //! yunjie: 下次取命令行参数要跳过该次参数对应的值 84 | ++i; 85 | } 86 | 87 | m_pair_ret[*ret_count_].arg_index = j; 88 | m_pair_ret[*ret_count_].arg_val = cur_option.has_val ? argv_[cur_arg_index + 1] : NULL; 89 | is_match = true; 90 | ++*ret_count_; 91 | 92 | if (NULL != cur_option.handler) 93 | { 94 | cur_option.handler( 95 | j, 96 | cur_option.name, 97 | cur_option.has_val ? argv_[cur_arg_index + 1] : NULL 98 | ); 99 | break; 100 | } 101 | } 102 | } 103 | 104 | if (!is_match) 105 | { 106 | ret = -1; 107 | } 108 | } 109 | 110 | return ret; 111 | 112 | PARSE_ERR: 113 | ret = -1; 114 | 115 | return ret; 116 | } 117 | 118 | void arg_helper_t::show_args( 119 | arg_option_t* options_, 120 | int option_count_ 121 | ) 122 | { 123 | printf("---------------- show args begin ----------------\n"); 124 | 125 | for (int j = 0 ; j < option_count_; ++j) 126 | { 127 | arg_option_t cur_option = options_[j]; 128 | printf("-%s %s\n", cur_option.name, cur_option.description); 129 | } 130 | 131 | printf("---------------- show args end ----------------\n\n"); 132 | } 133 | 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /chaos/utility/arg_helper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_ARG_HELPER_H_ 10 | #define _CHAOS_ARG_HELPER_H_ 11 | 12 | /*! 13 | * @file arg_helper.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.6.2 18 | * @brief arg helper 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | using namespace std; 25 | 26 | namespace chaos 27 | { 28 | 29 | namespace utility 30 | { 31 | 32 | typedef void (*arg_handler_t) (int, const char*, const char*); 33 | 34 | struct arg_option_t 35 | { 36 | arg_option_t() 37 | : 38 | name(NULL), 39 | description(NULL), 40 | has_val(false), 41 | handler(NULL) 42 | { 43 | } 44 | 45 | arg_option_t(const char* name_, const char* des_, bool has_val_, arg_handler_t handler_) 46 | : 47 | name(name_), 48 | description(des_), 49 | has_val(has_val_), 50 | handler(handler_) 51 | { 52 | } 53 | 54 | const char* name; 55 | const char* description; 56 | bool has_val; 57 | arg_handler_t handler; 58 | }; 59 | 60 | struct arg_pair_t 61 | { 62 | arg_pair_t() 63 | : 64 | arg_index(0), 65 | arg_val(NULL) 66 | { 67 | } 68 | 69 | int arg_index; 70 | char* arg_val; 71 | }; 72 | 73 | class arg_helper_t 74 | { 75 | public: 76 | arg_helper_t() 77 | : 78 | m_arg_options_ptr(NULL) 79 | { 80 | } 81 | 82 | int parse_arg( 83 | arg_option_t* options_, 84 | int option_count_, 85 | char** argv_, 86 | int argc_, 87 | arg_pair_t** ret_arr_, 88 | int* ret_count_ 89 | ); 90 | 91 | void show_args( 92 | arg_option_t* options_, 93 | int option_count_ 94 | ); 95 | 96 | private: 97 | arg_option_t* m_arg_options_ptr; 98 | arg_pair_t m_pair_ret[255]; 99 | }; 100 | 101 | } 102 | 103 | } 104 | 105 | #endif //! _CHAOS_ARG_HELPER_H_ 106 | -------------------------------------------------------------------------------- /chaos/utility/atomic_val.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _ATOMICVAL_H_ 10 | #define _ATOMICVAL_H_ 11 | 12 | /*! 13 | * @file atomic_val.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief atomic val 19 | * @last changed 20 | * 21 | */ 22 | 23 | #ifdef _WIN32 24 | #include 25 | #endif 26 | 27 | #include 28 | 29 | namespace chaos 30 | { 31 | 32 | namespace utility 33 | { 34 | 35 | 36 | template 37 | class atomic_val_t 38 | { 39 | public: 40 | atomic_val_t(): m_val(0) {} 41 | explicit atomic_val_t(T val): 42 | #ifdef _WIN32 43 | m_val((Int32)val) 44 | #else 45 | m_val((size_t)val) 46 | #endif 47 | {} 48 | atomic_val_t(const atomic_val_t& other): m_val(other._val) {} 49 | inline operator T() const 50 | { 51 | #ifdef _WIN32 52 | return (const T)m_val; 53 | #else 54 | return (const T)__sync_fetch_and_xor((volatile size_t *)&m_val, 0); 55 | #endif 56 | } 57 | inline T value() const { return *this; } 58 | inline atomic_val_t& operator=(T val) 59 | { 60 | #ifdef _WIN32 61 | InterlockedExchange((volatile LONG *)&m_val, (LONG)val); 62 | #else 63 | size_t oldval; 64 | do { 65 | oldval = __sync_fetch_and_xor((volatile size_t *)&m_val, 0); 66 | } while(!__sync_bool_compare_and_swap(&m_val, oldval, val)); 67 | #endif 68 | return *this; 69 | } 70 | inline atomic_val_t& operator=(const atomic_val_t& other) 71 | { 72 | return (*this) = other.value(); 73 | } 74 | inline T operator ++ () 75 | { 76 | #ifdef _WIN32 77 | return (T)InterlockedIncrement(&m_val); 78 | #else 79 | return (T)__sync_add_and_fetch(&m_val, 1); 80 | #endif 81 | } 82 | inline T operator ++ (int) 83 | { 84 | #ifdef _WIN32 85 | T result = m_val; 86 | InterlockedIncrement(&m_val); 87 | return result; 88 | #else 89 | return (T)__sync_fetch_and_add(&m_val, 1); 90 | #endif 91 | } 92 | inline T operator -- () 93 | { 94 | #ifdef _WIN32 95 | return (T)InterlockedDecrement(&m_val); 96 | #else 97 | return (T)__sync_sub_and_fetch(&m_val, 1); 98 | #endif 99 | } 100 | inline T operator -- (int) 101 | { 102 | #ifdef _WIN32 103 | T result = m_val; 104 | InterlockedDecrement(&m_val); 105 | return result; 106 | #else 107 | return (T)__sync_fetch_and_sub(&m_val, 1); 108 | #endif 109 | } 110 | inline T operator += (T n) 111 | { 112 | #ifdef _WIN32 113 | InterlockedExchangeAdd(&m_val, n); 114 | return _val; 115 | #else 116 | return (T)__sync_fetch_and_add(&m_val, n); 117 | #endif 118 | } 119 | inline T operator -= (T n) 120 | { 121 | #ifdef _WIN32 122 | InterlockedExchangeAdd(&m_val, -n); 123 | return _val; 124 | #else 125 | return (T)__sync_fetch_and_sub(&m_val, n); 126 | #endif 127 | } 128 | 129 | private: 130 | #ifdef _WIN32 131 | volatile LONG m_val; 132 | #else 133 | volatile size_t m_val; 134 | #endif 135 | }; 136 | 137 | } 138 | 139 | } 140 | 141 | #endif // _ATOMICVAL_H_ 142 | -------------------------------------------------------------------------------- /chaos/utility/itoa.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _ITOA_H_ 10 | #define _ITOA_H_ 11 | 12 | /*! 13 | * @file itoa.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | 24 | #include 25 | #include 26 | 27 | namespace chaos 28 | { 29 | 30 | namespace utility 31 | { 32 | 33 | template 34 | std::string itoa(Type num_, int dec_ = 10) 35 | { 36 | char buffer[256]; 37 | char* p = buffer; 38 | 39 | do 40 | { 41 | *p++ = "0123456789"[num_ % dec_]; 42 | num_ /= dec_; 43 | if (static_cast(p - buffer) > sizeof(buffer)) return ""; 44 | } while (num_); 45 | 46 | buffer[p - buffer] = 0; 47 | 48 | int i, j; 49 | for (i = 0, j = (int)strlen(buffer) - 1; i < j; i++, j--) 50 | { 51 | buffer[j] = buffer[i] + buffer[j]; 52 | buffer[i] = buffer[j] - buffer[i]; 53 | buffer[j] = buffer[j] - buffer[i]; 54 | } 55 | 56 | return buffer; 57 | } 58 | 59 | } 60 | 61 | } 62 | 63 | #endif // _ITOA_H_ 64 | -------------------------------------------------------------------------------- /chaos/utility/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_CMALLOC_H_ 10 | #define _CHAOS_CMALLOC_H_ 11 | 12 | /*! 13 | * @file chaos_malloc.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 2012.10.23 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | namespace chaos 26 | { 27 | 28 | namespace utility 29 | { 30 | 31 | #define chaos_malloc je_malloc 32 | #define chaos_callos je_calloc 33 | #define chaos_realloc je_realloc 34 | #define chaos_free je_free 35 | #define chaos_rallocm je_rallocm 36 | 37 | static const size_t jemalloc_min_in_place_expandable = 4096; 38 | 39 | inline size_t align_to_jesize(size_t size_) 40 | { 41 | if (size_ <= 64) 42 | { 43 | return 64; 44 | } 45 | 46 | if (size_ <= 512) 47 | { 48 | return (size_ + 63) & ~size_t(63); 49 | } 50 | 51 | if (size_ <= 3840) 52 | { 53 | return (size_ + 255) & ~size_t(255); 54 | } 55 | 56 | if (size_ <= 4072 * 1024) 57 | { 58 | return (size_ + 4095) & ~size_t(4095); 59 | } 60 | 61 | return (size_ + 4194303) & ~size_t(4194303); 62 | } 63 | 64 | #include 65 | 66 | template 67 | inline T* construct() 68 | { 69 | T* ptr = (T*)chaos_malloc(sizeof(T)); 70 | 71 | if (NULL != ptr) 72 | { 73 | try 74 | { 75 | new (ptr) T(); 76 | } 77 | catch (...) 78 | { 79 | if (NULL != ptr) { chaos_free(ptr); ptr = NULL; } 80 | } 81 | } 82 | 83 | return ptr; 84 | } 85 | 86 | template 87 | inline void destroy(T* t_) 88 | { 89 | if (NULL != t_) 90 | { 91 | t_->~T(); 92 | chaos_free(t_); 93 | } 94 | } 95 | 96 | } 97 | 98 | } 99 | 100 | #endif /* _CHAOS_CMALLOC_H_ */ 101 | -------------------------------------------------------------------------------- /chaos/utility/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_MISC_H_ 10 | #define _CHAOS_MISC_H_ 11 | 12 | #include 13 | 14 | namespace chaos 15 | { 16 | 17 | namespace utility 18 | { 19 | 20 | #define SAFE_DELETE(x) { if (NULL != x) { delete(x); (x) = NULL; } } 21 | #define SAFE_DELETE_ARR(x) { if (NULL != x) { delete [] (x); (x) = NULL; } } 22 | 23 | #define GET_LONG_HIGH_PART(value) (value >> 32) 24 | #define GET_LONG_LOW_PART(value) (value & 0xFFFFFFFF) 25 | #define PARSE_TO_LONG(high, low) (uint64_t)(((uint64_t)high) << 32 | low) 26 | 27 | #define RED_COLOR_HEAD "\033[0;31m" 28 | #define YELLOW_COLOR_HEAD "\033[1;33m" 29 | #define GREEN_COLOR_HEAD "\033[32m" 30 | #define NONE_COLOR_HEAD "\033[0m" 31 | #define COLOR_END "\033[0m" 32 | 33 | #define STRERR strerror(errno) 34 | 35 | #define EXCEPTION_BEGIN \ 36 | try \ 37 | { 38 | 39 | #define EXCEPTION_END(module, ext) \ 40 | } \ 41 | catch (std::exception& e) \ 42 | { \ 43 | LOGWARN((module, "%s, %s, exception:[%s]", __PRETTY_FUNCTION__, ext, e.what())); \ 44 | } \ 45 | catch (...) \ 46 | { \ 47 | LOGWARN((module, "%s, %s, unknown exception", __PRETTY_FUNCTION__, ext)); \ 48 | } 49 | 50 | } 51 | 52 | } 53 | 54 | #endif //! _CHAOS_MISC_H_ 55 | -------------------------------------------------------------------------------- /chaos/utility/noncopyable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_NONCOPYALBE_H_ 10 | #define _CHAOS_NONCOPYALBE_H_ 11 | 12 | /** 13 | * @file noncopyable.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | namespace chaos 24 | { 25 | 26 | namespace utility 27 | { 28 | 29 | class noncopyable_t 30 | { 31 | protected: 32 | noncopyable_t() {} 33 | ~noncopyable_t() {} 34 | 35 | //! yunjie: 将拷贝构造和赋值函数 设置为私有 36 | private: 37 | noncopyable_t( const noncopyable_t& ); 38 | const noncopyable_t& operator = ( const noncopyable_t& ); 39 | }; 40 | 41 | } 42 | 43 | } 44 | 45 | #endif //! _CHAOS_NONCOPYALBE_H_ 46 | -------------------------------------------------------------------------------- /chaos/utility/processor_helper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | /** 10 | * @file processor_helper.cpp 11 | * @author yunjie.lu 12 | * @email lyjdamzwf@gmail.com 13 | * @weibo http://weibo.com/crazyprogramerlyj 14 | * @date 15 | * @brief 16 | * @last changed 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | using namespace std; 32 | 33 | #include 34 | 35 | namespace chaos 36 | { 37 | 38 | namespace utility 39 | { 40 | 41 | //! daemonize the current process 42 | void processor_helper_t::daemonize() 43 | { 44 | pid_t pid, sid; 45 | 46 | if ((pid = fork()) < 0) 47 | { 48 | cerr<<"processor_helper_t::daemonize fork failed:"<= 0; --i) 71 | { 72 | (void)close(i); 73 | } 74 | //! dup the base io 75 | int fd_dev = open("/dev/null", O_RDWR); 76 | (void)dup(fd_dev); 77 | (void)dup(fd_dev); 78 | } 79 | 80 | } 81 | 82 | } 83 | 84 | 85 | -------------------------------------------------------------------------------- /chaos/utility/processor_helper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _PROCESSOR_HELPER_H_ 10 | #define _PROCESSOR_HELPER_H_ 11 | 12 | /** 13 | * @file processor_helper.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | namespace chaos 27 | { 28 | 29 | namespace utility 30 | { 31 | 32 | class processor_helper_t 33 | { 34 | public: 35 | //! yunjie: daemonize the current process 36 | static void daemonize(); 37 | 38 | }; 39 | 40 | } 41 | 42 | } 43 | 44 | #endif //! _PROCESSOR_HELPER_H_ 45 | 46 | -------------------------------------------------------------------------------- /chaos/utility/random.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | /** 10 | * @file random.cpp 11 | * @author yunjie.lu 12 | * @email lyjdamzwf@gmail.com 13 | * @weibo http://weibo.com/crazyprogramerlyj 14 | * @date 15 | * @brief 16 | * @last changed 17 | * 18 | */ 19 | 20 | #include 21 | 22 | namespace chaos 23 | { 24 | 25 | namespace utility 26 | { 27 | 28 | rand_gen_t rand_gen = rand_gen_t((unsigned)time(NULL)); 29 | 30 | //! yunjie: 随机范围 - [start_, end_) 31 | uint32_t rand_gen_t::get_rand(uint32_t start_, uint32_t end_) 32 | { 33 | return (uint32_t)((end_ - start_) * rand_gen.rand_double()) + start_; 34 | } 35 | 36 | int rand_gen_t::rand_str(char * dest_, uint32_t len_) 37 | { 38 | for (uint32_t i = 0; i < len_; ++i) 39 | { 40 | int x = rand_gen_t::get_rand(0, sizeof(CCH) - 1); 41 | dest_[i] = CCH[x]; 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | int rand_gen_t::rand_str(string& dest_, uint32_t len_) 48 | { 49 | dest_ = ""; 50 | dest_.reserve(len_ + 1); 51 | for (uint32_t i = 0; i < len_; ++i) 52 | { 53 | int x = rand_gen_t::get_rand(0, sizeof(CCH) - 1); 54 | dest_ += CCH[x]; 55 | } 56 | 57 | return 0; 58 | } 59 | 60 | bool rand_gen_t::calc_probability(int rate_) 61 | { 62 | if (rate_ <= 0) 63 | { 64 | return false; 65 | } 66 | else if (rate_ >= 100) 67 | { 68 | return true; 69 | } 70 | 71 | int val = rand_gen_t::get_rand(1, 100); 72 | bool ret = val <= rate_ ? true : false ; 73 | 74 | return ret; 75 | } 76 | 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /chaos/utility/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_RANDOM_H_ 10 | #define _CHAOS_RANDOM_H_ 11 | 12 | /** 13 | * @file random.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | using namespace std; 29 | 30 | namespace chaos 31 | { 32 | 33 | namespace utility 34 | { 35 | 36 | //! yunjie: it's safe in mutil thread environment. 37 | class rand_gen_t 38 | { 39 | public: 40 | typedef unsigned long seed_type_t; 41 | 42 | public: 43 | static const seed_type_t max_32_bit_long = 0xFFFFFFFFLU; 44 | static const seed_type_t random_max = max_32_bit_long; 45 | 46 | rand_gen_t(const seed_type_t seed_ = 0) 47 | { 48 | reset(seed_); 49 | } 50 | 51 | //! yunjie: 种子处理 52 | void reset(const seed_type_t seed_ = 0) 53 | { 54 | m_seed[0] = (seed_ ^ 0xFEA09B9DLU) & 0xFFFFFFFELU; 55 | m_seed[0] ^= (((m_seed[0] << 7) & max_32_bit_long) ^ m_seed[0]) >> 31; 56 | 57 | m_seed[1] = (seed_ ^ 0x9C129511LU) & 0xFFFFFFF8LU; 58 | m_seed[1] ^= (((m_seed[1] << 2) & max_32_bit_long) ^ m_seed[1]) >> 29; 59 | 60 | m_seed[2] = (seed_ ^ 0x2512CFB8LU) & 0xFFFFFFF0LU; 61 | m_seed[2] ^= (((m_seed[2] << 9) & max_32_bit_long) ^ m_seed[2]) >> 28; 62 | 63 | rand_uint(); 64 | } 65 | 66 | //! yunjie: 0~RandMax uint 随机数 67 | unsigned long rand_uint(void) 68 | { 69 | m_seed[0] = (((m_seed[0] & 0xFFFFFFFELU) << 24) & max_32_bit_long) 70 | ^ ((m_seed[0] ^ ((m_seed[0] << 7) & max_32_bit_long)) >> 7); 71 | 72 | m_seed[1] = (((m_seed[1] & 0xFFFFFFF8LU) << 7) & max_32_bit_long) 73 | ^ ((m_seed[1] ^ ((m_seed[1] << 2) & max_32_bit_long)) >> 22); 74 | 75 | m_seed[2] = (((m_seed[2] & 0xFFFFFFF0LU) << 11) & max_32_bit_long) 76 | ^ ((m_seed[2] ^ ((m_seed[2] << 9) & max_32_bit_long)) >> 17); 77 | 78 | return (m_seed[0] ^ m_seed[1] ^ m_seed[2]); 79 | } 80 | 81 | //! yunjie: 返回[0.0, 1.0]之间的双精度浮点 82 | double rand_double(void) 83 | { 84 | return static_cast(rand_uint()) 85 | / (static_cast(random_max) ); 86 | } 87 | 88 | //! yunjie: 随机范围 - [start_, end_) 89 | static uint32_t get_rand(uint32_t start_, uint32_t end_); 90 | static int rand_str(char * dest_, uint32_t len_); 91 | static int rand_str(string& dest_, uint32_t len_); 92 | 93 | //! yunjie: 0 <= rate_ <= 100 94 | static bool calc_probability(int rate_); 95 | 96 | private: 97 | seed_type_t m_seed[3]; 98 | }; 99 | 100 | static const char CCH[] = "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; 101 | 102 | } 103 | 104 | } 105 | 106 | #endif // _CHAOS_RANDOM_H_ 107 | -------------------------------------------------------------------------------- /chaos/utility/signal_handler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | /** 10 | * @file signal_handler.cpp 11 | * @author yunjie.lu 12 | * @email lyjdamzwf@gmail.com 13 | * @weibo http://weibo.com/crazyprogramerlyj 14 | * @date 15 | * @brief 16 | * @last changed 17 | * 18 | */ 19 | 20 | #include 21 | 22 | namespace chaos 23 | { 24 | 25 | namespace utility 26 | { 27 | 28 | signal_handler_t::signal_handler_t() 29 | { 30 | //! yunjie: 清空信号掩码 31 | sigemptyset(&m_wait_mask); 32 | } 33 | 34 | signal_handler_t::~signal_handler_t() 35 | { 36 | } 37 | 38 | //! yunjie: 阻塞当前线程对所有信号的接受 39 | //! 注; 这里是阻塞而不是忽略, 发生在内核态 40 | int signal_handler_t::block_all_signal() 41 | { 42 | sigset_t all_mask; 43 | sigfillset(&all_mask); 44 | return pthread_sigmask(SIG_BLOCK, &all_mask, NULL); 45 | } 46 | 47 | int signal_handler_t::register_quit_signal(unsigned int signal_num_) 48 | { 49 | sigaddset(&m_wait_mask, signal_num_); 50 | if (m_signal_func_map.end() != m_signal_func_map.find(signal_num_)) 51 | { 52 | return -1; 53 | } 54 | 55 | m_signal_func_map[signal_num_] = signal_callback_func_t(); 56 | return 0; 57 | } 58 | 59 | int signal_handler_t::register_signal(unsigned int signal_num_, signal_callback_func_t signal_callback_func_) 60 | { 61 | sigaddset(&m_wait_mask, signal_num_); 62 | if (m_signal_func_map.end() != m_signal_func_map.find(signal_num_)) 63 | { 64 | return -1; 65 | } 66 | 67 | m_signal_func_map[signal_num_] = signal_callback_func_; 68 | return 0; 69 | } 70 | 71 | int signal_handler_t::event_loop() 72 | { 73 | pthread_sigmask(SIG_BLOCK, &m_wait_mask, 0); 74 | 75 | int sig_num = 0; 76 | while (!sigwait(&m_wait_mask, &sig_num)) 77 | { 78 | m_signal_func_map_t::iterator it = m_signal_func_map.find(sig_num); 79 | if (m_signal_func_map.end() != it) 80 | { 81 | if (it->second) 82 | { 83 | //! yunjie: 调用signal callback进行处理 84 | (it->second)(); 85 | } 86 | else 87 | { 88 | //! yunjie: 收到退出信号, 退出循环 89 | return 0; 90 | } 91 | } 92 | else 93 | { 94 | return -1; 95 | } 96 | } 97 | 98 | return 0; 99 | } 100 | 101 | } 102 | 103 | } 104 | 105 | -------------------------------------------------------------------------------- /chaos/utility/signal_handler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_SIGNAL_HANDLER_H_ 10 | #define _CHAOS_SIGNAL_HANDLER_H_ 11 | 12 | /** 13 | * @file signal_handler.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | using namespace std; 28 | 29 | namespace chaos 30 | { 31 | 32 | namespace utility 33 | { 34 | 35 | class signal_handler_t 36 | { 37 | public: 38 | 39 | //! yunjie: 注册的信号callback函数 40 | typedef void (*signal_callback_func_t)(); 41 | typedef map m_signal_func_map_t; 42 | 43 | public: 44 | 45 | signal_handler_t(); 46 | 47 | ~signal_handler_t(); 48 | 49 | //! yunjie: 阻塞当前线程对所有信号的接受 50 | int block_all_signal(); 51 | 52 | int register_quit_signal(unsigned int signal_num_); 53 | 54 | int register_signal(unsigned int signal_num_, signal_callback_func_t signal_callback_func_ = NULL); 55 | 56 | int event_loop(); 57 | 58 | private: 59 | 60 | sigset_t m_wait_mask; //! yunjie: 信号掩码 61 | m_signal_func_map_t m_signal_func_map; 62 | }; 63 | 64 | } 65 | 66 | } 67 | 68 | #endif //! _CHAOS_SIGNAL_HANDLER_H_ 69 | 70 | 71 | -------------------------------------------------------------------------------- /chaos/utility/singleton.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef SINGLETON_INC 10 | #define SINGLETON_INC 11 | 12 | /** 13 | * @file singleton.h 14 | * @author yunjie.lu 15 | * @email lyjdamzwf@gmail.com 16 | * @weibo http://weibo.com/crazyprogramerlyj 17 | * @date 18 | * @brief 19 | * @last changed 20 | * 21 | */ 22 | 23 | #include 24 | 25 | namespace chaos 26 | { 27 | 28 | namespace utility 29 | { 30 | /** 31 | * usage: chaos::utility::singleton_t::instance().m_member; 32 | **/ 33 | 34 | template 35 | class singleton_t 36 | { 37 | public: 38 | static Type& instance() // Unique point of access 39 | { 40 | if (s_instance == 0) 41 | { 42 | s_instance = new(Type)(); 43 | atexit(destroy); 44 | } 45 | return *s_instance; 46 | } 47 | protected: 48 | singleton_t() {} 49 | virtual ~singleton_t() {} 50 | private: 51 | static void destroy() // Destroy the only instance 52 | { 53 | if (s_instance != 0) 54 | { 55 | delete(s_instance); 56 | s_instance = 0; 57 | } 58 | } 59 | static Type* volatile s_instance; // The one and oly instance 60 | }; 61 | 62 | template 63 | Type* volatile singleton_t::s_instance = 0; 64 | 65 | 66 | } 67 | 68 | } 69 | 70 | #endif 71 | 72 | -------------------------------------------------------------------------------- /chaos/utility/utility_inc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012, Yunjie Lu. All rights reserved. 3 | * https://github.com/lyjdamzwf/chaos 4 | * 5 | * Use of this source code is governed by a BSD-style 6 | * license that can be found in the License file. 7 | */ 8 | 9 | #ifndef _CHAOS_UTILITY_INC_H_ 10 | #define _CHAOS_UTILITY_INC_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #endif //! _CHAOS_UTILITY_INC_H_ 26 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(0.001) 5 | AC_INIT(chaos, 0.6.30, lyjdamzwf@gmail.com) 6 | AM_INIT_AUTOMAKE([foreign]) 7 | chaos_ver=0.6.30 8 | AC_SUBST(chaos_ver) 9 | 10 | # Checks for programs. 11 | AC_PROG_RANLIB 12 | AC_PROG_CXX 13 | AC_PROG_LIBTOOL 14 | 15 | AC_CONFIG_HEADERS([conf.h]) 16 | 17 | # Add a line like "export ENV_MAKE=-D_DEBUG_NYS" in ~/.bash_rc to cutomize make process 18 | CXXFLAGS="$CXXFLAGS ${ENV_MAKE}" 19 | 20 | # Checks for libraries. 21 | 22 | # Checks for header files. 23 | 24 | # Checks for typedefs, structures, and compiler characteristics. 25 | 26 | # Checks for library functions. 27 | 28 | 29 | AC_ARG_ENABLE([log], 30 | AS_HELP_STRING([--enable-log], [enbale log trace,info,debug]), 31 | [enable_log="yes"], 32 | [enable_log="yes"]) 33 | if test "x$enable_log" = "xyes"; then 34 | AC_DEFINE([ENABLE_LOG_FATAL],[],[enable fatal output]) 35 | AC_DEFINE([ENABLE_LOG_ERROR],[],[enable error output]) 36 | AC_DEFINE([ENABLE_LOG_WARN],[],[enable warn output]) 37 | AC_DEFINE([ENABLE_LOG_INFO],[],[enable info output]) 38 | AC_DEFINE([ENABLE_LOG_TRACE],[],[enable trace output]) 39 | AC_DEFINE([ENABLE_LOG_DEBUG],[],[enable debug output]) 40 | fi 41 | 42 | AC_CONFIG_FILES([Makefile 43 | chaos/Makefile 44 | chaos/utility/Makefile 45 | chaos/thread/Makefile 46 | chaos/task_service/Makefile 47 | chaos/log/Makefile 48 | chaos/network/Makefile 49 | chaos/statistic/Makefile 50 | test/Makefile 51 | test/arg_helper/Makefile 52 | test/async_method/Makefile 53 | test/echo_server/Makefile 54 | test/log/Makefile 55 | test/task_service/Makefile 56 | test/thread/Makefile 57 | test/throughput_client/Makefile 58 | test/press_client/Makefile 59 | test/timer/Makefile 60 | test/msg_buffer/Makefile 61 | test/utility/Makefile 62 | test/perf/Makefile 63 | ]) 64 | 65 | # yunjie: 获取内核信息 66 | full_kernel_version=$(uname -r) 67 | echo $full_kernel_version 68 | fir_v=$(echo $full_kernel_version|awk -F"." '{print $1}') 69 | sec_v=$(echo $full_kernel_version|awk -F"." '{print $2}') 70 | suffix=$(echo $full_kernel_version|awk -F"." '{print $3}') 71 | thi_v=$(echo $suffix|awk -F"-" '{print $1}') 72 | 73 | AC_DEFINE_UNQUOTED([KER_V1],[${fir_v}],[the first kernel version]) 74 | AC_DEFINE_UNQUOTED([KER_V2],[${sec_v}],[the second kernel version]) 75 | AC_DEFINE_UNQUOTED([KER_V3],[${thi_v}],[the third kernel version]) 76 | AC_OUTPUT 77 | 78 | cp conf.h chaos 79 | 80 | -------------------------------------------------------------------------------- /fixunix.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # 3 | # Converting text files from CR/LF to LF format. 4 | 5 | 6 | echo "Converting files to Unix format..." 7 | 8 | find . -type f "(" \ 9 | -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.s" -o \ 10 | -name "*.asm" -o -name "*.inc" -o -name "*.vc" -o -name "*.ls" -o \ 11 | -name "*.acs" -o -name "*.cfg" -o -name "*.txt" -o -name "*.vs" -o \ 12 | -name "*.mak" -o -name "*.mgw" -o \ 13 | -name "makefile.*" -o -name "makefile" -o -name "Makefile" \ 14 | -name "Makefile.in" -o -name "Makefile.am" \ 15 | ")" \ 16 | -exec sh -c "echo {}; 17 | mv {} _tmpfile; 18 | tr -d \\\r < _tmpfile > {}; 19 | touch -r _tmpfile {}; 20 | rm _tmpfile" \; 21 | 22 | find . -name "*.h" -o -name "*.cpp" -o -name "*.am" | xargs sed -i 's/[ \t]*$//g' 23 | 24 | echo "Done!" 25 | -------------------------------------------------------------------------------- /op_lvl.sh: -------------------------------------------------------------------------------- 1 | ## 2 | # yunjie - 3 | # 用法: sh op_lvl.sh O2 O0 --将chaos编译优化选项从O2改为O0 4 | 5 | ori_op_lvl=$1 6 | new_op_lvl=$2 7 | 8 | 9 | echo "optimizing level:"$ori_op_lvl" >> "$new_op_lvl 10 | 11 | seq_str="s/"$ori_op_lvl"/"$new_op_lvl"/g" 12 | echo "seq_str:"$seq_str 13 | find . -name "*.am" | xargs sed -i "$seq_str" 14 | 15 | -------------------------------------------------------------------------------- /test/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS= arg_helper async_method echo_server log task_service thread throughput_client press_client timer msg_buffer utility perf 2 | -------------------------------------------------------------------------------- /test/arg_helper/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_arg_helper 4 | 5 | chaos_arg_helper_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_arg_helper_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos.a \ 10 | -lpthread 11 | -------------------------------------------------------------------------------- /test/arg_helper/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_arg_helper.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | //! arg_pair_t* pair_arr = NULL; 11 | //! int pair_count = 0; 12 | 13 | test_arg_helper(argc_, argv_); 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /test/arg_helper/test_arg_helper.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_ARG_HELER_H_ 2 | #define _CHAOS_TEST_ARG_HELER_H_ 3 | 4 | void arg1_handler(int index, const char* arg_name_, const char* arg_val_) 5 | { 6 | printf("arg_handler1 val:[%d %s %s]\n", index, arg_name_, arg_val_); 7 | } 8 | 9 | void arg2_handler(int index, const char* arg_name_, const char* arg_val_) 10 | { 11 | printf("arg_handler2 val:[%d %s %s]\n", index, arg_name_, arg_val_); 12 | } 13 | 14 | void arg3_handler(int index, const char* arg_name_, const char* arg_val_) 15 | { 16 | printf("arg_handler3 val:[%d %s %s]\n", index, arg_name_, arg_val_); 17 | } 18 | 19 | arg_option_t test_arg_options[] = 20 | { 21 | arg_option_t("arg1", "this is arg1", true, arg1_handler), 22 | arg_option_t("arg2", "this is arg2", true, arg2_handler), 23 | arg_option_t("arg3", "this is arg3", false, arg3_handler), 24 | arg_option_t("arg4", "this is arg4", true, arg1_handler) 25 | }; 26 | 27 | void test_arg_helper(int argc_, char* argv_[]) 28 | { 29 | singleton_t::instance().show_args( 30 | test_arg_options, 31 | sizeof(test_arg_options) / sizeof(arg_option_t) 32 | ); 33 | arg_pair_t* pair_arr; 34 | int pair_count; 35 | 36 | int parse_ret = singleton_t::instance().parse_arg 37 | ( 38 | test_arg_options, 39 | sizeof(test_arg_options) / sizeof(arg_option_t), 40 | argv_, 41 | argc_, 42 | &pair_arr, 43 | &pair_count 44 | ); 45 | 46 | for (int i = 0; i < pair_count; ++i) 47 | { 48 | arg_pair_t pair = pair_arr[i]; 49 | printf("index:%d val:%s\n", pair.arg_index, pair.arg_val); 50 | } 51 | 52 | printf("arg parse ret:[%d]\n", parse_ret); 53 | } 54 | 55 | #endif //! _CHAOS_TEST_ARG_HELER_H_ 56 | -------------------------------------------------------------------------------- /test/async_method/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_async_method 4 | 5 | chaos_async_method_SOURCES = chaos.cpp 6 | 7 | chaos_async_method_LDADD = \ 8 | $(top_srcdir)/chaos/libchaos_je.a \ 9 | -lpthread 10 | -------------------------------------------------------------------------------- /test/async_method/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_async_method.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | //! test_async_method(); 11 | check_memory_leak(); 12 | 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /test/async_method/test_async_method.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_ASYNC_METHOD_H_ 2 | #define _CHAOS_TEST_ASYNC_METHOD_H_ 3 | 4 | class hw_t 5 | { 6 | public: 7 | hw_t() 8 | { 9 | } 10 | 11 | void call0() 12 | { 13 | printf("call0.\n"); 14 | } 15 | 16 | void call1(int arg_) 17 | { 18 | printf("call1:%d\n", arg_); 19 | } 20 | 21 | void call2(int arg_, string arg2_) 22 | { 23 | printf("call2:%d, %s\n", arg_, arg2_.c_str()); 24 | } 25 | 26 | void call3(int arg_, const string& arg2_, double arg3_) 27 | { 28 | printf("call2:%d, %s, %f\n", arg_, arg2_.c_str(), arg3_); 29 | } 30 | 31 | void call4(int arg_, string arg2_, double arg3_, const string arg4_) 32 | { 33 | printf("call2:%d, %s, %f, %s\n", arg_, arg2_.c_str(), arg3_, arg4_.c_str()); 34 | } 35 | 36 | void call5(int arg_, string& arg2_, double arg3_, string arg4_, uint64_t arg5_) 37 | { 38 | printf("call2:%d, %s, %f, %s, %lu\n", arg_, arg2_.c_str(), arg3_, arg4_.c_str(), arg5_); 39 | } 40 | }; 41 | 42 | 43 | void test_static_func(string& s_) 44 | { 45 | printf("test_static_func:%s\n", s_.c_str()); 46 | } 47 | 48 | class foo_t 49 | { 50 | public: 51 | static void test_static_func(string s_) 52 | { 53 | printf("foo_t::test_static_func:%s\n", s_.c_str()); 54 | } 55 | }; 56 | 57 | void test_async_method() 58 | { 59 | hw_t hw; 60 | const string tmp_str = "yunjie"; 61 | string tmp_str2 = "luyunjie"; 62 | 63 | async_method_t m1 = bindfunc(&hw, &hw_t::call0); 64 | async_method_t m2 = bindfunc(&hw, &hw_t::call1, 123456); 65 | async_method_t m3 = bindfunc(&hw, &hw_t::call2, 1121, string("lyj")); 66 | async_method_t m4 = bindfunc(&hw, &hw_t::call3, 1121, string("lyj"), 123.456); 67 | async_method_t m5 = bindfunc(&hw, &hw_t::call4, 1121, string("lyj"), 123.456, string("zark")); 68 | async_method_t m6 = bindfunc(&hw, &hw_t::call5, 1121, string("lyj"), 123.456, string("zark"), (uint64_t)123456789); 69 | async_method_t m7 = bindfunc(&test_static_func, tmp_str); 70 | async_method_t m8 = bindfunc(&foo_t::test_static_func, tmp_str2); 71 | 72 | vector task_queue; 73 | task_queue.push_back(m1); 74 | task_queue.push_back(m2); 75 | task_queue.push_back(m3); 76 | task_queue.push_back(m4); 77 | task_queue.push_back(m5); 78 | task_queue.push_back(m6); 79 | task_queue.push_back(m7); 80 | task_queue.push_back(m8); 81 | for (vector::iterator it = task_queue.begin(); it != task_queue.end(); ++it) 82 | { 83 | (*it)(); 84 | } 85 | } 86 | 87 | void check_memory_leak() 88 | { 89 | string str = "123abc"; 90 | 91 | for (;;) 92 | { 93 | async_method_t m = bindfunc(&foo_t::test_static_func, str); 94 | } 95 | } 96 | 97 | 98 | #endif //! _CHAOS_TEST_ASYNC_METHOD_H_ 99 | -------------------------------------------------------------------------------- /test/echo_server/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_echo_server 4 | 5 | chaos_echo_server_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_echo_server_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | 12 | -------------------------------------------------------------------------------- /test/echo_server/test_tcp_server.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_TCP_SERVER_H_ 2 | #define _CHAOS_TEST_TCP_SERVER_H_ 3 | 4 | #include 5 | 6 | void tcp_conn_event( 7 | conn_event_e conn_event_, 8 | conn_status_e conn_status_, 9 | conn_id_t conn_id_, 10 | void* use_data_ 11 | ) 12 | { 13 | switch (conn_event_) 14 | { 15 | case EV_INIT_COMPLETE: 16 | { 17 | LOGINFO((TEST_MODULE, "tcp_conn_event init complete sockfd:[%d]", conn_id_.socket)); 18 | } 19 | break; 20 | 21 | case EV_DECONSTRUCT: 22 | { 23 | LOGINFO((TEST_MODULE, "tcp_conn_event deconstruct sockfd:[%d]", conn_id_.socket)); 24 | } 25 | break; 26 | 27 | case EV_ACCEPTED_COMPLETE: 28 | { 29 | LOGINFO((TEST_MODULE, "tcp_conn_event accepted complete sockfd:[%d]", conn_id_.socket)); 30 | } 31 | break; 32 | 33 | case EV_CONNECT_SUCCESS: 34 | { 35 | LOGINFO((TEST_MODULE, "tcp_conn_event connect success sockfd:[%d]", conn_id_.socket)); 36 | } 37 | break; 38 | 39 | case EV_CONNECT_FAILED: 40 | { 41 | LOGINFO((TEST_MODULE, "tcp_conn_event connect failed sockfd:[%d]", conn_id_.socket)); 42 | } 43 | break; 44 | 45 | case EV_PASSIVE_CLOSED: 46 | { 47 | LOGINFO((TEST_MODULE, "tcp_conn_event conn passive closed sockfd:[%d]", conn_id_.socket)); 48 | } 49 | break; 50 | 51 | case EV_ACTIVE_CLOSED: 52 | { 53 | LOGINFO((TEST_MODULE, "tcp_conn_event conn active closed sockfd:[%d]", conn_id_.socket)); 54 | } 55 | break; 56 | 57 | case EV_TIMEOUT_CLOSED: 58 | { 59 | LOGINFO((TEST_MODULE, "tcp_conn_event conn timeout closed sockfd:[%d]", conn_id_.socket)); 60 | } 61 | break; 62 | 63 | case EV_ERROR_OCCURRED: 64 | { 65 | LOGINFO((TEST_MODULE, "tcp_conn_event error occurred sockfd:[%d]", conn_id_.socket)); 66 | connection_t::async_close(conn_id_); 67 | } 68 | break; 69 | 70 | default: 71 | { 72 | LOGINFO((TEST_MODULE, "tcp_conn_event unknown event sockfd:[%d]", conn_id_.socket)); 73 | } 74 | } 75 | } 76 | 77 | //! yunjie: echo服务器的连接策略类 78 | class test_server_echo_conn_t : public default_conn_strategy_t 79 | { 80 | protected: 81 | void on_write_complete(uint32_t transferred_size_) 82 | { 83 | //! TP_STAT(SS(), "throughput", transferred_size_); 84 | default_conn_strategy_t::on_write_complete(transferred_size_); 85 | } 86 | 87 | void handle_packet(const packet_header_t& packet_header_, const char* data_ptr_, uint32_t data_size_) 88 | { 89 | //! PERF_GUARD(SS(), "handle request perf"); 90 | //! ACT_CNT_STAT(SS(), "handle request count"); 91 | //! TP_STAT(SS(), "throughput", (data_size_ + HEADER_SIZE)); 92 | /* 93 | LOGINFO((TEST_MODULE, 94 | "test_server_echo_conn_t::handle_packet fd:[%d] cmd:[%d] data size:[%d]", 95 | native_socket(), packet_header_.cmd, data_size_ 96 | )); 97 | */ 98 | 99 | packet_wrapper_t packet; 100 | packet.append((char*)&packet_header_, sizeof(packet_header_)); 101 | packet.append((char*)data_ptr_, data_size_); 102 | 103 | connection_t::async_send(this->get_conn_id(), packet); 104 | } 105 | }; 106 | 107 | #endif //! _CHAOS_TEST_TCP_SERVER_H_ 108 | -------------------------------------------------------------------------------- /test/log/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_log 4 | 5 | chaos_log_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_log_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | 12 | -------------------------------------------------------------------------------- /test/log/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_log.h" 7 | 8 | EXTERN_SERVICE_DECL 9 | 10 | int main(int argc_, char* argv_[]) 11 | { 12 | NEW_SERVICE(); 13 | 14 | log_tool_t::start_log_service("testlog.log", 6, 1, 1); 15 | 16 | test_log(); 17 | 18 | application_tool_t::wait_signal(); 19 | log_tool_t::stop_log_service(); 20 | 21 | DEL_SERVICE(); 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /test/log/test_log.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_LOG_H_ 2 | #define _CHAOS_TEST_LOG_H_ 3 | 4 | void test_log() 5 | { 6 | LOGFATAL((TEST_MODULE, "weibo - %s", "http://weibo.com/crazyprogramerlyj")); 7 | LOGERROR((TEST_MODULE, "my name is %s", "yunjie")); 8 | LOGWARN((TEST_MODULE, "maya's birthday:%d", 1104)); 9 | LOGTRACE((TEST_MODULE, "I will wait for you")); 10 | LOGDEBUG((TEST_MODULE, "value:%lu", 1121)); 11 | } 12 | 13 | 14 | #endif //! _CHAOS_TEST_LOG_H_ 15 | -------------------------------------------------------------------------------- /test/misc_impl.cpp: -------------------------------------------------------------------------------- 1 | #include "misc_def.h" 2 | 3 | task_service_t* task_service_1104; 4 | work_service_t* work_service_1104; 5 | work_service_group_t* work_service_group_1104; 6 | task_service_t* log_service_1104; 7 | statistic_service_t* stat_service_1104; 8 | 9 | volatile bool log_tool_t::is_started = false; 10 | -------------------------------------------------------------------------------- /test/msg_buffer/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_msg_buffer 4 | 5 | chaos_msg_buffer_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_msg_buffer_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | 12 | -------------------------------------------------------------------------------- /test/msg_buffer/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_msg_buffer.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | //! test_msg_buffer(); 11 | //test_buffer_list(); 12 | //test_msg_buffer_limit(); 13 | //test_buffer_list_limit(); 14 | //! test_serialize(); 15 | //! test_chaos_new(); 16 | test_serialize_copy(); 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /test/perf/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_perf 4 | 5 | chaos_perf_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_perf_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | 12 | -------------------------------------------------------------------------------- /test/perf/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_perf.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | NEW_SERVICE(); 11 | 12 | application_tool_t::block_all_signal(); 13 | log_tool_t::start_log_service("test_perf.log", 6, 1, 0); 14 | WSG().start(WSG_THREAD_NUM); 15 | 16 | test_atomic_perf(); 17 | 18 | application_tool_t::wait_signal(); 19 | WSG().stop(); 20 | log_tool_t::stop_log_service(); 21 | 22 | DEL_SERVICE(); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /test/perf/test_perf.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_PERF_H_ 2 | #define _CHAOS_TEST_PERF_H_ 3 | 4 | #define WSG_THREAD_NUM 1 5 | #define USE_ATOMIC 0 6 | #define VAL_PER_THREAD 1 7 | 8 | //! yunjie: 这里不能用数组, 性能会被cache line影响 9 | #if USE_ATOMIC 10 | atomic_val_t g_aval0; 11 | atomic_val_t g_aval1; 12 | atomic_val_t g_aval2; 13 | atomic_val_t g_aval3; 14 | atomic_val_t g_aval4; 15 | atomic_val_t g_aval5; 16 | atomic_val_t g_aval6; 17 | atomic_val_t g_aval7; 18 | #else 19 | volatile uint32_t g_val0 = 0; 20 | volatile uint32_t g_val1 = 0; 21 | volatile uint32_t g_val2 = 0; 22 | volatile uint32_t g_val3 = 0; 23 | volatile uint32_t g_val4 = 0; 24 | volatile uint32_t g_val5 = 0; 25 | volatile uint32_t g_val6 = 0; 26 | volatile uint32_t g_val7 = 0; 27 | #endif 28 | 29 | void val_op(int index_) 30 | { 31 | struct timeval begin_tv; 32 | gettimeofday(&begin_tv, NULL); 33 | 34 | #if USE_ATOMIC 35 | atomic_val_t* 36 | #else 37 | volatile uint32_t* 38 | #endif 39 | tmp_val; 40 | switch (index_) 41 | { 42 | case 0: 43 | tmp_val = 44 | #if USE_ATOMIC 45 | &g_aval0; 46 | #else 47 | &g_val0; 48 | #endif 49 | break; 50 | case 1: 51 | tmp_val = 52 | #if USE_ATOMIC 53 | &g_aval1; 54 | #else 55 | &g_val1; 56 | #endif 57 | break; 58 | case 2: 59 | tmp_val = 60 | #if USE_ATOMIC 61 | &g_aval2; 62 | #else 63 | &g_val2; 64 | #endif 65 | break; 66 | case 3: 67 | tmp_val = 68 | #if USE_ATOMIC 69 | &g_aval3; 70 | #else 71 | &g_val3; 72 | #endif 73 | break; 74 | case 4: 75 | tmp_val = 76 | #if USE_ATOMIC 77 | &g_aval4; 78 | #else 79 | &g_val4; 80 | #endif 81 | break; 82 | case 5: 83 | tmp_val = 84 | #if USE_ATOMIC 85 | &g_aval5; 86 | #else 87 | &g_val5; 88 | #endif 89 | break; 90 | case 6: 91 | tmp_val = 92 | #if USE_ATOMIC 93 | &g_aval6; 94 | #else 95 | &g_val6; 96 | #endif 97 | break; 98 | case 7: 99 | tmp_val = 100 | #if USE_ATOMIC 101 | &g_aval7; 102 | #else 103 | &g_val7; 104 | #endif 105 | break; 106 | default: 107 | tmp_val = NULL; 108 | break; 109 | } 110 | 111 | for (uint64_t i = 0; i < 100000000; ++i) 112 | { 113 | (*tmp_val)++; 114 | (*tmp_val)--; 115 | } 116 | 117 | struct timeval end_tv; 118 | gettimeofday(&end_tv, NULL); 119 | uint64_t cost_us = (end_tv.tv_sec - begin_tv.tv_sec) * 1000 * 1000 + (end_tv.tv_usec - begin_tv.tv_usec); 120 | 121 | printf("cost us:[%lu]\n", cost_us); 122 | } 123 | 124 | void test_atomic_perf() 125 | { 126 | for (int i = 0; i < WSG().size(); ++i) 127 | { 128 | int arg; 129 | #if VAL_PER_THREAD 130 | arg = i; 131 | #else 132 | arg = 0; 133 | #endif 134 | WSG()[i]->post(bindfunc(&val_op, arg)); 135 | } 136 | } 137 | 138 | #endif //! _CHAOS_TEST_PERF_H_ 139 | -------------------------------------------------------------------------------- /test/press_client/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_pressclient 4 | 5 | chaos_pressclient_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp \ 7 | test_pressclient.cpp 8 | 9 | chaos_pressclient_LDADD = \ 10 | $(top_srcdir)/chaos/libchaos_je.a \ 11 | -lpthread 12 | 13 | -------------------------------------------------------------------------------- /test/press_client/test_pressclient.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_TCP_SERVER_H_ 2 | #define _CHAOS_TEST_TCP_SERVER_H_ 3 | 4 | #include "../misc_def.h" 5 | 6 | //! yunjie: test press begin 7 | 8 | #define PRESS_CONN_NUM 100 9 | #define MIN_PACKET_SIZE (sizeof(packet_header_t)) 10 | #define MAX_PACKET_SIZE (sizeof(packet_header_t) + 4096) 11 | 12 | #define BROADCAST_PACKET_SIZE (32 * 1024) 13 | 14 | #define CHECK_SUM 1 15 | 16 | #define BROADCAST_CMD 1104 17 | #define GENERAL_CMD 1121 18 | 19 | enum press_conn_action_e 20 | { 21 | PCA_BEGIN = 0, 22 | PCA_REPEAT, 23 | PCA_RESEND, 24 | PCA_BROADCAST, 25 | PCA_CLOSE, 26 | PCA_WAIT_HEART_BEAT, 27 | PCA_END 28 | }; 29 | 30 | #define CROSS_THREAD 100 31 | 32 | #define REPEAT_PRO 50 33 | #define RESEND_PRO 50 34 | #define BC_PRO 50 35 | #define CLOSE_PRO 20 36 | #define HB_PRO 20 37 | 38 | class entity_t 39 | { 40 | public: 41 | ~entity_t(); 42 | 43 | void handle_wrapper_message( 44 | const packet_header_t& packet_header_, 45 | packet_wrapper_t& message_, 46 | const conn_id_t& conn_id_, 47 | const string& service_name_ 48 | ); 49 | 50 | void handle_message( 51 | const packet_header_t& packet_header_, 52 | const char* data_ptr_, 53 | uint32_t data_size_, 54 | const conn_id_t& conn_id_, 55 | const string& service_name_ 56 | ); 57 | 58 | packet_wrapper_t last_packet() const { return m_last_packet; } 59 | 60 | private: 61 | packet_wrapper_t m_last_packet; 62 | }; 63 | 64 | class press_client_t 65 | { 66 | public: 67 | 68 | static void tcp_press_conn_event( 69 | conn_event_e conn_event_, 70 | conn_status_e conn_status_, 71 | conn_id_t conn_id_, 72 | void* user_data_ 73 | ); 74 | 75 | static int test_press_client(int conn_num_); 76 | }; 77 | 78 | class test_press_conn_strategy_t : public default_conn_strategy_t 79 | { 80 | protected: 81 | void handle_packet( 82 | const packet_header_t& packet_header_, 83 | const char* data_ptr_, 84 | uint32_t data_size_ 85 | ); 86 | 87 | private: 88 | void init_entity() { get_entity(); } 89 | 90 | entity_t* get_entity() 91 | { 92 | entity_t* ret; 93 | while (NULL == (ret = (entity_t*)get_userdata())) 94 | { 95 | set_userdata(new entity_t); 96 | } 97 | 98 | return ret; 99 | } 100 | }; 101 | 102 | 103 | //! yunjie: test press end 104 | 105 | #endif //! _CHAOS_TEST_TCP_SERVER_H_ 106 | -------------------------------------------------------------------------------- /test/task_service/Makefile.am: -------------------------------------------------------------------------------- 1 | INCLUDES = -I/opt/local/boost/include/boost-1_44 2 | 3 | CXXFLAGS += -Wall -static -g -O2 -fPIC 4 | 5 | noinst_PROGRAMS = chaos_task_service 6 | 7 | chaos_task_service_SOURCES = chaos.cpp \ 8 | ../misc_impl.cpp 9 | 10 | chaos_task_service_LDFLAGS = -L/opt/local/boost.1.44/lib/ 11 | #chaos_task_service_LDFLAGS = -L/opt/local/boost/lib/boost-1_44/ 12 | 13 | chaos_task_service_LDADD = \ 14 | $(top_srcdir)/chaos/libchaos_je.a \ 15 | -lpthread \ 16 | -lboost_system -lboost_thread -lboost_regex 17 | 18 | -------------------------------------------------------------------------------- /test/task_service/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_task_service.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | NEW_SERVICE(); 11 | 12 | application_tool_t::block_all_signal(); 13 | log_tool_t::start_log_service("test_task_service.log", 6, 1, 1); 14 | TS().start(1); 15 | 16 | test_task_service_performance(); 17 | 18 | application_tool_t::wait_signal(); 19 | TS().stop(); 20 | log_tool_t::stop_log_service(); 21 | 22 | DEL_SERVICE(); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /test/thread/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_thread 4 | 5 | chaos_thread_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_thread_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | 12 | -------------------------------------------------------------------------------- /test/thread/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_thread.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | test_thread(); 11 | 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /test/thread/test_thread.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_THREAD_H_ 2 | #define _CHAOS_TEST_THREAD_H_ 3 | 4 | void thread_func(string str_, int count_) 5 | { 6 | for (int i = 0; i < count_; ++i) 7 | { 8 | printf("[%d]thread_func:%s\n", i + 1, str_.c_str()); 9 | } 10 | } 11 | 12 | void test_thread() 13 | { 14 | thread_t thd; 15 | thd.start(bindfunc(thread_func, string("my name is yunjie.lu"), 10)); 16 | 17 | printf("thread join\n"); 18 | thd.join(); 19 | printf("thread join finish\n"); 20 | } 21 | 22 | 23 | 24 | #endif //! _CHAOS_TEST_THREAD_H_ 25 | -------------------------------------------------------------------------------- /test/throughput_client/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_tpclient 4 | 5 | chaos_tpclient_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_tpclient_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | -------------------------------------------------------------------------------- /test/timer/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_timer 4 | 5 | chaos_timer_SOURCES = chaos.cpp \ 6 | ../misc_impl.cpp 7 | 8 | chaos_timer_LDADD = \ 9 | $(top_srcdir)/chaos/libchaos_je.a \ 10 | -lpthread 11 | 12 | -------------------------------------------------------------------------------- /test/timer/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_timer.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | NEW_SERVICE(); 11 | 12 | test_time_event(); 13 | 14 | application_tool_t::wait_signal(); 15 | 16 | DEL_SERVICE(); 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /test/timer/test_timer.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_TIMER_H_ 2 | #define _CHAOS_TEST_TIMER_H_ 3 | 4 | timer_manager_t timer_manager; 5 | 6 | void timer_event() 7 | { 8 | printf("timer_event called!!!!!\n"); 9 | timer_manager.register_timer(1, bindfunc(timer_event), false); 10 | } 11 | 12 | void test_time_event() 13 | { 14 | timer_manager.initialize(false); 15 | 16 | timer_manager.register_timer(1, bindfunc(timer_event), false, time(NULL) + 10); 17 | 18 | while (1) 19 | { 20 | timer_manager.flush_time(); 21 | timer_manager.exec(); 22 | } 23 | } 24 | 25 | 26 | #endif //! _CHAOS_TEST_TIMER_H_ 27 | -------------------------------------------------------------------------------- /test/utility/Makefile.am: -------------------------------------------------------------------------------- 1 | CXXFLAGS += -Wall -static -g -O2 -fPIC 2 | 3 | noinst_PROGRAMS = chaos_test_utility 4 | 5 | chaos_test_utility_SOURCES = chaos.cpp 6 | 7 | chaos_test_utility_LDADD = \ 8 | $(top_srcdir)/chaos/libchaos_je.a \ 9 | -lpthread 10 | -------------------------------------------------------------------------------- /test/utility/chaos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #include "../misc_def.h" 6 | #include "test_shared_ptr.h" 7 | 8 | int main(int argc_, char* argv_[]) 9 | { 10 | test_shared_ptr(); 11 | 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /test/utility/test_shared_ptr.h: -------------------------------------------------------------------------------- 1 | #ifndef _CHAOS_TEST_UTILITY_H_ 2 | #define _CHAOS_TEST_UTILITY_H_ 3 | 4 | class obj_t 5 | { 6 | public: 7 | obj_t() 8 | : 9 | a(1121) 10 | { 11 | printf("obj_t construct\n"); 12 | } 13 | 14 | ~obj_t() 15 | { 16 | printf("obj_t deconstruct\n"); 17 | } 18 | 19 | void call() 20 | { 21 | printf("obj_t::call a:[%d]\n", a); 22 | } 23 | 24 | private: 25 | int a; 26 | }; 27 | 28 | struct self_t 29 | { 30 | self_t() 31 | { 32 | printf("self_t construct\n"); 33 | } 34 | 35 | ~self_t() 36 | { 37 | printf("self_t deconstruct\n"); 38 | } 39 | 40 | shared_ptr_t self_sptr; 41 | }; 42 | 43 | void test_shared_ptr() 44 | { 45 | shared_ptr_t sptr1; 46 | sptr1.dump(); 47 | 48 | sptr1 = new obj_t; 49 | sptr1.dump(); 50 | 51 | shared_ptr_t sptr2 = sptr1; 52 | 53 | //sptr1.reset(); 54 | sptr1.dump(); 55 | sptr2.dump(); 56 | 57 | sptr1.reset(); 58 | sptr1.dump(); 59 | sptr2.dump(); 60 | 61 | sptr1 = sptr2; 62 | sptr1.dump(); 63 | sptr2.dump(); 64 | 65 | shared_ptr_t self = new self_t; 66 | self->self_sptr = self; 67 | self->self_sptr.reset(); 68 | self.reset(); 69 | 70 | printf("leave test_shared_ptr\n"); 71 | } 72 | 73 | #endif //! _CHAOS_TEST_UTILITY_H_ 74 | --------------------------------------------------------------------------------