diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d0c228..01b9790 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,15 +19,17 @@ include_directories(. "${PROJECT_SOURCE_DIR}/include" ${INCLUDE_DIRECTORIES} /op link_directories(${LINK_DIRECTORIES} /opt/local/lib) find_package(PCRE REQUIRED) find_package(Judy REQUIRED) +find_package(Jemalloc REQUIRED) -set(LIBS ${LIBS} ${PCRE_LIBRARIES} ${Judy_LIBRARIES} r3) +set(LIBS ${LIBS} ${PCRE_LIBRARIES} ${Judy_LIBRARIES} ${Jemalloc_LIBRARIES} r3) +set (CMAKE_CFLAGS "-Wall -pipe -g3 -funroll-loops") # set (CMAKE_CXX_FLAGS "-std=c++0x -arch x86_64 -stdlib=libc++ -g3 -Wall -O0") enable_testing() add_subdirectory(src) add_subdirectory(tests) -add_test(test_tree ${CMAKE_CURRENT_BINARY_DIR}/test_tree) +add_test(test_r3 ${CMAKE_CURRENT_BINARY_DIR}/tests/test_r3) add_executable(demo demo.c) diff --git a/cmake_modules/FindJemalloc.cmake b/cmake_modules/FindJemalloc.cmake new file mode 100644 index 0000000..c97bb1b --- /dev/null +++ b/cmake_modules/FindJemalloc.cmake @@ -0,0 +1,44 @@ +# - Try to find jemalloc headers and libraries. +# +# Usage of this module as follows: +# +# find_package(JeMalloc) +# +# Variables used by this module, they can change the default behaviour and need +# to be set before calling find_package: +# +# JEMALLOC_ROOT_DIR Set this variable to the root installation of +# jemalloc if the module has problems finding +# the proper installation path. +# +# Variables defined by this module: +# +# JEMALLOC_FOUND System has jemalloc libs/headers +# JEMALLOC_LIBRARIES The jemalloc library/libraries +# JEMALLOC_INCLUDE_DIR The location of jemalloc headers + +find_path(JEMALLOC_ROOT_DIR + NAMES include/jemalloc/jemalloc.h +) + +find_library(JEMALLOC_LIBRARIES + NAMES jemalloc + HINTS ${JEMALLOC_ROOT_DIR}/lib +) + +find_path(JEMALLOC_INCLUDE_DIR + NAMES jemalloc/jemalloc.h + HINTS ${JEMALLOC_ROOT_DIR}/include +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(JeMalloc DEFAULT_MSG + JEMALLOC_LIBRARIES + JEMALLOC_INCLUDE_DIR +) + +mark_as_advanced( + JEMALLOC_ROOT_DIR + JEMALLOC_LIBRARIES + JEMALLOC_INCLUDE_DIR +) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba5a3bb..9dd603e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,8 +2,10 @@ include_directories("${PROJECT_SOURCE_DIR}/include") # install(TARGETS swiftnav-static DESTINATION lib${LIB_SUFFIX}) -set(libr3_SRCS node.c) -# add_library(r3 STATIC ${libr3_SRCS}) +set(libr3_SRCS node.c str.c) +set(CMAKE_CFLAGS "-Wall -pipe -g3 -funroll-loops") + +# add_library(r3-static STATIC ${libr3_SRCS}) add_library(r3 SHARED ${libr3_SRCS}) # target_link_libraries(r3 cblas) # install(FILES ${libswiftnav_HEADERS} DESTINATION include/libswiftnav) diff --git a/src/node.c b/src/node.c index 8b13789..5c92a4a 100644 --- a/src/node.c +++ b/src/node.c @@ -1 +1,12 @@ +#include +#include +#include +#include +// Jemalloc memory management +#include + +// Judy array +#include + +// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm diff --git a/src/str.c b/src/str.c new file mode 100644 index 0000000..0d4d8d2 --- /dev/null +++ b/src/str.c @@ -0,0 +1,99 @@ +/* + * str.c + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ +#include +#include +#include +#include +#include "str.h" + + +/** + * This function is used to split route path into a string array, not for performance. + * hence this function should be safe. + * + * Split "/path/foo/{id}" into [ "/path" , "/foo" , "/{id}" ] + * Split "/path/bar/{id}" into [ "/path" , "/foo" , "/{id}" ] + * Split "/blog/post/{id}" into [ "/blog" , "/post" , "/{id}" ] + * Split "/blog/{id}" into [ "/blog" , "/{id}" ] + * Split "/blog" into [ "/blog" ] + * Split "/b" into [ "/b" ] + * + * @param char* pattern + * @param int pattern_len + * + * @return char** + */ +char** split_route_pattern(char *pattern, int pattern_len) { + + char *p = pattern; + + while (*p) { + // a slug + if ( *p == '{' ) { + char *s1 = p; + char *s2; + while (*(p++) != '}') { + if ( p - pattern > pattern_len ) { + // XXX: unexpected error (unclosed slug) + } + } + s2 = p; + } + } + + + return NULL; +} + + +char** str_split(char* a_str, const char a_delim) +{ + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + /* Count how many elements will be extracted. */ + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e5107c3..a94e572 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,13 +11,13 @@ else (NOT CHECK_FOUND) include_directories(${CHECK_INCLUDE_DIRS}) # include_directories("${PROJECT_SOURCE_DIR}/include/r2") - add_executable(test_tree test_tree.c) - target_link_libraries(test_tree ${TEST_LIBS}) + add_executable(test_r3 test_tree.c) + target_link_libraries(test_r3 ${TEST_LIBS}) add_custom_command( - TARGET test_tree POST_BUILD + TARGET test_r3 POST_BUILD COMMENT "Running unit tests" - COMMAND test_tree + COMMAND test_r3 ) endif (NOT CHECK_FOUND) diff --git a/tests/test_tree.c b/tests/test_tree.c index 506f748..6251c4a 100644 --- a/tests/test_tree.c +++ b/tests/test_tree.c @@ -1,25 +1,35 @@ #include #include +#include "str.h" +#include "node.h" -START_TEST (test_tree) +START_TEST (test_route) { + } END_TEST +START_TEST (test_route2) +{ + +} +END_TEST + +Suite* r3_suite (void) { + Suite *suite = suite_create("blah"); + + TCase *tcase = tcase_create("testcase"); + tcase_add_test(tcase, test_route); + tcase_add_test(tcase, test_route2); -Suite* str_suite (void) { - Suite *suite = suite_create("test_tree"); - /* - TCase *tcase = tcase_create("case"); - tcase_add_test(tcase, test_tree); suite_add_tcase(suite, tcase); - */ + return suite; } int main (int argc, char *argv[]) { int number_failed; - Suite *suite = str_suite(); + Suite *suite = r3_suite(); SRunner *runner = srunner_create(suite); srunner_run_all(runner, CK_NORMAL); number_failed = srunner_ntests_failed(runner);