diff options
| author | ruki <[email protected]> | 2017-03-29 03:05:39 +0000 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-03-29 03:05:39 +0000 |
| commit | 93b3fed70161eb32c7ce2dbb462a3b3d1ce680c3 (patch) | |
| tree | 49948c7ddd5252be02d86bc1b270a447f40bd519 | |
| parent | 0d22564e5a9062f83830739998e4b237bd3cf0e9 (diff) | |
update tbox for linux i386
354 files changed, 45086 insertions, 1 deletions
diff --git a/core/pkg/tbox.pkg/inc/linux/tbox.config.h b/core/pkg/tbox.pkg/inc/linux/tbox.config.h index 300f1917b..9519e4014 100644 --- a/core/pkg/tbox.pkg/inc/linux/tbox.config.h +++ b/core/pkg/tbox.pkg/inc/linux/tbox.config.h @@ -6,7 +6,131 @@ #define TB_CONFIG_VERSION_MAJOR 1 #define TB_CONFIG_VERSION_MINOR 6 #define TB_CONFIG_VERSION_ALTER 2 -#define TB_CONFIG_VERSION_BUILD 201703201504 +#define TB_CONFIG_VERSION_BUILD 201703290304 + +// defines +#define TB_CONFIG_OS_LINUX 1 +#define _GNU_SOURCE 1 +#define _REENTRANT 1 +#define TB_CONFIG_SMALL 1 +#define TB_CONFIG_TYPE_HAVE_FLOAT 1 +#define TB_CONFIG_MODULE_HAVE_HASH 1 +#define TB_CONFIG_LIBC_HAVE_MEMCPY 1 +#define TB_CONFIG_LIBC_HAVE_MEMSET 1 +#define TB_CONFIG_LIBC_HAVE_MEMMOVE 1 +#define TB_CONFIG_LIBC_HAVE_MEMCMP 1 +#define TB_CONFIG_LIBC_HAVE_STRCAT 1 +#define TB_CONFIG_LIBC_HAVE_STRNCAT 1 +#define TB_CONFIG_LIBC_HAVE_STRCPY 1 +#define TB_CONFIG_LIBC_HAVE_STRNCPY 1 +#define TB_CONFIG_LIBC_HAVE_STRLEN 1 +#define TB_CONFIG_LIBC_HAVE_STRNLEN 1 +#define TB_CONFIG_LIBC_HAVE_STRSTR 1 +#define TB_CONFIG_LIBC_HAVE_STRCMP 1 +#define TB_CONFIG_LIBC_HAVE_STRCASECMP 1 +#define TB_CONFIG_LIBC_HAVE_STRNCMP 1 +#define TB_CONFIG_LIBC_HAVE_STRNCASECMP 1 +#define TB_CONFIG_LIBC_HAVE_WCSCAT 1 +#define TB_CONFIG_LIBC_HAVE_WCSNCAT 1 +#define TB_CONFIG_LIBC_HAVE_WCSCPY 1 +#define TB_CONFIG_LIBC_HAVE_WCSNCPY 1 +#define TB_CONFIG_LIBC_HAVE_WCSLEN 1 +#define TB_CONFIG_LIBC_HAVE_WCSNLEN 1 +#define TB_CONFIG_LIBC_HAVE_WCSSTR 1 +#define TB_CONFIG_LIBC_HAVE_WCSCMP 1 +#define TB_CONFIG_LIBC_HAVE_WCSCASECMP 1 +#define TB_CONFIG_LIBC_HAVE_WCSNCMP 1 +#define TB_CONFIG_LIBC_HAVE_WCSNCASECMP 1 +#define TB_CONFIG_LIBC_HAVE_WCSTOMBS 1 +#define TB_CONFIG_LIBC_HAVE_MBSTOWCS 1 +#define TB_CONFIG_LIBC_HAVE_GMTIME 1 +#define TB_CONFIG_LIBC_HAVE_MKTIME 1 +#define TB_CONFIG_LIBC_HAVE_LOCALTIME 1 +#define TB_CONFIG_LIBC_HAVE_GETTIMEOFDAY 1 +#define TB_CONFIG_LIBC_HAVE_SIGNAL 1 +#define TB_CONFIG_LIBC_HAVE_SETJMP 1 +#define TB_CONFIG_LIBC_HAVE_SIGSETJMP 1 +#define TB_CONFIG_LIBC_HAVE_KILL 1 +#define TB_CONFIG_LIBC_HAVE_BACKTRACE 1 +#define TB_CONFIG_LIBC_HAVE_SETLOCALE 1 +#define TB_CONFIG_LIBC_HAVE_FPUTS 1 +#define TB_CONFIG_LIBC_HAVE_SRANDOM 1 +#define TB_CONFIG_LIBC_HAVE_RANDOM 1 +#define TB_CONFIG_LIBM_HAVE_LOG2 1 +#define TB_CONFIG_LIBM_HAVE_LOG2F 1 +#define TB_CONFIG_LIBM_HAVE_SQRT 1 +#define TB_CONFIG_LIBM_HAVE_SQRTF 1 +#define TB_CONFIG_LIBM_HAVE_ACOS 1 +#define TB_CONFIG_LIBM_HAVE_ACOSF 1 +#define TB_CONFIG_LIBM_HAVE_ASIN 1 +#define TB_CONFIG_LIBM_HAVE_ASINF 1 +#define TB_CONFIG_LIBM_HAVE_POW 1 +#define TB_CONFIG_LIBM_HAVE_POWF 1 +#define TB_CONFIG_LIBM_HAVE_FMOD 1 +#define TB_CONFIG_LIBM_HAVE_FMODF 1 +#define TB_CONFIG_LIBM_HAVE_TAN 1 +#define TB_CONFIG_LIBM_HAVE_TANF 1 +#define TB_CONFIG_LIBM_HAVE_ATAN 1 +#define TB_CONFIG_LIBM_HAVE_ATANF 1 +#define TB_CONFIG_LIBM_HAVE_ATAN2 1 +#define TB_CONFIG_LIBM_HAVE_ATAN2F 1 +#define TB_CONFIG_LIBM_HAVE_COS 1 +#define TB_CONFIG_LIBM_HAVE_COSF 1 +#define TB_CONFIG_LIBM_HAVE_SIN 1 +#define TB_CONFIG_LIBM_HAVE_SINF 1 +#define TB_CONFIG_LIBM_HAVE_EXP 1 +#define TB_CONFIG_LIBM_HAVE_EXPF 1 +#define TB_CONFIG_POSIX_HAVE_POLL 1 +#define TB_CONFIG_POSIX_HAVE_SELECT 1 +#define TB_CONFIG_POSIX_HAVE_PTHREAD_MUTEX_INIT 1 +#define TB_CONFIG_POSIX_HAVE_PTHREAD_CREATE 1 +#define TB_CONFIG_POSIX_HAVE_PTHREAD_SETSPECIFIC 1 +#define TB_CONFIG_POSIX_HAVE_PTHREAD_GETSPECIFIC 1 +#define TB_CONFIG_POSIX_HAVE_PTHREAD_KEY_CREATE 1 +#define TB_CONFIG_POSIX_HAVE_PTHREAD_KEY_DELETE 1 +#define TB_CONFIG_POSIX_HAVE_SOCKET 1 +#define TB_CONFIG_POSIX_HAVE_OPENDIR 1 +#define TB_CONFIG_POSIX_HAVE_DLOPEN 1 +#define TB_CONFIG_POSIX_HAVE_OPEN 1 +#define TB_CONFIG_POSIX_HAVE_GETHOSTNAME 1 +#define TB_CONFIG_POSIX_HAVE_GETIFADDRS 1 +#define TB_CONFIG_POSIX_HAVE_SEM_INIT 1 +#define TB_CONFIG_POSIX_HAVE_GETPAGESIZE 1 +#define TB_CONFIG_POSIX_HAVE_SYSCONF 1 +#define TB_CONFIG_POSIX_HAVE_SCHED_YIELD 1 +#define TB_CONFIG_POSIX_HAVE_REGCOMP 1 +#define TB_CONFIG_POSIX_HAVE_REGEXEC 1 +#define TB_CONFIG_POSIX_HAVE_READV 1 +#define TB_CONFIG_POSIX_HAVE_WRITEV 1 +#define TB_CONFIG_POSIX_HAVE_PREADV 1 +#define TB_CONFIG_POSIX_HAVE_PWRITEV 1 +#define TB_CONFIG_POSIX_HAVE_FDATASYNC 1 +#define TB_CONFIG_POSIX_HAVE_SENDFILE 1 +#define TB_CONFIG_POSIX_HAVE_EPOLL_CREATE 1 +#define TB_CONFIG_POSIX_HAVE_EPOLL_WAIT 1 +#define TB_CONFIG_POSIX_HAVE_POSIX_SPAWNP 1 +#define TB_CONFIG_POSIX_HAVE_EXECVP 1 +#define TB_CONFIG_POSIX_HAVE_FORK 1 +#define TB_CONFIG_POSIX_HAVE_VFORK 1 +#define TB_CONFIG_POSIX_HAVE_WAITPID 1 +#define TB_CONFIG_POSIX_HAVE_GETDTABLESIZE 1 +#define TB_CONFIG_POSIX_HAVE_GETRLIMIT 1 +#define TB_CONFIG_POSIX_HAVE_GETADDRINFO 1 +#define TB_CONFIG_POSIX_HAVE_GETNAMEINFO 1 +#define TB_CONFIG_POSIX_HAVE_GETHOSTBYNAME 1 +#define TB_CONFIG_POSIX_HAVE_GETHOSTBYADDR 1 +#define TB_CONFIG_SYSTEMV_HAVE_SEMGET 1 + +#endif +#ifndef TB_CONFIG_H +#define TB_CONFIG_H + +// version +#define TB_CONFIG_VERSION "1.6.2" +#define TB_CONFIG_VERSION_MAJOR 1 +#define TB_CONFIG_VERSION_MINOR 6 +#define TB_CONFIG_VERSION_ALTER 2 +#define TB_CONFIG_VERSION_BUILD 201703290304 // defines #define TB_CONFIG_OS_LINUX 1 diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/algorithm.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/algorithm.h index 0f983d8c5..34ac36f0e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/algorithm.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/algorithm.h @@ -57,3 +57,62 @@ #include "remove_first_if.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file algorithm.h + * @defgroup algorithm + * + */ +#ifndef TB_ALGORITHM_H +#define TB_ALGORITHM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "predicate.h" +#include "distance.h" +#include "for.h" +#include "for_if.h" +#include "rfor.h" +#include "rfor_if.h" +#include "sort.h" +#include "heap_sort.h" +#include "quick_sort.h" +#include "insert_sort.h" +#include "bubble_sort.h" +#include "find.h" +#include "find_if.h" +#include "rfind.h" +#include "rfind_if.h" +#include "binary_find.h" +#include "binary_find_if.h" +#include "walk.h" +#include "rwalk.h" +#include "count.h" +#include "count_if.h" +#include "remove.h" +#include "remove_if.h" +#include "remove_first.h" +#include "remove_first_if.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find.h index af36766b2..498b09162 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find.h @@ -65,3 +65,70 @@ tb_size_t tb_binary_find_all(tb_iterator_ref_t iterator, tb_cpointer_t */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file binary_find.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_BINARY_FIND_H +#define TB_ALGORITHM_BINARY_FIND_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! binary find item + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param item the finded item + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_binary_find(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_cpointer_t item); + +/*!binary find item for all + * + * @param iterator the iterator + * @param item the finded item + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_binary_find_all(tb_iterator_ref_t iterator, tb_cpointer_t item); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find_if.h index 4b264674b..bd5d1ad75 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/binary_find_if.h @@ -67,3 +67,72 @@ tb_size_t tb_binary_find_all_if(tb_iterator_ref_t iterator, tb_iterato */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file binary_find_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_BINARY_FIND_IF_H +#define TB_ALGORITHM_BINARY_FIND_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! binary find item if !comp(item, priv) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer func + * @param priv the comparer data + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_binary_find_if(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_iterator_comp_t comp, tb_cpointer_t priv); + +/*! binary find item for all if !comp(item, priv) + * + * @param iterator the iterator + * @param comp the comparer func + * @param priv the comparer data + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_binary_find_all_if(tb_iterator_ref_t iterator, tb_iterator_comp_t comp, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/bubble_sort.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/bubble_sort.h index e45aeb359..8312ae1d8 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/bubble_sort.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/bubble_sort.h @@ -63,3 +63,68 @@ tb_void_t tb_bubble_sort_all(tb_iterator_ref_t iterator, tb_iterator_c */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bubble_sort.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_BUBBLE_SORT_H +#define TB_ALGORITHM_BUBBLE_SORT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the bubble sorter, O(n^2) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_bubble_sort(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_iterator_comp_t comp); + +/*! the bubble sorter for all + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_bubble_sort_all(tb_iterator_ref_t iterator, tb_iterator_comp_t comp); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/count.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/count.h index f90114fa5..352e4f9dc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/count.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/count.h @@ -65,3 +65,70 @@ tb_size_t tb_count_all(tb_iterator_ref_t iterator, tb_cpointer_t value */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file count.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_COUNT_H +#define TB_ALGORITHM_COUNT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! count items + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param value the value of the predicate + * + * @return the real count + */ +tb_size_t tb_count(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_cpointer_t value); + +/*! count items for all + * + * @param iterator the iterator + * @param value the value of the predicate + * + * @return the real count + */ +tb_size_t tb_count_all(tb_iterator_ref_t iterator, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/count_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/count_if.h index 880481e58..4c712a86c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/count_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/count_if.h @@ -68,3 +68,73 @@ tb_size_t tb_count_all_if(tb_iterator_ref_t iterator, tb_predicate_ref */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file count_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_COUNT_IF_H +#define TB_ALGORITHM_COUNT_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "predicate.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! count items if pred(item, value) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param pred the predicate + * @param value the value of the predicate + * + * @return the real count + */ +tb_size_t tb_count_if(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_predicate_ref_t pred, tb_cpointer_t value); + +/*! count items for all if pred(item, value) + * + * @param iterator the iterator + * @param pred the predicate + * @param value the value of the predicate + * + * @return the real count + */ +tb_size_t tb_count_all_if(tb_iterator_ref_t iterator, tb_predicate_ref_t pred, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/distance.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/distance.h index 6653401bf..4fd9d7660 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/distance.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/distance.h @@ -55,3 +55,60 @@ tb_size_t tb_distance(tb_iterator_ref_t iterator, tb_size_t head, tb_s */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file distance.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_DISTANCE_H +#define TB_ALGORITHM_DISTANCE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! compute distance from head to tail + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * + * @return the distance + */ +tb_size_t tb_distance(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/find.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/find.h index 784488743..569f293c0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/find.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/find.h @@ -65,3 +65,70 @@ tb_size_t tb_find_all(tb_iterator_ref_t iterator, tb_cpointer_t value) */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file find.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_FIND_H +#define TB_ALGORITHM_FIND_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the finder + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_find(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_cpointer_t value); + +/*! the finder for all + * + * @param iterator the iterator + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_find_all(tb_iterator_ref_t iterator, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/find_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/find_if.h index 14833e05f..0935253ec 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/find_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/find_if.h @@ -68,3 +68,73 @@ tb_size_t tb_find_all_if(tb_iterator_ref_t iterator, tb_predicate_ref_ */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file find_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_FIND_IF_H +#define TB_ALGORITHM_FIND_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "predicate.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! find item if pred(item, value) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param pred the predicate + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_find_if(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_predicate_ref_t pred, tb_cpointer_t value); + +/*! find item for all if pred(item, value) + * + * @param iterator the iterator + * @param pred the predicate + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_find_all_if(tb_iterator_ref_t iterator, tb_predicate_ref_t pred, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/for.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/for.h index 06451dd2a..49c0a5460 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/for.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/for.h @@ -94,3 +94,99 @@ tb_for(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file for.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_FOR_H +#define TB_ALGORITHM_FOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! for items using iterator + * + * @code + * tb_for(tb_char_t*, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_for(tb_size_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_for(tb_hash_map_item_ref_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator) + * { + * if (item) tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_for(type, item, head, tail, iterator) \ + /* iterator */ \ + tb_iterator_ref_t item##_iterator = (tb_iterator_ref_t)iterator; \ + tb_assert(!item##_iterator || (tb_iterator_mode(item##_iterator) & (TB_ITERATOR_MODE_FORWARD | TB_ITERATOR_MODE_RACCESS))); \ + /* init */ \ + type item; \ + tb_size_t item##_itor = head; \ + tb_size_t item##_head = head; \ + tb_size_t item##_tail = tail; \ + /* walk */ \ + if (item##_iterator && item##_head != item##_tail) \ + for ( ; \ + item##_itor != item##_tail && ((item = (type)tb_iterator_item(item##_iterator, item##_itor)), 1); \ + item##_itor = tb_iterator_next(item##_iterator, item##_itor)) + +/*! for all items using iterator + * + * @code + * + * tb_for_all(tb_char_t*, item, iterator) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_for_all(tb_size_t, item, iterator) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_for_all(tb_hash_map_item_ref_t, item, iterator) + * { + * if (item) tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_for_all(type, item, iterator) \ + tb_iterator_ref_t item##_iterator_all = (tb_iterator_ref_t)iterator; \ + tb_for(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/for_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/for_if.h index 2550fad7f..ec81c1d26 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/for_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/for_if.h @@ -94,3 +94,99 @@ tb_for_if(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all, cond) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file for_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_FOR_IF_H +#define TB_ALGORITHM_FOR_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! for items using iterator if the condition ok + * + * @code + * tb_for_if(tb_char_t*, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator, item) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_for_if(tb_size_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator, item > 10) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_for_if(tb_hash_map_item_ref_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator, item != tb_null) + * { + * tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_for_if(type, item, head, tail, iterator, cond) \ + /* iterator */ \ + tb_iterator_ref_t item##_iterator = (tb_iterator_ref_t)iterator; \ + tb_assert(!item##_iterator || (tb_iterator_mode(item##_iterator) & (TB_ITERATOR_MODE_FORWARD | TB_ITERATOR_MODE_RACCESS))); \ + /* init */ \ + type item; \ + tb_size_t item##_itor = head; \ + tb_size_t item##_head = head; \ + tb_size_t item##_tail = tail; \ + /* walk */ \ + if (item##_iterator && item##_head != item##_tail) \ + for ( ; \ + item##_itor != item##_tail && ((item = (type)tb_iterator_item(item##_iterator, item##_itor)), 1); \ + item##_itor = tb_iterator_next(item##_iterator, item##_itor)) if ((cond)) + +/*! for all items using iterator if the condition ok + * + * @code + * + * tb_for_all_if(tb_char_t*, item, iterator, item) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_for_all_if(tb_size_t, item, iterator, item > 10) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_for_all_if(tb_hash_map_item_ref_t, item, iterator, item != tb_null) + * { + * if (item) tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_for_all_if(type, item, iterator, cond) \ + tb_iterator_ref_t item##_iterator_all = (tb_iterator_ref_t)iterator; \ + tb_for_if(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all, cond) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/heap_sort.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/heap_sort.h index 79a0239eb..d62caafe1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/heap_sort.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/heap_sort.h @@ -63,3 +63,68 @@ tb_void_t tb_heap_sort_all(tb_iterator_ref_t iterator, tb_iterator_com */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file heap_sort.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_HEAP_SORT_H +#define TB_ALGORITHM_HEAP_SORT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the heap sorter, O(nlog(n)) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_heap_sort(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_iterator_comp_t comp); + +/*! the heap sorter for all + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_heap_sort_all(tb_iterator_ref_t iterator, tb_iterator_comp_t comp); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/insert_sort.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/insert_sort.h index b2a0035e8..2c25e8176 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/insert_sort.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/insert_sort.h @@ -63,3 +63,68 @@ tb_void_t tb_insert_sort_all(tb_iterator_ref_t iterator, tb_iterator_c */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file insert_sort.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_INSERT_SORT_H +#define TB_ALGORITHM_INSERT_SORT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the insert sorter, O(n^2) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_insert_sort(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_iterator_comp_t comp); + +/*! the insert sorter for all + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_insert_sort_all(tb_iterator_ref_t iterator, tb_iterator_comp_t comp); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/predicate.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/predicate.h index 43ee6edee..3d8416722 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/predicate.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/predicate.h @@ -119,3 +119,124 @@ tb_bool_t tb_predicate_beq(tb_iterator_ref_t iterator, tb_cpointer_t i */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file predicate.h + * @ingroup algorithm + */ +#ifndef TB_ALGORITHM_PREDICATE_H +#define TB_ALGORITHM_PREDICATE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the predicate ref type + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * + * @return tb_true or tb_false + */ +typedef tb_bool_t (*tb_predicate_ref_t)(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value); + +/*! the predicate break ref type + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * @param is_break is break now? + * + * @return tb_true or tb_false + */ +typedef tb_bool_t (*tb_predicate_break_ref_t)(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value, tb_bool_t* is_break); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the predicate: if (item == value)? + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_predicate_eq(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value); + +/*! the predicate: if (item < value)? + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_predicate_le(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value); + +/*! the predicate: if (item > value)? + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_predicate_be(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value); + +/*! the predicate: if (item <= value)? + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_predicate_leq(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value); + +/*! the predicate: if (item >= value)? + * + * @param iterator the iterator + * @param item the inner item of the container + * @param value the outer value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_predicate_beq(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/prefix.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/prefix.h index 38aea0c48..0bb78c93c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/prefix.h @@ -33,3 +33,38 @@ #include "../container/container.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_PREFIX_H +#define TB_ALGORITHM_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../container/container.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/quick_sort.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/quick_sort.h index 38841fd6a..43bb960e1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/quick_sort.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/quick_sort.h @@ -63,3 +63,68 @@ tb_void_t tb_quick_sort_all(tb_iterator_ref_t iterator, tb_iterator_co */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file quick_sort.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_QUICK_SORT_H +#define TB_ALGORITHM_QUICK_SORT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the quick sorter, O(nlog(n)) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_quick_sort(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_iterator_comp_t comp); + +/*! the quick sorter for all + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_quick_sort_all(tb_iterator_ref_t iterator, tb_iterator_comp_t comp); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove.h index 7ce4d3ae8..84a403d01 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove.h @@ -52,3 +52,57 @@ tb_void_t tb_remove(tb_iterator_ref_t iterator, tb_cpointer_t value); */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file remove.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_REMOVE_H +#define TB_ALGORITHM_REMOVE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! remove items + * + * @param iterator the iterator + * @param value the value of the predicate + */ +tb_void_t tb_remove(tb_iterator_ref_t iterator, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first.h index d101fc7ba..669c70113 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first.h @@ -52,3 +52,57 @@ tb_void_t tb_remove_first(tb_iterator_ref_t iterator, tb_cpointer_t va */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file remove_first.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_REMOVE_FIRST_H +#define TB_ALGORITHM_REMOVE_FIRST_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! remove the first item + * + * @param iterator the iterator + * @param value the value of the predicate + */ +tb_void_t tb_remove_first(tb_iterator_ref_t iterator, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first_if.h index ecb9886b1..73a44b8b0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_first_if.h @@ -54,3 +54,59 @@ tb_void_t tb_remove_first_if(tb_iterator_ref_t iterator, tb_predicate_ */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file remove_first_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_REMOVE_FIRST_IF_H +#define TB_ALGORITHM_REMOVE_FIRST_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "predicate.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! remove the first item if pred(item, value) + * + * @param iterator the iterator + * @param pred the predicate + * @param value the value of the predicate + */ +tb_void_t tb_remove_first_if(tb_iterator_ref_t iterator, tb_predicate_ref_t pred, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_if.h index 1c0ef5c68..2bda61042 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/remove_if.h @@ -62,3 +62,67 @@ tb_void_t tb_remove_if_until(tb_iterator_ref_t iterator, tb_predicate_ */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file remove_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_REMOVE_IF_H +#define TB_ALGORITHM_REMOVE_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "predicate.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! remove items if pred(item, value) + * + * @param iterator the iterator + * @param pred the predicate + * @param value the value of the predicate + */ +tb_void_t tb_remove_if(tb_iterator_ref_t iterator, tb_predicate_ref_t pred, tb_cpointer_t value); + +/*! remove items if pred(item, value, &is_break) until is_break == tb_true + * + * @param iterator the iterator + * @param pred the predicate with break + * @param value the value of the predicate + */ +tb_void_t tb_remove_if_until(tb_iterator_ref_t iterator, tb_predicate_break_ref_t pred, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind.h index 151a261c3..cc8c464ab 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind.h @@ -65,3 +65,70 @@ tb_size_t tb_rfind_all(tb_iterator_ref_t iterator, tb_cpointer_t value */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file rfind.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_RFIND_H +#define TB_ALGORITHM_RFIND_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! reverse find item + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_rfind(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_cpointer_t value); + +/*! reverse find item for all + * + * @param iterator the iterator + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_rfind_all(tb_iterator_ref_t iterator, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind_if.h index 4488fc9b1..81d1e0f93 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfind_if.h @@ -68,3 +68,73 @@ tb_size_t tb_rfind_all_if(tb_iterator_ref_t iterator, tb_predicate_ref */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file rfind_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_RFIND_IF_H +#define TB_ALGORITHM_RFIND_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "predicate.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! reverse find item if pred(item, value) + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param pred the predicate + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_rfind_if(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_predicate_ref_t pred, tb_cpointer_t value); + +/*! reverse find item for all if pred(item, value) + * + * @param iterator the iterator + * @param pred the predicate + * @param value the value of the predicate + * + * @return the iterator itor, return tb_iterator_tail(iterator) if not found + */ +tb_size_t tb_rfind_all_if(tb_iterator_ref_t iterator, tb_predicate_ref_t pred, tb_cpointer_t value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor.h index 0597b98ec..55399b017 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor.h @@ -94,3 +94,99 @@ tb_rfor(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file rfor.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_RFOR_H +#define TB_ALGORITHM_RFOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! for items using iterator + * + * @code + * tb_rfor(tb_char_t*, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_rfor(tb_size_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_rfor(tb_hash_map_item_ref_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator) + * { + * if (item) tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_rfor(type, item, head, tail, iterator) \ + /* iterator */ \ + tb_iterator_ref_t item##_iterator = (tb_iterator_ref_t)iterator; \ + tb_assert(!item##_iterator || (tb_iterator_mode(item##_iterator) & (TB_ITERATOR_MODE_FORWARD | TB_ITERATOR_MODE_RACCESS))); \ + /* init */ \ + type item; \ + tb_size_t item##_itor; \ + tb_size_t item##_head = head; \ + tb_size_t item##_tail = tail; \ + /* walk */ \ + if (item##_iterator && item##_head != item##_tail) \ + for ( item##_itor = tb_iterator_prev(item##_iterator, item##_tail); \ + item##_itor != item##_tail && ((item = (type)tb_iterator_item(item##_iterator, item##_itor)), item##_itor = item##_itor != item##_head? item##_itor : item##_tail, 1); \ + item##_itor = item##_itor != item##_tail? tb_iterator_prev(item##_iterator, item##_itor) : item##_tail) + +/*! for all items using iterator + * + * @code + * + * tb_rfor_all(tb_char_t*, item, iterator) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_rfor_all(tb_size_t, item, iterator) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_rfor_all(tb_hash_map_item_ref_t, item, iterator) + * { + * if (item) tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_rfor_all(type, item, iterator) \ + tb_iterator_ref_t item##_iterator_all = (tb_iterator_ref_t)iterator; \ + tb_rfor(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor_if.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor_if.h index 494e114a9..37c6dc14b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor_if.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/rfor_if.h @@ -94,3 +94,99 @@ tb_rfor_if(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all, cond) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file rfor_if.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_RFOR_IF_H +#define TB_ALGORITHM_RFOR_IF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! for items using iterator + * + * @code + * tb_rfor_if(tb_char_t*, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator, item) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_rfor_if(tb_size_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator, item > 10) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_rfor_if(tb_hash_map_item_ref_t, item, tb_iterator_head(iterator), tb_iterator_tail(iterator), iterator, item != tb_null) + * { + * tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_rfor_if(type, item, head, tail, iterator, cond) \ + /* iterator */ \ + tb_iterator_ref_t item##_iterator = (tb_iterator_ref_t)iterator; \ + tb_assert(!item##_iterator || (tb_iterator_mode(item##_iterator) & (TB_ITERATOR_MODE_FORWARD | TB_ITERATOR_MODE_RACCESS))); \ + /* init */ \ + type item; \ + tb_size_t item##_itor; \ + tb_size_t item##_head = head; \ + tb_size_t item##_tail = tail; \ + /* walk */ \ + if (item##_iterator && item##_head != item##_tail) \ + for ( item##_itor = tb_iterator_prev(item##_iterator, item##_tail); \ + item##_itor != item##_tail && ((item = (type)tb_iterator_item(item##_iterator, item##_itor)), item##_itor = item##_itor != item##_head? item##_itor : item##_tail, 1); \ + item##_itor = item##_itor != item##_tail? tb_iterator_prev(item##_iterator, item##_itor) : item##_tail) if ((cond)) + +/*! for all items using iterator + * + * @code + * + * tb_rfor_all(tb_char_t*, item, iterator, item) + * { + * tb_trace_d("item: %s", item); + * } + * + * tb_rfor_all(tb_size_t, item, iterator, itme > 10) + * { + * tb_trace_d("item: %lu", item); + * } + * + * tb_rfor_all(tb_hash_map_item_ref_t, item, iterator, item != tb_null) + * { + * tb_trace_d("item: %p => %p", item->name, item->data); + * } + * @endcode + */ +#define tb_rfor_all_if(type, item, iterator, cond) \ + tb_iterator_ref_t item##_iterator_all = (tb_iterator_ref_t)iterator; \ + tb_rfor_if(type, item, tb_iterator_head(item##_iterator_all), tb_iterator_tail(item##_iterator_all), item##_iterator_all, cond) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/rwalk.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/rwalk.h index f056d1975..99cf074f4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/rwalk.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/rwalk.h @@ -74,3 +74,79 @@ tb_size_t tb_rwalk_all(tb_iterator_ref_t iterator, tb_rwalk_func_t fun */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file rwalk.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_RWALK_H +#define TB_ALGORITHM_RWALK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the reverse walk func type +typedef tb_bool_t (*tb_rwalk_func_t)(tb_iterator_ref_t iterator, tb_pointer_t item, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the reverse walker + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param func the walker func + * @param priv the func private data + * + * @return the item count + */ +tb_size_t tb_rwalk(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_rwalk_func_t func, tb_cpointer_t priv); + +/*! the reverse walker for all + * + * @param iterator the iterator + * @param func the walker func + * @param priv the func private data + * + * @return the item count + */ +tb_size_t tb_rwalk_all(tb_iterator_ref_t iterator, tb_rwalk_func_t func, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/sort.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/sort.h index 3f32271af..119adc684 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/sort.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/sort.h @@ -63,3 +63,68 @@ tb_void_t tb_sort_all(tb_iterator_ref_t iterator, tb_iterator_comp_t c */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file sort.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_SORT_H +#define TB_ALGORITHM_SORT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the sorter + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_sort(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_iterator_comp_t comp); + +/*! the sorter for all + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param comp the comparer + */ +tb_void_t tb_sort_all(tb_iterator_ref_t iterator, tb_iterator_comp_t comp); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/algorithm/walk.h b/core/pkg/tbox.pkg/inc/tbox/algorithm/walk.h index 8386f76d2..1add2d05d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/algorithm/walk.h +++ b/core/pkg/tbox.pkg/inc/tbox/algorithm/walk.h @@ -74,3 +74,79 @@ tb_size_t tb_walk_all(tb_iterator_ref_t iterator, tb_walk_func_t func, */ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file walk.h + * @ingroup algorithm + * + */ +#ifndef TB_ALGORITHM_WALK_H +#define TB_ALGORITHM_WALK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the walk func type +typedef tb_bool_t (*tb_walk_func_t)(tb_iterator_ref_t iterator, tb_pointer_t item, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the walker + * + * @param iterator the iterator + * @param head the iterator head + * @param tail the iterator tail + * @param func the walker func + * @param priv the func private data + * + * @return the item count + */ +tb_size_t tb_walk(tb_iterator_ref_t iterator, tb_size_t head, tb_size_t tail, tb_walk_func_t func, tb_cpointer_t priv); + +/*! the walker for all + * + * @param iterator the iterator + * @param func the walker func + * @param priv the func private data + * + * @return the item count + */ +tb_size_t tb_walk_all(tb_iterator_ref_t iterator, tb_walk_func_t func, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/asio.h b/core/pkg/tbox.pkg/inc/tbox/asio/asio.h index bb4f1ea7d..2534aeaad 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/asio.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/asio.h @@ -34,3 +34,39 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file asio.h + * + */ +#ifndef TB_ASIO_H +#define TB_ASIO_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_API_HAVE_DEPRECATED +# include "deprecated/deprecated.h" +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aice.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aice.h index 90a4ea661..2876155fd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aice.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aice.h @@ -482,3 +482,487 @@ typedef struct __tb_aice_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file aice.h + * @ingroup asio + */ +#ifndef TB_ASIO_AICE_H +#define TB_ASIO_AICE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "aico.h" +#include "../../network/ipaddr.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the aice code enum +typedef enum __tb_aice_code_e +{ + TB_AICE_CODE_NONE = 0 + +, TB_AICE_CODE_ACPT = 1 //!< for sock, accept it +, TB_AICE_CODE_CONN = 2 //!< for sock, connect to the host address +, TB_AICE_CODE_RECV = 3 //!< for sock, recv data for tcp +, TB_AICE_CODE_SEND = 4 //!< for sock, send data for tcp +, TB_AICE_CODE_URECV = 5 //!< for sock, recv data for udp +, TB_AICE_CODE_USEND = 6 //!< for sock, send data for udp +, TB_AICE_CODE_RECVV = 7 //!< for sock, recv iovec data for tcp +, TB_AICE_CODE_SENDV = 8 //!< for sock, send iovec data for tcp +, TB_AICE_CODE_URECVV = 9 //!< for sock, recv iovec data for udp +, TB_AICE_CODE_USENDV = 10 //!< for sock, send iovec data for udp +, TB_AICE_CODE_SENDF = 11 //!< for sock, maybe return TB_STATE_NOT_SUPPORTED + +, TB_AICE_CODE_READ = 12 //!< for file, read data +, TB_AICE_CODE_WRIT = 13 //!< for file, writ data +, TB_AICE_CODE_READV = 14 //!< for file, read iovec data +, TB_AICE_CODE_WRITV = 15 //!< for file, writ iovec data +, TB_AICE_CODE_FSYNC = 16 //!< for file, flush data to file + +, TB_AICE_CODE_RUNTASK = 17 //!< for task or sock or file, run task with the given delay +, TB_AICE_CODE_CLOS = 18 //!< for task or sock or file + +, TB_AICE_CODE_MAXN = 19 + +}tb_aice_code_e; + +/// the acpt aice type +typedef struct __tb_aice_acpt_t +{ + /// the client aico + tb_aico_ref_t aico; + + /// the client addr + tb_ipaddr_t addr; + + /// the private data for using the left space of the union + tb_cpointer_t priv[1]; + +}tb_aice_acpt_t; + +/// the conn aice type +typedef struct __tb_aice_conn_t +{ + /// the addr + tb_ipaddr_t addr; + +}tb_aice_conn_t; + +#ifdef TB_CONFIG_OS_WINDOWS +/// the recv aice type, base: tb_iovec_t +typedef struct __tb_aice_recv_t +{ + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the recv data for (tb_iovec_t*)->data + tb_byte_t* data; + + /// the data real + tb_size_t real; + +}tb_aice_recv_t; + +/// the send aice type, base: tb_iovec_t +typedef struct __tb_aice_send_t +{ + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the send data for (tb_iovec_t*)->data + tb_byte_t const* data; + + /// the data real + tb_size_t real; + +}tb_aice_send_t; + +/// the urecv aice type, base: tb_iovec_t +typedef struct __tb_aice_urecv_t +{ + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the recv data for (tb_iovec_t*)->data + tb_byte_t* data; + + /// the data real + tb_size_t real; + + /// the addr + tb_ipaddr_t addr; + +}tb_aice_urecv_t; + +/// the usend aice type, base: tb_iovec_t +typedef struct __tb_aice_usend_t +{ + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the send data for (tb_iovec_t*)->data + tb_byte_t const* data; + + /// the data real + tb_size_t real; + + /// the peer addr + tb_ipaddr_t addr; + +}tb_aice_usend_t; + +/// the read aice type, base: tb_iovec_t +typedef struct __tb_aice_read_t +{ + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the read data for (tb_iovec_t*)->data + tb_byte_t* data; + + /// the data real + tb_size_t real; + + /// the file seek + tb_hize_t seek; + +}tb_aice_read_t; + +/// the writ aice type, base: tb_iovec_t +typedef struct __tb_aice_writ_t +{ + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the writ data for (tb_iovec_t*)->data + tb_byte_t const* data; + + /// the data real + tb_size_t real; + + /// the file seek + tb_hize_t seek; + +}tb_aice_writ_t; +#else +/// the recv aice type, base: tb_iovec_t +typedef struct __tb_aice_recv_t +{ + /// the recv data for (tb_iovec_t*)->data + tb_byte_t* data; + + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the data real + tb_size_t real; + +}tb_aice_recv_t; + +/// the send aice type, base: tb_iovec_t +typedef struct __tb_aice_send_t +{ + /// the send data for (tb_iovec_t*)->data + tb_byte_t const* data; + + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the data real + tb_size_t real; + +}tb_aice_send_t; + +/// the urecv aice type, base: tb_iovec_t +typedef struct __tb_aice_urecv_t +{ + /// the recv data for (tb_iovec_t*)->data + tb_byte_t* data; + + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the data real + tb_size_t real; + + /// the addr + tb_ipaddr_t addr; + +}tb_aice_urecv_t; + +/// the usend aice type, base: tb_iovec_t +typedef struct __tb_aice_usend_t +{ + /// the send data for (tb_iovec_t*)->data + tb_byte_t const* data; + + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the data real + tb_size_t real; + + /// the addr + tb_ipaddr_t addr; + +}tb_aice_usend_t; + +/// the read aice type, base: tb_iovec_t +typedef struct __tb_aice_read_t +{ + /// the read data for (tb_iovec_t*)->data + tb_byte_t* data; + + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the data real + tb_size_t real; + + /// the file seek + tb_hize_t seek; + +}tb_aice_read_t; + +/// the writ aice type, base: tb_iovec_t +typedef struct __tb_aice_writ_t +{ + /// the writ data for (tb_iovec_t*)->data + tb_byte_t const* data; + + /// the data size for (tb_iovec_t*)->size + tb_iovec_size_t size; + + /// the data real + tb_size_t real; + + /// the file seek + tb_hize_t seek; + +}tb_aice_writ_t; +#endif + +/// the recvv aice type +typedef struct __tb_aice_recvv_t +{ + /// the recv list + tb_iovec_t const* list; + + /// the list size + tb_size_t size; + + /// the data real + tb_size_t real; + +}tb_aice_recvv_t; + +/// the sendv aice type +typedef struct __tb_aice_sendv_t +{ + /// the send list + tb_iovec_t const* list; + + /// the list size + tb_size_t size; + + /// the data real + tb_size_t real; + +}tb_aice_sendv_t; + +/// the urecvv aice type +typedef struct __tb_aice_urecvv_t +{ + /// the recv list + tb_iovec_t const* list; + + /// the list size + tb_size_t size; + + /// the data real + tb_size_t real; + + /// the peer addr + tb_ipaddr_t addr; + +}tb_aice_urecvv_t; + +/// the usendv aice type +typedef struct __tb_aice_usendv_t +{ + /// the send list + tb_iovec_t const* list; + + /// the list size + tb_size_t size; + + /// the data real + tb_size_t real; + + /// the addr + tb_ipaddr_t addr; + +}tb_aice_usendv_t; + +/// the sendf aice type +typedef struct __tb_aice_sendf_t +{ + /// the file + tb_file_ref_t file; + + /// the private data for using the left space of the union + tb_handle_t priv[1]; + + /// the real + tb_size_t real; + + /// the size + tb_hize_t size; + + /// the seek + tb_hize_t seek; + +}tb_aice_sendf_t; + +/// the readv aice type +typedef struct __tb_aice_readv_t +{ + /// the read list + tb_iovec_t const* list; + + /// the list size + tb_size_t size; + + /// the data real + tb_size_t real; + + /// the file seek + tb_hize_t seek; + +}tb_aice_readv_t; + +/// the writv aice type +typedef struct __tb_aice_writv_t +{ + /// the writ list + tb_iovec_t const* list; + + /// the list size + tb_size_t size; + + /// the data real + tb_size_t real; + + /// the file seek + tb_hize_t seek; + +}tb_aice_writv_t; + +/// the runtask aice type +typedef struct __tb_aice_runtask_t +{ + /// the when + tb_hize_t when; + + /// the delay + tb_size_t delay; + +}tb_aice_runtask_t; + +/// the aice type +typedef struct __tb_aice_t +{ + /// the aice code + tb_uint8_t code; + + /*! the state + * + * TB_STATE_OK + * TB_STATE_FAILED + * TB_STATE_KILLED + * TB_STATE_CLOSED + * TB_STATE_PENDING + * TB_STATE_TIMEOUT + * TB_STATE_NOT_SUPPORTED + */ + tb_size_t state; + + /// the aico func + tb_aico_func_t func; + + /// the aico private data + tb_cpointer_t priv; + + /// the aico + tb_aico_ref_t aico; + + /*! the events + * + * tb_iovec_t must be aligned by cpu-bytes for windows WSABUF + */ +#ifdef TB_CONFIG_OS_WINDOWS + __tb_cpu_aligned__ union +#else + union +#endif + { + // for sock + tb_aice_acpt_t acpt; + tb_aice_conn_t conn; + tb_aice_recv_t recv; + tb_aice_send_t send; + tb_aice_urecv_t urecv; + tb_aice_usend_t usend; + tb_aice_recvv_t recvv; + tb_aice_sendv_t sendv; + tb_aice_urecvv_t urecvv; + tb_aice_usendv_t usendv; + tb_aice_sendf_t sendf; + + // for file + tb_aice_read_t read; + tb_aice_writ_t writ; + tb_aice_readv_t readv; + tb_aice_writv_t writv; + + // for task + tb_aice_runtask_t runtask; + + } u; + +}tb_aice_t, *tb_aice_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aico.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aico.h index c7c32201d..0c3b2fbac 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aico.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aico.h @@ -731,3 +731,736 @@ tb_bool_t tb_aico_task_run_(tb_aico_ref_t aico, tb_size_t delay, tb_ai __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file aico.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_AICO_H +#define TB_ASIO_AICO_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../../network/ipaddr.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_aico_clos(aico, func, priv) tb_aico_clos_(aico, func, priv __tb_debug_vals__) +#define tb_aico_acpt(aico, func, priv) tb_aico_acpt_(aico, func, priv __tb_debug_vals__) +#define tb_aico_conn(aico, addr, func, priv) tb_aico_conn_(aico, addr, func, priv __tb_debug_vals__) +#define tb_aico_recv(aico, data, size, func, priv) tb_aico_recv_(aico, data, size, func, priv __tb_debug_vals__) +#define tb_aico_send(aico, data, size, func, priv) tb_aico_send_(aico, data, size, func, priv __tb_debug_vals__) +#define tb_aico_urecv(aico, data, size, func, priv) tb_aico_urecv_(aico, data, size, func, priv __tb_debug_vals__) +#define tb_aico_usend(aico, addr, data, size, func, priv) tb_aico_usend_(aico, addr, data, size, func, priv __tb_debug_vals__) +#define tb_aico_recvv(aico, list, size, func, priv) tb_aico_recvv_(aico, list, size, func, priv __tb_debug_vals__) +#define tb_aico_sendv(aico, list, size, func, priv) tb_aico_sendv_(aico, list, size, func, priv __tb_debug_vals__) +#define tb_aico_urecvv(aico, addr, list, size, func, priv) tb_aico_urecvv_(aico, addr, list, size, func, priv __tb_debug_vals__) +#define tb_aico_usendv(aico, addr, list, size, func, priv) tb_aico_usendv_(aico, addr, list, size, func, priv __tb_debug_vals__) +#define tb_aico_sendf(aico, file, seek, size, func, priv) tb_aico_sendf_(aico, file, seek, size, func, priv __tb_debug_vals__) +#define tb_aico_read(aico, seek, data, size, func, priv) tb_aico_read_(aico, seek, data, size, func, priv __tb_debug_vals__) +#define tb_aico_writ(aico, seek, data, size, func, priv) tb_aico_writ_(aico, seek, data, size, func, priv __tb_debug_vals__) +#define tb_aico_readv(aico, seek, list, size, func, priv) tb_aico_readv_(aico, seek, list, size, func, priv __tb_debug_vals__) +#define tb_aico_writv(aico, seek, list, size, func, priv) tb_aico_writv_(aico, seek, list, size, func, priv __tb_debug_vals__) +#define tb_aico_fsync(aico, func, priv) tb_aico_fsync_(aico, func, priv __tb_debug_vals__) + +#define tb_aico_clos_after(aico, delay, func, priv) tb_aico_clos_after_(aico, delay, func, priv __tb_debug_vals__) +#define tb_aico_acpt_after(aico, delay, func, priv) tb_aico_acpt_after_(aico, delay, func, priv __tb_debug_vals__) +#define tb_aico_conn_after(aico, delay, addr, func, priv) tb_aico_conn_after_(aico, delay, addr, func, priv __tb_debug_vals__) +#define tb_aico_recv_after(aico, delay, data, size, func, priv) tb_aico_recv_after_(aico, delay, data, size, func, priv __tb_debug_vals__) +#define tb_aico_send_after(aico, delay, data, size, func, priv) tb_aico_send_after_(aico, delay, data, size, func, priv __tb_debug_vals__) +#define tb_aico_urecv_after(aico, delay, data, size, func, priv) tb_aico_urecv_after_(aico, delay, data, size, func, priv __tb_debug_vals__) +#define tb_aico_usend_after(aico, delay, addr, data, size, func, priv) tb_aico_usend_after_(aico, delay, addr, data, size, func, priv __tb_debug_vals__) +#define tb_aico_recvv_after(aico, delay, list, size, func, priv) tb_aico_recvv_after_(aico, delay, list, size, func, priv __tb_debug_vals__) +#define tb_aico_sendv_after(aico, delay, list, size, func, priv) tb_aico_sendv_after_(aico, delay, list, size, func, priv __tb_debug_vals__) +#define tb_aico_urecvv_after(aico, delay, addr, list, size, func, priv) tb_aico_urecvv_after_(aico, delay, addr, list, size, func, priv __tb_debug_vals__) +#define tb_aico_usendv_after(aico, delay, addr, list, size, func, priv) tb_aico_usendv_after_(aico, delay, addr, list, size, func, priv __tb_debug_vals__) +#define tb_aico_sendf_after(aico, delay, file, seek, size, func, priv) tb_aico_sendf_after_(aico, delay, file, seek, size, func, priv __tb_debug_vals__) +#define tb_aico_read_after(aico, delay, seek, data, size, func, priv) tb_aico_read_after_(aico, delay, seek, data, size, func, priv __tb_debug_vals__) +#define tb_aico_writ_after(aico, delay, seek, data, size, func, priv) tb_aico_writ_after_(aico, delay, seek, data, size, func, priv __tb_debug_vals__) +#define tb_aico_readv_after(aico, delay, seek, list, size, func, priv) tb_aico_readv_after_(aico, delay, seek, list, size, func, priv __tb_debug_vals__) +#define tb_aico_writv_after(aico, delay, seek, list, size, func, priv) tb_aico_writv_after_(aico, delay, seek, list, size, func, priv __tb_debug_vals__) +#define tb_aico_fsync_after(aico, delay, func, priv) tb_aico_fsync_after_(aico, delay, func, priv __tb_debug_vals__) + +#define tb_aico_task_run(aico, delay, func, priv) tb_aico_task_run_(aico, delay, func, priv __tb_debug_vals__) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +struct __tb_aice_t; +/// the aico func type +typedef tb_bool_t (*tb_aico_func_t)(struct __tb_aice_t* aice); + +/// the aico type enum +typedef enum __tb_aico_type_e +{ + TB_AICO_TYPE_NONE = 0 //!< null +, TB_AICO_TYPE_SOCK = 1 //!< sock +, TB_AICO_TYPE_FILE = 2 //!< file +, TB_AICO_TYPE_TASK = 3 //!< task +, TB_AICO_TYPE_MAXN = 4 + +}tb_aico_type_e; + +/// the aico timeout enum, only for sock +typedef enum __tb_aico_timeout_e +{ + TB_AICO_TIMEOUT_CONN = 0 +, TB_AICO_TIMEOUT_RECV = 1 +, TB_AICO_TIMEOUT_SEND = 2 +, TB_AICO_TIMEOUT_MAXN = 3 + +}tb_aico_timeout_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the aico + * + * @param aicp the aicp + * + * @return the aico + */ +__tb_deprecated__ +tb_aico_ref_t tb_aico_init(tb_aicp_ref_t aicp); + +/*! open the sock aico + * + * @param aicp the aicp + * @param sock the socket + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_open_sock(tb_aico_ref_t aico, tb_socket_ref_t sock); + +/*! open the sock aico from the socket type + * + * @param aicp the aicp + * @param type the socket type + * @param family the address family, default: ipv4 + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_open_sock_from_type(tb_aico_ref_t aico, tb_size_t type, tb_size_t family); + +/*! open the file aico + * + * @param aicp the aicp + * @param file the file + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_open_file(tb_aico_ref_t aico, tb_file_ref_t file); + +/*! open the file aico from path + * + * @param aicp the aicp + * @param path the file path + * @param mode the file mode + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_open_file_from_path(tb_aico_ref_t aico, tb_char_t const* path, tb_size_t mode); + +/*! open the task aico + * + * @param aicp the aicp + * @param ltimer is the lower precision timer? + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_open_task(tb_aico_ref_t aico, tb_bool_t ltimer); + +/*! kill the aico + * + * @param aico the aico + */ +__tb_deprecated__ +tb_void_t tb_aico_kill(tb_aico_ref_t aico); + +/*! exit the aico + * + * @param aico the aico + */ +__tb_deprecated__ +tb_void_t tb_aico_exit(tb_aico_ref_t aico); + +/*! the aico aicp + * + * @param aico the aico + * + * @return the aico aicp + */ +__tb_deprecated__ +tb_aicp_ref_t tb_aico_aicp(tb_aico_ref_t aico); + +/*! the aico type + * + * @param aico the aico + * + * @return the aico type + */ +__tb_deprecated__ +tb_size_t tb_aico_type(tb_aico_ref_t aico); + +/*! get the socket if the aico is socket type + * + * @param aico the aico + * + * @return the socket + */ +__tb_deprecated__ +tb_socket_ref_t tb_aico_sock(tb_aico_ref_t aico); + +/*! get the file if the aico is file type + * + * @param aico the aico + * + * @return the file + */ +__tb_deprecated__ +tb_file_ref_t tb_aico_file(tb_aico_ref_t aico); + +/*! try to close it + * + * @param aico the aico + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_clos_try(tb_aico_ref_t aico); + +/*! the aico timeout + * + * @param aico the aico + * @param type the timeout type + * + * @return the timeout + */ +__tb_deprecated__ +tb_long_t tb_aico_timeout(tb_aico_ref_t aico, tb_size_t type); + +/*! set the aico timeout + * + * @param aico the aico + * @param type the timeout type + * @param timeout the timeout + */ +__tb_deprecated__ +tb_void_t tb_aico_timeout_set(tb_aico_ref_t aico, tb_size_t type, tb_long_t timeout); + +/*! post the clos + * + * @param aicp the aicp + * @param func the func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_clos_(tb_aico_ref_t aico, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the acpt + * + * @param aico the aico + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_acpt_(tb_aico_ref_t aico, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the conn + * + * @param aico the aico + * @param addr the address + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_conn_(tb_aico_ref_t aico, tb_ipaddr_ref_t addr, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the recv for sock + * + * @param aico the aico + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_recv_(tb_aico_ref_t aico, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the send for sock + * + * @param aico the aico + * @param data the data + * @param size the size, send the left file data if size == 0 + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_send_(tb_aico_ref_t aico, tb_byte_t const* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the urecv for sock + * + * @param aico the aico + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_urecv_(tb_aico_ref_t aico, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the usend for sock + * + * @param aico the aico + * @param addr the addr + * @param data the data + * @param size the size, send the left file data if size == 0 + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_usend_(tb_aico_ref_t aico, tb_ipaddr_ref_t addr, tb_byte_t const* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the recvv for sock + * + * @param aico the aico + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_recvv_(tb_aico_ref_t aico, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the sendv for sock + * + * @param aico the aico + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_sendv_(tb_aico_ref_t aico, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the urecvv for sock + * + * @param aico the aico + * @param addr the addr + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_urecvv_(tb_aico_ref_t aico, tb_ipaddr_ref_t addr, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the usendv for sock + * + * @param aico the aico + * @param addr the addr + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_usendv_(tb_aico_ref_t aico, tb_ipaddr_ref_t addr, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the sendfile for sock + * + * @param aico the aico + * @param file the file handle + * @param seek the seek + * @param size the size, send the left data if size == 0 + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_sendf_(tb_aico_ref_t aico, tb_file_ref_t file, tb_hize_t seek, tb_hize_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the read for file + * + * @param aico the aico + * @param seek the seek + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_read_(tb_aico_ref_t aico, tb_hize_t seek, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the writ for file + * + * @param aico the aico + * @param seek the seek + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_writ_(tb_aico_ref_t aico, tb_hize_t seek, tb_byte_t const* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the readv for file + * + * @param aico the aico + * @param seek the seek + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_readv_(tb_aico_ref_t aico, tb_hize_t seek, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the writv for file + * + * @param aico the aico + * @param seek the seek + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_writv_(tb_aico_ref_t aico, tb_hize_t seek, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the fsync for file + * + * @param aico the aico + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_fsync_(tb_aico_ref_t aico, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the clos after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_clos_after_(tb_aico_ref_t aico, tb_size_t delay, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the acpt after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_acpt_after_(tb_aico_ref_t aico, tb_size_t delay, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the conn after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param addr the address + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_conn_after_(tb_aico_ref_t aico, tb_size_t delay, tb_ipaddr_ref_t addr, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the recv for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_recv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the send for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param data the data + * @param size the size, send the left file data if size == 0 + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_send_after_(tb_aico_ref_t aico, tb_size_t delay, tb_byte_t const* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the urecv for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_urecv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the usend for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param addr the addr + * @param data the data + * @param size the size, send the left file data if size == 0 + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_usend_after_(tb_aico_ref_t aico, tb_size_t delay, tb_ipaddr_ref_t addr, tb_byte_t const* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the recvv for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_recvv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the sendv for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_sendv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the urecvv for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param addr the addr + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_urecvv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_ipaddr_ref_t addr, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the usendv for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param addr the addr + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_usendv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_ipaddr_ref_t addr, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the sendfile for sock after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param file the file handle + * @param seek the seek + * @param size the size, send the left data if size == 0 + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_sendf_after_(tb_aico_ref_t aico, tb_size_t delay, tb_file_ref_t file, tb_hize_t seek, tb_hize_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the read for file after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param seek the seek + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_read_after_(tb_aico_ref_t aico, tb_size_t delay, tb_hize_t seek, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the writ for file after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param seek the seek + * @param data the data + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_writ_after_(tb_aico_ref_t aico, tb_size_t delay, tb_hize_t seek, tb_byte_t const* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the readv for file after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param seek the seek + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_readv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_hize_t seek, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the writv for file after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param seek the seek + * @param list the list + * @param size the size + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_writv_after_(tb_aico_ref_t aico, tb_size_t delay, tb_hize_t seek, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! post the fsync for file after the delay time + * + * @param aico the aico + * @param delay the delay time, ms + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_fsync_after_(tb_aico_ref_t aico, tb_size_t delay, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! run aico task after timeout and will be auto-remove it after be expired + * + * only once, need continue to call it again if want to repeat task + * + * @param aico the aico + * @param delay the delay time, ms + * @param func the callback func + * @param priv the callback data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aico_task_run_(tb_aico_ref_t aico, tb_size_t delay, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aicp.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aicp.h index a3e302952..90c35a5a3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aicp.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aicp.h @@ -178,3 +178,183 @@ tb_hong_t tb_aicp_time(tb_aicp_ref_t aicp); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file aicp.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_AICP_H +#define TB_ASIO_AICP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "aice.h" +#include "../../platform/timer.h" +#include "../../container/container.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// post +#define tb_aicp_post(aicp, aice) tb_aicp_post_(aicp, aice __tb_debug_vals__) +#define tb_aicp_post_after(aicp, delay, aice) tb_aicp_post_after_(aicp, delay, aice __tb_debug_vals__) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the aicp instance + * + * @return the aicp + */ +__tb_deprecated__ +tb_aicp_ref_t tb_aicp(tb_noarg_t); + +/*! init the aicp + * + * @param maxn the aico maxn, using the default maxn if be zero + * + * @return the aicp + */ +__tb_deprecated__ +tb_aicp_ref_t tb_aicp_init(tb_size_t maxn); + +/*! exit the aicp + * + * @param aicp the aicp + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_exit(tb_aicp_ref_t aicp); + +/*! the aico maxn + * + * @param aicp the aicp + * + * @return the aico maxn + */ +__tb_deprecated__ +tb_size_t tb_aicp_maxn(tb_aicp_ref_t aicp); + +/*! post the aice + * + * @param aicp the aicp + * @param aice the aice + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_post_(tb_aicp_ref_t aicp, tb_aice_ref_t aice __tb_debug_decl__); + +/*! post the aice + * + * @param aicp the aicp + * @param delay the delay time, ms + * @param aice the aice + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_post_after_(tb_aicp_ref_t aicp, tb_size_t delay, tb_aice_ref_t aice __tb_debug_decl__); + +/*! loop aicp for the external thread + * + * @code + * tb_pointer_t tb_aicp_worker_thread(tb_pointer_t) + * { + * tb_aicp_loop(aicp); + * } + * @endcode + * + * @param aicp the aicp + */ +__tb_deprecated__ +tb_void_t tb_aicp_loop(tb_aicp_ref_t aicp); + +/*! loop aicp util ... for the external thread + * + * @code + * tb_bool_t tb_aicp_stop_func(tb_pointer_t) + * { + * if (...) return tb_true; + * return tb_false; + * } + * tb_pointer_t tb_aicp_worker_thread(tb_pointer_t) + * { + * tb_aicp_loop_util(aicp, stop_func, tb_null); + * } + * @endcode + * + * @param aicp the aicp + */ +__tb_deprecated__ +tb_void_t tb_aicp_loop_util(tb_aicp_ref_t aicp, tb_bool_t (*stop)(tb_cpointer_t priv), tb_cpointer_t priv); + +/*! kill loop + * + * @param aicp the aicp + */ +__tb_deprecated__ +tb_void_t tb_aicp_kill(tb_aicp_ref_t aicp); + +/*! kill all and cannot continue to post it, but not kill loop + * + * @param aicp the aicp + */ +__tb_deprecated__ +tb_void_t tb_aicp_kill_all(tb_aicp_ref_t aicp); + +/*! wait all exiting + * + * @param aicp the aicp + * @param timeout the timeout + * + * @return ok: > 0, timeout: 0, failed: -1 + */ +__tb_deprecated__ +tb_long_t tb_aicp_wait_all(tb_aicp_ref_t aicp, tb_long_t timeout); + +/*! the spak time + * + * @param aicp the aicp + * + * @return the time + */ +__tb_deprecated__ +tb_hong_t tb_aicp_time(tb_aicp_ref_t aicp); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioe.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioe.h index 7bc84c34e..fa84b7b71 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioe.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioe.h @@ -74,3 +74,79 @@ typedef struct __tb_aioe_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file aioe.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_AIOE_H +#define TB_ASIO_AIOE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the aioe code enum, only for sock +typedef enum __tb_aioe_code_e +{ + TB_AIOE_CODE_NONE = 0x0000 +, TB_AIOE_CODE_CONN = 0x0001 +, TB_AIOE_CODE_ACPT = 0x0002 +, TB_AIOE_CODE_RECV = 0x0004 +, TB_AIOE_CODE_SEND = 0x0008 +, TB_AIOE_CODE_EALL = TB_AIOE_CODE_RECV | TB_AIOE_CODE_SEND | TB_AIOE_CODE_ACPT | TB_AIOE_CODE_CONN +, TB_AIOE_CODE_CLEAR = 0x0010 //!< edge trigger. after the event is retrieved by the user, its state is reset +, TB_AIOE_CODE_ONESHOT = 0x0020 //!< causes the event to return only the first occurrence of the filter being triggered + +}tb_aioe_code_e; + +/// the aioe type +typedef struct __tb_aioe_t +{ + /// the code + tb_size_t code; + + /// the priv + tb_cpointer_t priv; + + /// the aioo + tb_aioo_ref_t aioo; + +}tb_aioe_t, *tb_aioe_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioo.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioo.h index f6c8bc7ed..714343642 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioo.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aioo.h @@ -70,3 +70,75 @@ tb_long_t tb_aioo_wait(tb_socket_ref_t sock, tb_size_t code, tb_long_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file aioo.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_AIOO_H +#define TB_ASIO_AIOO_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the aioo handle + * + * @param aioo the aioo + * + * @return the socket + */ +__tb_deprecated__ +tb_socket_ref_t tb_aioo_sock(tb_aioo_ref_t aioo); + +/*! wait the aioo + * + * blocking wait the single event aioo, so need not aiop + * return the event type if ok, otherwise return 0 for timeout + * + * @param sock the sock + * @param code the aioe code + * @param timeout the timeout, infinity: -1 + * + * @return > 0: the aioe code, 0: timeout, -1: failed + */ +__tb_deprecated__ +tb_long_t tb_aioo_wait(tb_socket_ref_t sock, tb_size_t code, tb_long_t timeout); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aiop.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aiop.h index 5a9181f59..15f0d7309 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aiop.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/aiop.h @@ -154,3 +154,159 @@ tb_long_t tb_aiop_wait(tb_aiop_ref_t aiop, tb_aioe_ref_t list, tb_size __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file aiop.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_AIOP_H +#define TB_ASIO_AIOP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "aioo.h" +#include "aioe.h" +#include "../../platform/prefix.h" +#include "../../container/container.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the aiop + * + * @param maxn the maximum number of concurrent objects + * + * @return the aiop + */ +__tb_deprecated__ +tb_aiop_ref_t tb_aiop_init(tb_size_t maxn); + +/*! exit the aiop + * + * @param aiop the aiop + */ +__tb_deprecated__ +tb_void_t tb_aiop_exit(tb_aiop_ref_t aiop); + +/*! cler the aiop + * + * @param aiop the aiop + */ +__tb_deprecated__ +tb_void_t tb_aiop_cler(tb_aiop_ref_t aiop); + +/*! kill the aiop + * + * @param aiop the aiop + */ +__tb_deprecated__ +tb_void_t tb_aiop_kill(tb_aiop_ref_t aiop); + +/*! spak the aiop, break the wait + * + * @param aiop the aiop + */ +__tb_deprecated__ +tb_void_t tb_aiop_spak(tb_aiop_ref_t aiop); + +/*! the aioe code is supported for the aiop? + * + * @param aiop the aiop + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aiop_have(tb_aiop_ref_t aiop, tb_size_t code); + +/*! addo the aioo + * + * @param aiop the aiop + * @param sock the socket + * @param code the code + * @param priv the private data + * + * @return the aioo + */ +__tb_deprecated__ +tb_aioo_ref_t tb_aiop_addo(tb_aiop_ref_t aiop, tb_socket_ref_t sock, tb_size_t code, tb_cpointer_t priv); + +/*! delo the aioo + * + * @param aiop the aiop + * @param aioo the aioo + * + */ +__tb_deprecated__ +tb_void_t tb_aiop_delo(tb_aiop_ref_t aiop, tb_aioo_ref_t aioo); + +/*! post the aioe + * + * @param aiop the aiop + * @param aioe the aioe + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aiop_post(tb_aiop_ref_t aiop, tb_aioe_ref_t aioe); + +/*! set the aioe + * + * @param aiop the aiop + * @param aioo the aioo + * @param code the code + * @param priv the private data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aiop_sete(tb_aiop_ref_t aiop, tb_aioo_ref_t aioo, tb_size_t code, tb_cpointer_t priv); + +/*! wait the asio objects in the pool + * + * blocking wait the multiple event objects + * return the event number if ok, otherwise return 0 for timeout + * + * @param aiop the aiop + * @param list the aioe list + * @param maxn the aioe maxn + * @param timeout the timeout, infinity: -1 + * + * @return > 0: the aioe list size, 0: timeout, -1: failed + */ +__tb_deprecated__ +tb_long_t tb_aiop_wait(tb_aiop_ref_t aiop, tb_aioe_ref_t list, tb_size_t maxn, tb_long_t timeout); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/asio.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/asio.h index a8b872fb0..3db3c4a7f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/asio.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/asio.h @@ -42,3 +42,47 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file asio.h + * @defgroup asio + * + */ +#ifndef TB_ASIO_DEPRECATED_H +#define TB_ASIO_DEPRECATED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "aioo.h" +#include "aioe.h" +#include "aiop.h" +#include "aico.h" +#include "aice.h" +#include "aicp.h" +#include "http.h" +#include "dns.h" +#include "ssl.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/deprecated.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/deprecated.h index 429191ba8..d733cfb14 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/deprecated.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/deprecated.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file deprecated.h + * @defgroup asio + * + */ +#ifndef TB_ASIO_DEPRECATED_H +#define TB_ASIO_DEPRECATED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "asio.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/dns.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/dns.h index 8b5ca3662..0b88ad783 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/dns.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/dns.h @@ -102,3 +102,107 @@ tb_aicp_ref_t tb_aicp_dns_aicp(tb_aicp_dns_ref_t dns); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file dns.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_DNS_H +#define TB_ASIO_DNS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "aicp.h" +#include "../../network/ipaddr.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the aicp dns ref type +typedef __tb_typeref__(aicp_dns); + +/// the aicp dns done func type +typedef tb_void_t (*tb_aicp_dns_done_func_t)(tb_aicp_dns_ref_t dns, tb_char_t const* host, tb_ipaddr_ref_t addr, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the dns + * + * @param aicp the aicp + * + * @return the dns + */ +__tb_deprecated__ +tb_aicp_dns_ref_t tb_aicp_dns_init(tb_aicp_ref_t aicp); + +/*! kill the dns + * + * @param dns the dns + */ +__tb_deprecated__ +tb_void_t tb_aicp_dns_kill(tb_aicp_dns_ref_t dns); + +/*! exit the dns + * + * @param dns the dns + */ +__tb_deprecated__ +tb_void_t tb_aicp_dns_exit(tb_aicp_dns_ref_t dns); + +/*! done the dns + * + * @param dns the dns + * @param host the host + * @param timeout the timeout, ms + * @param func the done func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_dns_done(tb_aicp_dns_ref_t dns, tb_char_t const* host, tb_long_t timeout, tb_aicp_dns_done_func_t func, tb_cpointer_t priv); + +/*! the dns aicp + * + * @param handle the dns handle + * + * @return the aicp + */ +__tb_deprecated__ +tb_aicp_ref_t tb_aicp_dns_aicp(tb_aicp_dns_ref_t dns); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/http.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/http.h index 521f16199..4fa1ea63f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/http.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/http.h @@ -257,3 +257,262 @@ tb_bool_t tb_aicp_http_ctrl(tb_aicp_http_ref_t http, tb_size_t option, __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file http.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_HTTP_H +#define TB_ASIO_HTTP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "aicp.h" +#include "../../network/http.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the aicp http ref type +typedef __tb_typeref__(aicp_http); + +/*! the aicp http open func type + * + * @param http the http handle + * @param state the state + * @param status the http status + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_http_open_func_t)(tb_aicp_http_ref_t http, tb_size_t state, tb_http_status_t const* status, tb_cpointer_t priv); + +/*! the aicp http open func type + * + * @param http the http handle + * @param state the state + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_void_t (*tb_aicp_http_clos_func_t)(tb_aicp_http_ref_t http, tb_size_t state, tb_cpointer_t priv); + +/*! the aicp http read func type + * + * @param http the http handle + * @param state the state + * @param data the readed data + * @param real the real size, maybe zero + * @param size the need size + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_http_read_func_t)(tb_aicp_http_ref_t http, tb_size_t state, tb_byte_t const* data, tb_size_t real, tb_size_t size, tb_cpointer_t priv); + +/*! the aicp http seek func type + * + * @param http the http handle + * @param state the state + * @param offset the real offset + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_http_seek_func_t)(tb_aicp_http_ref_t http, tb_size_t state, tb_hize_t offset, tb_cpointer_t priv); + +/*! the aicp http task func type + * + * @param http the http handle + * @param state the state + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_http_task_func_t)(tb_aicp_http_ref_t http, tb_size_t state, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the http + * + * @param aicp the aicp + * + * @return the http + */ +__tb_deprecated__ +tb_aicp_http_ref_t tb_aicp_http_init(tb_aicp_ref_t aicp); + +/*! kill the http + * + * @param http the http + */ +__tb_deprecated__ +tb_void_t tb_aicp_http_kill(tb_aicp_http_ref_t http); + +/*! exit the http + * + * @param http the http + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_exit(tb_aicp_http_ref_t http); + +/*! open the http + * + * @param http the http + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_open(tb_aicp_http_ref_t http, tb_aicp_http_open_func_t func, tb_cpointer_t priv); + +/*! close the http + * + * @param http the http + * @param func the func + * @param priv the private data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_clos(tb_aicp_http_ref_t http, tb_aicp_http_clos_func_t func, tb_cpointer_t priv); + +/*! try closing the http + * + * @param http the http + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_clos_try(tb_aicp_http_ref_t http); + +/*! read the http + * + * @param http the http + * @param size the read size, using the default size if be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_read(tb_aicp_http_ref_t http, tb_size_t size, tb_aicp_http_read_func_t func, tb_cpointer_t priv); + +/*! read the http after the delay time + * + * @param http the http + * @param delay the delay time, ms + * @param size the read size, using the default size if be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_read_after(tb_aicp_http_ref_t http, tb_size_t delay, tb_size_t size, tb_aicp_http_read_func_t func, tb_cpointer_t priv); + +/*! seek the http + * + * @param http the http + * @param offset the offset + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_seek(tb_aicp_http_ref_t http, tb_hize_t offset, tb_aicp_http_seek_func_t func, tb_cpointer_t priv); + +/*! task the http + * + * @param http the http + * @param delay the delay time, ms + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_task(tb_aicp_http_ref_t http, tb_size_t delay, tb_aicp_http_task_func_t func, tb_cpointer_t priv); + +/*! open and read the http, open it first if not opened + * + * @param http the http + * @param size the read size, using the default size if be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_open_read(tb_aicp_http_ref_t http, tb_size_t size, tb_aicp_http_read_func_t func, tb_cpointer_t priv); + +/*! open and seek the http, open it first if not opened + * + * @param http the http + * @param offset the offset + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_open_seek(tb_aicp_http_ref_t http, tb_hize_t offset, tb_aicp_http_seek_func_t func, tb_cpointer_t priv); + +/*! the http aicp + * + * @param http the http + * + * @return the aicp + */ +__tb_deprecated__ +tb_aicp_ref_t tb_aicp_http_aicp(tb_aicp_http_ref_t http); + +/*! ctrl the http option + * + * @param http the http + * @param option the http option + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_http_ctrl(tb_aicp_http_ref_t http, tb_size_t option, ...); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/prefix.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/prefix.h index 7250b5905..5fd5b03e3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/prefix.h @@ -137,3 +137,142 @@ typedef __tb_typeref__(aicp); typedef __tb_typeref__(aiop); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_ASIO_DEPRECATED_PREFIX_H +#define TB_ASIO_DEPRECATED_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../../platform/prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the aioo ref type +typedef __tb_typeref__(aioo); + +/// the aico ref type +typedef __tb_typeref__(aico); + +/*! the aico pool ref type + * + * <pre> + * |------------------------------------------------| + * | astream | + * |------------------------------------------------| + * | addr | http | file | sock | .. | + * '------------------------------------------------' + * | + * init: [aicp] + * | + * |------------------------------------------------| + * addo: | aico0 aico1 aico2 aico3 ... | <= sock, file, and task aico + * '------------------------------------------------' + * | + * [aicp] + * | + * post: |------------------------------------------------| <= only post one aice for the same aico util the aice is finished + * aice: | aice0 aice1 aice2 aice3 ... | <--------------------------------------------------------------------------------- + * '------------------------------------------------' | + * | | + * [aicp] | + * | <= input aices | + * | | + * '-------------------------------------------------------------- | + * | | | + * |--------------------------------------------------------------------------------------------------| | + * | unix proactor | | windows proactor | | + * |-----------------------------------------------------------------------|--------------------------| | + * | | | | | | + * | continue to spak aice | | |----- | | + * | -------------------------------> | | | | | | + * | | \/ [lock] | \/ | | | + * aiop: |------|-------|-------|-------|---- ... --|-----| |-----| | done post | | + * aico: | aico0 aico1 aico2 aico3 ... | | | | | | | | | + * wait: |------|-------|-------|-------|---- ... --|-----| |aice4| | |----------------| | | + * | | | | | | | | | | | | + * | aice0 aice2 | |aice5| | | | | | + * | | | | | | | | | | | | + * | aice1 ... | |aice6| | | iocp | | | + * | | | | | | | | | | | + * | aice3 | |aice7| | | | | | + * | | | | | | | | | | | + * | ... | | ... | | | | | | + * | | | | | | | | wait0 wait1 .. | | | + * | | | | | ---------------- | | + * | wait poll | |queue| | | | | | + * '------------------------------------------------' '-----'-----------'--------------------------' | + * /\ | [lock] | | | + * | | | | | + * | no data? wait aice --------------------------->----------------- | + * |<-----------------------------| worker0 | worker1 | ... | <= done loop for workers | + * -------------------<------------------------- | + * | | | | + * |---------------------------------------------| | + * | aice0 | aice2 | ... | | + * | aice1 | aice3 | ... | <= output aices | + * | ... | aice4 | ... | | + * | ... | ... | ... | | + * '---------------------------------------------' | + * | | | | + * |---------------------------------------------| | + * | caller0 | caller2 | ... | | + * | caller1 | ... | ... | <= done callers | + * | ... | caller3 | ... | | + * | ... | ... | ... | | + * '---------------------------------------------' | + * | | | + * ... ... | + * post aice end ---- | + * | | | + * '---------------------|------------------------------------------------>' + * | + * kill: ... | + * | | + * exit: ... <---------' + * + * </pre> + * + */ +typedef __tb_typeref__(aicp); + +/*! the asio poll pool type + * + * @note only for sock and using level triggered mode + * + * <pre> + * objs: |-----|------|------|--- ... ...---|-------| + * wait: | | + * evet: read writ ... + * </pre> + * + */ +typedef __tb_typeref__(aiop); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/ssl.h b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/ssl.h index b14a56df0..fcd0e29a5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/ssl.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/deprecated/ssl.h @@ -285,3 +285,290 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ssl.h + * @ingroup asio + * + */ +#ifndef TB_ASIO_SSL_H +#define TB_ASIO_SSL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "aicp.h" +#include "../../network/ssl.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the aicp ssl ref type +typedef __tb_typeref__(aicp_ssl); + +/*! the aicp ssl open func type + * + * @param ssl the ssl + * @param state the state + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_ssl_open_func_t)(tb_aicp_ssl_ref_t ssl, tb_size_t state, tb_cpointer_t priv); + +/*! the aicp ssl clos func type + * + * @param ssl the ssl + * @param state the state + * @param priv the func private data + */ +typedef tb_void_t (*tb_aicp_ssl_clos_func_t)(tb_aicp_ssl_ref_t ssl, tb_size_t state, tb_cpointer_t priv); + +/*! the aicp ssl read func type + * + * @param ssl the ssl + * @param state the state + * @param data the readed data + * @param real the real size, maybe zero + * @param size the need size + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_ssl_read_func_t)(tb_aicp_ssl_ref_t ssl, tb_size_t state, tb_byte_t* data, tb_size_t real, tb_size_t size, tb_cpointer_t priv); + +/*! the aicp ssl writ func type + * + * @param ssl the ssl + * @param state the state + * @param data the writed data + * @param real the real size, maybe zero + * @param size the need size + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_ssl_writ_func_t)(tb_aicp_ssl_ref_t ssl, tb_size_t state, tb_byte_t const* data, tb_size_t real, tb_size_t size, tb_cpointer_t priv); + +/*! the aicp ssl task func type + * + * @param ssl the ssl + * @param state the state + * @param delay the delay + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_aicp_ssl_task_func_t)(tb_aicp_ssl_ref_t ssl, tb_size_t state, tb_size_t delay, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the ssl + * + * @param aicp the aicp + * @param bserver is server endpoint? + * + * @return the ssl + */ +__tb_deprecated__ +tb_aicp_ssl_ref_t tb_aicp_ssl_init(tb_aicp_ref_t aicp, tb_bool_t bserver); + +/*! kill the ssl + * + * @param ssl the ssl + */ +__tb_deprecated__ +tb_void_t tb_aicp_ssl_kill(tb_aicp_ssl_ref_t ssl); + +/*! exit the ssl + * + * @param ssl the ssl + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_exit(tb_aicp_ssl_ref_t ssl); + +/*! set the ssl aico + * + * @param ssl the ssl + * @param aico the aico + */ +__tb_deprecated__ +tb_void_t tb_aicp_ssl_set_aico(tb_aicp_ssl_ref_t ssl, tb_aico_ref_t aico); + +/*! set the ssl timeout + * + * @param ssl the ssl + * @param timeout the ssl timeout, using the default timeout if be zero + */ +__tb_deprecated__ +tb_void_t tb_aicp_ssl_set_timeout(tb_aicp_ssl_ref_t ssl, tb_long_t timeout); + +/*! open the ssl + * + * @param ssl the ssl + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_open(tb_aicp_ssl_ref_t ssl, tb_aicp_ssl_open_func_t func, tb_cpointer_t priv); + +/*! close the ssl + * + * @param handle the ssl + * @param func the func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_clos(tb_aicp_ssl_ref_t ssl, tb_aicp_ssl_clos_func_t func, tb_cpointer_t priv); + +/*! try closing the ssl + * + * @param ssl the ssl + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_clos_try(tb_aicp_ssl_ref_t ssl); + +/*! read the ssl + * + * @param ssl the ssl + * @param data the read data + * @param size the read size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_read(tb_aicp_ssl_ref_t ssl, tb_byte_t* data, tb_size_t size, tb_aicp_ssl_read_func_t func, tb_cpointer_t priv); + +/*! writ the ssl + * + * @param ssl the ssl + * @param data the data + * @param size the size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_writ(tb_aicp_ssl_ref_t ssl, tb_byte_t const* data, tb_size_t size, tb_aicp_ssl_writ_func_t func, tb_cpointer_t priv); + +/*! read the ssl after the delay time + * + * @param ssl the ssl + * @param delay the delay time, ms + * @param data the read data + * @param size the read size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_read_after(tb_aicp_ssl_ref_t ssl, tb_size_t delay, tb_byte_t* data, tb_size_t size, tb_aicp_ssl_read_func_t func, tb_cpointer_t priv); + +/*! writ the ssl after the delay time + * + * @param ssl the ssl + * @param delay the delay time, ms + * @param data the data + * @param size the size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_writ_after(tb_aicp_ssl_ref_t ssl, tb_size_t delay, tb_byte_t const* data, tb_size_t size, tb_aicp_ssl_writ_func_t func, tb_cpointer_t priv); + +/*! task the ssl + * + * @param ssl the ssl + * @param delay the delay time, ms + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_task(tb_aicp_ssl_ref_t ssl, tb_size_t delay, tb_aicp_ssl_task_func_t func, tb_cpointer_t priv); + +/*! open and read the ssl, open it first if not opened + * + * @param ssl the ssl + * @param data the read data + * @param size the read size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_open_read(tb_aicp_ssl_ref_t ssl, tb_byte_t* data, tb_size_t size, tb_aicp_ssl_read_func_t func, tb_cpointer_t priv); + +/*! open and writ the ssl, open it first if not opened + * + * @param ssl the ssl + * @param data the data + * @param size the size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +__tb_deprecated__ +tb_bool_t tb_aicp_ssl_open_writ(tb_aicp_ssl_ref_t ssl, tb_byte_t const* data, tb_size_t size, tb_aicp_ssl_writ_func_t func, tb_cpointer_t priv); + +/*! the ssl aicp + * + * @param ssl the ssl + * + * @return the aicp + */ +__tb_deprecated__ +tb_aicp_ref_t tb_aicp_ssl_aicp(tb_aicp_ssl_ref_t ssl); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/asio/prefix.h b/core/pkg/tbox.pkg/inc/tbox/asio/prefix.h index ee23c2646..615adebed 100644 --- a/core/pkg/tbox.pkg/inc/tbox/asio/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/asio/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_ASIO_PREFIX_H +#define TB_ASIO_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/charset/charset.h b/core/pkg/tbox.pkg/inc/tbox/charset/charset.h index 14a6b0033..d7fc8276d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/charset/charset.h +++ b/core/pkg/tbox.pkg/inc/tbox/charset/charset.h @@ -185,3 +185,190 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file charset.h + * @defgroup charset + * @ingroup charset + * + */ +#ifndef TB_CHARSET_H +#define TB_CHARSET_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../stream/stream.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the endian for the charset type +#define TB_CHARSET_TYPE_LE (0x0100) +#define TB_CHARSET_TYPE_ME (0x0100) +#ifdef TB_WORDS_BIGENDIAN +# define TB_CHARSET_TYPE_NE (TB_CHARSET_TYPE_BE) +#else +# define TB_CHARSET_TYPE_NE (TB_CHARSET_TYPE_LE) +#endif + +// type +#define TB_CHARSET_TYPE(type) (((type) & ~TB_CHARSET_TYPE_ME)) + +// ok? +#define TB_CHARSET_TYPE_OK(type) (TB_CHARSET_TYPE(type) != TB_CHARSET_TYPE_NONE) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the charset type enum + * + * @note default: big endian + */ +typedef enum __tb_charset_type_e +{ + TB_CHARSET_TYPE_NONE = 0x00 +, TB_CHARSET_TYPE_ASCII = 0x01 +, TB_CHARSET_TYPE_GB2312 = 0x02 +, TB_CHARSET_TYPE_GBK = 0x03 +, TB_CHARSET_TYPE_ISO8859 = 0x04 +, TB_CHARSET_TYPE_UCS2 = 0x05 +, TB_CHARSET_TYPE_UCS4 = 0x06 +, TB_CHARSET_TYPE_UTF16 = 0x07 +, TB_CHARSET_TYPE_UTF32 = 0x08 +, TB_CHARSET_TYPE_UTF8 = 0x09 + +}tb_charset_type_e; + +/// the charset type +typedef struct __tb_charset_t +{ + /// the charset type + tb_size_t type; + + /// the charset name + tb_char_t const* name; + + /*! get ucs4 character + * + * return: -1, 0 or 1 + * + * -1: failed, break it + * 0: no character, skip and continue it + * 1: ok, continue it + */ + tb_long_t (*get)(tb_static_stream_ref_t sstream, tb_bool_t be, tb_uint32_t* ch); + + /*! set ucs4 character + * + * return: -1, 0 or 1 + * + * -1: failed, break it + * 0: no character, skip and continue it + * 1: ok, continue it + */ + tb_long_t (*set)(tb_static_stream_ref_t sstream, tb_bool_t be, tb_uint32_t ch); + +}tb_charset_t; + +/// the charset ref type +typedef tb_charset_t* tb_charset_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the charset name + * + * @param type the charset type + * + * @return the charset name + */ +tb_char_t const* tb_charset_name(tb_size_t type); + +/*! the charset type + * + * @param name the charset name + * + * @return the charset type + */ +tb_size_t tb_charset_type(tb_char_t const* name); + +/*! find the charset + * + * @param type the charset type + * + * @return the charset pointer + */ +tb_charset_ref_t tb_charset_find(tb_size_t type); + +/*! convert charset from static stream + * + * @param ftype the from charset + * @param ttype the to charset + * @param fst the from stream + * @param tst the to stream + * + * @return the converted bytes for output or -1 + */ +tb_long_t tb_charset_conv_bst(tb_size_t ftype, tb_size_t ttype, tb_static_stream_ref_t fst, tb_static_stream_ref_t tst); + +/*! convert charset from cstr + * + * @param ftype the from charset + * @param ttype the to charset + * @param cstr the cstr + * @param data the data + * @param maxn the size + * + * @return the converted bytes for output or -1 + */ +tb_long_t tb_charset_conv_cstr(tb_size_t ftype, tb_size_t ttype, tb_char_t const* cstr, tb_byte_t* data, tb_size_t size); + +/*! convert charset from data + * + * @param ftype the from charset + * @param ttype the to charset + * @param idata the idata + * @param isize the isize + * @param odata the odata + * @param osize the osize + * + * @return the converted bytes for output or -1 + */ +tb_long_t tb_charset_conv_data(tb_size_t ftype, tb_size_t ttype, tb_byte_t const* idata, tb_size_t isize, tb_byte_t* odata, tb_size_t osize); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/charset/prefix.h b/core/pkg/tbox.pkg/inc/tbox/charset/prefix.h index b7ad9fc63..915d0d1a1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/charset/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/charset/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_CHARSET_PREFIX_H +#define TB_CHARSET_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/config.h b/core/pkg/tbox.pkg/inc/tbox/config.h index bcf623fe6..32d66b560 100644 --- a/core/pkg/tbox.pkg/inc/tbox/config.h +++ b/core/pkg/tbox.pkg/inc/tbox/config.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file config.h + * + */ +#ifndef TB_TBOX_CONFIG_H +#define TB_TBOX_CONFIG_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "tbox.config.h" + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/bloom_filter.h b/core/pkg/tbox.pkg/inc/tbox/container/bloom_filter.h index 933ad14c1..28459c52b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/bloom_filter.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/bloom_filter.h @@ -207,3 +207,212 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bloom_filter.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_BLOOM_FILTER_H +#define TB_CONTAINER_BLOOM_FILTER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the item maxn +#define TB_BLOOM_FILTER_ITEM_MAXN_MICRO (1 << 16) +#define TB_BLOOM_FILTER_ITEM_MAXN_SMALL (1 << 20) +#define TB_BLOOM_FILTER_ITEM_MAXN_LARGE (1 << 24) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the bloom filter type + * + * A Bloom filter is a space-efficient probabilistic data structure, + * conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set. + * False positive matches are possible, but false negatives are not; + * i.e. a query returns either "possibly in set" or "definitely not in set". + * Elements can be added to the set, but not removed (though this can be addressed with a "counting" filter). + * The more elements that are added to the set, the larger the probability of false positives. + * + * Assume that a hash function selects each array position with equal probability. + * If m is the number of bits in the array, and k is the number of hash functions, + * then the probability that a certain bit is not set to 1 by a certain hash function + * during the insertion of an element is then + * 1 - 1 / m + * + * The probability that it is not set to 1 by any of the hash functions is + * (1 - 1/ m) ^ k + * + * If we have inserted n elements, the probability that a certain bit is still 0 is + * (1 - 1/ m) ^ kn + * + * the probability that it is 1 is therefore + * 1 - ((1 - 1/ m) ^ kn) + * + * Now test membership of an element that is not in the set. + * Each of the k array positions computed by the hash functions is 1 with a probability as above. + * The probability of all of them being 1, + * which would cause the algorithm to erroneously claim that the element is in the set, is often given as + * p = (1 - ((1 - 1/ m) ^ kn))^k ~= (1 - e^(-kn/m))^k + * + * For a given m and n, the value of k (the number of hash functions) that minimizes the probability is + * k = (m / n) * ln2 ~= (m / n) * (9 / 13) + * + * which gives + * 2 ^ -k ~= 0.6185 ^ (m / n) + * + * The required number of bits m, given n (the number of inserted elements) + * and a desired false positive probability p (and assuming the optimal value of k is used) + * can be computed by substituting the optimal value of k in the probability expression above: + * p = (1 - e ^-(m/nln2)n/m))^(m/nln2) + * + * which can be simplified to: + * lnp = -m/n * (ln2)^2 + * + * This optimal results in: + * s = m/n = -lnp / (ln2 * ln2) = -log2(p) / ln2 + * k = s * ln2 = -log2(p) <= note: this k will be larger + * + * compute s(m/n) for given k and p: + * p = (1 - e^(-kn/m))^k = (1 - e^(-k/s))^k + * => lnp = k * ln(1 - e^(-k/s)) + * => (lnp) / k = ln(1 - e^(-k/s)) + * => e^((lnp) / k) = 1 - e^(-k/s) + * => e^(-k/s) = 1 - e^((lnp) / k) = 1 - (e^lnp)^(1/k) = 1 - p^(1/k) + * => -k/s = ln(1 - p^(1/k)) + * => s = -k / ln(1 - p^(1/k)) and define c = p^(1/k) + * => s = -k / ln(1 - c)) and ln(1 + x) ~= x - 0.5x^2 while x < 1 + * => s ~= -k / (-c-0.5c^2) = 2k / (2c + c * c) + * + * so + * c = p^(1/k) + * s = m / n = 2k / (2c + c * c) + */ +typedef __tb_typeref__(bloom_filter); + +/// the probability of false positives +typedef enum __tb_bloom_filter_probability_e +{ + TB_BLOOM_FILTER_PROBABILITY_0_1 = 3 ///!< 1 / 2^3 = 0.125 ~= 0.1 +, TB_BLOOM_FILTER_PROBABILITY_0_01 = 6 ///!< 1 / 2^6 = 0.015625 ~= 0.01 +, TB_BLOOM_FILTER_PROBABILITY_0_001 = 10 ///!< 1 / 2^10 = 0.0009765625 ~= 0.001 +, TB_BLOOM_FILTER_PROBABILITY_0_0001 = 13 ///!< 1 / 2^13 = 0.0001220703125 ~= 0.0001 +, TB_BLOOM_FILTER_PROBABILITY_0_00001 = 16 ///!< 1 / 2^16 = 0.0000152587890625 ~= 0.00001 +, TB_BLOOM_FILTER_PROBABILITY_0_000001 = 20 ///!< 1 / 2^20 = 0.00000095367431640625 ~= 0.000001 + +}tb_bloom_filter_probability_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init bloom filter + * + * @note not supports iterator + * + * @param probability the probability of false positives + * @param hash_count the hash count: < 16 + * @param item_maxn the item maxn + * @param element the element only for hash + * + * @return the bloom filter + */ +tb_bloom_filter_ref_t tb_bloom_filter_init(tb_size_t probability, tb_size_t hash_count, tb_size_t item_maxn, tb_element_t element); + +/*! exit bloom filter + * + * @param bloom_filter the bloom filter + */ +tb_void_t tb_bloom_filter_exit(tb_bloom_filter_ref_t bloom_filter); + +/*! clear bloom filter + * + * @param bloom_filter the bloom filter + */ +tb_void_t tb_bloom_filter_clear(tb_bloom_filter_ref_t bloom_filter); + +/*! set data to the bloom filter + * + * @code + * if (tb_bloom_filter_set(filter, data)) + * { + * tb_trace_i("this data not exists, set ok!"); + * } + * else + * { + * tb_trace_i("this data have been existed, set failed!"); + * + * // note: maybe false positives + * } + * @endcode + * + * @param bloom_filter the bloom filter + * @param data the item data + * + * @return return tb_false if the data have been existed, otherwise set it and return tb_true + */ +tb_bool_t tb_bloom_filter_set(tb_bloom_filter_ref_t bloom_filter, tb_cpointer_t data); + +/*! get data to the bloom filter + * + * @code + * if (tb_bloom_filter_get(filter, data)) + * { + * tb_trace_i("this data have been existed, get ok!"); + * + * // note: maybe false positives + * } + * else + * { + * tb_trace_i("this data not exists, get failed!"); + * } + * @endcode + * + * @param bloom_filter the bloom filter + * @param data the item data + * + * @return return tb_true if the data exists, otherwise return tb_false + */ +tb_bool_t tb_bloom_filter_get(tb_bloom_filter_ref_t bloom_filter, tb_cpointer_t data); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/circle_queue.h b/core/pkg/tbox.pkg/inc/tbox/container/circle_queue.h index 9a83994a9..ffd03979d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/circle_queue.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/circle_queue.h @@ -177,3 +177,182 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file queue.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_CIRCLE_QUEUE_H +#define TB_CONTAINER_CIRCLE_QUEUE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the circle queue ref type + * + * <pre> + * queue: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||------| + * head last tail + * + * queue: ||||||||||||||-----|--------------------------|||||||||||||||||||||||||| + * last tail head + * + * performance: + * + * put: O(1) + * pop: O(1) + * + * iterator: + * + * next: fast + * prev: fast + * + * </pre> + * + * @note the index of the same item is mutable + * + */ +typedef tb_iterator_ref_t tb_circle_queue_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init queue + * + * @param maxn the item maxn, using the default maxn if be zero + * @param element the element + * + * @return the queue + */ +tb_circle_queue_ref_t tb_circle_queue_init(tb_size_t maxn, tb_element_t element); + +/*! exit queue + * + * @param queue the queue + */ +tb_void_t tb_circle_queue_exit(tb_circle_queue_ref_t queue); + +/*! the queue head item + * + * @param queue the queue + * + * @return the head item + */ +tb_pointer_t tb_circle_queue_head(tb_circle_queue_ref_t queue); + +/*! the queue last item + * + * @param queue the queue + * + * @return the last item + */ +tb_pointer_t tb_circle_queue_last(tb_circle_queue_ref_t queue); + +/*! clear the queue + * + * @param queue the queue + */ +tb_void_t tb_circle_queue_clear(tb_circle_queue_ref_t queue); + +/*! put the queue item + * + * @param queue the queue + * @param data the item data + */ +tb_void_t tb_circle_queue_put(tb_circle_queue_ref_t queue, tb_cpointer_t data); + +/*! pop the queue item + * + * @param queue the queue + */ +tb_void_t tb_circle_queue_pop(tb_circle_queue_ref_t queue); + +/*! get the queue item + * + * @param queue the queue + * + * @return the queue item + */ +tb_pointer_t tb_circle_queue_get(tb_circle_queue_ref_t queue); + +/*! the queue size + * + * @param queue the queue + * + * @return the queue size + */ +tb_size_t tb_circle_queue_size(tb_circle_queue_ref_t queue); + +/*! the queue maxn + * + * @param queue the queue + * + * @return the queue maxn + */ +tb_size_t tb_circle_queue_maxn(tb_circle_queue_ref_t queue); + +/*! the queue full? + * + * @param queue the queue + * + * @return tb_true or tb_false + */ +tb_bool_t tb_circle_queue_full(tb_circle_queue_ref_t queue); + +/*! the queue null? + * + * @param queue the queue + * + * @return tb_true or tb_false + */ +tb_bool_t tb_circle_queue_null(tb_circle_queue_ref_t queue); + +#ifdef __tb_debug__ +/*! dump queue + * + * @param queue the queue + */ +tb_void_t tb_circle_queue_dump(tb_circle_queue_ref_t circle_queue); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/container.h b/core/pkg/tbox.pkg/inc/tbox/container/container.h index b6f03d8ba..4f8c5bd98 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/container.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/container.h @@ -47,3 +47,52 @@ #include "bloom_filter.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file container.h + * @defgroup container + * + */ +#ifndef TB_CONTAINER_H +#define TB_CONTAINER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" +#include "heap.h" +#include "stack.h" +#include "vector.h" +#include "hash_set.h" +#include "hash_map.h" +#include "queue.h" +#include "circle_queue.h" +#include "priority_queue.h" +#include "list.h" +#include "list_entry.h" +#include "single_list.h" +#include "single_list_entry.h" +#include "bloom_filter.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/container/element.h b/core/pkg/tbox.pkg/inc/tbox/container/element.h index 0e4e94af6..08e7fa5ca 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/element.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/element.h @@ -325,3 +325,330 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file element.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_ELEMENT_H +#define TB_CONTAINER_ELEMENT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the element ref type +typedef struct __tb_element_t* tb_element_ref_t; + +/*! the element data hash function type + * + * @param element the element + * @param data the element data + * @param mask the hash mask + * @param index the hash index + * + * @return the hash value + */ +typedef tb_size_t (*tb_element_hash_func_t)(tb_element_ref_t element, tb_cpointer_t data, tb_size_t mask, tb_size_t index); + +/*! the element data compare function type + * + * @param element the element + * @param ldata the left-hand data + * @param rdata the right-hand data + * + * @return equal: 0, 1: >, -1: < + */ +typedef tb_long_t (*tb_element_comp_func_t)(tb_element_ref_t element, tb_cpointer_t ldata, tb_cpointer_t rdata); + +/*! the element data function type + * + * @param element the element + * @param buff the element data address + * + * @return the element data + */ +typedef tb_pointer_t (*tb_element_data_func_t)(tb_element_ref_t element, tb_cpointer_t buff); + +/*! the element data string function type + * + * @param element the element + * @param data the element data + * @param cstr the string buffer + * @param maxn the string buffer maximum size + * + * @return the string pointer + */ +typedef tb_char_t const* (*tb_element_cstr_func_t)(tb_element_ref_t element, tb_cpointer_t data, tb_char_t* cstr, tb_size_t maxn); + +/*! the element free function type + * + * @param element the element + * @param buff the element buffer + */ +typedef tb_void_t (*tb_element_free_func_t)(tb_element_ref_t element, tb_pointer_t buff); + +/*! the element duplicate function type + * + * allocate a new element and copy the element data + * + * @param element the element + * @param buff the element buffer + * @param data the element data + */ +typedef tb_void_t (*tb_element_dupl_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_cpointer_t data); + +/*! the element replace function type + * + * free the previous element data and duplicate the new data + * + * @param element the element + * @param buff the element buffer + * @param data the element data + */ +typedef tb_void_t (*tb_element_repl_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_cpointer_t data); + +/*! the element copy function type + * + * only copy the element data and not allocate new element + * + * @param element the element + * @param buff the element buffer + * @param data the element data + */ +typedef tb_void_t (*tb_element_copy_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_cpointer_t data); + +/*! the elements free function type + * + * free some elements + * + * @param element the element + * @param buff the element buffer + * @param size the element count + */ +typedef tb_void_t (*tb_element_nfree_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_size_t size); + +/*! the elements duplicate function type + * + * duplicate some elements + * + * @param element the element + * @param buff the element buffer + * @param data the element data + * @param size the element count + */ +typedef tb_void_t (*tb_element_ndupl_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_cpointer_t data, tb_size_t size); + +/*! the elements replace function type + * + * replace some elements + * + * @param element the element + * @param buff the element buffer + * @param data the element data + * @param size the element count + */ +typedef tb_void_t (*tb_element_nrepl_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_cpointer_t data, tb_size_t size); + +/*! the elements copy function type + * + * copy some elements + * + * @param element the element + * @param buff the element buffer + * @param data the element data + * @param size the element count + */ +typedef tb_void_t (*tb_element_ncopy_func_t)(tb_element_ref_t element, tb_pointer_t buff, tb_cpointer_t data, tb_size_t size); + +/// the element type +typedef enum __tb_element_type_t +{ + TB_ELEMENT_TYPE_NULL = 0 //!< null +, TB_ELEMENT_TYPE_LONG = 1 //!< long +, TB_ELEMENT_TYPE_SIZE = 2 //!< size +, TB_ELEMENT_TYPE_UINT8 = 3 //!< uint8 +, TB_ELEMENT_TYPE_UINT16 = 4 //!< uint16 +, TB_ELEMENT_TYPE_UINT32 = 5 //!< uint32 +, TB_ELEMENT_TYPE_STR = 6 //!< string +, TB_ELEMENT_TYPE_PTR = 7 //!< pointer +, TB_ELEMENT_TYPE_MEM = 8 //!< memory +, TB_ELEMENT_TYPE_OBJ = 9 //!< object +, TB_ELEMENT_TYPE_TRUE = 10 //!< true +, TB_ELEMENT_TYPE_USER = 11 //!< the user-defined type + +}tb_element_type_t; + +/// the element type +typedef struct __tb_element_t +{ + /// the element type + tb_uint16_t type; + + /// the element flag + tb_uint16_t flag; + + /// the element size + tb_uint16_t size; + + /// the priv data + tb_cpointer_t priv; + + /// the hash function + tb_element_hash_func_t hash; + + /// the compare function + tb_element_comp_func_t comp; + + /// the data function + tb_element_data_func_t data; + + /// the string function + tb_element_cstr_func_t cstr; + + /// the free element + tb_element_free_func_t free; + + /// the duplicate function + tb_element_dupl_func_t dupl; + + /// the replace function + tb_element_repl_func_t repl; + + /// the copy function + tb_element_copy_func_t copy; + + /// the free elements function + tb_element_nfree_func_t nfree; + + /// the duplicate elements function + tb_element_ndupl_func_t ndupl; + + /// the replace elements function + tb_element_nrepl_func_t nrepl; + + /// the copy elements function + tb_element_ncopy_func_t ncopy; + +}tb_element_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the null element, no space + * + * @return the element + */ +tb_element_t tb_element_null(tb_noarg_t); + +/*! the true element, no space + * + * .e.g for hash data + * + * @return the element + */ +tb_element_t tb_element_true(tb_noarg_t); + +/*! the long element + * + * @return the element + */ +tb_element_t tb_element_long(tb_noarg_t); + +/*! the size element + * + * @return the element + */ +tb_element_t tb_element_size(tb_noarg_t); + +/*! the uint8 element + * + * @return the element + */ +tb_element_t tb_element_uint8(tb_noarg_t); + +/*! the uint16 element for + * + * @return the element + */ +tb_element_t tb_element_uint16(tb_noarg_t); + +/*! the uint32 element + * + * @return the element + */ +tb_element_t tb_element_uint32(tb_noarg_t); + +/*! the string element + * + * @param is_case is case? + * + * @return the element + */ +tb_element_t tb_element_str(tb_bool_t is_case); + +/*! the pointer element + * + * @note if the free function have been hooked, the nfree need hook too. + * + * @param free the element free function + * @param priv the private data of the element free function + * + * @return the element + */ +tb_element_t tb_element_ptr(tb_element_free_func_t free, tb_cpointer_t priv); + +/*! the object element + * + * @return the element + */ +tb_element_t tb_element_obj(tb_noarg_t); + +/*! the memory element with the fixed space + * + * @param size the element size + * @param free the element free function + * @param priv the private data of the element free function + * + * @return the element + */ +tb_element_t tb_element_mem(tb_size_t size, tb_element_free_func_t free, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/element/hash.h b/core/pkg/tbox.pkg/inc/tbox/container/element/hash.h index 19e28a94b..534a7760d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/element/hash.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/element/hash.h @@ -96,3 +96,101 @@ tb_size_t tb_element_hash_data(tb_byte_t const* data, tb_size_t size, tb_size_t tb_element_hash_cstr(tb_char_t const* cstr, tb_size_t mask, tb_size_t index); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file hash.h + * + */ +#ifndef TB_CONTAINER_ELEMENT_HASH_H +#define TB_CONTAINER_ELEMENT_HASH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* compute the uint8 hash + * + * @param value the value + * @param mask the mask + * @param index the hash func index + * + * @return the hash value + */ +tb_size_t tb_element_hash_uint8(tb_uint8_t value, tb_size_t mask, tb_size_t index); + +/* compute the uint16 hash + * + * @param value the value + * @param mask the mask + * @param index the hash func index + * + * @return the hash value + */ +tb_size_t tb_element_hash_uint16(tb_uint16_t value, tb_size_t mask, tb_size_t index); + +/* compute the uint32 hash + * + * @param value the value + * @param mask the mask + * @param index the hash func index + * + * @return the hash value + */ +tb_size_t tb_element_hash_uint32(tb_uint32_t value, tb_size_t mask, tb_size_t index); + +/* compute the uint64 hash + * + * @param value the value + * @param mask the mask + * @param index the hash func index + * + * @return the hash value + */ +tb_size_t tb_element_hash_uint64(tb_uint64_t value, tb_size_t mask, tb_size_t index); + +/* compute the data hash + * + * @param data the data + * @param size the size + * @param mask the mask + * @param index the hash func index + * + * @return the hash value + */ +tb_size_t tb_element_hash_data(tb_byte_t const* data, tb_size_t size, tb_size_t mask, tb_size_t index); + +/* compute the cstring hash + * + * @param cstr the cstring + * @param mask the mask + * @param index the hash func index + * + * @return the hash value + */ +tb_size_t tb_element_hash_cstr(tb_char_t const* cstr, tb_size_t mask, tb_size_t index); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/container/element/prefix.h b/core/pkg/tbox.pkg/inc/tbox/container/element/prefix.h index 7989dd5a6..746a50e0d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/element/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/element/prefix.h @@ -39,3 +39,44 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_CONTAINER_ELEMENT_PREFIX_H +#define TB_CONTAINER_ELEMENT_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../element.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" +#include "../../memory/memory.h" +#include "../../object/object.h" +#include "../../stream/stream.h" +#include "../../platform/platform.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/container/hash_map.h b/core/pkg/tbox.pkg/inc/tbox/container/hash_map.h index 71837c066..e536248f9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/hash_map.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/hash_map.h @@ -219,3 +219,224 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file hash_map.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_HASH_MAP_H +#define TB_CONTAINER_HASH_MAP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the micro hash bucket size +#define TB_HASH_MAP_BUCKET_SIZE_MICRO (64) + +/// the small hash bucket size +#define TB_HASH_MAP_BUCKET_SIZE_SMALL (256) + +/// the large hash bucket size +#define TB_HASH_MAP_BUCKET_SIZE_LARGE (65536) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the hash map item type +typedef struct __tb_hash_map_item_t +{ + /// the item name + tb_pointer_t name; + + /// the item data + tb_pointer_t data; + +}tb_hash_map_item_t, *tb_hash_map_item_ref_t; + +/*! the hash map ref type + * + * <pre> + * 0 1 3 ... ... n n + 1 + * hash_list: |--------|--------|--------|--------|--------|--------|--------|--------| + * | + * ----- + * item_list: | | key:0 + * ----- + * | | key:1 + * ----- <= insert by binary search algorithm + * | | key:2 + * ----- + * | | key:3 + * ----- + * | | key:4 + * ----- + * | | + * ----- + * | | + * ----- + * | | + * ----- + * + * </pre> + * + * @note the itor of the same item is mutable + */ +typedef tb_iterator_ref_t tb_hash_map_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init hash map + * + * @param bucket_size the hash bucket size, using the default size if be zero + * @param element_name the item for name + * @param element_data the item for data + * + * @return the hash map + */ +tb_hash_map_ref_t tb_hash_map_init(tb_size_t bucket_size, tb_element_t element_name, tb_element_t element_data); + +/*! exit hash map + * + * @param hash_map the hash map + */ +tb_void_t tb_hash_map_exit(tb_hash_map_ref_t hash_map); + +/*! clear hash map + * + * @param hash_map the hash map + */ +tb_void_t tb_hash_map_clear(tb_hash_map_ref_t hash_map); + +/*! get item data from name + * + * @note + * the return value may be zero if the item type is integer + * so we need call tb_hash_map_find for judging whether to get value successfully + * + * @code + * + * // find item and get item data + * tb_xxxx_ref_t data = (tb_xxxx_ref_t)tb_hash_map_get(hash_map, name); + * if (data) + * { + * // ... + * } + * @endcode + * + * @param hash_map the hash map + * @param name the item name + * + * @return the item data + */ +tb_pointer_t tb_hash_map_get(tb_hash_map_ref_t hash_map, tb_cpointer_t name); + +/*! find item from name + * + * @code + * + * // find item + * tb_size_t itor = tb_hash_map_find(hash_map, name); + * if (itor != tb_iterator_tail(hash_map)) + * { + * // get data + * tb_xxxx_ref_t data = (tb_xxxx_ref_t)tb_iterator_item(hash_map, itor); + * tb_assert(data); + * + * // remove it + * tb_iterator_remove(hash_map, itor); + * } + * @endcode + * + * @param hash_map the hash map + * @param name the item name + * + * @return the item itor, @note: the itor of the same item is mutable + */ +tb_size_t tb_hash_map_find(tb_hash_map_ref_t hash_map, tb_cpointer_t name); + +/*! insert item data from name + * + * @note the pair (name => data) is unique + * + * @param hash_map the hash map + * @param name the item name + * @param data the item data + * + * @return the item itor, @note: the itor of the same item is mutable + */ +tb_size_t tb_hash_map_insert(tb_hash_map_ref_t hash_map, tb_cpointer_t name, tb_cpointer_t data); + +/*! remove item from name + * + * @param hash_map the hash map + * @param name the item name + */ +tb_void_t tb_hash_map_remove(tb_hash_map_ref_t hash_map, tb_cpointer_t name); + +/*! the hash map size + * + * @param hash_map the hash map + * + * @return the hash map size + */ +tb_size_t tb_hash_map_size(tb_hash_map_ref_t hash_map); + +/*! the hash map maxn + * + * @param hash_map the hash map + * + * @return the hash map maxn + */ +tb_size_t tb_hash_map_maxn(tb_hash_map_ref_t hash_map); + +#ifdef __tb_debug__ +/*! dump hash + * + * @param hash_map the hash map + */ +tb_void_t tb_hash_map_dump(tb_hash_map_ref_t hash_map); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/hash_set.h b/core/pkg/tbox.pkg/inc/tbox/container/hash_set.h index a36c65d2f..7ecf06211 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/hash_set.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/hash_set.h @@ -168,3 +168,173 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file hash_set.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_HASH_SET_H +#define TB_CONTAINER_HASH_SET_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "hash_map.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the micro hash bucket size +#define TB_HASH_SET_BUCKET_SIZE_MICRO TB_HASH_MAP_BUCKET_SIZE_MICRO + +/// the small hash bucket size +#define TB_HASH_SET_BUCKET_SIZE_SMALL TB_HASH_MAP_BUCKET_SIZE_SMALL + +/// the large hash bucket size +#define TB_HASH_SET_BUCKET_SIZE_LARGE TB_HASH_MAP_BUCKET_SIZE_LARGE + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the hash set ref type + * + * @note the itor of the same item is mutable + */ +typedef tb_iterator_ref_t tb_hash_set_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init hash set + * + * @param bucket_size the hash bucket size, using the default size if be zero + * @param element the element + * + * @return the hash set + */ +tb_hash_set_ref_t tb_hash_set_init(tb_size_t bucket_size, tb_element_t element); + +/*! exit hash set + * + * @param hash_set the hash set + */ +tb_void_t tb_hash_set_exit(tb_hash_set_ref_t hash_set); + +/*! clear hash set + * + * @param hash_set the hash set + */ +tb_void_t tb_hash_set_clear(tb_hash_set_ref_t hash_set); + +/*! get item? + * + * @code + * if (tb_hash_set_get(hash_set, name)) + * { + * } + * @endcode + * + * @param hash_set the hash set + * @param data the item data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_hash_set_get(tb_hash_set_ref_t hash_set, tb_cpointer_t data); + +/*! find item + * + * @code + * + * // find item + * tb_size_t itor = tb_hash_set_find(hash_set, data); + * if (itor != tb_iterator_tail(hash_set)) + * { + * // remove it + * tb_iterator_remove(hash_set, itor); + * } + * @endcode + * + * @param hash_set the hash set + * @param data the item data + * + * @return the item itor, @note: the itor of the same item is mutable + */ +tb_size_t tb_hash_set_find(tb_hash_set_ref_t hash_set, tb_cpointer_t data); + +/*! insert item + * + * @note each item is unique + * + * @param hash_set the hash set + * @param data the item data + * + * @return the item itor, @note: the itor of the same item is mutable + */ +tb_size_t tb_hash_set_insert(tb_hash_set_ref_t hash_set, tb_cpointer_t data); + +/*! remove item + * + * @param hash_set the hash set + * @param data the item data + */ +tb_void_t tb_hash_set_remove(tb_hash_set_ref_t hash_set, tb_cpointer_t data); + +/*! the hash set size + * + * @param hash_set the hash set + * + * @return the hash set size + */ +tb_size_t tb_hash_set_size(tb_hash_set_ref_t hash_set); + +/*! the hash set maxn + * + * @param hash_set the hash set + * + * @return the hash set maxn + */ +tb_size_t tb_hash_set_maxn(tb_hash_set_ref_t hash_set); + +#ifdef __tb_debug__ +/*! dump hash + * + * @param hash_set the hash set + */ +tb_void_t tb_hash_set_dump(tb_hash_set_ref_t hash_set); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/heap.h b/core/pkg/tbox.pkg/inc/tbox/container/heap.h index 54141dedb..1b418bb63 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/heap.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/heap.h @@ -161,3 +161,166 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file heap.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_HEAP_H +#define TB_CONTAINER_HEAP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the head ref type + * + * <pre> + * heap: 1 4 2 6 9 7 8 10 14 16 + * + * 1(head) + * ------------------------- + * | | + * 4 2 + * -------------- ------------- + * | | | | + * 6 (last / 2 - 1)9 7 8 + * --------- ---- + * | | | + * 10 14 16(last - 1) + * </pre> + * + * performance: + * + * put: O(lgn) + * pop: O(1) + * top: O(1) + * del: O(lgn) + find: O(n) + * + * iterator: + * + * next: fast + * prev: fast + * + * </pre> + * + * @note the itor of the same item is mutable + */ +typedef tb_iterator_ref_t tb_heap_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init heap, default: minheap + * + * @param grow the item grow, using the default grow if be zero + * @param element the element + * + * @return the heap + */ +tb_heap_ref_t tb_heap_init(tb_size_t grow, tb_element_t element); + +/*! exit heap + * + * @param heap the heap + */ +tb_void_t tb_heap_exit(tb_heap_ref_t heap); + +/*! clear the heap + * + * @param heap the heap + */ +tb_void_t tb_heap_clear(tb_heap_ref_t heap); + +/*! the heap size + * + * @param heap the heap + * + * @return the heap size + */ +tb_size_t tb_heap_size(tb_heap_ref_t heap); + +/*! the heap maxn + * + * @param heap the heap + * + * @return the heap maxn + */ +tb_size_t tb_heap_maxn(tb_heap_ref_t heap); + +/*! the heap top item + * + * @param heap the heap + * + * @return the heap top item + */ +tb_pointer_t tb_heap_top(tb_heap_ref_t heap); + +/*! put the heap item + * + * @param heap the heap + * @param data the item data + */ +tb_void_t tb_heap_put(tb_heap_ref_t heap, tb_cpointer_t data); + +/*! pop the heap item + * + * @param heap the heap + */ +tb_void_t tb_heap_pop(tb_heap_ref_t heap); + +/*! remove the heap item using iterator only for algorithm(find, ...) + * + * @param heap the heap + * @param itor the itor + */ +tb_void_t tb_heap_remove(tb_heap_ref_t heap, tb_size_t itor); + +#ifdef __tb_debug__ +/*! dump heap + * + * @param heap the heap + */ +tb_void_t tb_heap_dump(tb_heap_ref_t heap); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/iterator.h b/core/pkg/tbox.pkg/inc/tbox/container/iterator.h index ae64e2b2a..15e8644fd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/iterator.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/iterator.h @@ -298,3 +298,303 @@ tb_long_t tb_iterator_comp(tb_iterator_ref_t iterator, tb_cpointer_t l __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file iterator.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_ITERATOR_H +#define TB_CONTAINER_ITERATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the iterator mode type +typedef enum __tb_iterator_mode_t +{ + TB_ITERATOR_MODE_FORWARD = 1 //!< forward iterator +, TB_ITERATOR_MODE_REVERSE = 2 //!< reverse iterator +, TB_ITERATOR_MODE_RACCESS = 4 //!< random access iterator +, TB_ITERATOR_MODE_MUTABLE = 8 //!< mutable iterator, the item of the same iterator is mutable for removing and moving, .e.g vector, hash, ... +, TB_ITERATOR_MODE_READONLY = 16 //!< readonly iterator + +}tb_iterator_mode_t; + +/// the iterator type +typedef struct __tb_iterator_t +{ + /// the iterator mode + tb_size_t mode; + + /// the iterator step + tb_size_t step; + + /// the iterator priv + tb_pointer_t priv; + + /// the iterator size + tb_size_t (*size)(struct __tb_iterator_t* iterator); + + /// the iterator head + tb_size_t (*head)(struct __tb_iterator_t* iterator); + + /// the iterator last + tb_size_t (*last)(struct __tb_iterator_t* iterator); + + /// the iterator tail + tb_size_t (*tail)(struct __tb_iterator_t* iterator); + + /// the iterator prev + tb_size_t (*prev)(struct __tb_iterator_t* iterator, tb_size_t itor); + + /// the iterator next + tb_size_t (*next)(struct __tb_iterator_t* iterator, tb_size_t itor); + + /// the iterator item + tb_pointer_t (*item)(struct __tb_iterator_t* iterator, tb_size_t itor); + + /// the iterator comp + tb_long_t (*comp)(struct __tb_iterator_t* iterator, tb_cpointer_t litem, tb_cpointer_t ritem); + + /// the iterator copy + tb_void_t (*copy)(struct __tb_iterator_t* iterator, tb_size_t itor, tb_cpointer_t item); + + /// the iterator remove + tb_void_t (*remove)(struct __tb_iterator_t* iterator, tb_size_t itor); + + /// the iterator remove range + tb_void_t (*remove_range)(struct __tb_iterator_t* iterator, tb_size_t prev, tb_size_t next, tb_size_t size); + +}tb_iterator_t; + +/// the array iterator type +typedef struct __tb_array_iterator_t +{ + /// the iterator base + tb_iterator_t base; + + /// the items + tb_pointer_t items; + + /// the items count + tb_size_t count; + +}tb_array_iterator_t, *tb_array_iterator_ref_t; + +/// the iterator ref type +typedef tb_iterator_t* tb_iterator_ref_t; + +/// the iterator comp func type +typedef tb_long_t (*tb_iterator_comp_t)(tb_iterator_ref_t iterator, tb_cpointer_t litem, tb_cpointer_t ritem); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make iterator for the long array + * + * @param iterator the iterator + * @param items the items + * @param count the count + * + * @return the iterator + */ +tb_iterator_ref_t tb_iterator_make_for_long(tb_array_iterator_ref_t iterator, tb_long_t* items, tb_size_t count); + +/*! make iterator for the size array + * + * @param iterator the iterator + * @param items the items + * @param count the count + * + * @return the iterator + */ +tb_iterator_ref_t tb_iterator_make_for_size(tb_array_iterator_ref_t iterator, tb_size_t* items, tb_size_t count); + +/*! make iterator for the c-string array + * + * @param iterator the iterator + * @param items the items + * @param count the count + * + * @return the iterator + */ +tb_iterator_ref_t tb_iterator_make_for_str(tb_array_iterator_ref_t iterator, tb_char_t** items, tb_size_t count); + +/*! make iterator for the c-string array and ignore case + * + * @param iterator the iterator + * @param items the items + * @param count the count + * + * @return the iterator + */ +tb_iterator_ref_t tb_iterator_make_for_istr(tb_array_iterator_ref_t iterator, tb_char_t** items, tb_size_t count); + +/*! make iterator for the pointer array + * + * @param iterator the iterator + * @param items the items + * @param count the count + * + * @return the iterator + */ +tb_iterator_ref_t tb_iterator_make_for_ptr(tb_array_iterator_ref_t iterator, tb_pointer_t* items, tb_size_t count); + +/*! make iterator for the memory array + * + * @param iterator the iterator + * @param items the items + * @param count the count + * @param size the element size + * + * @return the iterator + */ +tb_iterator_ref_t tb_iterator_make_for_mem(tb_array_iterator_ref_t iterator, tb_pointer_t items, tb_size_t count, tb_size_t size); + +/*! the iterator mode + * + * @param iterator the iterator + * + * @return the iterator mode + */ +tb_size_t tb_iterator_mode(tb_iterator_ref_t iterator); + +/*! the iterator step + * + * @param iterator the iterator + * + * @return the iterator step + */ +tb_size_t tb_iterator_step(tb_iterator_ref_t iterator); + +/*! the iterator size + * + * @param iterator the iterator + * + * @return the iterator size + */ +tb_size_t tb_iterator_size(tb_iterator_ref_t iterator); + +/*! the iterator head + * + * @param iterator the iterator + * + * @return the iterator head + */ +tb_size_t tb_iterator_head(tb_iterator_ref_t iterator); + +/*! the iterator last + * + * @param iterator the iterator + * + * @return the iterator last + */ +tb_size_t tb_iterator_last(tb_iterator_ref_t iterator); + +/*! the iterator tail + * + * @param iterator the iterator + * + * @return the iterator tail + */ +tb_size_t tb_iterator_tail(tb_iterator_ref_t iterator); + +/*! the iterator prev + * + * @param iterator the iterator + * @param itor the item itor + * + * @return the iterator prev + */ +tb_size_t tb_iterator_prev(tb_iterator_ref_t iterator, tb_size_t itor); + +/*! the iterator next + * + * @param iterator the iterator + * @param itor the item itor + * + * @return the iterator next + */ +tb_size_t tb_iterator_next(tb_iterator_ref_t iterator, tb_size_t itor); + +/*! the iterator item + * + * @param iterator the iterator + * @param itor the item itor + * + * @return the iterator item + */ +tb_pointer_t tb_iterator_item(tb_iterator_ref_t iterator, tb_size_t itor); + +/*! remove the iterator item + * + * @param iterator the iterator + * @param itor the item itor + */ +tb_void_t tb_iterator_remove(tb_iterator_ref_t iterator, tb_size_t itor); + +/*! remove the iterator items from range(prev, next) + * + * @param iterator the iterator + * @param prev the prev item + * @param next the next item + * @param size the removed size + */ +tb_void_t tb_iterator_remove_range(tb_iterator_ref_t iterator, tb_size_t prev, tb_size_t next, tb_size_t size); + +/*! copy the iterator item + * + * @param iterator the iterator + * @param itor the item itor + * @param item the copied item + */ +tb_void_t tb_iterator_copy(tb_iterator_ref_t iterator, tb_size_t itor, tb_cpointer_t item); + +/*! compare the iterator item + * + * @param iterator the iterator + * @param itor the item + * @param item the compared item + * + * @return =: 0, >: 1, <: -1 + */ +tb_long_t tb_iterator_comp(tb_iterator_ref_t iterator, tb_cpointer_t litem, tb_cpointer_t ritem); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/container/iterator/prefix.h b/core/pkg/tbox.pkg/inc/tbox/container/iterator/prefix.h index c5e0f98d2..071b11b60 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/iterator/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/iterator/prefix.h @@ -38,3 +38,43 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_CONTAINER_ITERATOR_PREFIX_H +#define TB_CONTAINER_ITERATOR_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../iterator.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" +#include "../../memory/memory.h" +#include "../../object/object.h" +#include "../../platform/platform.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/container/list.h b/core/pkg/tbox.pkg/inc/tbox/container/list.h index ebc39fc70..13ea6a2f5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/list.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/list.h @@ -256,3 +256,261 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file list.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_LIST_H +#define TB_CONTAINER_LIST_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the doubly-linked list ref type + * + * + * <pre> + * list: tail => |-----| => |-------------------------------------------------=> |------| => |------| => tail + * | head last | + * | | + * <----------------------------------------------------------------------------------------------- + * + * performance: + * + * insert: + * insert midd: fast + * insert head: fast + * insert tail: fast + * insert next: fast + * + * remove: + * remove midd: fast + * remove head: fast + * remove last: fast + * remove next: fast + * + * iterator: + * next: fast + * prev: fast + * </pre> + * + */ +typedef tb_iterator_ref_t tb_list_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init list + * + * @param grow the grow size + * @param element the element + * + * @return the list + */ +tb_list_ref_t tb_list_init(tb_size_t grow, tb_element_t element); + +/*! exit list + * + * @param list the list + */ +tb_void_t tb_list_exit(tb_list_ref_t list); + +/*! clear list + * + * @param list the list + */ +tb_void_t tb_list_clear(tb_list_ref_t list); + +/*! the list head item + * + * @param list the list + * + * @return the head item + */ +tb_pointer_t tb_list_head(tb_list_ref_t list); + +/*! the list last item + * + * @param list the list + * + * @return the last item + */ +tb_pointer_t tb_list_last(tb_list_ref_t list); + +/*! insert the prev item + * + * @param list the list + * @param itor the item itor + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_list_insert_prev(tb_list_ref_t list, tb_size_t itor, tb_cpointer_t data); + +/*! insert the next item + * + * @param list the list + * @param itor the item itor + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_list_insert_next(tb_list_ref_t list, tb_size_t itor, tb_cpointer_t data); + +/*! insert the head item + * + * @param list the list + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_list_insert_head(tb_list_ref_t list, tb_cpointer_t data); + +/*! insert the tail item + * + * @param list the list + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_list_insert_tail(tb_list_ref_t list, tb_cpointer_t data); + +/*! replace the item + * + * @param list the list + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_list_replace(tb_list_ref_t list, tb_size_t itor, tb_cpointer_t data); + +/*! replace the head item + * + * @param list the list + * @param data the item data + */ +tb_void_t tb_list_replace_head(tb_list_ref_t list, tb_cpointer_t data); + +/*! replace the tail item + * + * @param list the list + * @param data the item data + */ +tb_void_t tb_list_replace_last(tb_list_ref_t list, tb_cpointer_t data); + +/*! remove the item + * + * @param list the list + * @param itor the item itor + * + * @return the next item + */ +tb_size_t tb_list_remove(tb_list_ref_t list, tb_size_t itor); + +/*! remove the head item + * + * @param list the list + */ +tb_void_t tb_list_remove_head(tb_list_ref_t list); + +/*! remove the last item + * + * @param list the list + */ +tb_void_t tb_list_remove_last(tb_list_ref_t list); + +/*! moveto the prev item + * + * @param list the list + * @param itor the item itor + * @param move the move itor + */ +tb_void_t tb_list_moveto_prev(tb_list_ref_t list, tb_size_t itor, tb_size_t move); + +/*! moveto the next item + * + * @param list the list + * @param itor the item itor + * @param move the move itor + */ +tb_void_t tb_list_moveto_next(tb_list_ref_t list, tb_size_t itor, tb_size_t move); + +/*! moveto the head item + * + * @param list the list + * @param itor the item itor + */ +tb_void_t tb_list_moveto_head(tb_list_ref_t list, tb_size_t move); + +/*! moveto the tail item + * + * @param list the list + * @param itor the item itor + */ +tb_void_t tb_list_moveto_tail(tb_list_ref_t list, tb_size_t move); + +/*! the item count + * + * @param list the list + * + * @return the item count + */ +tb_size_t tb_list_size(tb_list_ref_t list); + +/*! the item max count + * + * @param list the list + * + * @return the item max count + */ +tb_size_t tb_list_maxn(tb_list_ref_t list); + +#ifdef __tb_debug__ +/*! dump list + * + * @param list the list + */ +tb_void_t tb_list_dump(tb_list_ref_t list); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/list_entry.h b/core/pkg/tbox.pkg/inc/tbox/container/list_entry.h index 7abebdd53..955b9009e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/list_entry.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/list_entry.h @@ -690,3 +690,695 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file list_entry.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_LIST_ENTRY_H +#define TB_CONTAINER_LIST_ENTRY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// get the list entry +#define tb_list_entry(head, entry) ((((tb_byte_t*)(entry)) - (head)->eoff)) + +/*! get the list entry with zero offset + * + * @code + * + // the xxxx entry type + typedef struct __tb_xxxx_entry_t + { + // the list entry (be placed in header with zero offset) + tb_list_entry_t entry; + + // .. + + }tb_xxxx_entry_t; + * + * @endcode + */ +#define tb_list_entry0(entry) (entry) + +/*! init the list entry + * + * @code + * + // the xxxx entry type + typedef struct __tb_xxxx_entry_t + { + // the list entry + tb_list_entry_t entry; + + // the data + tb_size_t data; + + }tb_xxxx_entry_t; + + // the xxxx entry copy func + static tb_void_t tb_xxxx_entry_copy(tb_pointer_t litem, tb_pointer_t ritem) + { + // check + tb_assert(litem && ritem); + + // copy it + ((tb_xxxx_entry_t*)litem)->data = ((tb_xxxx_entry_t*)ritem)->data; + } + + // init the list + tb_list_entry_head_t list; + tb_list_entry_init(&list, tb_xxxx_entry_t, entry, tb_xxxx_entry_copy); + + * @endcode + */ +#define tb_list_entry_init(list, type, entry, copy) tb_list_entry_init_(list, tb_offsetof(type, entry), sizeof(type), copy) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the doubly-linked list entry type + * + * <pre> + * list: list => ... => last + * | | + * <--------------- + * + * </pre> + */ +typedef struct __tb_list_entry_t +{ + /// the next entry + struct __tb_list_entry_t* next; + + /// the prev entry + struct __tb_list_entry_t* prev; + +}tb_list_entry_t, *tb_list_entry_ref_t; + +/// the list entry head type +typedef struct __tb_list_entry_head_t +{ + /// the next entry + struct __tb_list_entry_t* next; + + /// the prev entry + struct __tb_list_entry_t* prev; + + /// the list size + tb_size_t size; + + /// the iterator + tb_iterator_t itor; + + /// the entry offset + tb_size_t eoff; + + /// the entry copy func + tb_entry_copy_t copy; + +}tb_list_entry_head_t, *tb_list_entry_head_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the list iterator + * + * @param list the list + * + * @return the list iterator + */ +tb_iterator_ref_t tb_list_entry_itor(tb_list_entry_head_ref_t list); + +/*! init list + * + * @param list the list + * @param entry_offset the entry offset + * @param entry_size the entry size + * @param copy the copy func of the entry for algorithm, .e.g sort + */ +tb_void_t tb_list_entry_init_(tb_list_entry_head_ref_t list, tb_size_t entry_offset, tb_size_t entry_size, tb_entry_copy_t copy); + +/*! exit list + * + * @param list the list + */ +tb_void_t tb_list_entry_exit(tb_list_entry_head_ref_t list); + +/*! clear list + * + * @param list the list + */ +static __tb_inline__ tb_void_t tb_list_entry_clear(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // clear it + list->next = (tb_list_entry_ref_t)list; + list->prev = (tb_list_entry_ref_t)list; + list->size = 0; +} + +/*! the list entry count + * + * @param list the list + * + * @return the list entry count + */ +static __tb_inline__ tb_size_t tb_list_entry_size(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return list->size; +} + +/*! the list next entry + * + * @param entry the entry + * + * @return the next entry + */ +static __tb_inline__ tb_list_entry_ref_t tb_list_entry_next(tb_list_entry_ref_t entry) +{ + // check + tb_assert(entry); + + // done + return entry->next; +} + +/*! the list prev entry + * + * @param entry the entry + * + * @return the prev entry + */ +static __tb_inline__ tb_list_entry_ref_t tb_list_entry_prev(tb_list_entry_ref_t entry) +{ + // check + tb_assert(entry); + + // done + return entry->prev; +} + +/*! the list head entry + * + * @param list the list + * + * @return the head entry + */ +static __tb_inline__ tb_list_entry_ref_t tb_list_entry_head(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return list->next; +} + +/*! the list last entry + * + * @param list the list + * + * @return the last entry + */ +static __tb_inline__ tb_list_entry_ref_t tb_list_entry_last(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return list->prev; +} + +/*! the list tail entry + * + * @param list the list + * + * @return the tail entry + */ +static __tb_inline__ tb_list_entry_ref_t tb_list_entry_tail(tb_list_entry_head_ref_t list) +{ + // done + return (tb_list_entry_ref_t)list; +} + +/*! the list is null? + * + * @param list the list + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_list_entry_is_null(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return !list->size; +} + +/*! is the list head entry? + * + * @param list the list + * @param entry the entry + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_list_entry_is_head(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // done + return list->next == entry; +} + +/*! is the list last entry? + * + * @param list the list + * @param entry the entry + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_list_entry_is_last(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // done + return list->prev == entry; +} + +/*! the list is valid? + * + * @param list the list + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_list_entry_is_valid(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return (list->next && list->next->prev == (tb_list_entry_ref_t)list) && (list->prev && list->prev->next == (tb_list_entry_ref_t)list); +} + +/*! splice the spliced_list to list[prev, next] + * + * @param list the list + * @param prev the prev + * @param next the next + * @param spliced_list the spliced list + */ +static __tb_inline__ tb_void_t tb_list_entry_splice(tb_list_entry_head_ref_t list, tb_list_entry_ref_t prev, tb_list_entry_ref_t next, tb_list_entry_head_ref_t spliced_list) +{ + // check + tb_assert(list && prev && next); + tb_assert(spliced_list && spliced_list->next && spliced_list->prev); + + // valid? + tb_assert(tb_list_entry_is_valid(list)); + tb_assert(tb_list_entry_is_valid(spliced_list)); + + // empty? + tb_check_return(!tb_list_entry_is_null(spliced_list)); + + // done + spliced_list->next->prev = prev; + prev->next = spliced_list->next; + spliced_list->prev->next = next; + next->prev = spliced_list->prev; + + // update size + list->size += spliced_list->size; + + // clear the spliced list + tb_list_entry_clear(spliced_list); +} + +/*! splice the spliced_list at the list head + * + * @param list the list + * @param spliced_list the spliced list + */ +static __tb_inline__ tb_void_t tb_list_entry_splice_head(tb_list_entry_head_ref_t list, tb_list_entry_head_ref_t spliced_list) +{ + // check + tb_assert(list); + + // done + tb_list_entry_splice(list, (tb_list_entry_ref_t)list, list->next, spliced_list); +} + +/*! splice the spliced_list at the list tail + * + * @param list the list + * @param spliced_list the spliced list + */ +static __tb_inline__ tb_void_t tb_list_entry_splice_tail(tb_list_entry_head_ref_t list, tb_list_entry_head_ref_t spliced_list) +{ + // check + tb_assert(list); + + // done + tb_list_entry_splice(list, list->prev, (tb_list_entry_ref_t)list, spliced_list); +} + +/*! insert entry to the next + * + * @param list the list + * @param node the list node + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_insert_next(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list && node && node->next && entry); + tb_assert(node != entry); + + // valid? + tb_assert(tb_list_entry_is_valid(list)); + + // insert entry + node->next->prev = entry; + entry->next = node->next; + entry->prev = node; + node->next = entry; + + // size++ + list->size++; +} + +/*! insert entry to the prev + * + * @param list the list + * @param node the list node + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_insert_prev(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list && node); + + // insert it + tb_list_entry_insert_next(list, node->prev, entry); +} + +/*! insert entry to the head + * + * @param list the list + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_insert_head(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + tb_list_entry_insert_next(list, (tb_list_entry_ref_t)list, entry); +} + +/*! insert entry to the tail + * + * @param list the list + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_insert_tail(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list && entry); + + // insert it + tb_list_entry_insert_next(list, list->prev, entry); +} + +/*! replace the entry + * + * @param list the list + * @param node the replaced list node + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_replace(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_assert(node && node->next && node->prev && entry); + tb_assert(node != entry); + + // valid? + tb_assert(tb_list_entry_is_valid(list)); + + // replace it + entry->next = node->next; + entry->next->prev = entry; + entry->prev = node->prev; + entry->prev->next = entry; +} + +/*! replace the next entry + * + * @param list the list + * @param node the list node + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_replace_next(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_assert(node); + + // replace it + tb_list_entry_replace(list, node->next, entry); +} + +/*! replace the prev entry + * + * @param list the list + * @param node the list node + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_replace_prev(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_assert(node); + + // replace it + tb_list_entry_replace(list, node->prev, entry); +} + +/*! replace the head entry + * + * @param list the list + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_replace_head(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // replace it + tb_list_entry_replace(list, list->next, entry); +} + +/*! replace the last entry + * + * @param list the list + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_replace_last(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // replace it + tb_list_entry_replace(list, list->prev, entry); +} + +/*! remove the entry safely + * + * @param list the list + * @param prev the prev entry + * @param next the next entry + */ +static __tb_inline__ tb_void_t tb_list_entry_remove_safe(tb_list_entry_head_ref_t list, tb_list_entry_ref_t prev, tb_list_entry_ref_t next) +{ + // check + tb_assert(list && list->size && prev && next); + + // valid? + tb_assert(tb_list_entry_is_valid(list)); + + // remove entries + prev->next = next; + next->prev = prev; + + // update size + list->size--; +} + +/*! remove the entry + * + * @param list the list + * @param entry the removed list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_remove(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(entry); + + // remove it + tb_list_entry_remove_safe(list, entry->prev, entry->next); +} + +/*! remove the next entry + * + * @param list the list + * @param prev the prev entry + */ +static __tb_inline__ tb_void_t tb_list_entry_remove_next(tb_list_entry_head_ref_t list, tb_list_entry_ref_t prev) +{ + // check + tb_assert(prev && prev->next); + + // remove it + tb_list_entry_remove_safe(list, prev, prev->next->next); +} + +/*! remove the prev entry + * + * @param list the list + * @param next the next entry + */ +static __tb_inline__ tb_void_t tb_list_entry_remove_prev(tb_list_entry_head_ref_t list, tb_list_entry_ref_t next) +{ + // check + tb_assert(next && next->prev); + + // remove it + tb_list_entry_remove_safe(list, next->prev->prev, next); +} + +/*! remove the head entry + * + * @param list the list + */ +static __tb_inline__ tb_void_t tb_list_entry_remove_head(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list && list->next); + + // remove it + tb_list_entry_remove_safe(list, (tb_list_entry_ref_t)list, list->next->next); +} + +/*! remove the last entry + * + * @param list the list + */ +static __tb_inline__ tb_void_t tb_list_entry_remove_last(tb_list_entry_head_ref_t list) +{ + // check + tb_assert(list && list->prev); + + // remove it + tb_list_entry_remove_safe(list, list->prev->prev, (tb_list_entry_ref_t)list); +} + +/*! moveto the next entry + * + * @param list the list + * @param node the list node + * @param entry the moved list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_moveto_next(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_check_return(node != entry); + + // move it + tb_list_entry_remove(list, entry); + tb_list_entry_insert_next(list, node, entry); +} + +/*! moveto the prev entry + * + * @param list the list + * @param node the list node + * @param entry the moved list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_moveto_prev(tb_list_entry_head_ref_t list, tb_list_entry_ref_t node, tb_list_entry_ref_t entry) +{ + // check + tb_assert(node); + + // move it + tb_list_entry_moveto_next(list, node->prev, entry); +} + +/*! moveto the head entry + * + * @param list the list + * @param entry the moved list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_moveto_head(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // move it + tb_list_entry_moveto_next(list, (tb_list_entry_ref_t)list, entry); +} + +/*! moveto the tail entry + * + * @param list the list + * @param entry the moved list entry + */ +static __tb_inline__ tb_void_t tb_list_entry_moveto_tail(tb_list_entry_head_ref_t list, tb_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // move it + tb_list_entry_moveto_next(list, list->prev, entry); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/prefix.h b/core/pkg/tbox.pkg/inc/tbox/container/prefix.h index 4e24d0261..939ebc00e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/prefix.h @@ -38,3 +38,43 @@ typedef tb_void_t (*tb_entry_copy_t)(tb_pointer_t litem, tb_pointer_t ritem); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_CONTAINER_PREFIX_H +#define TB_CONTAINER_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the entry copy func type +typedef tb_void_t (*tb_entry_copy_t)(tb_pointer_t litem, tb_pointer_t ritem); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/container/priority_queue.h b/core/pkg/tbox.pkg/inc/tbox/container/priority_queue.h index 41484aaf4..a8822d657 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/priority_queue.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/priority_queue.h @@ -132,3 +132,137 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file priority_queue.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_PRIORITY_QUEUE_H +#define TB_CONTAINER_PRIORITY_QUEUE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "element.h" +#include "heap.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the priority queue ref type + * + * using the min/max heap + */ +typedef tb_heap_ref_t tb_priority_queue_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init queue, default: min-priority + * + * @param grow the element grow, using the default grow if be zero + * @param element the element + * + * @return the queue + */ +tb_priority_queue_ref_t tb_priority_queue_init(tb_size_t grow, tb_element_t element); + +/*! exit queue + * + * @param queue the queue + */ +tb_void_t tb_priority_queue_exit(tb_priority_queue_ref_t queue); + +/*! clear the queue + * + * @param queue the queue + */ +tb_void_t tb_priority_queue_clear(tb_priority_queue_ref_t queue); + +/*! the queue size + * + * @param queue the queue + * + * @return the queue size + */ +tb_size_t tb_priority_queue_size(tb_priority_queue_ref_t queue); + +/*! the queue maxn + * + * @param queue the queue + * + * @return the queue maxn + */ +tb_size_t tb_priority_queue_maxn(tb_priority_queue_ref_t queue); + +/*! get the queue item + * + * @param queue the queue + * + * @return the queue top item + */ +tb_pointer_t tb_priority_queue_get(tb_priority_queue_ref_t queue); + +/*! put the queue item + * + * @param queue the queue + * @param data the item data + */ +tb_void_t tb_priority_queue_put(tb_priority_queue_ref_t queue, tb_cpointer_t data); + +/*! pop the queue item + * + * @param queue the queue + */ +tb_void_t tb_priority_queue_pop(tb_priority_queue_ref_t queue); + +/*! remove the queue item + * + * @param queue the queue + * @param itor the itor + */ +tb_void_t tb_priority_queue_remove(tb_priority_queue_ref_t queue, tb_size_t itor); + +#ifdef __tb_debug__ +/*! dump queue + * + * @param queue the queue + */ +tb_void_t tb_priority_queue_dump(tb_priority_queue_ref_t queue); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/queue.h b/core/pkg/tbox.pkg/inc/tbox/container/queue.h index 9eb67fac3..1b3551938 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/queue.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/queue.h @@ -172,3 +172,177 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file queue.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_QUEUE_H +#define TB_CONTAINER_QUEUE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" +#include "single_list.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the queue ref type + * + * <pre> + * queue: |-----| => |-----| => ... => |-----| => tail + * head last + * + * performance: + * + * put: O(1) + * pop: O(1) + * + * iterator: + * + * next: fast + * prev: fast + * + * </pre> + */ +typedef tb_single_list_ref_t tb_queue_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init queue + * + * @param grow the grow size, using the default grow size if be zero + * @param element the element + * + * @return the queue + */ +tb_queue_ref_t tb_queue_init(tb_size_t grow, tb_element_t element); + +/*! exit queue + * + * @param queue the queue + */ +tb_void_t tb_queue_exit(tb_queue_ref_t queue); + +/*! the queue head item + * + * @param queue the queue + * + * @return the head item + */ +tb_pointer_t tb_queue_head(tb_queue_ref_t queue); + +/*! the queue last item + * + * @param queue the queue + * + * @return the last item + */ +tb_pointer_t tb_queue_last(tb_queue_ref_t queue); + +/*! clear the queue + * + * @param queue the queue + */ +tb_void_t tb_queue_clear(tb_queue_ref_t queue); + +/*! put the queue item + * + * @param queue the queue + * @param data the item data + */ +tb_void_t tb_queue_put(tb_queue_ref_t queue, tb_cpointer_t data); + +/*! pop the queue item + * + * @param queue the queue + */ +tb_void_t tb_queue_pop(tb_queue_ref_t queue); + +/*! get the queue item + * + * @param queue the queue + * + * @return the queue item + */ +tb_pointer_t tb_queue_get(tb_queue_ref_t queue); + +/*! the queue size + * + * @param queue the queue + * + * @return the queue size + */ +tb_size_t tb_queue_size(tb_queue_ref_t queue); + +/*! the queue maxn + * + * @param queue the queue + * + * @return the queue maxn + */ +tb_size_t tb_queue_maxn(tb_queue_ref_t queue); + +/*! the queue full? + * + * @param queue the queue + * + * @return tb_true or tb_false + */ +tb_bool_t tb_queue_full(tb_queue_ref_t queue); + +/*! the queue null? + * + * @param queue the queue + * + * @return tb_true or tb_false + */ +tb_bool_t tb_queue_null(tb_queue_ref_t queue); + +#ifdef __tb_debug__ +/*! dump queue + * + * @param queue the queue + */ +tb_void_t tb_queue_dump(tb_queue_ref_t queue); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/single_list.h b/core/pkg/tbox.pkg/inc/tbox/container/single_list.h index ca6b00882..619a69348 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/single_list.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/single_list.h @@ -205,3 +205,210 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file single_list.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_SINGLE_LIST_H +#define TB_CONTAINER_SINGLE_LIST_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the double list ref type + * + * + * <pre> + * list: |-----| => |-------------------------------------------------=> |------| => |------| => tail + * head last + * + * performance: + * + * insert: + * insert midd: slow + * insert head: fast + * insert tail: fast + * insert next: fast + * + * remove: + * remove midd: slow + * remove head: fast + * remove last: fast + * remove next: fast + * + * iterator: + * next: fast + * </pre> + * + */ +typedef tb_iterator_ref_t tb_single_list_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init list + * + * @param grow the grow size + * @param element the element + * + * @return the list + */ +tb_single_list_ref_t tb_single_list_init(tb_size_t grow, tb_element_t element); + +/*! exit list + * + * @param list the list + */ +tb_void_t tb_single_list_exit(tb_single_list_ref_t list); + +/*! clear list + * + * @param list the list + */ +tb_void_t tb_single_list_clear(tb_single_list_ref_t list); + +/*! the list head item + * + * @param list the list + * + * @return the head item + */ +tb_pointer_t tb_single_list_head(tb_single_list_ref_t list); + +/*! the list last item + * + * @param list the list + * + * @return the last item + */ +tb_pointer_t tb_single_list_last(tb_single_list_ref_t list); + +/*! insert the next item + * + * @param list the list + * @param itor the item itor + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_single_list_insert_next(tb_single_list_ref_t list, tb_size_t itor, tb_cpointer_t data); + +/*! insert the head item + * + * @param list the list + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_single_list_insert_head(tb_single_list_ref_t list, tb_cpointer_t data); + +/*! insert the tail item + * + * @param list the list + * @param data the item data + * + * @return the item itor + */ +tb_size_t tb_single_list_insert_tail(tb_single_list_ref_t list, tb_cpointer_t data); + +/*! replace the item + * + * @param list the list + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_single_list_replace(tb_single_list_ref_t list, tb_size_t itor, tb_cpointer_t data); + +/*! replace the head item + * + * @param list the list + * @param data the item data + */ +tb_void_t tb_single_list_replace_head(tb_single_list_ref_t list, tb_cpointer_t data); + +/*! replace the tail item + * + * @param list the list + * @param data the item data + */ +tb_void_t tb_single_list_replace_last(tb_single_list_ref_t list, tb_cpointer_t data); + +/*! remove the next item + * + * @param list the list + * @param itor the item itor + */ +tb_void_t tb_single_list_remove_next(tb_single_list_ref_t list, tb_size_t itor); + +/*! remove the head item + * + * @param list the list + */ +tb_void_t tb_single_list_remove_head(tb_single_list_ref_t list); + +/*! the item count + * + * @param list the list + * + * @return the item count + */ +tb_size_t tb_single_list_size(tb_single_list_ref_t list); + +/*! the item max count + * + * @param list the list + * + * @return the item max count + */ +tb_size_t tb_single_list_maxn(tb_single_list_ref_t list); + +#ifdef __tb_debug__ +/*! dump list + * + * @param list the list + */ +tb_void_t tb_single_list_dump(tb_single_list_ref_t list); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/single_list_entry.h b/core/pkg/tbox.pkg/inc/tbox/container/single_list_entry.h index 13606d59a..fac6f0b5c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/single_list_entry.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/single_list_entry.h @@ -430,3 +430,435 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file single_list_entry.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_SINGLE_LIST_ENTRY_H +#define TB_CONTAINER_SINGLE_LIST_ENTRY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the list entry +#define tb_single_list_entry(head, entry) ((((tb_byte_t*)(entry)) - (head)->eoff)) + +/*! get the list entry with zero offset + * + * @code + * + // the xxxx entry type + typedef struct __tb_xxxx_entry_t + { + // the list entry (be placed in header with zero offset) + tb_single_list_entry_t entry; + + // .. + + }tb_xxxx_entry_t; + * + * @endcode + */ +#define tb_single_list_entry0(entry) (entry) + +/*! init the list entry + * + * @code + * + // the xxxx entry type + typedef struct __tb_xxxx_entry_t + { + // the list entry + tb_single_list_entry_t entry; + + // the data + tb_size_t data; + + }tb_xxxx_entry_t; + + // the xxxx entry copy func + static tb_void_t tb_xxxx_entry_copy(tb_pointer_t litem, tb_pointer_t ritem) + { + // check + tb_assert(litem && ritem); + + // copy it + ((tb_xxxx_entry_t*)litem)->data = ((tb_xxxx_entry_t*)ritem)->data; + } + + // init the list + tb_single_list_entry_head_t list; + tb_single_list_entry_init(&list, tb_xxxx_entry_t, entry, tb_xxxx_entry_copy); + + * @endcode + */ +#define tb_single_list_entry_init(list, type, entry, copy) tb_single_list_entry_init_(list, tb_offsetof(type, entry), sizeof(type), copy) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the single-linked list entry type + * + * <pre> + * list: head => ... => last => null + * + * </pre> + */ +typedef struct __tb_single_list_entry_t +{ + /// the next entry + struct __tb_single_list_entry_t* next; + +}tb_single_list_entry_t, *tb_single_list_entry_ref_t; + +/// the single-linked list entry head type +typedef struct __tb_single_list_entry_head_t +{ + /// the next entry + struct __tb_single_list_entry_t* next; + + /// the last entry + struct __tb_single_list_entry_t* last; + + /// the list size + tb_size_t size; + + /// the iterator + tb_iterator_t itor; + + /// the entry offset + tb_size_t eoff; + + /// the entry copy func + tb_entry_copy_t copy; + +}tb_single_list_entry_head_t, *tb_single_list_entry_head_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the list iterator + * + * @param list the list + * + * @return the list iterator + */ +tb_iterator_ref_t tb_single_list_entry_itor(tb_single_list_entry_head_ref_t list); + +/*! init list + * + * @param list the list + * @param entry_offset the entry offset + * @param entry_size the entry size + * @param copy the copy func of the entry for algorithm, .e.g sort + */ +tb_void_t tb_single_list_entry_init_(tb_single_list_entry_head_ref_t list, tb_size_t entry_offset, tb_size_t entry_size, tb_entry_copy_t copy); + +/*! exit list + * + * @param list the list + */ +tb_void_t tb_single_list_entry_exit(tb_single_list_entry_head_ref_t list); + +/*! clear list + * + * @param list the list + */ +static __tb_inline__ tb_void_t tb_single_list_entry_clear(tb_single_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // clear it + list->next = tb_null; + list->last = tb_null; + list->size = 0; +} + +/*! the list entry count + * + * @param list the list + * + * @return the list entry count + */ +static __tb_inline__ tb_size_t tb_single_list_entry_size(tb_single_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return list->size; +} + +/*! the list next entry + * + * @param entry the entry + * + * @return the next entry + */ +static __tb_inline__ tb_single_list_entry_ref_t tb_single_list_entry_next(tb_single_list_entry_ref_t entry) +{ + // check + tb_assert(entry); + + // done + return entry->next; +} + +/*! the list head entry + * + * @param list the list + * + * @return the head entry + */ +static __tb_inline__ tb_single_list_entry_ref_t tb_single_list_entry_head(tb_single_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return list->next; +} + +/*! the list last entry + * + * @param list the list + * + * @return the last entry + */ +static __tb_inline__ tb_single_list_entry_ref_t tb_single_list_entry_last(tb_single_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return list->last; +} + +/*! the list tail entry + * + * @param list the list + * + * @return the tail entry + */ +static __tb_inline__ tb_single_list_entry_ref_t tb_single_list_entry_tail(tb_single_list_entry_head_ref_t list) +{ + return tb_null; +} + +/*! the list is null? + * + * @param list the list + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_single_list_entry_is_null(tb_single_list_entry_head_ref_t list) +{ + // check + tb_assert(list); + + // done + return !list->size; +} +/*! is the list head entry? + * + * @param list the list + * @param entry the entry + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_single_list_entry_is_head(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // done + return list->next == entry; +} + +/*! is the list last entry? + * + * @param list the list + * @param entry the entry + * + * @return tb_true or tb_false + */ +static __tb_inline__ tb_bool_t tb_single_list_entry_is_last(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t entry) +{ + // check + tb_assert(list); + + // done + return list->last == entry; +} + +/*! insert entry to the next + * + * @param list the list + * @param node the list node + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_insert_next(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t node, tb_single_list_entry_ref_t entry) +{ + // check + tb_assert(list && node && entry); + tb_assert(node != entry); + + // update last + if (node == list->last || !list->next) list->last = entry; + + // insert entry + entry->next = node->next; + node->next = entry; + + // size++ + list->size++; +} + +/*! insert entry to the head + * + * @param list the list + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_insert_head(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t entry) +{ + // insert it + tb_single_list_entry_insert_next(list, (tb_single_list_entry_ref_t)list, entry); +} + +/*! insert entry to the tail + * + * @param list the list + * @param entry the inserted list entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_insert_tail(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t entry) +{ + // check + tb_assert(list && entry); + + // insert it + if (list->last) tb_single_list_entry_insert_next(list, list->last, entry); + else tb_single_list_entry_insert_head(list, entry); +} + +/*! replace the next entry + * + * @param list the list + * @param node the list node + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_replace_next(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t node, tb_single_list_entry_ref_t entry) +{ + // check + tb_assert(node && node->next); + tb_assert(node != entry); + + // update last + if (node->next == list->last) list->last = entry; + + // replace it + entry->next = node->next->next; + node->next = entry; +} + +/*! replace the head entry + * + * @param list the list + * @param entry the new list entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_replace_head(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t entry) +{ + // replace it + tb_single_list_entry_replace_next(list, (tb_single_list_entry_ref_t)list, entry); +} + +/*! remove the entry safely + * + * @param list the list + * @param prev the prev entry + * @param next the next entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_remove_safe(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t prev, tb_single_list_entry_ref_t next) +{ + // check + tb_assert(list && list->size && prev); + + // update last + if (prev->next == list->last) list->last = next; + + // remove entries + prev->next = next; + + // update size + list->size--; +} + +/*! remove the next entry + * + * @param list the list + * @param entry the prev entry + */ +static __tb_inline__ tb_void_t tb_single_list_entry_remove_next(tb_single_list_entry_head_ref_t list, tb_single_list_entry_ref_t prev) +{ + // check + tb_assert(prev && prev->next); + + // remove it + tb_single_list_entry_remove_safe(list, prev, prev->next->next); +} + +/*! remove the head entry + * + * @param list the list + */ +static __tb_inline__ tb_void_t tb_single_list_entry_remove_head(tb_single_list_entry_head_ref_t list) +{ + // check + tb_assert(list->next); + + // remove it + tb_single_list_entry_remove_safe(list, (tb_single_list_entry_ref_t)list, list->next->next); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/stack.h b/core/pkg/tbox.pkg/inc/tbox/container/stack.h index 7de5efb42..cb6b5e28c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/stack.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/stack.h @@ -165,3 +165,170 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file stack.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_STACK_H +#define TB_CONTAINER_STACK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "vector.h" +#include "element.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the stack ref type + * + * <pre> + * stack: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||------| + * head last tail + * + * stack: |||||||||||||||||||||||||||||||||||||||||------| + * head last tail + * + * performance: + * + * push: fast + * pop: fast + * + * iterator: + * next: fast + * prev: fast + * </pre> + * + * @note the itor of the same item is fixed + * + */ +typedef tb_vector_ref_t tb_stack_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init stack + * + * @param grow the item grow + * @param element the element + * + * @return the stack + */ +tb_stack_ref_t tb_stack_init(tb_size_t grow, tb_element_t element); + +/*! exit stack + * + * @param stack the stack + */ +tb_void_t tb_stack_exit(tb_stack_ref_t stack); + +/*! the stack head item + * + * @param stack the stack + * + * @return the head item + */ +tb_pointer_t tb_stack_head(tb_stack_ref_t stack); + +/*! the stack last item + * + * @param stack the stack + * + * @return the last item + */ +tb_pointer_t tb_stack_last(tb_stack_ref_t stack); + +/*! clear the stack + * + * @param stack the stack + */ +tb_void_t tb_stack_clear(tb_stack_ref_t stack); + +/*! copy the stack + * + * @param stack the stack + * @param copy the copied stack + */ +tb_void_t tb_stack_copy(tb_stack_ref_t stack, tb_stack_ref_t copy); + +/*! put the stack item + * + * @param stack the stack + * @param data the item data + */ +tb_void_t tb_stack_put(tb_stack_ref_t stack, tb_cpointer_t data); + +/*! pop the stack item + * + * @param stack the stack + */ +tb_void_t tb_stack_pop(tb_stack_ref_t stack); + +/*! the stack top item + * + * @param stack the stack + * + * @return the stack top item + */ +tb_pointer_t tb_stack_top(tb_stack_ref_t stack); + +/*! the stack size + * + * @param stack the stack + * + * @return the stack size + */ +tb_size_t tb_stack_size(tb_stack_ref_t stack); + +/*! the stack maxn + * + * @param stack the stack + * + * @return the stack maxn + */ +tb_size_t tb_stack_maxn(tb_stack_ref_t stack); + +#ifdef __tb_debug__ +/*! dump stack + * + * @param stack the stack + */ +tb_void_t tb_stack_dump(tb_stack_ref_t stack); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/container/vector.h b/core/pkg/tbox.pkg/inc/tbox/container/vector.h index 7532ce1c3..1efc50f65 100644 --- a/core/pkg/tbox.pkg/inc/tbox/container/vector.h +++ b/core/pkg/tbox.pkg/inc/tbox/container/vector.h @@ -368,3 +368,373 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file vector.h + * @ingroup container + * + */ +#ifndef TB_CONTAINER_VECTOR_H +#define TB_CONTAINER_VECTOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "element.h" +#include "iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the vector ref type + * + * <pre> + * vector: |-----|--------------------------------------------------------|------| + * head last tail + * + * performance: + * + * insert: + * insert midd: slow + * insert head: slow + * insert tail: fast + * + * ninsert: + * ninsert midd: fast + * ninsert head: fast + * ninsert tail: fast + * + * remove: + * remove midd: slow + * remove head: slow + * remove last: fast + * + * nremove: + * nremove midd: fast + * nremove head: fast + * nremove last: fast + * + * iterator: + * next: fast + * prev: fast + * </pre> + * + * @note the itor of the same item is mutable + * + */ +typedef tb_iterator_ref_t tb_vector_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init vector + * + * @code + * + // init vector + tb_vector_ref_t vector = tb_vector_init(0, tb_element_str(tb_true)); + if (vector) + { + // insert elements into head + tb_vector_insert_head(vector, "hi!"); + + // insert elements into tail + tb_vector_insert_tail(vector, "how"); + tb_vector_insert_tail(vector, "are"); + tb_vector_insert_tail(vector, "you"); + + // dump elements + tb_for_all (tb_char_t const*, cstr, vector) + { + // trace + tb_trace_d("%s", cstr); + } + + // exit vector + tb_vector_exit(vector); + } + * @endcode + * + * @param grow the item grow + * @param element the element + * + * @return the vector + */ +tb_vector_ref_t tb_vector_init(tb_size_t grow, tb_element_t element); + +/*! exist vector + * + * @param vector the vector + */ +tb_void_t tb_vector_exit(tb_vector_ref_t vector); + +/*! the vector data + * + * @param vector the vector + * + * @return the vector data + */ +tb_pointer_t tb_vector_data(tb_vector_ref_t vector); + +/*! the vector head item + * + * @param vector the vector + * + * @return the vector head item + */ +tb_pointer_t tb_vector_head(tb_vector_ref_t vector); + +/*! the vector last item + * + * @param vector the vector + * + * @return the vector last item + */ +tb_pointer_t tb_vector_last(tb_vector_ref_t vector); + +/*! resize the vector + * + * @param vector the vector + * @param size the vector size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_vector_resize(tb_vector_ref_t vector, tb_size_t size); + +/*! clear the vector + * + * @param vector the vector + */ +tb_void_t tb_vector_clear(tb_vector_ref_t vector); + +/*! copy the vector + * + * @param vector the vector + * @param copy the copied vector + */ +tb_void_t tb_vector_copy(tb_vector_ref_t vector, tb_vector_ref_t copy); + +/*! insert the vector prev item + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_vector_insert_prev(tb_vector_ref_t vector, tb_size_t itor, tb_cpointer_t data); + +/*! insert the vector next item + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_vector_insert_next(tb_vector_ref_t vector, tb_size_t itor, tb_cpointer_t data); + +/*! insert the vector head item + * + * @param vector the vector + * @param data the item data + */ +tb_void_t tb_vector_insert_head(tb_vector_ref_t vector, tb_cpointer_t data); + +/*! insert the vector tail item + * + * @param vector the vector + * @param data the item data + */ +tb_void_t tb_vector_insert_tail(tb_vector_ref_t vector, tb_cpointer_t data); + +/*! insert the vector prev items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_ninsert_prev(tb_vector_ref_t vector, tb_size_t itor, tb_cpointer_t data, tb_size_t size); + +/*! insert the vector next items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_ninsert_next(tb_vector_ref_t vector, tb_size_t itor, tb_cpointer_t data, tb_size_t size); + +/*! insert the vector head items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_ninsert_head(tb_vector_ref_t vector, tb_cpointer_t data, tb_size_t size); + +/*! insert the vector tail items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_ninsert_tail(tb_vector_ref_t vector, tb_cpointer_t data, tb_size_t size); + +/*! replace the vector item + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_vector_replace(tb_vector_ref_t vector, tb_size_t itor, tb_cpointer_t data); + +/*! replace the vector head item + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_vector_replace_head(tb_vector_ref_t vector, tb_cpointer_t data); + +/*! replace the vector last item + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + */ +tb_void_t tb_vector_replace_last(tb_vector_ref_t vector, tb_cpointer_t data); + +/*! replace the vector items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_nreplace(tb_vector_ref_t vector, tb_size_t itor, tb_cpointer_t data, tb_size_t size); + +/*! replace the vector head items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_nreplace_head(tb_vector_ref_t vector, tb_cpointer_t data, tb_size_t size); + +/*! replace the vector last items + * + * @param vector the vector + * @param itor the item itor + * @param data the item data + * @param size the item count + */ +tb_void_t tb_vector_nreplace_last(tb_vector_ref_t vector, tb_cpointer_t data, tb_size_t size); + +/*! remove the vector item + * + * @param vector the vector + * @param itor the item itor + */ +tb_void_t tb_vector_remove(tb_vector_ref_t vector, tb_size_t itor); + +/*! remove the vector head item + * + * @param vector the vector + */ +tb_void_t tb_vector_remove_head(tb_vector_ref_t vector); + +/*! remove the vector last item + * + * @param vector the vector + */ +tb_void_t tb_vector_remove_last(tb_vector_ref_t vector); + +/*! remove the vector items + * + * @param vector the vector + * @param itor the item itor + * @param size the item count + */ +tb_void_t tb_vector_nremove(tb_vector_ref_t vector, tb_size_t itor, tb_size_t size); + +/*! remove the vector head items + * + * @param vector the vector + * @param size the item count + */ +tb_void_t tb_vector_nremove_head(tb_vector_ref_t vector, tb_size_t size); + +/*! remove the vector last items + * + * @param vector the vector + * @param size the item count + */ +tb_void_t tb_vector_nremove_last(tb_vector_ref_t vector, tb_size_t size); + +/*! the vector size + * + * @param vector the vector + * + * @return the vector size + */ +tb_size_t tb_vector_size(tb_vector_ref_t vector); + +/*! the vector grow + * + * @param vector the vector + * + * @return the vector grow + */ +tb_size_t tb_vector_grow(tb_vector_ref_t vector); + +/*! the vector maxn + * + * @param vector the vector + * + * @return the vector maxn + */ +tb_size_t tb_vector_maxn(tb_vector_ref_t vector); + +#ifdef __tb_debug__ +/*! dump vector + * + * @param vector the vector + */ +tb_void_t tb_vector_dump(tb_vector_ref_t vector); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/channel.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/channel.h index c5b6c0749..ba6260fee 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/channel.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/channel.h @@ -118,3 +118,123 @@ tb_bool_t tb_co_channel_recv_try(tb_co_channel_ref_t channel, tb_p __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file channel.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_CHANNEL_H +#define TB_COROUTINE_CHANNEL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../container/container.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the coroutine channel ref type +typedef __tb_typeref__(co_channel); + +/*! the free function type + * + * @param data the channel data + * @param priv the user private data + */ +typedef tb_void_t (*tb_co_channel_free_func_t)(tb_pointer_t data, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init channel + * + * @param size the buffer size, 0: no buffer + * @param free the free function + * @param priv the user private data + * + * @return the channel + */ +tb_co_channel_ref_t tb_co_channel_init(tb_size_t size, tb_co_channel_free_func_t free, tb_cpointer_t priv); + +/*! exit channel + * + * @param channel the channel + */ +tb_void_t tb_co_channel_exit(tb_co_channel_ref_t channel); + +/*! send data into channel + * + * the current coroutine will be suspend if this channel is full + * + * @param channel the channel + * @param data the channel data + */ +tb_void_t tb_co_channel_send(tb_co_channel_ref_t channel, tb_cpointer_t data); + +/*! recv data from channel + * + * the current coroutine will be suspend if no data + * + * @param channel the channel + * + * @return the channel data + */ +tb_pointer_t tb_co_channel_recv(tb_co_channel_ref_t channel); + +/*! try sending data into channel only with buffer + * + * the current coroutine will be suspend if this channel is full + * + * @param channel the channel + * @param data the channel data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_co_channel_send_try(tb_co_channel_ref_t channel, tb_cpointer_t data); + +/*! try recving data from channel only with buffer + * + * the current coroutine will be suspend if no data + * + * @param channel the channel + * @param pdata the channel data pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_co_channel_recv_try(tb_co_channel_ref_t channel, tb_pointer_t* pdata); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/coroutine.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/coroutine.h index 7bdc33d82..df0069fa3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/coroutine.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/coroutine.h @@ -118,3 +118,123 @@ tb_coroutine_ref_t tb_coroutine_self(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file coroutine.h + * @defgroup coroutine + * + */ +#ifndef TB_COROUTINE_H +#define TB_COROUTINE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "lock.h" +#include "channel.h" +#include "semaphore.h" +#include "scheduler.h" +#include "stackless/stackless.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the coroutine ref type +typedef __tb_typeref__(coroutine); + +/// the coroutine function type +typedef tb_void_t (*tb_coroutine_func_t)(tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! start coroutine + * + * @param scheduler the scheduler, uses the current scheduler if be null + * @param func the coroutine function + * @param priv the passed user private data as the argument of function + * @param stacksize the stack size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_coroutine_start(tb_co_scheduler_ref_t scheduler, tb_coroutine_func_t func, tb_cpointer_t priv, tb_size_t stacksize); + +/*! yield the current coroutine + * + * @return tb_true(yield ok) or tb_false(yield failed, no more coroutines) + */ +tb_bool_t tb_coroutine_yield(tb_noarg_t); + +/*! resume the given coroutine (suspended) + * + * @param coroutine the suspended coroutine + * @param priv the user private data as the return value of suspend() or sleep() + * + * @return the user private data from suspend(priv) + */ +tb_pointer_t tb_coroutine_resume(tb_coroutine_ref_t coroutine, tb_cpointer_t priv); + +/*! suspend the current coroutine + * + * @param priv the user private data as the return value of resume() + * + * @return the user private data from resume(priv) + */ +tb_pointer_t tb_coroutine_suspend(tb_cpointer_t priv); + +/*! sleep some times (ms) + * + * @param interval the interval (ms), infinity: -1 + * + * @return the user private data from resume(priv) + */ +tb_pointer_t tb_coroutine_sleep(tb_long_t interval); + +/*! wait io events + * + * @param sock the socket + * @param events the waited events, will remove this socket from io scheduler if be TB_SOCKET_EVENT_NONE + * @param timeout the timeout, infinity: -1 + * + * @return > 0: the events, 0: timeout, -1: failed + */ +tb_long_t tb_coroutine_waitio(tb_socket_ref_t sock, tb_size_t events, tb_long_t timeout); + +/*! get the current coroutine + * + * @return the current coroutine + */ +tb_coroutine_ref_t tb_coroutine_self(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/lock.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/lock.h index 1c3ab2473..533b3f9e0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/lock.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/lock.h @@ -85,3 +85,90 @@ tb_void_t tb_co_lock_leave(tb_co_lock_ref_t lock); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file lock.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_LOCK_H +#define TB_COROUTINE_LOCK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the coroutine lock ref type +typedef __tb_typeref__(co_lock); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init lock + * + * @return the lock + */ +tb_co_lock_ref_t tb_co_lock_init(tb_noarg_t); + +/*! exit lock + * + * @param lock the lock + */ +tb_void_t tb_co_lock_exit(tb_co_lock_ref_t lock); + +/*! enter lock + * + * @param lock the lock + */ +tb_void_t tb_co_lock_enter(tb_co_lock_ref_t lock); + +/*! try to enter lock + * + * @param lock the lock + * + * @return tb_true or tb_false + */ +tb_bool_t tb_co_lock_enter_try(tb_co_lock_ref_t lock); + +/*! leave lock + * + * @param lock the lock + */ +tb_void_t tb_co_lock_leave(tb_co_lock_ref_t lock); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/prefix.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/prefix.h index a937c67c0..5e4f9b44d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_COROUTINE_PREFIX_H +#define TB_COROUTINE_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/scheduler.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/scheduler.h index 78ebdcb8c..290b93b90 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/scheduler.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/scheduler.h @@ -85,3 +85,90 @@ tb_co_scheduler_ref_t tb_co_scheduler_self(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file scheduler.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_SCHEDULER_H +#define TB_COROUTINE_SCHEDULER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the coroutine scheduler ref type +typedef __tb_typeref__(co_scheduler); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init scheduler + * + * @return the scheduler + */ +tb_co_scheduler_ref_t tb_co_scheduler_init(tb_noarg_t); + +/*! exit scheduler + * + * @param scheduler the scheduler + */ +tb_void_t tb_co_scheduler_exit(tb_co_scheduler_ref_t scheduler); + +/* kill the scheduler + * + * @param scheduler the scheduler + */ +tb_void_t tb_co_scheduler_kill(tb_co_scheduler_ref_t scheduler); + +/*! run the scheduler loop + * + * @param scheduler the scheduler + * @param exclusive enable exclusive mode, we need ensure only one loop() be called at the same time, + * but it will be faster using thr global scheduler instead of TLS storage + */ +tb_void_t tb_co_scheduler_loop(tb_co_scheduler_ref_t schedule, tb_bool_t exclusive); + +/*! get the scheduler of the current coroutine + * + * @return the scheduler + */ +tb_co_scheduler_ref_t tb_co_scheduler_self(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/semaphore.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/semaphore.h index a9eede0ac..b00eb3f2f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/semaphore.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/semaphore.h @@ -93,3 +93,98 @@ tb_long_t tb_co_semaphore_wait(tb_co_semaphore_ref_t semaphore, tb __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file semaphore.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_SEMAPHORE_H +#define TB_COROUTINE_SEMAPHORE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the coroutine semaphore ref type +typedef __tb_typeref__(co_semaphore); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init semaphore + * + * @param value the initial semaphore value + * + * @return the semaphore + */ +tb_co_semaphore_ref_t tb_co_semaphore_init(tb_size_t value); + +/*! exit semaphore + * + * @return the semaphore + */ +tb_void_t tb_co_semaphore_exit(tb_co_semaphore_ref_t semaphore); + +/*! post semaphore + * + * @param semaphore the semaphore + * @param post the post semaphore value + * + * @return tb_true or tb_false + */ +tb_void_t tb_co_semaphore_post(tb_co_semaphore_ref_t semaphore, tb_size_t post); + +/*! the semaphore value + * + * @param semaphore the semaphore + * + * @return the semaphore value + */ +tb_size_t tb_co_semaphore_value(tb_co_semaphore_ref_t semaphore); + +/*! wait semaphore + * + * @param semaphore the semaphore + * @param timeout the timeout + * + * @return ok: 1, timeout: 0, fail: -1 + */ +tb_long_t tb_co_semaphore_wait(tb_co_semaphore_ref_t semaphore, tb_long_t timeout); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/core.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/core.h index 1322ed11a..0bfb4b3ac 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/core.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/core.h @@ -169,3 +169,174 @@ typedef struct __tb_lo_core_t #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file core.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_STACKLESS_CORE_H +#define TB_COROUTINE_STACKLESS_CORE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/* + * Copyright (c) 2004-2005, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +// get core from coroutine +#define tb_lo_core(co) ((tb_lo_core_ref_t)(co)) + +// get core state +#define tb_lo_core_state(co) (tb_lo_core(co)->state) + +// set core state +#define tb_lo_core_state_set(co, val) tb_lo_core(co)->state = (val) + +#ifdef TB_COMPILER_IS_GCC +/* + * Implementation of local continuations based on the "Labels as + * values" feature of gcc + * + * @author Adam Dunkels <[email protected]> + * + * This implementation of local continuations is based on a special + * feature of the GCC C compiler called "labels as values". This + * feature allows assigning pointers with the address of the code + * corresponding to a particular C label. + * + * For more information, see the GCC documentation: + * http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html + */ +# define tb_lo_core_init(co) tb_lo_core(co)->branch = tb_null; tb_lo_core(co)->state = TB_STATE_READY +# define tb_lo_core_resume(co) \ + if (tb_lo_core(co)->branch) \ + { \ + goto *(tb_lo_core(co)->branch); \ + } \ + else + +# define tb_lo_core_record(co) \ + do \ + { \ + __tb_mconcat_ex__(__tb_lo_core_label, __tb_line__): \ + tb_lo_core(co)->branch = &&__tb_mconcat_ex__(__tb_lo_core_label, __tb_line__); \ + \ + } while(0) + +# define tb_lo_core_exit(co) tb_lo_core(co)->branch = tb_null, tb_lo_core(co)->state = TB_STATE_END + +#else + +/* + * Implementation of local continuations based on switch() statment + * + * @author Adam Dunkels <[email protected]> + * + * This implementation of local continuations uses the C switch() + * statement to resume execution of a function somewhere inside the + * function's body. The implementation is based on the fact that + * switch() statements are able to jump directly into the bodies of + * control structures such as if() or while() statmenets. + * + * This implementation borrows heavily from Simon Tatham's coroutines + * implementation in C: + * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html + * + * + * + * WARNING! the implementation using switch() does not work if an + * core_set() is done within another switch() statement! + */ +# define tb_lo_core_init(co) tb_lo_core(co)->branch = 0; tb_lo_core(co)->state = TB_STATE_READY +# define tb_lo_core_resume(co) switch (tb_lo_core(co)->branch) case 0: +# define tb_lo_core_record_(co, label) tb_lo_core(co)->branch = (tb_uint16_t)label; case label: +# define tb_lo_core_exit(co) tb_lo_core(co)->branch = 0, tb_lo_core(co)->state = TB_STATE_END +# ifdef TB_COMPILER_IS_MSVC +# define tb_lo_core_record(co) tb_lo_core_record_(co, __COUNTER__ + 1) +# else +# define tb_lo_core_record(co) tb_lo_core_record_(co, __tb_line__) +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the core branch type +#ifdef TB_COMPILER_IS_GCC +typedef tb_pointer_t tb_lo_core_branch_t; +#else +typedef tb_uint16_t tb_lo_core_branch_t; +#endif + +// the stackless coroutine core type +typedef struct __tb_lo_core_t +{ + // the code branch + tb_lo_core_branch_t branch; + + /* the state + * + * TB_STATE_READY + * TB_STATE_SUSPEND + * TB_STATE_END + */ + tb_uint8_t state; + +}tb_lo_core_t, *tb_lo_core_ref_t; + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/coroutine.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/coroutine.h index cc71cb96d..eecfd8e6c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/coroutine.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/coroutine.h @@ -411,3 +411,416 @@ tb_void_t tb_lo_coroutine_resume(tb_lo_coroutine_ref_t coroutine); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file coroutine.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_STACKLESS_COROUTINE_H +#define TB_COROUTINE_STACKLESS_COROUTINE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "core.h" +#include "scheduler.h" +#include "../../libc/libc.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the self coroutine +#define tb_lo_coroutine_self() (co__) + +/* enter coroutine + * + * @code + * + // before + tb_lo_coroutine_enter(co) + { + for (i = 0; i < 100; i++) + { + tb_lo_coroutine_yield(); + } + } + + // after expanding again (init: branch = 0, state = TB_STATE_READY) + tb_lo_coroutine_ref_t co__ = co; + tb_int_t lo_yield_flag__ = 1; + for (; lo_yield_flag__; tb_lo_core(co__)->branch = 0, tb_lo_core(co__)->state = TB_STATE_END, lo_yield_flag__ = 0) + switch (tb_lo_core(co__)->branch) + case 0: + { + for (i = 0; i < 100; i++) + { + lo_yield_flag__ = 0; + tb_lo_core(co__)->branch = __tb_line__; case __tb_line__:; + if (lo_yield_flag__ == 0) + return ; + } + } + + // or .. + + // after expanding again for gcc label (init: branch = tb_null, state = TB_STATE_READY) + tb_lo_coroutine_ref_t co__ = co; + tb_int_t lo_yield_flag__ = 1; + for (; lo_yield_flag__; tb_lo_core(co__)->branch = tb_null, tb_lo_core(co__)->state = TB_STATE_END, lo_yield_flag__ = 0) + if (tb_lo_core(co)->branch) + { + goto *(tb_lo_core(co)->branch); + } + else + { + for (i = 0; i < 100; i++) + { + lo_yield_flag__ = 0; + do + { + __tb_mconcat_ex__(__tb_lo_core_label, __tb_line__): + tb_lo_core(co)->branch = &&__tb_mconcat_ex__(__tb_lo_core_label, __tb_line__); + + } while(0) + + if (lo_yield_flag__ == 0) + return ; + } + } + + * @endcode + */ +#define tb_lo_coroutine_enter(co) \ + tb_lo_coroutine_ref_t co__ = (co); \ + tb_int_t lo_yield_flag__ = 1; \ + for ( ; lo_yield_flag__; tb_lo_core_exit(tb_lo_coroutine_self()), lo_yield_flag__ = 0) \ + tb_lo_core_resume(tb_lo_coroutine_self()) + +/// yield coroutine +#define tb_lo_coroutine_yield() \ +do \ +{ \ + lo_yield_flag__ = 0; \ + tb_lo_core_record(co__); \ + if (lo_yield_flag__ == 0) \ + return ; \ + \ +} while(0) + +/*! suspend current coroutine + * + * the scheduler will move this coroutine to the suspended coroutines after the function be returned + * + * @code + * + // before + tb_lo_coroutine_enter(co) + { + for (i = 0; i < 100; i++) + { + tb_lo_coroutine_yield(); + tb_lo_coroutine_suspend(); + } + } + + // after expanding again (init: branch = 0, state = TB_STATE_READY) + tb_lo_coroutine_ref_t co__ = co; + tb_int_t lo_yield_flag__ = 1; + for (; lo_yield_flag__; tb_lo_core(co__)->branch = 0, tb_lo_core(co__)->state = TB_STATE_END, lo_yield_flag__ = 0) + switch (tb_lo_core(co__)->branch) + case 0: + { + for (i = 0; i < 100; i++) + { + lo_yield_flag__ = 0; + tb_lo_core(co__)->branch = __tb_line__; case __tb_line__:; + if (lo_yield_flag__ == 0) + return ; + + // suspend coroutine + tb_lo_core(co__)->state = TB_STATE_SUSPEND; + tb_lo_core(co__)->branch = __tb_line__; case __tb_line__:; + if (tb_lo_core(co__)->state == TB_STATE_SUSPEND) + return ; + } + } + * @endcode + */ +#define tb_lo_coroutine_suspend() \ +do \ +{ \ + tb_used(&lo_yield_flag__); \ + tb_lo_core_state_set(tb_lo_coroutine_self(), TB_STATE_SUSPEND); \ + tb_lo_core_record(tb_lo_coroutine_self()); \ + if (tb_lo_core_state(tb_lo_coroutine_self()) == TB_STATE_SUSPEND) \ + return ; \ + \ +} while(0) + +/// sleep some time +#define tb_lo_coroutine_sleep(interval) \ +do \ +{ \ + if (interval) \ + { \ + tb_lo_coroutine_sleep_(tb_lo_coroutine_self(), interval); \ + tb_lo_coroutine_suspend(); \ + } \ + \ +} while(0) + +/// wait io socket events +#define tb_lo_coroutine_waitio(sock, events, interval) \ +do \ +{ \ + if (tb_lo_coroutine_waitio_(tb_lo_coroutine_self(), sock, events, interval)) \ + { \ + tb_lo_coroutine_suspend(); \ + } \ + \ +} while(0) + +/// wait until coroutine be true +#define tb_lo_coroutine_wait_until(cond) \ +do \ +{ \ + tb_used(&lo_yield_flag__); \ + tb_lo_core_record(tb_lo_coroutine_self()); \ + if (!(cond)) \ + return ; \ + \ +} while(0) + +/// wait while coroutine be true +#define tb_lo_coroutine_wait_while(pt, cond) tb_lo_coroutine_wait_until(!(cond)) + +/// get socket events after waiting +#define tb_lo_coroutine_events() tb_lo_coroutine_events_(tb_lo_coroutine_self()) + +/*! pass the user private data + * + * @code + + // start coroutine + tb_lo_coroutine_start(scheduler, coroutine_func, tb_lo_coroutine_pass(tb_xxxx_priv_t)); + + * @endcode + * + * => + * + * @code + + // start coroutine + tb_lo_coroutine_start(scheduler, coroutine_func, tb_malloc0_type(tb_xxxx_priv_t), tb_lo_coroutine_pass_free_); + + * @endcode + */ +#define tb_lo_coroutine_pass(type) tb_malloc0_type(type), tb_lo_coroutine_pass_free_ + +/*! pass the user private data and init one member + * + * @code + + typedef struct __tb_xxxx_priv_t + { + tb_size_t member; + tb_size_t others; + + }tb_xxxx_priv_t; + + // start coroutine + tb_lo_coroutine_start(scheduler, coroutine_func, tb_lo_coroutine_pass1(tb_xxxx_priv_t, member, value)); + + * @endcode + * + * => + * + * @code + + tb_xxxx_priv_t* priv = tb_malloc0_type(tb_xxxx_priv_t); + if (priv) + { + priv->member = value; + } + + // start coroutine + tb_lo_coroutine_start(scheduler, coroutine_func, priv, tb_lo_coroutine_pass_free_); + + * @endcode + */ +#define tb_lo_coroutine_pass1(type, member, value) tb_lo_coroutine_pass1_make_(sizeof(type), &(value), tb_offsetof(type, member), tb_memsizeof(type, member)), tb_lo_coroutine_pass_free_ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private interfaces + */ + +/* get the scheduler of coroutine + * + * @param coroutine the coroutine + * + * @return the scheduler + */ +tb_lo_scheduler_ref_t tb_lo_coroutine_scheduler_(tb_lo_coroutine_ref_t coroutine); + +/* sleep the current coroutine + * + * @param coroutine the coroutine + * @param interval the interval (ms), infinity: -1 + */ +tb_void_t tb_lo_coroutine_sleep_(tb_lo_coroutine_ref_t coroutine, tb_long_t interval); + +/* wait io events + * + * @param coroutine the coroutine + * @param sock the socket + * @param events the waited events + * @param timeout the timeout, infinity: -1 + * + * @return suspend coroutine if be tb_true + */ +tb_bool_t tb_lo_coroutine_waitio_(tb_lo_coroutine_ref_t coroutine, tb_socket_ref_t sock, tb_size_t events, tb_long_t timeout); + +/* get the events after waiting socket + * + * @param coroutine the coroutine + * + * @return events: > 0, failed: -1, timeout: 0 + */ +tb_long_t tb_lo_coroutine_events_(tb_lo_coroutine_ref_t coroutine); + +/* free the user private data for pass() + * + * @note only be a wrapper of free() for tb_lo_coroutine_pass() + * + * @param priv the user private data + */ +tb_void_t tb_lo_coroutine_pass_free_(tb_cpointer_t priv); + +/* make the user private data for pass1() + * + * @param type_size the data type size + * @param value the value pointer + * @param offset the member offset + * @param size the value size + * + * @return the user private data + */ +tb_pointer_t tb_lo_coroutine_pass1_make_(tb_size_t type_size, tb_cpointer_t value, tb_size_t offset, tb_size_t size); + +/* make the user private data for pass2() + * + * @param type_size the data type size + * @param value1 the value1 pointer + * @param offset1 the member1 offset + * @param size1 the value1 size + * @param value2 the value2 pointer + * @param offset2 the member2 offset + * @param size2 the value2 size + * + * @return the user private data + */ +tb_pointer_t tb_lo_coroutine_pass2_make_(tb_size_t type_size, tb_cpointer_t value1, tb_size_t offset1, tb_size_t size1, tb_cpointer_t value2, tb_size_t offset2, tb_size_t size2); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! start coroutine + * + * @code + static tb_void_t switchtask(tb_lo_coroutine_ref_t coroutine, tb_cpointer_t priv) + { + // get count pointer (@note only allow non-status local variables) + tb_size_t* count = (tb_size_t*)priv; + + // enter coroutine + tb_lo_coroutine_enter(coroutine); + + // @note can not define local variables here + // ... + + // loop + while ((*count)--) + { + // yield + tb_lo_coroutine_yield(); + } + + // leave coroutine + tb_lo_coroutine_leave(); + } + + tb_int_t main (tb_int_t argc, tb_char_t** argv) + { + // init tbox + if (!tb_init(tb_null, tb_null)) return -1; + + // init scheduler + tb_lo_scheduler_ref_t scheduler = tb_lo_scheduler_init(); + if (scheduler) + { + // start coroutine + tb_size_t counts[] = {100, 100}; + tb_lo_coroutine_start(scheduler, switchtask, &counts[0], tb_null); + tb_lo_coroutine_start(scheduler, switchtask, &counts[1], tb_null); + + // run scheduler + tb_lo_scheduler_loop(scheduler); + + // exit scheduler + tb_lo_scheduler_exit(scheduler); + } + + // exit tbox + tb_exit(); + } + + * @endcode + * + * @param scheduler the scheduler (can not be null, we can get scheduler of the current coroutine from tb_lo_scheduler_self()) + * @param func the coroutine function + * @param priv the passed user private data as the argument of function + * @param free the user private free function + * + * @return tb_true or tb_false + */ +tb_bool_t tb_lo_coroutine_start(tb_lo_scheduler_ref_t scheduler, tb_lo_coroutine_func_t func, tb_cpointer_t priv, tb_lo_coroutine_free_t free); + +/*! resume the given coroutine + * + * @param coroutine the coroutine + */ +tb_void_t tb_lo_coroutine_resume(tb_lo_coroutine_ref_t coroutine); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/lock.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/lock.h index a5b82f3f7..036ab3329 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/lock.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/lock.h @@ -79,3 +79,84 @@ typedef tb_lo_semaphore_t tb_lo_lock_t; typedef tb_lo_semaphore_ref_t tb_lo_lock_ref_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file lock.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_STACKLESS_LOCK_H +#define TB_COROUTINE_STACKLESS_LOCK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "semaphore.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! init lock + * + * @param lock the lock pointer + */ +#define tb_lo_lock_init(lock) tb_lo_semaphore_init(lock, 1) + +/*! exit lock + * + * @param lock the lock pointer + */ +#define tb_lo_lock_exit(lock) tb_lo_semaphore_exit(lock) + +/*! enter lock + * + * @param lock the lock pointer + */ +#define tb_lo_lock_enter(lock) tb_lo_semaphore_wait(lock) + +/*! try to enter lock + * + * @param lock the lock pointer + * + * @return tb_true or tb_false + */ +#define tb_lo_lock_enter_try(lock) tb_lo_semaphore_wait_try(lock) + +/*! leave lock + * + * @param lock the lock pointer + */ +#define tb_lo_lock_leave(lock) tb_lo_semaphore_post(lock, 1) + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the stackless lock type +typedef tb_lo_semaphore_t tb_lo_lock_t; + +/// the stackless lock ref type +typedef tb_lo_semaphore_ref_t tb_lo_lock_ref_t; + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/prefix.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/prefix.h index 1abfd678f..febf72239 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/prefix.h @@ -55,3 +55,60 @@ typedef tb_void_t (*tb_lo_coroutine_free_t)(tb_cpointer_t priv); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_COROUTINE_STACKLESS_PREFIX_H +#define TB_COROUTINE_STACKLESS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the stackless coroutine ref type +typedef __tb_typeref__(lo_coroutine); + +/// the stackless scheduler ref type +typedef __tb_typeref__(lo_scheduler); + +/*! the coroutine function type + * + * @param coroutine the coroutine self + * @param priv the user private data from start(.., priv) + */ +typedef tb_void_t (*tb_lo_coroutine_func_t)(tb_lo_coroutine_ref_t coroutine, tb_cpointer_t priv); + +/*! the user private data free function type + * + * @param priv the user private data from start(.., priv) + */ +typedef tb_void_t (*tb_lo_coroutine_free_t)(tb_cpointer_t priv); + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/scheduler.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/scheduler.h index a3d19c15d..0bfe4a2ec 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/scheduler.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/scheduler.h @@ -79,3 +79,84 @@ tb_void_t tb_lo_scheduler_loop(tb_lo_scheduler_ref_t scheduler, tb __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file scheduler.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_STACKLESS_SCHEDULER_H +#define TB_COROUTINE_STACKLESS_SCHEDULER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the self scheduler +#define tb_lo_scheduler_self() tb_lo_coroutine_scheduler_(co__) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init scheduler + * + * @return the scheduler + */ +tb_lo_scheduler_ref_t tb_lo_scheduler_init(tb_noarg_t); + +/*! exit scheduler + * + * @param scheduler the scheduler + */ +tb_void_t tb_lo_scheduler_exit(tb_lo_scheduler_ref_t scheduler); + +/* kill the scheduler + * + * @param scheduler the scheduler + */ +tb_void_t tb_lo_scheduler_kill(tb_lo_scheduler_ref_t scheduler); + +/*! run the scheduler loop + * + * @param scheduler the scheduler + * @param exclusive enable exclusive mode, we need ensure only one loop() be called at the same time, + * but it will be faster using thr global scheduler instead of TLS storage + */ +tb_void_t tb_lo_scheduler_loop(tb_lo_scheduler_ref_t scheduler, tb_bool_t exclusive); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/semaphore.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/semaphore.h index 495883b1b..54ef27975 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/semaphore.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/semaphore.h @@ -102,3 +102,107 @@ typedef struct __tb_lo_semaphore_t }tb_lo_semaphore_t, *tb_lo_semaphore_ref_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file semaphore.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_STACKLESS_SEMAPHORE_H +#define TB_COROUTINE_STACKLESS_SEMAPHORE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "coroutine.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! init semaphore + * + * @param sem the semaphore pointer + * @param val the initial semaphore value + */ +#define tb_lo_semaphore_init(sem, val) (sem)->value = val + +/*! exit semaphore + * + * @param sem the semaphore pointer + */ +#define tb_lo_semaphore_exit(sem) (sem)->value = 0 + +/*! get semaphore value + * + * @param sem the semaphore pointer + * + * @return the semaphore value + */ +#define tb_lo_semaphore_value(sem) ((sem)->value) + +/*! post semaphore + * + * @param sem the semaphore pointer + * @param post the post semaphore value + */ +#define tb_lo_semaphore_post(sem, post) \ +do \ +{ \ + (sem)->value += (post); \ + tb_lo_coroutine_yield(); \ + \ +} while (0) + +/*! wait semaphore + * + * @param sem the semaphore pointer + */ +#define tb_lo_semaphore_wait(sem) \ +do \ +{ \ + tb_lo_coroutine_wait_until((sem)->value > 0); \ + (sem)->value--; \ + \ +} while(0) + +/*! try to wait semaphore + * + * @param sem the semaphore pointer + * + * @return tb_true or tb_false + */ +#define tb_lo_semaphore_wait_try(sem) (((sem)->value > 0)? (sem)->value-- : 0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the stackless semaphore type +typedef struct __tb_lo_semaphore_t +{ + // the semaphore value + tb_long_t value; + +}tb_lo_semaphore_t, *tb_lo_semaphore_ref_t; + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/stackless.h b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/stackless.h index 9b0b5d916..020b06a7c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/stackless.h +++ b/core/pkg/tbox.pkg/inc/tbox/coroutine/stackless/stackless.h @@ -36,3 +36,41 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file stackless.h + * @ingroup coroutine + * + */ +#ifndef TB_COROUTINE_STACKLESS_H +#define TB_COROUTINE_STACKLESS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "coroutine.h" +#include "scheduler.h" +#include "semaphore.h" +#include "lock.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/database/database.h b/core/pkg/tbox.pkg/inc/tbox/database/database.h index 1546e107b..be66d4e87 100644 --- a/core/pkg/tbox.pkg/inc/tbox/database/database.h +++ b/core/pkg/tbox.pkg/inc/tbox/database/database.h @@ -34,3 +34,39 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file database.h + * @defgroup database + */ +#ifndef TB_DATABASE_H +#define TB_DATABASE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "sql.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/database/prefix.h b/core/pkg/tbox.pkg/inc/tbox/database/prefix.h index 6bc82299d..af1119c67 100644 --- a/core/pkg/tbox.pkg/inc/tbox/database/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/database/prefix.h @@ -36,3 +36,41 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup database + * + */ +#ifndef TB_DATABASE_PREFIX_H +#define TB_DATABASE_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../libc/libc.h" +#include "../network/url.h" +#include "../container/iterator.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/database/sql.h b/core/pkg/tbox.pkg/inc/tbox/database/sql.h index 62e06facd..448538087 100644 --- a/core/pkg/tbox.pkg/inc/tbox/database/sql.h +++ b/core/pkg/tbox.pkg/inc/tbox/database/sql.h @@ -337,3 +337,342 @@ tb_bool_t tb_database_sql_statement_bind(tb_database_s __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file sql.h + * @ingroup database + */ +#ifndef TB_DATABASE_SQL_H +#define TB_DATABASE_SQL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "value.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the sql database type enum +typedef enum __tb_database_sql_type_e +{ + TB_DATABASE_SQL_TYPE_NONE = 0 +, TB_DATABASE_SQL_TYPE_MYSQL = 1 +, TB_DATABASE_SQL_TYPE_SQLITE3 = 2 + +}tb_database_sql_type_e; + +/// the database sql ref type +typedef __tb_typeref__(database_sql); + +/// the database sql statement ref type +typedef __tb_typeref__(database_sql_statement); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init sql database + * + * @param url the database url + * "sql://localhost/?type=mysql&username=xxxx&password=xxxx" + * "sql://localhost:3306/?type=mysql&username=xxxx&password=xxxx&database=xxxx" + * "sql:///home/file.sqlitedb?type=sqlite3" + * "/home/file.sqlite3" + * "file:///home/file.sqlitedb" + * "C://home/file.sqlite3" + * + * @return the database + */ +tb_database_sql_ref_t tb_database_sql_init(tb_char_t const* url); + +/*! exit database + * + * @param database the database handle + */ +tb_void_t tb_database_sql_exit(tb_database_sql_ref_t database); + +/*! the database type + * + * @param database the database handle + * + * @return the database type + */ +tb_size_t tb_database_sql_type(tb_database_sql_ref_t database); + +/*! open database + * + * @code + tb_database_sql_ref_t database = tb_database_sql_init("sql://localhost/?type=mysql&username=xxxx&password=xxxx"); + if (database) + { + // open it + if (tb_database_sql_open(database)) + { + // done it + // ... + + // close it + tb_database_sql_clos(database); + } + tb_database_sql_exit(database); + } + * @endcode + * + * @param database the database handle + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_open(tb_database_sql_ref_t database); + +/*! clos database + * + * @param database the database handle + */ +tb_void_t tb_database_sql_clos(tb_database_sql_ref_t database); + +/*! begin transaction + * + * @param database the database handle + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_begin(tb_database_sql_ref_t database); + +/*! commit transaction + * + * @param database the database handle + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_commit(tb_database_sql_ref_t database); + +/*! rollback transaction + * + * @param database the database handle + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_rollback(tb_database_sql_ref_t database); + +/*! the database state + * + * @param database the database handle + * + * @return the database state + */ +tb_size_t tb_database_sql_state(tb_database_sql_ref_t database); + +/*! done database + * + * @code + * + * // done sql + * if (!tb_database_sql_done(database, "select * from table")) + * { + * // trace + * tb_trace_e("done sql failed, error: %s", tb_state_cstr(tb_database_sql_state(database))); + * return ; + * } + * + * // load result + * // .. + * + * @endcode + * + * @param database the database handle + * @param sql the sql command + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_done(tb_database_sql_ref_t database, tb_char_t const* sql); + +/*! load the database result + * + * @code + * + // done sql + // .. + + // load result + tb_iterator_ref_t result = tb_database_sql_result_load(database, tb_true); + if (result) + { + // walk result + tb_for_all_if (tb_iterator_ref_t, row, result, row) + { + // walk values + tb_for_all_if (tb_database_sql_value_t*, value, row, value) + { + tb_trace_i("name: %s, data: %s, at: %lux%lu", tb_database_sql_value_name(value), tb_database_sql_value_text(value), row_itor, item_itor); + } + } + + // exit result + tb_database_sql_result_exit(result); + } + + // load result + tb_iterator_ref_t result = tb_database_sql_result_load(database, tb_false); + if (result) + { + // walk result + tb_for_all_if (tb_iterator_ref_t, row, result, row) + { + // field count + tb_trace_i("count: %lu", tb_iterator_size(row)); + + // id + tb_database_sql_value_t const* id = tb_iterator_item(row, 0); + if (id) + { + tb_trace_i("id: %d", tb_database_sql_value_int32(id)); + } + + // name + tb_database_sql_value_t const* name = tb_iterator_item(row, 1); + if (name) + { + tb_trace_i("name: %s", tb_database_sql_value_text(name)); + } + + // blob + tb_database_sql_value_t const* blob = tb_iterator_item(row, 2); + if (blob) + { + // data? + tb_stream_ref_t stream = tb_null; + if (tb_database_sql_value_blob(blob)) + { + // trace + tb_trace_i("[data: %p, size: %lu] ", tb_database_sql_value_blob(blob), tb_database_sql_value_size(blob)); + } + // stream? + else if ((stream = tb_database_sql_value_blob_stream(blob))) + { + // trace + tb_trace_i("[stream: %p, size: %lld] ", stream, tb_stream_size(stream)); + + // read stream + // ... + } + // null? + else + { + // trace + tb_trace_i("[%s:null] ", tb_database_sql_value_name(blob)); + } + } + + } + + // exit result + tb_database_sql_result_exit(result); + } + + * @endcode + * + * @param database the database handle + * @param try_all try loading all result into memory + * + * @return the database result + */ +tb_iterator_ref_t tb_database_sql_result_load(tb_database_sql_ref_t database, tb_bool_t try_all); + +/*! exit the database result + * + * @param database the database handle + * @param result the database result + */ +tb_void_t tb_database_sql_result_exit(tb_database_sql_ref_t database, tb_iterator_ref_t result); + +/*! init the database statement + * + * @param database the database handle + * @param sql the sql command + * + * @return the statement handle + */ +tb_database_sql_statement_ref_t tb_database_sql_statement_init(tb_database_sql_ref_t database, tb_char_t const* sql); + +/*! exit the database statement + * + * @param database the database handle + * @param statement the statement handle + */ +tb_void_t tb_database_sql_statement_exit(tb_database_sql_ref_t database, tb_database_sql_statement_ref_t statement); + +/*! done the database statement + * + * @code + tb_database_sql_statement_ref_t statement = tb_database_sql_statement_init(database, "select * from table where id=?"); + if (statement) + { + // bind arguments + tb_database_sql_value_t list[1] = {0}; + tb_database_sql_value_set_int32(&list[0], 12345); + if (tb_database_sql_statement_bind(database, statement, list, tb_arrayn(list))) + { + // done statement + if (tb_database_sql_statement_done(database, statement)) + { + // load result + // ... + } + } + + // exit statement + tb_database_sql_statement_exit(database, statement); + } + * @endcode + * + * @param database the database handle + * @param statement the statement handle + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_statement_done(tb_database_sql_ref_t database, tb_database_sql_statement_ref_t statement); + +/*! bind the database statement argument + * + * @param database the database handle + * @param statement the statement handle + * @param list the argument value list + * @param size the argument value count + * + * @return tb_true or tb_false + */ +tb_bool_t tb_database_sql_statement_bind(tb_database_sql_ref_t database, tb_database_sql_statement_ref_t statement, tb_database_sql_value_t const* list, tb_size_t size); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/database/value.h b/core/pkg/tbox.pkg/inc/tbox/database/value.h index 9c41e65ad..7b5368831 100644 --- a/core/pkg/tbox.pkg/inc/tbox/database/value.h +++ b/core/pkg/tbox.pkg/inc/tbox/database/value.h @@ -461,3 +461,466 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file value.h + * @ingroup database + * + */ +#ifndef TB_DATABASE_SQL_VALUE_H +#define TB_DATABASE_SQL_VALUE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../stream/stream.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the sql database value type enum +typedef enum __tb_database_sql_value_type_e +{ + TB_DATABASE_SQL_VALUE_TYPE_NULL = 0 +, TB_DATABASE_SQL_VALUE_TYPE_INT8 = 1 +, TB_DATABASE_SQL_VALUE_TYPE_INT16 = 2 +, TB_DATABASE_SQL_VALUE_TYPE_INT32 = 3 +, TB_DATABASE_SQL_VALUE_TYPE_INT64 = 4 +, TB_DATABASE_SQL_VALUE_TYPE_UINT8 = 5 +, TB_DATABASE_SQL_VALUE_TYPE_UINT16 = 6 +, TB_DATABASE_SQL_VALUE_TYPE_UINT32 = 7 +, TB_DATABASE_SQL_VALUE_TYPE_UINT64 = 8 +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +, TB_DATABASE_SQL_VALUE_TYPE_FLOAT = 13 +, TB_DATABASE_SQL_VALUE_TYPE_DOUBLE = 14 +#endif +, TB_DATABASE_SQL_VALUE_TYPE_BLOB8 = 9 +, TB_DATABASE_SQL_VALUE_TYPE_BLOB16 = 10 +, TB_DATABASE_SQL_VALUE_TYPE_BLOB32 = 11 +, TB_DATABASE_SQL_VALUE_TYPE_TEXT = 12 + +}tb_database_sql_value_type_e; + +/// the sql database value type +typedef struct __tb_database_sql_value_t +{ + /// the type + tb_size_t type; + + /// the name + tb_char_t const* name; + + /// the data + union + { + // int + tb_int8_t i8; + tb_int16_t i16; + tb_int32_t i32; + tb_int64_t i64; + + // uint + tb_uint8_t u8; + tb_uint16_t u16; + tb_uint32_t u32; + tb_uint64_t u64; + + // float +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + tb_float_t f; + tb_double_t d; +#endif + + // blob + struct + { + tb_byte_t const* data; + tb_size_t size; + + // the stream for blob32 + tb_stream_ref_t stream; + + } blob; + + // text + struct + { + tb_char_t const* data; + tb_size_t hint; + + } text; + + }u; + +}tb_database_sql_value_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the value data size + * + * @param value the value + * + * @return the value data size + */ +tb_size_t tb_database_sql_value_size(tb_database_sql_value_t const* value); + +/*! the int8 value + * + * @param value the value + * + * @return the int8 value + */ +tb_int8_t tb_database_sql_value_int8(tb_database_sql_value_t const* value); + +/*! the int16 value + * + * @param value the value + * + * @return the int16 value + */ +tb_int16_t tb_database_sql_value_int16(tb_database_sql_value_t const* value); + +/*! the int32 value + * + * @param value the value + * + * @return the int32 value + */ +tb_int32_t tb_database_sql_value_int32(tb_database_sql_value_t const* value); + +/*! the int64 value + * + * @param value the value + * + * @return the int64 value + */ +tb_int64_t tb_database_sql_value_int64(tb_database_sql_value_t const* value); + +/*! the uint8 value + * + * @param value the value + * + * @return the uint8 value + */ +tb_uint8_t tb_database_sql_value_uint8(tb_database_sql_value_t const* value); + +/*! the uint16 value + * + * @param value the value + * + * @return the uint16 value + */ +tb_uint16_t tb_database_sql_value_uint16(tb_database_sql_value_t const* value); + +/*! the uint32 value + * + * @param value the value + * + * @return the uint32 value + */ +tb_uint32_t tb_database_sql_value_uint32(tb_database_sql_value_t const* value); + +/*! the uint64 value + * + * @param value the value + * + * @return the uint64 value + */ +tb_uint64_t tb_database_sql_value_uint64(tb_database_sql_value_t const* value); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! the float value + * + * @param value the value + * + * @return the float value + */ +tb_float_t tb_database_sql_value_float(tb_database_sql_value_t const* value); + +/*! the double value + * + * @param value the value + * + * @return the double value + */ +tb_double_t tb_database_sql_value_double(tb_database_sql_value_t const* value); +#endif + +/*! set the null value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_null(tb_database_sql_value_t* value); + +/*! set the int8 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_int8(tb_database_sql_value_t* value, tb_int8_t number); + +/*! set the int16 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_int16(tb_database_sql_value_t* value, tb_int16_t number); + +/*! set the int32 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_int32(tb_database_sql_value_t* value, tb_int32_t number); + +/*! set the int64 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_int64(tb_database_sql_value_t* value, tb_int64_t number); + +/*! set the uint8 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_uint8(tb_database_sql_value_t* value, tb_uint8_t number); + +/*! set the uint16 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_uint16(tb_database_sql_value_t* value, tb_uint16_t number); + +/*! set the uint32 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_uint32(tb_database_sql_value_t* value, tb_uint32_t number); + +/*! set the uint64 value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_uint64(tb_database_sql_value_t* value, tb_uint64_t number); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! set the float value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_float(tb_database_sql_value_t* value, tb_float_t number); + +/*! set the double value + * + * @param value the value + * @param number the number + */ +tb_void_t tb_database_sql_value_set_double(tb_database_sql_value_t* value, tb_double_t number); +#endif + +/*! set the text value + * + * @param value the value + * @param text the text data + * @param hint the text size hint + */ +tb_void_t tb_database_sql_value_set_text(tb_database_sql_value_t* value, tb_char_t const* text, tb_size_t hint); + +/*! set the blob8 value + * + * @param value the value + * @param data the blob data + * @param size the blob size + */ +tb_void_t tb_database_sql_value_set_blob8(tb_database_sql_value_t* value, tb_byte_t const* data, tb_size_t size); + +/*! set the blob16 value + * + * @param value the value + * @param data the blob data + * @param size the blob size + */ +tb_void_t tb_database_sql_value_set_blob16(tb_database_sql_value_t* value, tb_byte_t const* data, tb_size_t size); + +/*! set the blob32 value + * + * @param value the value + * @param data the blob data + * @param size the blob size + * @param stream the stream, using it if data == null + */ +tb_void_t tb_database_sql_value_set_blob32(tb_database_sql_value_t* value, tb_byte_t const* data, tb_size_t size, tb_stream_ref_t stream); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +/// the value is null? +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_null(tb_database_sql_value_t const* value) +{ + return (value && value->type == TB_DATABASE_SQL_VALUE_TYPE_NULL)? tb_true : tb_false; +} + +/// the value is text? +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_text(tb_database_sql_value_t const* value) +{ + return (value && value->type == TB_DATABASE_SQL_VALUE_TYPE_TEXT)? tb_true : tb_false; +} + +/// the value is blob? +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_blob(tb_database_sql_value_t const* value) +{ + return ( value + && ( value->type == TB_DATABASE_SQL_VALUE_TYPE_BLOB32 + || value->type == TB_DATABASE_SQL_VALUE_TYPE_BLOB16 + || value->type == TB_DATABASE_SQL_VALUE_TYPE_BLOB8))? tb_true : tb_false; +} + +/// the value is blob32? +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_blob32(tb_database_sql_value_t const* value) +{ + return (value && value->type == TB_DATABASE_SQL_VALUE_TYPE_BLOB32)? tb_true : tb_false; +} +/// the value is integer? +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_integer(tb_database_sql_value_t const* value) +{ + return (value && value->type >= TB_DATABASE_SQL_VALUE_TYPE_INT8 && value->type <= TB_DATABASE_SQL_VALUE_TYPE_UINT64)? tb_true : tb_false; +} + +/// the value is float? +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_float(tb_database_sql_value_t const* value) +{ + return ( value + && ( value->type == TB_DATABASE_SQL_VALUE_TYPE_FLOAT + || value->type == TB_DATABASE_SQL_VALUE_TYPE_DOUBLE))? tb_true : tb_false; +} +#endif + +/// the value is number? +static __tb_inline_force__ tb_bool_t tb_database_sql_value_is_number(tb_database_sql_value_t const* value) +{ + return (value && value->type >= TB_DATABASE_SQL_VALUE_TYPE_INT8 && value->type < TB_DATABASE_SQL_VALUE_TYPE_BLOB8)? tb_true : tb_false; +} + +/// the value type +static __tb_inline_force__ tb_size_t tb_database_sql_value_type(tb_database_sql_value_t const* value) +{ + // check + tb_assert_and_check_return_val(value, TB_DATABASE_SQL_VALUE_TYPE_NULL); + + // the type + return value->type; +} + +/// the value name +static __tb_inline_force__ tb_char_t const* tb_database_sql_value_name(tb_database_sql_value_t const* value) +{ + // check + tb_assert_and_check_return_val(value, tb_null); + + // the name + return value->name; +} + +/// the value text data +static __tb_inline_force__ tb_char_t const* tb_database_sql_value_text(tb_database_sql_value_t const* value) +{ + // check + tb_assert_and_check_return_val(value, tb_null); + + // is text? + if (tb_database_sql_value_is_text(value)) + return value->u.text.data; + // is blob? + else if (tb_database_sql_value_is_blob(value)) + return (tb_char_t const*)value->u.blob.data; + // is null? + else if (tb_database_sql_value_is_null(value)) + return tb_null; + + // trace + tb_trace_e("not text value type: %lu", value->type); + return tb_null; +} + +/// the value blob data +static __tb_inline_force__ tb_byte_t const* tb_database_sql_value_blob(tb_database_sql_value_t const* value) +{ + // check + tb_assert_and_check_return_val(value, tb_null); + + // is blob? + if (tb_database_sql_value_is_blob(value)) + return value->u.blob.data; + // is text? + else if (tb_database_sql_value_is_text(value)) + return (tb_byte_t const*)value->u.text.data; + // is null? + else if (tb_database_sql_value_is_null(value)) + return tb_null; + + // trace + tb_trace_e("not blob value type: %lu", value->type); + return tb_null; +} + +/// the value blob stream +static __tb_inline_force__ tb_stream_ref_t tb_database_sql_value_blob_stream(tb_database_sql_value_t const* value) +{ + // the blob stream + return tb_database_sql_value_is_blob(value)? value->u.blob.stream : tb_null; +} + +/// set the value name +static __tb_inline_force__ tb_void_t tb_database_sql_value_name_set(tb_database_sql_value_t* value, tb_char_t const* name) +{ + // check + tb_assert_and_check_return(value); + + // set the name + value->name = name; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/adler32.h b/core/pkg/tbox.pkg/inc/tbox/hash/adler32.h index 2afb5c57c..a8ec4d625 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/adler32.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/adler32.h @@ -66,3 +66,71 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file adler32.h + * @ingroup hash + * + */ +#ifndef TB_HASH_ADLER32_H +#define TB_HASH_ADLER32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make adler32 hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the adler32 value + */ +tb_uint32_t tb_adler32_make(tb_byte_t const* data, tb_size_t size, tb_uint32_t seed); + +/*! make adler32 hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the adler32 value + */ +tb_uint32_t tb_adler32_make_from_cstr(tb_char_t const* cstr, tb_uint32_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/ap.h b/core/pkg/tbox.pkg/inc/tbox/hash/ap.h index 173014759..acfd88a0c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/ap.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/ap.h @@ -65,3 +65,70 @@ tb_size_t tb_ap_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ap.h + * @ingroup hash + * + */ +#ifndef TB_HASH_AP_H +#define TB_HASH_AP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make ap hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the ap value + */ +tb_size_t tb_ap_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make ap hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the ap value + */ +tb_size_t tb_ap_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/bkdr.h b/core/pkg/tbox.pkg/inc/tbox/hash/bkdr.h index e10724327..f32965dc8 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/bkdr.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/bkdr.h @@ -65,3 +65,70 @@ tb_size_t tb_bkdr_make_from_cstr(tb_char_t const* cstr, tb_size_t seed __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bkdr.h + * @ingroup hash + * + */ +#ifndef TB_HASH_BKDR_H +#define TB_HASH_BKDR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make bkdr hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the bkdr value + */ +tb_size_t tb_bkdr_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make bkdr hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the bkdr value + */ +tb_size_t tb_bkdr_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/blizzard.h b/core/pkg/tbox.pkg/inc/tbox/hash/blizzard.h index 429622a7f..def1c7035 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/blizzard.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/blizzard.h @@ -67,3 +67,72 @@ tb_size_t tb_blizzard_make_from_cstr(tb_char_t const* cstr, tb_size_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file blizzard.h + * @ingroup hash + * + */ +#ifndef TB_HASH_BLIZZARD_H +#define TB_HASH_BLIZZARD_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make blizzard hash + * + * Blizzard One-Way Hash algorithm from MPQ + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the blizzard value + */ +tb_size_t tb_blizzard_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make blizzard hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the blizzard value + */ +tb_size_t tb_blizzard_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/crc16.h b/core/pkg/tbox.pkg/inc/tbox/hash/crc16.h index b27a3f9ec..8e2cc2c7a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/crc16.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/crc16.h @@ -92,3 +92,97 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file crc16.h + * @ingroup hash + * + */ +#ifndef TB_HASH_CRC16_H +#define TB_HASH_CRC16_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// encode value +#define tb_crc16_make_value(mode, crc, value) tb_crc16_make(mode, crc, (tb_byte_t const*)&(value), sizeof(value)) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make crc16 (ANSI) + * + * @param data the input data + * @param size the input size + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint16_t tb_crc16_make(tb_byte_t const* data, tb_size_t size, tb_uint16_t seed); + +/*! make crc16 (ANSI) for cstr + * + * @param cstr the input cstr + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint16_t tb_crc16_make_from_cstr(tb_char_t const* cstr, tb_uint16_t seed); + +/*! make crc16 (CCITT) + * + * @param data the input data + * @param size the input size + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint16_t tb_crc16_ccitt_make(tb_byte_t const* data, tb_size_t size, tb_uint16_t seed); + +/*! make crc16 (CCITT) for cstr + * + * @param cstr the input cstr + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint16_t tb_crc16_ccitt_make_from_cstr(tb_char_t const* cstr, tb_uint16_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/crc32.h b/core/pkg/tbox.pkg/inc/tbox/hash/crc32.h index e6dec3804..00f83deb5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/crc32.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/crc32.h @@ -92,3 +92,97 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file crc32.h + * @ingroup hash + * + */ +#ifndef TB_HASH_CRC32_H +#define TB_HASH_CRC32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// encode value +#define tb_crc32_make_value(mode, crc, value) tb_crc32_make(mode, crc, (tb_byte_t const*)&(value), sizeof(value)) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make crc32 (IEEE) + * + * @param data the input data + * @param size the input size + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint32_t tb_crc32_make(tb_byte_t const* data, tb_size_t size, tb_uint32_t seed); + +/*! make crc32 (IEEE) for cstr + * + * @param cstr the input cstr + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint32_t tb_crc32_make_from_cstr(tb_char_t const* cstr, tb_uint32_t seed); + +/*! make crc32 (IEEE LE) + * + * @param data the input data + * @param size the input size + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint32_t tb_crc32_le_make(tb_byte_t const* data, tb_size_t size, tb_uint32_t seed); + +/*! make crc32 (IEEE LE) for cstr + * + * @param cstr the input cstr + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint32_t tb_crc32_le_make_from_cstr(tb_char_t const* cstr, tb_uint32_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/crc8.h b/core/pkg/tbox.pkg/inc/tbox/hash/crc8.h index fb69f6b04..f906bfdd2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/crc8.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/crc8.h @@ -73,3 +73,78 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file crc8.h + * @ingroup hash + * + */ +#ifndef TB_HASH_CRC8_H +#define TB_HASH_CRC8_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// encode value +#define tb_crc8_make_value(mode, crc, value) tb_crc8_make(mode, crc, (tb_byte_t const*)&(value), sizeof(value)) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make crc8 (ATM) + * + * @param data the input data + * @param size the input size + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint8_t tb_crc8_make(tb_byte_t const* data, tb_size_t size, tb_uint8_t seed); + +/*! make crc8 (ATM) for cstr + * + * @param cstr the input cstr + * @param seed uses this seed if be non-zero + * + * @return the crc value + */ +tb_uint8_t tb_crc8_make_from_cstr(tb_char_t const* cstr, tb_uint8_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/djb2.h b/core/pkg/tbox.pkg/inc/tbox/hash/djb2.h index 53600b95d..41a2358db 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/djb2.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/djb2.h @@ -67,3 +67,72 @@ tb_size_t tb_djb2_make_from_cstr(tb_char_t const* cstr, tb_size_t seed __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file djb2.h + * @ingroup hash + * + */ +#ifndef TB_HASH_DJB2_H +#define TB_HASH_DJB2_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make djb2 hash + * + * see http://www.cse.yorku.ca/~oz/hash.html + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the djb2 value + */ +tb_size_t tb_djb2_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make djb2 hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the djb2 value + */ +tb_size_t tb_djb2_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/fnv32.h b/core/pkg/tbox.pkg/inc/tbox/hash/fnv32.h index 859947604..d50bbdf55 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/fnv32.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/fnv32.h @@ -84,3 +84,89 @@ tb_uint32_t tb_fnv32_1a_make_from_cstr(tb_char_t const* cstr, tb_uint32_ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author alexyer, ruki + * @file fnv32.h + * @ingroup hash + * + */ +#ifndef TB_HASH_FNV32_H +#define TB_HASH_FNV32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make fnv32 hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the fnv32 value + */ +tb_uint32_t tb_fnv32_make(tb_byte_t const* data, tb_size_t size, tb_uint32_t seed); + +/*! make fnv32 hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the fnv32 value + */ +tb_uint32_t tb_fnv32_make_from_cstr(tb_char_t const* cstr, tb_uint32_t seed); + +/*! make fnv32(1a) hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the fnv32 value + */ +tb_uint32_t tb_fnv32_1a_make(tb_byte_t const* data, tb_size_t size, tb_uint32_t seed); + +/*! make fnv32(1a) hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the fnv32 value + */ +tb_uint32_t tb_fnv32_1a_make_from_cstr(tb_char_t const* cstr, tb_uint32_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/fnv64.h b/core/pkg/tbox.pkg/inc/tbox/hash/fnv64.h index 870f4dd42..2ab24fa00 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/fnv64.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/fnv64.h @@ -84,3 +84,89 @@ tb_uint64_t tb_fnv64_1a_make_from_cstr(tb_char_t const* cstr, tb_uint64_ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author alexyer, ruki + * @file fnv64.h + * @ingroup hash + * + */ +#ifndef TB_HASH_FNV64_H +#define TB_HASH_FNV64_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make fnv64 hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the fnv64 value + */ +tb_uint64_t tb_fnv64_make(tb_byte_t const* data, tb_size_t size, tb_uint64_t seed); + +/*! make fnv64 hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the fnv64 value + */ +tb_uint64_t tb_fnv64_make_from_cstr(tb_char_t const* cstr, tb_uint64_t seed); + +/*! make fnv64(1a) hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the fnv64 value + */ +tb_uint64_t tb_fnv64_1a_make(tb_byte_t const* data, tb_size_t size, tb_uint64_t seed); + +/*! make fnv64(1a) hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the fnv64 value + */ +tb_uint64_t tb_fnv64_1a_make_from_cstr(tb_char_t const* cstr, tb_uint64_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/hash.h b/core/pkg/tbox.pkg/inc/tbox/hash/hash.h index d39ccdc25..2d5613d0d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/hash.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/hash.h @@ -48,3 +48,53 @@ #include "blizzard.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file hash.h + * @defgroup hash + * + */ +#ifndef TB_HASH_H +#define TB_HASH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "ap.h" +#include "rs.h" +#include "sha.h" +#include "md5.h" +#include "uuid.h" +#include "djb2.h" +#include "sdbm.h" +#include "bkdr.h" +#include "crc8.h" +#include "crc16.h" +#include "crc32.h" +#include "fnv32.h" +#include "fnv64.h" +#include "murmur.h" +#include "adler32.h" +#include "blizzard.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/md5.h b/core/pkg/tbox.pkg/inc/tbox/hash/md5.h index 571d6f327..cdaa3d828 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/md5.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/md5.h @@ -95,3 +95,100 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file md5.h + * @ingroup hash + * + */ +#ifndef TB_HASH_MD5_H +#define TB_HASH_MD5_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// data structure for md5 (message data) computation +typedef struct __tb_md5_t +{ + tb_uint32_t i[2]; //!< number of _bits_ handled mod 2^64 + tb_uint32_t sp[4]; //!< scratch buffer + tb_byte_t ip[64]; //!< input buffer + tb_byte_t data[16]; //!< actual data after tb_md5_exit call + +}tb_md5_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init md5 + * + * @param md5 the md5 + * @param pseudo_rand the pseudo rand + */ +tb_void_t tb_md5_init(tb_md5_t* md5, tb_uint32_t pseudo_rand); + +/*! exit md5 + * + * @param md5 the md5 + * @param data the data + * @param size the size + */ +tb_void_t tb_md5_exit(tb_md5_t* md5, tb_byte_t* data, tb_size_t size); + +/*! spak md5 + * + * @param md5 the md5 + * @param data the data + * @param size the size + */ +tb_void_t tb_md5_spak(tb_md5_t* md5, tb_byte_t const* data, tb_size_t size); + +/*! make md5 + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_md5_make(tb_byte_t const* ib, tb_size_t in, tb_byte_t* ob, tb_size_t on); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/murmur.h b/core/pkg/tbox.pkg/inc/tbox/hash/murmur.h index feac3f547..f04d8518e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/murmur.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/murmur.h @@ -67,3 +67,72 @@ tb_size_t tb_murmur_make_from_cstr(tb_char_t const* cstr, tb_size_t se __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file murmur.h + * @ingroup hash + * + */ +#ifndef TB_HASH_MURMUR_H +#define TB_HASH_MURMUR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make murmur hash + * + * one-byte-at-a-time hash based on Murmur's mix + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the murmur value + */ +tb_size_t tb_murmur_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make murmur hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the murmur value + */ +tb_size_t tb_murmur_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/prefix.h b/core/pkg/tbox.pkg/inc/tbox/hash/prefix.h index a526eab89..adfebe64f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_HASH_PREFIX_H +#define TB_HASH_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../libc/libc.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/rs.h b/core/pkg/tbox.pkg/inc/tbox/hash/rs.h index a689508ec..4ad3ef6d6 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/rs.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/rs.h @@ -65,3 +65,70 @@ tb_size_t tb_rs_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file rs.h + * @ingroup hash + * + */ +#ifndef TB_HASH_RS_H +#define TB_HASH_RS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make rs hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the rs value + */ +tb_size_t tb_rs_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make rs hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the rs value + */ +tb_size_t tb_rs_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/sdbm.h b/core/pkg/tbox.pkg/inc/tbox/hash/sdbm.h index c84ac2fd8..9beaffbad 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/sdbm.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/sdbm.h @@ -65,3 +65,70 @@ tb_size_t tb_sdbm_make_from_cstr(tb_char_t const* cstr, tb_size_t seed __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file sdbm.h + * @ingroup hash + * + */ +#ifndef TB_HASH_SDBM_H +#define TB_HASH_SDBM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make sdbm hash + * + * @param data the data + * @param size the size + * @param seed uses this seed if be non-zero + * + * @return the sdbm value + */ +tb_size_t tb_sdbm_make(tb_byte_t const* data, tb_size_t size, tb_size_t seed); + +/*! make sdbm hash from c-string + * + * @param cstr the c-string + * @param seed uses this seed if be non-zero + * + * @return the sdbm value + */ +tb_size_t tb_sdbm_make_from_cstr(tb_char_t const* cstr, tb_size_t seed); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/sha.h b/core/pkg/tbox.pkg/inc/tbox/hash/sha.h index 8d2d580c1..f4e4539a1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/sha.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/sha.h @@ -105,3 +105,110 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file sha.h + * @ingroup hash + * + */ +#ifndef TB_HASH_SHA_H +#define TB_HASH_SHA_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// data structure for sha (message data) computation +typedef struct __tb_sha_t +{ + tb_uint8_t digest_len; //!< digest length in 32-bit words + tb_hize_t count; //!< number of bytes in buffer + tb_uint8_t buffer[64]; //!< 512-bit buffer of input values used in hash updating + tb_uint32_t state[8]; //!< current hash value + tb_void_t (*transform)(tb_uint32_t *state, tb_uint8_t const buffer[64]); + +}tb_sha_t; + +// the sha mode type +typedef enum __tb_sha_mode_t +{ + TB_SHA_MODE_SHA1_160 = 160 +, TB_SHA_MODE_SHA2_224 = 224 +, TB_SHA_MODE_SHA2_256 = 256 + +}tb_sha_mode_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init sha + * + * @param sha the sha + * @param mode the mode + */ +tb_void_t tb_sha_init(tb_sha_t* sha, tb_size_t mode); + +/*! exit sha + * + * @param sha the sha + * @param data the data + * @param size the size + */ +tb_void_t tb_sha_exit(tb_sha_t* sha, tb_byte_t* data, tb_size_t size); + +/*! spak sha + * + * @param sha the sha + * @param data the data + * @param size the size + */ +tb_void_t tb_sha_spak(tb_sha_t* sha, tb_byte_t const* data, tb_size_t size); + +/*! make sha + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_sha_make(tb_size_t mode, tb_byte_t const* ib, tb_size_t ip, tb_byte_t* ob, tb_size_t on); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/hash/uuid.h b/core/pkg/tbox.pkg/inc/tbox/hash/uuid.h index 63024bc49..ad6615c5f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/hash/uuid.h +++ b/core/pkg/tbox.pkg/inc/tbox/hash/uuid.h @@ -65,3 +65,70 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file uuid.h + * @ingroup hash + * + */ +#ifndef TB_HASH_UUID_H +#define TB_HASH_UUID_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make an uuid + * + * @param uuid the uuid output buffer + * @param name we only generate it using a simple hashing function for speed if name is supplied + * + * @return tb_true or tb_false + */ +tb_bool_t tb_uuid_make(tb_byte_t uuid[16], tb_char_t const* name); + +/*! make an uuid string + * + * @param uuid_cstr the uuid output c-string + * @param name we only generate it using a simple hashing function for speed if name is supplied + * + * @return the uuid c-string or tb_null + */ +tb_char_t const* tb_uuid_make_cstr(tb_char_t uuid_cstr[37], tb_char_t const* name); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/libc.h b/core/pkg/tbox.pkg/inc/tbox/libc/libc.h index eeeec15ee..44bebb2a4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/libc.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/libc.h @@ -36,3 +36,41 @@ #include "string/string.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file libc.h + * @defgroup libc + * + */ +#ifndef TB_LIBC_H +#define TB_LIBC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "misc/misc.h" +#include "stdio/stdio.h" +#include "stdlib/stdlib.h" +#include "string/string.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/ctype.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/ctype.h index 77afa9995..19dde3be9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/ctype.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/ctype.h @@ -55,3 +55,60 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ctype.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_CTYPE_H +#define TB_LIBC_CTYPE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// is +#define tb_isspace(x) (((x) == 0x20) || ((x) > 0x8 && (x) < 0xe)) +#define tb_isgraph(x) ((x) > 0x1f && (x) < 0x7f) +#define tb_isalpha(x) (((x) > 0x40 && (x) < 0x5b) || ((x) > 0x60 && (x) < 0x7b)) +#define tb_isupper(x) ((x) > 0x40 && (x) < 0x5b) +#define tb_islower(x) ((x) > 0x60 && (x) < 0x7b) +#define tb_isascii(x) ((x) >= 0x0 && (x) < 0x80) +#define tb_isdigit(x) ((x) > 0x2f && (x) < 0x3a) +#define tb_isdigit2(x) ((x) == '0' || (x) == '1') +#define tb_isdigit8(x) (((x) > 0x2f && (x) < 0x38)) +#define tb_isdigit10(x) (tb_isdigit(x)) +#define tb_isdigit16(x) (((x) > 0x2f && (x) < 0x3a) || ((x) > 0x40 && (x) < 0x47) || ((x) > 0x60 && (x) < 0x67)) + +// to lower & upper +#define tb_tolower(x) (tb_isupper(x)? (x) + 0x20 : (x)) +#define tb_toupper(x) (tb_islower(x)? (x) - 0x20 : (x)) + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/limits.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/limits.h index 74afbaa7c..27d20215b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/limits.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/limits.h @@ -36,3 +36,41 @@ */ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file limits.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_MISC_LIMITS_H +#define TB_LIBC_MISC_LIMITS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/misc.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/misc.h index ba01c95f9..90e9112d6 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/misc.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/misc.h @@ -39,3 +39,44 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file misc.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_MISC_H +#define TB_LIBC_MISC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "ctype.h" +#include "stdarg.h" +#include "setjmp.h" +#include "signal.h" +#include "limits.h" +#include "./time/time.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/prefix.h index 7a2c6ea2f..b7c9d3ad7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_LIBC_MISC_PREFIX_H +#define TB_LIBC_MISC_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/setjmp.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/setjmp.h index ba59bd2ad..cd2edb13b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/setjmp.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/setjmp.h @@ -72,3 +72,77 @@ typedef sigjmp_buf tb_sigjmpbuf_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file setjmp.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_MISC_SETJMP_H +#define TB_LIBC_MISC_SETJMP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#if defined(TB_CONFIG_LIBC_HAVE_SETJMP) || defined(TB_CONFIG_LIBC_HAVE_SIGSETJMP) +# include <setjmp.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// setjmp +#ifdef TB_CONFIG_LIBC_HAVE_SETJMP +# define tb_setjmp(buf) setjmp(buf) +# define tb_longjmp(buf, val) longjmp(buf, val) +#else +# undef tb_setjmp +# undef tb_longjmp +#endif + +// sigsetjmp +#ifdef TB_CONFIG_LIBC_HAVE_SIGSETJMP +# define tb_sigsetjmp(buf, sig) sigsetjmp(buf, sig) +# define tb_siglongjmp(buf, val) siglongjmp(buf, val) +#else +# undef tb_sigsetjmp +# undef tb_siglongjmp +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the jmpbuf type +#ifdef TB_CONFIG_LIBC_HAVE_SETJMP +typedef jmp_buf tb_jmpbuf_t; +#endif + +// the sigjmpbuf type +#ifdef TB_CONFIG_LIBC_HAVE_SIGSETJMP +typedef sigjmp_buf tb_sigjmpbuf_t; +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/signal.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/signal.h index e4dbc67d8..42830733e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/signal.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/signal.h @@ -84,3 +84,89 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file signal.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_MISC_SIGNAL_H +#define TB_LIBC_MISC_SIGNAL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_LIBC_HAVE_SIGNAL +# include <signal.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_CONFIG_LIBC_HAVE_SIGNAL +# define tb_signal(sig, func) signal(sig, func) +#else +# undef tb_signal +#endif + +#ifdef TB_CONFIG_LIBC_HAVE_SIGNAL +# define TB_SIGHUP (1) // hangup (posix). +# define TB_SIGINT (2) // interrupt (ansi). +# define TB_SIGQUIT (3) // quit (posix). +# define TB_SIGILL (4) // illegal instruction (ansi). +# define TB_SIGTRAP (5) // trace trap (posix). +# define TB_SIGABRT (6) // abort (ansi). +# define TB_SIGIOT (6) // iot trap (4.2 bsd). +# define TB_SIGBUS (7) // bus error (4.2 bsd). +# define TB_SIGFPE (8) // floating-point exception (ansi). +# define TB_SIGKILL (9) // kill, unblockable (posix). +# define TB_SIGUSR1 (10) // user-defined signal 1 (posix). +# define TB_SIGSEGV (11) // segmentation violation (ansi). +# define TB_SIGUSR2 (12) // user-defined signal 2 (posix). +# define TB_SIGPIPE (13) // broken pipe (posix). +# define TB_SIGALRM (14) // alarm clock (posIX). +# define TB_SIGTERM (15) // termination (ansi). +# define TB_SIGSTKFLT (16) // stack fault. +# define TB_SIGCLD (TB_SIGCHLD) // same as sigchld (system v). +# define TB_SIGCHLD (17) // child status has changed (posix). +# define TB_SIGCONT (18) // continue (posix). +# define TB_SIGSTOP (19) // stop, unblockable (posix). +# define TB_SIGTSTP (20) // keyboard stop (posix). +# define TB_SIGTTIN (21) // background read from tty (posix). +# define TB_SIGTTOU (22) // background write to tty (posix). +# define TB_SIGURG (23) // urgent condition on socket (4.2 bsd). +# define TB_SIGXCPU (24) // cpu limit exceeded (4.2 bsd). +# define TB_SIGXFSZ (25) // file size limit exceeded (4.2 bsd). +# define TB_SIGVTALRM (26) // virtual alarm clock (4.2 bsd). +# define TB_SIGPROF (27) // profiling alarm clock (4.2 bsd). +# define TB_SIGWINCH (28) // window size change (4.3 bsd, sun). +# define TB_SIGPOLL (TB_SIGIO) // pollable event occurred (system v). +# define TB_SIGIO (29) // i/o now possible (4.2 bsd). +# define TB_SIGPWR (30) // power failure restart (system v). +# define TB_SIGSYS (31) // bad system call. +# define TB_SIGUNUSED (31) + +# define TB_SIG_DFL ((tb_void_t (*)(tb_int_t))0) +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/stdarg.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/stdarg.h index 7ea425a58..aec2b55c9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/stdarg.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/stdarg.h @@ -64,3 +64,69 @@ typedef va_list tb_va_list_t; #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file stdarg.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_MISC_STDARG_H +#define TB_LIBC_MISC_STDARG_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../../prefix.h" +#ifndef TB_COMPILER_IS_GCC +# include <stdarg.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_COMPILER_IS_GCC +# define tb_va_start(v, l) __builtin_va_start(v, l) +# define tb_va_end(v) __builtin_va_end(v) +# define tb_va_arg(v, l) __builtin_va_arg(v, l) +# define tb_va_copy(v, c) __builtin_va_copy(v, c) +#else +# define tb_va_start(v, l) va_start(v, l) +# define tb_va_end(v) va_end(v) +# define tb_va_arg(v, l) va_arg(v, l) +# ifndef va_copy +# define tb_va_copy(v, c) ((v) = (c)) +# else +# define tb_va_copy(v, c) va_copy(v, c) +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +#ifdef TB_COMPILER_IS_GCC +typedef __builtin_va_list tb_va_list_t; +#else +typedef va_list tb_va_list_t; +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/prefix.h index 4eaf5ccd3..c1192969f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_LIBC_MISC_TIME_PREFIX_H +#define TB_LIBC_MISC_TIME_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "type.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/time.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/time.h index 0e508aa7a..073fe302b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/time.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/time.h @@ -76,3 +76,81 @@ tb_time_t tb_mktime(tb_tm_t const* tm); tb_time_t tb_gmmktime(tb_tm_t const* tm); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file time.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_MISC_TIME_H +#define TB_LIBC_MISC_TIME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the time as the number of seconds since the epoch, 1970-01-01 00:00:00 +0000 (utc) + * + * @return the returned time or -1 + */ +tb_time_t tb_time(tb_noarg_t); + +/*! the gmt time + * + * @param the time value + * @param the gmt time pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_gmtime(tb_time_t time, tb_tm_t* tm); + +/*! the local time + * + * @param the time value + * @param the local time pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_localtime(tb_time_t time, tb_tm_t* tm); + +/*! make the time value from the local time + * + * @param the time + * + * @return the time value + */ +tb_time_t tb_mktime(tb_tm_t const* tm); + +/*! make the time value from the gmt time + * + * @param the time + * + * @return the time value + */ +tb_time_t tb_gmmktime(tb_tm_t const* tm); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/type.h b/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/type.h index 989215185..8a6157254 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/type.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/misc/time/type.h @@ -50,3 +50,55 @@ typedef struct __tb_tm_t }tb_tm_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file type.h + * + */ +#ifndef TB_LIBC_MISC_TIME_TYPE_H +#define TB_LIBC_MISC_TIME_TYPE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the tm type +typedef struct __tb_tm_t +{ + tb_long_t second; + tb_long_t minute; + tb_long_t hour; + tb_long_t mday; + tb_long_t month; + tb_long_t year; + tb_long_t week; + tb_long_t yday; + tb_long_t isdst; + +}tb_tm_t; + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libc/prefix.h index 918b7548c..62eebee1c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/prefix.h @@ -34,3 +34,39 @@ #include "./misc/stdarg.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_LIBC_PREFIX_H +#define TB_LIBC_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "./misc/ctype.h" +#include "./misc/limits.h" +#include "./misc/stdarg.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/stdio/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libc/stdio/prefix.h index feb039c4a..a377eb4fc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/stdio/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/stdio/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_LIBC_STDIO_PREFIX_H +#define TB_LIBC_STDIO_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/stdio/printf_object.h b/core/pkg/tbox.pkg/inc/tbox/libc/stdio/printf_object.h index 0e6906b8c..6245241f3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/stdio/printf_object.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/stdio/printf_object.h @@ -123,3 +123,128 @@ tb_void_t tb_printf_object_dump(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file printf_object.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_STDIO_PRINTF_OBJECT_H +#define TB_LIBC_STDIO_PRINTF_OBJECT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the printf object name maxn +#define TB_PRINTF_OBJECT_NAME_MAXN (16) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the printf object func type + * + * @param object the printf object + * @param cstr the string buffer + * @param maxn the string buffer maxn + * + * @return the real string size + */ +typedef tb_long_t (*tb_printf_object_func_t)(tb_cpointer_t object, tb_char_t* cstr, tb_size_t maxn); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! register the printf object func + * + * @note non thread-safe + * + * @param name the format name + * @param func the format func + * + * @code + static tb_long_t tb_printf_object_ipv4(tb_cpointer_t object, tb_char_t* cstr, tb_size_t maxn) + { + // check + tb_assert_and_check_return_val(object && cstr && maxn, -1); + + // the ipv4 + tb_ipv4_ref_t ipv4 = (tb_ipv4_ref_t)object; + + // format + tb_long_t size = tb_snprintf(cstr, maxn - 1, "%u.%u.%u.%u", ipv4->u8[0], ipv4->u8[1], ipv4->u8[2], ipv4->u8[3]); + if (size >= 0) cstr[size] = '\0'; + + // ok? + return size; + } + + // register the "ipv4" printf object func + tb_printf_object_register("ipv4", tb_printf_object_ipv4); + + // init ipv4 + tb_ipv4_t ipv4; + tb_ipv4_cstr_set(&ipv4, "127.0.0.1"); + + // trace ipv4, output: "ipv4: 127.0.0.1" + tb_trace_i("ipv4: %{ipv4}", &ipv4); + + * @endcode + */ +tb_void_t tb_printf_object_register(tb_char_t const* name, tb_printf_object_func_t func); + +/*! find the printf object func from the given format name + * + * @param name the format name + * + * @return the format func + */ +tb_printf_object_func_t tb_printf_object_find(tb_char_t const* name); + +/*! exit the printf object + */ +tb_void_t tb_printf_object_exit(tb_noarg_t); + +#ifdef __tb_debug__ +/*! dump the printf object + */ +tb_void_t tb_printf_object_dump(tb_noarg_t); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/stdio/stdio.h b/core/pkg/tbox.pkg/inc/tbox/libc/stdio/stdio.h index 3ca44f7b7..058322903 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/stdio/stdio.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/stdio/stdio.h @@ -254,3 +254,259 @@ tb_long_t tb_vswprintf(tb_wchar_t* s, tb_size_t n, tb_wchar_t const* f __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file stdio.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_STDIO_H +#define TB_LIBC_STDIO_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "printf_object.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// vsnprintf format +#define tb_vsnprintf_format(s, n, format, r) \ +do \ +{ \ + tb_long_t __tb_ret = 0; \ + tb_va_list_t __tb_varg_list; \ + tb_va_start(__tb_varg_list, format); \ + __tb_ret = tb_vsnprintf(s, (n), format, __tb_varg_list); \ + tb_va_end(__tb_varg_list); \ + if (__tb_ret >= 0) s[__tb_ret] = '\0'; \ + *r = __tb_ret > 0? __tb_ret : 0; \ + \ +} while (0) + +// vswprintf format +#define tb_vswprintf_format(s, n, format, r) \ +do \ +{ \ + tb_long_t __tb_ret = 0; \ + tb_va_list_t __tb_varg_list; \ + tb_va_start(__tb_varg_list, format); \ + __tb_ret = tb_vswprintf(s, (n), format, __tb_varg_list); \ + tb_va_end(__tb_varg_list); \ + if (__tb_ret >= 0) s[__tb_ret] = L'\0'; \ + *r = __tb_ret > 0? __tb_ret : 0; \ + \ +} while (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! puts + * + * @param string the string + * + * @return the real size + */ +tb_long_t tb_puts(tb_char_t const* string); + +/*! wputs + * + * @param string the string + * + * @return the real size + */ +tb_long_t tb_wputs(tb_wchar_t const* string); + +/*! printf + * + * @param format the format string + * + * @return the real size + * + * - format: %[flags][width][.precision][qualifier]type + * + * - flags: + * - default: right-justified, left-pad the output with spaces until the required length of output is attained. + * If combined with '0' (see below), + * it will cause the sign to become a space when positive, + * but the remaining characters will be zero-padded + * - -: left-justified, e.g. %-d + * - +: denote the sign '+' or '-' of a number + * - 0: use 0 instead of spaces to left-fill a fixed-length field + * - #: add prefix or suffix + * - %#o => add prefix: 0... + * - %#x => add prefix: 0x... + * - %#X => add prefix: 0X... + * - %#b => add prefix: 0b... + * - %#B => add prefix: 0B... + * - %#f => add prefix: 0f... + * - %#F => add prefix: 0F... + * + * - width: + * - n: n = 1, 2, 3, ..., fill spaces + * - 0n: n = 1, 2, 3, ..., fill 0 + * - *: causes printf to pad the output until it is n characters wide, + * where n is an integer value stored in the a function argument just preceding + * that represented by the modified type. + * e.g. printf("%*d", 5, 10) will result in "10" being printed with a width of 5. + * + * - .precision: + * - .n: for non-integral numeric types, causes the decimal portion of the output to be expressed in at least number digits. + * for the string type, causes the output to be truncated at number characters. + * if the precision is zero, nothing is printed for the corresponding argument. + * - *: same as the above, but uses an integer value in the intaken argument to + * determine the number of decimal places or maximum string length. + * e.g. printf("%.*s", 3, "abcdef") will result in "abc" being printed. + * + * - qualifier: + * - h: short integer or single double-point + * - l: long integer or double double-point + * - I8: 8-bit integer + * - I16: 16-bit integer + * - I32: 32-bit integer + * - I64/ll: 64-bit integer + * + * @note support h, l, I8, I16, I32, I64, ll + * + * - type(e.g. %d %x %u %% ...): + * - d, i: print an int as a signed decimal number. + * '%d' and '%i' are synonymous for output, but are different when used with scanf() for input. + * - u: print decimal unsigned int. + * - o: print an unsigned int in octal. + * - x/X: print an unsigned int as a hexadecimal number. 'x' uses lower-case letters and 'X' uses upper-case. + * - b/B: print an unsigned binary interger + * - e/E: print a double value in standard form ([-]d.ddd e[+/-]ddd). + * An E conversion uses the letter E (rather than e) to introduce the exponent. + * The exponent always contains at least two digits; if the value is zero, the exponent is 00. + * e.g. 3.141593e+00 + * - f/F: Print a double in normal (fixed-point) notation. + * 'f' and 'F' only differs in how the strings for an infinite number or NaN are printed + * ('inf', 'infinity' and 'nan' for 'f', 'INF', 'INFINITY' and 'NAN' for 'F'). + * - g/G: print a double in either normal or exponential notation, whichever is more appropriate for its magnitude. + * 'g' uses lower-case letters, 'G' uses upper-case letters. + * This type differs slightly from fixed-point notation in + * that insignificant zeroes to the right of the decimal point are not included. + * Also, the decimal point is not included on whole numbers. + * - c: print a char (character). + * - s: print a character string + * - p: print a void * (pointer to void) in an implementation-defined format. + * - n: print nothing, but write number of characters successfully written so far into an integer pointer parameter. + * - %: % + * + * @note support d, i, u, o, u, x/X, b/B, f/F, c, s + * @note not support e/E, g/G, p, n + * + * @code + * tb_printf("|hello world|\n"); + * tb_printf("|%-10s|%%|%10s|\n", "hello", "world"); + * tb_printf("|%#2c|%2.5c|%*c|\n", 'A', 'B', 5, 'C'); + * tb_printf("|%#2d|%#8.3o|%*.*d|\n", -56, 56, 10, 5, 56); + * tb_printf("|%#-8.5x|%#2.9X|\n", 0x1f, 0x1f); + * tb_printf("|%#-8.5b|%#2.9B|\n", 0x1f, 0x1f); + * tb_printf("|%-6Id|%5I8u|%#I64x|%#llx|\n", 256, 255, (tb_int64_t)0x8fffffffffff, (tb_int64_t)0x8fffffffffff); + * tb_printf("|%lf|\n", -3.1415926535897932384626433832795); + * tb_printf("|%lf|%lf|%lf|\n", 3.14, 0, -0); + * tb_printf("|%0.9f|\n", 3.1415926535897932384626433832795); + * tb_printf("|%16.9f|\n", 3.1415926535897932384626433832795); + * tb_printf("|%016.9f|\n", 3.14159); + * tb_printf("|%lf|\n", 1.0 / 6.0); + * tb_printf("|%lf|\n", 0.0003141596); + * tb_printf("|%F|\n", tb_float_to_fixed(3.1415)); + * tb_printf("|%{object_name}|\n", object); + * @endcode + */ +tb_long_t tb_printf(tb_char_t const* format, ...); + +/*! wprintf + * + * @param format the format string + * + * @return the real size + */ +tb_long_t tb_wprintf(tb_wchar_t const* format, ...); + +/*! sprintf + * + * @param s the string data + * @param format the format string + * + * @return the real size + */ +tb_long_t tb_sprintf(tb_char_t* s, tb_char_t const* format, ...); + +/*! snprintf + * + * @param s the string data + * @param n the string size + * @param format the format string + * + * @return the real size + */ +tb_long_t tb_snprintf(tb_char_t* s, tb_size_t n, tb_char_t const* format, ...); + +/*! vsnprintf + * + * @param s the string data + * @param n the string size + * @param format the format string + * @param args the arguments + * + * @return the real size + */ +tb_long_t tb_vsnprintf(tb_char_t* s, tb_size_t n, tb_char_t const* format, tb_va_list_t args); + +/*! swprintf + * + * @param s the string data + * @param n the string size + * @param format the format string + * + * @return the real size + */ +tb_long_t tb_swprintf(tb_wchar_t* s, tb_size_t n, tb_wchar_t const* format, ...); + +/*! vswprintf + * + * @param s the string data + * @param n the string size + * @param format the format string + * @param args the arguments + * + * @return the real size + */ +tb_long_t tb_vswprintf(tb_wchar_t* s, tb_size_t n, tb_wchar_t const* format, tb_va_list_t args); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/prefix.h index ab25e3b8c..3cb437a4f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_LIBC_STDLIB_PREFIX_H +#define TB_LIBC_STDLIB_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/stdlib.h b/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/stdlib.h index 201551853..1648349bc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/stdlib.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/stdlib/stdlib.h @@ -306,3 +306,311 @@ tb_long_t tb_random(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file stdlib.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_STDLIB_H +#define TB_LIBC_STDLIB_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// for uint32 +#define tb_s2tou32(s) ((tb_uint32_t)tb_s2tou64(s)) +#define tb_s8tou32(s) ((tb_uint32_t)tb_s8tou64(s)) +#define tb_s10tou32(s) ((tb_uint32_t)tb_s10tou64(s)) +#define tb_s16tou32(s) ((tb_uint32_t)tb_s16tou64(s)) +#define tb_stou32(s) ((tb_uint32_t)tb_stou64(s)) +#define tb_sbtou32(s, b) ((tb_uint32_t)tb_sbtou64(s, b)) + +// for int32 +#define tb_s2toi32(s) ((tb_int32_t)tb_s2tou64(s)) +#define tb_s8toi32(s) ((tb_int32_t)tb_s8tou64(s)) +#define tb_s10toi32(s) ((tb_int32_t)tb_s10tou64(s)) +#define tb_s16toi32(s) ((tb_int32_t)tb_s16tou64(s)) +#define tb_stoi32(s) ((tb_int32_t)tb_stou64(s)) +#define tb_sbtoi32(s, b) ((tb_int32_t)tb_sbtou64(s, b)) + +// for int64 +#define tb_s2toi64(s) ((tb_int64_t)tb_s2tou64(s)) +#define tb_s8toi64(s) ((tb_int64_t)tb_s8tou64(s)) +#define tb_s10toi64(s) ((tb_int64_t)tb_s10tou64(s)) +#define tb_s16toi64(s) ((tb_int64_t)tb_s16tou64(s)) +#define tb_stoi64(s) ((tb_int64_t)tb_stou64(s)) +#define tb_sbtoi64(s, b) ((tb_int64_t)tb_sbtou64(s, b)) + +// for float +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_s2tof(s) ((tb_float_t)tb_s2tod(s)) +# define tb_s8tof(s) ((tb_float_t)tb_s8tod(s)) +# define tb_s10tof(s) ((tb_float_t)tb_s10tod(s)) +# define tb_s16tof(s) ((tb_float_t)tb_s16tod(s)) +# define tb_stof(s) ((tb_float_t)tb_stod(s)) +# define tb_sbtof(s, b) ((tb_float_t)tb_sbtod(s, b)) +#endif + +// for porting libc +#define tb_atoi(s) tb_s10toi32(s) +#define tb_atoll(s) tb_s10toi64(s) +#define tb_strtol(s, e, b) tb_sbtoi32(s, b) +#define tb_strtoll(s, e, b) tb_sbtoi64(s, b) +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_atof(s) tb_s10tod(s) +# define tb_strtof(s, e) tb_s10tof(s) +# define tb_strtod(s, e) tb_s10tod(s) +#endif +#if TB_CPU_BIT64 +# define tb_atol(s) (tb_long_t)tb_s10toi64(s) +#else +# define tb_atol(s) (tb_long_t)tb_s10toi32(s) +#endif + +// atow +#define tb_atow(s1, s2, n) tb_mbstowcs(s1, s2, n) + +// wtoa +#define tb_wtoa(s1, s2, n) tb_wcstombs(s1, s2, n) + +// rand +#define tb_rand() (tb_int_t)tb_random() +#define tb_srand(seed) tb_srandom(seed) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! convert the binary string to uint64 + * + * <pre> + * .e.g + * + * "1001" => 9 + * "0b1001" => 9 + * </pre> + * + * @param s the string + * + * @return the uint64 number + */ +tb_uint64_t tb_s2tou64(tb_char_t const* s); + +/*! convert the octal string to uint64 + * + * <pre> + * .e.g + * + * "11" => 9 + * "011" => 9 + * </pre> + * + * @param s the string + * + * @return the uint64 number + */ +tb_uint64_t tb_s8tou64(tb_char_t const* s); + +/*! convert the decimal string to uint64 + * + * .e.g "9" => 9 + * + * @param s the string + * + * @return the uint64 number + */ +tb_uint64_t tb_s10tou64(tb_char_t const* s); + +/*! convert the hex string to uint64 + * + * <pre> + * .e.g + * + * "9" => 9 + * "0x9" => 9 + * </pre> + * + * @param s the string + * + * @return the uint64 number + */ +tb_uint64_t tb_s16tou64(tb_char_t const* s); + +/*! auto convert string to uint64 + * + * <pre> + * .e.g + * + * "0b1001" => 9 + * "011" => 9 + * "9" => 9 + * "0x9" => 9 + * </pre> + * + * @param s the string + * + * @return the uint64 number + */ +tb_uint64_t tb_stou64(tb_char_t const* s); + +/*! convert string to uint64 using the given base number + * + * @param s the string + * + * @return the uint64 number + */ +tb_uint64_t tb_sbtou64(tb_char_t const* s, tb_int_t base); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +/*! convert the binary string to double + * + * <pre> + * .e.g + * + * "1001" => 9 + * "0b1001" => 9 + * </pre> + * + * @param s the string + * + * @return the double number + */ +tb_double_t tb_s2tod(tb_char_t const* s); + +/*! convert the binary string to double + * + * <pre> + * .e.g + * + * "11" => 9 + * "011" => 9 + * </pre> + * + * @param s the string + * + * @return the double number + */ +tb_double_t tb_s8tod(tb_char_t const* s); + +/*! convert the decimal string to double + * + * .e.g "9" => 9 + * + * @param s the string + * + * @return the double number + */ +tb_double_t tb_s10tod(tb_char_t const* s); + +/*! convert the hex string to double + * + * <pre> + * .e.g + * + * "9" => 9 + * "0x9" => 9 + * </pre> + * + * @param s the string + * + * @return the double number + */ +tb_double_t tb_s16tod(tb_char_t const* s); + +/*! auto convert string to double + * + * <pre> + * .e.g + * + * "0b1001" => 9 + * "011" => 9 + * "9" => 9 + * "0x9" => 9 + * </pre> + * + * @param s the string + * + * @return the double number + */ +tb_double_t tb_stod(tb_char_t const* s); + +/*! convert string to double using the given base number + * + * @param s the string + * + * @return the double number + */ +tb_double_t tb_sbtod(tb_char_t const* s, tb_int_t base); + +#endif + +/*! mbstowcs, convert string to wstring + * + * @param s1 the wstring data + * @param s2 the string data + * @param n the string length + * + * @return the wstring length + */ +tb_size_t tb_mbstowcs(tb_wchar_t* s1, tb_char_t const* s2, tb_size_t n); + +/*! wcstombs, convert wstring to string + * + * @param s1 the string data + * @param s2 the wstring data + * @param n the wstring length + * + * @return the string length + */ +tb_size_t tb_wcstombs(tb_char_t* s1, tb_wchar_t const* s2, tb_size_t n); + +/*! update random seed + * + * @param seed the random seed + */ +tb_void_t tb_srandom(tb_size_t seed); + +/*! generate the random with range: [0, max) + * + * @return the random value + */ +tb_long_t tb_random(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/string/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libc/string/prefix.h index a92de642b..322a52702 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/string/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/string/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_LIBC_STRING_PREFIX_H +#define TB_LIBC_STRING_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libc/string/string.h b/core/pkg/tbox.pkg/inc/tbox/libc/string/string.h index 84f672570..9921e748f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libc/string/string.h +++ b/core/pkg/tbox.pkg/inc/tbox/libc/string/string.h @@ -180,3 +180,185 @@ tb_wchar_t* tb_wcsnirstr(tb_wchar_t const* s1, tb_size_t n, tb_wchar_t c __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file string.h + * @ingroup libc + * + */ +#ifndef TB_LIBC_STRING_H +#define TB_LIBC_STRING_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// memset_ptr +#if TB_CPU_BIT64 +# define tb_memset_ptr(s, p, n) tb_memset_u64(s, (tb_uint64_t)(p), n) +#else +# define tb_memset_ptr(s, p, n) tb_memset_u32(s, (tb_uint32_t)(p), n) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// memset +tb_pointer_t tb_memset(tb_pointer_t s, tb_byte_t c, tb_size_t n); +tb_pointer_t tb_memset_(tb_pointer_t s, tb_byte_t c, tb_size_t n); +tb_pointer_t tb_memset_u16(tb_pointer_t s, tb_uint16_t c, tb_size_t n); +tb_pointer_t tb_memset_u24(tb_pointer_t s, tb_uint32_t c, tb_size_t n); +tb_pointer_t tb_memset_u32(tb_pointer_t s, tb_uint32_t c, tb_size_t n); +tb_pointer_t tb_memset_u64(tb_pointer_t s, tb_uint64_t c, tb_size_t n); + +// memdup +tb_pointer_t tb_memdup(tb_cpointer_t s, tb_size_t n); +tb_pointer_t tb_memdup_(tb_cpointer_t s, tb_size_t n); + +// memcpy +tb_pointer_t tb_memcpy(tb_pointer_t s1, tb_cpointer_t s2, tb_size_t n); +tb_pointer_t tb_memcpy_(tb_pointer_t s1, tb_cpointer_t s2, tb_size_t n); + +// memmov +tb_pointer_t tb_memmov(tb_pointer_t s1, tb_cpointer_t s2, tb_size_t n); +tb_pointer_t tb_memmov_(tb_pointer_t s1, tb_cpointer_t s2, tb_size_t n); + +// memcmp +tb_long_t tb_memcmp(tb_cpointer_t s1, tb_cpointer_t s2, tb_size_t n); +tb_long_t tb_memcmp_(tb_cpointer_t s1, tb_cpointer_t s2, tb_size_t n); + +// memmem +tb_pointer_t tb_memmem(tb_cpointer_t s1, tb_size_t n1, tb_cpointer_t s2, tb_size_t n2); +tb_pointer_t tb_memmem_(tb_cpointer_t s1, tb_size_t n1, tb_cpointer_t s2, tb_size_t n2); + +// strlen +tb_size_t tb_strlen(tb_char_t const* s); +tb_size_t tb_strnlen(tb_char_t const* s, tb_size_t n); + +// strdup +tb_char_t* tb_strdup(tb_char_t const* s); +tb_char_t* tb_strndup(tb_char_t const* s, tb_size_t n); + +// strcat +tb_char_t* tb_strcat(tb_char_t* s1, tb_char_t const* s2); +tb_char_t* tb_strncat(tb_char_t* s1, tb_char_t const* s2, tb_size_t n); + +// strcpy +tb_char_t* tb_strcpy(tb_char_t* s1, tb_char_t const* s2); +tb_char_t* tb_strncpy(tb_char_t* s1, tb_char_t const* s2, tb_size_t n); +tb_size_t tb_strlcpy(tb_char_t* s1, tb_char_t const* s2, tb_size_t n); + +// strcmp +tb_long_t tb_strcmp(tb_char_t const* s1, tb_char_t const* s2); +tb_long_t tb_strncmp(tb_char_t const* s1, tb_char_t const* s2, tb_size_t n); + +tb_long_t tb_stricmp(tb_char_t const* s1, tb_char_t const* s2); +tb_long_t tb_strnicmp(tb_char_t const* s1, tb_char_t const* s2, tb_size_t n); + +// strchr +tb_char_t* tb_strchr(tb_char_t const* s, tb_char_t c); +tb_char_t* tb_strichr(tb_char_t const* s, tb_char_t c); + +// strrchr +tb_char_t* tb_strrchr(tb_char_t const* s, tb_char_t c); +tb_char_t* tb_strirchr(tb_char_t const* s, tb_char_t c); + +tb_char_t* tb_strnrchr(tb_char_t const* s, tb_size_t n, tb_char_t c); +tb_char_t* tb_strnirchr(tb_char_t const* s, tb_size_t n, tb_char_t c); + +// strstr +tb_char_t* tb_strstr(tb_char_t const* s1, tb_char_t const* s2); +tb_char_t* tb_stristr(tb_char_t const* s1, tb_char_t const* s2); + +tb_char_t* tb_strnstr(tb_char_t const* s1, tb_size_t n1, tb_char_t const* s2); +tb_char_t* tb_strnistr(tb_char_t const* s1, tb_size_t n1, tb_char_t const* s2); + +// strrstr +tb_char_t* tb_strrstr(tb_char_t const* s1, tb_char_t const* s2); +tb_char_t* tb_strirstr(tb_char_t const* s1, tb_char_t const* s2); + +tb_char_t* tb_strnrstr(tb_char_t const* s1, tb_size_t n1, tb_char_t const* s2); +tb_char_t* tb_strnirstr(tb_char_t const* s1, tb_size_t n1, tb_char_t const* s2); + +// wcslen +tb_size_t tb_wcslen(tb_wchar_t const* s); +tb_size_t tb_wcsnlen(tb_wchar_t const* s, tb_size_t n); + +// wcsdup +tb_wchar_t* tb_wcsdup(tb_wchar_t const* s); +tb_wchar_t* tb_wcsndup(tb_wchar_t const* s, tb_size_t n); + +// wcscat +tb_wchar_t* tb_wcscat(tb_wchar_t* s1, tb_wchar_t const* s2); +tb_wchar_t* tb_wcsncat(tb_wchar_t* s1, tb_wchar_t const* s2, tb_size_t n); + +// wcscpy +tb_wchar_t* tb_wcscpy(tb_wchar_t* s1, tb_wchar_t const* s2); +tb_wchar_t* tb_wcsncpy(tb_wchar_t* s1, tb_wchar_t const* s2, tb_size_t n); +tb_size_t tb_wcslcpy(tb_wchar_t* s1, tb_wchar_t const* s2, tb_size_t n); + +// wcscmp +tb_long_t tb_wcscmp(tb_wchar_t const* s1, tb_wchar_t const* s2); +tb_long_t tb_wcsncmp(tb_wchar_t const* s1, tb_wchar_t const* s2, tb_size_t n); + +tb_long_t tb_wcsicmp(tb_wchar_t const* s1, tb_wchar_t const* s2); +tb_long_t tb_wcsnicmp(tb_wchar_t const* s1, tb_wchar_t const* s2, tb_size_t n); + +// wcschr +tb_wchar_t* tb_wcschr(tb_wchar_t const* s, tb_wchar_t c); +tb_wchar_t* tb_wcsichr(tb_wchar_t const* s, tb_wchar_t c); + +// wcsrchr +tb_wchar_t* tb_wcsrchr(tb_wchar_t const* s, tb_wchar_t c); +tb_wchar_t* tb_wcsirchr(tb_wchar_t const* s, tb_wchar_t c); + +tb_wchar_t* tb_wcsnrchr(tb_wchar_t const* s, tb_size_t n, tb_wchar_t c); +tb_wchar_t* tb_wcsnirchr(tb_wchar_t const* s, tb_size_t n, tb_wchar_t c); + +// wcsstr +tb_wchar_t* tb_wcsstr(tb_wchar_t const* s1, tb_wchar_t const* s2); +tb_wchar_t* tb_wcsistr(tb_wchar_t const* s1, tb_wchar_t const* s2); + +// wcsrstr +tb_wchar_t* tb_wcsrstr(tb_wchar_t const* s1, tb_wchar_t const* s2); +tb_wchar_t* tb_wcsirstr(tb_wchar_t const* s1, tb_wchar_t const* s2); + +tb_wchar_t* tb_wcsnrstr(tb_wchar_t const* s1, tb_size_t n, tb_wchar_t const* s2); +tb_wchar_t* tb_wcsnirstr(tb_wchar_t const* s1, tb_size_t n, tb_wchar_t const* s2); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/ceil.h b/core/pkg/tbox.pkg/inc/tbox/libm/ceil.h index 50470c26b..d630ec4b7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/ceil.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/ceil.h @@ -37,3 +37,42 @@ #define tb_ceil(x) ((x) > 0? (tb_int32_t)((x) + 0.9999999999) : (tb_int32_t)(x)) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ceil.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_CEIL_H +#define TB_LIBM_CEIL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_ceil(x) ((x) > 0? (tb_int32_t)((x) + 0.9999999999) : (tb_int32_t)(x)) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/fabs.h b/core/pkg/tbox.pkg/inc/tbox/libm/fabs.h index c7549ee2a..a5edd0e8d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/fabs.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/fabs.h @@ -37,3 +37,42 @@ #define tb_fabs tb_abs #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fabs.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_FABS_H +#define TB_LIBM_FABS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_fabs tb_abs + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/floor.h b/core/pkg/tbox.pkg/inc/tbox/libm/floor.h index e04b09a81..40a59cd99 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/floor.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/floor.h @@ -37,3 +37,42 @@ #define tb_floor(x) ((x) > 0? (tb_int32_t)(x) : (tb_int32_t)((x) - 0.9999999999)) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file floor.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_FLOOR_H +#define TB_LIBM_FLOOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_floor(x) ((x) > 0? (tb_int32_t)(x) : (tb_int32_t)((x) - 0.9999999999)) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/inf.h b/core/pkg/tbox.pkg/inc/tbox/libm/inf.h index 7d4ee644c..4fb595296 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/inf.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/inf.h @@ -45,3 +45,50 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file inf.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_INF_H +#define TB_LIBM_INF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "maf.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(3, 3) +# define TB_INF (__builtin_inff ()) +#else +# define TB_INF TB_MAF +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/libm.h b/core/pkg/tbox.pkg/inc/tbox/libm/libm.h index 7d8d89b44..597587760 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/libm.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/libm.h @@ -32,3 +32,37 @@ #include "math.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file libm.h + * @defgroup libm + * + */ +#ifndef TB_LIBM_H +#define TB_LIBM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "math.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/maf.h b/core/pkg/tbox.pkg/inc/tbox/libm/maf.h index 8c96d150c..06494f549 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/maf.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/maf.h @@ -55,3 +55,60 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file maf.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_MAF_H +#define TB_LIBM_MAF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(3, 3) +# define TB_MAF (__builtin_huge_val()) +#elif defined(TB_COMPILER_IS_GCC) && TB_COMPILER_VERSION_BE(2, 96) +# define TB_MAF (__extension__ 0x1.0p2047) +#elif defined(TB_COMPILER_IS_GCC) +# define TB_MAF (__extension__ ((union { unsigned __l __attribute__((__mode__(__DI__))); tb_double_t __d; }) { __l: 0x7ff0000000000000ULL }).__d) +#else + typedef union { tb_byte_t __c[8]; tb_double_t __d; } __tb_maf_t; +# ifdef TB_WORDS_BIGENDIAN +# define __tb_maf_bytes { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } +# else +# define __tb_maf_bytes { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f } +# endif + static __tb_maf_t __tb_maf = { __tb_maf_bytes }; +# define TB_MAF (__tb_maf.__d) +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/math.h b/core/pkg/tbox.pkg/inc/tbox/libm/math.h index 99ef382ca..d2ebc0ae8 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/math.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/math.h @@ -142,3 +142,147 @@ tb_uint32_t tb_idivi8(tb_uint32_t x, tb_uint8_t y); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file math.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_MATH_H +#define TB_LIBM_MATH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# include "nan.h" +# include "inf.h" +# include "maf.h" +# include "mif.h" +# include "pi.h" +# include "fabs.h" +# include "round.h" +# include "ceil.h" +# include "floor.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +// is infinite? +tb_long_t tb_isinf(tb_double_t x); +tb_long_t tb_isinff(tb_float_t x); + +// is finite? +tb_long_t tb_isfin(tb_double_t x); +tb_long_t tb_isfinf(tb_float_t x); + +// is nan? +tb_long_t tb_isnan(tb_double_t x); +tb_long_t tb_isnanf(tb_float_t x); + +// sqrt +tb_double_t tb_sqrt(tb_double_t x); +tb_float_t tb_sqrtf(tb_float_t x); + +// sin +tb_double_t tb_sin(tb_double_t x); +tb_float_t tb_sinf(tb_float_t x); + +// cos +tb_double_t tb_cos(tb_double_t x); +tb_float_t tb_cosf(tb_float_t x); + +// tan +tb_double_t tb_tan(tb_double_t x); +tb_float_t tb_tanf(tb_float_t x); + +// atan +tb_double_t tb_atan(tb_double_t x); +tb_float_t tb_atanf(tb_float_t x); + +// exp +tb_double_t tb_exp(tb_double_t x); +tb_float_t tb_expf(tb_float_t x); + +// expi +tb_double_t tb_expi(tb_long_t x); +tb_float_t tb_expif(tb_long_t x); + +// exp1 +tb_double_t tb_exp1(tb_double_t x); +tb_float_t tb_exp1f(tb_float_t x); + +// asin +tb_double_t tb_asin(tb_double_t x); +tb_float_t tb_asinf(tb_float_t x); + +// acos +tb_double_t tb_acos(tb_double_t x); +tb_float_t tb_acosf(tb_float_t x); + +// atan2 +tb_double_t tb_atan2(tb_double_t y, tb_double_t x); +tb_float_t tb_atan2f(tb_float_t y, tb_float_t x); + +// log2 +tb_double_t tb_log2(tb_double_t x); +tb_float_t tb_log2f(tb_float_t x); + +// sincos +tb_void_t tb_sincos(tb_double_t x, tb_double_t* s, tb_double_t* c); +tb_void_t tb_sincosf(tb_float_t x, tb_float_t* s, tb_float_t* c); + +// pow +tb_double_t tb_pow(tb_double_t x, tb_double_t y); +tb_float_t tb_powf(tb_float_t x, tb_float_t y); + +// fmod +tb_double_t tb_fmod(tb_double_t x, tb_double_t y); +tb_float_t tb_fmodf(tb_float_t x, tb_float_t y); +#endif + +// ilog2i +tb_uint32_t tb_ilog2i(tb_uint32_t x); + +// isqrti +tb_uint32_t tb_isqrti(tb_uint32_t x); +tb_uint32_t tb_isqrti64(tb_uint64_t x); + +// idivi8 +tb_uint32_t tb_idivi8(tb_uint32_t x, tb_uint8_t y); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/mif.h b/core/pkg/tbox.pkg/inc/tbox/libm/mif.h index 926904a20..4ad6594ce 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/mif.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/mif.h @@ -38,3 +38,43 @@ #define TB_MIF (-TB_MAF) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file mif.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_MIF_H +#define TB_LIBM_MIF_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "maf.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define TB_MIF (-TB_MAF) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/nan.h b/core/pkg/tbox.pkg/inc/tbox/libm/nan.h index 05d8dafbc..6c6c868a5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/nan.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/nan.h @@ -52,3 +52,57 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file nan.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_NAN_H +#define TB_LIBM_NAN_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + + +#if defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(3, 3) +# define TB_NAN (__builtin_nanf ("")) +#elif defined(TB_COMPILER_IS_GCC) +# define TB_NAN (__extension__ ((union { unsigned __l __attribute__ ((__mode__ (__SI__))); tb_float_t __d; }) { __l: 0x7fc00000UL }).__d) +#else +# ifdef TB_WORDS_BIGENDIAN +# define __tb_nan_bytes { 0x7f, 0xc0, 0, 0 } +# else +# define __tb_nan_bytes { 0, 0, 0xc0, 0x7f } +# endif + static union { tb_byte_t __c[4]; tb_float_t __d; } __tb_nan_union = { __tb_nan_bytes }; +# define TB_NAN (__tb_nan_union.__d) +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/pi.h b/core/pkg/tbox.pkg/inc/tbox/libm/pi.h index b415d6fcc..b8f451941 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/pi.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/pi.h @@ -37,3 +37,42 @@ #define TB_PI (3.141592653589793) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file pi.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_PI_H +#define TB_LIBM_PI_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define TB_PI (3.141592653589793) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/prefix.h b/core/pkg/tbox.pkg/inc/tbox/libm/prefix.h index 0d4b654b7..4d1dfbcdd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/prefix.h @@ -75,3 +75,80 @@ typedef union __tb_ieee_float_t #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_PREFIX_H +#define TB_LIBM_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +// the ieee float type +typedef union __tb_ieee_float_t +{ + tb_float_t f; + tb_uint32_t i; + +}tb_ieee_float_t; + +// the ieee double type +# ifdef TB_FLOAT_BIGENDIAN + typedef union __tb_ieee_double_t + { + tb_double_t d; + struct + { + tb_uint32_t h; + tb_uint32_t l; + + }i; + + }tb_ieee_double_t; +# else + typedef union __tb_ieee_double_t + { + tb_double_t d; + struct + { + tb_uint32_t l; + tb_uint32_t h; + }i; + + }tb_ieee_double_t; +# endif + +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/libm/round.h b/core/pkg/tbox.pkg/inc/tbox/libm/round.h index 509c836ed..558af16c3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/libm/round.h +++ b/core/pkg/tbox.pkg/inc/tbox/libm/round.h @@ -37,3 +37,42 @@ #define tb_round(x) ((x) > 0? (tb_int32_t)((x) + 0.5) : (tb_int32_t)((x) - 0.5)) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file round.h + * @ingroup libm + * + */ +#ifndef TB_LIBM_ROUND_H +#define TB_LIBM_ROUND_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_round(x) ((x) > 0? (tb_int32_t)((x) + 0.5) : (tb_int32_t)((x) - 0.5)) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/math/fixed.h b/core/pkg/tbox.pkg/inc/tbox/math/fixed.h index e3239e070..85b1fff2f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/fixed.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/fixed.h @@ -102,3 +102,107 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed.h + * @ingroup math + * + */ +#ifndef TB_MATH_FIXED_H +#define TB_MATH_FIXED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "fixed6.h" +#include "fixed16.h" +#include "fixed30.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define TB_FIXED_ONE TB_FIXED16_ONE +#define TB_FIXED_HALF TB_FIXED16_HALF +#define TB_FIXED_MAX TB_FIXED16_MAX +#define TB_FIXED_MIN TB_FIXED16_MIN +#define TB_FIXED_NAN TB_FIXED16_NAN +#define TB_FIXED_INF TB_FIXED16_INF +#define TB_FIXED_PI TB_FIXED16_PI +#define TB_FIXED_SQRT2 TB_FIXED16_SQRT2 +#define TB_FIXED_NEAR0 TB_FIXED16_NEAR0 + +// conversion +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed_to_float(x) tb_fixed16_to_float(x) +# define tb_float_to_fixed(x) tb_float_to_fixed16(x) +#endif + +#define tb_long_to_fixed(x) tb_long_to_fixed16(x) +#define tb_fixed_to_long(x) tb_fixed16_to_long(x) + +#define tb_fixed6_to_fixed(x) tb_fixed6_to_fixed16(x) +#define tb_fixed_to_fixed6(x) tb_fixed16_to_fixed6(x) + +#define tb_fixed30_to_fixed(x) tb_fixed30_to_fixed16(x) +#define tb_fixed_to_fixed30(x) tb_fixed16_to_fixed30(x) + +// round +#define tb_fixed_round(x) tb_fixed16_round(x) +#define tb_fixed_ceil(x) tb_fixed16_ceil(x) +#define tb_fixed_floor(x) tb_fixed16_floor(x) + +// nearly equal? +#define tb_fixed_near_eq(x, y) tb_fixed16_near_eq(x, y) + +// operations +#define tb_fixed_abs(x) tb_fixed16_abs(x) +#define tb_fixed_avg(x, y) tb_fixed16_avg(x, y) +#define tb_fixed_lsh(x, y) tb_fixed16_lsh(x, y) +#define tb_fixed_rsh(x, y) tb_fixed16_rsh(x, y) +#define tb_fixed_mul(x, y) tb_fixed16_mul(x, y) +#define tb_fixed_div(x, y) tb_fixed16_div(x, y) +#define tb_fixed_imul(x, y) tb_fixed16_imul(x, y) +#define tb_fixed_idiv(x, y) tb_fixed16_idiv(x, y) +#define tb_fixed_imuldiv(x, y, z) tb_fixed16_imuldiv(x, y, z) +#define tb_fixed_imulsub(x, y, z) tb_fixed16_imulsub(x, y, z) +#define tb_fixed_invert(x) tb_fixed16_invert(x) +#define tb_fixed_sqre(x) tb_fixed16_sqre(x) +#define tb_fixed_sqrt(x) tb_fixed16_sqrt(x) +#define tb_fixed_sin(x) tb_fixed16_sin(x) +#define tb_fixed_cos(x) tb_fixed16_cos(x) +#define tb_fixed_sincos(x, s, c) tb_fixed16_sincos(x, s, c) +#define tb_fixed_tan(x) tb_fixed16_tan(x) +#define tb_fixed_asin(x) tb_fixed16_asin(x) +#define tb_fixed_acos(x) tb_fixed16_acos(x) +#define tb_fixed_atan(x) tb_fixed16_atan(x) +#define tb_fixed_atan2(y, x) tb_fixed16_atan2(y, x) +#define tb_fixed_exp(x) tb_fixed16_exp(x) +#define tb_fixed_exp1(x) tb_fixed16_exp1(x) +#define tb_fixed_expi(x) tb_fixed16_expi(x) +#define tb_fixed_ilog2(x) tb_fixed16_ilog2(x) + + + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/fixed16.h b/core/pkg/tbox.pkg/inc/tbox/math/fixed16.h index be772f9ac..85bb36f30 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/fixed16.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/fixed16.h @@ -520,3 +520,525 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed16.h + * @ingroup math + * + */ +#ifndef TB_MATH_FIXED16_H +#define TB_MATH_FIXED16_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "int32.h" +#include "../libm/libm.h" +#ifdef TB_ARCH_ARM +# include "impl/fixed16_arm.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// constant +#define TB_FIXED16_ONE (1 << 16) +#define TB_FIXED16_HALF (1 << 15) +#define TB_FIXED16_MAX (TB_MAXS32) +#define TB_FIXED16_MIN (TB_MINS32) +#define TB_FIXED16_NAN ((tb_int_t)0x80000000) +#define TB_FIXED16_INF (TB_MAXS32) +#define TB_FIXED16_PI (0x3243f) +#define TB_FIXED16_SQRT2 (92682) +#define TB_FIXED16_NEAR0 (1 << 4) + +// conversion +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# ifndef tb_fixed16_to_float +# define tb_fixed16_to_float(x) ((tb_float_t)((x) * 1.5258789e-5)) +# endif +# ifndef tb_float_to_fixed16 +# define tb_float_to_fixed16(x) ((tb_fixed16_t)((x) * TB_FIXED16_ONE)) +# endif +#endif + +#ifdef __tb_debug__ +# define tb_long_to_fixed16(x) tb_long_to_fixed16_check(x) +# define tb_fixed16_to_long(x) tb_fixed16_to_long_check(x) +#else +# define tb_long_to_fixed16(x) (tb_fixed16_t)((x) << 16) +# define tb_fixed16_to_long(x) (tb_long_t)((x) >> 16) +#endif + +// round +#define tb_fixed16_round(x) (((x) + TB_FIXED16_HALF) >> 16) + +// ceil +#define tb_fixed16_ceil(x) (((x) + TB_FIXED16_ONE - 1) >> 16) + +// floor +#define tb_fixed16_floor(x) ((x) >> 16) + +// abs +#define tb_fixed16_abs(x) tb_abs(x) + +// avg +#define tb_fixed16_avg(x, y) (((x) + (y)) >> 1) + +// nearly equal? +#define tb_fixed16_near_eq(x, y) (tb_fixed16_abs((x) - (y)) <= TB_FIXED16_NEAR0) + +// mul +#ifndef tb_fixed16_mul +# ifdef __tb_debug__ +# define tb_fixed16_mul(x, y) tb_fixed16_mul_check(x, y) +# else +# define tb_fixed16_mul(x, y) ((tb_fixed16_t)(((tb_hong_t)(x) * (y)) >> 16)) +# endif +#endif + +// div +#ifndef tb_fixed16_div +# ifdef __tb_debug__ +# define tb_fixed16_div(x, y) tb_fixed16_div_check(x, y) +# else +# define tb_fixed16_div(x, y) ((tb_fixed16_t)((((tb_hong_t)(x)) << 16) / (y))) +# endif +#endif + +// imul +#ifndef tb_fixed16_imul +# ifdef __tb_debug__ +# define tb_fixed16_imul(x, y) tb_fixed16_imul_check(x, y) +# else +# define tb_fixed16_imul(x, y) ((tb_fixed16_t)((x) * (y))) +# endif +#endif + +// idiv +#ifndef tb_fixed16_idiv +# ifdef __tb_debug__ +# define tb_fixed16_idiv(x, y) tb_fixed16_idiv_check(x, y) +# else +# define tb_fixed16_idiv(x, y) ((tb_fixed16_t)((x) / (y))) +# endif +#endif + +// imuldiv +#ifndef tb_fixed16_imuldiv +# ifdef __tb_debug__ +# define tb_fixed16_imuldiv(x, y, z) tb_fixed16_imuldiv_check(x, y, z) +# else +# define tb_fixed16_imuldiv(x, y, z) ((tb_fixed16_t)(((tb_hong_t)(x) * (y)) / (z))) +# endif +#endif + +// imulsub +#ifndef tb_fixed16_imulsub +# ifdef __tb_debug__ +# define tb_fixed16_imulsub(x, y, z) tb_fixed16_imulsub_check(x, y, z) +# else +# define tb_fixed16_imulsub(x, y, z) ((tb_fixed16_t)(((tb_hong_t)(x) * (y)) - (z))) +# endif +#endif + +// lsh +#ifndef tb_fixed16_lsh +# define tb_fixed16_lsh(x, y) ((x) << (y)) +#endif + +// rsh +#ifndef tb_fixed16_rsh +# define tb_fixed16_rsh(x, y) ((x) >> (y)) +#endif + +// invert: 1 / x +#ifndef tb_fixed16_invert +# define tb_fixed16_invert(x) tb_fixed16_div(TB_FIXED16_ONE, x) +#endif + +// sqre +#ifndef tb_fixed16_sqre +# ifdef __tb_debug__ +# define tb_fixed16_sqre(x) tb_fixed16_sqre_check(x) +# else +# define tb_fixed16_sqre(x) ((tb_fixed16_t)(((tb_hong_t)(x) * (x)) >> 16)) +# endif +#endif + +// sqrt +#ifndef tb_fixed16_sqrt +# define tb_fixed16_sqrt(x) tb_fixed16_sqrt_int32(x) +#endif + +// sin +#ifndef tb_fixed16_sin +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_sin(x) tb_fixed16_sin_float(x) +# else +# define tb_fixed16_sin(x) tb_fixed16_sin_int32(x) +# endif +#endif + +// cos +#ifndef tb_fixed16_cos +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_cos(x) tb_fixed16_cos_float(x) +# else +# define tb_fixed16_cos(x) tb_fixed16_cos_int32(x) +# endif +#endif + +// sincos +#ifndef tb_fixed16_sincos +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_sincos(x, s, c) tb_fixed16_sincos_float(x, s, c) +# else +# define tb_fixed16_sincos(x, s, c) tb_fixed16_sincos_int32(x, s, c) +# endif +#endif + +// tan +#ifndef tb_fixed16_tan +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_tan(x) tb_fixed16_tan_float(x) +# else +# define tb_fixed16_tan(x) tb_fixed16_tan_int32(x) +# endif +#endif + +// asin +#ifndef tb_fixed16_asin +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_asin(x) tb_fixed16_asin_float(x) +# else +# define tb_fixed16_asin(x) tb_fixed16_asin_int32(x) +# endif +#endif + +// acos +#ifndef tb_fixed16_acos +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_acos(x) tb_fixed16_acos_float(x) +# else +# define tb_fixed16_acos(x) tb_fixed16_acos_int32(x) +# endif +#endif + +// atan +#ifndef tb_fixed16_atan +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_atan(x) tb_fixed16_atan_float(x) +# else +# define tb_fixed16_atan(x) tb_fixed16_atan_int32(x) +# endif +#endif + +// atan2 +#ifndef tb_fixed16_atan2 +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_atan2(y, x) tb_fixed16_atan2_float(y, x) +# else +# define tb_fixed16_atan2(y, x) tb_fixed16_atan2_int32(y, x) +# endif +#endif + +// exp +#ifndef tb_fixed16_exp +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_exp(x) tb_fixed16_exp_float(x) +# else +# define tb_fixed16_exp(x) tb_fixed16_exp_int32(x) +# endif +#endif + +#ifndef tb_fixed16_expi +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_expi(x) tb_fixed16_expi_float(x) +# else +# define tb_fixed16_expi(x) tb_assert(0) +# endif +#endif + +#ifndef tb_fixed16_exp1 +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_fixed16_exp1(x) tb_fixed16_exp1_float(x) +# else +# define tb_fixed16_exp1(x) tb_assert(0) +# endif +#endif + +// log +#ifndef tb_fixed16_ilog2 +# define tb_fixed16_ilog2(x) tb_fixed16_ilog2_int32(x) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! compute the invert of the fixed-point value + * + * @param x the fixed-point x-value + * + * @return the result + */ +tb_fixed16_t tb_fixed16_invert_int32(tb_fixed16_t x); + +/*! compute the sin and cos value of the fixed-point value + * + * @param x the fixed-point x-value + * @param s the sin fixed-point value + * @param c the cos fixed-point value + */ +tb_void_t tb_fixed16_sincos_int32(tb_fixed16_t x, tb_fixed16_t* s, tb_fixed16_t* c); + +/*! compute the atan2 value of the fixed-point value + * + * @param y the fixed-point y-value + * @param x the fixed-point x-value + * + * @return the result + */ +tb_fixed16_t tb_fixed16_atan2_int32(tb_fixed16_t y, tb_fixed16_t x); + +/*! compute the asin value of the fixed-point value + * + * @param x the fixed-point x-value + * + * @return the result + */ +tb_fixed16_t tb_fixed16_asin_int32(tb_fixed16_t x); + +/*! compute the atan value of the fixed-point value + * + * @param x the fixed-point x-value + * + * @return the result + */ +tb_fixed16_t tb_fixed16_atan_int32(tb_fixed16_t x); + +/*! compute the exp value of the fixed-point value + * + * @param x the fixed-point x-value + * + * @return the result + */ +tb_fixed16_t tb_fixed16_exp_int32(tb_fixed16_t x); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ +static __tb_inline__ tb_fixed16_t tb_long_to_fixed16_check(tb_long_t x) +{ + // check overflow + tb_assert(x == (tb_int16_t)x); + + // ok + return (tb_fixed16_t)(x << 16); +} +static __tb_inline__ tb_long_t tb_fixed16_to_long_check(tb_fixed16_t x) +{ + // check overflow + tb_assert(x >= TB_FIXED16_MIN && x <= TB_FIXED16_MAX); + + // ok + return (x >> 16); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_mul_check(tb_fixed16_t x, tb_fixed16_t y) +{ + // done + tb_hong_t v = (((tb_hong_t)x * y) >> 16); + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_div_check(tb_fixed16_t x, tb_fixed16_t y) +{ + // check + tb_assert(y); + + // done + tb_hong_t v = ((((tb_hong_t)x) << 16) / y); + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_sqre_check(tb_fixed16_t x) +{ + // done + tb_hong_t v = (((tb_hong_t)x * x) >> 16); + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_imul_check(tb_fixed16_t x, tb_long_t y) +{ + // done + tb_hong_t v = ((tb_hong_t)x * y); + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_idiv_check(tb_fixed16_t x, tb_long_t y) +{ + // check + tb_assert(y); + + // ok + return (tb_fixed16_t)(x / y); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_imuldiv_check(tb_fixed16_t x, tb_long_t y, tb_long_t z) +{ + // done + tb_hong_t v = ((tb_hong_t)x * y) / z; + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_imulsub_check(tb_fixed16_t x, tb_long_t y, tb_long_t z) +{ + // done + tb_hong_t v = ((tb_hong_t)x * y) - z; + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +static __tb_inline__ tb_fixed16_t tb_fixed16_sin_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_sinf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_cos_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_cosf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_void_t tb_fixed16_sincos_float(tb_fixed16_t x, tb_fixed16_t* s, tb_fixed16_t* c) +{ + tb_float_t sf, cf; + tb_sincosf(tb_fixed16_to_float(x), &sf, &cf); + if (s) *s = tb_float_to_fixed16(sf); + if (s) *c = tb_float_to_fixed16(cf); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_tan_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_tanf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_asin_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_asinf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_acos_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_acosf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_atan_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_atanf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_atan2_float(tb_fixed16_t y, tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_atan2f(tb_fixed16_to_float(y), tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_exp_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_expf(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_exp1_float(tb_fixed16_t x) +{ + return tb_float_to_fixed16(tb_exp1f(tb_fixed16_to_float(x))); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_expi_float(tb_long_t x) +{ + return tb_float_to_fixed16(tb_expif(x)); +} +#endif +static __tb_inline__ tb_fixed16_t tb_fixed16_sqrt_int32(tb_fixed16_t x) +{ + tb_assert(x > 0); + return (x > 0? (tb_isqrti(x) << 8) : 0); +} +static __tb_inline__ tb_uint32_t tb_fixed16_ilog2_int32(tb_fixed16_t x) +{ + tb_assert(x > 0); + tb_uint32_t lg = tb_ilog2i(x); + return (lg > 16? (lg - 16) : 0); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_sin_int32(tb_fixed16_t x) +{ + tb_fixed16_t s = 0; + tb_fixed16_sincos_int32(x, &s, tb_null); + return s; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_cos_int32(tb_fixed16_t x) +{ + tb_fixed16_t c = 0; + tb_fixed16_sincos_int32(x, tb_null, &c); + return c; +} +static __tb_inline__ tb_fixed16_t tb_fixed16_tan_int32(tb_fixed16_t x) +{ + tb_fixed16_t s = 0; + tb_fixed16_t c = 0; + tb_fixed16_sincos_int32(x, &s, &c); + return tb_fixed16_div(s, c); +} +static __tb_inline__ tb_fixed16_t tb_fixed16_acos_int32(tb_fixed16_t x) +{ + // asin + acos = pi / 2 + tb_fixed16_t z = tb_fixed16_asin_int32(x); + return ((TB_FIXED16_PI >> 1) - z); +} + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/fixed30.h b/core/pkg/tbox.pkg/inc/tbox/math/fixed30.h index 67bbd13a4..71eedd4d3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/fixed30.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/fixed30.h @@ -232,3 +232,237 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed30.h + * @ingroup math + * + */ +#ifndef TB_MATH_FIXED30_H +#define TB_MATH_FIXED30_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "int32.h" +#include "../libm/libm.h" +#ifdef TB_ARCH_ARM +# include "impl/fixed16_arm.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// constant +#define TB_FIXED30_ONE (1 << 30) +#define TB_FIXED30_HALF (1 << 29) +#define TB_FIXED30_MAX (TB_MAXS32) +#define TB_FIXED30_MIN (-TB_FIXED30_MAX) +#define TB_FIXED30_NAN ((tb_int_t)0x80000000) +#define TB_FIXED30_INF (TB_MAXS32) +#define TB_FIXED30_SQRT2 (0x5a827999) + +// conversion +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# ifndef tb_fixed30_to_float +# define tb_fixed30_to_float(x) (((x) * 0.00000000093132257f)) +# endif +# ifndef tb_float_to_fixed30 +# ifdef __tb_debug__ +# define tb_float_to_fixed30(x) tb_float_to_fixed30_check(x) +# else +# define tb_float_to_fixed30(x) ((tb_fixed30_t)((x) * TB_FIXED30_ONE)) +# endif +# endif +#endif + +#ifdef __tb_debug__ +# define tb_fixed16_to_fixed30(x) tb_fixed16_to_fixed30_check(x) +#else +# define tb_fixed16_to_fixed30(x) ((x) << 14) +#endif +#define tb_fixed30_to_fixed16(x) ((x) >> 14) + +// abs +#define tb_fixed30_abs(x) tb_abs(x) + +// avg +#define tb_fixed30_avg(x, y) (((x) + (y)) >> 1) + +// mul +#ifndef tb_fixed30_mul +# if 1 +# define tb_fixed30_mul(x, y) tb_fixed30_mul_int64(x, y) +# elif defined(TB_CONFIG_TYPE_HAVE_FLOAT) +# define tb_fixed30_mul(x, y) tb_fixed30_mul_float(x, y) +# else +# define tb_fixed30_mul(x, y) tb_fixed30_mul_int32(x, y) +# endif +#endif + +// div +#ifndef tb_fixed30_div +# if 1 +# define tb_fixed30_div(x, y) tb_fixed30_div_int64(x, y) +# elif defined(TB_CONFIG_TYPE_HAVE_FLOAT) +# define tb_fixed30_div(x, y) tb_fixed30_div_float(x, y) +# else +# define tb_fixed30_div(x, y) tb_int32_div(x, y, 30) +# endif +#endif + +// sqre +#ifndef tb_fixed30_sqre +# if 1 +# define tb_fixed30_sqre(x) tb_fixed30_sqre_int64(x) +# elif defined(TB_CONFIG_TYPE_HAVE_FLOAT) +# define tb_fixed30_sqre(x) tb_fixed30_sqre_float(x) +# else +# define tb_fixed30_sqre(x) tb_fixed30_sqre_int32(x) +# endif +#endif + +// sqrt +#ifndef tb_fixed30_sqrt +# define tb_fixed30_sqrt(x) tb_fixed30_sqrt_int32(x) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +#ifdef __tb_debug__ +# ifdef TB_CONFIG_TYPE_HAVE_FLOAT +static __tb_inline__ tb_fixed30_t tb_float_to_fixed30_check(tb_float_t x) +{ + // check overflow, [-2., 2.] + tb_assert(x >= -2. && x <= 2.); + return ((tb_fixed30_t)((x) * TB_FIXED30_ONE)); +} +# endif +static __tb_inline__ tb_fixed30_t tb_fixed16_to_fixed30_check(tb_fixed16_t x) +{ + // check overflow, [-2, 2] + tb_assert((x >> 16) >= -2 && (x >> 16) <= 2); + return (x << 14); +} +#endif + +static __tb_inline__ tb_fixed30_t tb_fixed30_mul_int64(tb_fixed30_t x, tb_fixed30_t y) +{ + return (tb_fixed30_t)((tb_hong_t)x * y >> 30); +} +static __tb_inline__ tb_fixed30_t tb_fixed30_div_int64(tb_fixed30_t x, tb_fixed30_t y) +{ + tb_assert(y); + return (tb_fixed30_t)((((tb_hong_t)x) << 30) / y); +} +static __tb_inline__ tb_fixed30_t tb_fixed30_sqre_int64(tb_fixed30_t x) +{ + return (tb_fixed30_t)((tb_hong_t)x * x >> 30); +} + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +static __tb_inline__ tb_fixed30_t tb_fixed30_mul_float(tb_fixed30_t x, tb_fixed30_t y) +{ + tb_float_t f = tb_fixed30_to_float(x) * tb_fixed30_to_float(y); + return tb_float_to_fixed30(f); +} +static __tb_inline__ tb_fixed30_t tb_fixed30_div_float(tb_fixed30_t x, tb_fixed30_t y) +{ + tb_assert(y); + return tb_float_to_fixed30((tb_float_t)x / y); +} +static __tb_inline__ tb_fixed30_t tb_fixed30_sqre_float(tb_fixed30_t x) +{ + tb_float_t f = tb_fixed30_to_float(x); + f *= f; + return tb_float_to_fixed30(f); +} +#endif + +static __tb_inline__ tb_fixed30_t tb_fixed30_mul_int32(tb_fixed30_t x, tb_fixed30_t y) +{ + // get sign + tb_int32_t s = tb_int32_get_sign(x ^ y); + x = tb_fixed30_abs(x); + y = tb_fixed30_abs(y); + + tb_uint32_t xh = x >> 16; + tb_uint32_t xl = x & 0xffff; + tb_uint32_t yh = y >> 16; + tb_uint32_t yl = y & 0xffff; + + tb_uint32_t xyh = xh * yh; + tb_uint32_t xyl = xl * yl; + tb_uint32_t xyhl = xh * yl + xl * yh; + + tb_uint32_t lo = xyl + (xyhl << 16); + tb_uint32_t hi = xyh + (xyhl >> 16) + (lo < xyl); + + // check overflow + tb_assert(!(hi >> 29)); + + tb_uint32_t r = (hi << 2) + (lo >> 30); + return tb_int32_set_sign(r, s); +} + +static __tb_inline__ tb_fixed30_t tb_fixed30_sqre_int32(tb_fixed30_t x) +{ + x = tb_fixed30_abs(x); + + tb_uint32_t xh = x >> 16; + tb_uint32_t xl = x & 0xffff; + + tb_uint32_t xxh = xh * xh; + tb_uint32_t xxl = xl * xl; + tb_uint32_t xxhl = (xh * xl) << 1; + + tb_uint32_t lo = xxl + (xxhl << 16); + tb_uint32_t hi = xxh + (xxhl >> 16) + (lo < xxl); + + // check overflow + tb_assert(!(hi >> 29)); + + return ((hi << 2) + (lo >> 30)); +} +static __tb_inline__ tb_fixed30_t tb_fixed30_sqrt_int32(tb_fixed30_t x) +{ + tb_assert(x > 0); + return (x > 0? (tb_isqrti(x) << 15) : 0); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/fixed6.h b/core/pkg/tbox.pkg/inc/tbox/math/fixed6.h index 25e6386b6..27c380e6f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/fixed6.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/fixed6.h @@ -189,3 +189,194 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed6.h + * @ingroup math + * + */ +#ifndef TB_MATH_FIXED6_H +#define TB_MATH_FIXED6_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "fixed16.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// constant +#define TB_FIXED6_ONE (64) +#define TB_FIXED6_HALF (32) +#define TB_FIXED6_MAX (TB_MAXS32) +#define TB_FIXED6_MIN (TB_MINS32) +#define TB_FIXED6_NAN ((tb_int_t)0x80000000) +#define TB_FIXED6_INF (TB_MAXS32) +#define TB_FIXED6_PI (0xc9) +#define TB_FIXED6_SQRT2 (0x5a) +#define TB_FIXED6_NEAR0 (0) + +// conversion +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# ifndef tb_fixed6_to_float +# define tb_fixed6_to_float(x) (((x) * 0.015625)) +# endif +# ifndef tb_float_to_fixed6 +# define tb_float_to_fixed6(x) ((tb_fixed6_t)((x) * TB_FIXED6_ONE)) +# endif +#endif + +#ifdef __tb_debug__ +# define tb_int_to_fixed6(x) tb_long_to_fixed6_check(x) +# define tb_fixed6_to_int(x) tb_fixed6_to_long_check(x) + +# define tb_long_to_fixed6(x) tb_long_to_fixed6_check(x) +# define tb_fixed6_to_long(x) tb_fixed6_to_long_check(x) +#else +# define tb_int_to_fixed6(x) (tb_fixed6_t)((x) << 6) +# define tb_fixed6_to_int(x) (tb_int_t)((x) >> 6) + +# define tb_long_to_fixed6(x) (tb_fixed6_t)((x) << 6) +# define tb_fixed6_to_long(x) (tb_long_t)((x) >> 6) +#endif + +#define tb_fixed6_to_fixed16(x) ((x) << 10) +#define tb_fixed16_to_fixed6(x) ((x) >> 10) + +// round +#define tb_fixed6_round(x) (((x) + TB_FIXED6_HALF) >> 6) + +// ceil +#define tb_fixed6_ceil(x) (((x) + TB_FIXED6_ONE - 1) >> 6) + +// floor +#define tb_fixed6_floor(x) ((x) >> 6) + +// abs +#define tb_fixed6_abs(x) tb_abs(x) + +// avg +#define tb_fixed6_avg(x, y) (((x) + (y)) >> 1) + +// nearly equal? +#define tb_fixed6_near_eq(x, y) (tb_fixed6_abs((x) - (y)) <= TB_FIXED6_NEAR0) + +// mul +#ifndef tb_fixed6_mul +# define tb_fixed6_mul(x, y) tb_fixed6_mul_inline(x, y) +#endif + +// div +#ifndef tb_fixed6_div +# define tb_fixed6_div(x, y) tb_fixed6_div_inline(x, y) +#endif + +// imul +#ifndef tb_fixed6_imul +# define tb_fixed6_imul(x, y) tb_fixed16_imul(x, y) +#endif + +// idiv +#ifndef tb_fixed6_idiv +# define tb_fixed6_idiv(x, y) tb_fixed16_idiv(x, y) +#endif + +// imuldiv +#ifndef tb_fixed6_imuldiv +# define tb_fixed6_imuldiv(x, y, z) tb_fixed16_imuldiv(x, y, z) +#endif + +// imulsub +#ifndef tb_fixed6_imulsub +# define tb_fixed6_imulsub(x, y, z) tb_fixed16_imulsub(x, y, z) +#endif + +// lsh +#ifndef tb_fixed6_lsh +# define tb_fixed6_lsh(x, y) tb_fixed16_lsh(x, y) +#endif + +// rsh +#ifndef tb_fixed6_rsh +# define tb_fixed6_rsh(x, y) tb_fixed16_rsh(x, y) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +#ifdef __tb_debug__ +static __tb_inline__ tb_fixed6_t tb_long_to_fixed6_check(tb_long_t x) +{ + // check overflow + tb_assert(x >= (TB_MINS32 >> 10) && x <= (TB_MAXS32 >> 10)); + + // ok + return (tb_fixed6_t)(x << 6); +} +static __tb_inline__ tb_long_t tb_fixed6_to_int_check(tb_fixed6_t x) +{ + // check overflow + tb_assert(x >= TB_FIXED6_MIN && x <= TB_FIXED6_MAX); + + // ok + return (tb_fixed6_t)(x >> 6); +} +#endif +static __tb_inline__ tb_fixed6_t tb_fixed6_mul_inline(tb_fixed6_t x, tb_fixed6_t y) +{ + // done + tb_hong_t v = (((tb_hong_t)x * y) >> 6); + + // check overflow + tb_assert(v == (tb_int32_t)v); + + // ok + return (tb_fixed16_t)v; +} +static __tb_inline__ tb_fixed16_t tb_fixed6_div_inline(tb_fixed6_t x, tb_fixed6_t y) +{ + // check + tb_assert(y); + + // no overflow? compute it fastly + if (x == (tb_int16_t)x) return (x << 16) / y; + + // done + return tb_fixed16_div(x, y); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed16_arm.h b/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed16_arm.h index 5c559606e..18f7b41e2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed16_arm.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed16_arm.h @@ -66,3 +66,71 @@ static __tb_inline__ tb_fixed16_t tb_fixed16_mul_asm(tb_fixed16_t x, tb_fixed16_ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed16_arm.h + * + */ +#ifndef TB_MATH_IMPL_FIXED16_ARM_H +#define TB_MATH_IMPL_FIXED16_ARM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifdef TB_ASSEMBLER_IS_GAS + +#if 0 +# define tb_fixed16_mul(x, y) tb_fixed16_mul_asm(x, y) +#endif + +#endif /* TB_ASSEMBLER_IS_GAS */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#if defined(TB_ASSEMBLER_IS_GAS) && !defined(TB_ARCH_ARM64) +static __tb_inline__ tb_fixed16_t tb_fixed16_mul_asm(tb_fixed16_t x, tb_fixed16_t y) +{ + __tb_register__ tb_fixed16_t t; + __tb_asm__ __tb_volatile__ + ( + "smull %0, %2, %1, %3 \n" // r64 = (l, h) = x * y + "mov %0, %0, lsr #16 \n" // to fixed16: r64 >>= 16 + "orr %0, %0, %2, lsl #16 \n" // x = l = (h << (32 - 16)) | (l >> 16); + + : "=r"(x), "=&r"(y), "=r"(t) + : "r"(x), "1"(y) + ); + return x; +} +#endif + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed30_arm.h b/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed30_arm.h index 1d756c220..1935bbe48 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed30_arm.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/impl/fixed30_arm.h @@ -65,3 +65,70 @@ static __tb_inline__ tb_fixed30_t tb_fixed30_mul_asm(tb_fixed30_t x, tb_fixed30_ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed30_arm.h + * + */ +#ifndef TB_MATH_IMPL_FIXED30_ARM_H +#define TB_MATH_IMPL_FIXED30_ARM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifdef TB_ASSEMBLER_IS_GAS + +#if 0 +# define tb_fixed30_mul(x, y) tb_fixed30_mul_asm(x, y) +#endif + +#endif /* TB_ASSEMBLER_IS_GAS */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ +#if defined(TB_ASSEMBLER_IS_GAS) +static __tb_inline__ tb_fixed30_t tb_fixed30_mul_asm(tb_fixed30_t x, tb_fixed30_t y) +{ + __tb_register__ tb_fixed30_t t; + __tb_asm__ __tb_volatile__ + ( + "smull %0, %2, %1, %3 \n" // r64 = (l, h) = x * y + "mov %0, %0, lsr #30 \n" // to fixed30: r64 >>= 30 + "orr %0, %0, %2, lsl #2 \n" // x = l = (h << (32 - 30)) | (l >> 30); + + : "=r"(x), "=&r"(y), "=r"(t) + : "r"(x), "1"(y) + ); + return x; +} +#endif + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/impl/impl.h b/core/pkg/tbox.pkg/inc/tbox/math/impl/impl.h index 771022e32..053aeb875 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/impl/impl.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/impl/impl.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file impl.h + * + */ +#ifndef TB_MATH_IMPL_H +#define TB_MATH_IMPL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "math.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/math/impl/math.h b/core/pkg/tbox.pkg/inc/tbox/math/impl/math.h index 6dfcba93e..465025e4e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/impl/math.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/impl/math.h @@ -55,3 +55,60 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file math.h + * + */ +#ifndef TB_MATH_IMPL_MATH_H +#define TB_MATH_IMPL_MATH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* init math envirnoment + * + * @return tb_true or tb_false + */ +tb_bool_t tb_math_init_env(tb_noarg_t); + +// exit math envirnoment +tb_void_t tb_math_exit_env(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/impl/prefix.h b/core/pkg/tbox.pkg/inc/tbox/math/impl/prefix.h index ac06401aa..2e08d0139 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/impl/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/impl/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_MATH_IMPL_PREFIX_H +#define TB_MATH_IMPL_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/math/int32.h b/core/pkg/tbox.pkg/inc/tbox/math/int32.h index ec0468187..14feaa8b7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/int32.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/int32.h @@ -86,3 +86,91 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file int32.h + * @ingroup math + * + */ +#ifndef TB_MATH_INT32_H +#define TB_MATH_INT32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// sign +#define tb_int32_get_sign(x) tb_int32_get_sign_inline(x) +#define tb_int32_set_sign(x, s) tb_int32_set_sign_inline(x, s) + +// bool: is true? +#define tb_int32_nz(x) tb_int32_nz_inline(x) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// div +tb_int32_t tb_int32_div(tb_int32_t x, tb_int32_t y, tb_int_t nbits); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inline + */ + +// return -1 if x < 0, else return 0 +static __tb_inline__ tb_int32_t tb_int32_get_sign_inline(tb_int32_t x) +{ + tb_int32_t s = ((tb_int32_t)(x) >> 31); + tb_assert((x < 0 && s == -1) || (x >= 0 && !s)); + return s; +} +// if s == -1, return -x, else s must be 0, and return x. +static __tb_inline__ tb_int32_t tb_int32_set_sign_inline(tb_int32_t x, tb_int32_t s) +{ + tb_assert(s == 0 || s == -1); + return (x ^ s) - s; +} +// non zero, return 1 if x != 0, else return 0 +static __tb_inline__ tb_long_t tb_int32_nz_inline(tb_uint32_t x) +{ + //return (x? 1 : 0); + return ((x | (0 - x)) >> 31); +} + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/math.h b/core/pkg/tbox.pkg/inc/tbox/math/math.h index 53d0c1084..5250b0579 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/math.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/math.h @@ -39,3 +39,44 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file math.h + * @defgroup math + * + */ +#ifndef TB_MATH_H +#define TB_MATH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "int32.h" +#include "fixed6.h" +#include "fixed16.h" +#include "fixed30.h" +#include "fixed.h" +#include "random/random.h" + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/prefix.h b/core/pkg/tbox.pkg/inc/tbox/math/prefix.h index 4dcf53d7f..bd6b20a7a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_MATH_PREFIX_H +#define TB_MATH_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/math/random/linear.h b/core/pkg/tbox.pkg/inc/tbox/math/random/linear.h index fbbd67348..76de7918e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/random/linear.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/random/linear.h @@ -59,3 +59,64 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file linear.h + * @ingroup math + * + */ +#ifndef TB_MATH_RANDOM_LINEAR_H +#define TB_MATH_RANDOM_LINEAR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! set the linear random seed + * + * @param seed the random seed + */ +tb_void_t tb_random_linear_seed(tb_size_t seed); + +/*! generate the linear random value + * + * @return the random value + */ +tb_long_t tb_random_linear_value(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/math/random/prefix.h b/core/pkg/tbox.pkg/inc/tbox/math/random/prefix.h index c6ed41ff5..f4658a9ac 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/random/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/random/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_MATH_RANDOM_PREFIX_H +#define TB_MATH_RANDOM_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/math/random/random.h b/core/pkg/tbox.pkg/inc/tbox/math/random/random.h index 24e407c4e..ffdcf3f58 100644 --- a/core/pkg/tbox.pkg/inc/tbox/math/random/random.h +++ b/core/pkg/tbox.pkg/inc/tbox/math/random/random.h @@ -86,3 +86,91 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file random.h + * + */ +#ifndef TB_MATH_RANDOM_H +#define TB_MATH_RANDOM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "linear.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! set the random seed + * + * @param seed the random seed + */ +tb_void_t tb_random_seed(tb_size_t seed); + +/*! reset value using the initial seed + * + * @param pseudo reset to the pseudo random? + */ +tb_void_t tb_random_reset(tb_bool_t pseudo); + +/*! generate the random value + * + * it will generate a pseudo-random sequence if the seed is not modified manually. + * + * @return the random value + */ +tb_long_t tb_random_value(tb_noarg_t); + +/*! generate the random with range: [begin, end) + * + * @param begin the begin value + * @param end the end value + * + * @return the random value + */ +tb_long_t tb_random_range(tb_long_t begin, tb_long_t end); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! generate the float random with range: [begin, end) + * + * @param begin the begin value + * @param end the end value + * + * @return the random value + */ +tb_float_t tb_random_rangef(tb_float_t begin, tb_float_t end); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/allocator.h b/core/pkg/tbox.pkg/inc/tbox/memory/allocator.h index cef68a383..5b10720df 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/allocator.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/allocator.h @@ -422,3 +422,427 @@ tb_bool_t tb_allocator_have(tb_allocator_ref_t allocator, tb_cpoin __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file allocator.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_ALLOCATOR_H +#define TB_MEMORY_ALLOCATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#define tb_allocator_malloc(allocator, size) tb_allocator_malloc_(allocator, size __tb_debug_vals__) +#define tb_allocator_malloc0(allocator, size) tb_allocator_malloc0_(allocator, size __tb_debug_vals__) + +#define tb_allocator_nalloc(allocator, item, size) tb_allocator_nalloc_(allocator, item, size __tb_debug_vals__) +#define tb_allocator_nalloc0(allocator, item, size) tb_allocator_nalloc0_(allocator, item, size __tb_debug_vals__) + +#define tb_allocator_ralloc(allocator, data, size) tb_allocator_ralloc_(allocator, (tb_pointer_t)(data), size __tb_debug_vals__) +#define tb_allocator_free(allocator, data) tb_allocator_free_(allocator, (tb_pointer_t)(data) __tb_debug_vals__) + +#define tb_allocator_large_malloc(allocator, size, real) tb_allocator_large_malloc_(allocator, size, real __tb_debug_vals__) +#define tb_allocator_large_malloc0(allocator, size, real) tb_allocator_large_malloc0_(allocator, size, real __tb_debug_vals__) + +#define tb_allocator_large_nalloc(allocator, item, size, real) tb_allocator_large_nalloc_(allocator, item, size, real __tb_debug_vals__) +#define tb_allocator_large_nalloc0(allocator, item, size, real) tb_allocator_large_nalloc0_(allocator, item, size, real __tb_debug_vals__) + +#define tb_allocator_large_ralloc(allocator, data, size, real) tb_allocator_large_ralloc_(allocator, (tb_pointer_t)(data), size, real __tb_debug_vals__) +#define tb_allocator_large_free(allocator, data) tb_allocator_large_free_(allocator, (tb_pointer_t)(data) __tb_debug_vals__) + +#define tb_allocator_align_malloc(allocator, size, align) tb_allocator_align_malloc_(allocator, size, align __tb_debug_vals__) +#define tb_allocator_align_malloc0(allocator, size, align) tb_allocator_align_malloc0_(allocator, size, align __tb_debug_vals__) + +#define tb_allocator_align_nalloc(allocator, item, size, align) tb_allocator_align_nalloc_(allocator, item, size, align __tb_debug_vals__) +#define tb_allocator_align_nalloc0(allocator, item, size, align) tb_allocator_align_nalloc0_(allocator, item, size, align __tb_debug_vals__) + +#define tb_allocator_align_ralloc(allocator, data, size, align) tb_allocator_align_ralloc_(allocator, (tb_pointer_t)(data), size, align __tb_debug_vals__) +#define tb_allocator_align_free(allocator, data) tb_allocator_align_free_(allocator, (tb_pointer_t)(data) __tb_debug_vals__) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the allocator type enum +typedef enum __tb_allocator_type_e +{ + TB_ALLOCATOR_NONE = 0 +, TB_ALLOCATOR_DEFAULT = 1 +, TB_ALLOCATOR_NATIVE = 2 +, TB_ALLOCATOR_STATIC = 4 +, TB_ALLOCATOR_LARGE = 5 +, TB_ALLOCATOR_SMALL = 6 + +}tb_allocator_type_e; + +/// the allocator type +typedef struct __tb_allocator_t +{ + /// the type + tb_size_t type; + + /// the lock + tb_spinlock_t lock; + + /*! malloc data + * + * @param allocator the allocator + * @param size the size + * + * @return the data address + */ + tb_pointer_t (*malloc)(struct __tb_allocator_t* allocator, tb_size_t size __tb_debug_decl__); + + /*! realloc data + * + * @param allocator the allocator + * @param data the data address + * @param size the data size + * + * @return the new data address + */ + tb_pointer_t (*ralloc)(struct __tb_allocator_t* allocator, tb_pointer_t data, tb_size_t size __tb_debug_decl__); + + /*! free data + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ + tb_bool_t (*free)(struct __tb_allocator_t* allocator, tb_pointer_t data __tb_debug_decl__); + + /*! malloc large data + * + * @param allocator the allocator + * @param size the size + * @param real the real allocated size >= size, optional + * + * @return the data address + */ + tb_pointer_t (*large_malloc)(struct __tb_allocator_t* allocator, tb_size_t size, tb_size_t* real __tb_debug_decl__); + + /*! realloc large data + * + * @param allocator the allocator + * @param data the data address + * @param size the data size + * @param real the real allocated size >= size, optional + * + * @return the new data address + */ + tb_pointer_t (*large_ralloc)(struct __tb_allocator_t* allocator, tb_pointer_t data, tb_size_t size, tb_size_t* real __tb_debug_decl__); + + /*! free large data + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ + tb_bool_t (*large_free)(struct __tb_allocator_t* allocator, tb_pointer_t data __tb_debug_decl__); + + /*! clear allocator + * + * @param allocator the allocator + */ + tb_void_t (*clear)(struct __tb_allocator_t* allocator); + + /*! exit allocator + * + * @param allocator the allocator + */ + tb_void_t (*exit)(struct __tb_allocator_t* allocator); + +#ifdef __tb_debug__ + /*! dump allocator + * + * @param allocator the allocator + */ + tb_void_t (*dump)(struct __tb_allocator_t* allocator); + + /*! have this given data addess? + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ + tb_bool_t (*have)(struct __tb_allocator_t* allocator, tb_cpointer_t data); +#endif + +}tb_allocator_t, *tb_allocator_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the allocator + * + * @return the allocator + */ +tb_allocator_ref_t tb_allocator(); + +/*! the native allocator + * + * uses system memory directly + * + * @return the allocator + */ +tb_allocator_ref_t tb_allocator_native(tb_noarg_t); + +/*! the allocator type + * + * @param allocator the allocator + * + * @return the allocator type + */ +tb_size_t tb_allocator_type(tb_allocator_ref_t allocator); + +/*! malloc data + * + * @param allocator the allocator + * @param size the size + * + * @return the data address + */ +tb_pointer_t tb_allocator_malloc_(tb_allocator_ref_t allocator, tb_size_t size __tb_debug_decl__); + +/*! malloc data and fill zero + * + * @param allocator the allocator + * @param size the size + * + * @return the data address + */ +tb_pointer_t tb_allocator_malloc0_(tb_allocator_ref_t allocator, tb_size_t size __tb_debug_decl__); + +/*! malloc data with the item count + * + * @param allocator the allocator + * @param item the item count + * @param size the item size + * + * @return the data address + */ +tb_pointer_t tb_allocator_nalloc_(tb_allocator_ref_t allocator, tb_size_t item, tb_size_t size __tb_debug_decl__); + +/*! malloc data with the item count and fill zero + * + * @param allocator the allocator + * @param item the item count + * @param size the item size + * + * @return the data address + */ +tb_pointer_t tb_allocator_nalloc0_(tb_allocator_ref_t allocator, tb_size_t item, tb_size_t size __tb_debug_decl__); + +/*! realloc data + * + * @param allocator the allocator + * @param data the data address + * @param size the data size + * + * @return the new data address + */ +tb_pointer_t tb_allocator_ralloc_(tb_allocator_ref_t allocator, tb_pointer_t data, tb_size_t size __tb_debug_decl__); + +/*! free data + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_allocator_free_(tb_allocator_ref_t allocator, tb_pointer_t data __tb_debug_decl__); + +/*! malloc large data + * + * @param allocator the allocator + * @param size the size + * @param real the real allocated size >= size, optional + * + * @return the data address + */ +tb_pointer_t tb_allocator_large_malloc_(tb_allocator_ref_t allocator, tb_size_t size, tb_size_t* real __tb_debug_decl__); + +/*! malloc large data and fill zero + * + * @param allocator the allocator + * @param size the size + * @param real the real allocated size >= size, optional + * + * @return the data address + */ +tb_pointer_t tb_allocator_large_malloc0_(tb_allocator_ref_t allocator, tb_size_t size, tb_size_t* real __tb_debug_decl__); + +/*! malloc large data with the item count + * + * @param allocator the allocator + * @param item the item count + * @param size the item size + * @param real the real allocated size >= item * size, optional + * + * @return the data address + */ +tb_pointer_t tb_allocator_large_nalloc_(tb_allocator_ref_t allocator, tb_size_t item, tb_size_t size, tb_size_t* real __tb_debug_decl__); + +/*! malloc large data with the item count and fill zero + * + * @param allocator the allocator + * @param item the item count + * @param size the item size + * @param real the real allocated size >= item * size, optional + * + * @return the data address + */ +tb_pointer_t tb_allocator_large_nalloc0_(tb_allocator_ref_t allocator, tb_size_t item, tb_size_t size, tb_size_t* real __tb_debug_decl__); + +/*! realloc large data + * + * @param allocator the allocator + * @param data the data address + * @param size the data size + * @param real the real allocated size >= size, optional + * + * @return the new data address + */ +tb_pointer_t tb_allocator_large_ralloc_(tb_allocator_ref_t allocator, tb_pointer_t data, tb_size_t size, tb_size_t* real __tb_debug_decl__); + +/*! free large data + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_allocator_large_free_(tb_allocator_ref_t allocator, tb_pointer_t data __tb_debug_decl__); + +/*! align malloc data + * + * @param allocator the allocator + * @param size the size + * @param align the alignment bytes + * + * @return the data address + */ +tb_pointer_t tb_allocator_align_malloc_(tb_allocator_ref_t allocator, tb_size_t size, tb_size_t align __tb_debug_decl__); + +/*! align malloc data and fill zero + * + * @param allocator the allocator + * @param size the size + * @param align the alignment bytes + * + * @return the data address + */ +tb_pointer_t tb_allocator_align_malloc0_(tb_allocator_ref_t allocator, tb_size_t size, tb_size_t align __tb_debug_decl__); + +/*! align malloc data with the item count + * + * @param allocator the allocator + * @param item the item count + * @param size the item size + * @param align the alignment bytes + * + * @return the data address + */ +tb_pointer_t tb_allocator_align_nalloc_(tb_allocator_ref_t allocator, tb_size_t item, tb_size_t size, tb_size_t align __tb_debug_decl__); + +/*! align malloc data with the item count and fill zero + * + * @param allocator the allocator + * @param item the item count + * @param size the item size + * @param align the alignment bytes + * + * @return the data address + */ +tb_pointer_t tb_allocator_align_nalloc0_(tb_allocator_ref_t allocator, tb_size_t item, tb_size_t size, tb_size_t align __tb_debug_decl__); + +/*! align realloc data + * + * @param allocator the allocator + * @param data the data address + * @param size the data size + * + * @return the new data address + */ +tb_pointer_t tb_allocator_align_ralloc_(tb_allocator_ref_t allocator, tb_pointer_t data, tb_size_t size, tb_size_t align __tb_debug_decl__); + +/*! align free data + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_allocator_align_free_(tb_allocator_ref_t allocator, tb_pointer_t data __tb_debug_decl__); + +/*! clear it + * + * @param allocator the allocator + */ +tb_void_t tb_allocator_clear(tb_allocator_ref_t allocator); + +/*! exit it + * + * @param allocator the allocator + */ +tb_void_t tb_allocator_exit(tb_allocator_ref_t allocator); + +#ifdef __tb_debug__ +/*! dump it + * + * @param allocator the allocator + */ +tb_void_t tb_allocator_dump(tb_allocator_ref_t allocator); + +/*! have this given data addess? + * + * @param allocator the allocator + * @param data the data address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_allocator_have(tb_allocator_ref_t allocator, tb_cpointer_t data); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/buffer.h b/core/pkg/tbox.pkg/inc/tbox/memory/buffer.h index 2a7389bac..367d441cc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/buffer.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/buffer.h @@ -265,3 +265,270 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file buffer.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_BUFFER_H +#define TB_MEMORY_BUFFER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the buffer type +typedef struct __tb_buffer_t +{ + /// the buffer data + tb_byte_t* data; + + /// the buffer size + tb_size_t size; + + /// the buffer maxn + tb_size_t maxn; + + /// the static buffer +#ifdef __tb_small__ + tb_byte_t buff[32]; +#else + tb_byte_t buff[64]; +#endif + +}tb_buffer_t, *tb_buffer_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the buffer + * + * @param buffer the buffer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_buffer_init(tb_buffer_ref_t buffer); + +/*! exit the buffer + * + * @param buffer the buffer + */ +tb_void_t tb_buffer_exit(tb_buffer_ref_t buffer); + +/*! the buffer data + * + * @param buffer the buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_data(tb_buffer_ref_t buffer); + +/*! the buffer data size + * + * @param buffer the buffer + * + * @return the buffer data size + */ +tb_size_t tb_buffer_size(tb_buffer_ref_t buffer); + +/*! the buffer data maxn + * + * @param buffer the buffer + * + * @return the buffer data maxn + */ +tb_size_t tb_buffer_maxn(tb_buffer_ref_t buffer); + +/*! clear the buffer + * + * @param buffer the buffer + */ +tb_void_t tb_buffer_clear(tb_buffer_ref_t buffer); + +/*! resize the buffer size + * + * @param buffer the buffer + * @param size the new buffer size + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_resize(tb_buffer_ref_t buffer, tb_size_t size); + +/*! memset: b => 0 ... e + * + * @param buffer the buffer + * @param b the filled byte + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memset(tb_buffer_ref_t buffer, tb_byte_t b); + +/*! memset: b => p ... e + * + * @param buffer the buffer + * @param p the start position + * @param b the filled byte + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memsetp(tb_buffer_ref_t buffer, tb_size_t p, tb_byte_t b); + +/*! memset: b => 0 ... n + * + * @param buffer the buffer + * @param b the filled byte + * @param n the filled count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memnset(tb_buffer_ref_t buffer, tb_byte_t b, tb_size_t n); + +/*! memset: b => p ... n + * + * @param buffer the buffer + * @param p the start position + * @param b the filled byte + * @param n the filled count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memnsetp(tb_buffer_ref_t buffer, tb_size_t p, tb_byte_t b, tb_size_t n); + +/*! memcpy: b => 0 ... + * + * @param buffer the buffer + * @param b the copied buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memcpy(tb_buffer_ref_t buffer, tb_buffer_ref_t b); + +/*! memcpy: b => p ... + * + * @param buffer the buffer + * @param p the start position + * @param b the copied buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memcpyp(tb_buffer_ref_t buffer, tb_size_t p, tb_buffer_ref_t b); + +/*! memcpy: b ... n => 0 ... + * + * @param buffer the buffer + * @param b the copied buffer + * @param n the copied count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memncpy(tb_buffer_ref_t buffer, tb_byte_t const* b, tb_size_t n); + +/*! memcpy: b ... n => p ... + * + * @param buffer the buffer + * @param p the start position + * @param b the copied buffer + * @param n the copied count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memncpyp(tb_buffer_ref_t buffer, tb_size_t p, tb_byte_t const* b, tb_size_t n); + +/*! memmov: b ... e => 0 ... + * + * @param buffer the buffer + * @param b the moved start position + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memmov(tb_buffer_ref_t buffer, tb_size_t b); + +/*! memmov: b ... e => p ... + * + * @param buffer the buffer + * @param p the moved destination position + * @param b the moved start position + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memmovp(tb_buffer_ref_t buffer, tb_size_t p, tb_size_t b); + +/*! memmov: b ... n => 0 ... + * + * @param buffer the buffer + * @param b the moved start position + * @param n the moved count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memnmov(tb_buffer_ref_t buffer, tb_size_t b, tb_size_t n); + +/*! memmov: b ... n => p ... + * + * @param buffer the buffer + * @param p the moved destination position + * @param b the moved start position + * @param n the moved count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memnmovp(tb_buffer_ref_t buffer, tb_size_t p, tb_size_t b, tb_size_t n); + +/*! memcat: b +=> e ... + * + * @param buffer the buffer + * @param b the concated buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memcat(tb_buffer_ref_t buffer, tb_buffer_ref_t b); + +/*! memcat: b ... n +=> e ... + * + * @param buffer the buffer + * @param b the concated buffer + * @param n the concated count + * + * @return the buffer data address + */ +tb_byte_t* tb_buffer_memncat(tb_buffer_ref_t buffer, tb_byte_t const* b, tb_size_t n); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/default_allocator.h b/core/pkg/tbox.pkg/inc/tbox/memory/default_allocator.h index 445ce2335..a97dcbedd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/default_allocator.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/default_allocator.h @@ -87,3 +87,92 @@ tb_allocator_ref_t tb_default_allocator_init(tb_allocator_ref_t large_a __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file default_allocator.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_DEFAULT_ALLOCATOR_H +#define TB_MEMORY_DEFAULT_ALLOCATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "allocator.h" +#include "large_allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the global default allocator + * + * @param data the buffer data, uses the native buffer if be null + * @param size the buffer size + * + * @return the allocator + */ +tb_allocator_ref_t tb_default_allocator(tb_byte_t* data, tb_size_t size); + +/*! init the default allocator + * + * <pre> + * + * ---------------- ------------------------------------------------------- + * | native memory | or | data | + * ---------------- ------------------------------------------------------- + * | if data be null | + * `---------------------------------------> | + * | + * ----------------------------------------------------------------------------- + * | large allocator | + * ----------------------------------------------------------------------------- + * | | + * | --------------------------------------- + * | | small allocator | + * | --------------------------------------- + * | | + * ----------------------------------------------------------------------------- + * | >3KB | <=3KB | + * |-----------------------------------------------------------------------------| + * | default allocator | + * ----------------------------------------------------------------------------- + * + * @param large_allocator the large allocator, cannot be null + * + * @return the allocator + */ +tb_allocator_ref_t tb_default_allocator_init(tb_allocator_ref_t large_allocator); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/fixed_pool.h b/core/pkg/tbox.pkg/inc/tbox/memory/fixed_pool.h index 4458596b7..38a209f42 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/fixed_pool.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/fixed_pool.h @@ -235,3 +235,240 @@ tb_void_t tb_fixed_pool_dump(tb_fixed_pool_ref_t pool); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file fixed_pool.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_FIXED_POOL_H +#define TB_MEMORY_FIXED_POOL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "large_allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_fixed_pool_malloc(pool) tb_fixed_pool_malloc_(pool __tb_debug_vals__) +#define tb_fixed_pool_malloc0(pool) tb_fixed_pool_malloc0_(pool __tb_debug_vals__) +#define tb_fixed_pool_free(pool, item) tb_fixed_pool_free_(pool, (tb_pointer_t)(item) __tb_debug_vals__) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the fixed pool ref type + * + * <pre> + * + * current: + * ----------- + * | | + * -------------- | + * | slot |<-- + * |--------------| + * |||||||||||||||| + * |--------------| + * | | + * |--------------| + * | | + * |--------------| + * |||||||||||||||| + * |--------------| + * |||||||||||||||| + * |--------------| + * | | + * -------------- + * + * partial: + * + * -------------- -------------- -------------- + * | slot | <=> | slot | <=> ... <=> | slot | + * |--------------| |--------------| |--------------| + * |||||||||||||||| | | | | + * |--------------| |--------------| |--------------| + * | | |||||||||||||||| | | + * |--------------| |--------------| |--------------| + * | | |||||||||||||||| |||||||||||||||| + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| | | + * |--------------| |--------------| |--------------| + * |||||||||||||||| | | | | + * |--------------| |--------------| |--------------| + * | | | | |||||||||||||||| + * -------------- -------------- -------------- + * + * full: + * + * -------------- -------------- -------------- + * | slot | <=> | slot | <=> ... <=> | slot | + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| |||||||||||||||| + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| |||||||||||||||| + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| |||||||||||||||| + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| |||||||||||||||| + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| |||||||||||||||| + * |--------------| |--------------| |--------------| + * |||||||||||||||| |||||||||||||||| |||||||||||||||| + * -------------- -------------- -------------- + * + * slot: + * + * -------------- ------------------------>| + * | head | | + * |--------------| | + * ||| item | | + * |--------------| | + * ||| item | | + * |--------------| | data + * ||| item | | + * |--------------| | + * | ... | | + * |--------------| | + * ||| item | | + * -------------- ------------------------>| + * + * </pre> + */ +typedef __tb_typeref__(fixed_pool); + +/// the item init func type +typedef tb_bool_t (*tb_fixed_pool_item_init_func_t)(tb_pointer_t data, tb_cpointer_t priv); + +/// the item exit func type +typedef tb_void_t (*tb_fixed_pool_item_exit_func_t)(tb_pointer_t data, tb_cpointer_t priv); + +/// the item walk func type +typedef tb_bool_t (*tb_fixed_pool_item_walk_func_t)(tb_pointer_t data, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init fixed pool + * + * @param large_allocator the large allocator, uses the global allocator if be null + * @param slot_size the item count per-slot, using the default size if be zero + * @param item_size the item size + * @param item_init the item init func + * @param item_exit the item exit func + * @param priv the private data + * + * @return the pool + */ +tb_fixed_pool_ref_t tb_fixed_pool_init(tb_allocator_ref_t large_allocator, tb_size_t slot_size, tb_size_t item_size, tb_fixed_pool_item_init_func_t item_init, tb_fixed_pool_item_exit_func_t item_exit, tb_cpointer_t priv); + +/*! exit pool + * + * @param pool the pool + */ +tb_void_t tb_fixed_pool_exit(tb_fixed_pool_ref_t pool); + +/*! the item count + * + * @param pool the pool + * + * @return the item count + */ +tb_size_t tb_fixed_pool_size(tb_fixed_pool_ref_t pool); + +/*! the item size + * + * @param pool the pool + * + * @return the item size + */ +tb_size_t tb_fixed_pool_item_size(tb_fixed_pool_ref_t pool); + +/*! clear pool + * + * @param pool the pool + */ +tb_void_t tb_fixed_pool_clear(tb_fixed_pool_ref_t pool); + +/*! malloc data + * + * @param pool the pool + * + * @return the data + */ +tb_pointer_t tb_fixed_pool_malloc_(tb_fixed_pool_ref_t pool __tb_debug_decl__); + +/*! malloc data and clear it + * + * @param pool the pool + * + * @return the data + */ +tb_pointer_t tb_fixed_pool_malloc0_(tb_fixed_pool_ref_t pool __tb_debug_decl__); + +/*! free data + * + * @param pool the pool + * @param data the data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_fixed_pool_free_(tb_fixed_pool_ref_t pool, tb_pointer_t data __tb_debug_decl__); + +/*! walk item + * + * @code + tb_bool_t tb_fixed_pool_item_func(tb_pointer_t data, tb_cpointer_t priv) + { + // ok or break + return tb_true; + } + * @endcode + * + * @param pool the pool + * @param func the walk func + * @param priv the private data + */ +tb_void_t tb_fixed_pool_walk(tb_fixed_pool_ref_t pool, tb_fixed_pool_item_walk_func_t func, tb_cpointer_t priv); + +#ifdef __tb_debug__ +/*! dump pool + * + * @param pool the pool + */ +tb_void_t tb_fixed_pool_dump(tb_fixed_pool_ref_t pool); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/large_allocator.h b/core/pkg/tbox.pkg/inc/tbox/memory/large_allocator.h index 877199fc1..45b71b707 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/large_allocator.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/large_allocator.h @@ -71,3 +71,76 @@ tb_allocator_ref_t tb_large_allocator_init(tb_byte_t* data, tb_size_t size) __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file large_allocator.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_LARGE_ALLOCATOR_H +#define TB_MEMORY_LARGE_ALLOCATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the large allocator + * + * <pre> + * + * ------------------------- ------------------------ + * | native memory | | data | + * ------------------------- ------------------------ + * | | + * ------------------------- ------------------------ + * | native large allocator | | static large allocator | + * ------------------------- ------------------------ + * | | + * ------------------------------------------------------ + * | large allocator | + * ------------------------------------------------------ + * + * </pre> + * + * @param data the data, uses the native memory if be null + * @param size the size + * + * @return the allocator + */ +tb_allocator_ref_t tb_large_allocator_init(tb_byte_t* data, tb_size_t size); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/memory.h b/core/pkg/tbox.pkg/inc/tbox/memory/memory.h index 9fb592eff..9c6cd5435 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/memory.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/memory.h @@ -102,3 +102,107 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file memory.h + * @defgroup memory + * + */ +#ifndef TB_MEMORY_H +#define TB_MEMORY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "buffer.h" +#include "allocator.h" +#include "fixed_pool.h" +#include "string_pool.h" +#include "queue_buffer.h" +#include "static_buffer.h" +#include "large_allocator.h" +#include "small_allocator.h" +#include "native_allocator.h" +#include "static_allocator.h" +#include "default_allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * description + */ + +/*!architecture + * + * <pre> + * + * ---------------- ------------------------------------------------------- ---------------------- + * | native memory | or | data | <- | static allocator | + * ---------------- ------------------------------------------------------- ---------------------- + * | if data be null | + * `---------------------------------------> | + * | + * ----------------------------------------------------------------------------- ---------------------- ------ ------ + * | large allocator | -> | fixed pool:NB | -> | slot | -> | slot | -> ... + * ----------------------------------------------------------------------------- ---------------------- ------ ------ + * | | + * | --------------------------------------- ---------------------- ------ ------ + * | | small allocator | -> | fixed pool:16B | -> | slot | -> | slot | -> ... + * | --------------------------------------- |----------------------| ------ ------ + * | | | fixed pool:32B | -> ... + * | | |----------------------| + * | | | fixed pool:64B | -> ... + * | | |----------------------| + * | | | fixed pool:96B* | -> ... + * | | |----------------------| + * | | | fixed pool:128B | -> ... + * | | |----------------------| + * | | | fixed pool:192B* | -> ... + * | | |----------------------| + * | | | fixed pool:256B | -> ... + * | | |----------------------| + * | | | fixed pool:384B* | -> ... + * | | |----------------------| + * | | | fixed pool:512B | -> ... + * | | |----------------------| + * | | | fixed pool:1024B | -> ... + * | | |----------------------| + * | | | fixed pool:2048B | -> ... + * | | |----------------------| + * | | | fixed pool:3072B* | -> ... + * | | ---------------------- + * | | + * | | + * ------------------------------------------------------------------------------ + * | >3KB | <=3KB | + * |------------------------------------------------------------------------------| + * | default allocator | + * ------------------------------------------------------------------------------ + * | + * ---------------------- + * | string pool | + * ---------------------- + * + * </pre> + */ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/native_allocator.h b/core/pkg/tbox.pkg/inc/tbox/memory/native_allocator.h index e4989bfb5..b21840845 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/native_allocator.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/native_allocator.h @@ -52,3 +52,57 @@ tb_allocator_ref_t tb_native_allocator(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file native_allocator.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_NATIVE_ALLOCATOR_H +#define TB_MEMORY_NATIVE_ALLOCATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the global native allocator + * + * @return the allocator + */ +tb_allocator_ref_t tb_native_allocator(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/prefix.h b/core/pkg/tbox.pkg/inc/tbox/memory/prefix.h index ddf458fff..7a2319de4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_MEMORY_PREFIX_H +#define TB_MEMORY_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/queue_buffer.h b/core/pkg/tbox.pkg/inc/tbox/memory/queue_buffer.h index f5993cffc..2a3876e94 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/queue_buffer.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/queue_buffer.h @@ -224,3 +224,229 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file queue_buffer.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_QUEUE_BUFFER_H +#define TB_MEMORY_QUEUE_BUFFER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the queue buffer type +typedef struct __tb_queue_buffer_t +{ + // the buffer data + tb_byte_t* data; + + // the buffer head + tb_byte_t* head; + + // the buffer size + tb_size_t size; + + // the buffer maxn + tb_size_t maxn; + +}tb_queue_buffer_t, *tb_queue_buffer_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init buffer + * + * @param buffer the buffer + * @param maxn the buffer maxn + * + * @return tb_true or tb_false + */ +tb_bool_t tb_queue_buffer_init(tb_queue_buffer_ref_t buffer, tb_size_t maxn); + +/*! exit buffer + * + * @param buffer the buffer + */ +tb_void_t tb_queue_buffer_exit(tb_queue_buffer_ref_t buffer); + +/*! the buffer data + * + * @param buffer the buffer + * + * @return the buffer data + */ +tb_byte_t* tb_queue_buffer_data(tb_queue_buffer_ref_t buffer); + +/*! the buffer head + * + * @param buffer the buffer + * + * @return the buffer head + */ +tb_byte_t* tb_queue_buffer_head(tb_queue_buffer_ref_t buffer); + +/*! the buffer tail + * + * @param buffer the buffer + * + * @return the buffer tail + */ +tb_byte_t* tb_queue_buffer_tail(tb_queue_buffer_ref_t buffer); + +/*! the buffer maxn + * + * @param buffer the buffer + * + * @return the buffer maxn + */ +tb_size_t tb_queue_buffer_maxn(tb_queue_buffer_ref_t buffer); + +/*! the buffer size + * + * @param buffer the buffer + * + * @return the buffer size + */ +tb_size_t tb_queue_buffer_size(tb_queue_buffer_ref_t buffer); + +/*! the buffer left + * + * @param buffer the buffer + * + * @return the buffer left + */ +tb_size_t tb_queue_buffer_left(tb_queue_buffer_ref_t buffer); + +/*! the buffer full? + * + * @param buffer the buffer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_queue_buffer_full(tb_queue_buffer_ref_t buffer); + +/*! the buffer null? + * + * @param buffer the buffer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_queue_buffer_null(tb_queue_buffer_ref_t buffer); + +/*! clear buffer + * + * @param buffer the buffer + */ +tb_void_t tb_queue_buffer_clear(tb_queue_buffer_ref_t buffer); + +/*! resize buffer size + * + * @param buffer the buffer + * @param maxn the buffer maxn + * + * @return the buffer data + */ +tb_byte_t* tb_queue_buffer_resize(tb_queue_buffer_ref_t buffer, tb_size_t maxn); + +/*! skip buffer + * + * @param buffer the buffer + * @param size the skiped size + * + * @return the real size + */ +tb_long_t tb_queue_buffer_skip(tb_queue_buffer_ref_t buffer, tb_size_t size); + +/*! read buffer + * + * @param buffer the buffer + * @param data the data + * @param size the size + * + * @return the real size + */ +tb_long_t tb_queue_buffer_read(tb_queue_buffer_ref_t buffer, tb_byte_t* data, tb_size_t size); + +/*! writ buffer + * + * @param buffer the buffer + * @param data the data + * @param size the size + * + * @return the real size + */ +tb_long_t tb_queue_buffer_writ(tb_queue_buffer_ref_t buffer, tb_byte_t const* data, tb_size_t size); + +/*! init pull buffer for reading + * + * @param buffer the buffer + * @param size the size + * + * @return the data + */ +tb_byte_t* tb_queue_buffer_pull_init(tb_queue_buffer_ref_t buffer, tb_size_t* size); + +/*! exit pull buffer for reading + * + * @param buffer the buffer + * @param size the size + */ +tb_void_t tb_queue_buffer_pull_exit(tb_queue_buffer_ref_t buffer, tb_size_t size); + +/*! init push buffer for writing + * + * @param buffer the buffer + * @param size the size + * + * @return the data + */ +tb_byte_t* tb_queue_buffer_push_init(tb_queue_buffer_ref_t buffer, tb_size_t* size); + +/*! exit push buffer for writing + * + * @param buffer the buffer + * @param size the size + */ +tb_void_t tb_queue_buffer_push_exit(tb_queue_buffer_ref_t buffer, tb_size_t size); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/small_allocator.h b/core/pkg/tbox.pkg/inc/tbox/memory/small_allocator.h index 8203e8ff0..a90f2c57f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/small_allocator.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/small_allocator.h @@ -91,3 +91,96 @@ tb_allocator_ref_t tb_small_allocator_init(tb_allocator_ref_t large_all __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file small_allocator.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_SMALL_ALLOCATOR_H +#define TB_MEMORY_SMALL_ALLOCATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "large_allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the data size maximum +#define TB_SMALL_ALLOCATOR_DATA_MAXN (3072) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the small allocator only for size <=3KB + * + * <pre> + * + * -------------------------------------- + * | fixed pool: 16B | 1-16B | + * |--------------------------------------| + * | fixed pool: 32B | 17-32B | + * |--------------------------------------| + * | fixed pool: 64B | 33-64B | + * |--------------------------------------| + * | fixed pool: 96B* | 65-96B* | + * |--------------------------------------| + * | fixed pool: 128B | 97-128B | + * |--------------------------------------| + * | fixed pool: 192B* | 129-192B* | + * |--------------------------------------| + * | fixed pool: 256B | 193-256B | + * |--------------------------------------| + * | fixed pool: 384B* | 257-384B* | + * |--------------------------------------| + * | fixed pool: 512B | 385-512B | + * |--------------------------------------| + * | fixed pool: 1024B | 513-1024B | + * |--------------------------------------| + * | fixed pool: 2048B | 1025-2048B | + * |--------------------------------------| + * | fixed pool: 3072B* | 2049-3072B* | + * -------------------------------------- + * + * </pre> + * + * @param large_allocator the large allocator, uses the global allocator if be null + * + * @return the pool + */ +tb_allocator_ref_t tb_small_allocator_init(tb_allocator_ref_t large_allocator); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/static_allocator.h b/core/pkg/tbox.pkg/inc/tbox/memory/static_allocator.h index 463b03203..9dc01f072 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/static_allocator.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/static_allocator.h @@ -85,3 +85,90 @@ tb_void_t tb_static_allocator_exit(tb_allocator_ref_t allocator); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file static_allocator.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_STATIC_ALLOCATOR_H +#define TB_MEMORY_STATIC_ALLOCATOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "allocator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the global static allocator + * + * @note + * this allocator may be called before tb_init() + * + * @param data the allocator data + * @param size the allocator size + * + * @return the allocator + */ +tb_allocator_ref_t tb_static_allocator(tb_byte_t* data, tb_size_t size); + +/*! init the static allocator + * + * <pre> + * + * ----------------------------------------------------- + * | data | + * ----------------------------------------------------- + * | + * ----------------------------------------------------- + * | static allocator | + * ----------------------------------------------------- + * + * </pre> + * + * @param data the allocator data + * @param size the allocator size + * + * @return the allocator + */ +tb_allocator_ref_t tb_static_allocator_init(tb_byte_t* data, tb_size_t size); + +/*! exit the allocator + * + * @param allocator the allocator + */ +tb_void_t tb_static_allocator_exit(tb_allocator_ref_t allocator); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/static_buffer.h b/core/pkg/tbox.pkg/inc/tbox/memory/static_buffer.h index f44458007..f7687b3a6 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/static_buffer.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/static_buffer.h @@ -259,3 +259,264 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file static_buffer.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_STATIC_BUFFER_H +#define TB_MEMORY_STATIC_BUFFER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the static buffer type +typedef struct __tb_static_buffer_t +{ + // the buffer data + tb_byte_t* data; + + // the buffer size + tb_size_t size; + + // the buffer maxn + tb_size_t maxn; + +}tb_static_buffer_t, *tb_static_buffer_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the static buffer + * + * @param buffer the static buffer + * @param data the data + * @param maxn the data maxn + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_buffer_init(tb_static_buffer_ref_t buffer, tb_byte_t* data, tb_size_t maxn); + +/*! exit the static buffer + * + * @param buffer the static buffer + */ +tb_void_t tb_static_buffer_exit(tb_static_buffer_ref_t buffer); + +/*! the buffer data + * + * @param buffer the static buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_data(tb_static_buffer_ref_t buffer); + +/*! the buffer data size + * + * @param buffer the static buffer + * + * @return the buffer data size + */ +tb_size_t tb_static_buffer_size(tb_static_buffer_ref_t buffer); + +/*! the buffer data maxn + * + * @param buffer the static buffer + * + * @return the buffer data maxn + */ +tb_size_t tb_static_buffer_maxn(tb_static_buffer_ref_t buffer); + +/*! clear the buffer + * + * @param buffer the static buffer + */ +tb_void_t tb_static_buffer_clear(tb_static_buffer_ref_t buffer); + +/*! resize the buffer size + * + * @param buffer the static buffer + * @param size the new buffer size + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_resize(tb_static_buffer_ref_t buffer, tb_size_t size); + +/*! memset: b => 0 ... e + * + * @param buffer the static buffer + * @param b the filled byte + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memset(tb_static_buffer_ref_t buffer, tb_byte_t b); + +/*! memset: b => p ... e + * + * @param buffer the static buffer + * @param p the start position + * @param b the filled byte + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memsetp(tb_static_buffer_ref_t buffer, tb_size_t p, tb_byte_t b); + +/*! memset: b => 0 ... n + * + * @param buffer the static buffer + * @param b the filled byte + * @param n the filled count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memnset(tb_static_buffer_ref_t buffer, tb_byte_t b, tb_size_t n); + +/*! memset: b => p ... n + * + * @param buffer the static buffer + * @param p the start position + * @param b the filled byte + * @param n the filled count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memnsetp(tb_static_buffer_ref_t buffer, tb_size_t p, tb_byte_t b, tb_size_t n); + +/*! memcpy: b => 0 ... + * + * @param buffer the static buffer + * @param b the copied buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memcpy(tb_static_buffer_ref_t buffer, tb_static_buffer_ref_t b); + +/*! memcpy: b => p ... + * + * @param buffer the static buffer + * @param p the start position + * @param b the copied buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memcpyp(tb_static_buffer_ref_t buffer, tb_size_t p, tb_static_buffer_ref_t b); + +/*! memcpy: b ... n => 0 ... + * + * @param buffer the static buffer + * @param b the copied buffer + * @param n the copied count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memncpy(tb_static_buffer_ref_t buffer, tb_byte_t const* b, tb_size_t n); + +/*! memcpy: b ... n => p ... + * + * @param buffer the static buffer + * @param p the start position + * @param b the copied buffer + * @param n the copied count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memncpyp(tb_static_buffer_ref_t buffer, tb_size_t p, tb_byte_t const* b, tb_size_t n); + +/*! memmov: b ... e => 0 ... + * + * @param buffer the static buffer + * @param b the moved start position + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memmov(tb_static_buffer_ref_t buffer, tb_size_t b); + +/*! memmov: b ... e => p ... + * + * @param buffer the static buffer + * @param p the moved destination position + * @param b the moved start position + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memmovp(tb_static_buffer_ref_t buffer, tb_size_t p, tb_size_t b); + +/*! memmov: b ... n => 0 ... + * + * @param buffer the static buffer + * @param b the moved start position + * @param n the moved count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memnmov(tb_static_buffer_ref_t buffer, tb_size_t b, tb_size_t n); + +/*! memmov: b ... n => p ... + * + * @param buffer the static buffer + * @param p the moved destination position + * @param b the moved start position + * @param n the moved count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memnmovp(tb_static_buffer_ref_t buffer, tb_size_t p, tb_size_t b, tb_size_t n); + +/*! memcat: b +=> e ... + * + * @param buffer the static buffer + * @param b the concated buffer + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memcat(tb_static_buffer_ref_t buffer, tb_static_buffer_ref_t b); + +/*! memcat: b ... n +=> e ... + * + * @param buffer the static buffer + * @param b the concated buffer + * @param n the concated count + * + * @return the buffer data address + */ +tb_byte_t* tb_static_buffer_memncat(tb_static_buffer_ref_t buffer, tb_byte_t const* b, tb_size_t n); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/memory/string_pool.h b/core/pkg/tbox.pkg/inc/tbox/memory/string_pool.h index 8d4f3d465..3244bc510 100644 --- a/core/pkg/tbox.pkg/inc/tbox/memory/string_pool.h +++ b/core/pkg/tbox.pkg/inc/tbox/memory/string_pool.h @@ -99,3 +99,104 @@ tb_void_t tb_string_pool_dump(tb_string_pool_ref_t pool); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file string_pool.h + * @ingroup memory + * + */ +#ifndef TB_MEMORY_STRING_POOL_H +#define TB_MEMORY_STRING_POOL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the string pool ref type +typedef __tb_typeref__(string_pool); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init string pool for small, readonly and repeat strings + * + * readonly, strip repeat strings and decrease memory fragmens + * + * @param bcase is case? + * + * @return the string pool + */ +tb_string_pool_ref_t tb_string_pool_init(tb_bool_t bcase); + +/*! exit the string pool + * + * @param pool the string pool + */ +tb_void_t tb_string_pool_exit(tb_string_pool_ref_t pool); + +/*! clear the string pool + * + * @param pool the string pool + */ +tb_void_t tb_string_pool_clear(tb_string_pool_ref_t pool); + +/*! insert string to the pool and increase the reference count + * + * @param pool the string pool + * @param data the string data + * + * @return the string data + */ +tb_char_t const* tb_string_pool_insert(tb_string_pool_ref_t pool, tb_char_t const* data); + +/*! remove string from the pool if the reference count be zero + * + * @param pool the string pool + * @param data the string data + */ +tb_void_t tb_string_pool_remove(tb_string_pool_ref_t pool, tb_char_t const* data); + +#ifdef __tb_debug__ +/*! dump the string pool + * + * @param pool the string pool + */ +tb_void_t tb_string_pool_dump(tb_string_pool_ref_t pool); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/cookies.h b/core/pkg/tbox.pkg/inc/tbox/network/cookies.h index b71cdc40b..7bf19b10b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/cookies.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/cookies.h @@ -296,3 +296,301 @@ tb_void_t tb_cookies_dump(tb_cookies_ref_t cookies); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file cookies.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_HTTP_COOKIES_H +#define TB_NETWORK_HTTP_COOKIES_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * INTRODUCTION + * Cookies are a general mechanism which server side connections (such as CGI scripts) + * can use to both store and retrieve information on the client side of the connection. + * The addition of a simple, persistent, client-side state significantly extends the capabilities + * of Web-based client/server applications. + * + * OVERVIEW + * A server, when returning an HTTP object to a client, may also send a piece of state information + * which the client will store. Included in that state object is a description of the range of + * URLs for which that state is valid. Any future HTTP requests made by the client which fall + * in that range will include a transmittal of the current value of the state object from the client back to the server. + * The state object is called a cookie, for no compelling reason. + * This simple mechanism provides a powerful new tool which enables a host of new types of applications to + * be written for web-based environments. Shopping applications can now store information about the currently + * selected items, for fee services can send back registration information and free the client from + * retyping a user-id on next connection, sites can store per-user preferences on the client, + * and have the client supply those preferences every time that site is connected to. + * + * SPECIFICATION + * A cookie is introduced to the client by including a Set-Cookie header as part of an HTTP response, + * typically this will be generated by a CGI script. + * Syntax of the Set-Cookie HTTP Response Header + * This is the format a CGI script would use to add to the HTTP headers a new piece of data which is to be stored + * by the client for later retrieval. + * Set-Cookie: NAME=VALUE; expires=DATE; + * path=PATH; domain=DOMAIN_NAME; secure + * + * NAME=VALUE + * This string is a sequence of characters excluding semi-colon, comma and white space. + * If there is a need to place such data in the name or value, some encoding method such as URL style %XX encoding + * is recommended, though no encoding is defined or required. + * This is the only required attribute on the Set-Cookie header. + * + * expires=DATE + * The expires attribute specifies a date string that defines the valid life time of that cookie. + * Once the expiration date has been reached, the cookie will no longer be stored or given out. + * The date string is formatted as: + * + * Wdy, DD-Mon-YYYY HH:MM:SS GMT + * This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123, with the variations that the only legal time zone + * is GMT and the separators between the elements of the date must be dashes. + * expires is an optional attribute. If not specified, the cookie will expire when the user's session ends. + * + * Note: There is a bug in Netscape Navigator version 1.1 and earlier. Only cookies whose path attribute + * is set explicitly to "/" will be properly saved between sessions if they have an expires attribute. + * + * + * domain=DOMAIN_NAME + * When searching the cookie list for valid cookies, a comparison of the domain attributes of the cookie + * is made with the Internet domain name of the host from which the URL will be fetched. If there is a tail match, + * then the cookie will go through path matching to see if it should be sent. + * "Tail matching" means that domain attribute is matched against the tail of the fully qualified domain name + * of the host. A domain attribute of "acme.com" would match host names "anvil.acme.com" as well as + * "shipping.crate.acme.com". + * Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) + * or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us". + * Any domain that fails within one of the seven special top level domains listed below only require two periods. + * Any other domain requires at least three. The seven special top level domains are: + * "COM", "EDU", "NET", "ORG", "GOV", "MIL", and "INT". + * + * The default value of domain is the host name of the server which generated the cookie response. + * + * + * path=PATH + * The path attribute is used to specify the subset of URLs in a domain for which the cookie is valid. + * If a cookie has already passed domain matching, then the pathname component of the URL is compared with + * the path attribute, and if there is a match, the cookie is considered valid and is sent along with the URL request. + * The path "/foo" would match "/foobar" and "/foo/bar.html". The path "/" is the most general path. + * If the path is not specified, it as assumed to be the same path as the document being described + * by the header which contains the cookie. + * + * + * secure + * If a cookie is marked secure, it will only be transmitted if the communications channel with the host + * is a secure one. Currently this means that secure cookies will only be sent to HTTPS (HTTP over SSL) servers. + * If secure is not specified, a cookie is considered safe to be sent in the clear over unsecured channels. + * + * max-age + * A positive valueindicates that the cookie will expire after that many seconds have passed. + * Note that the value is the maximum age when the cookie will expire, not the cookie's current age. + * A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. + * A zero value causes the cookie to be deleted. + * The default value is -1 + * + * Syntax of the Cookie HTTP Request Header + * When requesting a URL from an HTTP server, the browser will match the URL against all cookies + * and if any of them match, a line containing the name/value pairs of all matching cookies will be included + * in the HTTP request. Here is the format of that line: + * Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2 ... + * + * Additional Notes + * Multiple Set-Cookie headers can be issued in a single server response. + * + * Instances of the same path and name will overwrit each other, with the latest instance taking precedence. + * Instances of the same path but different names will add additional mappings. + * + * Setting the path to a higher-level value does not override other more specific path mappings. + * If there are multiple matches for a given cookie name, but with separate paths, + * all the matching cookies will be sent. (See examples below.) + * + * The expires header lets the client know when it is safe to purge the mapping but the client is not required to do so. + * A client may also delete a cookie before it's expiration date arrives if the number of cookies exceeds + * its internal limits. + * + * When sending cookies to a server, all cookies with a more specific path mapping should be sent before cookies + * with less specific path mappings. For example, a cookie "name1=foo" with a path mapping of "/" should be + * sent after a cookie "name1=foo2" with a path mapping of "/bar" if they are both to be sent. + * + * There are limitations on the number of cookies that a client can store at any one time. + * This is a specification of the minimum number of cookies that a client should be prepared to receive and store. + * 300 total cookies + * 4 kilobytes per cookie, where the name and the OPAQUE_STRING combine to form the 4 kilobyte limit. + * 20 cookies per server or domain. (note that completely specified hosts and domains are treated as + * separate entities and have a 20 cookie limitation for each, not combined) + * Servers should not expect clients to be able to exceed these limits. When the 300 cookie limit + * or the 20 cookie per server limit is exceeded, clients should delete the least recently used cookie. + * When a cookie larger than 4 kilobytes is encountered the cookie should be trimmed to fit, + * but the name should remain intact as long as it is less than 4 kilobytes. + * + * If a CGI script wishes to delete a cookie, it can do so by returning a cookie with the same name, + * and an expires time which is in the past. The path and name must match exactly in order for the expiring cookie + * to replace the valid cookie. This requirement makes it difficult for anyone but the originator of a cookie + * to delete a cookie. + * + * When caching HTTP, as a proxy server might do, the Set-cookie response header should never be cached. + * + * If a proxy server receives a response which contains a Set-cookie header, + * it should propagate the Set-cookie header to the client, regardless of whether the response was 304 (Not Modified) + * or 200 (OK). + * Similarly, if a client request contains a Cookie: header, it should be forwarded through a proxy, + * even if the conditional If-modified-since request is being made. + * + * EXAMPLES + * Here are some sample exchanges which are designed to illustrate the use of cookies. + * First Example transaction sequence: + * Client requests a document, and receives in the response: + * Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT + * When client requests a URL in path "/" on this server, it sends: + * Cookie: CUSTOMER=WILE_E_COYOTE + * Client requests a document, and receives in the response: + * Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ + * When client requests a URL in path "/" on this server, it sends: + * Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 + * Client receives: + * Set-Cookie: SHIPPING=FEDEX; path=/foo + * When client requests a URL in path "/" on this server, it sends: + * Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 + * When client requests a URL in path "/foo" on this server, it sends: + * Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX + * Second Example transaction sequence: + * Assume all mappings from above have been cleared. + * + * Client receives: + * Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ + * When client requests a URL in path "/" on this server, it sends: + * Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001 + * Client receives: + * Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo + * When client requests a URL in path "/ammo" on this server, it sends: + * Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001 + * NOTE: There are two name/value pairs named "PART_NUMBER" due to the inheritance of the "/" mapping + * in addition to the "/ammo" mapping. + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../string/string.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the cookies ref type +typedef __tb_typeref__(cookies); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the cookies instances + * + * @return the cookies + */ +tb_cookies_ref_t tb_cookies(tb_noarg_t); + +/*! init cookies + * + * @return the cookies + */ +tb_cookies_ref_t tb_cookies_init(tb_noarg_t); + +/*! exit cookies + * + * @param cookies the cookies + */ +tb_void_t tb_cookies_exit(tb_cookies_ref_t cookies); + +/*! set cookies from the given domain and path + * + * @param cookies the cookies + * @param domain the domain, .e.g .xxx.com or xxx.com and compatible www.xxx.com + * @param path the path, .e.g /root/path + * @param secure is secure? + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_cookies_set(tb_cookies_ref_t cookies, tb_char_t const* domain, tb_char_t const* path, tb_bool_t secure, tb_char_t const* value); + +/*! set cookies from the given url + * + * @param cookies the cookies + * @param url the url + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_cookies_set_from_url(tb_cookies_ref_t cookies, tb_char_t const* url, tb_char_t const* value); + +/*! get cookies from the given domain and path + * + * @param cookies the cookies + * @param domain the domain, .e.g .xxx.com or xxx.com and compatible www.xxx.com + * @param path the path, .e.g /root/path + * @param secure is secure? + * @param value the cookies value + * + * @return the cookies data + */ +tb_char_t const* tb_cookies_get(tb_cookies_ref_t cookies, tb_char_t const* domain, tb_char_t const* path, tb_bool_t secure, tb_string_ref_t value); + +/*! get cookies from the given url + * + * @param cookies the cookies + * @param url the url + * @param value the cookies value + * + * @return the cookies data + */ +tb_char_t const* tb_cookies_get_from_url(tb_cookies_ref_t cookies, tb_char_t const* url, tb_string_ref_t value); + +/*! clear cookies + * + * @param cookies the cookies + */ +tb_void_t tb_cookies_clear(tb_cookies_ref_t cookies); + +#ifdef __tb_debug__ +/*! dump cookies + * + * @param cookies the cookies + */ +tb_void_t tb_cookies_dump(tb_cookies_ref_t cookies); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/dns/cache.h b/core/pkg/tbox.pkg/inc/tbox/network/dns/cache.h index c4de13bb0..6c905929a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/dns/cache.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/dns/cache.h @@ -63,3 +63,68 @@ tb_bool_t tb_dns_cache_get(tb_char_t const* name, tb_ipaddr_ref_t addr tb_void_t tb_dns_cache_set(tb_char_t const* name, tb_ipaddr_ref_t addr); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file cache.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_DNS_CACHE_H +#define TB_NETWORK_DNS_CACHE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the cache list + * + * not using ctime default + * + * @return tb_true or tb_false + */ +tb_bool_t tb_dns_cache_init(tb_noarg_t); + +/// exit the cache list +tb_void_t tb_dns_cache_exit(tb_noarg_t); + +/*! get addr from cache + * + * @param name the host name + * @param addr the host addr + * + * @return tb_true or tb_false + */ +tb_bool_t tb_dns_cache_get(tb_char_t const* name, tb_ipaddr_ref_t addr); + +/*! set addr to cache + * + * @param name the host name + * @param addr the host addr + */ +tb_void_t tb_dns_cache_set(tb_char_t const* name, tb_ipaddr_ref_t addr); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/dns/dns.h b/core/pkg/tbox.pkg/inc/tbox/network/dns/dns.h index 755042b22..499a2fb9b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/dns/dns.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/dns/dns.h @@ -35,3 +35,40 @@ #include "looker.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file dns.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_DNS_H +#define TB_NETWORK_DNS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "cache.h" +#include "server.h" +#include "looker.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/dns/looker.h b/core/pkg/tbox.pkg/inc/tbox/network/dns/looker.h index 215e7c9a5..fa2818e8e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/dns/looker.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/dns/looker.h @@ -86,3 +86,91 @@ tb_void_t tb_dns_looker_exit(tb_dns_looker_ref_t looker); tb_bool_t tb_dns_looker_done(tb_char_t const* name, tb_ipaddr_ref_t addr); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file looker.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_DNS_LOOKER_H +#define TB_NETWORK_DNS_LOOKER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the dns looker type +typedef __tb_typeref__(dns_looker); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init for looking ipv4 from the host name, non-block + * + * @param name the host name + * + * @return the looker handle + */ +tb_dns_looker_ref_t tb_dns_looker_init(tb_char_t const* name); + +/*! spak the looker + * + * @param looker the looker + * @param addr the address + * + * @return 1: ok, 0: continue: -1: failed + */ +tb_long_t tb_dns_looker_spak(tb_dns_looker_ref_t looker, tb_ipaddr_ref_t addr); + +/*! wait the looker + * + * @param looker the looker + * @param timeout the timeout + * + * @return 1: ok, 0: continue: -1: failed + */ +tb_long_t tb_dns_looker_wait(tb_dns_looker_ref_t looker, tb_long_t timeout); + +/*! exit the looker + * + * @param looker the looker + */ +tb_void_t tb_dns_looker_exit(tb_dns_looker_ref_t looker); + +/*! look address from the host name, block + * + * try to look it from cache first + * + * @param name the host name + * @param addr the address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_dns_looker_done(tb_char_t const* name, tb_ipaddr_ref_t addr); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/dns/prefix.h b/core/pkg/tbox.pkg/inc/tbox/network/dns/prefix.h index 8b3c16854..4a892b601 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/dns/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/dns/prefix.h @@ -179,3 +179,184 @@ static __tb_inline__ tb_char_t const* tb_dns_decode_name(tb_static_stream_ref_t } #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_NETWORK_DNS_PREFIX_H +#define TB_NETWORK_DNS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../ipaddr.h" +#include "../../utils/utils.h" +#include "../../stream/static_stream.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the protocol header +#define TB_DNS_HEADER_SIZE (12) +#define TB_DNS_HEADER_MAGIC (0xbeef) + +// the protocol port +#define TB_DNS_HOST_PORT (53) + +// the name maximum size +#define TB_DNS_NAME_MAXN (256) + +// the rpkt maximum size +#define TB_DNS_RPKT_MAXN (TB_DNS_HEADER_SIZE + TB_DNS_NAME_MAXN + 256) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the dns header type +typedef struct __tb_dns_header_t +{ + tb_uint16_t id; // identification number + + tb_uint16_t qr :1; // query/response flag + tb_uint16_t opcode :4; // purpose of message + tb_uint16_t aa :1; // authoritive answer + tb_uint16_t tc :1; // truncated message + tb_uint16_t rd :1; // recursion desired + + tb_uint16_t ra :1; // recursion available + tb_uint16_t z :1; // its z! reserved + tb_uint16_t ad :1; // authenticated data + tb_uint16_t cd :1; // checking disabled + tb_uint16_t rcode :4; // response code + + tb_uint16_t question; // number of question entries + tb_uint16_t answer; // number of answer entries + tb_uint16_t authority; // number of authority entries + tb_uint16_t resource; // number of resource entries + +}tb_dns_header_t; + +// the dns question type +typedef struct __tb_dns_question_t +{ + tb_uint16_t type; + tb_uint16_t class_; + +}tb_dns_question_t; + +// the dns resource type +typedef struct __tb_dns_resource_t +{ + tb_uint16_t type; + tb_uint16_t class_; + tb_uint32_t ttl; + tb_uint16_t size; + +}tb_dns_resource_t; + +// the dns answer type +typedef struct __tb_dns_answer_t +{ + tb_char_t name[TB_DNS_NAME_MAXN]; + tb_dns_resource_t res; + tb_byte_t const* rdata; + +}tb_dns_answer_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +// size + data, e.g. .www.google.com => 3www6google3com +static __tb_inline__ tb_char_t const* tb_dns_encode_name(tb_char_t* name) +{ + tb_assert_and_check_return_val(name && name[0] == '.', tb_null); + + // encode + tb_byte_t n = 0; + tb_char_t* b = name; + tb_char_t* p = name + 1; + while (*p) + { + if (*p == '.') + { + //*b = '0' + n; + *b = (tb_char_t)n; + n = 0; + b = p; + } + else n++; + p++; + } + //*b = '0' + n; + *b = n; + + // ok + return name; +} +static __tb_inline__ tb_char_t const* tb_dns_decode_name_impl(tb_char_t const* sb, tb_char_t const* se, tb_char_t const* ps, tb_char_t** pd) +{ + tb_char_t const* p = ps; + tb_char_t* q = *pd; + while (p < se) + { + tb_byte_t c = *p++; + if (!c) break; + // is pointer? 11xxxxxx xxxxxxxx + else if (c >= 0xc0) + { + tb_uint16_t pos = c; + pos &= ~0xc0; + pos <<= 8; + pos |= *p++; + tb_dns_decode_name_impl(sb, se, sb + pos, &q); + break; + } + // is ascii? 00xxxxxx + else + { + while (c--) *q++ = *p++; + *q++ = '.'; + } + } + *pd = q; + return p; +} +static __tb_inline__ tb_char_t const* tb_dns_decode_name(tb_static_stream_ref_t sstream, tb_char_t* name) +{ + tb_char_t* q = name; + tb_char_t* p = (tb_char_t*)tb_dns_decode_name_impl((tb_char_t const*)tb_static_stream_beg(sstream), (tb_char_t const*)tb_static_stream_end(sstream), (tb_char_t const*)tb_static_stream_pos(sstream), &q); + if (p) + { + tb_assert(q - name < TB_DNS_NAME_MAXN); + if (q > name && *(q - 1) == '.') *--q = '\0'; + tb_static_stream_goto(sstream, (tb_byte_t*)p); + return name; + } + else return tb_null; +} + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/dns/server.h b/core/pkg/tbox.pkg/inc/tbox/network/dns/server.h index 81f0fa7e3..e85eef37b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/dns/server.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/dns/server.h @@ -75,3 +75,80 @@ tb_void_t tb_dns_server_add(tb_char_t const* addr); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file server.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_DNS_SERVER_H +#define TB_NETWORK_DNS_SERVER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the server list + * + * @return tb_true or tb_false + */ +tb_bool_t tb_dns_server_init(tb_noarg_t); + +/// exit the server list +tb_void_t tb_dns_server_exit(tb_noarg_t); + +/// dump the server list +tb_void_t tb_dns_server_dump(tb_noarg_t); + +/// sort the server list by the response speed +tb_void_t tb_dns_server_sort(tb_noarg_t); + +/*! get the server + * + * @param addr the server address list, addr[0] is the fastest + * + * @return the server size + */ +tb_size_t tb_dns_server_get(tb_ipaddr_t addr[2]); + +/*! add the server + * + * @param addr the server address + */ +tb_void_t tb_dns_server_add(tb_char_t const* addr); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/http.h b/core/pkg/tbox.pkg/inc/tbox/network/http.h index f7b31030e..a07973ff2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/http.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/http.h @@ -415,3 +415,420 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file http.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_HTTP_H +#define TB_NETWORK_HTTP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "cookies.h" +#include "url.h" +#include "../string/string.h" +#include "../container/container.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the http option code: get +#define TB_HTTP_OPTION_CODE_GET(x) ((x) + 1) + +/// the http option code: set +#define TB_HTTP_OPTION_CODE_SET(x) (0xff00 | ((x) + 1)) + +/// the http option code is setter? +#define TB_HTTP_OPTION_CODE_IS_SET(x) ((x) & 0xff00) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the http method enum +typedef enum __tb_http_method_e +{ + TB_HTTP_METHOD_GET = 0 +, TB_HTTP_METHOD_POST = 1 +, TB_HTTP_METHOD_HEAD = 2 +, TB_HTTP_METHOD_PUT = 3 +, TB_HTTP_METHOD_OPTIONS = 4 +, TB_HTTP_METHOD_DELETE = 5 +, TB_HTTP_METHOD_TRACE = 6 +, TB_HTTP_METHOD_CONNECT = 7 + +}tb_http_method_e; + +/// the http code enum +typedef enum __tb_http_code_e +{ + TB_HTTP_CODE_CONTINUE = 100 +, TB_HTTP_CODE_SWITCHING_PROTOCOLS = 101 +, TB_HTTP_CODE_PROCESSING = 102 + +, TB_HTTP_CODE_OK = 200 +, TB_HTTP_CODE_CREATED = 201 +, TB_HTTP_CODE_ACCEPTED = 202 +, TB_HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203 +, TB_HTTP_CODE_NO_CONTENT = 204 +, TB_HTTP_CODE_RESET_CONTENT = 205 +, TB_HTTP_CODE_PARTIAL_CONTENT = 206 +, TB_HTTP_CODE_MULTI_STATUS = 207 + +, TB_HTTP_CODE_MULTIPLE_CHOICES = 300 +, TB_HTTP_CODE_MOVED_PERMANENTLY = 301 +, TB_HTTP_CODE_MOVED_TEMPORARILY = 302 +, TB_HTTP_CODE_SEE_OTHER = 303 +, TB_HTTP_CODE_NOT_MODIFIED = 304 +, TB_HTTP_CODE_USE_PROXY = 305 +, TB_HTTP_CODE_SWITCH_PROXY = 306 +, TB_HTTP_CODE_TEMPORARY_REDIRECT = 307 + +, TB_HTTP_CODE_BAD_REQUEST = 400 +, TB_HTTP_CODE_UNAUTHORIZED = 401 +, TB_HTTP_CODE_FORBIDDEN = 403 +, TB_HTTP_CODE_NOT_FOUND = 404 +, TB_HTTP_CODE_METHOD_NOT_ALLOWED = 405 +, TB_HTTP_CODE_NOT_ACCEPTABLE = 406 +, TB_HTTP_CODE_REQUEST_TIMEOUT = 408 +, TB_HTTP_CODE_CONFLICT = 409 +, TB_HTTP_CODE_GONE = 410 +, TB_HTTP_CODE_LENGTH_REQUIRED = 411 +, TB_HTTP_CODE_PRECONDITION_FAILED = 412 +, TB_HTTP_CODE_REQUEST_ENTITY_TOO_LONG = 413 +, TB_HTTP_CODE_REQUEST_URI_TOO_LONG = 414 +, TB_HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415 +, TB_HTTP_CODE_RANGE_NOT_SATISFIABLE = 416 +, TB_HTTP_CODE_EXPECTATION_FAILED = 417 +, TB_HTTP_CODE_UNPROCESSABLE_ENTITY = 422 +, TB_HTTP_CODE_LOCKED = 423 +, TB_HTTP_CODE_FAILED_DEPENDENCY = 424 +, TB_HTTP_CODE_UNORDERED_COLLECTION = 425 +, TB_HTTP_CODE_UPGRADE_REQUIRED = 426 +, TB_HTTP_CODE_RETRY_WITH = 449 + +, TB_HTTP_CODE_INTERNAL_SERVER_ERROR = 500 +, TB_HTTP_CODE_NOT_IMPLEMENTED = 501 +, TB_HTTP_CODE_BAD_GATEWAY = 502 +, TB_HTTP_CODE_SERVICE_UNAVAILABLE = 503 +, TB_HTTP_CODE_GATEWAY_TIMEOUT = 504 +, TB_HTTP_CODE_INSUFFICIENT_STORAGE = 507 +, TB_HTTP_CODE_LOOP_DETECTED = 508 +, TB_HTTP_CODE_NOT_EXTENDED = 510 + +}tb_http_code_e; + +/// the http option enum +typedef enum __tb_http_option_e +{ + TB_HTTP_OPTION_NONE = 0 + +, TB_HTTP_OPTION_GET_SSL = TB_HTTP_OPTION_CODE_GET(1) +, TB_HTTP_OPTION_GET_URL = TB_HTTP_OPTION_CODE_GET(2) +, TB_HTTP_OPTION_GET_HOST = TB_HTTP_OPTION_CODE_GET(3) +, TB_HTTP_OPTION_GET_PORT = TB_HTTP_OPTION_CODE_GET(4) +, TB_HTTP_OPTION_GET_PATH = TB_HTTP_OPTION_CODE_GET(5) +, TB_HTTP_OPTION_GET_HEAD = TB_HTTP_OPTION_CODE_GET(6) +, TB_HTTP_OPTION_GET_RANGE = TB_HTTP_OPTION_CODE_GET(7) +, TB_HTTP_OPTION_GET_METHOD = TB_HTTP_OPTION_CODE_GET(8) +, TB_HTTP_OPTION_GET_VERSION = TB_HTTP_OPTION_CODE_GET(9) +, TB_HTTP_OPTION_GET_TIMEOUT = TB_HTTP_OPTION_CODE_GET(10) +, TB_HTTP_OPTION_GET_COOKIES = TB_HTTP_OPTION_CODE_GET(11) +, TB_HTTP_OPTION_GET_REDIRECT = TB_HTTP_OPTION_CODE_GET(12) +, TB_HTTP_OPTION_GET_HEAD_FUNC = TB_HTTP_OPTION_CODE_GET(13) +, TB_HTTP_OPTION_GET_HEAD_PRIV = TB_HTTP_OPTION_CODE_GET(14) +, TB_HTTP_OPTION_GET_AUTO_UNZIP = TB_HTTP_OPTION_CODE_GET(15) +, TB_HTTP_OPTION_GET_POST_URL = TB_HTTP_OPTION_CODE_GET(16) +, TB_HTTP_OPTION_GET_POST_DATA = TB_HTTP_OPTION_CODE_GET(17) +, TB_HTTP_OPTION_GET_POST_FUNC = TB_HTTP_OPTION_CODE_GET(18) +, TB_HTTP_OPTION_GET_POST_PRIV = TB_HTTP_OPTION_CODE_GET(19) +, TB_HTTP_OPTION_GET_POST_LRATE = TB_HTTP_OPTION_CODE_GET(20) + +, TB_HTTP_OPTION_SET_SSL = TB_HTTP_OPTION_CODE_SET(1) +, TB_HTTP_OPTION_SET_URL = TB_HTTP_OPTION_CODE_SET(2) +, TB_HTTP_OPTION_SET_HOST = TB_HTTP_OPTION_CODE_SET(3) +, TB_HTTP_OPTION_SET_PORT = TB_HTTP_OPTION_CODE_SET(4) +, TB_HTTP_OPTION_SET_PATH = TB_HTTP_OPTION_CODE_SET(5) +, TB_HTTP_OPTION_SET_HEAD = TB_HTTP_OPTION_CODE_SET(6) +, TB_HTTP_OPTION_SET_RANGE = TB_HTTP_OPTION_CODE_SET(7) +, TB_HTTP_OPTION_SET_METHOD = TB_HTTP_OPTION_CODE_SET(8) +, TB_HTTP_OPTION_SET_VERSION = TB_HTTP_OPTION_CODE_SET(9) +, TB_HTTP_OPTION_SET_TIMEOUT = TB_HTTP_OPTION_CODE_SET(10) +, TB_HTTP_OPTION_SET_COOKIES = TB_HTTP_OPTION_CODE_SET(11) +, TB_HTTP_OPTION_SET_REDIRECT = TB_HTTP_OPTION_CODE_SET(12) +, TB_HTTP_OPTION_SET_HEAD_FUNC = TB_HTTP_OPTION_CODE_SET(13) +, TB_HTTP_OPTION_SET_HEAD_PRIV = TB_HTTP_OPTION_CODE_SET(14) +, TB_HTTP_OPTION_SET_AUTO_UNZIP = TB_HTTP_OPTION_CODE_SET(15) +, TB_HTTP_OPTION_SET_POST_URL = TB_HTTP_OPTION_CODE_SET(16) +, TB_HTTP_OPTION_SET_POST_DATA = TB_HTTP_OPTION_CODE_SET(17) +, TB_HTTP_OPTION_SET_POST_FUNC = TB_HTTP_OPTION_CODE_SET(18) +, TB_HTTP_OPTION_SET_POST_PRIV = TB_HTTP_OPTION_CODE_SET(19) +, TB_HTTP_OPTION_SET_POST_LRATE = TB_HTTP_OPTION_CODE_SET(20) + +}tb_http_option_e; + +/// the http range type +typedef struct __tb_http_range_t +{ + /// the begin offset + tb_hize_t bof; + + /// the end offset + tb_hize_t eof; + +}tb_http_range_t; + +/// the http ref type +typedef __tb_typeref__(http); + +/*! the http head func type + * + * @param line the http head line + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it + */ +typedef tb_bool_t (*tb_http_head_func_t)(tb_char_t const* line, tb_cpointer_t priv); + +/*! the http post func type + * + * @param offset the istream offset + * @param size the istream size, no size: -1 + * @param save the saved size + * @param rate the current rate, bytes/s + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it + */ +typedef tb_bool_t (*tb_http_post_func_t)(tb_size_t state, tb_hize_t offset, tb_hong_t size, tb_hize_t save, tb_size_t rate, tb_cpointer_t priv); + +/// the http option type +typedef struct __tb_http_option_t +{ + /// the method + tb_uint16_t method : 4; + + /// auto unzip for gzip encoding? + tb_uint16_t bunzip : 1; + + /// the http version, 0: HTTP/1.0, 1: HTTP/1.1 + tb_uint16_t version : 1; + + /// the redirect maxn + tb_uint16_t redirect : 10; + + /// the url + tb_url_t url; + + /// timeout: ms + tb_long_t timeout; + + /// range + tb_http_range_t range; + + /// the cookies + tb_cookies_ref_t cookies; + + /// the priv data + tb_pointer_t head_priv; + + /// the head func + tb_http_head_func_t head_func; + + /// the head data + tb_buffer_t head_data; + + /// the post url + tb_url_t post_url; + + /// the post data + tb_byte_t const* post_data; + + /// the post size + tb_size_t post_size; + + /// the post func + tb_http_post_func_t post_func; + + /// the post data + tb_cpointer_t post_priv; + + /// the post limit rate + tb_size_t post_lrate; + +}tb_http_option_t; + +/// the http status type +typedef struct __tb_http_status_t +{ + /// the http code + tb_uint16_t code : 10; + + /// the http version + tb_uint16_t version : 1; + + /// keep alive? + tb_uint16_t balived : 1; + + /// be able to seek? + tb_uint16_t bseeked : 1; + + /// is chunked? + tb_uint16_t bchunked : 1; + + /// is gzip? + tb_uint16_t bgzip : 1; + + /// is deflate? + tb_uint16_t bdeflate : 1; + + /// the state + tb_size_t state; + + /// the document size + tb_hong_t document_size; + + /// the current content size, maybe in range + tb_hong_t content_size; + + /// the content type + tb_string_t content_type; + + /// the location + tb_string_t location; + +}tb_http_status_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init http + * + * return the http + */ +tb_http_ref_t tb_http_init(tb_noarg_t); + +/*! exit http + * + * @param http the http + */ +tb_void_t tb_http_exit(tb_http_ref_t http); + +/*! kill http + * + * @param http the http + */ +tb_void_t tb_http_kill(tb_http_ref_t http); + +/*! wait the http + * + * blocking wait the single event object, so need not aiop + * return the event type if ok, otherwise return 0 for timeout + * + * @param http the http + * @param events the events + * @param timeout the timeout value, return immediately if 0, infinity if -1 + * + * @return the event type, return 0 if timeout, return -1 if error + */ +tb_long_t tb_http_wait(tb_http_ref_t http, tb_size_t events, tb_long_t timeout); + +/*! open the http + * + * @param http the http + * + * @return tb_true or tb_false + */ +tb_bool_t tb_http_open(tb_http_ref_t http); + +/*! close http + * + * @param http the http + * + * @return tb_true or tb_false + */ +tb_bool_t tb_http_clos(tb_http_ref_t http); + +/*! seek http + * + * @param http the http + * @param offset the offset + * + * @return tb_true or tb_false + */ +tb_bool_t tb_http_seek(tb_http_ref_t http, tb_hize_t offset); + +/*! read data, non-blocking + * + * @param http the http + * @param data the data + * @param size the size + * + * @return ok: real size, continue: 0, fail: -1 + */ +tb_long_t tb_http_read(tb_http_ref_t http, tb_byte_t* data, tb_size_t size); + +/*! read data, blocking + * + * @param http the http + * @param data the data + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_http_bread(tb_http_ref_t http, tb_byte_t* data, tb_size_t size); + +/*! ctrl the http option + * + * @param http the http + * @param option the ctrl option + * + * @return tb_true or tb_false + */ +tb_bool_t tb_http_ctrl(tb_http_ref_t http, tb_size_t option, ...); + +/*! the http status + * + * @param http the http + * + * @return the http status + */ +tb_http_status_t const* tb_http_status(tb_http_ref_t http); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/network/hwaddr.h b/core/pkg/tbox.pkg/inc/tbox/network/hwaddr.h index 04013efce..12da6eb10 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/hwaddr.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/hwaddr.h @@ -109,3 +109,114 @@ tb_bool_t tb_hwaddr_cstr_set(tb_hwaddr_ref_t hwaddr, tb_char_t const* __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file hwaddr.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_HWADDR_H +#define TB_NETWORK_HWADDR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the hwaddr string data maxn +#define TB_HWADDR_CSTR_MAXN (18) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the hardware address type + * + * mac: xx:xx:xx:xx:xx:xx + */ +typedef struct __tb_hwaddr_t +{ + /// u8 + tb_byte_t u8[6]; + +}tb_hwaddr_t, *tb_hwaddr_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! clear the hwaddr + * + * @param hwaddr the hwaddr + */ +tb_void_t tb_hwaddr_clear(tb_hwaddr_ref_t hwaddr); + +/*! copy address, faster than *hwaddr = *other + * + * @param hwaddr the address + * @param copied the copied address + */ +tb_void_t tb_hwaddr_copy(tb_hwaddr_ref_t hwaddr, tb_hwaddr_ref_t copied); + +/*! is equal? + * + * @param hwaddr the hwaddr + * @param other the other hwaddr + * + * @return tb_true or tb_false + */ +tb_bool_t tb_hwaddr_is_equal(tb_hwaddr_ref_t hwaddr, tb_hwaddr_ref_t other); + +/*! get the hwaddr string + * + * @param hwaddr the hwaddr + * @param data the hwaddr data + * @param maxn the data maxn + * + * @return the hwaddr address + */ +tb_char_t const* tb_hwaddr_cstr(tb_hwaddr_ref_t hwaddr, tb_char_t* data, tb_size_t maxn); + +/*! set the hwaddr from string + * + * @param hwaddr the hwaddr + * @param cstr the hwaddr string + * + * @return tb_true or tb_false + */ +tb_bool_t tb_hwaddr_cstr_set(tb_hwaddr_ref_t hwaddr, tb_char_t const* cstr); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/ipaddr.h b/core/pkg/tbox.pkg/inc/tbox/network/ipaddr.h index 66072b324..cb85e4714 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/ipaddr.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/ipaddr.h @@ -269,3 +269,274 @@ tb_void_t tb_ipaddr_port_set(tb_ipaddr_ref_t ipaddr, tb_uint16_t port) __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ipaddr.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_IPADDR_H +#define TB_NETWORK_IPADDR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "ipv4.h" +#include "ipv6.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the address string data maxn +#define TB_IPADDR_CSTR_MAXN (TB_IPV6_CSTR_MAXN + 20) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the ip address family enum +typedef enum __tb_ipaddr_family_e +{ + TB_IPADDR_FAMILY_NONE = 0 +, TB_IPADDR_FAMILY_IPV4 = 1 +, TB_IPADDR_FAMILY_IPV6 = 2 + +}tb_ipaddr_family_e; + +/// the ip address type +typedef struct __tb_ipaddr_t +{ + /// the family + tb_uint8_t family : 7; + + /// have ip? + tb_uint8_t have_ip : 1; + + /// the port + tb_uint16_t port; + + /// the address + union + { + /// the ipv4 + tb_ipv4_t ipv4; + + /// the ipv6 + tb_ipv6_t ipv6; + + }u; + +}tb_ipaddr_t, *tb_ipaddr_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! clear the address + * + * @param ipaddr the address + */ +tb_void_t tb_ipaddr_clear(tb_ipaddr_ref_t ipaddr); + +/*! copy address, faster than *ipaddr = *other + * + * @param ipaddr the address + * @param copied the copied address + */ +tb_void_t tb_ipaddr_copy(tb_ipaddr_ref_t ipaddr, tb_ipaddr_ref_t copied); + +/*! is empty? + * + * @param ipaddr the address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_is_empty(tb_ipaddr_ref_t ipaddr); + +/*! is equal? + * + * @param ipaddr the address + * @param other the other address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_is_equal(tb_ipaddr_ref_t ipaddr, tb_ipaddr_ref_t other); + +/*! get the address string + * + * @param ipaddr the address + * @param data the address string data + * @param maxn the address string data maxn + * + * @return the address string + */ +tb_char_t const* tb_ipaddr_cstr(tb_ipaddr_ref_t ipaddr, tb_char_t* data, tb_size_t maxn); + +/*! set the ip address from string + * + * @param ipaddr the address, only analyze format if be null + * @param cstr the address string, init any address if be null + * @param port the port, optional + * @param family the address family, will analyze family automaticly if be none, optional + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_set(tb_ipaddr_ref_t ipaddr, tb_char_t const* cstr, tb_uint16_t port, tb_uint8_t family); + +/*! clear ip + * + * @param ipaddr the address + */ +tb_void_t tb_ipaddr_ip_clear(tb_ipaddr_ref_t ipaddr); + +/*! the ip is empty? + * + * @param ipaddr the address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_ip_is_empty(tb_ipaddr_ref_t ipaddr); + +/*! the ip is any? + * + * @param ipaddr the address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_ip_is_any(tb_ipaddr_ref_t ipaddr); + +/*! the ip is loopback? + * + * @param ipaddr the address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_ip_is_loopback(tb_ipaddr_ref_t ipaddr); + +/*! the ip is equal? + * + * @param ipaddr the address + * @param other the other address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_ip_is_equal(tb_ipaddr_ref_t ipaddr, tb_ipaddr_ref_t other); + +/*! get the ip address string + * + * @param ipaddr the address + * @param data the address string data + * @param maxn the address string data maxn + * + * @return the address string + */ +tb_char_t const* tb_ipaddr_ip_cstr(tb_ipaddr_ref_t ipaddr, tb_char_t* data, tb_size_t maxn); + +/*! set the ip address from string + * + * @param ipaddr the address, only analyze format if be null + * @param cstr the address string + * @param family the address family, will analyze family automaticly if be none + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipaddr_ip_cstr_set(tb_ipaddr_ref_t ipaddr, tb_char_t const* cstr, tb_uint8_t family); + +/*! only set ip address + * + * @param ipaddr the address + * @param other the other address with ip data, clear it if be null + */ +tb_void_t tb_ipaddr_ip_set(tb_ipaddr_ref_t ipaddr, tb_ipaddr_ref_t other); + +/*! get the ipv4 address + * + * @param ipaddr the address + * + * @return the ipv4 + */ +tb_ipv4_ref_t tb_ipaddr_ipv4(tb_ipaddr_ref_t ipaddr); + +/*! set the address from ipv4 + * + * @param ipaddr the address + * @param ipv4 the ipv4, clear it if be null + */ +tb_void_t tb_ipaddr_ipv4_set(tb_ipaddr_ref_t ipaddr, tb_ipv4_ref_t ipv4); + +/*! get the ipv6 address + * + * @param ipaddr the address + * + * @return the ipv6 + */ +tb_ipv6_ref_t tb_ipaddr_ipv6(tb_ipaddr_ref_t ipaddr); + +/*! set the address from ipv6 + * + * @param ipaddr the address + * @param ipv6 the ipv6, clear it if be null + */ +tb_void_t tb_ipaddr_ipv6_set(tb_ipaddr_ref_t ipaddr, tb_ipv6_ref_t ipv6); + +/*! get the address family + * + * @param ipaddr the address + * + * @return the family + */ +tb_size_t tb_ipaddr_family(tb_ipaddr_ref_t ipaddr); + +/*! set the address family + * + * @param ipaddr the address + * @param family the family + */ +tb_void_t tb_ipaddr_family_set(tb_ipaddr_ref_t ipaddr, tb_size_t family); + +/*! get the address port + * + * @param ipaddr the address + * + * @return the port + */ +tb_uint16_t tb_ipaddr_port(tb_ipaddr_ref_t ipaddr); + +/*! set the address family + * + * @param ipaddr the address + * @param port the port + */ +tb_void_t tb_ipaddr_port_set(tb_ipaddr_ref_t ipaddr, tb_uint16_t port); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/ipv4.h b/core/pkg/tbox.pkg/inc/tbox/network/ipv4.h index af27912dc..cb4b3fe46 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/ipv4.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/ipv4.h @@ -124,3 +124,129 @@ tb_bool_t tb_ipv4_cstr_set(tb_ipv4_ref_t ipv4, tb_char_t const* cstr); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ipv4.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_IPV4_H +#define TB_NETWORK_IPV4_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the ipv4 string data maxn +#define TB_IPV4_CSTR_MAXN (16) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the ipv4 type + * + * xxx.xxx.xxx.xxx + */ +typedef union __tb_ipv4_t +{ + /// u32, little-endian + tb_uint32_t u32; + + /// u16 + tb_uint16_t u16[2]; + + /// u8 + tb_uint8_t u8[4]; + +}tb_ipv4_t, *tb_ipv4_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! clear the ipv4 + * + * @param ipv4 the ipv4 + */ +tb_void_t tb_ipv4_clear(tb_ipv4_ref_t ipv4); + +/*! is any address? + * + * @param ipv4 the ipv4 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv4_is_any(tb_ipv4_ref_t ipv4); + +/*! is loopback address? + * + * @param ipv4 the ipv4 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv4_is_loopback(tb_ipv4_ref_t ipv4); + +/*! is equal? + * + * @param ipv4 the ipv4 + * @param other the other ipv4 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv4_is_equal(tb_ipv4_ref_t ipv4, tb_ipv4_ref_t other); + +/*! get the ipv4 string + * + * @param ipv4 the ipv4 + * @param data the ipv4 data + * @param maxn the data maxn + * + * @return the ipv4 address + */ +tb_char_t const* tb_ipv4_cstr(tb_ipv4_ref_t ipv4, tb_char_t* data, tb_size_t maxn); + +/*! set the ipv4 from string + * + * @param ipv4 the ipv4 + * @param cstr the ipv4 string + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv4_cstr_set(tb_ipv4_ref_t ipv4, tb_char_t const* cstr); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/ipv6.h b/core/pkg/tbox.pkg/inc/tbox/network/ipv6.h index bf631d067..aa230cb10 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/ipv6.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/ipv6.h @@ -169,3 +169,174 @@ tb_bool_t tb_ipv6_cstr_set(tb_ipv6_ref_t ipv6, tb_char_t const* cstr); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ipv6.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_IPV6_H +#define TB_NETWORK_IPV6_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the ipv6 string data maxn +#define TB_IPV6_CSTR_MAXN (40 + 20) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the ipv6 type + * + * <pre> + * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx + * xxxx::xxxx:xxxx + * ::ffff:xxx.xxx.xxx.xxx + * ::fe80:1%1 + * </pre> + */ +typedef struct __tb_ipv6_t +{ + /// the scope id + tb_uint32_t scope_id; + + /// the address + union + { + /// u32, little-endian + tb_uint32_t u32[4]; + + /// u16, little-endian + tb_uint16_t u16[8]; + + /// u8 + tb_uint8_t u8[16]; + + } addr; + +}tb_ipv6_t, *tb_ipv6_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! clear the ipv6 + * + * @param ipv6 the ipv6 + */ +tb_void_t tb_ipv6_clear(tb_ipv6_ref_t ipv6); + +/*! is any address? + * + * @param ipv6 the ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_any(tb_ipv6_ref_t ipv6); + +/*! is loopback address? + * + * @param ipv6 the ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_loopback(tb_ipv6_ref_t ipv6); + +/*! is linklocal address? + * + * @param ipv6 the ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_linklocal(tb_ipv6_ref_t ipv6); + +/*! is mc linklocal address? + * + * @param ipv6 the ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_mc_linklocal(tb_ipv6_ref_t ipv6); + +/*! is sitelocal address? + * + * @param ipv6 the ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_sitelocal(tb_ipv6_ref_t ipv6); + +/*! is multicast address? + * + * @param ipv6 the ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_multicast(tb_ipv6_ref_t ipv6); + +/*! is equal? + * + * @param ipv6 the ipv6 + * @param other the other ipv6 + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_is_equal(tb_ipv6_ref_t ipv6, tb_ipv6_ref_t other); + +/*! get the ipv6 string + * + * @param ipv6 the ipv6 + * @param data the ipv6 string data + * @param maxn the ipv6 string data maxn + * + * @return the ipv6 string + */ +tb_char_t const* tb_ipv6_cstr(tb_ipv6_ref_t ipv6, tb_char_t* data, tb_size_t maxn); + +/*! set the ipv6 from string + * + * @param ipv6 the ipv6 + * @param cstr the ipv6 string + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ipv6_cstr_set(tb_ipv6_ref_t ipv6, tb_char_t const* cstr); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/network.h b/core/pkg/tbox.pkg/inc/tbox/network/network.h index b1db5758a..f522cf976 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/network.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/network.h @@ -41,3 +41,46 @@ #include "dns/dns.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file network.h + * @defgroup network + * + */ +#ifndef TB_NETWORK_H +#define TB_NETWORK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "ssl.h" +#include "url.h" +#include "ipv4.h" +#include "ipv6.h" +#include "ipaddr.h" +#include "hwaddr.h" +#include "http.h" +#include "cookies.h" +#include "dns/dns.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/prefix.h b/core/pkg/tbox.pkg/inc/tbox/network/prefix.h index b0e30d241..6b19b4e81 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_NETWORK_PREFIX_H +#define TB_NETWORK_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/ssl.h b/core/pkg/tbox.pkg/inc/tbox/network/ssl.h index b6586d579..60003007e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/ssl.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/ssl.h @@ -231,3 +231,236 @@ tb_size_t tb_ssl_state(tb_ssl_ref_t ssl); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ssl.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_SSL_H +#define TB_NETWORK_SSL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// enable ssl? +#if defined(TB_CONFIG_PACKAGE_HAVE_OPENSSL) \ + || defined(TB_CONFIG_PACKAGE_HAVE_POLARSSL) +# define TB_SSL_ENABLE +#else +# undef TB_SSL_ENABLE +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the ssl read func type + * + * @param priv the priv data for context + * @param data the data + * @param size the size + * + * @return the real size, no data: 0, failed: -1 + */ +typedef tb_long_t (*tb_ssl_func_read_t)(tb_cpointer_t priv, tb_byte_t* data, tb_size_t size); + +/*! the ssl writ func type + * + * @param priv the priv data for context + * @param data the data + * @param size the size + * + * @return the real size, no data: 0, failed: -1 + */ +typedef tb_long_t (*tb_ssl_func_writ_t)(tb_cpointer_t priv, tb_byte_t const* data, tb_size_t size); + +/*! the ssl wait func type + * + * @param priv the priv data for context + * @param code the events code + * @param timeout the timeout + * + * @return the real code, no event: 0, failed or closed: -1 + */ +typedef tb_long_t (*tb_ssl_func_wait_t)(tb_cpointer_t priv, tb_size_t code, tb_long_t timeout); + +/// the ssl ref type +typedef __tb_typeref__(ssl); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init ssl + * + * @param bserver is server endpoint? + * + * @return the ssl + */ +tb_ssl_ref_t tb_ssl_init(tb_bool_t bserver); + +/*! exit ssl + * + * @param ssl the ssl + */ +tb_void_t tb_ssl_exit(tb_ssl_ref_t ssl); + +/*! set ssl bio sock + * + * @param ssl the ssl + * @param sock the sock handle, non-blocking + */ +tb_void_t tb_ssl_set_bio_sock(tb_ssl_ref_t ssl, tb_socket_ref_t sock); + +/*! set ssl bio read and writ func + * + * @param ssl the ssl + * @param read the read func + * @param writ the writ func + * #param wait the wait func only for tb_ssl_open and tb_ssl_wait + * @param priv the priv data + */ +tb_void_t tb_ssl_set_bio_func(tb_ssl_ref_t ssl, tb_ssl_func_read_t read, tb_ssl_func_writ_t writ, tb_ssl_func_wait_t wait, tb_cpointer_t priv); + +/*! set ssl timeout for opening + * + * @param ssl the ssl + * @param timeout the timeout + */ +tb_void_t tb_ssl_set_timeout(tb_ssl_ref_t ssl, tb_long_t timeout); + +/*! open ssl using blocking mode + * + * @note need wait func + * + * @param ssl the ssl + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ssl_open(tb_ssl_ref_t ssl); + +/*! try opening ssl using non-blocking mode + * + * @code + * + // open it + tb_long_t ok = -1; + while (!(ok = tb_ssl_open_try(handle))) + { + // wait it + ok = tb_ssl_wait(handle, TB_SOCKET_EVENT_RECV | TB_SOCKET_EVENT_SEND, timeout); + tb_check_break(ok > 0); + } + + * @endcode + * + * @param ssl the ssl + * + * @return ok: 1, continue: 0, failed: -1 + */ +tb_long_t tb_ssl_open_try(tb_ssl_ref_t ssl); + +/*! clos ssl + * + * @param ssl the ssl + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ssl_clos(tb_ssl_ref_t ssl); + +/*! try closing ssl using non-blocking mode + * + * @code + * + // open it + tb_long_t ok = -1; + while (!(ok = tb_ssl_clos_try(handle))) + { + // wait it + ok = tb_ssl_wait(handle, TB_SOCKET_EVENT_RECV | TB_SOCKET_EVENT_SEND, timeout); + tb_check_break(ok > 0); + } + + * @endcode + * + * @param ssl the ssl + * + * @return ok: 1, continue: 0, failed: -1 + */ +tb_long_t tb_ssl_clos_try(tb_ssl_ref_t ssl); + +/*! read ssl data + * + * @param ssl the ssl + * @param data the data + * @param size the size + * + * @return the real size, no data: 0 and see state for waiting, failed: -1 + */ +tb_long_t tb_ssl_read(tb_ssl_ref_t ssl, tb_byte_t* data, tb_size_t size); + +/*! writ ssl data + * + * @param ssl the ssl + * @param data the data + * @param size the size + * + * @return the real size, no data: 0 and see state for waiting, failed: -1 + */ +tb_long_t tb_ssl_writ(tb_ssl_ref_t ssl, tb_byte_t const* data, tb_size_t size); + +/*! wait ssl data + * + * @param ssl the ssl + * @param events the events + * @param timeout the timeout + * + * @return the real events, no event: 0, failed or closed: -1 + */ +tb_long_t tb_ssl_wait(tb_ssl_ref_t ssl, tb_size_t events, tb_long_t timeout); + +/*! the ssl state see the stream ssl state + * + * @param ssl the ssl + * + * @return the ssl state + */ +tb_size_t tb_ssl_state(tb_ssl_ref_t ssl); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/network/url.h b/core/pkg/tbox.pkg/inc/tbox/network/url.h index 68902e95d..192868256 100644 --- a/core/pkg/tbox.pkg/inc/tbox/network/url.h +++ b/core/pkg/tbox.pkg/inc/tbox/network/url.h @@ -272,3 +272,277 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file url.h + * @ingroup network + * + */ +#ifndef TB_NETWORK_URL_H +#define TB_NETWORK_URL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "ipaddr.h" +#include "../string/string.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the url protocol type +typedef enum __tb_url_protocol_t +{ + TB_URL_PROTOCOL_NONE = 0 +, TB_URL_PROTOCOL_FILE = 1 //!< file://... +, TB_URL_PROTOCOL_SOCK = 2 //!< sock://... or socks://... +, TB_URL_PROTOCOL_HTTP = 3 //!< http://... or https://... +, TB_URL_PROTOCOL_DATA = 4 //!< data://... +, TB_URL_PROTOCOL_RTSP = 5 //!< rtsp://... +, TB_URL_PROTOCOL_SQL = 6 //!< sql://... + +}tb_url_protocol_t; + +/// the url type +typedef struct __tb_url_t +{ + // the protocol + tb_uint16_t poto: 6; + + // is ssl? + tb_uint16_t bssl: 1; + + // is win? + tb_uint16_t bwin: 1; + + // the windows prefix + tb_uint16_t pwin: 8; + + // the addr + tb_ipaddr_t addr; + + // the host + tb_string_t host; + + // the path + tb_string_t path; + + // the args + tb_string_t args; + + // the url cache + tb_string_t cache; + +}tb_url_t, *tb_url_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init url + * + * @param url the url + * + * @return tb_true or tb_false + */ +tb_bool_t tb_url_init(tb_url_ref_t url); + +/*! init url from c-string + * + * @param url the url + * @param cstr the url c-string + * + * @return tb_true or tb_false + */ +tb_bool_t tb_url_init_from_cstr(tb_url_ref_t url, tb_char_t const* cstr); + +/*! exit url + * + * @param url the url + */ +tb_void_t tb_url_exit(tb_url_ref_t url); + +/*! clear url + * + * @param url the url + */ +tb_void_t tb_url_clear(tb_url_ref_t url); + +/*! get the url c-string + * + * @param url the url + * + * @return the c-string + */ +tb_char_t const* tb_url_cstr(tb_url_ref_t url); + +/*! set the url c-string + * + * @param url the url + * @param cstr the url c-string + * + * @return tb_true or tb_false + */ +tb_bool_t tb_url_cstr_set(tb_url_ref_t url, tb_char_t const* cstr); + +/*! copy the url + * + * @param url the url + * @param copy the copied url + */ +tb_void_t tb_url_copy(tb_url_ref_t url, tb_url_ref_t copy); + +/*! is ssl? + * + * @param url the url + * + * @return tb_true or tb_false + */ +tb_bool_t tb_url_ssl(tb_url_ref_t url); + +/*! set ssl + * + * @param url the url + * @param bssl is ssl? + */ +tb_void_t tb_url_ssl_set(tb_url_ref_t url, tb_bool_t bssl); + +/*! get the protocol from the url + * + * @param url the url + * + * @return the url protocol + */ +tb_size_t tb_url_protocol(tb_url_ref_t url); + +/*! set the protocol to the url + * + * @param url the url + * @param poto the url protocol + */ +tb_void_t tb_url_protocol_set(tb_url_ref_t url, tb_size_t protocol); + +/*! the protocol c-string + * + * @param url the url + * + * @return the url protocol c-string + */ +tb_char_t const* tb_url_protocol_cstr(tb_url_ref_t url); + +/*! probe the protocol from the url + * + * @param url the url + * + * @return the url protocol + */ +tb_size_t tb_url_protocol_probe(tb_char_t const* url); + +/*! get the port from the url + * + * @param url the url + * + * @return the url port + */ +tb_uint16_t tb_url_port(tb_url_ref_t url); + +/*! set the port to the url + * + * @param url the url + * @param port the url port + */ +tb_void_t tb_url_port_set(tb_url_ref_t url, tb_uint16_t port); + +/*! get the address from the url + * + * @param url the url + * + * @return the url address + */ +tb_ipaddr_ref_t tb_url_addr(tb_url_ref_t url); + +/*! set the address to the url + * + * @param url the url + * @param addr the url address + */ +tb_void_t tb_url_addr_set(tb_url_ref_t url, tb_ipaddr_ref_t addr); + +/*! get the host from the url + * + * @param url the url + * + * @return the url host + */ +tb_char_t const* tb_url_host(tb_url_ref_t url); + +/*! set the host to the url + * + * @param url the url + * @param host the url host + */ +tb_void_t tb_url_host_set(tb_url_ref_t url, tb_char_t const* host); + +/*! get the path from the url + * + * @param url the url + * + * @return the url path + */ +tb_char_t const* tb_url_path(tb_url_ref_t url); + +/*! set the path to the url + * + * @param url the url + * @param path the url path + */ +tb_void_t tb_url_path_set(tb_url_ref_t url, tb_char_t const* path); + +/*! get the arguments from the url + * + * @param url the url + * + * @return the url arguments + */ +tb_char_t const* tb_url_args(tb_url_ref_t url); + +/*! set the arguments to the url + * + * @param url the url + * @param args the url arguments + */ +tb_void_t tb_url_args_set(tb_url_ref_t url, tb_char_t const* args); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/object/array.h b/core/pkg/tbox.pkg/inc/tbox/object/array.h index cfac90d82..07a883f50 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/array.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/array.h @@ -128,3 +128,133 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file array.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_ARRAY_H +#define TB_OBJECT_ARRAY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init array + * + * @param grow the array grow + * @param incr is increase refn? + * + * @return the array object + */ +tb_object_ref_t tb_oc_array_init(tb_size_t grow, tb_bool_t incr); + +/*! the array size + * + * @param array the array object + * + * @return the array size + */ +tb_size_t tb_oc_array_size(tb_object_ref_t array); + +/*! the array item at index + * + * @param array the array object + * @param index the array index + * + * @return the array item + */ +tb_object_ref_t tb_oc_array_item(tb_object_ref_t array, tb_size_t index); + +/*! set the array incr + * + * @param array the array object + * @param incr is increase refn? + */ +tb_void_t tb_oc_array_incr(tb_object_ref_t array, tb_bool_t incr); + +/*! the array iterator + * + * @param array the array object + * + * @return the array iterator + * + * @code + * tb_for_all (tb_object_ref_t, item, tb_oc_array_itor(array)) + * { + * if (item) + * { + * // ... + * } + * } + * @endcode + */ +tb_iterator_ref_t tb_oc_array_itor(tb_object_ref_t array); + +/*! remove the item from index + * + * @param array the array object + * @param index the array index + */ +tb_void_t tb_oc_array_remove(tb_object_ref_t array, tb_size_t index); + +/*! append item to array + * + * @param array the array object + * @param index the array index + */ +tb_void_t tb_oc_array_append(tb_object_ref_t array, tb_object_ref_t item); + +/*! insert item to array + * + * @param array the array object + * @param index the array index + * @param item the array item + */ +tb_void_t tb_oc_array_insert(tb_object_ref_t array, tb_size_t index, tb_object_ref_t item); + +/*! replace item to array + * + * @param array the array object + * @param index the array index + * @param item the array item + */ +tb_void_t tb_oc_array_replace(tb_object_ref_t array, tb_size_t index, tb_object_ref_t item); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/boolean.h b/core/pkg/tbox.pkg/inc/tbox/object/boolean.h index 6926edb56..0d4ae5b98 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/boolean.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/boolean.h @@ -76,3 +76,81 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file boolean.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_BOOLEAN_H +#define TB_OBJECT_BOOLEAN_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init boolean + * + * @param value the value + * + * @return the boolean object + */ +tb_object_ref_t tb_oc_boolean_init(tb_bool_t value); + +/*! the boolean value: true + * + * @return the boolean object + */ +tb_object_ref_t tb_oc_boolean_true(tb_noarg_t); + +/*! the boolean value: false + * + * @return the boolean object + */ +tb_object_ref_t tb_oc_boolean_false(tb_noarg_t); + +/*! the boolean value + * + * @param the boolean object + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_boolean_bool(tb_object_ref_t boolean); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/data.h b/core/pkg/tbox.pkg/inc/tbox/object/data.h index c78fa0b85..2de267709 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/data.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/data.h @@ -115,3 +115,120 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file data.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DATA_H +#define TB_OBJECT_DATA_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init data from url + * + * @param data the data + * @param size the size + * + * @return the data object + */ +tb_object_ref_t tb_oc_data_init_from_url(tb_char_t const* url); + +/*! init data from data + * + * @param data the data + * @param size the size + * + * @return the data object + */ +tb_object_ref_t tb_oc_data_init_from_data(tb_pointer_t data, tb_size_t size); + +/*! init data from buffer + * + * @param buffer the buffer + * + * @return the data object + */ +tb_object_ref_t tb_oc_data_init_from_buffer(tb_buffer_ref_t buffer); + +/*! get the data + * + * @param data the data object + * + * @return the data address + */ +tb_pointer_t tb_oc_data_getp(tb_object_ref_t data); + +/*! set the data + * + * @param data the data object + * @param addr the data address + * @param size the data size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_data_setp(tb_object_ref_t data, tb_pointer_t addr, tb_size_t size); + +/*! the data size + * + * @param data the data object + * + * @return the data size + */ +tb_size_t tb_oc_data_size(tb_object_ref_t data); + +/*! the data buffer + * + * @param data the data object + * + * @return the data buffer + */ +tb_buffer_ref_t tb_oc_data_buffer(tb_object_ref_t data); + +/*! writ data to url + * + * @param data the data object + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_data_writ_to_url(tb_object_ref_t data, tb_char_t const* url); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/date.h b/core/pkg/tbox.pkg/inc/tbox/object/date.h index 7046b6499..b96b861c7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/date.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/date.h @@ -86,3 +86,91 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file date.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DATE_H +#define TB_OBJECT_DATE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init date from now + * + * @return the date object + */ +tb_object_ref_t tb_oc_date_init_from_now(tb_noarg_t); + +/*! init date from time + * + * @param the date time + * + * @return the date object + */ +tb_object_ref_t tb_oc_date_init_from_time(tb_time_t time); + +/*! the date time + * + * @param the date object + * + * @return the date time + */ +tb_time_t tb_oc_date_time(tb_object_ref_t date); + +/*! set the date time + * + * @param the date object + * @param the date time + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_date_time_set(tb_object_ref_t date, tb_time_t time); + +/*! set the date time for now + * + * @param the date object + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_date_time_set_now(tb_object_ref_t date); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/array.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/array.h index 6819a0ff2..352ee14a3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/array.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/array.h @@ -47,3 +47,52 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file array.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_ARRAY_H +#define TB_OBJECT_DEPRECATED_ARRAY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#define tb_object_array_init tb_oc_array_init +#define tb_object_array_size tb_oc_array_size +#define tb_object_array_item tb_oc_array_item +#define tb_object_array_incr tb_oc_array_incr +#define tb_object_array_itor tb_oc_array_itor +#define tb_object_array_remove tb_oc_array_remove +#define tb_object_array_append tb_oc_array_append +#define tb_object_array_insert tb_oc_array_insert +#define tb_object_array_replace tb_oc_array_replace + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/boolean.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/boolean.h index 8bc8a4d28..05ea8af9a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/boolean.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/boolean.h @@ -43,3 +43,48 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file boolean.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_BOOLEAN_H +#define TB_OBJECT_DEPRECATED_BOOLEAN_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#define tb_object_boolean_init tb_oc_boolean_init +#define tb_object_boolean_true tb_oc_boolean_true +#define tb_object_boolean_false tb_oc_boolean_false +#define tb_object_boolean_bool tb_oc_boolean_bool + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/data.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/data.h index eee8a9900..47757091a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/data.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/data.h @@ -45,3 +45,50 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file data.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_DATA_H +#define TB_OBJECT_DEPRECATED_DATA_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ +#define tb_object_data_init_from_url tb_oc_data_init_from_url +#define tb_object_data_init_from_data tb_oc_data_init_from_data +#define tb_object_data_init_from_buffer tb_oc_data_init_from_buffer +#define tb_object_data_getp tb_oc_data_getp +#define tb_object_data_setp tb_oc_data_setp +#define tb_object_data_size tb_oc_data_size +#define tb_object_data_buffer tb_oc_data_buffer +#define tb_object_data_writ_to_url tb_oc_data_writ_to_url + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/date.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/date.h index c4a0a7a82..98cdf8e0f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/date.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/date.h @@ -45,3 +45,50 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file date.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_DATE_H +#define TB_OBJECT_DEPRECATED_DATE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#define tb_object_date_init_from_now tb_oc_date_init_from_now +#define tb_object_date_init_from_time tb_oc_date_init_from_time +#define tb_object_date_time tb_oc_date_time +#define tb_object_date_time_set tb_oc_date_time_set +#define tb_object_date_time_set_now tb_oc_date_time_set_now + + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/deprecated.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/deprecated.h index 281e5a710..1b87b34b4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/deprecated.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/deprecated.h @@ -39,3 +39,44 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file deprecated.h + * + */ +#ifndef TB_OBJECT_DEPRECATED_H +#define TB_OBJECT_DEPRECATED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "null.h" +#include "data.h" +#include "date.h" +#include "array.h" +#include "string.h" +#include "number.h" +#include "boolean.h" +#include "dictionary.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/dictionary.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/dictionary.h index 927ab4410..825cba3c6 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/dictionary.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/dictionary.h @@ -66,3 +66,71 @@ typedef struct __tb_object_dictionary_item_t #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file dictionary.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_DICTIONARY_H +#define TB_OBJECT_DEPRECATED_DICTIONARY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define TB_OBJECT_DICTIONARY_SIZE_MICRO (64) +#define TB_OBJECT_DICTIONARY_SIZE_SMALL (256) +#define TB_OBJECT_DICTIONARY_SIZE_LARGE (65536) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the dictionary item type +typedef struct __tb_object_dictionary_item_t +{ + /// the key + tb_char_t const* key; + + /// the value + tb_object_ref_t val; + +}tb_object_dictionary_item_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ +#define tb_object_dictionary_init tb_oc_dictionary_init +#define tb_object_dictionary_size tb_oc_dictionary_size +#define tb_object_dictionary_incr tb_oc_dictionary_incr +#define tb_object_dictionary_itor tb_oc_dictionary_itor +#define tb_object_dictionary_value tb_oc_dictionary_value +#define tb_object_dictionary_insert tb_oc_dictionary_insert +#define tb_object_dictionary_remove tb_oc_dictionary_remove + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/null.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/null.h index bd4eb3d5d..0f86e75f9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/null.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/null.h @@ -40,3 +40,45 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file null.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_NULL_H +#define TB_OBJECT_DEPRECATED_NULL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#define tb_object_null_init tb_oc_null_init + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/number.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/number.h index 87e140ecd..a59ab111f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/number.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/number.h @@ -98,3 +98,103 @@ typedef enum __tb_object_number_type_e #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file number.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_NUMBER_H +#define TB_OBJECT_DEPRECATED_NUMBER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the number type enum +typedef enum __tb_object_number_type_e +{ + TB_NUMBER_TYPE_NONE = 0 +, TB_NUMBER_TYPE_UINT8 = 1 +, TB_NUMBER_TYPE_SINT8 = 2 +, TB_NUMBER_TYPE_UINT16 = 3 +, TB_NUMBER_TYPE_SINT16 = 4 +, TB_NUMBER_TYPE_UINT32 = 5 +, TB_NUMBER_TYPE_SINT32 = 6 +, TB_NUMBER_TYPE_UINT64 = 7 +, TB_NUMBER_TYPE_SINT64 = 8 +, TB_NUMBER_TYPE_FLOAT = 9 +, TB_NUMBER_TYPE_DOUBLE = 10 + +}tb_object_number_type_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#define tb_object_number_init_from_uint8 tb_oc_number_init_from_uint8 +#define tb_object_number_init_from_sint8 tb_oc_number_init_from_sint8 +#define tb_object_number_init_from_uint16 tb_oc_number_init_from_uint16 +#define tb_object_number_init_from_sint16 tb_oc_number_init_from_sint16 +#define tb_object_number_init_from_uint32 tb_oc_number_init_from_uint32 +#define tb_object_number_init_from_sint32 tb_oc_number_init_from_sint32 +#define tb_object_number_init_from_uint64 tb_oc_number_init_from_uint64 +#define tb_object_number_init_from_sint64 tb_oc_number_init_from_sint64 +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_object_number_init_from_float tb_oc_number_init_from_float +# define tb_object_number_init_from_double tb_oc_number_init_from_double +#endif + +#define tb_object_number_type tb_oc_number_type +#define tb_object_number_uint8 tb_oc_number_uint8 +#define tb_object_number_sint8 tb_oc_number_sint8 +#define tb_object_number_uint16 tb_oc_number_uint16 +#define tb_object_number_sint16 tb_oc_number_sint16 +#define tb_object_number_uint32 tb_oc_number_uint32 +#define tb_object_number_sint32 tb_oc_number_sint32 +#define tb_object_number_uint64 tb_oc_number_uint64 +#define tb_object_number_sint64 tb_oc_number_sint64 +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_object_number_float tb_oc_number_float +# define tb_object_number_double tb_oc_number_double +#endif + +#define tb_object_number_uint8_set tb_oc_number_uint8_set +#define tb_object_number_sint8_set tb_oc_number_sint8_set +#define tb_object_number_uint16_set tb_oc_number_uint16_set +#define tb_object_number_sint16_set tb_oc_number_sint16_set +#define tb_object_number_uint32_set tb_oc_number_uint32_set +#define tb_object_number_sint32_set tb_oc_number_sint32_set +#define tb_object_number_uint64_set tb_oc_number_uint64_set +#define tb_object_number_sint64_set tb_oc_number_sint64_set +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# define tb_object_number_floa_set tb_oc_number_float_set +# define tb_object_number_double_set tb_oc_number_double_set +#endif + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/prefix.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/prefix.h index 80098707e..1370bc84f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_OBJECT_DEPRECATED_PREFIX_H +#define TB_OBJECT_DEPRECATED_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/string.h b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/string.h index d4f6d7659..4240949c0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/deprecated/string.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/deprecated/string.h @@ -43,3 +43,48 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file string.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DEPRECATED_STRING_H +#define TB_OBJECT_DEPRECATED_STRING_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#define tb_object_string_init_from_cstr tb_oc_string_init_from_cstr +#define tb_object_string_init_from_str tb_oc_string_init_from_str +#define tb_object_string_cstr tb_oc_string_cstr +#define tb_object_string_cstr_set tb_oc_string_cstr_set +#define tb_object_string_size tb_oc_string_size + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/dictionary.h b/core/pkg/tbox.pkg/inc/tbox/object/dictionary.h index 67a144ed2..a60058efc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/dictionary.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/dictionary.h @@ -161,3 +161,166 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file dictionary.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_DICTIONARY_H +#define TB_OBJECT_DICTIONARY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define TB_OC_DICTIONARY_SIZE_MICRO (64) +#define TB_OC_DICTIONARY_SIZE_SMALL (256) +#define TB_OC_DICTIONARY_SIZE_LARGE (65536) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the dictionary item type +typedef struct __tb_oc_dictionary_item_t +{ + /// the key + tb_char_t const* key; + + /// the value + tb_object_ref_t val; + +}tb_oc_dictionary_item_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init dictionary + * + * @code + // init dictionary + // {"key1": "hello", "key2" :"world", "key3": 12345, "key4": true} + tb_object_ref_t dict = tb_oc_dictionary_init(0, tb_false); + if (dict) + { + // key1 => hello + tb_oc_dictionary_insert(dict, "key1", tb_oc_string_init_from_cstr("hello")); + + // key2 => world + tb_oc_dictionary_insert(dict, "key2", tb_oc_string_init_from_cstr("world")); + + // key3 => 12345 + tb_oc_dictionary_insert(dict, "key3", tb_oc_number_init_from_sint32(12345)); + + // key4 => true + tb_oc_dictionary_insert(dict, "key4", tb_oc_boolean_true()); + + // exit dictionary + tb_object_exit(dict); + } + * @endcode + * + * @param size the dictionary size, using the default size if be zero + * @param incr is increase refn? + * + * @return the dictionary object + */ +tb_object_ref_t tb_oc_dictionary_init(tb_size_t size, tb_bool_t incr); + +/*! the dictionary size + * + * @param dictionary the dictionary object + * + * @return the dictionary size + */ +tb_size_t tb_oc_dictionary_size(tb_object_ref_t dictionary); + +/*! set the dictionary incr + * + * @param dictionary the dictionary object + * @param incr is increase refn? + */ +tb_void_t tb_oc_dictionary_incr(tb_object_ref_t dictionary, tb_bool_t incr); + +/*! the dictionary iterator + * + * @param dictionary the dictionary object + * + * @return the dictionary iterator + * + * @code + tb_for_all (tb_oc_dictionary_item_t*, item, tb_oc_dictionary_itor(dictionary)) + { + if (item) + { + tb_char_t const* key = item->key; + tb_object_ref_t val = item->val; + + // ... + } + } + * @endcode + */ +tb_iterator_ref_t tb_oc_dictionary_itor(tb_object_ref_t dictionary); + +/*! get the dictionary value + * + * @param dictionary the dictionary object + * @param key the key + * + * @return the dictionary value + */ +tb_object_ref_t tb_oc_dictionary_value(tb_object_ref_t dictionary, tb_char_t const* key); + +/*! insert dictionary item + * + * @param dictionary the dictionary object + * @param key the key + * @param val the value + */ +tb_void_t tb_oc_dictionary_insert(tb_object_ref_t dictionary, tb_char_t const* key, tb_object_ref_t val); + +/*! remove dictionary item + * + * @param dictionary the dictionary object + * @param key the key + */ +tb_void_t tb_oc_dictionary_remove(tb_object_ref_t dictionary, tb_char_t const* key); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/null.h b/core/pkg/tbox.pkg/inc/tbox/object/null.h index c9c066f3b..24525b356 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/null.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/null.h @@ -53,3 +53,58 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file null.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_NULL_H +#define TB_OBJECT_NULL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init null + * + * @return the null object + */ +tb_object_ref_t tb_oc_null_init(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/number.h b/core/pkg/tbox.pkg/inc/tbox/object/number.h index d518906cf..4099561e1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/number.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/number.h @@ -332,3 +332,337 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file number.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_NUMBER_H +#define TB_OBJECT_NUMBER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the number type enum +typedef enum __tb_oc_number_type_e +{ + TB_OC_NUMBER_TYPE_NONE = 0 +, TB_OC_NUMBER_TYPE_UINT8 = 1 +, TB_OC_NUMBER_TYPE_SINT8 = 2 +, TB_OC_NUMBER_TYPE_UINT16 = 3 +, TB_OC_NUMBER_TYPE_SINT16 = 4 +, TB_OC_NUMBER_TYPE_UINT32 = 5 +, TB_OC_NUMBER_TYPE_SINT32 = 6 +, TB_OC_NUMBER_TYPE_UINT64 = 7 +, TB_OC_NUMBER_TYPE_SINT64 = 8 +, TB_OC_NUMBER_TYPE_FLOAT = 9 +, TB_OC_NUMBER_TYPE_DOUBLE = 10 + +}tb_oc_number_type_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init number from uint8 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_uint8(tb_uint8_t value); + +/*! init number from sint8 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_sint8(tb_sint8_t value); + +/*! init number from uint16 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_uint16(tb_uint16_t value); + +/*! init number from sint16 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_sint16(tb_sint16_t value); + +/*! init number from uint32 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_uint32(tb_uint32_t value); + +/*! init number from sint32 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_sint32(tb_sint32_t value); + +/*! init number from uint64 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_uint64(tb_uint64_t value); + +/*! init number from sint64 + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_sint64(tb_sint64_t value); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! init number from float + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_float(tb_float_t value); + +/*! init number from double + * + * @param value the value + * + * @return the number object + */ +tb_object_ref_t tb_oc_number_init_from_double(tb_double_t value); +#endif + +/*! the number type + * + * @param object the object pointer + * + * @return the number type + */ +tb_size_t tb_oc_number_type(tb_object_ref_t number); + +/*! the uint8 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_uint8_t tb_oc_number_uint8(tb_object_ref_t number); + +/*! the sint8 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_sint8_t tb_oc_number_sint8(tb_object_ref_t number); + +/*! the uint16 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_uint16_t tb_oc_number_uint16(tb_object_ref_t number); + +/*! the sint16 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_sint16_t tb_oc_number_sint16(tb_object_ref_t number); + +/*! the uint32 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_uint32_t tb_oc_number_uint32(tb_object_ref_t number); + +/*! the sint32 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_sint32_t tb_oc_number_sint32(tb_object_ref_t number); + +/*! the uint64 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_uint64_t tb_oc_number_uint64(tb_object_ref_t number); + +/*! the sint64 value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_sint64_t tb_oc_number_sint64(tb_object_ref_t number); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! the float value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_float_t tb_oc_number_float(tb_object_ref_t number); + +/*! the double value of the number + * + * @param object the object pointer + * + * @return the number value + */ +tb_double_t tb_oc_number_double(tb_object_ref_t number); +#endif + +/*! set the uint8 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_uint8_set(tb_object_ref_t number, tb_uint8_t value); + +/*! set the sint8 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_sint8_set(tb_object_ref_t number, tb_sint8_t value); + +/*! set the uint16 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_uint16_set(tb_object_ref_t number, tb_uint16_t value); + +/*! set the sint16 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_sint16_set(tb_object_ref_t number, tb_sint16_t value); + +/*! set the uint32 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_uint32_set(tb_object_ref_t number, tb_uint32_t value); + +/*! set the sint32 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_sint32_set(tb_object_ref_t number, tb_sint32_t value); + +/*! set the uint64 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_uint64_set(tb_object_ref_t number, tb_uint64_t value); + +/*! set the sint64 value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_sint64_set(tb_object_ref_t number, tb_sint64_t value); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! set the float value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_float_set(tb_object_ref_t number, tb_float_t value); + +/*! set the double value + * + * @param object the object pointer + * @param value the number value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_oc_number_double_set(tb_object_ref_t number, tb_double_t value); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/object.h b/core/pkg/tbox.pkg/inc/tbox/object/object.h index 0a3c9108f..e3f7c112b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/object.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/object.h @@ -256,3 +256,261 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file object.h + * @defgroup object + * + */ +#ifndef TB_OBJECT_H +#define TB_OBJECT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "null.h" +#include "data.h" +#include "date.h" +#include "array.h" +#include "string.h" +#include "number.h" +#include "boolean.h" +#include "dictionary.h" +#ifdef TB_CONFIG_API_HAVE_DEPRECATED +# include "deprecated/deprecated.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init object + * + * @param object the object + * @param flag the object flag + * @param type the object type + * + * @return tb_true or tb_false + */ +tb_bool_t tb_object_init(tb_object_ref_t object, tb_size_t flag, tb_size_t type); + +/*! decrease the object reference count, will exit it if --refn == 0 + * + * @param object the object + * + * @note the reference count must be one + */ +tb_void_t tb_object_exit(tb_object_ref_t object); + +/*! clear object + * + * @param object the object + */ +tb_void_t tb_object_clear(tb_object_ref_t object); + +/*! set the object private data + * + * @param object the object + * @param priv the private data + * + */ +tb_void_t tb_object_setp(tb_object_ref_t object, tb_cpointer_t priv); + +/*! get the object private data + * + * @param object the object + * + * @return the private data + */ +tb_cpointer_t tb_object_getp(tb_object_ref_t object); + +/*! read object + * + * @param stream the stream + * + * @return the object + */ +tb_object_ref_t tb_object_read(tb_stream_ref_t stream); + +/*! read object from url + * + * @param url the url + * + * @return the object + */ +tb_object_ref_t tb_object_read_from_url(tb_char_t const* url); + +/*! read object from data + * + * @param data the data + * @param size the size + * + * @return the object + */ +tb_object_ref_t tb_object_read_from_data(tb_byte_t const* data, tb_size_t size); + +/*! writ object + * + * @param object the object + * @param stream the stream + * @param format the object format + * + * @return the writed size, failed: -1 + */ +tb_long_t tb_object_writ(tb_object_ref_t object, tb_stream_ref_t stream, tb_size_t format); + +/*! writ object to url + * + * @param object the object + * @param url the url + * @param format the format + * + * @return the writed size, failed: -1 + */ +tb_long_t tb_object_writ_to_url(tb_object_ref_t object, tb_char_t const* url, tb_size_t format); + +/*! writ object to data + * + * @param object the object + * @param data the data + * @param size the size + * @param format the format + * + * @return the writed size, failed: -1 + */ +tb_long_t tb_object_writ_to_data(tb_object_ref_t object, tb_byte_t* data, tb_size_t size, tb_size_t format); + +/*! copy object + * + * @param object the object + * + * @return the object copy + */ +tb_object_ref_t tb_object_copy(tb_object_ref_t object); + +/*! the object type + * + * @param object the object + * + * @return the object type + */ +tb_size_t tb_object_type(tb_object_ref_t object); + +/*! the object data + * + * @param object the object + * @param format the format + * + * @return the data object + */ +tb_object_ref_t tb_object_data(tb_object_ref_t object, tb_size_t format); + +/*! seek to the object for the gived path + * + * <pre> + * + file: + { + "string": "hello world!" + , "com.xxx.xxx": "hello world" + , "integer": 31415926 + , "array": + [ + "hello world!" + , 31415926 + , 3.1415926 + , false + , true + , { "string": "hello world!" } + ] + , "macro": "$.array[2]" + , "macro2": "$.com\\\\.xxx\\\\.xxx" + , "macro3": "$.macro" + , "macro4": "$.array" + } + + path: + 1. ".string" : hello world! + 2. ".array[1]" : 31415926 + 3. ".array[5].string" : hello world! + 4. ".com\\.xxx\\.xxx" : hello world + 5. ".macro" : 3.1415926 + 6. ".macro2" : hello world + 7. ".macro3" : 3.1415926 + 8. ".macro4[0]" : "hello world!" + + * + * </pre> + * + * @param object the object + * @param path the object path + * @param bmacro enable macro(like "$.path")? + * + * <code> + * tb_object_ref_t object = tb_object_seek(root, ".array[5].string", tb_false); + * if (object) + * { + * tb_trace_d("%s", tb_oc_string_cstr(object)); + * } + * <endcode> + * + * + * @return the object + */ +tb_object_ref_t tb_object_seek(tb_object_ref_t object, tb_char_t const* path, tb_bool_t bmacro); + +/*! dump the object + * + * @param object the object + * @param format the format, support: .xml, .xplist, .json + * + * @return the object + */ +tb_object_ref_t tb_object_dump(tb_object_ref_t object, tb_size_t format); + +/*! the object reference count + * + * @param object the object + * + * @return the object reference count + */ +tb_size_t tb_object_refn(tb_object_ref_t object); + +/*! retain object and increase the object reference count + * + * @param object the object + */ +tb_void_t tb_object_retain(tb_object_ref_t object); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/object/prefix.h b/core/pkg/tbox.pkg/inc/tbox/object/prefix.h index 837adcea6..3ac5a2b45 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/prefix.h @@ -102,3 +102,107 @@ typedef struct __tb_object_t }tb_object_t, *tb_object_ref_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_PREFIX_H +#define TB_OBJECT_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../xml/xml.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the object type enum +typedef enum __tb_object_type_e +{ + TB_OBJECT_TYPE_NONE = 0 +, TB_OBJECT_TYPE_DATA = 1 +, TB_OBJECT_TYPE_DATE = 2 +, TB_OBJECT_TYPE_ARRAY = 3 +, TB_OBJECT_TYPE_STRING = 4 +, TB_OBJECT_TYPE_NUMBER = 5 +, TB_OBJECT_TYPE_BOOLEAN = 6 +, TB_OBJECT_TYPE_DICTIONARY = 7 +, TB_OBJECT_TYPE_NULL = 8 +, TB_OBJECT_TYPE_USER = 9 //!< the user defined type, ... + +}tb_object_type_e; + +/// the object flag enum +typedef enum __tb_object_flag_e +{ + TB_OBJECT_FLAG_NONE = 0 +, TB_OBJECT_FLAG_READONLY = 1 +, TB_OBJECT_FLAG_SINGLETON = 2 + +}tb_object_flag_e; + +/// the object format enum +typedef enum __tb_object_format_e +{ + TB_OBJECT_FORMAT_NONE = 0x0000 //!< none +, TB_OBJECT_FORMAT_BIN = 0x0001 //!< the tbox binary format +, TB_OBJECT_FORMAT_BPLIST = 0x0002 //!< the bplist format for apple +, TB_OBJECT_FORMAT_XPLIST = 0x0003 //!< the xplist format for apple +, TB_OBJECT_FORMAT_XML = 0x0004 //!< the xml format +, TB_OBJECT_FORMAT_JSON = 0x0005 //!< the json format +, TB_OBJECT_FORMAT_MAXN = 0x000f //!< the format maxn +, TB_OBJECT_FORMAT_DEFLATE = 0x0100 //!< deflate? + +}tb_object_format_e; + +/// the object type +typedef struct __tb_object_t +{ + /// the object flag + tb_uint8_t flag; + + /// the object type + tb_uint16_t type; + + /// the object reference count + tb_size_t refn; + + /// the object private data + tb_cpointer_t priv; + + /// the copy func + struct __tb_object_t* (*copy)(struct __tb_object_t* object); + + /// the clear func + tb_void_t (*clear)(struct __tb_object_t* object); + + /// the exit func + tb_void_t (*exit)(struct __tb_object_t* object); + +}tb_object_t, *tb_object_ref_t; + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/object/string.h b/core/pkg/tbox.pkg/inc/tbox/object/string.h index ce4e6c4eb..56ed30f08 100644 --- a/core/pkg/tbox.pkg/inc/tbox/object/string.h +++ b/core/pkg/tbox.pkg/inc/tbox/object/string.h @@ -88,3 +88,93 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file string.h + * @ingroup object + * + */ +#ifndef TB_OBJECT_STRING_H +#define TB_OBJECT_STRING_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init string from c-string + * + * @param cstr the c-string + * + * @return the string object + */ +tb_object_ref_t tb_oc_string_init_from_cstr(tb_char_t const* cstr); + +/*! init string from string + * + * @param str the string + * + * @return the string object + */ +tb_object_ref_t tb_oc_string_init_from_str(tb_string_ref_t str); + +/*! the c-string + * + * @param string the string object + * + * @return the c-string + */ +tb_char_t const* tb_oc_string_cstr(tb_object_ref_t string); + +/*! set the c-string + * + * @param string the string object + * @param cstr the c-string + * + * @return the string size + */ +tb_size_t tb_oc_string_cstr_set(tb_object_ref_t string, tb_char_t const* cstr); + +/*! the string size + * + * @param string the string object + * + * @return the string size + */ +tb_size_t tb_oc_string_size(tb_object_ref_t string); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/addrinfo.h b/core/pkg/tbox.pkg/inc/tbox/platform/addrinfo.h index aa7bbb2d8..ad78cd055 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/addrinfo.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/addrinfo.h @@ -92,3 +92,97 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file addrinfo.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_ADDRINFO_H +#define TB_PLATFORM_ADDRINFO_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../network/ipaddr.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! get the first dns address from the host name + * + * @code + + // get the default address (ipv4) + tb_ipaddr_t addr = {0}; + if (tb_addrinfo_addr("www.tboox.org", &addr)) + tb_trace_i("%{ipaddr}", &addr); + + // get the ipv6 address by the hint info + tb_ipaddr_t addr = {0}; + tb_ipaddr_family_set(&addr, TB_IPADDR_FAMILY_IPV6); + if (tb_addrinfo_addr("www.tboox.org", &addr)) + tb_trace_i("%{ipaddr}", &addr); + + * @endcode + * + * @param name the host name (cannot be null) + * @param addr the ip address (we can fill some hint info first) + * + * @return tb_true or tb_false + */ +tb_bool_t tb_addrinfo_addr(tb_char_t const* name, tb_ipaddr_ref_t addr); + +/*! get the host name from the given address + * + * @code + + // get the host name by address + tb_ipaddr_t addr; + tb_char_t host[256]; + tb_ipaddr_ip_cstr_set(&addr, "127.0.0.1"); + tb_trace_i("%s", tb_addrinfo_name(&addr, host, sizeof(host))); + + * @endcode + * + * @param addr the ip address (cannot be null) + * @param name the host name buffer + * @param maxn the host name buffer maxn + * + * @return the host name or tb_null + */ +tb_char_t const* tb_addrinfo_name(tb_ipaddr_ref_t addr, tb_char_t* name, tb_size_t maxn); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/android/android.h b/core/pkg/tbox.pkg/inc/tbox/platform/android/android.h index 905f1d5bb..8a1858535 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/android/android.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/android/android.h @@ -52,3 +52,57 @@ tb_void_t tb_android_exit_env(tb_noarg_t); JavaVM* tb_android_jvm(tb_noarg_t); #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file android.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_ANDROID_H +#define TB_PLATFORM_ANDROID_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the android platform + * + * @param jvm the java machine pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_android_init_env(JavaVM* jvm); + +/// exit the android platform +tb_void_t tb_android_exit_env(tb_noarg_t); + +/*! the java machine pointer + * + * @return the java machine pointer + */ +JavaVM* tb_android_jvm(tb_noarg_t); + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/android/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/android/prefix.h index a3b9a9f44..4a1ef3b5b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/android/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/android/prefix.h @@ -34,3 +34,39 @@ #include <jni.h> #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_ANDROID_PREFIX_H +#define TB_PLATFORM_ANDROID_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" +#include <jni.h> + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/frame.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/frame.h index 7d84bc33c..f29855d14 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/frame.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/frame.h @@ -66,3 +66,71 @@ typedef struct __tb_frame_layout_t #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file frame.h + * + */ +#ifndef TB_PLATFORM_ARCH_ARM_FRAME_H +#define TB_PLATFORM_ARCH_ARM_FRAME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the current stack frame address +#if !defined(TB_CURRENT_STACK_FRAME) \ + && defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(4, 1) +# define TB_CURRENT_STACK_FRAME (__builtin_frame_address(0) - 12) +#endif + +// the advance stack frame address +#ifndef TB_ADVANCE_STACK_FRAME +# define TB_ADVANCE_STACK_FRAME(next) ((tb_frame_layout_t*)(next) - 1) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the frame layout type +typedef struct __tb_frame_layout_t +{ + // the next + struct __tb_frame_layout_t* next; + + // the sp + tb_pointer_t sp; + + // the frame return address + tb_pointer_t return_address; + +}tb_frame_layout_t; + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/prefix.h index 442d92602..0769d7451 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_ARCH_ARM_PREFIX_H +#define TB_PLATFORM_ARCH_ARM_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm64/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm64/prefix.h index 0eb321e2b..830e2f8b6 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm64/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/arm64/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_ARCH_ARM64_PREFIX_H +#define TB_PLATFORM_ARCH_ARM64_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/atomic.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/atomic.h index 9ec0f99d4..bc177fe0d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/atomic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/atomic.h @@ -37,3 +37,42 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic.h + * + */ +#ifndef TB_PLATFORM_ARCH_ATOMIC_H +#define TB_PLATFORM_ARCH_ATOMIC_H + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#if defined(TB_ARCH_x86) +# include "x86/atomic.h" +#elif defined(TB_ARCH_x64) +# include "x64/atomic.h" +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/barrier.h index 21037035f..f676a6ae9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/barrier.h @@ -36,3 +36,41 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * + */ +#ifndef TB_PLATFORM_ARCH_BARRIER_H +#define TB_PLATFORM_ARCH_BARRIER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#if defined(TB_ARCH_x86) +# include "x86/barrier.h" +#elif defined(TB_ARCH_x64) +# include "x64/barrier.h" +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/frame.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/frame.h index 2a0e16e77..4a341085e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/frame.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/frame.h @@ -70,3 +70,75 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file frame.h + * + */ +#ifndef TB_PLATFORM_ARCH_FRAME_H +#define TB_PLATFORM_ARCH_FRAME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#if defined(TB_ARCH_x86) +# include "x86/frame.h" +#elif defined(TB_ARCH_x64) +# include "x64/frame.h" +#elif defined(TB_ARCH_ARM) +# include "arm/frame.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the first frame address +#if !defined(TB_FIRST_FRAME_POINTER) \ + && defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(4, 1) +# define TB_FIRST_FRAME_POINTER __builtin_frame_address(0) +#endif + +// the current stack frame address +#ifndef TB_CURRENT_STACK_FRAME +# define TB_CURRENT_STACK_FRAME ({ tb_char_t __csf; &__csf; }) +#endif + +/* the advance stack frame address + * + * by default assume the `next' pointer in struct layout points to the next struct layout. + */ +#ifndef TB_ADVANCE_STACK_FRAME +# define TB_ADVANCE_STACK_FRAME(next) ((tb_frame_layout_t*)(next)) +#endif + +/* the address is inner than the stack address + * + * by default we assume that the stack grows downward. + */ +#ifndef TB_STACK_INNER_THAN +# define TB_STACK_INNER_THAN < +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/mips/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/mips/prefix.h index d784b6573..916ed5bd8 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/mips/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/mips/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_ARCH_MIPS_PREFIX_H +#define TB_PLATFORM_ARCH_MIPS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/prefix.h index c55a5e9c6..ef684a109 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_ARCH_PREFIX_H +#define TB_PLATFORM_ARCH_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/atomic.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/atomic.h index 69ad484d8..35ffebc1a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/atomic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/atomic.h @@ -31,3 +31,36 @@ #include "../x86/atomic.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic.h + * + */ +#ifndef TB_PLATFORM_ARCH_x64_ATOMIC_H +#define TB_PLATFORM_ARCH_x64_ATOMIC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../x86/atomic.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/barrier.h index 9972c1915..09d47f572 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/barrier.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * + */ +#ifndef TB_PLATFORM_ARCH_x64_BARRIER_H +#define TB_PLATFORM_ARCH_x64_BARRIER_H + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../x86/barrier.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/frame.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/frame.h index 163ef71f2..60ca5b215 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/frame.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/frame.h @@ -57,3 +57,62 @@ typedef struct __tb_frame_layout_t #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file frame.h + * + */ +#ifndef TB_PLATFORM_ARCH_x64_FRAME_H +#define TB_PLATFORM_ARCH_x64_FRAME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the current stack frame address +#if !defined(TB_CURRENT_STACK_FRAME) \ + && defined(TB_ASSEMBLER_IS_GAS) +# define TB_CURRENT_STACK_FRAME ({ __tb_register__ tb_char_t* frame __tb_asm__("rsp"); frame; }) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the frame layout type +typedef struct __tb_frame_layout_t +{ + // the next + struct __tb_frame_layout_t* next; + + // the frame return address + tb_pointer_t return_address; + +}tb_frame_layout_t; + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/prefix.h index 48da70eb9..a7dce9148 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x64/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_ARCH_x64_PREFIX_H +#define TB_PLATFORM_ARCH_x64_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/atomic.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/atomic.h index 6583ae5f7..145001c49 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/atomic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/atomic.h @@ -133,3 +133,138 @@ static __tb_inline__ tb_long_t tb_atomic_fetch_and_add_x86(tb_atomic_t* a, tb_lo #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic.h + * + */ +#ifndef TB_PLATFORM_ARCH_x86_ATOMIC_H +#define TB_PLATFORM_ARCH_x86_ATOMIC_H + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_ASSEMBLER_IS_GAS + +#ifndef tb_atomic_fetch_and_set +# define tb_atomic_fetch_and_set(a, v) tb_atomic_fetch_and_set_x86(a, v) +#endif + +#ifndef tb_atomic_fetch_and_pset +# define tb_atomic_fetch_and_pset(a, p, v) tb_atomic_fetch_and_pset_x86(a, p, v) +#endif + +#ifndef tb_atomic_fetch_and_add +# define tb_atomic_fetch_and_add(a, v) tb_atomic_fetch_and_add_x86(a, v) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ +static __tb_inline__ tb_long_t tb_atomic_fetch_and_set_x86(tb_atomic_t* a, tb_long_t v) +{ + __tb_asm__ __tb_volatile__ + ( +#if TB_CPU_BITSIZE == 64 + "lock xchgq %0, %1\n" //!< xchgq v, [a] +#else + "lock xchgl %0, %1\n" //!< xchgl v, [a] +#endif + + : "+r" (v) + : "m" (*a) + : "memory" + ); + + return v; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_pset_x86(tb_atomic_t* a, tb_long_t p, tb_long_t v) +{ + /* + * cmpxchgl v, [a]: + * + * if (eax == [a]) + * { + * zf = 1; + * [a] = v; + * } + * else + * { + * zf = 0; + * eax = [a]; + * } + * + */ + tb_long_t o; + __tb_asm__ __tb_volatile__ + ( +#if TB_CPU_BITSIZE == 64 + "lock cmpxchgq %3, %1 \n" //!< cmpxchgl v, [a] +#else + "lock cmpxchgl %3, %1 \n" //!< cmpxchgq v, [a] +#endif + + : "=a" (o) + : "m" (*a), "a" (p), "r" (v) + : "cc", "memory" //!< "cc" means that flags were changed. + ); + + return o; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_add_x86(tb_atomic_t* a, tb_long_t v) +{ + /* + * xaddl v, [a]: + * + * o = [a] + * [a] += v; + * v = o; + * + * cf, ef, of, sf, zf, pf... maybe changed + */ + __tb_asm__ __tb_volatile__ + ( +#if TB_CPU_BITSIZE == 64 + "lock xaddq %0, %1 \n" //!< xaddq v, [a] +#else + "lock xaddl %0, %1 \n" //!< xaddl v, [a] +#endif + + : "+r" (v) + : "m" (*a) + : "cc", "memory" + ); + + return v; +} + + +#endif // TB_ASSEMBLER_IS_GAS + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/barrier.h index 5263f83a3..b11e852ea 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/barrier.h @@ -42,3 +42,47 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * + */ +#ifndef TB_PLATFORM_ARCH_x86_BARRIER_H +#define TB_PLATFORM_ARCH_x86_BARRIER_H + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_ASSEMBLER_IS_GAS +# ifndef tb_barrier +# define tb_barrier() __tb_asm__ __tb_volatile__ ("" ::: "memory") +# endif +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/frame.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/frame.h index 5a50383d6..0055627c2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/frame.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/frame.h @@ -57,3 +57,62 @@ typedef struct __tb_frame_layout_t }tb_frame_layout_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file frame.h + * + */ +#ifndef TB_PLATFORM_ARCH_x86_FRAME_H +#define TB_PLATFORM_ARCH_x86_FRAME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the current stack frame address +#if !defined(TB_CURRENT_STACK_FRAME) \ + && defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(4, 1) +# define TB_CURRENT_STACK_FRAME __builtin_frame_address(0) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the frame layout type +typedef struct __tb_frame_layout_t +{ + // the next + struct __tb_frame_layout_t* next; + + // the frame return address + tb_pointer_t return_address; + +}tb_frame_layout_t; + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/prefix.h index 5e55325ac..979f0f8cd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/arch/x86/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_ARCH_x86_PREFIX_H +#define TB_PLATFORM_ARCH_x86_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/atomic.h b/core/pkg/tbox.pkg/inc/tbox/platform/atomic.h index 3fcb25e46..c252b905f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/atomic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/atomic.h @@ -208,3 +208,213 @@ static __tb_inline__ tb_long_t tb_atomic_fetch_and_or_generic(tb_atomic_t* a, tb #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_ATOMIC_H +#define TB_PLATFORM_ATOMIC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#if defined(TB_CONFIG_OS_WINDOWS) +# include "windows/atomic.h" +#elif defined(TB_COMPILER_IS_GCC) \ + && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 +# include "compiler/gcc/atomic.h" +#endif +#include "arch/atomic.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifndef tb_atomic_fetch_and_pset +# define tb_atomic_fetch_and_pset(a, p, v) tb_atomic_fetch_and_pset_generic(a, p, v) +#endif + +#ifndef tb_atomic_fetch_and_set +# define tb_atomic_fetch_and_set(a, v) tb_atomic_fetch_and_set_generic(a, v) +#endif + +#ifndef tb_atomic_fetch_and_add +# define tb_atomic_fetch_and_add(a, v) tb_atomic_fetch_and_add_generic(a, v) +#endif + +#ifndef tb_atomic_fetch_and_or +# define tb_atomic_fetch_and_or(a, v) tb_atomic_fetch_and_or_generic(a, v) +#endif + +#ifndef tb_atomic_fetch_and_xor +# define tb_atomic_fetch_and_xor(a, v) tb_atomic_fetch_and_xor_generic(a, v) +#endif + +#ifndef tb_atomic_fetch_and_and +# define tb_atomic_fetch_and_and(a, v) tb_atomic_fetch_and_and_generic(a, v) +#endif + +#ifndef tb_atomic_get +# define tb_atomic_get(a) tb_atomic_fetch_and_pset(a, 0, 0) +#endif + +#ifndef tb_atomic_set +# define tb_atomic_set(a, v) tb_atomic_fetch_and_set(a, v) +#endif + +#ifndef tb_atomic_set0 +# define tb_atomic_set0(a) tb_atomic_set(a, 0) +#endif + +#ifndef tb_atomic_pset +# define tb_atomic_pset(a, p, v) tb_atomic_fetch_and_pset(a, p, v) +#endif + +#ifndef tb_atomic_fetch_and_set0 +# define tb_atomic_fetch_and_set0(a) tb_atomic_fetch_and_set(a, 0) +#endif + +#ifndef tb_atomic_fetch_and_inc +# define tb_atomic_fetch_and_inc(a) tb_atomic_fetch_and_add(a, 1) +#endif + +#ifndef tb_atomic_fetch_and_dec +# define tb_atomic_fetch_and_dec(a) tb_atomic_fetch_and_add(a, -1) +#endif + +#ifndef tb_atomic_fetch_and_sub +# define tb_atomic_fetch_and_sub(a, v) tb_atomic_fetch_and_add(a, -(v)) +#endif + +#ifndef tb_atomic_add_and_fetch +# define tb_atomic_add_and_fetch(a, v) (tb_atomic_fetch_and_add(a, v) + (v)) +#endif + +#ifndef tb_atomic_inc_and_fetch +# define tb_atomic_inc_and_fetch(a) tb_atomic_add_and_fetch(a, 1) +#endif + +#ifndef tb_atomic_dec_and_fetch +# define tb_atomic_dec_and_fetch(a) tb_atomic_add_and_fetch(a, -1) +#endif + +#ifndef tb_atomic_sub_and_fetch +# define tb_atomic_sub_and_fetch(a, v) tb_atomic_add_and_fetch(a, -(v)) +#endif + +#ifndef tb_atomic_or_and_fetch +# define tb_atomic_or_and_fetch(a, v) (tb_atomic_fetch_and_or(a, v) | (v)) +#endif + +#ifndef tb_atomic_xor_and_fetch +# define tb_atomic_xor_and_fetch(a, v) (tb_atomic_fetch_and_xor(a, v) ^ (v)) +#endif + +#ifndef tb_atomic_and_and_fetch +# define tb_atomic_and_and_fetch(a, v) (tb_atomic_fetch_and_and(a, v) & (v)) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ +static __tb_inline__ tb_long_t tb_atomic_fetch_and_pset_generic(tb_atomic_t* a, tb_long_t p, tb_long_t v) +{ + // FIXME + // no safe + + // done + tb_long_t o = *a; if (o == p) *a = v; + + // ok + return o; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_set_generic(tb_atomic_t* a, tb_long_t v) +{ + // done + tb_long_t o; + do + { + o = *a; + + } while (tb_atomic_fetch_and_pset(a, o, v) != o); + + // ok + return o; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_add_generic(tb_atomic_t* a, tb_long_t v) +{ + // done + tb_long_t o; + do + { + o = *a; + + } while (tb_atomic_fetch_and_pset(a, o, o + v) != o); + + // ok + return o; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_xor_generic(tb_atomic_t* a, tb_long_t v) +{ + // done + tb_long_t o; + do + { + o = *a; + + } while (tb_atomic_fetch_and_pset(a, o, o ^ v) != o); + + // ok + return o; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_and_generic(tb_atomic_t* a, tb_long_t v) +{ + // done + tb_long_t o; + do + { + o = *a; + + } while (tb_atomic_fetch_and_pset(a, o, o & v) != o); + + // ok + return o; +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_or_generic(tb_atomic_t* a, tb_long_t v) +{ + // done + tb_long_t o; + do + { + o = *a; + + } while (tb_atomic_fetch_and_pset(a, o, o | v) != o); + + // ok + return o; +} + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/atomic64.h b/core/pkg/tbox.pkg/inc/tbox/platform/atomic64.h index cee148961..0a2e1dd13 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/atomic64.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/atomic64.h @@ -226,3 +226,231 @@ tb_hong_t tb_atomic64_fetch_and_or_generic(tb_atomic64_t* __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic64.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_ATOMIC64_H +#define TB_PLATFORM_ATOMIC64_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "atomic.h" +#if !TB_CPU_BIT64 +# if defined(TB_CONFIG_OS_WINDOWS) +# include "windows/atomic64.h" +# elif defined(TB_COMPILER_IS_GCC) \ + && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 +# include "compiler/gcc/atomic64.h" +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if TB_CPU_BIT64 + +# define tb_atomic64_get(a) tb_atomic_get(a) +# define tb_atomic64_set(a, v) tb_atomic_set(a, v) +# define tb_atomic64_set0(a) tb_atomic_set0(a) +# define tb_atomic64_pset(a, p, v) tb_atomic_pset(a, p, v) +# define tb_atomic64_fetch_and_set0(a) tb_atomic_fetch_and_set0(a) +# define tb_atomic64_fetch_and_set(a, v) tb_atomic_fetch_and_set(a, v) +# define tb_atomic64_fetch_and_pset(a, p, v) tb_atomic_fetch_and_pset(a, p, v) + +# define tb_atomic64_fetch_and_inc(a) tb_atomic_fetch_and_inc(a) +# define tb_atomic64_fetch_and_dec(a) tb_atomic_fetch_and_dec(a) +# define tb_atomic64_fetch_and_add(a, v) tb_atomic_fetch_and_add(a, v) +# define tb_atomic64_fetch_and_sub(a, v) tb_atomic_fetch_and_sub(a, v) +# define tb_atomic64_fetch_and_or(a, v) tb_atomic_fetch_and_or(a, v) +# define tb_atomic64_fetch_and_xor(a, v) tb_atomic_fetch_and_xor(a, v) +# define tb_atomic64_fetch_and_and(a, v) tb_atomic_fetch_and_and(a, v) + +# define tb_atomic64_inc_and_fetch(a) tb_atomic_inc_and_fetch(a) +# define tb_atomic64_dec_and_fetch(a) tb_atomic_dec_and_fetch(a) +# define tb_atomic64_add_and_fetch(a, v) tb_atomic_add_and_fetch(a, v) +# define tb_atomic64_sub_and_fetch(a, v) tb_atomic_sub_and_fetch(a, v) +# define tb_atomic64_or_and_fetch(a, v) tb_atomic_or_and_fetch(a, v) +# define tb_atomic64_xor_and_fetch(a, v) tb_atomic_xor_and_fetch(a, v) +# define tb_atomic64_and_and_fetch(a, v) tb_atomic_and_and_fetch(a, v) + +#endif + +#ifndef tb_atomic64_fetch_and_pset +# define tb_atomic64_fetch_and_pset(a, p, v) tb_atomic64_fetch_and_pset_generic(a, p, v) +#endif + +#ifndef tb_atomic64_fetch_and_set +# define tb_atomic64_fetch_and_set(a, v) tb_atomic64_fetch_and_set_generic(a, v) +#endif + +#ifndef tb_atomic64_fetch_and_add +# define tb_atomic64_fetch_and_add(a, v) tb_atomic64_fetch_and_add_generic(a, v) +#endif + +#ifndef tb_atomic64_fetch_and_or +# define tb_atomic64_fetch_and_or(a, v) tb_atomic64_fetch_and_or_generic(a, v) +#endif + +#ifndef tb_atomic64_fetch_and_xor +# define tb_atomic64_fetch_and_xor(a, v) tb_atomic64_fetch_and_xor_generic(a, v) +#endif + +#ifndef tb_atomic64_fetch_and_and +# define tb_atomic64_fetch_and_and(a, v) tb_atomic64_fetch_and_and_generic(a, v) +#endif + +#ifndef tb_atomic64_get +# define tb_atomic64_get(a) tb_atomic64_fetch_and_pset(a, 0, 0) +#endif + +#ifndef tb_atomic64_set +# define tb_atomic64_set(a, v) tb_atomic64_fetch_and_set(a, v) +#endif + +#ifndef tb_atomic64_set0 +# define tb_atomic64_set0(a) tb_atomic64_set(a, 0) +#endif + +#ifndef tb_atomic64_pset +# define tb_atomic64_pset(a, p, v) tb_atomic64_fetch_and_pset(a, p, v) +#endif + +#ifndef tb_atomic64_fetch_and_set0 +# define tb_atomic64_fetch_and_set0(a) tb_atomic64_fetch_and_set(a, 0) +#endif + +#ifndef tb_atomic64_fetch_and_inc +# define tb_atomic64_fetch_and_inc(a) tb_atomic64_fetch_and_add(a, 1) +#endif + +#ifndef tb_atomic64_fetch_and_dec +# define tb_atomic64_fetch_and_dec(a) tb_atomic64_fetch_and_add(a, -1) +#endif + +#ifndef tb_atomic64_fetch_and_sub +# define tb_atomic64_fetch_and_sub(a, v) tb_atomic64_fetch_and_add(a, -(v)) +#endif + +#ifndef tb_atomic64_add_and_fetch +# define tb_atomic64_add_and_fetch(a, v) (tb_atomic64_fetch_and_add(a, v) + (v)) +#endif + +#ifndef tb_atomic64_inc_and_fetch +# define tb_atomic64_inc_and_fetch(a) tb_atomic64_add_and_fetch(a, 1) +#endif + +#ifndef tb_atomic64_dec_and_fetch +# define tb_atomic64_dec_and_fetch(a) tb_atomic64_add_and_fetch(a, -1) +#endif + +#ifndef tb_atomic64_sub_and_fetch +# define tb_atomic64_sub_and_fetch(a, v) tb_atomic64_add_and_fetch(a, -(v)) +#endif + +#ifndef tb_atomic64_or_and_fetch +# define tb_atomic64_or_and_fetch(a, v) (tb_atomic64_fetch_and_or(a, v) | (v)) +#endif + +#ifndef tb_atomic64_xor_and_fetch +# define tb_atomic64_xor_and_fetch(a, v) (tb_atomic64_fetch_and_xor(a, v) ^ (v)) +#endif + +#ifndef tb_atomic64_and_and_fetch +# define tb_atomic64_and_and_fetch(a, v) (tb_atomic64_fetch_and_and(a, v) & (v)) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* fetch and set the 64bits value + * + * @param a the atomic value + * @param v the assigned value + * + * @return the old value + */ +tb_hong_t tb_atomic64_fetch_and_set_generic(tb_atomic64_t* a, tb_hong_t v); + +/* fetch and set the 64bits value if old_value == p + * + * @param a the atomic value + * @param p the compared value + * @param v the assigned value + * + * @return the old value + */ +tb_hong_t tb_atomic64_fetch_and_pset_generic(tb_atomic64_t* a, tb_hong_t p, tb_hong_t v); + +/* fetch and add the 64bits value + * + * @param a the atomic value + * @param v the added value + * + * @return the old value + */ +tb_hong_t tb_atomic64_fetch_and_add_generic(tb_atomic64_t* a, tb_hong_t v); + +/* fetch and xor the 64bits value + * + * @param a the atomic value + * @param v the xor-value + * + * @return the old value + */ +tb_hong_t tb_atomic64_fetch_and_xor_generic(tb_atomic64_t* a, tb_hong_t v); + +/* fetch and and the 64bits value + * + * @param a the atomic value + * @param v the and-value + * + * @return the old value + */ +tb_hong_t tb_atomic64_fetch_and_and_generic(tb_atomic64_t* a, tb_hong_t v); + +/* fetch and or the 64bits value + * + * @param a the atomic value + * @param v the or-value + * + * @return the old value + */ +tb_hong_t tb_atomic64_fetch_and_or_generic(tb_atomic64_t* a, tb_hong_t v); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/backtrace.h b/core/pkg/tbox.pkg/inc/tbox/platform/backtrace.h index c93cc909e..6012f906a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/backtrace.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/backtrace.h @@ -90,3 +90,95 @@ tb_void_t tb_backtrace_dump(tb_char_t const* prefix, tb_pointer_t* __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file backtrace.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_BACKTRACE_H +#define TB_PLATFORM_BACKTRACE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! get backtrace frames + * + * @param frames the backtrace frames + * @param nframe the backtrace frame maxn + * @param nskip the backtrace frame skip + * + * @return the real backtrace frame count + */ +tb_size_t tb_backtrace_frames(tb_pointer_t* frames, tb_size_t nframe, tb_size_t nskip); + +/*! init backtrace frame symbols + * + * @param frames the backtrace frames + * @param nframe the backtrace frame count + * + * @return the backtrace frame symbols handle + */ +tb_handle_t tb_backtrace_symbols_init(tb_pointer_t* frames, tb_size_t nframe); + +/*! get backtrace frame symbol name + * + * @param symbols the symbols handle + * @param frames the backtrace frames + * @param nframe the backtrace frame count + * @param frame the backtrace frame index + * + * @return the symbol name + */ +tb_char_t const* tb_backtrace_symbols_name(tb_handle_t symbols, tb_pointer_t* frames, tb_size_t nframe, tb_size_t iframe); + +/*! exit backtrace frame symbols + * + * @param symbols the symbols handle + */ +tb_void_t tb_backtrace_symbols_exit(tb_handle_t symbols); + +/*! dump backtrace + * + * @param prefix the backtrace prefix + * @param frames the backtrace frames, dump the current frames if null + * @param nframe the backtrace frame count + */ +tb_void_t tb_backtrace_dump(tb_char_t const* prefix, tb_pointer_t* frames, tb_size_t nframe); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/barrier.h index d8ac67740..2a9354cef 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/barrier.h @@ -49,3 +49,54 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_BARRIER_H +#define TB_PLATFORM_BARRIER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#if defined(TB_CONFIG_OS_MACOSX) || defined(TB_CONFIG_OS_IOS) +# include "mach/barrier.h" +#elif defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(4, 1) +# include "compiler/gcc/barrier.h" +#elif defined(TB_CONFIG_OS_WINDOWS) +# include "windows/barrier.h" +#endif +#include "arch/barrier.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +#ifndef tb_barrier +# define tb_barrier() +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/cache_time.h b/core/pkg/tbox.pkg/inc/tbox/platform/cache_time.h index 1c33b806f..22bdb264d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/cache_time.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/cache_time.h @@ -78,3 +78,83 @@ tb_hong_t tb_cache_time_clock(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file cache_time.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_CACHE_TIME_H +#define TB_PLATFORM_CACHE_TIME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the cached time, like tb_time + * + * lower accuracy and faster + * + * @return the now time, s + */ +tb_time_t tb_cache_time(tb_noarg_t); + +/*! spak cached time + * + * update the cached time for the external loop thread + * + * @return the now ms-clock + */ +tb_hong_t tb_cache_time_spak(tb_noarg_t); + +/*! the cached ms-clock + * + * lower accuracy and faster + * + * @return the now ms-clock + */ +tb_hong_t tb_cache_time_mclock(tb_noarg_t); + +/*! the cached s-clock + * + * lower accuracy and faster + * + * @return the now s-clock + */ +tb_hong_t tb_cache_time_clock(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic.h b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic.h index 78d21e8e3..337372a7e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic.h @@ -111,3 +111,116 @@ static __tb_inline__ tb_long_t tb_atomic_or_and_fetch_sync(tb_atomic_t* a, tb_lo } #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic.h + * + */ +#ifndef TB_PLATFORM_COMPILER_GCC_ATOMIC_H +#define TB_PLATFORM_COMPILER_GCC_ATOMIC_H + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#define tb_atomic_fetch_and_set(a, v) tb_atomic_fetch_and_set_sync(a, v) +#define tb_atomic_fetch_and_pset(a, p, v) tb_atomic_fetch_and_pset_sync(a, p, v) + +#define tb_atomic_fetch_and_add(a, v) tb_atomic_fetch_and_add_sync(a, v) +#define tb_atomic_fetch_and_sub(a, v) tb_atomic_fetch_and_sub_sync(a, v) +#define tb_atomic_fetch_and_or(a, v) tb_atomic_fetch_and_or_sync(a, v) +#define tb_atomic_fetch_and_and(a, v) tb_atomic_fetch_and_and_sync(a, v) + +#define tb_atomic_add_and_fetch(a, v) tb_atomic_add_and_fetch_sync(a, v) +#define tb_atomic_sub_and_fetch(a, v) tb_atomic_sub_and_fetch_sync(a, v) +#define tb_atomic_or_and_fetch(a, v) tb_atomic_or_and_fetch_sync(a, v) +#define tb_atomic_and_and_fetch(a, v) tb_atomic_and_and_fetch_sync(a, v) + +// FIXME: ios armv6: no defined refernece? +#if !(defined(TB_CONFIG_OS_IOS) && TB_ARCH_ARM_VERSION < 7) +# define tb_atomic_fetch_and_xor(a, v) tb_atomic_fetch_and_xor_sync(a, v) +# define tb_atomic_xor_and_fetch(a, v) tb_atomic_xor_and_fetch_sync(a, v) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ +static __tb_inline__ tb_long_t tb_atomic_fetch_and_set_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_lock_test_and_set(a, v); +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_pset_sync(tb_atomic_t* a, tb_long_t p, tb_long_t v) +{ + return __sync_val_compare_and_swap(a, p, v); +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_add_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_fetch_and_add(a, v); +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_sub_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_fetch_and_sub(a, v); +} +#if !(defined(TB_CONFIG_OS_IOS) && (TB_ARCH_ARM_VERSION < 7)) +static __tb_inline__ tb_long_t tb_atomic_fetch_and_xor_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_fetch_and_xor(a, v); +} +#endif +static __tb_inline__ tb_long_t tb_atomic_fetch_and_and_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_fetch_and_and(a, v); +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_or_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_fetch_and_or(a, v); +} +static __tb_inline__ tb_long_t tb_atomic_add_and_fetch_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_add_and_fetch(a, v); +} +static __tb_inline__ tb_long_t tb_atomic_sub_and_fetch_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_sub_and_fetch(a, v); +} +#if !(defined(TB_CONFIG_OS_IOS) && (TB_ARCH_ARM_VERSION < 7)) +static __tb_inline__ tb_long_t tb_atomic_xor_and_fetch_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_xor_and_fetch(a, v); +} +#endif +static __tb_inline__ tb_long_t tb_atomic_and_and_fetch_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_and_and_fetch(a, v); +} +static __tb_inline__ tb_long_t tb_atomic_or_and_fetch_sync(tb_atomic_t* a, tb_long_t v) +{ + return __sync_or_and_fetch(a, v); +} + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic64.h b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic64.h index 0c6364a64..8b40d0cbc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic64.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/atomic64.h @@ -111,3 +111,116 @@ static __tb_inline__ tb_hong_t tb_atomic64_or_and_fetch_sync(tb_atomic64_t* a, t } #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic64.h + * + */ +#ifndef TB_PLATFORM_COMPILER_GCC_ATOMIC64_H +#define TB_PLATFORM_COMPILER_GCC_ATOMIC64_H + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#define tb_atomic64_fetch_and_set(a, v) tb_atomic64_fetch_and_set_sync(a, v) +#define tb_atomic64_fetch_and_pset(a, p, v) tb_atomic64_fetch_and_pset_sync(a, p, v) + +#define tb_atomic64_fetch_and_add(a, v) tb_atomic64_fetch_and_add_sync(a, v) +#define tb_atomic64_fetch_and_sub(a, v) tb_atomic64_fetch_and_sub_sync(a, v) +#define tb_atomic64_fetch_and_or(a, v) tb_atomic64_fetch_and_or_sync(a, v) +#define tb_atomic64_fetch_and_and(a, v) tb_atomic64_fetch_and_and_sync(a, v) + +#define tb_atomic64_add_and_fetch(a, v) tb_atomic64_add_and_fetch_sync(a, v) +#define tb_atomic64_sub_and_fetch(a, v) tb_atomic64_sub_and_fetch_sync(a, v) +#define tb_atomic64_or_and_fetch(a, v) tb_atomic64_or_and_fetch_sync(a, v) +#define tb_atomic64_and_and_fetch(a, v) tb_atomic64_and_and_fetch_sync(a, v) + +// FIXME: ios armv6: no defined refernece? +#if !(defined(TB_CONFIG_OS_IOS) && TB_ARCH_ARM_VERSION < 7) +# define tb_atomic64_fetch_and_xor(a, v) tb_atomic64_fetch_and_xor_sync(a, v) +# define tb_atomic64_xor_and_fetch(a, v) tb_atomic64_xor_and_fetch_sync(a, v) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_set_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_lock_test_and_set_8(a, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_pset_sync(tb_atomic64_t* a, tb_hong_t p, tb_hong_t v) +{ + return __sync_val_compare_and_swap_8(a, p, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_add_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_fetch_and_add_8(a, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_sub_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_fetch_and_sub_8(a, v); +} +#if !(defined(TB_CONFIG_OS_IOS) && (TB_ARCH_ARM_VERSION < 7)) +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_xor_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_fetch_and_xor_8(a, v); +} +#endif +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_and_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_fetch_and_and_8(a, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_or_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_fetch_and_or_8(a, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_add_and_fetch_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_add_and_fetch_8(a, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_sub_and_fetch_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_sub_and_fetch_8(a, v); +} +#if !(defined(TB_CONFIG_OS_IOS) && (TB_ARCH_ARM_VERSION < 7)) +static __tb_inline__ tb_hong_t tb_atomic64_xor_and_fetch_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_xor_and_fetch_8(a, v); +} +#endif +static __tb_inline__ tb_hong_t tb_atomic64_and_and_fetch_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_and_and_fetch_8(a, v); +} +static __tb_inline__ tb_hong_t tb_atomic64_or_and_fetch_sync(tb_atomic64_t* a, tb_hong_t v) +{ + return __sync_or_and_fetch_8(a, v); +} + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/barrier.h index 355b13539..a86edfc3b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/barrier.h @@ -39,3 +39,44 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * + */ +#ifndef TB_PLATFORM_COMPILER_GCC_BARRIER_H +#define TB_PLATFORM_COMPILER_GCC_BARRIER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifndef tb_barrier +# define tb_barrier() __sync_synchronize() +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/prefix.h index d7c2e8f94..74066ebfa 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/gcc/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_COMPILER_GCC_PREFIX_H +#define TB_PLATFORM_COMPILER_GCC_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/prefix.h index 4466b8e95..3cd8d581c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/compiler/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/compiler/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_COMPILER_PREFIX_H +#define TB_PLATFORM_COMPILER_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/context.h b/core/pkg/tbox.pkg/inc/tbox/platform/context.h index 13fd69979..199d03db4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/context.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/context.h @@ -89,3 +89,94 @@ tb_context_from_t tb_context_jump(tb_context_ref_t context, tb_cpointer_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file context.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_CONTEXT_H +#define TB_PLATFORM_CONTEXT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the context ref type +typedef __tb_typeref__(context); + +// the context-from type +typedef struct __tb_context_from_t +{ + // the from-context + tb_context_ref_t context; + + // the passed user private data + tb_cpointer_t priv; + +}tb_context_from_t; + +/*! the context entry function type + * + * @param from the from-context + */ +typedef tb_void_t (*tb_context_func_t)(tb_context_from_t from); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! make context from the given the stack space and the callback function + * + * @param stackdata the stack data + * @param stacksize the stack size + * @param func the entry function + * + * @return the context pointer + */ +tb_context_ref_t tb_context_make(tb_byte_t* stackdata, tb_size_t stacksize, tb_context_func_t func); + +/*! jump to the given context + * + * @param context the to-context + * @param priv the passed user private data + * + * @return the from-context + */ +tb_context_from_t tb_context_jump(tb_context_ref_t context, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/deprecated.h b/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/deprecated.h index 19bf52e10..08682fc8c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/deprecated.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/deprecated.h @@ -31,3 +31,36 @@ #include "prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file deprecated.h + * + */ +#ifndef TB_PLATFORM_DEPRECATED_H +#define TB_PLATFORM_DEPRECATED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/prefix.h index 722b6448d..7b798bcf5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/deprecated/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_DEPRECATED_PREFIX_H +#define TB_PLATFORM_DEPRECATED_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/directory.h b/core/pkg/tbox.pkg/inc/tbox/platform/directory.h index 9b6cb0771..37e99df79 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/directory.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/directory.h @@ -132,3 +132,137 @@ tb_bool_t tb_directory_copy(tb_char_t const* path, tb_char_t const __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file directory.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_DIRECTORY_H +#define TB_PLATFORM_DIRECTORY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "file.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the directory walk func type + * + * @param path the file path + * @param info the file info + * @param priv the user private data + * + * @return continue: tb_true, break: tb_false + */ +typedef tb_bool_t (*tb_directory_walk_func_t)(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! create the directory + * + * @param path the directory path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_directory_create(tb_char_t const* path); + +/*! remove the directory + * + * @param path the directory path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_directory_remove(tb_char_t const* path); + +/*! the home directory + * + * @param path the directory path data + * @param maxn the directory path maxn + * + * @return the directory path size + */ +tb_size_t tb_directory_home(tb_char_t* path, tb_size_t maxn); + +/*! the current directory + * + * @param path the directory path data + * @param maxn the directory path maxn + * + * @return the directory path size + */ +tb_size_t tb_directory_current(tb_char_t* path, tb_size_t maxn); + +/*! set the current directory + * + * @param path the directory path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_directory_current_set(tb_char_t const* path); + +/*! the temporary directory + * + * @param path the directory path data + * @param maxn the directory path maxn + * + * @return the directory path size + */ +tb_size_t tb_directory_temporary(tb_char_t* path, tb_size_t maxn); + +/*! the directory walk + * + * @param path the directory path + * @param recursion is recursion? + * @param prefix is prefix recursion? directory is the first item + * @param func the callback func + * @param data the callback data + * + */ +tb_void_t tb_directory_walk(tb_char_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv); + +/*! copy directory + * + * @param path the directory path + * @param dest the directory dest + * + * @return tb_true or tb_false + */ +tb_bool_t tb_directory_copy(tb_char_t const* path, tb_char_t const* dest); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/dynamic.h b/core/pkg/tbox.pkg/inc/tbox/platform/dynamic.h index 03ab5d927..14a40560e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/dynamic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/dynamic.h @@ -85,3 +85,90 @@ tb_pointer_t tb_dynamic_pvar(tb_dynamic_ref_t dynamic, tb_char_t const* n __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file mutex.h + * @ingroup dynamic + * + */ +#ifndef TB_PLATFORM_DYNAMIC_H +#define TB_PLATFORM_DYNAMIC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the dynamic ref type +typedef __tb_typeref__(dynamic); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init dynamic library + * + * @param name the library name + * + * @return the dynamic library + */ +tb_dynamic_ref_t tb_dynamic_init(tb_char_t const* name); + +/*! exit dynamic library + * + * @param dynamic the dynamic library + */ +tb_void_t tb_dynamic_exit(tb_dynamic_ref_t dynamic); + +/*! the dynamic library function + * + * @param dynamic the dynamic library + * @param name the function name + * + * @return the function address + */ +tb_pointer_t tb_dynamic_func(tb_dynamic_ref_t dynamic, tb_char_t const* name); + +/*! the dynamic library variable + * + * @param dynamic the dynamic library + * @param name the variable name + * + * @return the variable address + */ +tb_pointer_t tb_dynamic_pvar(tb_dynamic_ref_t dynamic, tb_char_t const* name); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/environment.h b/core/pkg/tbox.pkg/inc/tbox/platform/environment.h index a580c6f68..d404c0712 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/environment.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/environment.h @@ -270,3 +270,275 @@ tb_bool_t tb_environment_remove(tb_char_t const* name); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file environment.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_ENVIRONMENT_H +#define TB_PLATFORM_ENVIRONMENT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../container/iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the environment variable ref type +typedef tb_iterator_ref_t tb_environment_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the environment variable + * + * @return the environment variable + */ +tb_environment_ref_t tb_environment_init(tb_noarg_t); + +/*! exit the environment variable + * + * @param environment the environment variable + */ +tb_void_t tb_environment_exit(tb_environment_ref_t environment); + +/*! the environment variable count + * + * @param environment the environment variable + * + * @return the environment variable count + */ +tb_size_t tb_environment_size(tb_environment_ref_t environment); + +/*! load the environment variable from the given name + * + * @code + * + // init environment + tb_environment_ref_t environment = tb_environment_init(); + if (environment) + { + // load variable + if (tb_environment_load(environment, "PATH")) + { + // trace + tb_trace_i("%s:", name); + + // dump values + tb_for_all_if (tb_char_t const*, value, environment, value) + { + // trace + tb_trace_i(" %s", value); + } + } + + // exit environment + tb_environment_exit(environment); + } + * @endcode + * + * @param environment the environment variable + * @param name the variable name + * + * @return the count of the variable value + */ +tb_size_t tb_environment_load(tb_environment_ref_t environment, tb_char_t const* name); + +/*! save the environment variable and will overwrite it + * + * we will remove this environment variable if environment is null or empty + * + * @code + * + // init environment + tb_environment_ref_t environment = tb_environment_init(); + if (environment) + { + // insert values + tb_environment_insert(environment, "/xxx/0", tb_false); + tb_environment_insert(environment, "/xxx/1", tb_false); + tb_environment_insert(environment, "/xxx/2", tb_false); + tb_environment_insert(environment, "/xxx/3", tb_false); + + // save variable + tb_environment_save(environment, "PATH"); + + // exit environment + tb_environment_exit(environment); + } + + * @endcode + * + * @param environment the environment variable + * @param name the variable name + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_save(tb_environment_ref_t environment, tb_char_t const* name); + +/*! get the environment variable from the given index + * + * @code + * + // init environment + tb_environment_ref_t environment = tb_environment_init(); + if (environment) + { + // load variable + if (tb_environment_load(environment, "PATH")) + { + tb_char_t const* value = tb_environment_at(environment, 0); + if (value) + { + // ... + } + } + + // exit environment + tb_environment_exit(environment); + } + * @endcode + * + * + * @param environment the environment variable + * @param index the variable index + * + * @return the variable value + */ +tb_char_t const* tb_environment_at(tb_environment_ref_t environment, tb_size_t index); + +/*! replace the environment variable and will overwrite it + * + * we will clear environment and overwrite it + * + * @param environment the environment variable + * @param value the variable value, will clear it if be null + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_replace(tb_environment_ref_t environment, tb_char_t const* value); + +/*! set the environment variable + * + * @param environment the environment variable + * @param value the variable value + * @param to_head insert value into the head? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_insert(tb_environment_ref_t environment, tb_char_t const* value, tb_bool_t to_head); + +#ifdef __tb_debug__ +/*! dump the environment variable + * + * @param environment the environment variable + * @param name the variable name + */ +tb_void_t tb_environment_dump(tb_environment_ref_t environment, tb_char_t const* name); +#endif + +/*! get the first environment variable value + * + * @code + + tb_char_t value[TB_PATH_MAXN]; + if (tb_environment_first("HOME", value, sizeof(value))) + { + // ... + } + + * @endcode + * + * @param name the variable name + * @param value the variable value + * @param maxn the variable value maxn + * + * @return the variable value size + */ +tb_size_t tb_environment_first(tb_char_t const* name, tb_char_t* value, tb_size_t maxn); + +/*! get the environment variable values + * + * @code + + tb_char_t value[TB_PATH_MAXN]; + if (tb_environment_get("HOME", value, sizeof(value))) + { + // ... + } + + * @endcode + * + * @param name the variable name + * @param values the variable values, separator: windows(';') or other(';') + * @param maxn the variable values maxn + * + * @return the variable values size + */ +tb_size_t tb_environment_get(tb_char_t const* name, tb_char_t* values, tb_size_t maxn); + +/*! set the environment variable values + * + * we will set all values and overwrite it + * + * @param name the variable name + * @param values the variable values, separator: windows(';') or other(';') + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_set(tb_char_t const* name, tb_char_t const* values); + +/*! add the environment variable values and not overwrite it + * + * @param name the variable name + * @param values the variable values, separator: windows(';') or other(';') + * @param to_head add value into the head? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_add(tb_char_t const* name, tb_char_t const* values, tb_bool_t to_head); + +/*! remove the given environment variable + * + * @param name the variable name + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_remove(tb_char_t const* name); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/event.h b/core/pkg/tbox.pkg/inc/tbox/platform/event.h index 19b4104c5..9b93018e9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/event.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/event.h @@ -76,3 +76,81 @@ tb_long_t tb_event_wait(tb_event_ref_t event, tb_long_t timeout); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file event.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_EVENT_H +#define TB_PLATFORM_EVENT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init event + * + * @return the event + */ +tb_event_ref_t tb_event_init(tb_noarg_t); + +/*! exit event + * + * @param event the event + */ +tb_void_t tb_event_exit(tb_event_ref_t event); + +/*! post event + * + * @param event the event + * + * @return tb_true or tb_false + */ +tb_bool_t tb_event_post(tb_event_ref_t event); + +/*! wait event + * + * @param event the event + * @param timeout the timeout + * + * @return ok: 1, timeout: 0, fail: -1 + */ +tb_long_t tb_event_wait(tb_event_ref_t event, tb_long_t timeout); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/exception.h b/core/pkg/tbox.pkg/inc/tbox/platform/exception.h index 05373c4b8..55c2d0f36 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/exception.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/exception.h @@ -87,3 +87,92 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file exception.h + * + */ +#ifndef TB_PLATFORM_EXCEPTION_H +#define TB_PLATFORM_EXCEPTION_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_EXCEPTION_ENABLE +# include "../libc/misc/signal.h" +# if defined(TB_CONFIG_OS_WINDOWS) +# include "windows/exception.h" +# elif defined(tb_signal) +# include "libc/exception.h" +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// try +#ifndef __tb_try +# define __tb_try do +#endif + +// except +#ifndef __tb_except +# define __tb_except(x) while (0); if (0) +#endif + +// leave +#ifndef __tb_leave +# define __tb_leave break +#endif + +// end +#ifndef __tb_end +# define __tb_end +#endif + +// check +#define tb_check_leave(x) { if (!(x)) __tb_leave ; } + +// assert +#ifdef __tb_debug__ +# define tb_assert_leave(x) { if (!(x)) {tb_trace_a("expr: %s", #x); __tb_leave ; } } +# define tb_assert_and_check_leave(x) tb_assert_leave(x) +#else +# define tb_assert_leave(x) +# define tb_assert_and_check_leave(x) tb_check_leave(x) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/file.h b/core/pkg/tbox.pkg/inc/tbox/platform/file.h index 4bc7bd59f..ac058b354 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/file.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/file.h @@ -313,3 +313,318 @@ tb_bool_t tb_file_link(tb_char_t const* path, tb_char_t const* des __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file file.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_FILE_H +#define TB_PLATFORM_FILE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the aligned size for direct mode +#define TB_FILE_DIRECT_ASIZE (512) + +/// the cached size for direct mode +#ifdef __tb_small__ +# define TB_FILE_DIRECT_CSIZE (1 << 14) +#else +# define TB_FILE_DIRECT_CSIZE (1 << 17) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the file mode type +typedef enum __tb_file_mode_t +{ + TB_FILE_MODE_RO = 1 //!< read only +, TB_FILE_MODE_WO = 2 //!< writ only +, TB_FILE_MODE_RW = 4 //!< read and writ +, TB_FILE_MODE_CREAT = 8 //!< create +, TB_FILE_MODE_APPEND = 16 //!< append +, TB_FILE_MODE_TRUNC = 32 //!< truncate +, TB_FILE_MODE_DIRECT = 64 //!< direct, no cache, @note data & size must be aligned by TB_FILE_DIRECT_ASIZE +, TB_FILE_MODE_ASIO = 128 //!< support for asio + +, TB_FILE_MODE_BINARY = 256 //!< binary (deprecated) + +}tb_file_mode_t; + +/// the file seek type +typedef enum __tb_file_seek_flag_t +{ + TB_FILE_SEEK_BEG = 0 +, TB_FILE_SEEK_CUR = 1 +, TB_FILE_SEEK_END = 2 + +}tb_file_seek_flag_t; + +/// the file type +typedef enum __tb_file_type_t +{ + TB_FILE_TYPE_NONE = 0 +, TB_FILE_TYPE_DIRECTORY = 1 +, TB_FILE_TYPE_FILE = 2 +, TB_FILE_TYPE_DOT = 3 +, TB_FILE_TYPE_DOT2 = 4 + +}tb_file_type_t; + +/// the file info type +typedef struct __tb_file_info_t +{ + /// the file type + tb_size_t type; + + /// the file size + tb_hize_t size; + + /// the last access time + tb_time_t atime; + + /// the last modify time + tb_time_t mtime; + +}tb_file_info_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the file + * + * @param path the file path + * @param mode the file mode + * + * @return the file + */ +tb_file_ref_t tb_file_init(tb_char_t const* path, tb_size_t mode); + +/*! exit the file + * + * @param file the file + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_exit(tb_file_ref_t file); + +/*! read the file data + * + * @param file the file + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_file_read(tb_file_ref_t file, tb_byte_t* data, tb_size_t size); + +/*! writ the file data + * + * @param file the file + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_file_writ(tb_file_ref_t file, tb_byte_t const* data, tb_size_t size); + +/*! pread the file data + * + * @param file the file + * @param data the data + * @param size the size + * @param offset the offset, the file offset will not be changed + * + * @return the real size or -1 + */ +tb_long_t tb_file_pread(tb_file_ref_t file, tb_byte_t* data, tb_size_t size, tb_hize_t offset); + +/*! pwrit the file data + * + * @param file the file + * @param data the data + * @param size the size + * @param offset the offset, the file offset will not be changed + * + * @return the real size or -1 + */ +tb_long_t tb_file_pwrit(tb_file_ref_t file, tb_byte_t const* data, tb_size_t size, tb_hize_t offset); + +/*! readv the file data + * + * @param file the file + * @param list the iovec list + * @param size the iovec size + * + * @return the real size or -1 + */ +tb_long_t tb_file_readv(tb_file_ref_t file, tb_iovec_t const* list, tb_size_t size); + +/*! writv the file data + * + * @param file the file + * @param list the iovec list + * @param size the iovec size + * + * @return the real size or -1 + */ +tb_long_t tb_file_writv(tb_file_ref_t file, tb_iovec_t const* list, tb_size_t size); + +/*! writf the file data + * + * @param file the file + * @param ifile the input file + * @param offset the input file offset, the file offset will not be changed + * @param size the writed size + * + * @return the real size or -1 + */ +tb_hong_t tb_file_writf(tb_file_ref_t file, tb_file_ref_t ifile, tb_hize_t offset, tb_hize_t size); + +/*! preadv the file data + * + * @param file the file + * @param list the iovec list + * @param size the iovec size + * @param offset the offset, the file offset will not be changed + * + * @return the real size or -1 + */ +tb_long_t tb_file_preadv(tb_file_ref_t file, tb_iovec_t const* list, tb_size_t size, tb_hize_t offset); + +/*! pwritv the file data + * + * @param file the file + * @param list the iovec list + * @param size the iovec size + * @param offset the offset, the file offset will not be changed + * + * @return the real size or -1 + */ +tb_long_t tb_file_pwritv(tb_file_ref_t file, tb_iovec_t const* list, tb_size_t size, tb_hize_t offset); + +/*! seek the file offset + * + * @param file the file + * @param offset the file offset + * @param mode the seek mode + * + * @return the real offset or -1 + */ +tb_hong_t tb_file_seek(tb_file_ref_t file, tb_hong_t offset, tb_size_t mode); + +/*! fsync the file + * + * @param file the file + */ +tb_bool_t tb_file_sync(tb_file_ref_t file); + +/*! the file size + * + * @param file the file + * + * @return the file size + */ +tb_hize_t tb_file_size(tb_file_ref_t file); + +/*! the file offset + * + * @param file the file + * + * @return the file offset or -1 + */ +tb_hong_t tb_file_offset(tb_file_ref_t file); + +/*! the file info for file or directory + * + * @param file the file handle + * @param info the file info + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_info(tb_char_t const* path, tb_file_info_t* info); + +/*! copy the file + * + * @param path the file path + * @param dest the dest path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_copy(tb_char_t const* path, tb_char_t const* dest); + +/*! create the file + * + * @param path the file path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_create(tb_char_t const* path); + +/*! remove the file + * + * @param path the file path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_remove(tb_char_t const* path); + +/*! rename the file + * + * @param path the source file path + * @param dest the destination file path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_rename(tb_char_t const* path, tb_char_t const* dest); + +/*! link the file + * + * @param path the source file path + * @param dest the destination file path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_file_link(tb_char_t const* path, tb_char_t const* dest); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/hostname.h b/core/pkg/tbox.pkg/inc/tbox/platform/hostname.h index fa6caefa1..6a763cb22 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/hostname.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/hostname.h @@ -57,3 +57,62 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file hostname.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_HOSTNAME_H +#define TB_PLATFORM_HOSTNAME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the host name + * + * @param name the hostname data + * @param size the hostname size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_hostname(tb_char_t* name, tb_size_t size); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/ifaddrs.h b/core/pkg/tbox.pkg/inc/tbox/platform/ifaddrs.h index 3969885de..0b0c28750 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/ifaddrs.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/ifaddrs.h @@ -164,3 +164,169 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ifaddrs.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_IFADDRS_H +#define TB_PLATFORM_IFADDRS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../network/ipaddr.h" +#include "../network/hwaddr.h" +#include "../container/iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the ifaddrs interface flag enum +typedef enum __tb_ifaddrs_interface_flag_e +{ + TB_IFADDRS_INTERFACE_FLAG_NONE = 0 +, TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR4 = 1 +, TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR6 = 2 +, TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR = TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR4 | TB_IFADDRS_INTERFACE_FLAG_HAVE_IPADDR6 +, TB_IFADDRS_INTERFACE_FLAG_HAVE_HWADDR = 4 +, TB_IFADDRS_INTERFACE_FLAG_IS_LOOPBACK = 8 + +}tb_ifaddrs_interface_flag_e; + +/// the ifaddrs interface type +typedef struct __tb_ifaddrs_interface_t +{ + /// the interface name + tb_char_t const* name; + + /// the interface flags + tb_uint32_t flags; + + // the hardware address + tb_hwaddr_t hwaddr; + + // the ipv4 address + tb_ipv4_t ipaddr4; + + // the ipv6 address + tb_ipv6_t ipaddr6; + +}tb_ifaddrs_interface_t, *tb_ifaddrs_interface_ref_t; + +/// the ifaddrs type +typedef __tb_typeref__(ifaddrs); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the ifaddrs instance + * + * @return the ifaddrs + */ +tb_ifaddrs_ref_t tb_ifaddrs(tb_noarg_t); + +/*! init ifaddrs + * + * @return the ifaddrs + */ +tb_ifaddrs_ref_t tb_ifaddrs_init(tb_noarg_t); + +/*! exit ifaddrs + * + * @param ifaddrs the ifaddrs + */ +tb_void_t tb_ifaddrs_exit(tb_ifaddrs_ref_t ifaddrs); + +/*! the ifaddrs interface iterator + * + * @code + * tb_for_all_if (tb_ifaddrs_interface_ref_t, interface, tb_ifaddrs_itor(ifaddrs, tb_false), interface) + * { + * // ... + * } + * @endcode + * + * @param ifaddrs the ifaddrs + * @param reload force to reload the ifaddrs list, will cache list if be false + * + * @return the interface iterator + */ +tb_iterator_ref_t tb_ifaddrs_itor(tb_ifaddrs_ref_t ifaddrs, tb_bool_t reload); + +/*! get the interface from the given interface name + * + * @param ifaddrs the ifaddrs + * @param name the interface name + * @param reload force to reload the ifaddrs list, will cache list if be false + * + * @return tb_true or tb_false + */ +tb_ifaddrs_interface_ref_t tb_ifaddrs_interface(tb_ifaddrs_ref_t ifaddrs, tb_char_t const* name, tb_bool_t reload); + +/*! the hardware address from the given interface name + * + * @param ifaddrs the ifaddrs + * @param name the interface name, get the first ether address if be null + * @param reload force to reload the ifaddrs list, will cache list if be false + * @param hwaddr the hardware address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ifaddrs_hwaddr(tb_ifaddrs_ref_t ifaddrs, tb_char_t const* name, tb_bool_t reload, tb_hwaddr_ref_t hwaddr); + +/*! the hardware address from the given interface name + * + * @param ifaddrs the ifaddrs + * @param name the interface name, get the first ether address if be null + * @param reload force to reload the ifaddrs list, will cache list if be false + * @param family the address family + * @param ipaddr the ip address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ifaddrs_ipaddr(tb_ifaddrs_ref_t ifaddrs, tb_char_t const* name, tb_bool_t reload, tb_size_t family, tb_ipaddr_ref_t ipaddr); + +#ifdef __tb_debug__ +/*! dump the ifaddrs + * + * @param ifaddrs the ifaddrs + */ +tb_void_t tb_ifaddrs_dump(tb_ifaddrs_ref_t ifaddrs); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/libc/exception.h b/core/pkg/tbox.pkg/inc/tbox/platform/libc/exception.h index 58e97ca70..6b9178956 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/libc/exception.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/libc/exception.h @@ -175,3 +175,180 @@ extern tb_thread_local_t g_exception_local; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file exception.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_LIBC_EXCEPTION_H +#define TB_PLATFORM_LIBC_EXCEPTION_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../thread_local.h" +#include "../../libc/misc/setjmp.h" +#include "../../container/container.h" +#ifdef TB_CONFIG_LIBC_HAVE_KILL +# include <unistd.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * globals + */ +extern tb_thread_local_t g_exception_local; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if defined(tb_sigsetjmp) && defined(tb_siglongjmp) + + // try +# define __tb_try \ + do \ + { \ + /* init exception stack */ \ + tb_stack_ref_t __stack = tb_null; \ + if (!(__stack = (tb_stack_ref_t)tb_thread_local_get(&g_exception_local))) \ + { \ + tb_stack_ref_t __stack_new = tb_stack_init(16, tb_element_mem(sizeof(tb_sigjmpbuf_t), tb_null, tb_null)); \ + if (__stack_new && tb_thread_local_set(&g_exception_local, __stack_new)) \ + __stack = __stack_new; \ + else if (__stack_new) \ + tb_stack_exit(__stack_new); \ + } \ + \ + /* push jmpbuf */ \ + tb_sigjmpbuf_t* __top = tb_null; \ + if (__stack) \ + { \ + tb_sigjmpbuf_t __buf; \ + tb_stack_put(__stack, &__buf); \ + __top = (tb_sigjmpbuf_t*)tb_stack_top(__stack); \ + } \ + \ + /* init jmpbuf and save sigmask */ \ + __tb_volatile__ tb_int_t __j = __top? tb_sigsetjmp(*__top, 1) : 0; \ + /* done try */ \ + if (!__j) \ + { + + // except +# define __tb_except(x) \ + } \ + \ + /* check */ \ + tb_assert(x >= 0); \ + /* pop the jmpbuf */ \ + if (__stack) tb_stack_pop(__stack); \ + /* do not this catch? */ \ + if (__j && !(x)) \ + { \ + /* goto the top exception stack */ \ + if (__stack && tb_stack_size(__stack)) \ + { \ + tb_sigjmpbuf_t* jmpbuf = (tb_sigjmpbuf_t*)tb_stack_top(__stack); \ + if (jmpbuf) tb_siglongjmp(*jmpbuf, 1); \ + } \ + else \ + { \ + /* no exception handler */ \ + tb_assert_and_check_break(0); \ + } \ + } \ + /* exception been catched? */ \ + if (__j) + +#else + + // try +# define __tb_try \ + do \ + { \ + /* init exception stack */ \ + tb_stack_ref_t __stack = tb_null; \ + if (!(__stack = (tb_stack_ref_t)tb_thread_local_get(&g_exception_local))) \ + { \ + tb_stack_ref_t __stack_new = tb_stack_init(16, tb_element_mem(sizeof(tb_jmpbuf_t), tb_null, tb_null)); \ + if (__stack_new && tb_thread_local_set(&g_exception_local, __stack_new)) \ + __stack = __stack_new; \ + else if (__stack_new) \ + tb_stack_exit(__stack_new); \ + } \ + \ + /* push jmpbuf */ \ + tb_jmpbuf_t* __top = tb_null; \ + if (__stack) \ + { \ + tb_jmpbuf_t __buf; \ + tb_stack_put(__stack, &__buf); \ + __top = (tb_jmpbuf_t*)tb_stack_top(__stack); \ + } \ + \ + /* init jmpbuf */ \ + __tb_volatile__ tb_int_t __j = __top? tb_setjmp(*__top) : 0; \ + /* done try */ \ + if (!__j) \ + { + + // except +# define __tb_except(x) \ + } \ + \ + /* check */ \ + tb_assert(x >= 0); \ + /* pop the jmpbuf */ \ + if (__stack) tb_stack_pop(__stack); \ + /* do not this catch? */ \ + if (__j && !(x)) \ + { \ + /* goto the top exception stack */ \ + if (__stack && tb_stack_size(__stack)) \ + { \ + tb_jmpbuf_t* jmpbuf = (tb_jmpbuf_t*)tb_stack_top(__stack); \ + if (jmpbuf) tb_longjmp(*jmpbuf, 1); \ + } \ + else \ + { \ + /* no exception handler */ \ + tb_assert_and_check_break(0); \ + } \ + } \ + /* exception been catched? */ \ + if (__j) + +#endif + + // end +#define __tb_end \ + } while (0); + + // leave +#define __tb_leave break + + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/libc/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/libc/prefix.h index 7664333df..670aa2830 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/libc/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/libc/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_LIBC_PREFIX_H +#define TB_PLATFORM_LIBC_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../platform.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/linux/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/linux/prefix.h index c8ab5f081..032e2e2be 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/linux/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/linux/prefix.h @@ -33,3 +33,38 @@ #include "../../utils/utils.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_LINUX_PREFIX_H +#define TB_PLATFORM_LINUX_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/ltimer.h b/core/pkg/tbox.pkg/inc/tbox/platform/ltimer.h index 32941e2ba..3e4ee406c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/ltimer.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/ltimer.h @@ -240,3 +240,245 @@ tb_void_t tb_ltimer_task_kill(tb_ltimer_ref_t timer, tb_ltimer_tas __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ltimer.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_LTIMER_H +#define TB_PLATFORM_LTIMER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "timer.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the timer tick enum +typedef enum __tb_ltimer_tick_e +{ + TB_LTIMER_TICK_100MS = 100 +, TB_LTIMER_TICK_S = 1000 +, TB_LTIMER_TICK_M = 60000 +, TB_LTIMER_TICK_H = 3600000 + +}tb_ltimer_tick_e; + +/// the ltimer task func type +typedef tb_timer_task_func_t tb_ltimer_task_func_t; + +/// the ltimer ref type +typedef __tb_typeref__(ltimer); + +/// the ltimer task ref type +typedef __tb_typeref__(ltimer_task); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init timer + * + * lower tick and limit range, but faster + * + * @param grow the timer grow + * @param tick the timer tick + * @param ctime using ctime? + * + * @return the timer + */ +tb_ltimer_ref_t tb_ltimer_init(tb_size_t grow, tb_size_t tick, tb_bool_t ctime); + +/*! exit timer + * + * @param timer the timer + */ +tb_void_t tb_ltimer_exit(tb_ltimer_ref_t timer); + +/*! kill timer for tb_ltimer_loop() + * + * @param timer the timer + */ +tb_void_t tb_ltimer_kill(tb_ltimer_ref_t timer); + +/*! clear timer + * + * @param timer the timer + */ +tb_void_t tb_ltimer_clear(tb_ltimer_ref_t timer); + +/*! the timer limit + * + * @param timer the timer + * + * @return the timer limit range: [now, now + limit) + */ +tb_size_t tb_ltimer_limit(tb_ltimer_ref_t timer); + +/*! the timer delay for spak + * + * @param timer the timer + * + * @return the timer delay, (tb_size_t)-1: error or no task + */ +tb_size_t tb_ltimer_delay(tb_ltimer_ref_t timer); + +/*! spak timer for the external loop at the single thread + * + * @code + tb_void_t tb_ltimer_loop() + { + while (1) + { + // wait + wait(tb_ltimer_delay(timer)) + + // spak timer + tb_ltimer_spak(timer); + } + } + * @endcode + * + * @param timer the timer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_ltimer_spak(tb_ltimer_ref_t timer); + +/*! loop timer for the external thread + * + * @code + tb_void_t tb_ltimer_thread(tb_cpointer_t priv) + { + tb_ltimer_loop(timer); + } + * @endcode + * + * @param timer the timer + * + * @return tb_true or tb_false + */ +tb_void_t tb_ltimer_loop(tb_ltimer_ref_t timer); + +/*! post timer task after delay and will be auto-remove it after be expired + * + * @param timer the timer + * @param delay the delay time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + */ +tb_void_t tb_ltimer_task_post(tb_ltimer_ref_t timer, tb_size_t delay, tb_bool_t repeat, tb_ltimer_task_func_t func, tb_cpointer_t priv); + +/*! post timer task at the absolute time and will be auto-remove it after be expired + * + * @param timer the timer + * @param when the absolute time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + */ +tb_void_t tb_ltimer_task_post_at(tb_ltimer_ref_t timer, tb_hize_t when, tb_size_t period, tb_bool_t repeat, tb_ltimer_task_func_t func, tb_cpointer_t priv); + +/*! run timer task after the relative time and will be auto-remove it after be expired + * + * @param timer the timer + * @param after the after time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + */ +tb_void_t tb_ltimer_task_post_after(tb_ltimer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_ltimer_task_func_t func, tb_cpointer_t priv); + +/*! init and post timer task after delay and need remove it manually + * + * @param timer the timer + * @param delay the delay time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + * @return the timer task + */ +tb_ltimer_task_ref_t tb_ltimer_task_init(tb_ltimer_ref_t timer, tb_size_t delay, tb_bool_t repeat, tb_ltimer_task_func_t func, tb_cpointer_t priv); + +/*! init and post timer task at the absolute time and need remove it manually + * + * @param timer the timer + * @param when the absolute time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + * @return the timer task + */ +tb_ltimer_task_ref_t tb_ltimer_task_init_at(tb_ltimer_ref_t timer, tb_hize_t when, tb_size_t period, tb_bool_t repeat, tb_ltimer_task_func_t func, tb_cpointer_t priv); + +/*! init and post timer task after the relative time and need remove it manually + * + * @param timer the timer + * @param after the after time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + * @return the timer task + */ +tb_ltimer_task_ref_t tb_ltimer_task_init_after(tb_ltimer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_ltimer_task_func_t func, tb_cpointer_t priv); + +/*! exit timer task, the task will be not called if have been not called + * + * @param timer the timer + * @param task the timer task + */ +tb_void_t tb_ltimer_task_exit(tb_ltimer_ref_t timer, tb_ltimer_task_ref_t task); + +/*! kill timer task, the task will be called immediately if have been not called + * + * @param timer the timer + * @param task the timer task + */ +tb_void_t tb_ltimer_task_kill(tb_ltimer_ref_t timer, tb_ltimer_task_ref_t task); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/mach/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/mach/barrier.h index 2d67fb1fd..eded47139 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/mach/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/mach/barrier.h @@ -38,3 +38,43 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * + */ +#ifndef TB_PLATFORM_MACH_BARRIER_H +#define TB_PLATFORM_MACH_BARRIER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include <libkern/OSAtomic.h> + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_barrier() OSMemoryBarrier() + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/mach/ios/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/mach/ios/prefix.h index 159ff9f7e..40975db49 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/mach/ios/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/mach/ios/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_MACH_IOS_PREFIX_H +#define TB_PLATFORM_MACH_IOS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/mach/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/mach/prefix.h index 27c5d5ac2..80d16cb82 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/mach/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/mach/prefix.h @@ -33,3 +33,38 @@ #include "../../utils/utils.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_MACH_PREFIX_H +#define TB_PLATFORM_MACH_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../platform.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/memory.h b/core/pkg/tbox.pkg/inc/tbox/platform/memory.h index dae8196fc..1c01e511b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/memory.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/memory.h @@ -110,3 +110,115 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file memory.h + * @defgroup platform + * + */ +#ifndef TB_PLATFORM_MEMORY_H +#define TB_PLATFORM_MEMORY_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init native memory + * + * @note + * need support to be called repeatly + * + * @return tb_true or tb_false + */ +tb_bool_t tb_native_memory_init(tb_noarg_t); + +/// exit native memory +tb_void_t tb_native_memory_exit(tb_noarg_t); + +/*! malloc the native memory + * + * @param size the size + * + * @return the data address + */ +tb_pointer_t tb_native_memory_malloc(tb_size_t size); + +/*! malloc the native memory and fill zero + * + * @param size the size + * + * @return the data address + */ +tb_pointer_t tb_native_memory_malloc0(tb_size_t size); + +/*! malloc the native memory with the item count + * + * @param item the item count + * @param size the item size + * + * @return the data address + */ +tb_pointer_t tb_native_memory_nalloc(tb_size_t item, tb_size_t size); + +/*! malloc the native memory with the item count and fill zero + * + * @param item the item count + * @param size the item size + * + * @return the data address + */ +tb_pointer_t tb_native_memory_nalloc0(tb_size_t item, tb_size_t size); + +/*! realloc the native memory + * + * @param data the data address + * @param size the size + * + * @return the new data address + */ +tb_pointer_t tb_native_memory_ralloc(tb_pointer_t data, tb_size_t size); + +/*! free the native memory + * + * @param data the data address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_native_memory_free(tb_pointer_t data); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/mutex.h b/core/pkg/tbox.pkg/inc/tbox/platform/mutex.h index d7e6a3dc7..c298ab9c3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/mutex.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/mutex.h @@ -82,3 +82,87 @@ tb_bool_t tb_mutex_leave(tb_mutex_ref_t mutex); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file mutex.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_MUTEX_H +#define TB_PLATFORM_MUTEX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init mutex + * + * @return the mutex + */ +tb_mutex_ref_t tb_mutex_init(tb_noarg_t); + +/* exit mutex + * + * @param mutex the mutex + */ +tb_void_t tb_mutex_exit(tb_mutex_ref_t mutex); + +/* enter mutex + * + * @param mutex the mutex + * + * @return tb_true or tb_false + */ +tb_bool_t tb_mutex_enter(tb_mutex_ref_t mutex); + +/* try to enter mutex + * + * @param mutex the mutex + * + * @return tb_true or tb_false + */ +tb_bool_t tb_mutex_enter_try(tb_mutex_ref_t mutex); + +/* leave mutex + * + * @param mutex the mutex + * + * @return tb_true or tb_false + */ +tb_bool_t tb_mutex_leave(tb_mutex_ref_t mutex); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/page.h b/core/pkg/tbox.pkg/inc/tbox/platform/page.h index dea46471e..c7a33f498 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/page.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/page.h @@ -67,3 +67,72 @@ tb_size_t tb_page_size(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file page.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_PAGE_H +#define TB_PLATFORM_PAGE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init page + * + * @note + * need support to be called repeatly + * + * @return tb_true or tb_false + */ +tb_bool_t tb_page_init(tb_noarg_t); + +/*! exit page + * + * @return the page size + */ +tb_void_t tb_page_exit(tb_noarg_t); + +/*! get page size + * + * @return the page size + */ +tb_size_t tb_page_size(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/path.h b/core/pkg/tbox.pkg/inc/tbox/platform/path.h index fdd2f1ab0..d20a9f1a5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/path.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/path.h @@ -127,3 +127,132 @@ tb_char_t const* tb_path_directory(tb_char_t const* path, tb_char_t* data __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @path path.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_PATH_H +#define TB_PLATFORM_PATH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the path maximum +#define TB_PATH_MAXN (4096) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! translate the path to the native path + * + * - transform the path separator + * - remove the repeat path separator + * - expand the user directory with the prefix: ~ + * + * @param path the path + * @param size the path size, optional + * @param maxn the path maxn + * + * @return tb_true or tb_false + */ +tb_size_t tb_path_translate(tb_char_t* path, tb_size_t size, tb_size_t maxn); + +/*! the path is absolute? + * + * @param path the path + * + * @return tb_true or tb_false + */ +tb_bool_t tb_path_is_absolute(tb_char_t const* path); + +/*! get the absolute path which relative to the current directory + * + * @param path the path + * @param data the path data + * @param maxn the path maxn + * + * @return the absolute path + */ +tb_char_t const* tb_path_absolute(tb_char_t const* path, tb_char_t* data, tb_size_t maxn); + +/*! get the absolute path which relative to the given root directory + * + * @param root the root path + * @param path the path + * @param data the path data + * @param maxn the path maxn + * + * @return the absolute path + */ +tb_char_t const* tb_path_absolute_to(tb_char_t const* root, tb_char_t const* path, tb_char_t* data, tb_size_t maxn); + +/*! get the path which relative to the current directory + * + * @param path the path + * @param data the path data + * @param maxn the path maxn + * + * @return the relative path + */ +tb_char_t const* tb_path_relative(tb_char_t const* path, tb_char_t* data, tb_size_t maxn); + +/*! get the path which relative to the given root directory + * + * @param root the root path + * @param path the path + * @param data the path data + * @param maxn the path maxn + * + * @return the relative path + */ +tb_char_t const* tb_path_relative_to(tb_char_t const* root, tb_char_t const* path, tb_char_t* data, tb_size_t maxn); + +/*! get the directory of path + * + * @param path the path + * @param data the path data + * @param maxn the path maxn + * + * @return the directory of path + */ +tb_char_t const* tb_path_directory(tb_char_t const* path, tb_char_t* data, tb_size_t maxn); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/platform.h b/core/pkg/tbox.pkg/inc/tbox/platform/platform.h index 190b31e9c..8eee756f1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/platform.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/platform.h @@ -68,3 +68,73 @@ #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file platform.h + * @defgroup platform + * + */ +#ifndef TB_PLATFORM_H +#define TB_PLATFORM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "page.h" +#include "path.h" +#include "file.h" +#include "time.h" +#include "mutex.h" +#include "event.h" +#include "timer.h" +#include "print.h" +#include "ltimer.h" +#include "socket.h" +#include "thread.h" +#include "atomic.h" +#include "memory.h" +#include "poller.h" +#include "context.h" +#include "ifaddrs.h" +#include "barrier.h" +#include "dynamic.h" +#include "process.h" +#include "syserror.h" +#include "addrinfo.h" +#include "spinlock.h" +#include "atomic64.h" +#include "hostname.h" +#include "processor.h" +#include "semaphore.h" +#include "backtrace.h" +#include "directory.h" +#include "exception.h" +#include "cache_time.h" +#include "environment.h" +#include "thread_pool.h" +#include "thread_local.h" +#ifdef TB_CONFIG_API_HAVE_DEPRECATED +# include "deprecated/deprecated.h" +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/poller.h b/core/pkg/tbox.pkg/inc/tbox/platform/poller.h index 3212f279c..f9bb4c69f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/poller.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/poller.h @@ -175,3 +175,180 @@ tb_long_t tb_poller_wait(tb_poller_ref_t poller, tb_poller_event_func_ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file poller.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_POLLER_H +#define TB_PLATFORM_POLLER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "socket.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the poller event enum, only for sock +typedef enum __tb_poller_event_e +{ + // the waited events + TB_POLLER_EVENT_NONE = TB_SOCKET_EVENT_NONE +, TB_POLLER_EVENT_CONN = TB_SOCKET_EVENT_CONN +, TB_POLLER_EVENT_ACPT = TB_SOCKET_EVENT_ACPT +, TB_POLLER_EVENT_RECV = TB_SOCKET_EVENT_RECV +, TB_POLLER_EVENT_SEND = TB_SOCKET_EVENT_SEND +, TB_POLLER_EVENT_EALL = TB_SOCKET_EVENT_EALL + + // the event flags after waiting +, TB_POLLER_EVENT_CLEAR = 0x0010 //!< edge trigger. after the event is retrieved by the user, its state is reset +, TB_POLLER_EVENT_ONESHOT = 0x0020 //!< causes the event to return only the first occurrence of the filter being triggered + + /*! the event flag will be marked if the connection be closed in the edge trigger (TB_POLLER_EVENT_CLEAR) + * + * be similar to epoll.EPOLLRDHUP and kqueue.EV_EOF + */ +, TB_POLLER_EVENT_EOF = 0x0100 + +}tb_poller_event_e; + +/// the poller ref type +typedef __tb_typeref__(poller); + +/*! the poller event func type + * + * @param poller the poller + * @param sock the socket + * @param events the poller events + * @param priv the user private data for this socket + */ +typedef tb_void_t (*tb_poller_event_func_t)(tb_poller_ref_t poller, tb_socket_ref_t sock, tb_size_t events, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init poller + * + * @param priv the user private data + * + * @param poller the poller + */ +tb_poller_ref_t tb_poller_init(tb_cpointer_t priv); + +/*! exit poller + * + * @param poller the poller + */ +tb_void_t tb_poller_exit(tb_poller_ref_t poller); + +/*! clear all sockets + * + * @param poller the poller + */ +tb_void_t tb_poller_clear(tb_poller_ref_t poller); + +/*! get the user private data + * + * @param poller the poller + * + * @return the user private data + */ +tb_cpointer_t tb_poller_priv(tb_poller_ref_t poller); + +/*! kill all waited events, tb_poller_wait() will return -1 + * + * @param poller the poller + */ +tb_void_t tb_poller_kill(tb_poller_ref_t poller); + +/*! spak the poller, break the tb_poller_wait() and return all events + * + * @param poller the poller + */ +tb_void_t tb_poller_spak(tb_poller_ref_t poller); + +/*! the events(clear, oneshot, ..) is supported for the poller? + * + * @param poller the poller + * @param events the poller events + * + * @return tb_true or tb_false + */ +tb_bool_t tb_poller_support(tb_poller_ref_t poller, tb_size_t events); + +/*! insert socket to poller + * + * @param poller the poller + * @param sock the socket + * @param events the poller events + * @param priv the private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_poller_insert(tb_poller_ref_t poller, tb_socket_ref_t sock, tb_size_t events, tb_cpointer_t priv); + +/*! remove socket from poller + * + * @param poller the poller + * @param aioo the aioo + * + * @return tb_true or tb_false + */ +tb_bool_t tb_poller_remove(tb_poller_ref_t poller, tb_socket_ref_t sock); + +/*! modify events for the given socket + * + * @param poller the poller + * @param sock the socket + * @param events the poller events + * @param priv the private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_poller_modify(tb_poller_ref_t poller, tb_socket_ref_t sock, tb_size_t events, tb_cpointer_t priv); + +/*! wait all sockets + * + * @param poller the poller + * @param func the events function + * @param timeout the timeout, infinity: -1 + * + * @return > 0: the events number, 0: timeout, -1: failed + */ +tb_long_t tb_poller_wait(tb_poller_ref_t poller, tb_poller_event_func_t func, tb_long_t timeout); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/posix/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/posix/prefix.h index 04de119b7..66acd688e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/posix/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/posix/prefix.h @@ -33,3 +33,38 @@ #include "../../utils/utils.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_POSIX_PREFIX_H +#define TB_PLATFORM_POSIX_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../platform.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/posix/sockaddr.h b/core/pkg/tbox.pkg/inc/tbox/platform/posix/sockaddr.h index 44ca87714..a241ca402 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/posix/sockaddr.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/posix/sockaddr.h @@ -204,3 +204,209 @@ static __tb_inline__ tb_size_t tb_sockaddr_load(struct sockaddr_storage* saddr, __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file sockaddr.h + */ +#ifndef TB_PLATFORM_POSIX_SOCKADDR_H +#define TB_PLATFORM_POSIX_SOCKADDR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_OS_WINDOWS +# include "ws2tcpip.h" +#else +# include <netinet/in.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* save the socket address to the ip address + * + * @param ipaddr the ip address + * @param saddr the socket address + * + * @return the socket address size + */ +static __tb_inline__ tb_size_t tb_sockaddr_save(tb_ipaddr_ref_t ipaddr, struct sockaddr_storage const* saddr) +{ + // check + tb_assert_and_check_return_val(ipaddr && saddr, 0); + + // clear address + tb_ipaddr_clear(ipaddr); + + // done + tb_size_t size = 0; + switch (saddr->ss_family) + { + case AF_INET: + { + // the ipv4 ipaddr + struct sockaddr_in* addr4 = (struct sockaddr_in*)saddr; + + // save family + tb_ipaddr_family_set(ipaddr, TB_IPADDR_FAMILY_IPV4); + + // make ipv4 + tb_ipv4_t ipv4; + ipv4.u32 = (tb_uint32_t)addr4->sin_addr.s_addr; + + // save ipv4 + tb_ipaddr_ipv4_set(ipaddr, &ipv4); + + // save port + tb_ipaddr_port_set(ipaddr, tb_bits_be_to_ne_u16(addr4->sin_port)); + + // save size + size = sizeof(struct sockaddr_in); + } + break; + case AF_INET6: + { + // the ipv6 ipaddr + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)saddr; + + // check + tb_assert_static(sizeof(ipaddr->u.ipv6.addr.u8) == sizeof(addr6->sin6_addr.s6_addr)); + tb_assert_static(tb_arrayn(ipaddr->u.ipv6.addr.u8) == tb_arrayn(addr6->sin6_addr.s6_addr)); + + // save family + tb_ipaddr_family_set(ipaddr, TB_IPADDR_FAMILY_IPV6); + + // save port + tb_ipaddr_port_set(ipaddr, tb_bits_be_to_ne_u16(addr6->sin6_port)); + + // make ipv6 + tb_ipv6_t ipv6; + tb_memcpy(ipv6.addr.u8, addr6->sin6_addr.s6_addr, sizeof(ipv6.addr.u8)); + + // save scope id + ipv6.scope_id = 0; + if (IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&addr6->sin6_addr)) + ipv6.scope_id = addr6->sin6_scope_id; + + // save ipv6 + tb_ipaddr_ipv6_set(ipaddr, &ipv6); + + // save size + size = sizeof(struct sockaddr_in6); + } + break; + default: + tb_assert(0); + break; + } + + // ok? + return size; +} + +/* load the ip address to socket address + * + * @param saddr the socket address + * @param ipaddr the ip address + * + * @return the socket address size + */ +static __tb_inline__ tb_size_t tb_sockaddr_load(struct sockaddr_storage* saddr, tb_ipaddr_ref_t ipaddr) +{ + // check + tb_assert_and_check_return_val(saddr && ipaddr, 0); + + // clear address + tb_memset(saddr, 0, sizeof(struct sockaddr_storage)); + + // done + tb_size_t size = 0; + switch (tb_ipaddr_family(ipaddr)) + { + case TB_IPADDR_FAMILY_IPV4: + { + // the ipv4 ipaddr + struct sockaddr_in* addr4 = (struct sockaddr_in*)saddr; + + // save family + addr4->sin_family = AF_INET; + + // save ipv4 + addr4->sin_addr.s_addr = tb_ipaddr_ip_is_any(ipaddr)? INADDR_ANY : ipaddr->u.ipv4.u32; + + // save port + addr4->sin_port = tb_bits_ne_to_be_u16(tb_ipaddr_port(ipaddr)); + + // save size + size = sizeof(struct sockaddr_in); + } + break; + case TB_IPADDR_FAMILY_IPV6: + { + // the ipv6 ipaddr + struct sockaddr_in6* addr6 = (struct sockaddr_in6*)saddr; + + // check + tb_assert_static(sizeof(ipaddr->u.ipv6.addr.u8) == sizeof(addr6->sin6_addr.s6_addr)); + tb_assert_static(tb_arrayn(ipaddr->u.ipv6.addr.u8) == tb_arrayn(addr6->sin6_addr.s6_addr)); + + // save family + addr6->sin6_family = AF_INET6; + + // save ipv6 + if (tb_ipaddr_ip_is_any(ipaddr)) addr6->sin6_addr = in6addr_any; + else tb_memcpy(addr6->sin6_addr.s6_addr, ipaddr->u.ipv6.addr.u8, sizeof(addr6->sin6_addr.s6_addr)); + + // save port + addr6->sin6_port = tb_bits_ne_to_be_u16(tb_ipaddr_port(ipaddr)); + + // save scope id + if (tb_ipv6_is_linklocal(&ipaddr->u.ipv6) || tb_ipv6_is_mc_linklocal(&ipaddr->u.ipv6)) + addr6->sin6_scope_id = ipaddr->u.ipv6.scope_id; + + // save size + size = sizeof(struct sockaddr_in6); + } + break; + default: + tb_assert(0); + break; + } + + // ok? + return size; +} + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/prefix.h index 79e536679..4cb704cf7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/prefix.h @@ -86,3 +86,91 @@ typedef struct __tb_iovec_t #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_PREFIX_H +#define TB_PLATFORM_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// fd to file +#define tb_fd2file(fd) ((fd) >= 0? (tb_file_ref_t)((tb_long_t)(fd) + 1) : tb_null) + +// file to fd +#define tb_file2fd(file) (tb_int_t)((file)? (((tb_long_t)(file)) - 1) : -1) + +// fd to sock +#define tb_fd2sock(fd) ((fd) >= 0? (tb_socket_ref_t)((tb_long_t)(fd) + 1) : tb_null) + +// sock to fd +#define tb_sock2fd(sock) (tb_int_t)((sock)? (((tb_long_t)(sock)) - 1) : -1) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the iovec size type + * + * @note + * we cannot use tb_size_t because sizeof(tb_size_t) != sizeof(u_long) for windows 64bits + */ +#ifdef TB_CONFIG_OS_WINDOWS +typedef unsigned long tb_iovec_size_t; +#else +typedef tb_size_t tb_iovec_size_t; +#endif + +#ifdef TB_CONFIG_OS_WINDOWS +/// the iovec type for WSASend, WSARecv using WSABUF +typedef struct __tb_iovec_t +{ + /// the size + tb_iovec_size_t size; + + /// the data + tb_byte_t* data; + +}tb_iovec_t; +#else +/// the iovec type for readv, preadv, writv, pwritv, recvv, sendv +typedef struct __tb_iovec_t +{ + /// the data + tb_byte_t* data; + + /// the size + tb_iovec_size_t size; + +}tb_iovec_t; +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/print.h b/core/pkg/tbox.pkg/inc/tbox/platform/print.h index 0862fefc1..7ecc6e56a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/print.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/print.h @@ -62,3 +62,67 @@ tb_void_t tb_print_sync(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file print.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_PRINT_H +#define TB_PLATFORM_PRINT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! print string and without newline + * + * @param string the print string + */ +tb_void_t tb_print(tb_char_t const* string); + +/*! print string and newline + * + * @param string the print string + */ +tb_void_t tb_printl(tb_char_t const* string); + +/*! sync print + */ +tb_void_t tb_print_sync(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/process.h b/core/pkg/tbox.pkg/inc/tbox/platform/process.h index d49596331..757375530 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/process.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/process.h @@ -333,3 +333,338 @@ tb_long_t tb_process_waitlist(tb_process_ref_t const* processes, t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file process.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_PROCESS_H +#define TB_PLATFORM_PROCESS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the process flag enum +typedef enum __tb_process_flag_e +{ + TB_PROCESS_FLAG_NONE = 0 +, TB_PROCESS_FLAG_SUSPEND = 1 //!< suspend process + +}tb_process_flag_e; + +/// the process attribute type +typedef struct __tb_process_attr_t +{ + /// the flags + tb_size_t flags; + + /// the stdout filename + tb_char_t const* outfile; + + /*! the stdout filemode + * + * default: TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC + * + * support: + * + * - TB_FILE_MODE_WO + * - TB_FILE_MODE_RW + * - TB_FILE_MODE_CREAT + * - TB_FILE_MODE_APPEND + * - TB_FILE_MODE_TRUNC + */ + tb_size_t outmode; + + /// the stderr filename + tb_char_t const* errfile; + + /// the stderr filemode + tb_size_t errmode; + + /*! the environment + * + * @code + + tb_char_t const* envp[] = + { + "path=/usr/bin" + , tb_null + }; + + attr.envp = envp; + + * @endcode + * + * the envp argument is an array of pointers to null-terminated strings + * and must be terminated by a null pointer + * + * if the value of envp is null, then the child process inherits + * the environment of the parent process. + */ + tb_char_t const** envp; + +}tb_process_attr_t, *tb_process_attr_ref_t; + +/// the process ref type +typedef __tb_typeref__(process); + +/// the process wait info type +typedef struct __tb_process_waitinfo_t +{ + // the index of the processes + tb_size_t index; + + // the process + tb_process_ref_t process; + + // the status + tb_long_t status; + +}tb_process_waitinfo_t, *tb_process_waitinfo_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! run a given process + * + * @code + + // init argv + tb_char_t const* argv[] = + { + "echo" + , "hello" + , tb_null + }; + + // init envp + tb_char_t const* envp[] = + { + "path=/usr/bin" + , tb_null + }; + + // init attr + tb_process_attr_t attr = {0}; + attr.envp = envp; + + // run bash + if (tb_process_run("echo", argv, &attr) == 0) + { + // trace + tb_trace_i("ok"); + } + + // run bash + if (tb_process_run("/bin/echo", tb_null, tb_null) == 0) + { + // trace + tb_trace_i("ok"); + } + + * @endcode + * + * @param pathname the process path or name + * @param argv the list of arguments must be terminated by a null pointer + * and must be terminated by a null pointer + * and argv[0] is the self path name + * @param attr the process attributes + * + * @return the status value, failed: -1, ok: 0, other: error code + */ +tb_long_t tb_process_run(tb_char_t const* pathname, tb_char_t const* argv[], tb_process_attr_ref_t attr); + +/*! run a given process from the command line + * + * @param cmd the command line + * @param attr the process attributes + * + * @return the status value, failed: -1, ok: 0, other: error code + */ +tb_long_t tb_process_run_cmd(tb_char_t const* cmd, tb_process_attr_ref_t attr); + +/*! init a given process + * + * @code + + // init process + tb_process_ref_t process = tb_process_init("/bin/echo", tb_null, tb_null); + if (process) + { + // wait process + tb_long_t status = 0; + if (tb_process_wait(process, &status, 10) > 0) + { + // trace + tb_trace_i("process exited: %ld", status); + } + // kill process + else + { + // kill it + tb_process_kill(process); + + // wait it again + tb_process_wait(process, &status, -1); + } + + // exit process + tb_process_exit(process); + } + + * @endcode + * + * @param pathname the process path or name + * + * @param argv the list of arguments must be terminated by a null pointer + * and must be terminated by a null pointer + * and argv[0] is the self path name + * + * @param attr the process attributes + * + * @return the process + */ +tb_process_ref_t tb_process_init(tb_char_t const* pathname, tb_char_t const* argv[], tb_process_attr_ref_t attr); + +/*! init a given process from the command line + * + * @param cmd the command line + * @param attr the process attributes + * + * @return the process + */ +tb_process_ref_t tb_process_init_cmd(tb_char_t const* cmd, tb_process_attr_ref_t attr); + +/*! exit the process + * + * @param process the process + */ +tb_void_t tb_process_exit(tb_process_ref_t process); + +/*! kill the process + * + * @param process the process + */ +tb_void_t tb_process_kill(tb_process_ref_t process); + +/*! resume the process + * + * @param process the process + */ +tb_void_t tb_process_resume(tb_process_ref_t process); + +/*! suspend the process + * + * @param process the process + */ +tb_void_t tb_process_suspend(tb_process_ref_t process); + +/*! wait the process + * + * @param process the process + * @param pstatus the process exited status pointer, maybe null + * @param timeout the timeout (ms), infinity: -1 + * + * @return wait failed: -1, timeout: 0, ok: 1 + */ +tb_long_t tb_process_wait(tb_process_ref_t process, tb_long_t* pstatus, tb_long_t timeout); + +/*! wait the process list + * + * @code + + // init processes + tb_size_t count1 = 0; + tb_process_ref_t processes1[5] = {0}; + tb_process_ref_t processes2[5] = {0}; + for (; count1 < 4; count1++) + { + processes1[count1] = tb_process_init(argv[1], (tb_char_t const**)(argv + 1), tb_null); + tb_assert_and_check_break(processes1[count1]); + } + + // ok? + while (count1) + { + // trace + tb_trace_i("waiting: %ld", count1); + + // wait processes + tb_long_t infosize = -1; + tb_process_waitinfo_t infolist[4]; + if ((infosize = tb_process_waitlist(processes1, infolist, tb_arrayn(infolist), -1)) > 0) + { + tb_size_t i = 0; + for (i = 0; i < infosize; i++) + { + // trace + tb_trace_i("process(%ld:%p) exited: %ld", infolist[i].index, infolist[i].process, infolist[i].status); + + // exit process + if (infolist[i].process) tb_process_exit(infolist[i].process); + + // remove this process + processes1[infolist[i].index] = tb_null; + } + + // update processes + tb_size_t count2 = 0; + for (i = 0; i < count1; i++) + { + if (processes1[i]) processes2[count2++] = processes1[i]; + } + tb_memcpy(processes1, processes2, count2 * sizeof(tb_process_ref_t)); + processes1[count2] = tb_null; + count1 = count2; + } + } + + * @endcode + * + * @param processes the null-terminated process list + * @param infolist the info list + * @param infomaxn the info maxn + * @param timeout the timeout (ms), infinity: -1 + * + * @return > 0: the info list size, 0: timeout, -1: failed + */ +tb_long_t tb_process_waitlist(tb_process_ref_t const* processes, tb_process_waitinfo_ref_t infolist, tb_size_t infomaxn, tb_long_t timeout); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/processor.h b/core/pkg/tbox.pkg/inc/tbox/platform/processor.h index a08750488..ce592f4bf 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/processor.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/processor.h @@ -52,3 +52,57 @@ tb_size_t tb_processor_count(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file processor.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_PROCESSOR_H +#define TB_PLATFORM_PROCESSOR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the processor count + * + * @return the processor count + */ +tb_size_t tb_processor_count(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/sched.h b/core/pkg/tbox.pkg/inc/tbox/platform/sched.h index 29445b038..7200e8a08 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/sched.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/sched.h @@ -53,3 +53,58 @@ tb_bool_t tb_sched_yield(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file sched.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_SCHED_H +#define TB_PLATFORM_SCHED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "time.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! yield the processor + * + * @return tb_true or tb_false + */ +tb_bool_t tb_sched_yield(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/semaphore.h b/core/pkg/tbox.pkg/inc/tbox/platform/semaphore.h index c63e12439..02a0ac8c0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/semaphore.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/semaphore.h @@ -87,3 +87,92 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file semaphore.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_SEMAPHORE_H +#define TB_PLATFORM_SEMAPHORE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init semaphore + * + * @param value the initial semaphore value + * + * @return the semaphore + */ +tb_semaphore_ref_t tb_semaphore_init(tb_size_t value); + +/*! exit semaphore + * + * @return the semaphore + */ +tb_void_t tb_semaphore_exit(tb_semaphore_ref_t semaphore); + +/*! post semaphore + * + * @param semaphore the semaphore + * @param post the post semaphore value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_semaphore_post(tb_semaphore_ref_t semaphore, tb_size_t post); + +/*! the semaphore value + * + * @param semaphore the semaphore + * + * @return >= 0: the semaphore value, -1: failed + */ +tb_long_t tb_semaphore_value(tb_semaphore_ref_t semaphore); + +/*! wait semaphore + * + * @param semaphore the semaphore + * @param timeout the timeout + * + * @return ok: 1, timeout: 0, fail: -1 + */ +tb_long_t tb_semaphore_wait(tb_semaphore_ref_t semaphore, tb_long_t timeout); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/socket.h b/core/pkg/tbox.pkg/inc/tbox/platform/socket.h index d2c954c4f..2c2492058 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/socket.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/socket.h @@ -290,3 +290,295 @@ tb_long_t tb_socket_wait(tb_socket_ref_t sock, tb_size_t events, tb_lo __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file socket.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_SOCKET_H +#define TB_PLATFORM_SOCKET_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../network/ipaddr.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the socket type enum +typedef enum __tb_socket_type_e +{ + TB_SOCKET_TYPE_NUL = 0 +, TB_SOCKET_TYPE_TCP = 1 +, TB_SOCKET_TYPE_UDP = 2 + +}tb_socket_type_e; + +/// the socket kill enum +typedef enum __tb_socket_kill_e +{ + TB_SOCKET_KILL_RO = 0 +, TB_SOCKET_KILL_WO = 1 +, TB_SOCKET_KILL_RW = 2 + +}tb_socket_kill_e; + +/// the socket ctrl enum +typedef enum __tb_socket_ctrl_e +{ + TB_SOCKET_CTRL_SET_BLOCK = 0 +, TB_SOCKET_CTRL_GET_BLOCK = 1 +, TB_SOCKET_CTRL_SET_RECV_BUFF_SIZE = 2 +, TB_SOCKET_CTRL_GET_RECV_BUFF_SIZE = 3 +, TB_SOCKET_CTRL_SET_SEND_BUFF_SIZE = 4 +, TB_SOCKET_CTRL_GET_SEND_BUFF_SIZE = 5 +, TB_SOCKET_CTRL_SET_TCP_NODELAY = 6 +, TB_SOCKET_CTRL_GET_TCP_NODELAY = 7 + +}tb_socket_ctrl_e; + +/// the socket event enum, only for sock +typedef enum __tb_socket_event_e +{ + TB_SOCKET_EVENT_NONE = 0x0000 +, TB_SOCKET_EVENT_RECV = 0x0001 +, TB_SOCKET_EVENT_SEND = 0x0002 +, TB_SOCKET_EVENT_CONN = TB_SOCKET_EVENT_SEND +, TB_SOCKET_EVENT_ACPT = TB_SOCKET_EVENT_RECV +, TB_SOCKET_EVENT_EALL = TB_SOCKET_EVENT_RECV | TB_SOCKET_EVENT_SEND + +}tb_socket_event_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init socket + * + * @param type the socket type + * @param family the address family, default: ipv4 + * + * @return the socket + */ +tb_socket_ref_t tb_socket_init(tb_size_t type, tb_size_t family); + +/*! exit socket + * + * @param sock the socket + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_exit(tb_socket_ref_t sock); + +/*! init socket pair + * + * @param type the socket type + * @param pair the socket pair + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_pair(tb_size_t type, tb_socket_ref_t pair[2]); + +/*! ctrl the socket + * + * @param sock the socket + * @param ctrl the ctrl code + */ +tb_bool_t tb_socket_ctrl(tb_socket_ref_t sock, tb_size_t ctrl, ...); + +/*! connect socket + * + * @param sock the socket + * @param addr the address + * + * @return ok: 1, continue: 0; failed: -1 + */ +tb_long_t tb_socket_connect(tb_socket_ref_t sock, tb_ipaddr_ref_t addr); + +/*! bind socket + * + * you can call tb_socket_local for the bound address + * + * @param sock the socket + * @param addr the address + * - bind any port if port == 0 + * - bind any ip address if ip is empty + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_bind(tb_socket_ref_t sock, tb_ipaddr_ref_t addr); + +/*! listen socket + * + * @param sock the socket + * @param backlog the maximum length for the queue of pending connections + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_listen(tb_socket_ref_t sock, tb_size_t backlog); + +/*! accept socket + * + * @param sock the socket + * @param addr the client address + * + * @return the client socket + */ +tb_socket_ref_t tb_socket_accept(tb_socket_ref_t sock, tb_ipaddr_ref_t addr); + +/*! get local address + * + * @param sock the socket + * @param addr the local address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_local(tb_socket_ref_t sock, tb_ipaddr_ref_t addr); + +/*! kill socket + * + * @param sock the socket + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_kill(tb_socket_ref_t sock, tb_size_t mode); + +/*! recv the socket data for tcp + * + * @param sock the socket + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_recv(tb_socket_ref_t sock, tb_byte_t* data, tb_size_t size); + +/*! send the socket data for tcp + * + * @param sock the socket + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_send(tb_socket_ref_t sock, tb_byte_t const* data, tb_size_t size); + +/*! recvv the socket data for tcp + * + * @param sock the socket + * @param list the iovec list + * @param size the iovec size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_recvv(tb_socket_ref_t sock, tb_iovec_t const* list, tb_size_t size); + +/*! sendv the socket data for tcp + * + * @param sock the socket + * @param list the iovec list + * @param size the iovec size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_sendv(tb_socket_ref_t sock, tb_iovec_t const* list, tb_size_t size); + +/*! sendf the socket data + * + * @param sock the socket + * @param file the file + * @param offset the offset + * @param size the size + * + * @return the real size or -1 + */ +tb_hong_t tb_socket_sendf(tb_socket_ref_t sock, tb_file_ref_t file, tb_hize_t offset, tb_hize_t size); + +/*! send the socket data for udp + * + * @param sock the socket + * @param addr the address + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_usend(tb_socket_ref_t sock, tb_ipaddr_ref_t addr, tb_byte_t const* data, tb_size_t size); + +/*! recv the socket data for udp + * + * @param sock the socket + * @param addr the peer address(output) + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_urecv(tb_socket_ref_t sock, tb_ipaddr_ref_t addr, tb_byte_t* data, tb_size_t size); + +/*! urecvv the socket data for udp + * + * @param sock the socket + * @param addr the peer address(output) + * @param list the iovec list + * @param size the iovec size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_urecvv(tb_socket_ref_t sock, tb_ipaddr_ref_t addr, tb_iovec_t const* list, tb_size_t size); + +/*! usendv the socket data for udp + * + * @param sock the socket + * @param addr the addr + * @param list the iovec list + * @param size the iovec size + * + * @return the real size or -1 + */ +tb_long_t tb_socket_usendv(tb_socket_ref_t sock, tb_ipaddr_ref_t addr, tb_iovec_t const* list, tb_size_t size); + +/*! wait socket events + * + * @param sock the sock + * @param events the socket events + * @param timeout the timeout, infinity: -1 + * + * @return > 0: the events code, 0: timeout, -1: failed + */ +tb_long_t tb_socket_wait(tb_socket_ref_t sock, tb_size_t events, tb_long_t timeout); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/spinlock.h b/core/pkg/tbox.pkg/inc/tbox/platform/spinlock.h index 4d734a15e..bb7de69b2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/spinlock.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/spinlock.h @@ -207,3 +207,212 @@ static __tb_inline_force__ tb_void_t tb_spinlock_leave(tb_spinlock_ref_t lock) } #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file spinlock.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_SPINLOCK_H +#define TB_PLATFORM_SPINLOCK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "sched.h" +#include "atomic.h" +#include "../utils/lock_profiler.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the initial value +#define TB_SPINLOCK_INIT (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init spinlock + * + * @param lock the lock + * + * @return tb_true or tb_false + */ +static __tb_inline_force__ tb_bool_t tb_spinlock_init(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + + // init + *lock = 0; + + // ok + return tb_true; +} + +/*! exit spinlock + * + * @param lock the lock + */ +static __tb_inline_force__ tb_void_t tb_spinlock_exit(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + + // exit + *lock = 0; +} + +/*! enter spinlock + * + * @param lock the lock + */ +static __tb_inline_force__ tb_void_t tb_spinlock_enter(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + + // init tryn + tb_size_t tryn = 5; + + // init occupied +#ifdef TB_LOCK_PROFILER_ENABLE + tb_bool_t occupied = tb_false; +#endif + + // lock it + while (tb_atomic_fetch_and_pset((tb_atomic_t*)lock, 0, 1)) + { +#ifdef TB_LOCK_PROFILER_ENABLE + // occupied + if (!occupied) + { + // occupied++ + occupied = tb_true; + tb_lock_profiler_occupied(tb_lock_profiler(), (tb_pointer_t)lock); + + // dump backtrace +#if 0//def __tb_debug__ + tb_backtrace_dump("spinlock", tb_null, 10); +#endif + } +#endif + + // yield the processor + if (!tryn--) + { + // yield + tb_sched_yield(); +// tb_usleep(1); + + // reset tryn + tryn = 5; + } + } +} + +/*! enter spinlock without the lock profiler + * + * @param lock the lock + */ +static __tb_inline_force__ tb_void_t tb_spinlock_enter_without_profiler(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + + // init tryn + tb_size_t tryn = 5; + + // lock it + while (tb_atomic_fetch_and_pset((tb_atomic_t*)lock, 0, 1)) + { + // yield the processor + if (!tryn--) + { + // yield + tb_sched_yield(); +// tb_usleep(1); + + // reset tryn + tryn = 5; + } + } +} + +/*! try to enter spinlock + * + * @param lock the lock + * + * @return tb_true or tb_false + */ +static __tb_inline_force__ tb_bool_t tb_spinlock_enter_try(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + +#ifndef TB_LOCK_PROFILER_ENABLE + // try locking it + return !tb_atomic_fetch_and_pset((tb_atomic_t*)lock, 0, 1); +#else + // try locking it + tb_bool_t ok = !tb_atomic_fetch_and_pset((tb_atomic_t*)lock, 0, 1); + + // occupied? + if (!ok) tb_lock_profiler_occupied(tb_lock_profiler(), (tb_pointer_t)lock); + + // ok? + return ok; +#endif +} + +/*! try to enter spinlock without the lock profiler + * + * @param lock the lock + * + * @return tb_true or tb_false + */ +static __tb_inline_force__ tb_bool_t tb_spinlock_enter_try_without_profiler(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + + // try locking it + return !tb_atomic_fetch_and_pset((tb_atomic_t*)lock, 0, 1); +} + +/*! leave spinlock + * + * @param lock the lock + */ +static __tb_inline_force__ tb_void_t tb_spinlock_leave(tb_spinlock_ref_t lock) +{ + // check + tb_assert(lock); + + // leave + *((tb_atomic_t*)lock) = 0; +} + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/syserror.h b/core/pkg/tbox.pkg/inc/tbox/platform/syserror.h index e19bfda1d..6c507c491 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/syserror.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/syserror.h @@ -54,3 +54,59 @@ tb_size_t tb_syserror_state(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file syserror.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_SYSERROR_H +#define TB_PLATFORM_SYSERROR_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! get the last system error state + * + * @note the error code see state.h + * + * @return the last system error state + */ +tb_size_t tb_syserror_state(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/systemv/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/systemv/prefix.h index 2cd49391f..ebbd6c9dd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/systemv/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/systemv/prefix.h @@ -33,3 +33,38 @@ #include "../../utils/utils.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_SYSTEMV_PREFIX_H +#define TB_PLATFORM_SYSTEMV_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../platform.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/thread.h b/core/pkg/tbox.pkg/inc/tbox/platform/thread.h index f847459b8..caa9cbeae 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/thread.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/thread.h @@ -181,3 +181,186 @@ tb_bool_t tb_thread_once(tb_atomic_t* lock, tb_bool_t (*func)(tb_c __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file thread.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_THREAD_H +#define TB_PLATFORM_THREAD_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the thread func type + * + * @param priv the passed private data + * + * @return the return value + */ +typedef tb_int_t (*tb_thread_func_t)(tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init thread + * + * @code + static tb_int_t tb_demo_thread_func(tb_cpointer_t priv) + { + // self + tb_size_t self = tb_thread_self(); + + // trace + tb_trace_i("thread[%lx: %s]: init", priv, self); + + // exit thread and return failed value: -1 + // tb_thread_return(-1); + + // trace + tb_trace_i("thread[%lx: %s]: exit", priv, self); + + // ok + return 0; + } + + // init thread + tb_thread_ref_t thread = tb_thread_init(tb_null, tb_demo_thread_func, "hello", 0); + if (thread) + { + // wait thread + tb_int_t retval = 0; + if (tb_thread_wait(thread, -1, &retval) > 0) + { + // trace + tb_trace_i("wait: ok, retval: %d", retval); + } + + // exit thread + tb_thread_exit(thread); + } + * @endcode + * + * @param name the thread name, maybe null + * @param func the thread func + * @param priv the thread func private data + * @param stack the thread stack, using the default stack size if be zero + * + * @return the thread handle + */ +tb_thread_ref_t tb_thread_init(tb_char_t const* name, tb_thread_func_t func, tb_cpointer_t priv, tb_size_t stack); + +/*! exit thread + * + * @param thread the thread + */ +tb_void_t tb_thread_exit(tb_thread_ref_t thread); + +/*! wait thread + * + * @param thread the thread + * @param timeout the timeout + * @param retval the return value pointer of the thread (optional) + * + * @return ok: 1, timeout: 0, error: -1 + */ +tb_long_t tb_thread_wait(tb_thread_ref_t thread, tb_long_t timeout, tb_int_t* retval); + +/*! suspend thread + * + * @param thread the thread + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_suspend(tb_thread_ref_t thread); + +/*! resume thread + * + * @param thread the thread + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_resume(tb_thread_ref_t thread); + +/*! the self thread identifier + * + * @return the self thread identifier + */ +tb_size_t tb_thread_self(tb_noarg_t); + +/*! return the thread value + * + * @param value the return value of the thread + */ +tb_void_t tb_thread_return(tb_int_t value); + +/*! run the given function only once + * + * @code + + // the once function + static tb_bool_t tb_thread_once_func(tb_cpointer_t priv) + { + // trace + tb_trace_i("%s", priv); + + // ok + return tb_true; + } + + // run the once function + static tb_atomic_t once = 0; + if (tb_thread_once(&once, tb_thread_once_func, "hello")) + { + // ok + // ... + } + + * @endcode + * + * @param lock the global or static atomic lock pointer (need be initialized as zero) + * @param func the function + * @param priv the user private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_once(tb_atomic_t* lock, tb_bool_t (*func)(tb_cpointer_t), tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/thread_local.h b/core/pkg/tbox.pkg/inc/tbox/platform/thread_local.h index 8943a17e6..c4701be5c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/thread_local.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/thread_local.h @@ -168,3 +168,173 @@ tb_bool_t tb_thread_local_set(tb_thread_local_ref_t local, tb_cpoi __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file thread_local.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_THREAD_LOCAL_H +#define TB_PLATFORM_THREAD_LOCAL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../container/single_list_entry.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the thread local initial value +#define TB_THREAD_LOCAL_INIT {{0}, 0} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the thread local free function type + * + * @param priv the thread local private data + */ +typedef tb_void_t (*tb_thread_local_free_t)(tb_cpointer_t priv); + +/// the thread local type +typedef struct __tb_thread_local_t +{ + // the list entry + tb_single_list_entry_t entry; + + // have been initialized? + tb_bool_t inited; + + // the atomice lock of once function + tb_atomic_t once; + + // the free function + tb_thread_local_free_t free; + + /* the private data space for implementation + * + * - sizeof(pthread_key_t) * 2 for pthread key + * - sizeof(DWORD) * 2 for windows key + */ + tb_byte_t priv[16]; + +}tb_thread_local_t, *tb_thread_local_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init a thread local + * + * @note support to be called repeatly, thread safely and only init it once + * + * @code + + // the thread local free function + static tb_void_t tb_thread_local_free_func(tb_cpointer_t priv) + { + if (priv) tb_free(priv); + } + + // the thread function + static tb_cpointer_t th_thread_func(tb_cpointer_t priv) + { + // init the thread local, only once + static tb_thread_local_t s_local = TB_THREAD_LOCAL_INIT; + if (!tb_thread_local_init(&s_local, tb_thread_local_free_func)) return tb_null; + + // get the thread local data + tb_char_t const* data = tb_null; + if (!(data = tb_thread_local_get(&s_local))) + { + tb_char_t const* cstr = tb_strdup("hello"); + if (tb_thread_local_set(&s_local, cstr)) + data = cstr; + else tb_free(cstr); + } + + // trace + tb_trace_i("data: %s", data); + + ... + } + + * @endcode + * + * @param local the global or static thread local (need be initialized as TB_THREAD_LOCAL_INIT) + * @param func the thread local free function + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_local_init(tb_thread_local_ref_t local, tb_thread_local_free_t func); + +/*! exit the thread local (optional) + * + * @note this local will be exited automaticlly after tb_exit() was called + * and we call also manually call this function to exit the thread local in earlier time + * + * @param local the thread local reference + */ +tb_void_t tb_thread_local_exit(tb_thread_local_ref_t local); + +/*! has thead local data on the current thread? + * + * @param local the thread local reference + * + * @return the thread local private data + */ +tb_bool_t tb_thread_local_has(tb_thread_local_ref_t local); + +/*! get thead local data from the current thread + * + * @param local the thread local reference + * + * @return the thread local private data + */ +tb_pointer_t tb_thread_local_get(tb_thread_local_ref_t local); + +/*! set thead local data to the current thread + * + * @note this data will be freed automaticlly after it's thread was returned + * + * @param local the thread local reference + * @param priv the thread local private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_local_set(tb_thread_local_ref_t local, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/thread_pool.h b/core/pkg/tbox.pkg/inc/tbox/platform/thread_pool.h index a0023ab6b..ad64f800a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/thread_pool.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/thread_pool.h @@ -245,3 +245,250 @@ tb_void_t tb_thread_pool_dump(tb_thread_pool_ref_t pool); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file thread_pool.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_THREAD_POOL_H +#define TB_PLATFORM_THREAD_POOL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the thread pool worker private data maximum count +#ifdef __tb_small__ +# define TB_THREAD_POOL_WORKER_PRIV_MAXN (16) +#else +# define TB_THREAD_POOL_WORKER_PRIV_MAXN (32) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the thread pool ref type +typedef __tb_typeref__(thread_pool); + +/// the thread pool task ref type +typedef __tb_typeref__(thread_pool_task); + +/// the thread pool worker ref type +typedef __tb_typeref__(thread_pool_worker); + +/// the thread pool priv exit func type +typedef tb_void_t (*tb_thread_pool_priv_exit_func_t)(tb_thread_pool_worker_ref_t worker, tb_cpointer_t priv); + +/// the thread pool task done func type +typedef tb_void_t (*tb_thread_pool_task_done_func_t)(tb_thread_pool_worker_ref_t worker, tb_cpointer_t priv); + +/// the thread pool task exit func type +typedef tb_void_t (*tb_thread_pool_task_exit_func_t)(tb_thread_pool_worker_ref_t worker, tb_cpointer_t priv); + +/// the thread pool task type +typedef struct __tb_thread_pool_task_t +{ + /// the task name + tb_char_t const* name; + + /// the task done func + tb_thread_pool_task_done_func_t done; + + /// the task exit func for freeing the private data + tb_thread_pool_task_exit_func_t exit; + + /// the task private data + tb_cpointer_t priv; + + /// is urgent task? + tb_bool_t urgent; + +}tb_thread_pool_task_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the thread pool instance + * + * @return the thread pool + */ +tb_thread_pool_ref_t tb_thread_pool(tb_noarg_t); + +/*! init thread pool + * + * @param worker_maxn the thread worker max count, using the default count + * @param stack the thread stack, using the default stack size if be zero + * + * @return the thread pool + */ +tb_thread_pool_ref_t tb_thread_pool_init(tb_size_t worker_maxn, tb_size_t stack); + +/*! exit thread pool + * + * @param pool the thread pool + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_pool_exit(tb_thread_pool_ref_t pool); + +/*! kill thread pool, all workers and waiting tasks + * + * @param pool the thread pool + */ +tb_void_t tb_thread_pool_kill(tb_thread_pool_ref_t pool); + +/*! the current worker count + * + * @param pool the thread pool + * + * @return the current worker count + */ +tb_size_t tb_thread_pool_worker_size(tb_thread_pool_ref_t pool); + +/*! set the worker private data + * + * @param worker the thread pool worker + * @param index the private data index + * @param exit the private data exit func + * @param priv the private data + */ +tb_void_t tb_thread_pool_worker_setp(tb_thread_pool_worker_ref_t worker, tb_size_t index, tb_thread_pool_priv_exit_func_t exit, tb_cpointer_t priv); + +/*! get the worker private data + * + * @param worker the thread pool worker + * @param index the private data index + * + * @return the private data + */ +tb_cpointer_t tb_thread_pool_worker_getp(tb_thread_pool_worker_ref_t worker, tb_size_t index); + +/*! the current waiting task count + * + * @param pool the thread pool + * + * @return the current task count + */ +tb_size_t tb_thread_pool_task_size(tb_thread_pool_ref_t pool); + +/*! post one task + * + * @param pool the thread pool + * @param name the task name, optional + * @param done the task done func + * @param exit the task exit func, optional + * @param priv the task private data + * @param urgent is urgent task? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_thread_pool_task_post(tb_thread_pool_ref_t pool, tb_char_t const* name, tb_thread_pool_task_done_func_t done, tb_thread_pool_task_exit_func_t exit, tb_cpointer_t priv, tb_bool_t urgent); + +/*! post task list + * + * @param pool the thread pool + * @param list the task list + * @param size the task count + * + * @return the real posted task count + */ +tb_size_t tb_thread_pool_task_post_list(tb_thread_pool_ref_t pool, tb_thread_pool_task_t const* list, tb_size_t size); + +/*! init one task + * + * @param pool the thread pool + * @param name the task name, optional + * @param done the task done func + * @param exit the task exit func, optional + * @param priv the task private data + * @param urgent is urgent task? + * + * @return the thread pool task + */ +tb_thread_pool_task_ref_t tb_thread_pool_task_init(tb_thread_pool_ref_t pool, tb_char_t const* name, tb_thread_pool_task_done_func_t done, tb_thread_pool_task_exit_func_t exit, tb_cpointer_t priv, tb_bool_t urgent); + +/*! kill the waiting task + * + * @param pool the thread pool + * @param task the task handle + */ +tb_void_t tb_thread_pool_task_kill(tb_thread_pool_ref_t pool, tb_thread_pool_task_ref_t task); + +/*! kill all waiting tasks + * + * @param pool the thread pool + */ +tb_void_t tb_thread_pool_task_kill_all(tb_thread_pool_ref_t pool); + +/*! wait one task + * + * @param pool the thread pool + * @param task the thread pool task + * @param timeout the timeout + * + * @return ok: 1, timeout: 0, error: -1 + */ +tb_long_t tb_thread_pool_task_wait(tb_thread_pool_ref_t pool, tb_thread_pool_task_ref_t task, tb_long_t timeout); + +/*! wait all waiting tasks + * + * @param pool the thread pool + * @param timeout the timeout + * + * @return ok: 1, timeout: 0, error: -1 + */ +tb_long_t tb_thread_pool_task_wait_all(tb_thread_pool_ref_t pool, tb_long_t timeout); + +/*! exit the task + * + * @param pool the thread pool + * @param task the thread pool task + */ +tb_void_t tb_thread_pool_task_exit(tb_thread_pool_ref_t pool, tb_thread_pool_task_ref_t task); + +#ifdef __tb_debug__ +/*! dump the thread pool + * + * @param pool the thread pool + */ +tb_void_t tb_thread_pool_dump(tb_thread_pool_ref_t pool); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/time.h b/core/pkg/tbox.pkg/inc/tbox/platform/time.h index c99a91146..81cd36682 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/time.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/time.h @@ -85,3 +85,90 @@ tb_bool_t tb_gettimeofday(tb_timeval_t* tv, tb_timezone_t* tz); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file time.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_TIME_H +#define TB_PLATFORM_TIME_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! usleep + * + * @param us the microsecond time + */ +tb_void_t tb_usleep(tb_size_t us); + +/*! msleep + * + * @param ms the millisecond time + */ +tb_void_t tb_msleep(tb_size_t ms); + +/*! sleep + * + * @param s the second time + */ +tb_void_t tb_sleep(tb_size_t s); + +/*! clock, ms + * + * @return the mclock + */ +tb_hong_t tb_mclock(tb_noarg_t); + +/*! uclock, us + * + * @return the uclock + */ +tb_hong_t tb_uclock(tb_noarg_t); + +/*! get the time from 1970-01-01 00:00:00:000 + * + * @param tv the timeval + * @param tz the timezone + * + * @return tb_true or tb_false + */ +tb_bool_t tb_gettimeofday(tb_timeval_t* tv, tb_timezone_t* tz); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/timer.h b/core/pkg/tbox.pkg/inc/tbox/platform/timer.h index b7a0cc806..73a9e748c 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/timer.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/timer.h @@ -236,3 +236,241 @@ tb_void_t tb_timer_task_kill(tb_timer_ref_t timer, tb_timer_task_ref_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file timer.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_TIMER_H +#define TB_PLATFORM_TIMER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the timer task func type + * + * @param killed is killed? + * @param data the timer data + */ +typedef tb_void_t (*tb_timer_task_func_t)(tb_bool_t killed, tb_cpointer_t priv); + +/// the timer ref type +typedef __tb_typeref__(timer); + +/// the timer task ref type +typedef __tb_typeref__(timer_task); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the global timer + * + * @return the timer + */ +tb_timer_ref_t tb_timer(tb_noarg_t); + +/*! init timer + * + * @param grow the timer grow + * @param ctime using ctime? + * + * @return the timer + */ +tb_timer_ref_t tb_timer_init(tb_size_t grow, tb_bool_t ctime); + +/*! exit timer + * + * @param timer the timer + */ +tb_void_t tb_timer_exit(tb_timer_ref_t timer); + +/*! kill timer for tb_timer_loop() + * + * @param timer the timer + */ +tb_void_t tb_timer_kill(tb_timer_ref_t timer); + +/*! clear timer + * + * @param timer the timer + */ +tb_void_t tb_timer_clear(tb_timer_ref_t timer); + +/*! the timer delay for spak + * + * @param timer the timer + * + * @return the timer delay, (tb_size_t)-1: error or no task + */ +tb_size_t tb_timer_delay(tb_timer_ref_t timer); + +/*! the timer top when + * + * @param timer the timer + * + * @return the top when, -1: no task + */ +tb_hize_t tb_timer_top(tb_timer_ref_t timer); + +/*! spak timer for the external loop at the single thread + * + * @code + * tb_void_t tb_timer_loop() + * { + * while (1) + * { + * // wait + * wait(tb_timer_delay(timer)) + * + * // spak timer + * tb_timer_spak(timer); + * } + * } + * @endcode + * + * @param timer the timer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_timer_spak(tb_timer_ref_t timer); + +/*! loop timer for the external thread + * + * @code + * tb_void_t tb_timer_thread(tb_cpointer_t priv) + * { + * tb_timer_loop(timer); + * } + * @endcode + * + * @param timer the timer + * + * @return tb_true or tb_false + */ +tb_void_t tb_timer_loop(tb_timer_ref_t timer); + +/*! post timer task after delay and will be auto-remove it after be expired + * + * @param timer the timer + * @param delay the delay time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + */ +tb_void_t tb_timer_task_post(tb_timer_ref_t timer, tb_size_t delay, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv); + +/*! post timer task at the absolute time and will be auto-remove it after be expired + * + * @param timer the timer + * @param when the absolute time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + */ +tb_void_t tb_timer_task_post_at(tb_timer_ref_t timer, tb_hize_t when, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv); + +/*! post timer task after the relative time and will be auto-remove it after be expired + * + * @param timer the timer + * @param after the after time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + */ +tb_void_t tb_timer_task_post_after(tb_timer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv); + +/*! init and post timer task after delay and need remove it manually + * + * @param timer the timer + * @param delay the delay time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + * @return the timer task + */ +tb_timer_task_ref_t tb_timer_task_init(tb_timer_ref_t timer, tb_size_t delay, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv); + +/*! init and post timer task at the absolute time and need remove it manually + * + * @param timer the timer + * @param when the absolute time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + * @return the timer task + */ +tb_timer_task_ref_t tb_timer_task_init_at(tb_timer_ref_t timer, tb_hize_t when, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv); + +/*! init and post timer task after the relative time and need remove it manually + * + * @param timer the timer + * @param after the after time, ms + * @param period the period time, ms + * @param repeat is repeat? + * @param func the timer func + * @param priv the timer priv + * + * @return the timer task + */ +tb_timer_task_ref_t tb_timer_task_init_after(tb_timer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv); + +/*! exit timer task, the task will be not called if have been not called + * + * @param timer the timer + * @param task the timer task + */ +tb_void_t tb_timer_task_exit(tb_timer_ref_t timer, tb_timer_task_ref_t task); + +/*! kill timer task, the task will be called immediately if have been not called + * + * @param timer the timer + * @param task the timer task + */ +tb_void_t tb_timer_task_kill(tb_timer_ref_t timer, tb_timer_task_ref_t task); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/unix/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/unix/prefix.h index 2b33cf427..5942c167b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/unix/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/unix/prefix.h @@ -33,3 +33,38 @@ #include "../../utils/utils.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_UNIX_PREFIX_H +#define TB_PLATFORM_UNIX_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/unix/syscall.h b/core/pkg/tbox.pkg/inc/tbox/platform/unix/syscall.h index 26296e43a..ace129823 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/unix/syscall.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/unix/syscall.h @@ -185,3 +185,190 @@ } #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file syscall.h + * @ingroup platform + */ +#ifndef TB_PLATFORM_UNIX_SYSCALL_H +#define TB_PLATFORM_UNIX_SYSCALL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include <sys/syscall.h> + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_ASSEMBLER_IS_GAS + +// x64 +# if defined(TB_ARCH_x64) +# define tb_syscall1(code, name, type1, arg1) \ + static __tb_inline__ tb_long_t name(type1 arg1) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("syscall" : "=a" (__ret) : \ + "0" (code), "D" (arg1)); \ + return __ret; \ + } + +# define tb_syscall2(code, name, type1, arg1, type2, arg2) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("syscall" : "=a" (__ret) : \ + "0" (code), "D" (arg1), "S" (arg2)); \ + return __ret; \ + } + +# define tb_syscall3(code, name, type1, arg1, type2, arg2, type3, arg3) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("syscall" : "=a" (__ret) : \ + "0" (code), "D" (arg1), "S" (arg2), \ + "d" (arg3) \ + ); \ + return __ret; \ + } + +# define tb_syscall4(code, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("movq %5,%%r10 ; syscall" : "=a" (__ret) : \ + "0" (code), "D" (arg1), "S" (arg2), \ + "d" (arg3), "g" (arg4) \ + ); \ + return __ret; \ + } + +# define tb_syscall5(code, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("movq %5,%%r10 ; movq %6,%%r8 ; syscall" : "=a" (__ret) : \ + "0" (code), "D" (arg1), "S" (arg2), \ + "d" (arg3), "g" (arg4), "g" (arg5) \ + ); \ + return __ret; \ + } + +// x86 +# elif defined(TB_ARCH_x86) +# define tb_syscall1(code, name, type1, arg1) \ + static __tb_inline__ tb_long_t name(type1 arg1) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("int $0x80" : "=a" (__ret) : \ + "0" (code), "b" (arg1)); \ + return __ret; \ + } + +# define tb_syscall2(code, name, type1, arg1, type2, arg2) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("int $0x80" : "=a" (__ret) : \ + "0" (code), "b" (arg1), "c" (arg2)); \ + return __ret; \ + } + +# define tb_syscall3(code, name, type1, arg1, type2, arg2, type3, arg3) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("int $0x80" : "=a" (__ret) : \ + "0" (code), "b" (arg1), "c" (arg2), \ + "d" (arg3) \ + ); \ + return __ret; \ + } + +# define tb_syscall4(code, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("int $0x80" : "=a" (__ret) : \ + "0" (code), "b" (arg1), "c" (arg2), \ + "d" (arg3), "S" (arg4) \ + ); \ + return __ret; \ + } + +# define tb_syscall5(code, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \ + { \ + tb_long_t __ret; \ + __asm__ __volatile__("int $0x80" : "=a" (__ret) : \ + "0" (code), "b" (arg1), "c" (arg2), \ + "d" (arg3), "S" (arg4), "D" (arg5) \ + ); \ + return __ret; \ + } + +# endif +#endif + +#ifndef tb_syscall1 +# define tb_syscall1(code, name, type1, arg1) \ + static __tb_inline__ tb_long_t name(type1 arg1) \ + { \ + return syscall(code, arg1); \ + } +#endif + +#ifndef tb_syscall2 +# define tb_syscall2(code, name, type1, arg1, type2, arg2) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2) \ + { \ + return syscall(code, arg1, arg2); \ + } +#endif + +#ifndef tb_syscall3 +# define tb_syscall3(code, name, type1, arg1, type2, arg2, type3, arg3) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3) \ + { \ + return syscall(code, arg1, arg2, arg3); \ + } +#endif + +#ifndef tb_syscall4 +# define tb_syscall4(code, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ + { \ + return syscall(code, arg1, arg2, arg3, arg4); \ + } +#endif + +#ifndef tb_syscall5 +# define tb_syscall5(code, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \ + static __tb_inline__ tb_long_t name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \ + { \ + return syscall(code, arg1, arg2, arg3, arg4, arg5); \ + } +#endif +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic.h index 64cb788d1..ce5506bf0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic.h @@ -90,3 +90,95 @@ static __tb_inline__ tb_long_t tb_atomic_dec_and_fetch_windows(tb_atomic_t* a) #endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_ATOMIC_H +#define TB_PLATFORM_WINDOWS_ATOMIC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if !defined(tb_atomic_fetch_and_set) && !TB_CPU_BIT64 +# define tb_atomic_fetch_and_set(a, v) tb_atomic_fetch_and_set_windows(a, v) +#endif + +#if !defined(tb_atomic_fetch_and_pset) +# define tb_atomic_fetch_and_pset(a, p, v) tb_atomic_fetch_and_pset_windows(a, p, v) +#endif + +#if !defined(tb_atomic_fetch_and_add) && !TB_CPU_BIT64 +# define tb_atomic_fetch_and_add(a, v) tb_atomic_fetch_and_add_windows(a, v) +#endif + +#if !defined(tb_atomic_inc_and_fetch) && !TB_CPU_BIT64 +# define tb_atomic_inc_and_fetch(a) tb_atomic_inc_and_fetch_windows(a) +#endif + +#if !defined(tb_atomic_dec_and_fetch) && !TB_CPU_BIT64 +# define tb_atomic_dec_and_fetch(a) tb_atomic_dec_and_fetch_windows(a) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ +#if TB_CPU_BIT64 +static __tb_inline__ tb_long_t tb_atomic_fetch_and_pset_windows(tb_atomic_t* a, tb_long_t p, tb_long_t v) +{ + // check + tb_assert_static(sizeof(tb_atomic_t) == sizeof(LONGLONG)); + + // done + return (tb_long_t)InterlockedCompareExchange64((LONGLONG __tb_volatile__*)a, v, p); +} +#else +static __tb_inline__ tb_long_t tb_atomic_fetch_and_set_windows(tb_atomic_t* a, tb_long_t v) +{ + return (tb_long_t)InterlockedExchange((LONG __tb_volatile__*)a, v); +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_pset_windows(tb_atomic_t* a, tb_long_t p, tb_long_t v) +{ + return (tb_long_t)InterlockedCompareExchange((LONG __tb_volatile__*)a, v, p); +} +static __tb_inline__ tb_long_t tb_atomic_fetch_and_add_windows(tb_atomic_t* a, tb_long_t v) +{ + return (tb_long_t)InterlockedExchangeAdd((LONG __tb_volatile__*)a, v); +} +static __tb_inline__ tb_long_t tb_atomic_inc_and_fetch_windows(tb_atomic_t* a) +{ + return (tb_long_t)InterlockedIncrement((LONG __tb_volatile__*)a); +} +static __tb_inline__ tb_long_t tb_atomic_dec_and_fetch_windows(tb_atomic_t* a) +{ + return (tb_long_t)InterlockedDecrement((LONG __tb_volatile__*)a); +} +#endif + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic64.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic64.h index 12c358517..a7f59c096 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic64.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/atomic64.h @@ -79,3 +79,84 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file atomic64.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_ATOMIC64_H +#define TB_PLATFORM_WINDOWS_ATOMIC64_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "interface/interface.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#if !defined(tb_atomic64_fetch_and_pset) +# define tb_atomic64_fetch_and_pset(a, p, v) tb_atomic64_fetch_and_pset_windows(a, p, v) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * declaration + */ +tb_hong_t tb_atomic64_fetch_and_pset_generic(tb_atomic64_t* a, tb_hong_t p, tb_hong_t v); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +/* fetch and set the 64bits value if old_value == p + * + * @param a the atomic value + * @param p the compared value + * @param v the assigned value + * + * @return the old value + */ +static __tb_inline__ tb_hong_t tb_atomic64_fetch_and_pset_windows(tb_atomic64_t* a, tb_hong_t p, tb_hong_t v) +{ + // the InterlockedCompareExchange64 func + tb_kernel32_InterlockedCompareExchange64_t pInterlockedCompareExchange64 = tb_kernel32()->InterlockedCompareExchange64; + + // done + if (pInterlockedCompareExchange64) return (tb_hong_t)pInterlockedCompareExchange64((LONGLONG __tb_volatile__*)a, v, p); + + // using the generic implementation + return tb_atomic64_fetch_and_pset_generic(a, p, v); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/barrier.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/barrier.h index ec7167338..ec9b08077 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/barrier.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/barrier.h @@ -43,3 +43,48 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file barrier.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_BARRIER_H +#define TB_PLATFORM_WINDOWS_BARRIER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#if defined(MemoryBarrier) +# define tb_barrier() MemoryBarrier() +#elif defined(_AMD64_) +# define tb_barrier() __faststorefence() +#elif defined(_IA64_) +# define tb_barrier() __mf() +#endif + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/exception.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/exception.h index 397193901..8fabf42b2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/exception.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/exception.h @@ -225,3 +225,230 @@ static __tb_inline__ tb_int_t tb_exception_func_impl(tb_pointer_t record, tb_exc #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file exception.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_EXCEPTION_H +#define TB_PLATFORM_WINDOWS_EXCEPTION_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if defined(tb_setjmp) && defined(tb_longjmp) + +# if defined(TB_COMPILER_IS_MSVC) +# define __tb_try __try +# define __tb_except(x) __except(!!(x)) +# define __tb_leave __leave +# define __tb_end +# elif defined(TB_ASSEMBLER_IS_GAS) && !TB_CPU_BIT64 + + // try +# define __tb_try \ + do \ + { \ + /* init */ \ + tb_exception_handler_t __h = {{0}}; \ + tb_exception_registration_t __r = {0}; \ + \ + /* init handler */ \ + __r.handler = (tb_exception_func_t)tb_exception_func_impl; \ + __r.exception_handler = &__h; \ + /* push seh */ \ + __tb_asm__ __tb_volatile__ ("movl %%fs:0, %0" : "=r" (__r.prev)); \ + __tb_asm__ __tb_volatile__ ("movl %0, %%fs:0" : : "r" (&__r)); \ + \ + /* save jmpbuf */ \ + __tb_volatile__ tb_int_t __j = tb_setjmp(__h.jmpbuf); \ + if (!__j) \ + { + + // except +# define __tb_except(x) \ + } \ + \ + /* check */ \ + tb_assert(x >= 0); \ + /* do not this catch? goto the top exception stack */ \ + if (__j && !(x)) \ + { \ + if (__r.prev && __r.prev->exception_handler) tb_longjmp(__r.prev->exception_handler->jmpbuf, 1); \ + else \ + { \ + /* no exception handler */ \ + tb_assert_and_check_break(0); \ + } \ + } \ + /* pop seh */ \ + __tb_asm__ __tb_volatile__ ("movl %0, %%fs:0" : : "r" (__r.prev)); \ + if (__j) + + // end +# define __tb_end \ + } while (0); + + // leave +# define __tb_leave break + +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// for mingw +#if defined(tb_setjmp) \ + && defined(tb_longjmp) \ + && !defined(TB_COMPILER_IS_MSVC) \ + && defined(TB_ASSEMBLER_IS_GAS) \ + && !TB_CPU_BIT64 + +#include "../../prefix/packed.h" + +// the seh expception handler func type +typedef tb_int_t (*tb_exception_func_t)(tb_pointer_t, tb_pointer_t, tb_pointer_t, tb_pointer_t); + +// the expception float context type +typedef struct __tb_exception_context_float_t +{ + tb_uint32_t controlword; + tb_uint32_t statusword; + tb_uint32_t tagword; + tb_uint32_t erroroffset; + tb_uint32_t errorselector; + tb_uint32_t dataoffset; + tb_uint32_t dataselector; + tb_byte_t registerarea[80]; + tb_uint32_t cr0npxstate; + +}__tb_packed__ tb_exception_context_float_t ; + +// the expception context type +typedef struct __tb_exception_context_t +{ + tb_uint32_t contextflags; + tb_uint32_t dr0; + tb_uint32_t dr1; + tb_uint32_t dr2; + tb_uint32_t dr3; + tb_uint32_t dr6; + tb_uint32_t dr7; + tb_exception_context_float_t floatsave; + tb_uint32_t seggs; + tb_uint32_t segfs; + tb_uint32_t seges; + tb_uint32_t segds; + tb_uint32_t edi; + tb_uint32_t esi; + tb_uint32_t ebx; + tb_uint32_t edx; + tb_uint32_t ecx; + tb_uint32_t eax; + tb_uint32_t ebp; + tb_uint32_t eip; + tb_uint32_t segcs; + tb_uint32_t eflags; + tb_uint32_t esp; + tb_uint32_t segss; + tb_byte_t extendedregisters[512]; + +}__tb_packed__ tb_exception_context_t; + +// the expception record type +typedef struct __tb_exception_record_t +{ + // the expception code + tb_uint32_t exception_code; + + // the expception flags + tb_uint32_t exception_flags; + + // the expception record + struct __tb_exception_record_t* exception_record; + + // the expception address + tb_pointer_t exception_address; + + // the parameters number + tb_uint32_t number_parameters; + + // the expception information + tb_pointer_t exception_information[15]; + +}__tb_packed__ tb_exception_record_t; + +// the expception registration type +struct __tb_exception_handler_t; +typedef struct __tb_exception_registration_t +{ + // the previous seh exception registration + struct __tb_exception_registration_t* prev; + + // the exception handler + tb_exception_func_t handler; + + // the seh handler + struct __tb_exception_handler_t* exception_handler; + +}tb_exception_registration_t; + +// the exception handler type +typedef struct __tb_exception_handler_t +{ + // the jmpbuf + tb_jmpbuf_t jmpbuf; + + // the exception record + tb_exception_record_t record; + + // the context + tb_exception_context_t context; + +}tb_exception_handler_t; + +#include "../../prefix/packed.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * handler + */ +static __tb_inline__ tb_int_t tb_exception_func_impl(tb_pointer_t record, tb_exception_registration_t* reg, tb_pointer_t context, tb_pointer_t record2) +{ + tb_assert(reg && reg->exception_handler && context && record); + if (context) tb_memcpy(®->exception_handler->context, context, sizeof(tb_exception_context_t)); + if (record) tb_memcpy(®->exception_handler->record, record, sizeof(tb_exception_record_t)); + tb_longjmp(reg->exception_handler->jmpbuf, 1); +} + +#endif /* for mingw */ + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/dbghelp.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/dbghelp.h index e8081a021..18b55fcdd 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/dbghelp.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/dbghelp.h @@ -100,3 +100,105 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file dbghelp.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_DBGHELP_H +#define TB_PLATFORM_WINDOWS_INTERFACE_DBGHELP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the symbol info type +typedef struct __tb_dbghelp_symbol_info_t +{ + ULONG SizeOfStruct; + ULONG TypeIndex; + ULONG64 Reserved[2]; + ULONG info; + ULONG Size; + ULONG64 ModBase; + ULONG Flags; + ULONG64 Value; + ULONG64 Address; + ULONG Register; + ULONG Scope; + ULONG Tag; + ULONG NameLen; + ULONG MaxNameLen; + CHAR Name[1]; + +}tb_dbghelp_symbol_info_t; + +// the SymInitialize func type +typedef BOOL (WINAPI* tb_dbghelp_SymInitialize_t)(HANDLE hProcess, LPCTSTR UserSearchPath, BOOL fInvadeProcess); + +// the SymFromAddr func type +typedef BOOL (WINAPI* tb_dbghelp_SymFromAddr_t)(HANDLE hProcess, DWORD64 Address, PDWORD64 Displacement, tb_dbghelp_symbol_info_t* Symbol); + +// the SymSetOptions func type +typedef DWORD (WINAPI* tb_dbghelp_SymSetOptions_t)(DWORD SymOptions); + +// the dbghelp interfaces type +typedef struct __tb_dbghelp_t +{ + // SymInitialize + tb_dbghelp_SymInitialize_t SymInitialize; + + // SymFromAddr + tb_dbghelp_SymFromAddr_t SymFromAddr; + + // SymSetOptions + tb_dbghelp_SymSetOptions_t SymSetOptions; + +}tb_dbghelp_t, *tb_dbghelp_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the dbghelp interfaces + * + * @return the dbghelp interfaces pointer + */ +tb_dbghelp_ref_t tb_dbghelp(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/interface.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/interface.h index 5a0b8a13d..c9758bee2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/interface.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/interface.h @@ -39,3 +39,44 @@ #include "iphlpapi.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file interface.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_H +#define TB_PLATFORM_WINDOWS_INTERFACE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "ole32.h" +#include "user32.h" +#include "ws2_32.h" +#include "mswsock.h" +#include "shell32.h" +#include "dbghelp.h" +#include "kernel32.h" +#include "iphlpapi.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/iphlpapi.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/iphlpapi.h index 157e532f7..33976df9b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/iphlpapi.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/iphlpapi.h @@ -79,3 +79,84 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file iphlpapi.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_IPHLPAPI_H +#define TB_PLATFORM_WINDOWS_INTERFACE_IPHLPAPI_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the GetNetworkParams func type +typedef DWORD (WINAPI* tb_iphlpapi_GetNetworkParams_t)(PFIXED_INFO pFixedInfo, PULONG pOutBufLen); + +// the GetAdaptersInfo func type +typedef DWORD (WINAPI* tb_iphlpapi_GetAdaptersInfo_t)(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen); + +// the GetAdaptersAddresses func type +typedef ULONG (WINAPI* tb_iphlpapi_GetAdaptersAddresses_t)(ULONG Family, ULONG Flags, PVOID Reserved, PIP_ADAPTER_ADDRESSES AdapterAddresses, PULONG SizePointer); + +// the iphlpapi interfaces type +typedef struct __tb_iphlpapi_t +{ + // GetNetworkParams + tb_iphlpapi_GetNetworkParams_t GetNetworkParams; + + // GetAdaptersInfo + tb_iphlpapi_GetAdaptersInfo_t GetAdaptersInfo; + + // GetAdaptersAddresses + tb_iphlpapi_GetAdaptersAddresses_t GetAdaptersAddresses; + +}tb_iphlpapi_t, *tb_iphlpapi_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the iphlpapi interfaces + * + * @return the iphlpapi interfaces pointer + */ +tb_iphlpapi_ref_t tb_iphlpapi(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h index 14b1a3a39..bb947cfc8 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h @@ -179,3 +179,184 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file kernel32.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_KERNEL32_H +#define TB_PLATFORM_WINDOWS_INTERFACE_KERNEL32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the OVERLAPPED_ENTRY type +typedef struct _tb_OVERLAPPED_ENTRY_t +{ + ULONG_PTR lpCompletionKey; + LPOVERLAPPED lpOverlapped; + ULONG_PTR Internal; + DWORD dwNumberOfBytesTransferred; + +}tb_OVERLAPPED_ENTRY_t, *tb_LPOVERLAPPED_ENTRY_t; + +// the GetQueuedCompletionStatusEx func type +typedef BOOL (WINAPI* tb_kernel32_GetQueuedCompletionStatusEx_t)(HANDLE CompletionPort, tb_LPOVERLAPPED_ENTRY_t lpCompletionPortEntries, ULONG ulCount, PULONG ulNumEntriesRemoved, DWORD dwMilliseconds, BOOL fAlertable); + +// the CancelIoEx func type +typedef BOOL (WINAPI* tb_kernel32_CancelIoEx_t)(HANDLE hFile, LPOVERLAPPED lpOverlapped); + +// the RtlCaptureStackBackTrace func type +typedef USHORT (WINAPI* tb_kernel32_RtlCaptureStackBackTrace_t)(ULONG FramesToSkip, ULONG FramesToCapture, PVOID *BackTrace, PULONG BackTraceHash); + +// the GetFileSizeEx func type +typedef BOOL (WINAPI* tb_kernel32_GetFileSizeEx_t)(HANDLE hFile, PLARGE_INTEGER lpFileSize); + +// the InterlockedCompareExchange64 func type +typedef LONGLONG (WINAPI* tb_kernel32_InterlockedCompareExchange64_t)(LONGLONG __tb_volatile__* Destination, LONGLONG Exchange, LONGLONG Comparand); + +// the GetEnvironmentVariableW func type +typedef DWORD (WINAPI* tb_kernel32_GetEnvironmentVariableW_t)(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize); + +// the SetEnvironmentVariableW func type +typedef BOOL (WINAPI* tb_kernel32_SetEnvironmentVariableW_t)(LPCWSTR lpName, LPCWSTR lpValue); + +// the CreateProcessW func type +typedef BOOL (WINAPI* tb_kernel32_CreateProcessW_t)(LPCWSTR lpApplicationName, LPCWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); + +// the CloseHandle func type +typedef BOOL (WINAPI* tb_kernel32_CloseHandle_t)(HANDLE hObject); + +// the WaitForSingleObject func type +typedef DWORD (WINAPI* tb_kernel32_WaitForSingleObject_t)(HANDLE hHandle, DWORD dwMilliseconds); + +// the WaitForMultipleObjects func type +typedef DWORD (WINAPI* tb_kernel32_WaitForMultipleObjects_t)(DWORD nCount, const HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds); + +// the GetExitCodeProcess func type +typedef BOOL (WINAPI* tb_kernel32_GetExitCodeProcess_t)(HANDLE hProcess, LPDWORD lpExitCode); + +// the TerminateProcess func type +typedef BOOL (WINAPI* tb_kernel32_TerminateProcess_t)(HANDLE hProcess, UINT uExitCode); + +// the SuspendThread func type +typedef DWORD (WINAPI* tb_kernel32_SuspendThread_t)(HANDLE hThread); + +// the ResumeThread func type +typedef DWORD (WINAPI* tb_kernel32_ResumeThread_t)(HANDLE hThread); + +// the GetEnvironmentStringsW func type +typedef LPWCH (WINAPI* tb_kernel32_GetEnvironmentStringsW_t)(tb_void_t); + +// the FreeEnvironmentStringsW func type +typedef DWORD (WINAPI* tb_kernel32_FreeEnvironmentStringsW_t)(LPWCH lpszEnvironmentBlock); + +// the SetHandleInformation func type +typedef BOOL (WINAPI* tb_kernel32_SetHandleInformation_t)(HANDLE hObject, DWORD dwMask, DWORD dwFlags); + +// the kernel32 interfaces type +typedef struct __tb_kernel32_t +{ + // CancelIoEx +// tb_kernel32_CancelIoEx_t CancelIoEx; + + // CaptureStackBackTrace + tb_kernel32_RtlCaptureStackBackTrace_t RtlCaptureStackBackTrace; + + // GetFileSizeEx + tb_kernel32_GetFileSizeEx_t GetFileSizeEx; + + // GetQueuedCompletionStatusEx + tb_kernel32_GetQueuedCompletionStatusEx_t GetQueuedCompletionStatusEx; + + // InterlockedCompareExchange64 + tb_kernel32_InterlockedCompareExchange64_t InterlockedCompareExchange64; + + // GetEnvironmentVariableW + tb_kernel32_GetEnvironmentVariableW_t GetEnvironmentVariableW; + + // SetEnvironmentVariableW + tb_kernel32_SetEnvironmentVariableW_t SetEnvironmentVariableW; + + // CreateProcessW + tb_kernel32_CreateProcessW_t CreateProcessW; + + // CloseHandle + tb_kernel32_CloseHandle_t CloseHandle; + + // WaitForSingleObject + tb_kernel32_WaitForSingleObject_t WaitForSingleObject; + + // WaitForMultipleObjects + tb_kernel32_WaitForMultipleObjects_t WaitForMultipleObjects; + + // GetExitCodeProcess + tb_kernel32_GetExitCodeProcess_t GetExitCodeProcess; + + // TerminateProcess + tb_kernel32_TerminateProcess_t TerminateProcess; + + // SuspendThread + tb_kernel32_SuspendThread_t SuspendThread; + + // ResumeThread + tb_kernel32_ResumeThread_t ResumeThread; + + // GetEnvironmentStringsW + tb_kernel32_GetEnvironmentStringsW_t GetEnvironmentStringsW; + + // FreeEnvironmentStringsW + tb_kernel32_FreeEnvironmentStringsW_t FreeEnvironmentStringsW; + + // SetHandleInformation + tb_kernel32_SetHandleInformation_t SetHandleInformation; + +}tb_kernel32_t, *tb_kernel32_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the kernel32 interfaces + * + * @return the kernel32 interfaces pointer + */ +tb_kernel32_ref_t tb_kernel32(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/mswsock.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/mswsock.h index ebeafadf7..fe030eed7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/mswsock.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/mswsock.h @@ -91,3 +91,96 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file mswsock.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_MSWSOCK_H +#define TB_PLATFORM_WINDOWS_INTERFACE_MSWSOCK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the AcceptEx func type +typedef BOOL (WINAPI* tb_mswsock_AcceptEx_t)(SOCKET sListenSocket, SOCKET sAcceptSocket, PVOID lpOutputBuffer, DWORD dwReceiveDataLength, DWORD dwLocalAddressLength, DWORD dwRemoteAddressLength, LPDWORD lpdwBytesReceived, LPOVERLAPPED lpOverlapped); + +// the ConnectEx func type +typedef BOOL (WINAPI* tb_mswsock_ConnectEx_t)(SOCKET s, struct sockaddr const* name, tb_int_t namelen, PVOID lpSendBuffer, DWORD dwSendDataLength, LPDWORD lpdwBytesSent, LPOVERLAPPED lpOverlapped); + +// the DisconnectEx func type +typedef BOOL (WINAPI* tb_mswsock_DisconnectEx_t)(SOCKET hSocket, LPOVERLAPPED lpOverlapped, DWORD dwFlags, DWORD reserved); + +// the TransmitFile func type +typedef BOOL (WINAPI* tb_mswsock_TransmitFile_t)(SOCKET hSocket, HANDLE hFile, DWORD nNumberOfBytesToWrite, DWORD nNumberOfBytesPerSend, LPOVERLAPPED lpOverlapped, LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers, DWORD dwReserved); + +// the GetAcceptExSockaddrs func type +typedef tb_void_t (WINAPI* tb_mswsock_GetAcceptExSockaddrs_t)(PVOID lpOutputBuffer, DWORD dwReceiveDataLength, DWORD dwLocalAddressLength, DWORD dwRemoteAddressLength, LPSOCKADDR *LocalSockaddr, LPINT LocalSockaddrLength, LPSOCKADDR *RemoteSockaddr, LPINT RemoteSockaddrLength); + +// the mswsock interfaces type +typedef struct __tb_mswsock_t +{ + // AcceptEx + tb_mswsock_AcceptEx_t AcceptEx; + + // ConnectEx + tb_mswsock_ConnectEx_t ConnectEx; + + // DisconnectEx + tb_mswsock_DisconnectEx_t DisconnectEx; + + // TransmitFile + tb_mswsock_TransmitFile_t TransmitFile; + + // GetAcceptExSockaddrs + tb_mswsock_GetAcceptExSockaddrs_t GetAcceptExSockaddrs; + +}tb_mswsock_t, *tb_mswsock_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the mswsock interfaces + * + * @return the mswsock interfaces pointer + */ +tb_mswsock_ref_t tb_mswsock(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ole32.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ole32.h index ca0f2ab3f..8d5de58e3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ole32.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ole32.h @@ -66,3 +66,71 @@ tb_ole32_ref_t tb_ole32(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ole32.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_OLE32_H +#define TB_PLATFORM_WINDOWS_INTERFACE_OLE32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the CoCreateGuid func type +typedef HRESULT (WSAAPI* tb_ole32_CoCreateGuid_t)(GUID* pguid); + +// the ole32 interfaces type +typedef struct __tb_ole32_t +{ + // CoCreateGuid + tb_ole32_CoCreateGuid_t CoCreateGuid; + +}tb_ole32_t, *tb_ole32_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the ole32 interfaces + * + * @return the ole32 interfaces pointer + */ +tb_ole32_ref_t tb_ole32(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/prefix.h index 6786d4d65..8b50e1d05 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/prefix.h @@ -43,3 +43,48 @@ } while (0) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_PREFIX_H +#define TB_PLATFORM_WINDOWS_INTERFACE_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../../atomic.h" +#include "../../dynamic.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define TB_INTERFACE_LOAD(module_name, interface_name) \ + do \ + { \ + module_name->interface_name = (tb_##module_name##_##interface_name##_t)GetProcAddress((HMODULE)module, #interface_name); \ + \ + } while (0) + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/shell32.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/shell32.h index ac0f58d5f..202ca94cc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/shell32.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/shell32.h @@ -73,3 +73,78 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file shell32.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_SHELL32_H +#define TB_PLATFORM_WINDOWS_INTERFACE_SHELL32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the SHGetSpecialFolderLocation func type +typedef HRESULT (WINAPI* tb_shell32_SHGetSpecialFolderLocation_t)(HWND hwndOwner, tb_int_t nFolder, tb_handle_t *ppidl); + +// the SHGetPathFromIDListW func type +typedef BOOL (WINAPI* tb_shell32_SHGetPathFromIDListW_t)(tb_handle_t pidl, LPWSTR pszPath); + +// the shell32 interfaces type +typedef struct __tb_shell32_t +{ + // SHGetSpecialFolderLocation + tb_shell32_SHGetSpecialFolderLocation_t SHGetSpecialFolderLocation; + + // SHGetPathFromIDListW + tb_shell32_SHGetPathFromIDListW_t SHGetPathFromIDListW; + +}tb_shell32_t, *tb_shell32_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the shell32 interfaces + * + * @return the shell32 interfaces pointer + */ +tb_shell32_ref_t tb_shell32(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/user32.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/user32.h index bcb9c747c..2df2991e8 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/user32.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/user32.h @@ -67,3 +67,72 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file user32.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_USER32_H +#define TB_PLATFORM_WINDOWS_INTERFACE_USER32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the GetSystemMetrics func type +typedef tb_int_t (WINAPI* tb_user32_GetSystemMetrics_t)(tb_int_t index); + +// the user32 interfaces type +typedef struct __tb_user32_t +{ + // GetSystemMetrics + tb_user32_GetSystemMetrics_t GetSystemMetrics; + +}tb_user32_t, *tb_user32_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the user32 interfaces + * + * @return the user32 interfaces pointer + */ +tb_user32_ref_t tb_user32(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ws2_32.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ws2_32.h index 946e289ed..0087e3dce 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ws2_32.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/ws2_32.h @@ -246,3 +246,251 @@ tb_ws2_32_ref_t tb_ws2_32(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ws2_32.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_INTERFACE_WS2_32_H +#define TB_PLATFORM_WINDOWS_INTERFACE_WS2_32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the WSAStartup func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSAStartup_t)(WORD wVersionRequested, LPWSADATA lpWSAData); + +// the WSACleanup func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSACleanup_t)(tb_void_t); + +// the WSASocketA func type +typedef SOCKET (WSAAPI* tb_ws2_32_WSASocketA_t)(tb_int_t af, tb_int_t type, tb_int_t protocol, LPWSAPROTOCOL_INFO lpProtocolInfo, GROUP g, DWORD dwFlags); + +// the WSAGetLastError func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSAGetLastError_t)(tb_void_t); + +// the WSAIoctl func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSAIoctl_t)(SOCKET s, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + +// the WSASend func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSASend_t)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesSent, DWORD dwFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + +// the WSARecv func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSARecv_t)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + +// the WSASendTo func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSASendTo_t)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesSent, DWORD dwFlags, struct sockaddr const* lpTo, tb_int_t iToLen, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + +// the WSARecvFrom func type +typedef tb_int_t (WSAAPI* tb_ws2_32_WSARecvFrom_t)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, struct sockaddr* lpFrom, LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + +// the send func type +typedef tb_int_t (WSAAPI* tb_ws2_32_send_t)(SOCKET s, tb_char_t const* buf, tb_int_t len, tb_int_t flags); + +// the recv func type +typedef tb_int_t (WSAAPI* tb_ws2_32_recv_t)(SOCKET s, tb_char_t *buf, tb_int_t len, tb_int_t flags); + +// the sendto func type +typedef tb_int_t (WSAAPI* tb_ws2_32_sendto_t)(SOCKET s, tb_char_t const* buf, tb_int_t len, tb_int_t flags, struct sockaddr const* to, tb_int_t tolen); + +// the recvfrom func type +typedef tb_int_t (WSAAPI* tb_ws2_32_recvfrom_t)(SOCKET s, tb_char_t* buf, tb_int_t len, tb_int_t flags, struct sockaddr* from, tb_int_t* fromlen); + +// the bind func type +typedef tb_int_t (WSAAPI* tb_ws2_32_bind_t)(SOCKET s, struct sockaddr const* name, tb_int_t namelen); + +// the accept func type +typedef SOCKET (WSAAPI* tb_ws2_32_accept_t)(SOCKET s, struct sockaddr* addr, tb_int_t* addrlen); + +// the listen func type +typedef tb_int_t (WSAAPI* tb_ws2_32_listen_t)(SOCKET s, tb_int_t backlog); + +// the select func type +typedef tb_int_t (WSAAPI* tb_ws2_32_select_t)(tb_int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval const* timeout); + +// the connect func type +typedef tb_int_t (WSAAPI* tb_ws2_32_connect_t)(SOCKET s, struct sockaddr const* name, tb_int_t namelen); + +// the shutdown func type +typedef tb_int_t (WSAAPI* tb_ws2_32_shutdown_t)(SOCKET s, tb_int_t how); + +// the getsockname func type +typedef tb_int_t (WSAAPI* tb_ws2_32_getsockname_t)(SOCKET s, struct sockaddr* name, tb_int_t* namelen); + +// the getsockopt func type +typedef tb_int_t (WSAAPI* tb_ws2_32_getsockopt_t)(SOCKET s, tb_int_t level, tb_int_t optname, tb_char_t* optval, tb_int_t* optlen); + +// the setsockopt func type +typedef tb_int_t (WSAAPI* tb_ws2_32_setsockopt_t)(SOCKET s, tb_int_t level, tb_int_t optname, tb_char_t const* optval, tb_int_t optlen); + +// the ioctlsocket func type +typedef tb_int_t (WSAAPI* tb_ws2_32_ioctlsocket_t)(SOCKET s, LONG cmd, u_long* argp); + +// the closesocket func type +typedef tb_int_t (WSAAPI* tb_ws2_32_closesocket_t)(SOCKET s); + +// the gethostname func type +typedef tb_int_t (WSAAPI* tb_ws2_32_gethostname_t)(tb_char_t* name, tb_int_t namelen); + +// the __WSAFDIsSet func type +typedef tb_int_t (WSAAPI* tb_ws2_32___WSAFDIsSet_t)(SOCKET fd, fd_set* set); + +// the getaddrinfo func type +typedef tb_int_t (WSAAPI* tb_ws2_32_getaddrinfo_t)(PCSTR pNodeName, PCSTR pServiceName, ADDRINFOA *pHints, PADDRINFOA *ppResult); + +// the freeaddrinfo func type +typedef tb_void_t (WSAAPI* tb_ws2_32_freeaddrinfo_t)(struct addrinfo* ai); + +// the getnameinfo func type +typedef tb_int_t (WSAAPI* tb_ws2_32_getnameinfo_t)(const struct sockaddr FAR *sa, socklen_t salen, tb_char_t FAR *host, DWORD hostlen, tb_char_t FAR *serv, DWORD servlen, tb_int_t flags); + +// the gethostbyname func type +typedef struct hostent* (WSAAPI* tb_ws2_32_gethostbyname_t)(tb_char_t const* name); + +// the gethostbyaddr func type +typedef struct hostent* (WSAAPI* tb_ws2_32_gethostbyaddr_t)(tb_char_t const* addr, tb_int_t len, tb_int_t type); + +// the ws2_32 interfaces type +typedef struct __tb_ws2_32_t +{ + // WSAStartup + tb_ws2_32_WSAStartup_t WSAStartup; + + // WSACleanup + tb_ws2_32_WSACleanup_t WSACleanup; + + // WSASocketA + tb_ws2_32_WSASocketA_t WSASocketA; + + // WSAIoctl + tb_ws2_32_WSAIoctl_t WSAIoctl; + + // WSAGetLastError + tb_ws2_32_WSAGetLastError_t WSAGetLastError; + + // WSASend + tb_ws2_32_WSASend_t WSASend; + + // WSARecv + tb_ws2_32_WSARecv_t WSARecv; + + // WSASendTo + tb_ws2_32_WSASendTo_t WSASendTo; + + // WSARecvFrom + tb_ws2_32_WSARecvFrom_t WSARecvFrom; + + // bind + tb_ws2_32_bind_t bind; + + // send + tb_ws2_32_send_t send; + + // recv + tb_ws2_32_recv_t recv; + + // sendto + tb_ws2_32_sendto_t sendto; + + // recvfrom + tb_ws2_32_recvfrom_t recvfrom; + + // accept + tb_ws2_32_accept_t accept; + + // listen + tb_ws2_32_listen_t listen; + + // select + tb_ws2_32_select_t select; + + // connect + tb_ws2_32_connect_t connect; + + // shutdown + tb_ws2_32_shutdown_t shutdown; + + // getsockname + tb_ws2_32_getsockname_t getsockname; + + // getsockopt + tb_ws2_32_getsockopt_t getsockopt; + + // setsockopt + tb_ws2_32_setsockopt_t setsockopt; + + // ioctlsocket + tb_ws2_32_ioctlsocket_t ioctlsocket; + + // closesocket + tb_ws2_32_closesocket_t closesocket; + + // gethostname + tb_ws2_32_gethostname_t gethostname; + + // getaddrinfo + tb_ws2_32_getaddrinfo_t getaddrinfo; + + // freeaddrinfo + tb_ws2_32_freeaddrinfo_t freeaddrinfo; + + // getnameinfo + tb_ws2_32_getnameinfo_t getnameinfo; + + // gethostbyname + tb_ws2_32_gethostbyname_t gethostbyname; + + // gethostbyaddr + tb_ws2_32_gethostbyaddr_t gethostbyaddr; + + // __WSAFDIsSet + tb_ws2_32___WSAFDIsSet_t __WSAFDIsSet; + +}tb_ws2_32_t, *tb_ws2_32_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* the ws2_32 interfaces + * + * @return the ws2_32 interfaces pointer + */ +tb_ws2_32_ref_t tb_ws2_32(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/ntstatus.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/ntstatus.h index 580bc939e..8e7f4cd2f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/ntstatus.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/ntstatus.h @@ -408,3 +408,413 @@ static __tb_inline__ tb_size_t tb_ntstatus_to_winerror(tb_size_t status) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file ntstatus.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_NTSTATUS_H +#define TB_PLATFORM_WINDOWS_NTSTATUS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +//#include <ntdef.h> + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifndef FACILITY_NTWIN32 +# define FACILITY_NTWIN32 (0x7) +#endif + +#ifndef STATUS_SUCCESS +# define STATUS_SUCCESS (0x00000000) +#endif + +#ifndef STATUS_PENDING +# define STATUS_PENDING (0x00000103) +#endif + +#ifndef STATUS_END_OF_FILE +# define STATUS_END_OF_FILE (0xC0000011) +#endif + +#ifndef STATUS_INVALID_HANDLE +# define STATUS_INVALID_HANDLE (0xC0000008) +#endif + +#ifndef STATUS_OBJECT_TYPE_MISMATCH +# define STATUS_OBJECT_TYPE_MISMATCH (0xC0000024) +#endif + +#ifndef STATUS_INSUFFICIENT_RESOURCES +# define STATUS_INSUFFICIENT_RESOURCES (0xC000009A) +#endif + +#ifndef STATUS_PAGEFILE_QUOTA +# define STATUS_PAGEFILE_QUOTA (0xC0000007) +#endif + +#ifndef STATUS_COMMITMENT_LIMIT +# define STATUS_COMMITMENT_LIMIT (0xC000012D) +#endif + +#ifndef STATUS_WORKING_SET_QUOTA +# define STATUS_WORKING_SET_QUOTA (0xC00000A1) +#endif + +#ifndef STATUS_NO_MEMORY +# define STATUS_NO_MEMORY (0xC0000017) +#endif + +#ifndef STATUS_CONFLICTING_ADDRESSES +# define STATUS_CONFLICTING_ADDRESSES (0xC0000018) +#endif + +#ifndef STATUS_QUOTA_EXCEEDED +# define STATUS_QUOTA_EXCEEDED (0xC0000044) +#endif + +#ifndef STATUS_TOO_MANY_PAGING_FILES +# define STATUS_TOO_MANY_PAGING_FILES (0xC0000097) +#endif + +#ifndef STATUS_REMOTE_RESOURCES +# define STATUS_REMOTE_RESOURCES (0xC000013D) +#endif + +#ifndef STATUS_TOO_MANY_ADDRESSES +# define STATUS_TOO_MANY_ADDRESSES (0xC0000209) +#endif + +#ifndef STATUS_SHARING_VIOLATION +# define STATUS_SHARING_VIOLATION (0xC0000043) +#endif + +#ifndef STATUS_HOPLIMIT_EXCEEDED +# define STATUS_HOPLIMIT_EXCEEDED (0xC000A012) +#endif + +#ifndef STATUS_ADDRESS_ALREADY_EXISTS +# define STATUS_ADDRESS_ALREADY_EXISTS (0xC000020A) +#endif + +#ifndef STATUS_LINK_TIMEOUT +# define STATUS_LINK_TIMEOUT (0xC000013F) +#endif + +#ifndef STATUS_IO_TIMEOUT +# define STATUS_IO_TIMEOUT (0xC00000B5) +#endif + +#ifndef STATUS_TIMEOUT +# define STATUS_TIMEOUT (0x00000102) +#endif + +#ifndef STATUS_GRACEFUL_DISCONNECT +# define STATUS_GRACEFUL_DISCONNECT (0xC0000237) +#endif + +#ifndef STATUS_REMOTE_DISCONNECT +# define STATUS_REMOTE_DISCONNECT (0xC000013C) +#endif + +#ifndef STATUS_CONNECTION_RESET +# define STATUS_CONNECTION_RESET (0xC000020D) +#endif + +#ifndef STATUS_LINK_FAILED +# define STATUS_LINK_FAILED (0xC000013E) +#endif + +#ifndef STATUS_CONNECTION_DISCONNECTED +# define STATUS_CONNECTION_DISCONNECTED (0xC000020C) +#endif + +#ifndef STATUS_PORT_UNREACHABLE +# define STATUS_PORT_UNREACHABLE (0xC000023F) +#endif + +#ifndef STATUS_HOPLIMIT_EXCEEDED +# define STATUS_HOPLIMIT_EXCEEDED (0xC000A012) +#endif + +#ifndef STATUS_LOCAL_DISCONNECT +# define STATUS_LOCAL_DISCONNECT (0xC000013B) +#endif + +#ifndef STATUS_FLTRSACTION_ABORTED +//# define STATUS_FLTRSACTION_ABORTED (0x00000000) +#endif + +#ifndef STATUS_CONNECTION_ABORTED +# define STATUS_CONNECTION_ABORTED (0xC0000241) +#endif + +#ifndef STATUS_BAD_NETWORK_PATH +# define STATUS_BAD_NETWORK_PATH (0xC00000BE) +#endif + +#ifndef STATUS_NETWORK_UNREACHABLE +# define STATUS_NETWORK_UNREACHABLE (0xC000023C) +#endif + +#ifndef STATUS_PROTOCOL_UNREACHABLE +# define STATUS_PROTOCOL_UNREACHABLE (0xC000023E) +#endif + +#ifndef STATUS_HOST_UNREACHABLE +# define STATUS_HOST_UNREACHABLE (0xC000023D) +#endif + +#ifndef STATUS_CANCELLED +# define STATUS_CANCELLED (0xC0000120) +#endif + +#ifndef STATUS_REQUEST_ABORTED +# define STATUS_REQUEST_ABORTED (0xC0000240) +#endif + +#ifndef STATUS_BUFFER_OVERFLOW +# define STATUS_BUFFER_OVERFLOW (0x80000005) +#endif + +#ifndef STATUS_INVALID_BUFFER_SIZE +# define STATUS_INVALID_BUFFER_SIZE (0xC0000206) +#endif + +#ifndef STATUS_BUFFER_TOO_SMALL +# define STATUS_BUFFER_TOO_SMALL (0xC0000023) +#endif + +#ifndef STATUS_ACCESS_VIOLATION +# define STATUS_ACCESS_VIOLATION (0xC0000005) +#endif + +#ifndef STATUS_DEVICE_NOT_READY +# define STATUS_DEVICE_NOT_READY (0xC00000A3) +#endif + +#ifndef STATUS_REQUEST_NOT_ACCEPTED +# define STATUS_REQUEST_NOT_ACCEPTED (0xC00000D0) +#endif + +#ifndef STATUS_INVALID_NETWORK_RESPONSE +# define STATUS_INVALID_NETWORK_RESPONSE (0xC00000C3) +#endif + +#ifndef STATUS_NETWORK_BUSY +# define STATUS_NETWORK_BUSY (0xC00000BF) +#endif + +#ifndef STATUS_NO_SUCH_DEVICE +# define STATUS_NO_SUCH_DEVICE (0xC000000E) +#endif + +#ifndef STATUS_NO_SUCH_FILE +# define STATUS_NO_SUCH_FILE (0xC000000F) +#endif + +#ifndef STATUS_OBJECT_PATH_NOT_FOUND +# define STATUS_OBJECT_PATH_NOT_FOUND (0xC000003A) +#endif + +#ifndef STATUS_OBJECT_NAME_NOT_FOUND +# define STATUS_OBJECT_NAME_NOT_FOUND (0xC0000034) +#endif + +#ifndef STATUS_UNEXPECTED_NETWORK_ERROR +# define STATUS_UNEXPECTED_NETWORK_ERROR (0xC00000C4) +#endif + +#ifndef STATUS_INVALID_CONNECTION +# define STATUS_INVALID_CONNECTION (0xC0000140) +#endif + +#ifndef STATUS_REMOTE_NOT_LISTENING +# define STATUS_REMOTE_NOT_LISTENING (0xC00000BC) +#endif + +#ifndef STATUS_CONNECTION_REFUSED +# define STATUS_CONNECTION_REFUSED (0xC0000236) +#endif + +#ifndef STATUS_PIPE_DISCONNECTED +# define STATUS_PIPE_DISCONNECTED (0xC00000B0) +#endif + +#ifndef STATUS_INVALID_ADDRESS +# define STATUS_INVALID_ADDRESS (0xC0000141) +#endif + +#ifndef STATUS_INVALID_ADDRESS_COMPONENT +# define STATUS_INVALID_ADDRESS_COMPONENT (0xC0000207) +#endif + +#ifndef STATUS_NOT_SUPPORTED +# define STATUS_NOT_SUPPORTED (0xC00000BB) +#endif + +#ifndef STATUS_NOT_IMPLEMENTED +# define STATUS_NOT_IMPLEMENTED (0xC0000002) +#endif + +#ifndef STATUS_ACCESS_DENIED +# define STATUS_ACCESS_DENIED (0xC0000022) +#endif + +#ifndef STATUS_INVALID_DEVICE_STATE +# define STATUS_INVALID_DEVICE_STATE (0xC0000184) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +static __tb_inline__ tb_size_t tb_ntstatus_to_winerror(tb_size_t status) +{ + switch (status) + { + case STATUS_SUCCESS: + return ERROR_SUCCESS; + + case STATUS_PENDING: + return ERROR_IO_PENDING; + + case STATUS_END_OF_FILE: + return ERROR_HANDLE_EOF; + + case STATUS_INVALID_HANDLE: + case STATUS_OBJECT_TYPE_MISMATCH: + return WSAENOTSOCK; + + case STATUS_INSUFFICIENT_RESOURCES: + case STATUS_PAGEFILE_QUOTA: + case STATUS_COMMITMENT_LIMIT: + case STATUS_WORKING_SET_QUOTA: + case STATUS_NO_MEMORY: + case STATUS_CONFLICTING_ADDRESSES: + case STATUS_QUOTA_EXCEEDED: + case STATUS_TOO_MANY_PAGING_FILES: + case STATUS_REMOTE_RESOURCES: + case STATUS_TOO_MANY_ADDRESSES: + return WSAENOBUFS; + + case STATUS_SHARING_VIOLATION: + case STATUS_ADDRESS_ALREADY_EXISTS: + return WSAEADDRINUSE; + + case STATUS_LINK_TIMEOUT: + case STATUS_IO_TIMEOUT: + case STATUS_TIMEOUT: + return WSAETIMEDOUT; + + case STATUS_GRACEFUL_DISCONNECT: + return WSAEDISCON; + + case STATUS_REMOTE_DISCONNECT: + case STATUS_CONNECTION_RESET: + case STATUS_LINK_FAILED: + case STATUS_CONNECTION_DISCONNECTED: + case STATUS_PORT_UNREACHABLE: + case STATUS_HOPLIMIT_EXCEEDED: + case STATUS_INVALID_DEVICE_STATE: + return WSAECONNRESET; + + case STATUS_LOCAL_DISCONNECT: +// case STATUS_FLTRSACTION_ABORTED: + case STATUS_CONNECTION_ABORTED: + return WSAECONNABORTED; + + case STATUS_BAD_NETWORK_PATH: + case STATUS_NETWORK_UNREACHABLE: + case STATUS_PROTOCOL_UNREACHABLE: + return WSAENETUNREACH; + + case STATUS_HOST_UNREACHABLE: + return WSAEHOSTUNREACH; + + case STATUS_CANCELLED: + case STATUS_REQUEST_ABORTED: + return WSAEINTR; + + case STATUS_BUFFER_OVERFLOW: + case STATUS_INVALID_BUFFER_SIZE: + return WSAEMSGSIZE; + + case STATUS_BUFFER_TOO_SMALL: + case STATUS_ACCESS_VIOLATION: + return WSAEFAULT; + + case STATUS_DEVICE_NOT_READY: + case STATUS_REQUEST_NOT_ACCEPTED: + return WSAEWOULDBLOCK; + + case STATUS_INVALID_NETWORK_RESPONSE: + case STATUS_NETWORK_BUSY: + case STATUS_NO_SUCH_DEVICE: + case STATUS_NO_SUCH_FILE: + case STATUS_OBJECT_PATH_NOT_FOUND: + case STATUS_OBJECT_NAME_NOT_FOUND: + case STATUS_UNEXPECTED_NETWORK_ERROR: + return WSAENETDOWN; + + case STATUS_INVALID_CONNECTION: + return WSAENOTCONN; + + case STATUS_REMOTE_NOT_LISTENING: + case STATUS_CONNECTION_REFUSED: + return WSAECONNREFUSED; + + case STATUS_PIPE_DISCONNECTED: + return WSAESHUTDOWN; + + case STATUS_INVALID_ADDRESS: + case STATUS_INVALID_ADDRESS_COMPONENT: + return WSAEADDRNOTAVAIL; + + case STATUS_NOT_SUPPORTED: + case STATUS_NOT_IMPLEMENTED: + return WSAEOPNOTSUPP; + + case STATUS_ACCESS_DENIED: + return WSAEACCES; + + default: + if ( (status & (FACILITY_NTWIN32 << 16)) == (FACILITY_NTWIN32 << 16) + && (status & (ERROR_SEVERITY_ERROR | ERROR_SEVERITY_WARNING))) + { + return (tb_size_t) (status & 0xffff); + } + else + { + tb_trace_e("ntstatus: unknown: %lx", status); + return WSAEINVAL; + } + } +} + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/prefix.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/prefix.h index 5c1372b10..fbb32b474 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/prefix.h @@ -64,3 +64,69 @@ static __tb_inline__ tb_wchar_t const* tb_path_absolute_w(tb_char_t const* path, } #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PLATFORM_WINDOWS_PREFIX_H +#define TB_PLATFORM_WINDOWS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../path.h" +#include "../../libc/libc.h" +#include "../../utils/utils.h" +#include "../../network/ipaddr.h" +#include <winsock2.h> +#include <mswsock.h> +#include <windows.h> +#include <iphlpapi.h> +#include <ws2tcpip.h> + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +// FILETIME => tb_time_t +static __tb_inline__ tb_time_t tb_filetime_to_time(FILETIME ft) +{ + ULARGE_INTEGER ui = {{0}}; + ui.LowPart = ft.dwLowDateTime; + ui.HighPart = ft.dwHighDateTime; + return (tb_time_t)((LONGLONG)(ui.QuadPart - 116444736000000000ull) / 10000000ul); +} + +// the path full for wchar +static __tb_inline__ tb_wchar_t const* tb_path_absolute_w(tb_char_t const* path, tb_wchar_t* full, tb_size_t maxn) +{ + // the path full + tb_char_t full_a[TB_PATH_MAXN] = {0}; + if (!tb_path_absolute(path, full_a, TB_PATH_MAXN)) return tb_null; + + // atow + return tb_atow(full, full_a, maxn) != -1? full : tb_null; +} + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/socket_pool.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/socket_pool.h index 1042b36f7..8a4b04d06 100644 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/socket_pool.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/socket_pool.h @@ -72,3 +72,77 @@ tb_socket_ref_t tb_socket_pool_get(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file socket_pool.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_WINDOWS_SOCKET_POOL_H +#define TB_PLATFORM_WINDOWS_SOCKET_POOL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../socket.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init socket pool + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_pool_init(tb_noarg_t); + +/*! exit socket pool + * + */ +tb_void_t tb_socket_pool_exit(tb_noarg_t); + +/*! put socket to the socket pool + * + * @param sock the tcp socket + * + * @return tb_true or tb_false + */ +tb_bool_t tb_socket_pool_put(tb_socket_ref_t sock); + +/*! get socket from the socket pool + * + * @return the tcp socket + */ +tb_socket_ref_t tb_socket_pool_get(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix.h b/core/pkg/tbox.pkg/inc/tbox/prefix.h index 9c9633e09..ffeef398f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PREFIX_H +#define TB_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix/prefix.h" + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/abort.h b/core/pkg/tbox.pkg/inc/tbox/prefix/abort.h index 639ff71a7..3b09792fa 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/abort.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/abort.h @@ -60,3 +60,65 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file abort.h + * + */ +#ifndef TB_PREFIX_ABORT_H +#define TB_PREFIX_ABORT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "trace.h" +#include "assembler.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/* abort it, @note ud2 cannot be aborted immediately for multi-thread + * and it will be not catched for linux exception (ignore int3 signal) + */ +#if (defined(TB_ARCH_x86) || defined(TB_ARCH_x64)) && \ + (!defined(TB_CONFIG_EXCEPTION_ENABLE) || defined(TB_CONFIG_OS_WINDOWS)) +# if defined(TB_ASSEMBLER_IS_MASM) && !defined(TB_ARCH_x64) +//# define tb_abort_done() do { __tb_asm__ { ud2 } } while (0) +# define tb_abort_done() do { __tb_asm__ { int 3 } } while (0) +# elif defined(TB_ASSEMBLER_IS_GAS) +//# define tb_abort_done() do { __tb_asm__ __tb_volatile__ ("ud2"); } while (0) +# define tb_abort_done() do { __tb_asm__ __tb_volatile__ ("int3"); } while (0) +# endif +#endif + +#ifndef tb_abort_done +# define tb_abort_done() do { *((__tb_volatile__ tb_int_t*)0) = 0; } while (0) +#endif + +// abort +#define tb_abort() do { tb_trace_e("abort"); tb_abort_done(); } while(0) + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/arch.h b/core/pkg/tbox.pkg/inc/tbox/prefix/arch.h index c5000262b..c7bdf67ed 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/arch.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/arch.h @@ -226,3 +226,231 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file arch.h + * + */ +#ifndef TB_PREFIX_ARCH_H +#define TB_PREFIX_ARCH_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/* arch + * + * gcc builtin macros for gcc -dM -E - < /dev/null + * + * .e.g gcc -m64 -dM -E - < /dev/null | grep 64 + * .e.g gcc -m32 -dM -E - < /dev/null | grep 86 + * .e.g gcc -march=armv6 -dM -E - < /dev/null | grep ARM + */ +#if defined(__i386) \ + || defined(__i686) \ + || defined(__i386__) \ + || defined(__i686__) \ + || defined(_M_IX86) +# define TB_ARCH_x86 +# if defined(__i386) || defined(__i386__) +# define TB_ARCH_STRING "i386" +# elif defined(__i686) || defined(__i686__) +# define TB_ARCH_STRING "i686" +# elif defined(_M_IX86) +# if (_M_IX86 == 300) +# define TB_ARCH_STRING "i386" +# elif (_M_IX86 == 400) +# define TB_ARCH_STRING "i486" +# elif (_M_IX86 == 500 || _M_IX86 == 600) +# define TB_ARCH_STRING "Pentium" +# endif +# else +# define TB_ARCH_STRING "x86" +# endif +#elif defined(__x86_64) \ + || defined(__amd64__) \ + || defined(__amd64) \ + || defined(_M_IA64) \ + || defined(_M_X64) +# define TB_ARCH_x64 +# if defined(__x86_64) +# define TB_ARCH_STRING "x86_64" +# elif defined(__amd64__) || defined(__amd64) +# define TB_ARCH_STRING "amd64" +# else +# define TB_ARCH_STRING "x64" +# endif +#elif defined(__arm__) || defined(__arm64) || defined(__arm64__) || (defined(__aarch64__) && __aarch64__) +# define TB_ARCH_ARM +# if defined(__ARM64_ARCH_8__) +# define TB_ARCH_ARM64 +# define TB_ARCH_ARM_VERSION (8) +# define TB_ARCH_ARM_v8 +# define TB_ARCH_STRING "arm64" +# elif defined(__ARM_ARCH_7A__) +# define TB_ARCH_ARM_VERSION (7) +# define TB_ARCH_ARM_v7A +# define TB_ARCH_STRING "armv7a" +# elif defined(__ARM_ARCH_7__) +# define TB_ARCH_ARM_VERSION (7) +# define TB_ARCH_ARM_v7 +# define TB_ARCH_STRING "armv7" +# elif defined(__ARM_ARCH_6__) +# define TB_ARCH_ARM_VERSION (6) +# define TB_ARCH_ARM_v6 +# define TB_ARCH_STRING "armv6" +# elif defined(__ARM_ARCH_5TE__) +# define TB_ARCH_ARM_VERSION (5) +# define TB_ARCH_ARM_v5te +# define TB_ARCH_STRING "armv5te" +# elif defined(__ARM_ARCH_5__) +# define TB_ARCH_ARM_VERSION (5) +# define TB_ARCH_ARM_v5 +# define TB_ARCH_STRING "armv5" +# elif defined(__ARM_ARCH) +# define TB_ARCH_ARM_VERSION __ARM_ARCH +# if __ARM_ARCH >= 8 +# define TB_ARCH_ARM_v8 +# if defined(__arm64) || defined(__arm64__) +# define TB_ARCH_ARM64 +# define TB_ARCH_STRING "arm64" +# else +# define TB_ARCH_STRING "armv7s" +# endif +# elif __ARM_ARCH >= 7 +# define TB_ARCH_ARM_v7 +# define TB_ARCH_STRING "armv7" +# elif __ARM_ARCH >= 6 +# define TB_ARCH_ARM_v6 +# define TB_ARCH_STRING "armv6" +# else +# define TB_ARCH_ARM_v5 +# define TB_ARCH_STRING "armv5" +# endif +# elif defined(__aarch64__) && __aarch64__ +# define TB_ARCH_ARM_v8 +# define TB_ARCH_ARM64 +# define TB_ARCH_STRING "arm64-v8a" +# else +# error unknown arm arch version +# endif +# if !defined(TB_ARCH_ARM64) && (defined(__arm64) || defined(__arm64__)) +# define TB_ARCH_ARM64 +# ifndef TB_ARCH_STRING +# define TB_ARCH_STRING "arm64" +# endif +# endif +# ifndef TB_ARCH_STRING +# define TB_ARCH_STRING "arm" +# endif +# if defined(__thumb__) +# define TB_ARCH_ARM_THUMB +# define TB_ARCH_STRING_2 "_thumb" +# endif +# if defined(__ARM_NEON__) +# define TB_ARCH_ARM_NEON +# define TB_ARCH_STRING_3 "_neon" +# endif +#elif defined(mips) \ + || defined(_mips) \ + || defined(__mips__) +# define TB_ARCH_MIPS +# define TB_ARCH_STRING "mips" +#else +//# define TB_ARCH_SPARC +//# define TB_ARCH_PPC +//# define TB_ARCH_SH4 +# error unknown arch +# define TB_ARCH_STRING "unknown_arch" +#endif + +// sse +#if defined(TB_ARCH_x86) || defined(TB_ARCH_x64) +# if defined(__SSE__) +# define TB_ARCH_SSE +# define TB_ARCH_STRING_2 "_sse" +# endif +# if defined(__SSE2__) +# define TB_ARCH_SSE2 +# undef TB_ARCH_STRING_2 +# define TB_ARCH_STRING_2 "_sse2" +# endif +# if defined(__SSE3__) +# define TB_ARCH_SSE3 +# undef TB_ARCH_STRING_2 +# define TB_ARCH_STRING_2 "_sse3" +# endif +#endif + +// vfp +#if defined(__VFP_FP__) +# define TB_ARCH_VFP +# define TB_ARCH_STRING_4 "_vfp" +#endif + +// elf +#if defined(__ELF__) +# define TB_ARCH_ELF +# define TB_ARCH_STRING_5 "_elf" +#endif + +// mach +#if defined(__MACH__) +# define TB_ARCH_MACH +# define TB_ARCH_STRING_5 "_mach" +#endif + +#ifndef TB_ARCH_STRING_1 +# define TB_ARCH_STRING_1 "" +#endif + +#ifndef TB_ARCH_STRING_2 +# define TB_ARCH_STRING_2 "" +#endif + +#ifndef TB_ARCH_STRING_3 +# define TB_ARCH_STRING_3 "" +#endif + +#ifndef TB_ARCH_STRING_4 +# define TB_ARCH_STRING_4 "" +#endif + +#ifndef TB_ARCH_STRING_5 +# define TB_ARCH_STRING_5 "" +#endif + + +// version string +#ifndef TB_ARCH_VERSION_STRING +# define TB_ARCH_VERSION_STRING __tb_mstrcat6__(TB_ARCH_STRING, TB_ARCH_STRING_1, TB_ARCH_STRING_2, TB_ARCH_STRING_3, TB_ARCH_STRING_4, TB_ARCH_STRING_5) +#endif + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/arm/prefix.S b/core/pkg/tbox.pkg/inc/tbox/prefix/arm/prefix.S index dc1114f8f..6f890f43f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/arm/prefix.S +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/arm/prefix.S @@ -120,3 +120,125 @@ ELF .hidden \name \name: .endm +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.S + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_ARCH_ELF +# define ELF +# define EXTERN_ASM +#else +# define ELF @ +# define EXTERN_ASM _ +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * arch + */ + +#if defined(TB_ARCH_ARM_v8) + .arch armv8-a +#elif defined(TB_ARCH_ARM_v7A) + .arch armv7-a +#elif defined(TB_ARCH_ARM_v7) + .arch armv7 +#elif defined(TB_ARCH_ARM_v6) + .arch armv6 +#elif defined(TB_ARCH_ARM_v5te) + .arch armv5te +#elif defined(TB_ARCH_ARM_v5) + .arch armv5 +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * fpu + */ + +#if defined(TB_ARCH_ARM_NEON) && !defined(TB_ARCH_ARM64) + .fpu neon +#elif defined(TB_ARCH_VFP) + .fpu vfp +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * syntax + */ +#if defined(TB_ARCH_ARM) && !defined(TB_ARCH_ARM64) + .syntax unified +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * eabi + */ +#if defined(TB_ARCH_ELF) && defined(TB_ARCH_ARM) && !defined(TB_ARCH_ARM64) + .eabi_attribute 25, 1 +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! function + * + * @code + function func_xxxx, export=1 + ... + endfunc + @endcode + */ +.macro function name, export=0 + .macro endfunc +ELF .size \name, . - \name + .purgem endfunc + .endm + + .text + .align TB_CPU_BITBYTE + .if \export + .global EXTERN_ASM\name +ELF .type EXTERN_ASM\name, %function +ELF .hidden EXTERN_ASM\name +EXTERN_ASM\name: + .else +ELF .type \name, %function +ELF .hidden \name +\name: + .endif +.endm + +/*! label + * + * @code + label name + ... + @endcode + */ +.macro label name + .align TB_CPU_BITBYTE +\name: +.endm + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/arm64/prefix.S b/core/pkg/tbox.pkg/inc/tbox/prefix/arm64/prefix.S index b2dd79425..f15d069ca 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/arm64/prefix.S +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/arm64/prefix.S @@ -120,3 +120,125 @@ ELF .hidden \name \name: .endm +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.S + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#ifdef TB_ARCH_ELF +# define ELF +# define EXTERN_ASM +#else +# define ELF # +# define EXTERN_ASM _ +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * arch + */ + +#if defined(TB_ARCH_ARM_v8) + .arch armv8-a +#elif defined(TB_ARCH_ARM_v7A) + .arch armv7-a +#elif defined(TB_ARCH_ARM_v7) + .arch armv7 +#elif defined(TB_ARCH_ARM_v6) + .arch armv6 +#elif defined(TB_ARCH_ARM_v5te) + .arch armv5te +#elif defined(TB_ARCH_ARM_v5) + .arch armv5 +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * fpu + */ + +#if defined(TB_ARCH_ARM_NEON) && !defined(TB_ARCH_ARM64) + .fpu neon +#elif defined(TB_ARCH_VFP) + .fpu vfp +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * syntax + */ +#if defined(TB_ARCH_ARM) && !defined(TB_ARCH_ARM64) + .syntax unified +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * eabi + */ +#if defined(TB_ARCH_ELF) && defined(TB_ARCH_ARM) && !defined(TB_ARCH_ARM64) + .eabi_attribute 25, 1 +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! function + * + * @code + function func_xxxx, export=1 + ... + endfunc + @endcode + */ +.macro function name, export=0 + .macro endfunc +ELF .size \name, . - \name + .purgem endfunc + .endm + + .text + .align TB_CPU_BITBYTE + .if \export + .global EXTERN_ASM\name +ELF .type EXTERN_ASM\name, %function +ELF .hidden EXTERN_ASM\name +EXTERN_ASM\name: + .else +ELF .type \name, %function +ELF .hidden \name +\name: + .endif +.endm + +/*! label + * + * @code + label name + ... + @endcode + */ +.macro label name + .align TB_CPU_BITBYTE +\name: +.endm + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/assembler.h b/core/pkg/tbox.pkg/inc/tbox/prefix/assembler.h index 9e5a86f06..de453050a 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/assembler.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/assembler.h @@ -46,3 +46,51 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file assembler.h + * + */ +#ifndef TB_PREFIX_ASSEMBLER_H +#define TB_PREFIX_ASSEMBLER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if defined(TB_COMPILER_IS_MSVC) +# define TB_ASSEMBLER_IS_MASM +#elif defined(TB_COMPILER_IS_GCC) \ + || defined(TB_COMPILER_IS_CLANG) \ + || defined(TB_COMPILER_IS_INTEL) +# define TB_ASSEMBLER_IS_GAS +#endif + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/assert.h b/core/pkg/tbox.pkg/inc/tbox/prefix/assert.h index f8e1340d8..6beedd436 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/assert.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/assert.h @@ -332,3 +332,337 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file assert.h + * + */ +#ifndef TB_PREFIX_ASSERT_H +#define TB_PREFIX_ASSERT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "trace.h" +#include "abort.h" +#include "check.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the assert backtrace prefix +#define TB_ASSERT_BACKTRACE_PREFIX " " + +// the assert backtrace nframe +#define TB_ASSERT_BACKTRACE_NFRAME (16) + +// the assert backtrace dump +#define tb_assert_backtrace_dump() tb_backtrace_dump(TB_ASSERT_BACKTRACE_PREFIX, tb_null, TB_ASSERT_BACKTRACE_NFRAME) + +// assert +#ifdef __tb_debug__ +# if defined(TB_COMPILER_IS_GCC) +# define tb_assertf(x, fmt, arg...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, ##arg); tb_assert_backtrace_dump(); tb_trace_sync(); tb_abort(); } } while(0) +# define tb_assertf_and_check_abort(x, fmt, arg...) tb_assertf(x, fmt, ##arg) +# define tb_assertf_and_check_return(x, fmt, arg...) tb_assertf(x, fmt, ##arg) +# define tb_assertf_and_check_return_val(x, v, fmt, arg...) tb_assertf(x, fmt, ##arg) +# define tb_assertf_and_check_goto(x, b, fmt, arg...) tb_assertf(x, fmt, ##arg) +# define tb_assertf_and_check_break(x, fmt, arg...) tb_assertf(x, fmt, ##arg) +# define tb_assertf_and_check_continue(x, fmt, arg...) tb_assertf(x, fmt, ##arg) +# elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_assertf(x, fmt, ...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, __VA_ARGS__); tb_assert_backtrace_dump(); tb_trace_sync(); tb_abort(); } } while(0) +# define tb_assertf_and_check_abort(x, fmt, ...) tb_assertf(x, fmt, __VA_ARGS__) +# define tb_assertf_and_check_return(x, fmt, ...) tb_assertf(x, fmt, __VA_ARGS__) +# define tb_assertf_and_check_return_val(x, v, fmt, ...) tb_assertf(x, fmt, __VA_ARGS__) +# define tb_assertf_and_check_goto(x, b, fmt, ...) tb_assertf(x, fmt, __VA_ARGS__) +# define tb_assertf_and_check_break(x, fmt, ...) tb_assertf(x, fmt, __VA_ARGS__) +# define tb_assertf_and_check_continue(x, fmt, ...) tb_assertf(x, fmt, __VA_ARGS__) +# else +# define tb_assertf +# define tb_assertf_and_check_abort +# define tb_assertf_and_check_return +# define tb_assertf_and_check_return_val +# define tb_assertf_and_check_goto +# define tb_assertf_and_check_break +# define tb_assertf_and_check_continue +# endif +#else +# if defined(TB_COMPILER_IS_GCC) +# define tb_assertf(x, fmt, arg...) +# define tb_assertf_and_check_abort(x, fmt, arg...) tb_check_abort(x) +# define tb_assertf_and_check_return(x, fmt, arg...) tb_check_return(x) +# define tb_assertf_and_check_return_val(x, v, fmt, arg...) tb_check_return_val(x, v) +# define tb_assertf_and_check_goto(x, b, fmt, arg...) tb_check_goto(x, b) +# define tb_assertf_and_check_break(x, fmt, arg...) tb_check_break(x) +# define tb_assertf_and_check_continue(x, fmt, arg...) tb_check_continue(x) +# elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_assertf(x, fmt, ...) +# define tb_assertf_and_check_abort(x, fmt, ...) tb_check_abort(x) +# define tb_assertf_and_check_return(x, fmt, ...) tb_check_return(x) +# define tb_assertf_and_check_return_val(x, v, fmt, ...) tb_check_return_val(x, v) +# define tb_assertf_and_check_goto(x, b, fmt, ...) tb_check_goto(x, b) +# define tb_assertf_and_check_break(x, fmt, ...) tb_check_break(x) +# define tb_assertf_and_check_continue(x, fmt, ...) tb_check_continue(x) +# else +# define tb_assertf +# define tb_assertf_and_check_abort +# define tb_assertf_and_check_return +# define tb_assertf_and_check_return_val +# define tb_assertf_and_check_goto +# define tb_assertf_and_check_break +# define tb_assertf_and_check_continue +# endif +#endif + +#ifdef __tb_debug__ +# define tb_assert(x) tb_assert_impl(!!(x), #x __tb_debug_vals__) +# define tb_assert_and_check_abort(x) tb_assert(x) +# define tb_assert_and_check_return(x) tb_assert(x) +# define tb_assert_and_check_return_val(x, v) tb_assert(x) +# define tb_assert_and_check_goto(x, b) tb_assert(x) +# define tb_assert_and_check_break(x) tb_assert(x) +# define tb_assert_and_check_continue(x) tb_assert(x) +# define tb_assert_and_check_break_state(x, s, v) tb_assert(x) +#else +# define tb_assert(x) tb_assert_empty_impl() +# define tb_assert_and_check_abort(x) tb_check_abort(x) +# define tb_assert_and_check_return(x) tb_check_return(x) +# define tb_assert_and_check_return_val(x, v) tb_check_return_val(x, v) +# define tb_assert_and_check_goto(x, b) tb_check_goto(x, b) +# define tb_assert_and_check_break(x) tb_check_break(x) +# define tb_assert_and_check_continue(x) tb_check_continue(x) +# define tb_assert_and_check_break_state(x, s, v) tb_check_break_state(x, s, v) +#endif + +// assert and pass code, not abort it +#ifdef __tb_debug__ +# if defined(TB_COMPILER_IS_GCC) +# define tb_assertf_pass_return(x, fmt, arg...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, ##arg); tb_assert_backtrace_dump(); tb_trace_sync(); return ; } } while(0) +# define tb_assertf_pass_return_val(x, v, fmt, arg...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, ##arg); tb_assert_backtrace_dump(); tb_trace_sync(); return (v); } } while(0) +# define tb_assertf_pass_goto(x, b, fmt, arg...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, ##arg); tb_assert_backtrace_dump(); tb_trace_sync(); goto b; } } while(0) +# define tb_assertf_pass_break(x, fmt, arg...) { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, ##arg); tb_assert_backtrace_dump(); tb_trace_sync(); break ; } } +# define tb_assertf_pass_continue(x, fmt, arg...) { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, ##arg); tb_assert_backtrace_dump(); tb_trace_sync(); continue ; } } +# define tb_assertf_pass_and_check_abort(x, fmt, arg...) tb_assertf_pass_abort(x, fmt, ##arg) +# define tb_assertf_pass_and_check_return(x, fmt, arg...) tb_assertf_pass_return(x, fmt, ##arg) +# define tb_assertf_pass_and_check_return_val(x, v, fmt, arg...) tb_assertf_pass_return_val(x, v, fmt, ##arg) +# define tb_assertf_pass_and_check_goto(x, b, fmt, arg...) tb_assertf_pass_goto(x, b, fmt, ##arg) +# define tb_assertf_pass_and_check_break(x, fmt, arg...) tb_assertf_pass_break(x, fmt, ##arg) +# define tb_assertf_pass_and_check_continue(x, fmt, arg...) tb_assertf_pass_continue(x, fmt, ##arg) +# elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_assertf_pass_return(x, fmt, ...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, __VA_ARGS__); tb_assert_backtrace_dump(); tb_trace_sync(); return ; } } while(0) +# define tb_assertf_pass_return_val(x, v, fmt, ...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, __VA_ARGS__); tb_assert_backtrace_dump(); tb_trace_sync(); return (v); } } while(0) +# define tb_assertf_pass_goto(x, b, fmt, ...) do { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, __VA_ARGS__); tb_assert_backtrace_dump(); tb_trace_sync(); goto b; } } while(0) +# define tb_assertf_pass_break(x, fmt, ...) { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, __VA_ARGS__); tb_assert_backtrace_dump(); tb_trace_sync(); break ; } } +# define tb_assertf_pass_continue(x, fmt, ...) { if (!(x)) {tb_trace_a("expr[%s]: " fmt, #x, __VA_ARGS__); tb_assert_backtrace_dump(); tb_trace_sync(); continue ; } } +# define tb_assertf_pass_and_check_abort(x, fmt, ...) tb_assertf_pass_abort(x, fmt, __VA_ARGS__) +# define tb_assertf_pass_and_check_return(x, fmt, ...) tb_assertf_pass_return(x, fmt, __VA_ARGS__) +# define tb_assertf_pass_and_check_return_val(x, v, fmt, ...) tb_assertf_pass_return_val(x, v, fmt, __VA_ARGS__) +# define tb_assertf_pass_and_check_goto(x, b, fmt, ...) tb_assertf_pass_goto(x, b, fmt, __VA_ARGS__) +# define tb_assertf_pass_and_check_break(x, fmt, ...) tb_assertf_pass_break(x, fmt, __VA_ARGS__) +# define tb_assertf_pass_and_check_continue(x, fmt, ...) tb_assertf_pass_continue(x, fmt, __VA_ARGS__) +# else +# define tb_assertf_pass_return +# define tb_assertf_pass_return_val +# define tb_assertf_pass_goto +# define tb_assertf_pass_break +# define tb_assertf_pass_continue +# define tb_assertf_pass_and_check_abort +# define tb_assertf_pass_and_check_return +# define tb_assertf_pass_and_check_return_val +# define tb_assertf_pass_and_check_goto +# define tb_assertf_pass_and_check_break +# define tb_assertf_pass_and_check_continue +# endif +#else +# if defined(TB_COMPILER_IS_GCC) +# define tb_assertf_pass_return(x, fmt, arg...) +# define tb_assertf_pass_return_val(x, v, fmt, arg...) +# define tb_assertf_pass_goto(x, b, fmt, arg...) +# define tb_assertf_pass_break(x, fmt, arg...) +# define tb_assertf_pass_continue(x, fmt, arg...) +# define tb_assertf_pass_and_check_abort(x, fmt, arg...) tb_check_abort(x) +# define tb_assertf_pass_and_check_return(x, fmt, arg...) tb_check_return(x) +# define tb_assertf_pass_and_check_return_val(x, v, fmt, arg...) tb_check_return_val(x, v) +# define tb_assertf_pass_and_check_goto(x, b, fmt, arg...) tb_check_goto(x, b) +# define tb_assertf_pass_and_check_break(x, fmt, arg...) tb_check_break(x) +# define tb_assertf_pass_and_check_continue(x, fmt, arg...) tb_check_continue(x) +# elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_assertf_pass_return(x, fmt, ...) +# define tb_assertf_pass_return_val(x, v, fmt, ...) +# define tb_assertf_pass_goto(x, b, fmt, ...) +# define tb_assertf_pass_break(x, fmt, ...) +# define tb_assertf_pass_continue(x, fmt, ...) +# define tb_assertf_pass_and_check_abort(x, fmt, ...) tb_check_abort(x) +# define tb_assertf_pass_and_check_return(x, fmt, ...) tb_check_return(x) +# define tb_assertf_pass_and_check_return_val(x, v, fmt, ...) tb_check_return_val(x, v) +# define tb_assertf_pass_and_check_goto(x, b, fmt, ...) tb_check_goto(x, b) +# define tb_assertf_pass_and_check_break(x, fmt, ...) tb_check_break(x) +# define tb_assertf_pass_and_check_continue(x, fmt, ...) tb_check_continue(x) +# else +# define tb_assertf_pass_return +# define tb_assertf_pass_return_val +# define tb_assertf_pass_goto +# define tb_assertf_pass_break +# define tb_assertf_pass_continue +# define tb_assertf_pass_and_check_abort +# define tb_assertf_pass_and_check_return +# define tb_assertf_pass_and_check_return_val +# define tb_assertf_pass_and_check_goto +# define tb_assertf_pass_and_check_break +# define tb_assertf_pass_and_check_continue +# endif +#endif + +#ifdef __tb_debug__ +# define tb_assert_pass_return(x) do { if (!(x)) {tb_trace_a("expr[%s]", #x); tb_assert_backtrace_dump(); tb_trace_sync(); return ; } } while(0) +# define tb_assert_pass_return_val(x, v) do { if (!(x)) {tb_trace_a("expr[%s]", #x); tb_assert_backtrace_dump(); tb_trace_sync(); return (v); } } while(0) +# define tb_assert_pass_goto(x, b) do { if (!(x)) {tb_trace_a("expr[%s]", #x); tb_assert_backtrace_dump(); tb_trace_sync(); goto b; } } while(0) +# define tb_assert_pass_break(x) { if (!(x)) {tb_trace_a("expr[%s]", #x); tb_assert_backtrace_dump(); tb_trace_sync(); break ; } } +# define tb_assert_pass_continue(x) { if (!(x)) {tb_trace_a("expr[%s]", #x); tb_assert_backtrace_dump(); tb_trace_sync(); continue ; } } +# define tb_assert_pass_break_state(x, s, v) { if (!(x)) { (s) = (v); tb_trace_a("expr[%s]", #x); tb_assert_backtrace_dump(); tb_trace_sync(); break ; } } + +# define tb_assert_pass_and_check_abort(x) tb_assert_pass_abort(x) +# define tb_assert_pass_and_check_return(x) tb_assert_pass_return(x) +# define tb_assert_pass_and_check_return_val(x, v) tb_assert_pass_return_val(x, v) +# define tb_assert_pass_and_check_goto(x, b) tb_assert_pass_goto(x, b) +# define tb_assert_pass_and_check_break(x) tb_assert_pass_break(x) +# define tb_assert_pass_and_check_continue(x) tb_assert_pass_continue(x) +# define tb_assert_pass_and_check_break_state(x, s, v) tb_assert_pass_break_state(x, s, v) +#else +# define tb_assert_pass_return(x) +# define tb_assert_pass_return_val(x, v) +# define tb_assert_pass_goto(x, b) +# define tb_assert_pass_break(x) +# define tb_assert_pass_continue(x) +# define tb_assert_pass_break_state(x, s, v) + +# define tb_assert_pass_and_check_abort(x) tb_check_abort(x) +# define tb_assert_pass_and_check_return(x) tb_check_return(x) +# define tb_assert_pass_and_check_return_val(x, v) tb_check_return_val(x, v) +# define tb_assert_pass_and_check_goto(x, b) tb_check_goto(x, b) +# define tb_assert_pass_and_check_break(x) tb_check_break(x) +# define tb_assert_pass_and_check_continue(x) tb_check_continue(x) +# define tb_assert_pass_and_check_break_state(x, s, v) tb_check_break_state(x, s, v) +#endif + +/// assert: noimpl +#define tb_assert_noimpl() tb_assertf(0, "noimpl") + +/*! the static assert + * + * @code + * + * tb_assert_static(sizeof(tb_uint32_t) == 4); + * + * @endcode + */ +#if __tb_has_feature__(c_static_assert) +# define tb_assert_static(x) _Static_assert(x, "") +#elif defined(TB_COMPILER_IS_GCC) && TB_COMPILER_VERSION_BE(4, 6) +# define tb_assert_static(x) _Static_assert(x, "") +#else +# define tb_assert_static(x) do { typedef int __tb_static_assert__[(x)? 1 : -1]; __tb_volatile__ __tb_static_assert__ __a; tb_used_ptr((tb_cpointer_t)(tb_size_t)__a); } while(0) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * declaration + */ + +/*! dump backtrace + * + * @param prefix the prefix tag + * @pragm frames the frame list + * @param nframe the frame count + */ +tb_void_t tb_backtrace_dump(tb_char_t const* prefix, tb_pointer_t* frames, tb_size_t nframe); + +/*! this variable have been used + * + * @param variable the variable + */ +tb_void_t tb_used_ptr(tb_cpointer_t variable); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * inlines + */ + +#ifdef __tb_debug__ +/* the assert implementation + * + * @code + * tb_size_t v = (tb_assert(x), value); + * tb_assert(v); + * @endcode + * + * @param x the boolean value of the expression + * @param expr the expression string + * + * @return the boolean value of the expression + */ +static __tb_inline__ tb_bool_t tb_assert_impl(tb_bool_t x, tb_char_t const* expr __tb_debug_decl__) +{ + // failed? + if (!x) + { + // trace + tb_trace_a("expr[%s] at %s(): %d, %s", expr __tb_debug_args__); + + // dump backtrace + tb_assert_backtrace_dump(); + + // sync trace + tb_trace_sync(); + + // abort it + tb_abort(); + } + + // ok? + return x; +} +#else +/* the assert empty implementation + * + * @return only tb_false + */ +static __tb_inline__ tb_bool_t tb_assert_empty_impl(tb_noarg_t) +{ + return tb_false; +} +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/cache.h b/core/pkg/tbox.pkg/inc/tbox/prefix/cache.h index 9b8128942..d7a2c4b2d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/cache.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/cache.h @@ -79,3 +79,84 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file cache.h + * + */ +#ifndef TB_PREFIX_CACHE_H +#define TB_PREFIX_CACHE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the cpu L1 cache shift, default: (1 << 5) == 32 bytes +#ifndef TB_L1_CACHE_SHIFT +# define TB_L1_CACHE_SHIFT (5) +#endif + +// the cpu L1 cache bytes, default: 32 bytes +#ifndef TB_L1_CACHE_BYTES +# define TB_L1_CACHE_BYTES (1 << TB_L1_CACHE_SHIFT) +#endif + +// the smp cache bytes +#ifndef TB_SMP_CACHE_BYTES +# define TB_SMP_CACHE_BYTES TB_L1_CACHE_BYTES +#endif + +// the cacheline aligned keyword +#ifndef __tb_cacheline_aligned__ +# if defined(TB_COMPILER_IS_GCC) +# define __tb_cacheline_aligned__ __attribute__((__aligned__(TB_SMP_CACHE_BYTES))) +# elif defined(TB_COMPILER_IS_MSVC) +# if TB_SMP_CACHE_BYTES == 4 +# define __tb_cacheline_aligned__ __declspec(align(4)) +# elif TB_SMP_CACHE_BYTES == 8 +# define __tb_cacheline_aligned__ __declspec(align(8)) +# elif TB_SMP_CACHE_BYTES == 16 +# define __tb_cacheline_aligned__ __declspec(align(16)) +# elif TB_SMP_CACHE_BYTES == 32 +# define __tb_cacheline_aligned__ __declspec(align(32)) +# elif TB_SMP_CACHE_BYTES == 64 +# define __tb_cacheline_aligned__ __declspec(align(64)) +# elif TB_SMP_CACHE_BYTES == 128 +# define __tb_cacheline_aligned__ __declspec(align(128)) +# elif TB_SMP_CACHE_BYTES == 256 +# define __tb_cacheline_aligned__ __declspec(align(256)) +# else +# error unknown cacheline bytes +# endif +# else +# define __tb_cacheline_aligned__ +# endif +#endif + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/check.h b/core/pkg/tbox.pkg/inc/tbox/prefix/check.h index 420f10610..bc35ca71b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/check.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/check.h @@ -49,3 +49,54 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file check.h + * + */ +#ifndef TB_PREFIX_CHECK_H +#define TB_PREFIX_CHECK_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "trace.h" +#include "abort.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// check +#define tb_check_return(x) do { if (!(x)) return ; } while (0) +#define tb_check_return_val(x, v) do { if (!(x)) return (v); } while (0) +#define tb_check_goto(x, b) do { if (!(x)) goto b; } while (0) +#define tb_check_break(x) { if (!(x)) break ; } +#define tb_check_abort(x) do { if (!(x)) tb_abort(); } while (0) +#define tb_check_continue(x) { if (!(x)) continue ; } +#define tb_check_break_state(x, s, v) { if (!(x)) { (s) = (v); break ;} } + + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/compiler.h b/core/pkg/tbox.pkg/inc/tbox/prefix/compiler.h index 553d25ad4..b196864c7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/compiler.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/compiler.h @@ -312,3 +312,317 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file compiler.h + * + */ +#ifndef TB_PREFIX_COMPILER_H +#define TB_PREFIX_COMPILER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// some help macros for making version string +#ifndef __tb_mstring__ +# define __tb_mstring__(x) #x +#endif + +#ifndef __tb_mstring_ex__ +# define __tb_mstring_ex__(x) __tb_mstring__(x) +#endif + +#ifndef __tb_mstrcat4__ +# define __tb_mstrcat4__(a, b, c, d) a b c d +#endif + +// intel c++ +#if defined(__INTEL_COMPILER) +# define TB_COMPILER_IS_INTEL +# define TB_COMPILER_VERSION_BT(major, minor) (__INTEL_COMPILER > ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_BE(major, minor) (__INTEL_COMPILER >= ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_EQ(major, minor) (__INTEL_COMPILER == ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LT(major, minor) (__INTEL_COMPILER < ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LE(major, minor) (__INTEL_COMPILER <= ((major) * 100 + (minor))) +# define TB_COMPILER_STRING "intel c/c++" +# if (__INTEL_COMPILER == 600) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 6.0" +# elif (__INTEL_COMPILER == 700) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 7.0" +# elif (__INTEL_COMPILER == 800) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 8.0" +# elif (__INTEL_COMPILER == 900) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 9.0" +# elif (__INTEL_COMPILER == 1000) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 10.0" +# elif (__INTEL_COMPILER == 1100) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 11.0" +# elif (__INTEL_COMPILER == 1110) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 11.1" +# elif (__INTEL_COMPILER == 1200) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 12.0" +# elif (__INTEL_COMPILER == 1210) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 12.1" +# elif (__INTEL_COMPILER == 1300) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 13.0" +# elif (__INTEL_COMPILER == 1310) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 13.1" +# elif (__INTEL_COMPILER == 1400) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 14.0" +# elif (__INTEL_COMPILER == 1410) +# define TB_COMPILER_VERSION_STRING "intel c/c++ 14.1" +# elif (__INTEL_COMPILER == 9999) +# define TB_COMPILER_VERSION_STRING "intel c/c++ mainline" +# else +# error Unknown Intel C++ Compiler Version +# endif + +// borland c++ +#elif defined(__BORLANDC__) +# define TB_COMPILER_IS_BORLAND +# define TB_COMPILER_VERSION_BT(major, minor) (__BORLANDC__ > ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_BE(major, minor) (__BORLANDC__ >= ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_EQ(major, minor) (__BORLANDC__ == ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LT(major, minor) (__BORLANDC__ < ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LE(major, minor) (__BORLANDC__ <= ((major) * 100 + (minor))) +# define TB_COMPILER_STRING "borland c/c++" +# if 0 +# define TB_COMPILER_VERSION_STRING "borland c++ 4.52" +# elif 0 +# define TB_COMPILER_VERSION_STRING "borland c++ 5.5" +# elif (__BORLANDC__ == 0x0551) +# define TB_COMPILER_VERSION_STRING "borland c++ 5.51" +# elif (__BORLANDC__ == 0x0560) +# define TB_COMPILER_VERSION_STRING "borland c++ 5.6" +# elif (__BORLANDC__ == 0x0564) +# define TB_COMPILER_VERSION_STRING "borland c++ 5.6.4 (c++ builderx)" +# elif (__BORLANDC__ == 0x0582) +# define TB_COMPILER_VERSION_STRING "borland c++ 5.82 (turbo c++)" +# else +# error Unknown borland c++ Compiler Version +# endif + +// gnu c/c++ +#elif defined(__GNUC__) +# define TB_COMPILER_IS_GCC +# define TB_COMPILER_VERSION_BT(major, minor) ((__GNUC__ * 100 + __GNUC_MINOR__) > ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_BE(major, minor) ((__GNUC__ * 100 + __GNUC_MINOR__) >= ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_EQ(major, minor) ((__GNUC__ * 100 + __GNUC_MINOR__) == ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LT(major, minor) ((__GNUC__ * 100 + __GNUC_MINOR__) < ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LE(major, minor) ((__GNUC__ * 100 + __GNUC_MINOR__) <= ((major) * 100 + (minor))) +# define TB_COMPILER_STRING "gnu c/c++" +# if __GNUC__ == 2 +# if __GNUC_MINOR__ < 95 +# define TB_COMPILER_VERSION_STRING "gnu c/c++ <2.95" +# elif __GNUC_MINOR__ == 95 +# define TB_COMPILER_VERSION_STRING "gnu c/c++ 2.95" +# elif __GNUC_MINOR__ == 96 +# define TB_COMPILER_VERSION_STRING "gnu c/c++ 2.96" +# else +# define TB_COMPILER_VERSION_STRING "gnu c/c++ > 2.96 && < 3.0" +# endif +# elif __GNUC__ == 3 +# if __GNUC_MINOR__ == 2 +# define TB_COMPILER_VERSION_STRING "gnu c/c++ 3.2" +# elif __GNUC_MINOR__ == 3 +# define TB_COMPILER_VERSION_STRING "gnu c/c++ 3.3" +# elif __GNUC_MINOR__ == 4 +# define TB_COMPILER_VERSION_STRING "gnu c/c++ 3.4" +# else +# define TB_COMPILER_VERSION_STRING "gnu c/c++ > 3.4 && < 4.0" +# endif +# elif __GNUC__ >= 4 && defined(__GNUC_MINOR__) +# define TB_COMPILER_VERSION_STRING __tb_mstrcat4__("gnu c/c++ ", __tb_mstring_ex__(__GNUC__), ".", __tb_mstring_ex__(__GNUC_MINOR__)) +# else +# error Unknown gnu c/c++ Compiler Version +# endif + + // clang +# if defined(__clang__) +# define TB_COMPILER_IS_CLANG +# undef TB_COMPILER_STRING +# define TB_COMPILER_STRING "clang c/c++" +# if defined(__VERSION__) +# undef TB_COMPILER_VERSION_STRING +# define TB_COMPILER_VERSION_STRING __VERSION__ +# elif defined(__clang_version__) +# undef TB_COMPILER_VERSION_STRING +# define TB_COMPILER_VERSION_STRING __clang_version__ +# endif + // suppress warning: empty struct has size 0 in C, size 1 in C++ +# ifdef __cplusplus +# pragma clang diagnostic ignored "-Wextern-c-compat" +# endif + /* suppress warning (must be placed in the front) + * + * in old version clang: + * + * unknown warning group '-Wnullability-completeness', ignored [-Werror,-Wunknown-pragmas] + */ +# pragma clang diagnostic ignored "-Wunknown-pragmas" + + /* suppress warning + * + * signal.h + * pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) + */ +# pragma clang diagnostic ignored "-Wnullability-completeness" +# endif + +// watcom c/c++ +#elif defined(__WATCOMC__) +# define TB_COMPILER_IS_WATCOM +# define TB_COMPILER_VERSION_BT(major, minor) (__WATCOMC__ > ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_BE(major, minor) (__WATCOMC__ >= ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_EQ(major, minor) (__WATCOMC__ == ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LT(major, minor) (__WATCOMC__ < ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LE(major, minor) (__WATCOMC__ <= ((major) * 100 + (minor))) +# define TB_COMPILER_STRING "watcom c/c++" +# if (__WATCOMC__ == 1100) +# define TB_COMPILER_VERSION_STRING "watcom c/c++ 11.0" +# elif (__WATCOMC__ == 1200) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.0 (watcom 12.0)" +# elif (__WATCOMC__ == 1210) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.1 (watcom 12.1)" +# elif (__WATCOMC__ == 1220) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.2 (watcom 12.2)" +# elif (__WATCOMC__ == 1230) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.3 (watcom 12.3)" +# elif (__WATCOMC__ == 1240) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.4 (watcom 12.4)" +# elif (__WATCOMC__ == 1250) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.5" +# elif (__WATCOMC__ == 1260) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.6" +# elif (__WATCOMC__ == 1270) +# define TB_COMPILER_VERSION_STRING "open watcom c/c++ 1.7" +# else +# error Unknown watcom c/c++ Compiler Version +# endif + +// digital mars c/c++ +#elif defined(__DMC__) +# define TB_COMPILER_IS_DMC +# define TB_COMPILER_VERSION_BT(major, minor) (__DMC__ > ((major) * 256 + (minor))) +# define TB_COMPILER_VERSION_BE(major, minor) (__DMC__ >= ((major) * 256 + (minor))) +# define TB_COMPILER_VERSION_EQ(major, minor) (__DMC__ == ((major) * 256 + (minor))) +# define TB_COMPILER_VERSION_LT(major, minor) (__DMC__ < ((major) * 256 + (minor))) +# define TB_COMPILER_VERSION_LE(major, minor) (__DMC__ <= ((major) * 256 + (minor))) +# define TB_COMPILER_STRING "digital mars c/c++" +# if (__DMC__ < 0x0826) +# error Only versions 8.26 and later of the digital mars c/c++ compilers are supported by the EXTL libraries +# else +# if __DMC__ >= 0x0832 +# define TB_COMPILER_VERSION_STRING __DMC_VERSION_STRING__ +# elif (__DMC__ == 0x0826) +# define TB_COMPILER_VERSION_STRING "digital mars c/c++ 8.26" +# elif (__DMC__ == 0x0827) +# define TB_COMPILER_VERSION_STRING "digital mars c/c++ 8.27" +# elif (__DMC__ == 0x0828) +# define TB_COMPILER_VERSION_STRING "digital mars c/c++ 8.28" +# elif (__DMC__ == 0x0829) +# define TB_COMPILER_VERSION_STRING "digital mars c/c++ 8.29" +# elif (__DMC__ == 0x0830) +# define TB_COMPILER_VERSION_STRING "digital mars c/c++ 8.30" +# elif (__DMC__ == 0x0831) +# define TB_COMPILER_VERSION_STRING "digital mars c/c++ 8.31" +# else +# error Unknown digital mars c/c++ Compiler Version +# endif +# endif + +// codeplay vector c/c++ +#elif defined(__VECTORC) +# define TB_COMPILER_IS_VECTORC +# define TB_COMPILER_VERSION_BT(major, minor) (__VECTORC > (major)) +# define TB_COMPILER_VERSION_BE(major, minor) (__VECTORC >= (major)) +# define TB_COMPILER_VERSION_EQ(major, minor) (__VECTORC == (major)) +# define TB_COMPILER_VERSION_LT(major, minor) (__VECTORC < (major)) +# define TB_COMPILER_VERSION_LE(major, minor) (__VECTORC <= (major)) +# define TB_COMPILER_VERSION_STRING "codeplay vector c/c++" +# if (__VECTORC == 1) +# define TB_COMPILER_VERSION_STRING "codeplay vector c/c++" +# else +# error Unknown CodePlay VectorC C++ Compiler Version +# endif + +// visual c++ +#elif defined(_MSC_VER) +# define TB_COMPILER_IS_MSVC +# define TB_COMPILER_VERSION_BT(major, minor) (_MSC_VER > ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_BE(major, minor) (_MSC_VER >= ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_EQ(major, minor) (_MSC_VER == ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LT(major, minor) (_MSC_VER < ((major) * 100 + (minor))) +# define TB_COMPILER_VERSION_LE(major, minor) (_MSC_VER <= ((major) * 100 + (minor))) +# define TB_COMPILER_STRING "visual c++" +# if defined(TB_FORCE_MSVC_4_2) && (_MSC_VER == 1020) +# define TB_COMPILER_VERSION_STRING "visual c++ 4.2" +# elif (_MSC_VER == 1100) +# define TB_COMPILER_VERSION_STRING "visual c++ 5.0" +# elif (_MSC_VER == 1200) +# define TB_COMPILER_VERSION_STRING "visual c++ 6.0" +# elif (_MSC_VER == 1300) +# define TB_COMPILER_VERSION_STRING "visual c++ .net (7.0)" +# elif (_MSC_VER == 1310) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2003 (7.1)" +# elif (_MSC_VER == 1400) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2005 (8.0)" +# elif (_MSC_VER == 1500) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2008 (9.0)" +# elif (_MSC_VER == 1600) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2010 (10.0)" +# elif (_MSC_VER == 1700) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2012 (11.0)" +# elif (_MSC_VER == 1800) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2013 (12.0)" +# elif (_MSC_VER == 1900) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2015 (14.0)" +# elif (_MSC_VER == 1910) +# define TB_COMPILER_VERSION_STRING "visual c++ .net 2017 (14.1)" +# else +# error Unknown visual c++ Compiler Version +# endif + +// suppress warning +# pragma warning(disable:4018) +# pragma warning(disable:4197) +# pragma warning(disable:4141) +# pragma warning(disable:4996) +# pragma warning(disable:4359) +# pragma warning(disable:4838) + +#else +# define TB_COMPILER_STRING "unknown compiler" +# define TB_COMPILER_VERSION_STRING "unknown compiler version" +# define TB_COMPILER_IS_UNKNOWN +#endif + + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/config.h b/core/pkg/tbox.pkg/inc/tbox/prefix/config.h index dfc256073..07d920192 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/config.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/config.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file config.h + * + */ +#ifndef TB_PREFIX_CONFIG_H +#define TB_PREFIX_CONFIG_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../config.h" + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/cpu.h b/core/pkg/tbox.pkg/inc/tbox/prefix/cpu.h index 8be64370a..000d50a8b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/cpu.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/cpu.h @@ -70,3 +70,75 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file cpu.h + * + */ +#ifndef TB_PREFIX_CPU_H +#define TB_PREFIX_CPU_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// check 64-bits +#if defined(__LP64__) \ + || defined(__64BIT__) \ + || defined(_LP64) \ + || defined(__x86_64) \ + || defined(__x86_64__) \ + || defined(__amd64) \ + || defined(__amd64__) \ + || defined(__arm64) \ + || defined(__arm64__) \ + || defined(__sparc64__) \ + || defined(__PPC64__) \ + || defined(__ppc64__) \ + || defined(__powerpc64__) \ + || defined(_M_X64) \ + || defined(_M_AMD64) \ + || defined(_M_IA64) \ + || (defined(__WORDSIZE) && (__WORDSIZE == 64)) +# define TB_CPU_BITSIZE (64) +# define TB_CPU_BITBYTE (8) +# define TB_CPU_BITALIGN (7) +# define TB_CPU_BIT32 (0) +# define TB_CPU_BIT64 (1) +# define TB_CPU_SHIFT (6) +#else +# define TB_CPU_BITSIZE (32) +# define TB_CPU_BITBYTE (4) +# define TB_CPU_BITALIGN (3) +# define TB_CPU_BIT32 (1) +# define TB_CPU_BIT64 (0) +# define TB_CPU_SHIFT (5) +#endif + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/endian.h b/core/pkg/tbox.pkg/inc/tbox/prefix/endian.h index fa3854f18..45e4655f0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/endian.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/endian.h @@ -53,3 +53,58 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file endian.h + * + */ +#ifndef TB_PREFIX_ENDIAN_H +#define TB_PREFIX_ENDIAN_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// words endian +#ifdef __BYTE_ORDER__ +# if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ +# define TB_WORDS_BIGENDIAN +# error check it +# endif +#endif + +// float endian +#ifdef __FLOAT_WORD_ORDER__ +# if __FLOAT_WORD_ORDER__ != __ORDER_LITTLE_ENDIAN__ +# define TB_FLOAT_BIGENDIAN +# error check it +# endif +#endif + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/keyword.h b/core/pkg/tbox.pkg/inc/tbox/prefix/keyword.h index c0a84fa1d..7b101b7ab 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/keyword.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/keyword.h @@ -295,3 +295,300 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file keyword.h + * + */ +#ifndef TB_PREFIX_KEYWORD_H +#define TB_PREFIX_KEYWORD_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "compiler.h" +#include "cpu.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the register keyword will be deprecated in C++ +#ifndef __cplusplus +# define __tb_register__ register +#else +# define __tb_register__ +#endif +#define __tb_volatile__ volatile +#define __tb_func__ __FUNCTION__ +#define __tb_file__ __FILE__ +#define __tb_line__ __LINE__ + +#if defined(TB_COMPILER_IS_MSVC) + +# define __tb_asm__ __asm +# define __tb_inline__ __inline +# define __tb_inline_force__ __forceinline +# define __tb_cdecl__ __cdecl +# define __tb_stdcall__ __stdcall +# define __tb_fastcall__ __fastcall +# define __tb_thiscall__ __thiscall +# define __tb_packed__ +# define __tb_aligned__(a) __declspec(align(a)) + +#elif defined(TB_COMPILER_IS_GCC) + +# define __tb_asm__ __asm__ +# define __tb_inline__ __inline__ +# define __tb_inline_force__ __inline__ __attribute__((always_inline)) +# define __tb_packed__ __attribute__((packed, aligned(1))) +# define __tb_aligned__(a) __attribute__((aligned(a))) + // gcc will generate attribute ignored warning +# if defined(__x86_64) \ + || defined(__amd64__) \ + || defined(__amd64) \ + || defined(_M_IA64) \ + || defined(_M_X64) +# define __tb_cdecl__ +# define __tb_stdcall__ +# define __tb_fastcall__ +# define __tb_thiscall__ +# else +# define __tb_cdecl__ __attribute__((__cdecl__)) +# define __tb_stdcall__ __attribute__((__stdcall__)) +# define __tb_fastcall__ __attribute__((__fastcall__)) +# define __tb_thiscall__ __attribute__((__thiscall__)) +# endif +#else + +# define __tb_asm__ +# define __tb_inline__ inline +# define __tb_inline_force__ inline +# define __tb_func__ +# define __tb_file__ "" +# define __tb_line__ (0) + +# define __tb_cdecl__ +# define __tb_stdcall__ +# define __tb_fastcall__ +# define __tb_thiscall__ +# define __tb_packed__ +# define __tb_aligned__(a) + +#endif + +/*! @def __tb_cpu_aligned__ + * + * the cpu byte alignment + */ +#if (TB_CPU_BITBYTE == 8) +# define __tb_cpu_aligned__ __tb_aligned__(8) +#elif (TB_CPU_BITBYTE == 4) +# define __tb_cpu_aligned__ __tb_aligned__(4) +#elif (TB_CPU_BITBYTE == 2) +# define __tb_cpu_aligned__ __tb_aligned__(2) +#else +# error unknown cpu bytes +#endif + +// like +#if defined(TB_COMPILER_IS_GCC) && TB_COMPILER_VERSION_BT(2, 0) +# define __tb_likely__(x) __builtin_expect((x), 1) +# define __tb_unlikely__(x) __builtin_expect((x), 0) +#else +# define __tb_likely__(x) (x) +# define __tb_unlikely__(x) (x) +#endif + +// debug +#ifdef __tb_debug__ +# define __tb_debug_decl__ , tb_char_t const* func_, tb_size_t line_, tb_char_t const* file_ +# define __tb_debug_vals__ , __tb_func__, __tb_line__, __tb_file__ +# define __tb_debug_args__ , func_, line_, file_ +#else +# define __tb_debug_decl__ +# define __tb_debug_vals__ +# define __tb_debug_args__ +#endif + +// small +#undef __tb_small__ +#ifdef TB_CONFIG_SMALL +# define __tb_small__ +#endif + +// newline +#ifdef TB_CONFIG_OS_WINDOWS +# define __tb_newline__ "\r\n" +#else +# define __tb_newline__ "\n" +#endif + +// the string only for the large mode +#ifdef __tb_small__ +# define __tb_lstring__(x) tb_null +#else +# define __tb_lstring__(x) x +#endif + +// the string only for the debug mode +#ifdef __tb_debug__ +# define __tb_dstring__(x) x +#else +# define __tb_dstring__(x) tb_null +#endif + +// extern c +#ifdef __cplusplus +# define __tb_extern_c__ extern "C" +# define __tb_extern_c_enter__ extern "C" { +# define __tb_extern_c_leave__ } +#else +# define __tb_extern_c__ +# define __tb_extern_c_enter__ +# define __tb_extern_c_leave__ +#endif + +// export for the shared library +#if defined(TB_COMPILER_IS_MSVC) +# define __tb_export__ __declspec(dllexport) +#elif defined(TB_COMPILER_IS_GCC) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) +# define __tb_export__ __attribute__((visibility("default"))) +#else +# define __tb_export__ +#endif + +#if defined(TB_COMPILER_IS_GCC) && TB_COMPILER_VERSION_BE(3, 0) +# define __tb_deprecated__ __attribute__((deprecated)) +#elif defined(TB_COMPILER_IS_MSVC) && defined(_MSC_VER) && _MSC_VER >= 1300 +# define __tb_deprecated__ __declspec(deprecated) +#else +# define __tb_deprecated__ +#endif + +// has feature +#ifdef __has_feature +# define __tb_has_feature__(x) __has_feature(x) +#else +# define __tb_has_feature__(x) 0 +#endif + +// has include +#ifdef __has_include +# define __tb_has_include__(x) __has_include(x) +#else +# define __tb_has_include__(x) 0 +#endif + +// has builtin +#ifdef __has_builtin +# define __tb_has_builtin__(x) __has_builtin(x) +#else +# define __tb_has_builtin__(x) 0 +#endif + +// no_sanitize_address +#if __tb_has_feature__(address_sanitizer) || defined(__SANITIZE_ADDRESS__) +# define __tb_no_sanitize_address__ __attribute__((no_sanitize_address)) +#else +# define __tb_no_sanitize_address__ +#endif + +// thread local +#if __tb_has_feature__(c_thread_local) +# define __tb_thread_local__ _Thread_local +#elif defined(TB_COMPILER_IS_GCC) +# if TB_COMPILER_VERSION_BE(4, 9) +# define __tb_thread_local__ _Thread_local +# else +# define __tb_thread_local__ __thread +# endif +#elif defined(TB_COMPILER_IS_MSVC) || defined(TB_COMPILER_IS_BORLAND) +# define __tb_thread_local__ __declspec(thread) +#endif + +/*! the type reference keyword for defining tb_xxxx_ref_t + * + * typedef __tb_typeref__(xxxx); + * + * + * suppress gcc 4.9 on c++ codes warning: '__tb_yyyy_t' has a field '__tb_yyyy_t::xxxx' whose type uses the anonymous namespace + * + * @code + * + typedef struct{}* tb_xxxx_ref_t; + + typedef struct __tb_yyyy_t + { + tb_xxxx_ref_t xxxx; + + }__tb_yyyy_t; + + * + * + * @endcode + * + */ +#define __tb_typeref__(object) struct __tb_##object##_dummy_t{tb_int_t dummy;}* tb_##object##_ref_t + +// macros +#define __tb_mstring__(x) #x +#define __tb_mstring_ex__(x) __tb_mstring__(x) + +#define __tb_mconcat__(a, b) a##b +#define __tb_mconcat_ex__(a, b) __tb_mconcat__(a, b) + +#define __tb_mconcat3__(a, b, c) a##b##c +#define __tb_mconcat3_ex__(a, b, c) __tb_mconcat3__(a, b, c) + +#define __tb_mconcat4__(a, b, c, d) a##b##c##d +#define __tb_mconcat4_ex__(a, b, c, d) __tb_mconcat4__(a, b, c, d) + +#define __tb_mconcat5__(a, b, c, d, e) a##b##c##d##e +#define __tb_mconcat5_ex__(a, b, c, d, e) __tb_mconcat5__(a, b, c, d, e) + +#define __tb_mconcat6__(a, b, c, d, e, f) a##b##c##d##e##f +#define __tb_mconcat6_ex__(a, b, c, d, e, f) __tb_mconcat6__(a, b, c, d, e, f) + +#define __tb_mconcat7__(a, b, c, d, e, f, g) a##b##c##d##e##f##g +#define __tb_mconcat7_ex__(a, b, c, d, e, f, g) __tb_mconcat7__(a, b, c, d, e, f, g) + +#define __tb_mconcat8__(a, b, c, d, e, f, g, h) a##b##c##d##e##f##g##h +#define __tb_mconcat8_ex__(a, b, c, d, e, f, g, h) __tb_mconcat8__(a, b, c, d, e, f, g, h) + +#define __tb_mconcat9__(a, b, c, d, e, f, g, h, i) a##b##c##d##e##f##g##h##i +#define __tb_mconcat9_ex__(a, b, c, d, e, f, g, h, i) __tb_mconcat9__(a, b, c, d, e, f, g, h, i) + +#define __tb_mstrcat__(a, b) a b +#define __tb_mstrcat3__(a, b, c) a b c +#define __tb_mstrcat4__(a, b, c, d) a b c d +#define __tb_mstrcat5__(a, b, c, d, e) a b c d e +#define __tb_mstrcat6__(a, b, c, d, e, f) a b c d e f +#define __tb_mstrcat7__(a, b, c, d, e, f, g) a b c d e f g +#define __tb_mstrcat8__(a, b, c, d, e, f, g, h) a b c d e f g h +#define __tb_mstrcat9__(a, b, c, d, e, f, g, h, i) a b c d e f g h i + + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/limits.h b/core/pkg/tbox.pkg/inc/tbox/prefix/limits.h index f8ad9a855..6a811e2e0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/limits.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/limits.h @@ -60,3 +60,65 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file limits.h + * + */ +#ifndef TB_PREFIX_LIMITS_H +#define TB_PREFIX_LIMITS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#define TB_MAXS8 ((tb_sint8_t)(0x7f)) +#define TB_MINS8 ((tb_sint8_t)(0x81)) +#define TB_MAXU8 ((tb_uint8_t)(0xff)) +#define TB_MINU8 ((tb_uint8_t)(0)) + +#define TB_MAXS16 ((tb_sint16_t)(0x7fff)) +#define TB_MINS16 ((tb_sint16_t)(0x8001)) +#define TB_MAXU16 ((tb_uint16_t)(0xffff)) +#define TB_MINU16 ((tb_uint16_t)(0)) + +#define TB_MAXS32 ((tb_sint32_t)(0x7fffffff)) +#define TB_MINS32 ((tb_sint32_t)(0x80000001)) +#define TB_MAXU32 ((tb_uint32_t)(0xffffffff)) +#define TB_MINU32 ((tb_uint32_t)(0)) + +#define TB_MAXS64 ((tb_sint64_t)(0x7fffffffffffffffLL)) +#define TB_MINS64 ((tb_sint64_t)(0x8000000000000001LL)) +#define TB_MAXU64 ((tb_uint64_t)(0xffffffffffffffffULL)) +#define TB_MINU64 ((tb_uint64_t)(0)) + +#define TB_NAN32 (0x80000000) + + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/malloc.h b/core/pkg/tbox.pkg/inc/tbox/prefix/malloc.h index 334815267..3990249bf 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/malloc.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/malloc.h @@ -127,3 +127,132 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file malloc.h + * + */ +#ifndef TB_PREFIX_MALLOC_H +#define TB_PREFIX_MALLOC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define tb_free(data) tb_allocator_free_(tb_allocator(), (tb_pointer_t)data __tb_debug_vals__) +#define tb_malloc(size) tb_allocator_malloc_(tb_allocator(), size __tb_debug_vals__) +#define tb_malloc0(size) tb_allocator_malloc0_(tb_allocator(), size __tb_debug_vals__) +#define tb_nalloc(item, size) tb_allocator_nalloc_(tb_allocator(), item, size __tb_debug_vals__) +#define tb_nalloc0(item, size) tb_allocator_nalloc0_(tb_allocator(), item, size __tb_debug_vals__) +#define tb_ralloc(data, size) tb_allocator_ralloc_(tb_allocator(), (tb_pointer_t)data, size __tb_debug_vals__) + +#define tb_malloc_cstr(size) (tb_char_t*)tb_allocator_malloc_(tb_allocator(), size __tb_debug_vals__) +#define tb_malloc0_cstr(size) (tb_char_t*)tb_allocator_malloc0_(tb_allocator(), size __tb_debug_vals__) +#define tb_nalloc_cstr(item, size) (tb_char_t*)tb_allocator_nalloc_(tb_allocator(), item, size __tb_debug_vals__) +#define tb_nalloc0_cstr(item, size) (tb_char_t*)tb_allocator_nalloc0_(tb_allocator(), item, size __tb_debug_vals__) +#define tb_ralloc_cstr(data, size) (tb_char_t*)tb_allocator_ralloc_(tb_allocator(), (tb_pointer_t)data, size __tb_debug_vals__) + +#define tb_malloc_bytes(size) (tb_byte_t*)tb_allocator_malloc_(tb_allocator(), size __tb_debug_vals__) +#define tb_malloc0_bytes(size) (tb_byte_t*)tb_allocator_malloc0_(tb_allocator(), size __tb_debug_vals__) +#define tb_nalloc_bytes(item, size) (tb_byte_t*)tb_allocator_nalloc_(tb_allocator(), item, size __tb_debug_vals__) +#define tb_nalloc0_bytes(item, size) (tb_byte_t*)tb_allocator_nalloc0_(tb_allocator(), item, size __tb_debug_vals__) +#define tb_ralloc_bytes(data, size) (tb_byte_t*)tb_allocator_ralloc_(tb_allocator(), (tb_pointer_t)data, size __tb_debug_vals__) + +#define tb_malloc_type(type) (type*)tb_allocator_malloc_(tb_allocator(), sizeof(type) __tb_debug_vals__) +#define tb_malloc0_type(type) (type*)tb_allocator_malloc0_(tb_allocator(), sizeof(type) __tb_debug_vals__) +#define tb_nalloc_type(item, type) (type*)tb_allocator_nalloc_(tb_allocator(), item, sizeof(type) __tb_debug_vals__) +#define tb_nalloc0_type(item, type) (type*)tb_allocator_nalloc0_(tb_allocator(), item, sizeof(type) __tb_debug_vals__) +#define tb_ralloc_type(data, item, type) (type*)tb_allocator_ralloc_(tb_allocator(), (tb_pointer_t)data, ((item) * sizeof(type)) __tb_debug_vals__) + +#define tb_align_free(data) tb_allocator_align_free_(tb_allocator(), (tb_pointer_t)data __tb_debug_vals__) +#define tb_align_malloc(size, align) tb_allocator_align_malloc_(tb_allocator(), size, align __tb_debug_vals__) +#define tb_align_malloc0(size, align) tb_allocator_align_malloc0_(tb_allocator(), size, align __tb_debug_vals__) +#define tb_align_nalloc(item, size, align) tb_allocator_align_nalloc_(tb_allocator(), item, size, align __tb_debug_vals__) +#define tb_align_nalloc0(item, size, align) tb_allocator_align_nalloc0_(tb_allocator(), item, size, align __tb_debug_vals__) +#define tb_align_ralloc(data, size, align) tb_allocator_align_ralloc_(tb_allocator(), (tb_pointer_t)data, size, align __tb_debug_vals__) + +#define tb_large_free(data) tb_allocator_large_free_(tb_allocator(), (tb_pointer_t)data __tb_debug_vals__) +#define tb_large_malloc(size, real) tb_allocator_large_malloc_(tb_allocator(), size, real __tb_debug_vals__) +#define tb_large_malloc0(size, real) tb_allocator_large_malloc0_(tb_allocator(), size, real __tb_debug_vals__) +#define tb_large_nalloc(item, size, real) tb_allocator_large_nalloc_(tb_allocator(), item, size, real __tb_debug_vals__) +#define tb_large_nalloc0(item, size, real) tb_allocator_large_nalloc0_(tb_allocator(), item, size, real __tb_debug_vals__) +#define tb_large_ralloc(data, size, real) tb_allocator_large_ralloc_(tb_allocator(), (tb_pointer_t)data, size, real __tb_debug_vals__) + +#if TB_CPU_BIT64 +# define tb_align8_free(data) tb_free((tb_pointer_t)data) +# define tb_align8_malloc(size) tb_malloc(size) +# define tb_align8_malloc0(size) tb_malloc0(size) +# define tb_align8_nalloc(item, size) tb_nalloc(item, size) +# define tb_align8_nalloc0(item, size) tb_nalloc0(item, size) +# define tb_align8_ralloc(data, size) tb_ralloc((tb_pointer_t)data, size) +#else +# define tb_align8_free(data) tb_align_free((tb_pointer_t)data) +# define tb_align8_malloc(size) tb_align_malloc(size, 8) +# define tb_align8_malloc0(size) tb_align_malloc0(size, 8) +# define tb_align8_nalloc(item, size) tb_align_nalloc(item, size, 8) +# define tb_align8_nalloc0(item, size) tb_align_nalloc0(item, size, 8) +# define tb_align8_ralloc(data, size) tb_align_ralloc((tb_pointer_t)data, size, 8) +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * declaration + */ + +struct __tb_allocator_t; +struct __tb_allocator_t* tb_allocator(); +tb_pointer_t tb_allocator_malloc_(struct __tb_allocator_t* allocator, tb_size_t size __tb_debug_decl__); +tb_pointer_t tb_allocator_malloc0_(struct __tb_allocator_t* allocator, tb_size_t size __tb_debug_decl__); +tb_pointer_t tb_allocator_nalloc_(struct __tb_allocator_t* allocator, tb_size_t item, tb_size_t size __tb_debug_decl__); +tb_pointer_t tb_allocator_nalloc0_(struct __tb_allocator_t* allocator, tb_size_t item, tb_size_t size __tb_debug_decl__); +tb_pointer_t tb_allocator_ralloc_(struct __tb_allocator_t* allocator, tb_pointer_t data, tb_size_t size __tb_debug_decl__); +tb_bool_t tb_allocator_free_(struct __tb_allocator_t* allocator, tb_pointer_t data __tb_debug_decl__); +tb_pointer_t tb_allocator_align_malloc_(struct __tb_allocator_t* allocator, tb_size_t size, tb_size_t align __tb_debug_decl__); +tb_pointer_t tb_allocator_align_malloc0_(struct __tb_allocator_t* allocator, tb_size_t size, tb_size_t align __tb_debug_decl__); +tb_pointer_t tb_allocator_align_nalloc_(struct __tb_allocator_t* allocator, tb_size_t item, tb_size_t size, tb_size_t align __tb_debug_decl__); +tb_pointer_t tb_allocator_align_nalloc0_(struct __tb_allocator_t* allocator, tb_size_t item, tb_size_t size, tb_size_t align __tb_debug_decl__); +tb_pointer_t tb_allocator_align_ralloc_(struct __tb_allocator_t* allocator, tb_pointer_t data, tb_size_t size, tb_size_t align __tb_debug_decl__); +tb_bool_t tb_allocator_align_free_(struct __tb_allocator_t* allocator, tb_pointer_t data __tb_debug_decl__); +tb_pointer_t tb_allocator_large_malloc_(struct __tb_allocator_t* allocator, tb_size_t size, tb_size_t* real __tb_debug_decl__); +tb_pointer_t tb_allocator_large_malloc0_(struct __tb_allocator_t* allocator, tb_size_t size, tb_size_t* real __tb_debug_decl__); +tb_pointer_t tb_allocator_large_nalloc_(struct __tb_allocator_t* allocator, tb_size_t item, tb_size_t size, tb_size_t* real __tb_debug_decl__); +tb_pointer_t tb_allocator_large_nalloc0_(struct __tb_allocator_t* allocator, tb_size_t item, tb_size_t size, tb_size_t* real __tb_debug_decl__); +tb_pointer_t tb_allocator_large_ralloc_(struct __tb_allocator_t* allocator, tb_pointer_t data, tb_size_t size, tb_size_t* real __tb_debug_decl__); +tb_bool_t tb_allocator_large_free_(struct __tb_allocator_t* allocator, tb_pointer_t data __tb_debug_decl__); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/mips/prefix.S b/core/pkg/tbox.pkg/inc/tbox/prefix/mips/prefix.S index 9441b0484..ab72f6154 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/mips/prefix.S +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/mips/prefix.S @@ -59,3 +59,64 @@ .endm +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.S + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! function + * + * @code + function func_xxxx, export=1 + ... + endfunc + @endcode + */ +.macro function name, export=0 + .macro endfunc + .end \name + .size \name, . - \name + .purgem endfunc + .endm + + .text + .align TB_CPU_BITBYTE + .if \export + .global \name + .type \name, @function + .hidden \name + .ent \name +\name: + .else + .type \name, @function + .hidden \name + .ent \name +\name: + .endif +.endm + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/packed.h b/core/pkg/tbox.pkg/inc/tbox/prefix/packed.h index 0c4e35270..8e9c5ad09 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/packed.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/packed.h @@ -63,3 +63,68 @@ # undef TB_PACKED_ALIGN # endif #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file packed_e.h + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "compiler.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/* packed + * + * // #define TB_PACKED_ALIGN 4 + * #include "tbox/prefix/packed.h" + * typedef struct __tb_xxxxx_t + * { + * tb_byte_t a; + * tb_uint32_t b; + * + * } __tb_packed__ tb_xxxxx_t; + * + * #include "tbox/prefix/packed.h" + * + * sizeof(tb_xxxxx_t) == 5 + * + */ +#ifdef TB_COMPILER_IS_MSVC +# ifndef TB_PACKED_ENTER +# ifdef TB_PACKED_ALIGN +# pragma pack(push, TB_PACKED_ALIGN) +# else +# pragma pack(push, 1) +# endif +# define TB_PACKED_ENTER +# else +# pragma pack(pop) +# undef TB_PACKED_ENTER +# undef TB_PACKED_ALIGN +# endif +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/prefix.h b/core/pkg/tbox.pkg/inc/tbox/prefix/prefix.h index e75fda462..d58d60288 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/prefix.h @@ -50,3 +50,55 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_PREFIX_PREFIX_H +#define TB_PREFIX_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "cpu.h" +#include "arch.h" +#include "type.h" +#include "limits.h" +#include "endian.h" +#include "version.h" +#include "compiler.h" +#include "assembler.h" +#include "keyword.h" +#include "trace.h" +#include "abort.h" +#include "check.h" +#include "assert.h" +#include "malloc.h" +#include "utils.h" +#include "state.h" +#include "cache.h" + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/state.h b/core/pkg/tbox.pkg/inc/tbox/prefix/state.h index 7d3baa8cd..ece779bbc 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/state.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/state.h @@ -188,3 +188,193 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file state.h + * + */ +#ifndef TB_PREFIX_STATE_H +#define TB_PREFIX_STATE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "type.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the state define +#define TB_STATE_DEFINE(type, state) (((type) << 16) | (state)) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the state type enum +typedef enum __tb_state_type_e +{ + TB_STATE_TYPE_NONE = 0 +, TB_STATE_TYPE_SOCK = 1 +, TB_STATE_TYPE_HTTP = 2 +, TB_STATE_TYPE_DATABASE = 3 +, TB_STATE_TYPE_SYSERROR = 4 + +}tb_state_type_e; + +/// the state enum +typedef enum __tb_state_e +{ + TB_STATE_OK = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 0) +, TB_STATE_END = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 1) +, TB_STATE_DEAD = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 2) +, TB_STATE_READY = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 3) +, TB_STATE_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 4) +, TB_STATE_KILLED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 5) +, TB_STATE_PAUSED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 6) +, TB_STATE_CLOSED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 7) +, TB_STATE_OPENED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 8) +, TB_STATE_OPENING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 9) +, TB_STATE_SUSPEND = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 10) +, TB_STATE_RUNNING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 11) +, TB_STATE_PENDING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 12) +, TB_STATE_WAITING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 13) +, TB_STATE_WORKING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 14) +, TB_STATE_KILLING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 15) +, TB_STATE_PAUSING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 16) +, TB_STATE_EXITING = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 17) +, TB_STATE_TIMEOUT = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 18) +, TB_STATE_FINISHED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 19) +, TB_STATE_WAIT_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 20) +, TB_STATE_POST_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 21) +, TB_STATE_NOT_SUPPORTED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 22) +, TB_STATE_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 23) + +, TB_STATE_SOCK_DNS_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 1) +, TB_STATE_SOCK_CONNECT_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 2) +, TB_STATE_SOCK_CONNECT_TIMEOUT = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 3) +, TB_STATE_SOCK_RECV_TIMEOUT = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 4) +, TB_STATE_SOCK_SEND_TIMEOUT = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 5) +, TB_STATE_SOCK_RECV_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 6) +, TB_STATE_SOCK_SEND_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 7) +, TB_STATE_SOCK_OPEN_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 8) +, TB_STATE_SOCK_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 9) +, TB_STATE_SOCK_SSL_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 10) +, TB_STATE_SOCK_SSL_TIMEOUT = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 11) +, TB_STATE_SOCK_SSL_WANT_READ = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 12) +, TB_STATE_SOCK_SSL_WANT_WRIT = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 13) +, TB_STATE_SOCK_SSL_WAIT_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 14) +, TB_STATE_SOCK_SSL_READ_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 15) +, TB_STATE_SOCK_SSL_WRIT_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 16) +, TB_STATE_SOCK_SSL_NOT_SUPPORTED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 17) +, TB_STATE_SOCK_SSL_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 18) + +, TB_STATE_HTTP_RESPONSE_100 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 1) +, TB_STATE_HTTP_RESPONSE_101 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 2) +, TB_STATE_HTTP_RESPONSE_102 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 3) +, TB_STATE_HTTP_RESPONSE_200 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 4) +, TB_STATE_HTTP_RESPONSE_204 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 5) +, TB_STATE_HTTP_RESPONSE_300 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 6) +, TB_STATE_HTTP_RESPONSE_301 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 7) +, TB_STATE_HTTP_RESPONSE_302 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 8) +, TB_STATE_HTTP_RESPONSE_303 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 9) +, TB_STATE_HTTP_RESPONSE_304 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 10) +, TB_STATE_HTTP_RESPONSE_305 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 11) +, TB_STATE_HTTP_RESPONSE_306 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 12) +, TB_STATE_HTTP_RESPONSE_307 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 13) +, TB_STATE_HTTP_RESPONSE_400 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 14) +, TB_STATE_HTTP_RESPONSE_401 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 15) +, TB_STATE_HTTP_RESPONSE_402 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 16) +, TB_STATE_HTTP_RESPONSE_403 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 17) +, TB_STATE_HTTP_RESPONSE_404 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 18) +, TB_STATE_HTTP_RESPONSE_405 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 19) +, TB_STATE_HTTP_RESPONSE_406 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 20) +, TB_STATE_HTTP_RESPONSE_407 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 21) +, TB_STATE_HTTP_RESPONSE_408 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 22) +, TB_STATE_HTTP_RESPONSE_409 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 23) +, TB_STATE_HTTP_RESPONSE_410 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 24) +, TB_STATE_HTTP_RESPONSE_411 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 25) +, TB_STATE_HTTP_RESPONSE_412 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 26) +, TB_STATE_HTTP_RESPONSE_413 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 27) +, TB_STATE_HTTP_RESPONSE_414 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 28) +, TB_STATE_HTTP_RESPONSE_415 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 29) +, TB_STATE_HTTP_RESPONSE_416 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 30) +, TB_STATE_HTTP_RESPONSE_500 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 31) +, TB_STATE_HTTP_RESPONSE_501 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 32) +, TB_STATE_HTTP_RESPONSE_502 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 33) +, TB_STATE_HTTP_RESPONSE_503 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 34) +, TB_STATE_HTTP_RESPONSE_504 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 35) +, TB_STATE_HTTP_RESPONSE_505 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 36) +, TB_STATE_HTTP_RESPONSE_506 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 37) +, TB_STATE_HTTP_RESPONSE_507 = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 38) +, TB_STATE_HTTP_RESPONSE_UNK = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 39) +, TB_STATE_HTTP_RESPONSE_NUL = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 40) +, TB_STATE_HTTP_REQUEST_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 41) +, TB_STATE_HTTP_REDIRECT_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 42) +, TB_STATE_HTTP_POST_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 43) +, TB_STATE_HTTP_RANGE_INVALID = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 44) +, TB_STATE_HTTP_GZIP_NOT_SUPPORTED = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 45) +, TB_STATE_HTTP_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_HTTP, 46) + +, TB_STATE_DATABASE_NO_SUCH_TABLE = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 1) +, TB_STATE_DATABASE_NO_SUCH_FIELD = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 2) +, TB_STATE_DATABASE_NO_SUCH_DATABASE = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 3) +, TB_STATE_DATABASE_ACCESS_DENIED = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 4) +, TB_STATE_DATABASE_PARSE_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 5) +, TB_STATE_DATABASE_VALUE_COUNT_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 6) +, TB_STATE_DATABASE_UNKNOWN_HOST = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 7) +, TB_STATE_DATABASE_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 8) + +, TB_STATE_SYSERROR_NOT_PERM = TB_STATE_DEFINE(TB_STATE_TYPE_SYSERROR, 1) +, TB_STATE_SYSERROR_NOT_FILEDIR = TB_STATE_DEFINE(TB_STATE_TYPE_SYSERROR, 2) +, TB_STATE_SYSERROR_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_SYSERROR, 3) + +}tb_state_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the state string + * + * @param state the state + * + * @return the state string + */ +tb_char_t const* tb_state_cstr(tb_size_t state); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/trace.h b/core/pkg/tbox.pkg/inc/tbox/prefix/trace.h index 7cca2c87d..0a248278d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/trace.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/trace.h @@ -320,3 +320,325 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file trace.h + * + */ +#ifndef TB_PREFIX_TRACE_H +#define TB_PREFIX_TRACE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "type.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the trace prefix +#ifndef __tb_prefix__ +# define __tb_prefix__ tb_null +#endif + +// the trace module name +#ifndef TB_TRACE_MODULE_NAME +# define TB_TRACE_MODULE_NAME tb_null +#endif + +// the trace module debug +#ifndef TB_TRACE_MODULE_DEBUG +# define TB_TRACE_MODULE_DEBUG (1) +#endif + +// trace prefix +#if defined(TB_COMPILER_IS_GCC) +# define tb_trace_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, fmt __tb_newline__, ## arg); } while (0) +# define tb_tracef_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, fmt, ## arg); } while (0) +# ifdef __tb_debug__ +# define tb_trace_error_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[error]: " fmt " at %s(): %d, %s" __tb_newline__, ##arg, __tb_func__, __tb_line__, __tb_file__); tb_trace_sync(); } while (0) +# define tb_trace_assert_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[assert]: " fmt " at %s(): %d, %s" __tb_newline__, ##arg, __tb_func__, __tb_line__, __tb_file__); tb_trace_sync(); } while (0) +# define tb_trace_warning_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[warning]: " fmt " at %s(): %d, %s" __tb_newline__, ##arg, __tb_func__, __tb_line__, __tb_file__); tb_trace_sync(); } while (0) +# define tb_tracef_error_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[error]: " fmt " at %s(): %d, %s", ##arg, __tb_func__, __tb_line__, __tb_file__); tb_trace_sync(); } while (0) +# define tb_tracef_assert_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[assert]: " fmt " at %s(): %d, %s", ##arg, __tb_func__, __tb_line__, __tb_file__); tb_trace_sync(); } while (0) +# define tb_tracef_warning_p(prefix, fmt, arg ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[warning]: " fmt " at %s(): %d, %s", ##arg, __tb_func__, __tb_line__, __tb_file__); tb_trace_sync(); } while (0) +# else +# define tb_trace_error_p(prefix, fmt, arg ...) +# define tb_trace_assert_p(prefix, fmt, arg ...) +# define tb_trace_warning_p(prefix, fmt, arg ...) +# define tb_tracef_error_p(prefix, fmt, arg ...) +# define tb_tracef_assert_p(prefix, fmt, arg ...) +# define tb_tracef_warning_p(prefix, fmt, arg ...) +# endif +#elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_trace_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, fmt __tb_newline__, __VA_ARGS__); } while (0) +# define tb_tracef_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, fmt, __VA_ARGS__); } while (0) +# ifdef __tb_debug__ +# define tb_trace_error_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[error]: at %s(): %d, %s: " fmt __tb_newline__, __tb_func__, __tb_line__, __tb_file__, __VA_ARGS__); tb_trace_sync(); } while (0) +# define tb_trace_assert_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[assert]: at %s(): %d, %s: " fmt __tb_newline__, __tb_func__, __tb_line__, __tb_file__, __VA_ARGS__); tb_trace_sync(); } while (0) +# define tb_trace_warning_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[warning]: at %s(): %d, %s: " fmt __tb_newline__, __tb_func__, __tb_line__, __tb_file__, __VA_ARGS__); tb_trace_sync(); } while (0) +# define tb_tracef_error_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[error]: at %s(): %d, %s: " fmt, __tb_func__, __tb_line__, __tb_file__, __VA_ARGS__); tb_trace_sync(); } while (0) +# define tb_tracef_assert_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[assert]: at %s(): %d, %s: " fmt, __tb_func__, __tb_line__, __tb_file__, __VA_ARGS__); tb_trace_sync(); } while (0) +# define tb_tracef_warning_p(prefix, fmt, ...) do { tb_trace_done(prefix, TB_TRACE_MODULE_NAME, "[warning]: at %s(): %d, %s: " fmt, __tb_func__, __tb_line__, __tb_file__, __VA_ARGS__); tb_trace_sync(); } while (0) +# else +# define tb_trace_error_p(prefix, fmt, ...) +# define tb_trace_assert_p(prefix, fmt, ...) +# define tb_trace_warning_p(prefix, fmt, ...) +# define tb_tracef_error_p(prefix, fmt, ...) +# define tb_tracef_assert_p(prefix, fmt, ...) +# define tb_tracef_warning_p(prefix, fmt, ...) +# endif +#else +# define tb_trace_p +# define tb_trace_error_p +# define tb_trace_assert_p +# define tb_trace_warning_p +# define tb_tracef_p +# define tb_tracef_error_p +# define tb_tracef_assert_p +# define tb_tracef_warning_p +#endif + +/* trace + * + * at file xxxx.c: + * + * // macros + * #define TB_TRACE_MODULE_NAME "module" + * #define TB_TRACE_MODULE_DEBUG (1) + * + * // includes + * #include "tbox.h" + * + * // trace + * tb_trace_d("trace debug"); + * tb_trace_i("trace info"); + * tb_trace_e("trace error"); + * tb_trace_w("trace warning"); + * + * // output for debug + * "[prefix]: [module]: trace debug" + * "[prefix]: [module]: trace info" + * "[prefix]: [module]: [error]: trace error" at func: xxx, line: xxx, file: xxx + * "[prefix]: [module]: [warning]: trace warning" at func: xxx, line: xxx, file: xxx + * + * // output for release or TB_TRACE_MODULE_DEBUG == 0 + * "[prefix]: [module]: trace info" + * "[prefix]: [module]: [error]: trace error" at func: xxx, line: xxx, file: xxx + * "[prefix]: [module]: [warning]: trace warning" at func: xxx, line: xxx, file: xxx + * + * note: [module]: will be not output if TB_TRACE_MODULE_NAME is not defined + * + */ +#if TB_TRACE_MODULE_DEBUG && defined(__tb_debug__) +# if defined(TB_COMPILER_IS_GCC) +# define TB_TRACE_DEBUG +# define tb_trace_d(fmt, arg ...) tb_trace_p(__tb_prefix__, fmt, ## arg) +# define tb_tracef_d(fmt, arg ...) tb_tracef_p(__tb_prefix__, fmt, ## arg) +# define tb_tracet_d(fmt, arg ...) tb_trace_tail(fmt, ## arg) +# elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define TB_TRACE_DEBUG +# define tb_trace_d(fmt, ...) tb_trace_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracef_d(fmt, ...) tb_tracef_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracet_d(fmt, ...) tb_trace_tail(fmt, __VA_ARGS__) +# else +# define tb_trace_d +# define tb_tracef_d +# define tb_tracet_d +# endif +#else +# if defined(TB_COMPILER_IS_GCC) || (defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0)) +# define tb_trace_d(fmt, ...) +# define tb_tracef_d(fmt, ...) +# define tb_tracet_d(fmt, ...) +# else +# define tb_trace_d +# define tb_tracef_d +# define tb_tracet_d +# endif +#endif + +#if defined(TB_COMPILER_IS_GCC) +# define tb_trace_i(fmt, arg ...) tb_trace_p(__tb_prefix__, fmt, ## arg) +# define tb_trace_e(fmt, arg ...) tb_trace_error_p(__tb_prefix__, fmt, ## arg) +# define tb_trace_a(fmt, arg ...) tb_trace_assert_p(__tb_prefix__, fmt, ## arg) +# define tb_trace_w(fmt, arg ...) tb_trace_warning_p(__tb_prefix__, fmt, ## arg) +# define tb_tracef_i(fmt, arg ...) tb_tracef_p(__tb_prefix__, fmt, ## arg) +# define tb_tracef_e(fmt, arg ...) tb_tracef_error_p(__tb_prefix__, fmt, ## arg) +# define tb_tracef_a(fmt, arg ...) tb_tracef_assert_p(__tb_prefix__, fmt, ## arg) +# define tb_tracef_w(fmt, arg ...) tb_tracef_warning_p(__tb_prefix__, fmt, ## arg) +# define tb_tracet_i(fmt, arg ...) tb_trace_tail(fmt, ## arg) +# define tb_tracet_e(fmt, arg ...) tb_trace_tail(fmt, ## arg) +# define tb_tracet_a(fmt, arg ...) tb_trace_tail(fmt, ## arg) +# define tb_tracet_w(fmt, arg ...) tb_trace_tail(fmt, ## arg) +#elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_trace_i(fmt, ...) tb_trace_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_trace_e(fmt, ...) tb_trace_error_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_trace_a(fmt, ...) tb_trace_assert_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_trace_w(fmt, ...) tb_trace_warning_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracef_i(fmt, ...) tb_tracef_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracef_e(fmt, ...) tb_tracef_error_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracef_a(fmt, ...) tb_tracef_assert_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracef_w(fmt, ...) tb_tracef_warning_p(__tb_prefix__, fmt, __VA_ARGS__) +# define tb_tracet_i(fmt, ...) tb_trace_tail(fmt, __VA_ARGS__) +# define tb_tracet_e(fmt, ...) tb_trace_tail(fmt, __VA_ARGS__) +# define tb_tracet_a(fmt, ...) tb_trace_tail(fmt, __VA_ARGS__) +# define tb_tracet_w(fmt, ...) tb_trace_tail(fmt, __VA_ARGS__) +#else +# define tb_trace_i +# define tb_trace_e +# define tb_trace_a +# define tb_trace_w +# define tb_tracef_i +# define tb_tracef_e +# define tb_tracef_a +# define tb_tracef_w +# define tb_tracet_i +# define tb_tracet_e +# define tb_tracet_a +# define tb_tracet_w +#endif + +// trace once +#if defined(TB_COMPILER_IS_GCC) +# define tb_trace1_d(fmt, arg ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_d(fmt, ## arg); __trace_once = tb_true; } } while (0) +# define tb_trace1_i(fmt, arg ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_i(fmt, ## arg); __trace_once = tb_true; } } while (0) +# define tb_trace1_e(fmt, arg ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_e(fmt, ## arg); __trace_once = tb_true; } } while (0) +# define tb_trace1_a(fmt, arg ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_a(fmt, ## arg); __trace_once = tb_true; } } while (0) +# define tb_trace1_w(fmt, arg ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_w(fmt, ## arg); __trace_once = tb_true; } } while (0) +#elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_trace1_d(fmt, ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_d(fmt, __VA_ARGS__); __trace_once = tb_true; } } while (0) +# define tb_trace1_i(fmt, ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_i(fmt, __VA_ARGS__); __trace_once = tb_true; } } while (0) +# define tb_trace1_e(fmt, ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_e(fmt, __VA_ARGS__); __trace_once = tb_true; } } while (0) +# define tb_trace1_a(fmt, ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_a(fmt, __VA_ARGS__); __trace_once = tb_true; } } while (0) +# define tb_trace1_w(fmt, ...) do { static tb_bool_t __trace_once = tb_false; if (!__trace_once) { tb_trace_w(fmt, __VA_ARGS__); __trace_once = tb_true; } } while (0) +#else +# define tb_trace1_i +#endif + +// trace more info? +#if !defined(__tb_debug__) && !defined(TB_CONFIG_INFO_TRACE_MORE) +# undef TB_TRACE_DEBUG +# undef TB_TRACE_MODULE_DEBUG +# undef tb_trace_d +# undef tb_trace_e +# undef tb_trace_a +# undef tb_trace_w +# undef tb_tracef_d +# undef tb_tracef_e +# undef tb_tracef_a +# undef tb_tracef_w +# undef tb_tracet_d +# undef tb_tracet_e +# undef tb_tracet_a +# undef tb_tracet_w +# undef tb_trace1_d +# undef tb_trace1_e +# undef tb_trace1_a +# undef tb_trace1_w +# define TB_TRACE_MODULE_DEBUG (0) +# if defined(TB_COMPILER_IS_GCC) +# define tb_trace_d(fmt, arg ...) +# define tb_trace_e(fmt, arg ...) +# define tb_trace_a(fmt, arg ...) +# define tb_trace_w(fmt, arg ...) +# define tb_tracef_d(fmt, arg ...) +# define tb_tracef_e(fmt, arg ...) +# define tb_tracef_a(fmt, arg ...) +# define tb_tracef_w(fmt, arg ...) +# define tb_tracet_d(fmt, arg ...) +# define tb_tracet_e(fmt, arg ...) +# define tb_tracet_a(fmt, arg ...) +# define tb_tracet_w(fmt, arg ...) +# define tb_trace1_d(fmt, arg ...) +# define tb_trace1_e(fmt, arg ...) +# define tb_trace1_a(fmt, arg ...) +# define tb_trace1_w(fmt, arg ...) +# elif defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BE(13, 0) +# define tb_trace_d(fmt, ...) +# define tb_trace_e(fmt, ...) +# define tb_trace_a(fmt, ...) +# define tb_trace_w(fmt, ...) +# define tb_tracef_d(fmt, ...) +# define tb_tracef_e(fmt, ...) +# define tb_tracef_a(fmt, ...) +# define tb_tracef_w(fmt, ...) +# define tb_tracet_d(fmt, ...) +# define tb_tracet_e(fmt, ...) +# define tb_tracet_a(fmt, ...) +# define tb_tracet_w(fmt, ...) +# define tb_trace1_d(fmt, ...) +# define tb_trace1_e(fmt, ...) +# define tb_trace1_a(fmt, ...) +# define tb_trace1_w(fmt, ...) +# else +# define tb_trace_d +# define tb_trace_e +# define tb_trace_a +# define tb_trace_w +# define tb_tracef_d +# define tb_tracef_e +# define tb_tracef_a +# define tb_tracef_w +# define tb_tracet_d +# define tb_tracet_e +# define tb_tracet_a +# define tb_tracet_w +# define tb_trace1_d +# define tb_trace1_e +# define tb_trace1_a +# define tb_trace1_w +# endif +#endif + +// noimpl +#define tb_trace_noimpl() tb_trace1_w("noimpl") + +// nosafe +#define tb_trace_nosafe() tb_trace1_w("nosafe") + +// deprecated +#define tb_trace_deprecated() tb_trace1_w("deprecated") + +/* ////////////////////////////////////////////////////////////////////////////////////// + * declaration + */ +tb_void_t tb_trace_sync(tb_noarg_t); +tb_void_t tb_trace_done(tb_char_t const* prefix, tb_char_t const* module, tb_char_t const* format, ...); +tb_void_t tb_trace_tail(tb_char_t const* format, ...); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/type.h b/core/pkg/tbox.pkg/inc/tbox/prefix/type.h index 5eec3c380..e294c0fd1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/type.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/type.h @@ -334,3 +334,339 @@ typedef union __tb_value_t #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file type.h + * + */ +#ifndef TB_PREFIX_TYPE_H +#define TB_PREFIX_TYPE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "keyword.h" +#include "compiler.h" +#include "cpu.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// bool values +#define tb_true ((tb_bool_t)1) +#define tb_false ((tb_bool_t)0) + +// null +#ifdef __cplusplus +# define tb_null (0) +#else +# define tb_null ((tb_pointer_t)0) +#endif + +// check config +#if !defined(TB_CONFIG_TYPE_SCALAR_IS_FIXED) \ + && !defined(TB_CONFIG_TYPE_HAVE_FLOAT) +# define TB_CONFIG_TYPE_SCALAR_IS_FIXED +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// basic +typedef signed int tb_int_t; +typedef unsigned int tb_uint_t; +typedef signed short tb_short_t; +typedef unsigned short tb_ushort_t; +typedef tb_int_t tb_bool_t; +typedef signed char tb_int8_t; +typedef tb_int8_t tb_sint8_t; +typedef unsigned char tb_uint8_t; +typedef tb_short_t tb_int16_t; +typedef tb_int16_t tb_sint16_t; +typedef tb_ushort_t tb_uint16_t; +typedef tb_int_t tb_int32_t; +typedef tb_int32_t tb_sint32_t; +typedef tb_uint_t tb_uint32_t; +typedef char tb_char_t; +typedef tb_int32_t tb_uchar_t; +typedef tb_uint8_t tb_byte_t; +typedef void tb_void_t; +typedef tb_void_t* tb_pointer_t; +typedef tb_void_t const* tb_cpointer_t; +typedef tb_pointer_t tb_handle_t; + +// no argument +#ifdef __cplusplus +# define tb_noarg_t +#else +# define tb_noarg_t tb_void_t +#endif + +// wchar +#ifdef TB_CONFIG_TYPE_HAVE_WCHAR + typedef wchar_t tb_wchar_t; +#elif defined(TB_CONFIG_OS_WINDOWS) +# if defined(_WCHAR_T_DEFINED) || defined(_NATIVE_WCHAR_T_DEFINED) + typedef wchar_t tb_wchar_t; +# else + typedef tb_uint16_t tb_wchar_t; +# endif +#elif defined(__WCHAR_TYPE__) +typedef __WCHAR_TYPE__ tb_wchar_t; +#elif defined(__WCHAR_WIDTH__) && (__WCHAR_WIDTH__ == 2) +typedef tb_int16_t tb_wchar_t; +#elif defined(__WCHAR_WIDTH__) && (__WCHAR_WIDTH__ == 4) +typedef tb_int32_t tb_wchar_t; +#else +typedef tb_int32_t tb_wchar_t; +#endif + +// int64 +#if defined(TB_COMPILER_IS_MSVC) +typedef __int64 tb_int64_t; +typedef unsigned __int64 tb_uint64_t; +#elif (TB_CPU_BITSIZE == 64) +typedef signed long tb_int64_t; +typedef unsigned long tb_uint64_t; +#else +typedef signed long long tb_int64_t; +typedef unsigned long long tb_uint64_t; +#endif +typedef tb_int64_t tb_sint64_t; + +// hong and hize +typedef tb_sint64_t tb_hong_t; +typedef tb_uint64_t tb_hize_t; + +// long and size +#if defined(TB_CONFIG_OS_WINDOWS) && TB_CPU_BIT64 +typedef tb_int64_t tb_long_t; +typedef tb_uint64_t tb_ulong_t; +#else +typedef signed long tb_long_t; +typedef unsigned long tb_ulong_t; +#endif +typedef tb_ulong_t tb_size_t; + +// double +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +typedef float tb_float_t; +typedef double tb_double_t; +#endif + +// fixed +typedef tb_int32_t tb_fixed6_t; +typedef tb_int32_t tb_fixed16_t; +typedef tb_int32_t tb_fixed30_t; +typedef tb_fixed16_t tb_fixed_t; + +/// the atomic type +#if TB_CPU_BIT64 +typedef __tb_volatile__ __tb_aligned__(8) tb_long_t tb_atomic_t; +#else +typedef __tb_volatile__ __tb_aligned__(4) tb_long_t tb_atomic_t; +#endif + +/// the atomic64 type, need be aligned for arm, .. +typedef __tb_volatile__ __tb_aligned__(8) tb_hong_t tb_atomic64_t; + +/// the spinlock type +typedef tb_atomic_t tb_spinlock_t; + +/// the spinlock ref type +typedef tb_spinlock_t* tb_spinlock_ref_t; + +/// the pool ref type +typedef __tb_typeref__(pool); + +/// the file ref type +typedef __tb_typeref__(file); + +/// the socket ref type +typedef __tb_typeref__(socket); + +/// the event ref type +typedef __tb_typeref__(event); + +/// the mutex ref type +typedef __tb_typeref__(mutex); + +/// the thread ref type +typedef __tb_typeref__(thread); + +/// the semaphore ref type +typedef __tb_typeref__(semaphore); + +/// the stream ref type +typedef __tb_typeref__(stream); + +/// the async stream ref type +typedef __tb_typeref__(async_stream); + +/// the time type +typedef tb_hong_t tb_time_t; + +/// the suseconds type +typedef tb_long_t tb_suseconds_t; + +/// the timeval type +typedef struct __tb_timeval_t +{ + /// the seconds + tb_time_t tv_sec; + + // the useconds + tb_suseconds_t tv_usec; + +}tb_timeval_t; + +/// the timezone type +typedef struct __tb_timezone_t +{ + /// the minutes west of greenwich + tb_int_t tz_minuteswest; + + /// the type of dst correction + tb_int_t tz_dsttime; + +}tb_timezone_t; + +/// the version type +typedef struct __tb_version_t +{ + /// the major + tb_byte_t major; + + /// the minor + tb_byte_t minor; + + /// the alter + tb_byte_t alter; + + /// the build + tb_hize_t build; + +}tb_version_t; + +/// the value type +typedef union __tb_value_t +{ + /// uint8 + tb_uint8_t u8; + + /// sint8 + tb_sint8_t s8; + + /// char + tb_char_t c; + + /// wchar + tb_wchar_t wc; + + /// boolean + tb_bool_t b; + + /// uint16 + tb_uint16_t u16; + + /// sint16 + tb_sint16_t s16; + + /// uint32 + tb_uint32_t u32; + + /// sint32 + tb_sint32_t s32; + + /// uint64 + tb_uint64_t u64; + + /// sint64 + tb_sint64_t s64; + + /// size + tb_size_t ul; + + /// long + tb_long_t l; + + /// time + tb_time_t t; + + /// pointer + tb_pointer_t ptr; + + /// const pointer + tb_cpointer_t cptr; + + /// sock + tb_socket_ref_t sock; + + /// file + tb_file_ref_t file; + + /// handle + tb_handle_t handle; + + /// string + tb_char_t* str; + + /// const string + tb_char_t const* cstr; + + /// wstring + tb_wchar_t* wstr; + + /// const wstring + tb_wchar_t const* wcstr; + + /// atomic + tb_atomic_t a; + + /// atomic64 + tb_atomic_t a64; + + /// spinlock + tb_spinlock_t lock; + + /// stream + tb_stream_ref_t stream; + + /// async stream + tb_async_stream_ref_t astream; + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + /// float + tb_float_t f; + + /// double + tb_double_t d; +#endif + +}tb_value_t, *tb_value_ref_t; + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/utils.h b/core/pkg/tbox.pkg/inc/tbox/prefix/utils.h index 549182a7e..58510da01 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/utils.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/utils.h @@ -152,3 +152,157 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file utils.h + * + */ +#ifndef TB_PREFIX_UTILS_H +#define TB_PREFIX_UTILS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "cpu.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// abs +#define tb_abs(x) ((x) > 0? (x) : -(x)) + +/// max +#define tb_max(x, y) (((x) > (y))? (x) : (y)) + +/// min +#define tb_min(x, y) (((x) < (y))? (x) : (y)) + +/// max3 +#define tb_max3(x, y, z) (((x) > (y))? (((x) > (z))? (x) : (z)) : (((y) > (z))? (y) : (z))) + +/// min3 +#define tb_min3(x, y, z) (((x) < (y))? (((x) < (z))? (x) : (z)) : (((y) < (z))? (y) : (z))) + +/// the number of entries in the array +#define tb_arrayn(x) ((sizeof((x)) / sizeof((x)[0]))) + +/// ispow2: 1, 2, 4, 8, 16, 32, ... +#define tb_ispow2(x) (!((x) & ((x) - 1)) && (x)) + +/// align2 +#define tb_align2(x) (((x) + 1) >> 1 << 1) + +/// align4 +#define tb_align4(x) (((x) + 3) >> 2 << 2) + +/// align8 +#define tb_align8(x) (((x) + 7) >> 3 << 3) + +/// align +#define tb_align(x, b) (((tb_size_t)(x) + ((tb_size_t)(b) - 1)) & ~((tb_size_t)(b) - 1)) + +/// align u32 +#define tb_align_u32(x, b) (((tb_uint32_t)(x) + ((tb_uint32_t)(b) - 1)) & ~((tb_uint32_t)(b) - 1)) + +/// align u64 +#define tb_align_u64(x, b) (((tb_uint64_t)(x) + ((tb_uint64_t)(b) - 1)) & ~((tb_uint64_t)(b) - 1)) + +/// align by pow2 +#define tb_align_pow2(x) (((x) > 1)? (tb_ispow2(x)? (x) : ((tb_size_t)1 << (32 - tb_bits_cl0_u32_be((tb_uint32_t)(x))))) : 1) + +/*! @def tb_align_cpu + * + * align by cpu bytes + */ +#if TB_CPU_BIT64 +# define tb_align_cpu(x) tb_align8(x) +#else +# define tb_align_cpu(x) tb_align4(x) +#endif + +/// offsetof +#if defined(TB_COMPILER_IS_GCC) \ + && TB_COMPILER_VERSION_BE(4, 1) +# define tb_offsetof(s, m) (tb_size_t)__builtin_offsetof(s, m) +#else +# define tb_offsetof(s, m) (tb_size_t)&(((s const*)0)->m) +#endif + +/// container of +#define tb_container_of(s, m, p) ((s*)(((tb_byte_t*)(p)) - tb_offsetof(s, m))) + +/// memsizeof +#define tb_memsizeof(s, m) sizeof(((s const*)0)->m) + +/// memtailof +#define tb_memtailof(s, m) (tb_offsetof(s, m) + tb_memsizeof(s, m)) + +/// memdiffof: lm - rm +#define tb_memdiffof(s, lm, rm) (tb_memtailof(s, lm) - tb_memtailof(s, rm)) + +/// check the offset and size of member for struct or union +#define tb_memberof_eq(ls, lm, rs, rm) ((tb_offsetof(ls, lm) == tb_offsetof(rs, rm)) && (tb_memsizeof(ls, lm) == tb_memsizeof(rs, rm))) + +/// pointer to bool +#define tb_p2b(x) ((tb_bool_t)(tb_size_t)(x)) + +/// pointer to u8 +#define tb_p2u8(x) ((tb_uint8_t)(tb_size_t)(x)) + +/// pointer to u16 +#define tb_p2u16(x) ((tb_uint16_t)(tb_size_t)(x)) + +/// pointer to u32 +#define tb_p2u32(x) ((tb_uint32_t)(tb_size_t)(x)) + +/// pointer to u64 +#define tb_p2u64(x) ((tb_uint64_t)(tb_size_t)(x)) + +/// pointer to s8 +#define tb_p2s8(x) ((tb_sint8_t)(tb_long_t)(x)) + +/// pointer to s16 +#define tb_p2s16(x) ((tb_sint16_t)(tb_long_t)(x)) + +/// pointer to s32 +#define tb_p2s32(x) ((tb_sint32_t)(tb_long_t)(x)) + +/// pointer to s64 +#define tb_p2s64(x) ((tb_sint64_t)(tb_long_t)(x)) + +/// bool to pointer +#define tb_b2p(x) ((tb_pointer_t)(tb_size_t)(x)) + +/// unsigned integer to pointer +#define tb_u2p(x) ((tb_pointer_t)(tb_size_t)(x)) + +/// integer to pointer +#define tb_i2p(x) ((tb_pointer_t)(tb_long_t)(x)) + +/// swap +#define tb_swap(t, l, r) do { t __p = (r); (r) = (l); (l) = __p; } while (0) + + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/version.h b/core/pkg/tbox.pkg/inc/tbox/prefix/version.h index e03f280e6..c71d31227 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/version.h +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/version.h @@ -59,3 +59,64 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file version.h + * + */ +#ifndef TB_PREFIX_VERSION_H +#define TB_PREFIX_VERSION_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "config.h" +#include "keyword.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the major version +#define TB_VERSION_MAJOR TB_CONFIG_VERSION_MAJOR + +/// the minor version +#define TB_VERSION_MINOR TB_CONFIG_VERSION_MINOR + +/// the alter version +#define TB_VERSION_ALTER TB_CONFIG_VERSION_ALTER + +/// the build version +#define TB_VERSION_BUILD TB_CONFIG_VERSION_BUILD + +/// the build version string +#define TB_VERSION_BUILD_STRING __tb_mstring_ex__(TB_CONFIG_VERSION_BUILD) + +/// the version string +#define TB_VERSION_STRING __tb_mstrcat6__("tbox_", __tb_mstring_ex__(__tb_mconcat8_ex__(v, TB_VERSION_MAJOR, _, TB_VERSION_MINOR, _, TB_VERSION_ALTER, _, TB_CONFIG_VERSION_BUILD)), "_", TB_ARCH_VERSION_STRING, " by ", TB_COMPILER_VERSION_STRING) + +/// the short version string +#define TB_VERSION_SHORT_STRING __tb_mstrcat__("tbox_", __tb_mstring_ex__(__tb_mconcat8_ex__(v, TB_VERSION_MAJOR, _, TB_VERSION_MINOR, _, TB_VERSION_ALTER, _, TB_CONFIG_VERSION_BUILD))) + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/x64/prefix.S b/core/pkg/tbox.pkg/inc/tbox/prefix/x64/prefix.S index 22c80181d..ec0a14b5e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/x64/prefix.S +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/x64/prefix.S @@ -27,3 +27,32 @@ * includes */ #include "../x86/prefix.S" +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.S + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../x86/prefix.S" diff --git a/core/pkg/tbox.pkg/inc/tbox/prefix/x86/prefix.S b/core/pkg/tbox.pkg/inc/tbox/prefix/x86/prefix.S index a351181cf..c61d3620e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/prefix/x86/prefix.S +++ b/core/pkg/tbox.pkg/inc/tbox/prefix/x86/prefix.S @@ -54,3 +54,59 @@ _##name: // end function #define endfunc +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.S + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/*! function + * + * @code + function(func_xxxx) + ... + endfunc + @endcode + */ +#ifdef TB_ARCH_ELF +# define function(name) \ + .text ;\ + .align TB_CPU_BITBYTE ; \ + .global name ;\ + .type name, %function; \ + .hidden name; \ +name: +#else +# define function(name) \ + .text ;\ + .align TB_CPU_BITBYTE ; \ + .global _##name ;\ +_##name: +#endif + +// end function +#define endfunc + diff --git a/core/pkg/tbox.pkg/inc/tbox/regex/prefix.h b/core/pkg/tbox.pkg/inc/tbox/regex/prefix.h index aa3435a69..f8249ffa0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/regex/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/regex/prefix.h @@ -35,3 +35,40 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * @ingroup regex + * + */ +#ifndef TB_REGEX_PREFIX_H +#define TB_REGEX_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../libc/libc.h" +#include "../container/container.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/regex/regex.h b/core/pkg/tbox.pkg/inc/tbox/regex/regex.h index 973601c7d..218c44053 100644 --- a/core/pkg/tbox.pkg/inc/tbox/regex/regex.h +++ b/core/pkg/tbox.pkg/inc/tbox/regex/regex.h @@ -420,3 +420,425 @@ tb_char_t const* tb_regex_replace_done_simple(tb_char_t const* pattern, t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file regex.h + * @defgroup regex + */ +#ifndef TB_REGEX_H +#define TB_REGEX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the regex ref type +typedef __tb_typeref__(regex); + +/// the regex match type +typedef struct _tb_regex_match_t +{ + /// the c-string data + tb_char_t const* cstr; + + /// the c-string size + tb_size_t size; + + /// the matched start position + tb_size_t start; + +}tb_regex_match_t, *tb_regex_match_ref_t; + +/// the regex mode enum +typedef enum __tb_regex_mode_e +{ + TB_REGEX_MODE_NONE = 0 //!< the default mode +, TB_REGEX_MODE_CASELESS = 1 //!< do caseless matching +, TB_REGEX_MODE_MULTILINE = 2 //!< ^ and $ match newlines within data +, TB_REGEX_MODE_GLOBAL = 4 //!< global replace all + +}tb_regex_mode_e; + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init regex + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * + * @return the regex + */ +tb_regex_ref_t tb_regex_init(tb_char_t const* pattern, tb_size_t mode); + +/*! exit regex + * + * @param regex the regex + */ +tb_void_t tb_regex_exit(tb_regex_ref_t regex); + +/*! match the given c-string and size by regex + * + * @param regex the regex + * @param cstr the c-string data + * @param size the c-string size + * @param start the start position + * @param plength the matched length pointer, do not get it if be null + * @param presults the results pointer, only match it if be null + * + * @return the matched position, not match: -1 + */ +tb_long_t tb_regex_match(tb_regex_ref_t regex, tb_char_t const* cstr, tb_size_t size, tb_size_t start, tb_size_t* plength, tb_vector_ref_t* presults); + +/*! match the given c-string by regex + * + * @code + + // init regex + tb_regex_ref_t regex = tb_regex_init("\\w+", 0); + if (regex) + { + // match single + // + // results: "hello" + // + tb_vector_ref_t results = tb_null; + if (tb_regex_match_cstr(regex, "hello world", 0, tb_null, &results) >= 0 && results) + { + // show results + tb_for_all_if (tb_regex_match_ref_t, entry, results, entry) + { + // trace + tb_trace_i("cstr: %s, size: %lu, start: %lu", entry->cstr, entry->size, entry->start); + } + } + + // match global + // + // results: "hello" + // results: "world" + // + tb_long_t start = 0; + tb_size_t length = 0; + tb_vector_ref_t results = tb_null; + while ((start = tb_regex_match_cstr(regex, "hello world", start + length, &length, &results)) >= 0 && results) + { + // show results + tb_for_all_if (tb_regex_match_ref_t, entry, results, entry) + { + // trace + tb_trace_i("cstr: %s, size: %lu, start: %lu", entry->cstr, entry->size, entry->start); + } + } + + // exit regex + tb_regex_exit(regex); + } + * @endcode + * + * @param regex the regex + * @param cstr the c-string + * @param start the start position + * @param plength the matched length pointer, do not get it if be null + * @param presults the results pointer, only match it if be null + * + * @return the matched position, not match: -1 + */ +tb_long_t tb_regex_match_cstr(tb_regex_ref_t regex, tb_char_t const* cstr, tb_size_t start, tb_size_t* plength, tb_vector_ref_t* presults); + +/*! simply match the given c-string by regex + * + * @note only supports single match + * + * @code + + // init regex + tb_regex_ref_t regex = tb_regex_init("\\w+", 0); + if (regex) + { + // match single + // + // results: "hello" + // + tb_vector_ref_t results = tb_regex_match_simple(regex, "hello world"); + if (results) + { + // show results + tb_for_all_if (tb_regex_match_ref_t, entry, results, entry) + { + // trace + tb_trace_i("cstr: %s, size: %lu, start: %lu", entry->cstr, entry->size, entry->start); + } + } + + // exit regex + tb_regex_exit(regex); + } + * @endcode + * + * @param regex the regex + * @param cstr the c-string + * + * @return the matched results + */ +tb_vector_ref_t tb_regex_match_simple(tb_regex_ref_t regex, tb_char_t const* cstr); + +/*! replace the given c-string and size by regex + * + * @param regex the regex + * @param cstr the c-string data + * @param size the c-string size + * @param start the start position + * @param replace_cstr the replacement c-string data + * @param replace_size the replacement c-string size + * @param plength the result c-string length pointer, do not get it if be null + * + * @return the result c-string + */ +tb_char_t const* tb_regex_replace(tb_regex_ref_t regex, tb_char_t const* cstr, tb_size_t size, tb_size_t start, tb_char_t const* replace_cstr, tb_size_t replace_size, tb_size_t* plength); + +/*! replace the given c-string by regex + * + * @param regex the regex + * @param cstr the c-string data + * @param start the start position + * @param replace_cstr the replacement c-string data + * @param plength the result c-string length pointer, do not get it if be null + * + * @return the result c-string + */ +tb_char_t const* tb_regex_replace_cstr(tb_regex_ref_t regex, tb_char_t const* cstr, tb_size_t start, tb_char_t const* replace_cstr, tb_size_t* plength); + +/*! simply replace the given c-string by regex + * + * @code + + // init regex + tb_regex_ref_t regex = tb_regex_init("\\w+", 0); + if (regex) + { + // match single + // + // results: "hi world" + // + tb_char_t const* results = tb_regex_replace_simple(regex, "hello world", "hi"); + if (results) + { + // trace + tb_trace_i("results: %s", results); + } + + // exit regex + tb_regex_exit(regex); + } + * @endcode + * + * @param regex the regex + * @param cstr the c-string data + * @param replace_cstr the replacement c-string data + * + * @return the result c-string + */ +tb_char_t const* tb_regex_replace_simple(tb_regex_ref_t regex, tb_char_t const* cstr, tb_char_t const* replace_cstr); + +/*! match the given c-string and size by the given regex pattern + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * @param cstr the c-string data + * @param size the c-string size + * @param start the start position + * @param plength the matched length pointer, do not get it if be null + * @param presults the results pointer, only match it if be null + * @note we need exit it manually + * + * @return the matched position, not match: -1 + */ +tb_long_t tb_regex_match_done(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_size_t size, tb_size_t start, tb_size_t* plength, tb_vector_ref_t* presults); + +/*! match the given c-string by the given regex pattern + * + * @code + + // match single + // + // results: "hello" + // + tb_vector_ref_t results = tb_null; + if (tb_regex_match_done_cstr("\\w+", 0, "hello world", 0, tb_null, &results) >= 0 && results) + { + // show results + tb_for_all_if (tb_regex_match_ref_t, entry, results, entry) + { + // trace + tb_trace_i("cstr: %s, size: %lu, start: %lu", entry->cstr, entry->size, entry->start); + } + + // exit results + tb_vector_exit(results); + } + + // match global + // + // results: "hello" + // results: "world" + // + tb_long_t start = 0; + tb_size_t length = 0; + tb_vector_ref_t results = tb_null; + while ((start = tb_regex_match_done_cstr("\\w+", 0, "hello world", start + length, &length, &results)) >= 0 && results) + { + // show results + tb_for_all_if (tb_regex_match_ref_t, entry, results, entry) + { + // trace + tb_trace_i("cstr: %s, size: %lu, start: %lu", entry->cstr, entry->size, entry->start); + } + + // exit results + tb_vector_exit(results); + } + + * @endcode + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * @param cstr the c-string data + * @param start the start position + * @param plength the matched length pointer, do not get it if be null + * @param presults the results pointer, only match it if be null + * @note we need exit it manually + * + * @return the matched position, not match: -1 + */ +tb_long_t tb_regex_match_done_cstr(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_size_t start, tb_size_t* plength, tb_vector_ref_t* presults); + +/*! simply match the given c-string by the given regex pattern + * + * @note only supports single match + * + * @code + + // match single + // + // results: "hello" + // + tb_vector_ref_t results = tb_regex_match_done_simple("\\w+", 0, "hello world"); + if (results) + { + // show results + tb_for_all_if (tb_regex_match_ref_t, entry, results, entry) + { + // trace + tb_trace_i("cstr: %s, size: %lu, start: %lu", entry->cstr, entry->size, entry->start); + } + + // exit results + tb_vector_exit(results); + } + + * @endcode + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * @param cstr the c-string data + * + * @return the matched results, we need exit it manually + */ +tb_vector_ref_t tb_regex_match_done_simple(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr); + +/*! replace the given c-string and size by the given regex pattern + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * @param cstr the c-string data + * @param size the c-string size + * @param start the start position + * @param replace_cstr the replacement c-string data + * @param replace_size the replacement c-string size + * @param plength the result c-string length pointer, do not get it if be null + * + * @return the result c-string + */ +tb_char_t const* tb_regex_replace_done(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_size_t size, tb_size_t start, tb_char_t const* replace_cstr, tb_size_t replace_size, tb_size_t* plength); + +/*! replace the given c-string by the given regex pattern + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * @param cstr the c-string data + * @param start the start position + * @param replace_cstr the replacement c-string data + * @param plength the result c-string length pointer, do not get it if be null + * + * @return the result c-string + */ +tb_char_t const* tb_regex_replace_done_cstr(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_size_t start, tb_char_t const* replace_cstr, tb_size_t* plength); + +/*! simply replace the given c-string by the given regex pattern + * + * @code + + // replace single + // + // results: "hi world" + // + tb_char_t const* results = tb_regex_replace_done_simple("\\w+", 0, "hello world", "hi"); + if (results) + { + // trace + tb_trace_i("results: %s", results); + + // exit results + tb_free(results); + } + + * @endcode + * + * @param pattern the regex pattern + * @param mode the regex mode, uses the default mode if be zero + * @param cstr the c-string data + * @param replace_cstr the replacement c-string data + * + * @return the result c-string, @note we need free it manually + */ +tb_char_t const* tb_regex_replace_done_simple(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_char_t const* replace_cstr); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_stream.h b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_stream.h index e6b2b3309..1baecbcc0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_stream.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_stream.h @@ -724,3 +724,729 @@ tb_size_t tb_async_stream_line(tb_async_stream_ref_t stream); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file async_stream.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_ASYNC_STREAM_H +#define TB_STREAM_ASYNC_STREAM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../../asio/deprecated/asio.h" +#include "../../libc/libc.h" +#include "../../network/url.h" +#include "../../memory/memory.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// open +#define tb_async_stream_open(stream, func, priv) tb_async_stream_open_(stream, func, priv __tb_debug_vals__) + +/// clos +#define tb_async_stream_clos(stream, func, priv) tb_async_stream_clos_(stream, func, priv __tb_debug_vals__) + +/// read +#define tb_async_stream_read(stream, size, func, priv) tb_async_stream_read_(stream, size, func, priv __tb_debug_vals__) + +/// writ +#define tb_async_stream_writ(stream, data, size, func, priv) tb_async_stream_writ_(stream, data, size, func, priv __tb_debug_vals__) + +/// seek +#define tb_async_stream_seek(stream, offset, func, priv) tb_async_stream_seek_(stream, offset, func, priv __tb_debug_vals__) + +/// sync +#define tb_async_stream_sync(stream, bclosing, func, priv) tb_async_stream_sync_(stream, bclosing, func, priv __tb_debug_vals__) + +/// task +#define tb_async_stream_task(stream, delay, func, priv) tb_async_stream_task_(stream, delay, func, priv __tb_debug_vals__) + +/// open and read +#define tb_async_stream_open_read(stream, size, func, priv) tb_async_stream_open_read_(stream, size, func, priv __tb_debug_vals__) + +/// open and writ +#define tb_async_stream_open_writ(stream, data, size, func, priv) tb_async_stream_open_writ_(stream, data, size, func, priv __tb_debug_vals__) + +/// open and seek +#define tb_async_stream_open_seek(stream, offset, func, priv) tb_async_stream_open_seek_(stream, offset, func, priv __tb_debug_vals__) + +/// read after delay +#define tb_async_stream_read_after(stream, delay, size, func, priv) tb_async_stream_read_after_(stream, delay, size, func, priv __tb_debug_vals__) + +/// writ after delay +#define tb_async_stream_writ_after(stream, delay, data, size, func, priv) tb_async_stream_writ_after_(stream, delay, data, size, func, priv __tb_debug_vals__) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the stream open func type + * + * @param stream the stream + * @param state the state + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_async_stream_open_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_cpointer_t priv); + +/*! the stream clos func type + * + * @param stream the stream + * @param state the state + * @param priv the func private data + */ +typedef tb_void_t (*tb_async_stream_clos_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_cpointer_t priv); + +/*! the stream read func type + * + * @param stream the stream + * @param state the state + * @param data the readed data + * @param real the real size, maybe zero + * @param size the need size + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it, but not break aicp + */ +typedef tb_bool_t (*tb_async_stream_read_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_byte_t const* data, tb_size_t real, tb_size_t size, tb_cpointer_t priv); + +/*! the stream writ func type + * + * @param stream the stream + * @param state the state + * @param data the writed data + * @param real the real size, maybe zero + * @param size the need size + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it, but not break aicp + */ +typedef tb_bool_t (*tb_async_stream_writ_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_byte_t const* data, tb_size_t real, tb_size_t size, tb_cpointer_t priv); + +/*! the stream seek func type + * + * @param stream the stream + * @param state the state + * @param offset the real offset + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_async_stream_seek_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_hize_t offset, tb_cpointer_t priv); + +/*! the stream sync func type + * + * @param stream the stream + * @param state the state + * @param bclosing is closing? + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_async_stream_sync_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_bool_t bclosing, tb_cpointer_t priv); + +/*! the stream task func type + * + * @param stream the stream + * @param state the state + * @param priv the func private data + * + * @return tb_true: ok, tb_false: error, but not break aicp + */ +typedef tb_bool_t (*tb_async_stream_task_func_t)(tb_async_stream_ref_t stream, tb_size_t state, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init async stream + * + * @param aicp the aicp + * @param type the stream type + * @param type_size the stream type size + * @param rcache the read cache size + * @param wcache the writ cache size + * @param open the stream impl func: open + * @param clos the stream impl func: clos + * @param exit the stream impl func: exit, optional + * @param ctrl the stream impl func: ctrl + * @param wait the stream impl func: wait + * @param read the stream impl func: read + * @param writ the stream impl func: writ + * @param seek the stream impl func: seek, optional + * @param sync the stream impl func: sync, optional + * @param kill the stream impl func: kill, optional + * + * @return the stream + * + * @code + // the custom xxxx async stream type + typedef struct __tb_async_stream_xxxx_impl_t + { + // the xxxx data + tb_handle_t xxxx; + + }tb_async_stream_xxxx_impl_t; + + static tb_bool_t tb_async_stream_xxxx_impl_open(tb_async_stream_ref_t stream, tb_async_stream_open_func_t func, tb_cpointer_t priv) + { + // check + tb_async_stream_xxxx_impl_t* impl = (tb_async_stream_xxxx_impl_t*)stream; + tb_assert_and_check_return_val(impl, tb_false); + + // ok + return tb_true; + } + static tb_bool_t tb_async_stream_xxxx_impl_clos(tb_async_stream_ref_t stream, tb_async_stream_clos_func_t, tb_cpointer_t priv) + { + // check + tb_async_stream_xxxx_impl_t* impl = (tb_async_stream_xxxx_impl_t*)stream; + tb_assert_and_check_return_val(impl, tb_false); + + // ok + return tb_true; + } + + // define other xxxx async stream func + // ... + + // init async stream + tb_async_stream_ref_t stream = tb_async_stream_init( tb_aicp() + , TB_STREAM_TYPE_XXXX + , sizeof(tb_async_stream_xxxx_impl_t) + , 0 + , 0 + , tb_async_stream_xxxx_impl_open_try + , tb_async_stream_xxxx_impl_clos_try + , tb_async_stream_xxxx_impl_open + , tb_async_stream_xxxx_impl_clos + , tb_async_stream_xxxx_impl_exit + , tb_async_stream_xxxx_impl_kill + , tb_async_stream_xxxx_impl_ctrl + , tb_async_stream_xxxx_impl_read + , tb_async_stream_xxxx_impl_writ + , tb_async_stream_xxxx_impl_seek + , tb_async_stream_xxxx_impl_sync + , tb_async_stream_xxxx_impl_task + ); + + // using async stream + // ... + + * @endcode + */ +tb_async_stream_ref_t tb_async_stream_init( tb_aicp_ref_t aicp + , tb_size_t type + , tb_size_t type_size + , tb_size_t rcache + , tb_size_t wcache + , tb_bool_t (*open_try)(tb_async_stream_ref_t stream) + , tb_bool_t (*clos_try)(tb_async_stream_ref_t stream) + , tb_bool_t (*open)(tb_async_stream_ref_t stream, tb_async_stream_open_func_t func, tb_cpointer_t priv) + , tb_bool_t (*clos)(tb_async_stream_ref_t stream, tb_async_stream_clos_func_t func, tb_cpointer_t priv) + , tb_bool_t (*exit)(tb_async_stream_ref_t stream) + , tb_void_t (*kill)(tb_async_stream_ref_t stream) + , tb_bool_t (*ctrl)(tb_async_stream_ref_t stream, tb_size_t ctrl, tb_va_list_t args) + , tb_bool_t (*read)(tb_async_stream_ref_t stream, tb_size_t delay, tb_byte_t* data, tb_size_t size, tb_async_stream_read_func_t func, tb_cpointer_t priv) + , tb_bool_t (*writ)(tb_async_stream_ref_t stream, tb_size_t delay, tb_byte_t const* data, tb_size_t size, tb_async_stream_writ_func_t func, tb_cpointer_t priv) + , tb_bool_t (*seek)(tb_async_stream_ref_t stream, tb_hize_t offset, tb_async_stream_seek_func_t func, tb_cpointer_t priv) + , tb_bool_t (*sync)(tb_async_stream_ref_t stream, tb_bool_t bclosing, tb_async_stream_sync_func_t func, tb_cpointer_t priv) + , tb_bool_t (*task)(tb_async_stream_ref_t stream, tb_size_t delay, tb_async_stream_task_func_t func, tb_cpointer_t priv)); + +/*! init data stream + * + * @param aicp the aicp, using the default aicp if be null + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_data(tb_aicp_ref_t aicp); + +/*! init file stream + * + * @param aicp the aicp, using the default aicp if be null + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_file(tb_aicp_ref_t aicp); + +/*! init sock stream + * + * @param aicp the aicp, using the default aicp if be null + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_sock(tb_aicp_ref_t aicp); + +/*! init http stream + * + * @param aicp the aicp, using the default aicp if be null + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_http(tb_aicp_ref_t aicp); + +/*! init filter stream + * + * @param aicp the aicp, using the default aicp if be null + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_filter(tb_aicp_ref_t aicp); + +/*! exit stream + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_exit(tb_async_stream_ref_t stream); + +/*! init stream from url + * + * @param aicp the aicp, using the default aicp if be null + * @param url the url + * <pre> + * data://base64 + * file://path or unix path: e.g. /root/xxxx/file + * sock://host:port?tcp= + * sock://host:port?udp= + * socks://host:port + * http://host:port/path?arg0=&arg1=... + * https://host:port/path?arg0=&arg1=... + * </pre> + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_from_url(tb_aicp_ref_t aicp, tb_char_t const* url); + +/*! init stream from data + * + * @param aicp the aicp, using the default aicp if be null + * @param data the data + * @param size the size + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_from_data(tb_aicp_ref_t aicp, tb_byte_t const* data, tb_size_t size); + +/*! init stream from file + * + * @param aicp the aicp, using the default aicp if be null + * @param path the file path + * @param mode the file mode, using the default ro mode if zero + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_from_file(tb_aicp_ref_t aicp, tb_char_t const* path, tb_size_t mode); + +/*! init stream from sock + * + * @param aicp the aicp, using the default aicp if be null + * @param host the host + * @param port the port + * @param type the socket type, tcp or udp + * @param bssl enable ssl? + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_from_sock(tb_aicp_ref_t aicp, tb_char_t const* host, tb_uint16_t port, tb_size_t type, tb_bool_t bssl); + +/*! init stream from http or https + * + * @param aicp the aicp, using the default aicp if be null + * @param host the host + * @param port the port + * @param path the path + * @param bssl enable ssl? + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_from_http(tb_aicp_ref_t aicp, tb_char_t const* host, tb_uint16_t port, tb_char_t const* path, tb_bool_t bssl); + +/*! init filter stream from null + * + * @param stream the stream + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_filter_from_null(tb_async_stream_ref_t stream); + +/*! init filter stream from zip + * + * @param stream the stream + * @param algo the zip algorithm + * @param action the zip action + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_filter_from_zip(tb_async_stream_ref_t stream, tb_size_t algo, tb_size_t action); + +/*! init filter stream from cache + * + * @param stream the stream + * @param size the initial cache size, using the default size if be zero + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_filter_from_cache(tb_async_stream_ref_t stream, tb_size_t size); + +/*! init filter stream from charset + * + * @param stream the stream + * @param fr the from charset + * @param to the to charset + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_filter_from_charset(tb_async_stream_ref_t stream, tb_size_t fr, tb_size_t to); + +/*! init filter stream from chunked + * + * @param stream the stream + * @param dechunked decode the chunked data? + * + * @return the stream + */ +tb_async_stream_ref_t tb_async_stream_init_filter_from_chunked(tb_async_stream_ref_t stream, tb_bool_t dechunked); + +/*! the stream url + * + * @param stream the stream + * + * @return the stream url + */ +tb_url_ref_t tb_async_stream_url(tb_async_stream_ref_t stream); + +/*! the stream type + * + * @param stream the stream + * + * @return the stream type + */ +tb_size_t tb_async_stream_type(tb_async_stream_ref_t stream); + +/*! the stream size and not seeking it + * + * @param stream the stream + * + * @return the stream size, no size: -1, empty or error: 0 + */ +tb_hong_t tb_async_stream_size(tb_async_stream_ref_t stream); + +/*! the stream left size and not seeking it + * + * @param stream the stream + * + * @return the stream left size, no size: infinity, empty or end: 0 + */ +tb_hize_t tb_async_stream_left(tb_async_stream_ref_t stream); + +/*! the stream is end? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_beof(tb_async_stream_ref_t stream); + +/*! the stream offset + * + * the offset is read + writ and using seek for modifying it if size != -1, .e.g: data, file, .. + * the offset is calculated from the last read/writ and not seeking it if size == -1, .e.g: sock, filter, .. + * + * @param stream the stream + * + * @return the stream offset + */ +tb_hize_t tb_async_stream_offset(tb_async_stream_ref_t stream); + +/*! is opened? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_is_opened(tb_async_stream_ref_t stream); + +/*! is closed? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_is_closed(tb_async_stream_ref_t stream); + +/*! is killed? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_is_killed(tb_async_stream_ref_t stream); + +/*! the stream timeout + * + * @param stream the stream + * + * @return the stream timeout + */ +tb_long_t tb_async_stream_timeout(tb_async_stream_ref_t stream); + +/*! ctrl stream + * + * @param stream the stream + * @param ctrl the ctrl code + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_ctrl(tb_async_stream_ref_t stream, tb_size_t ctrl, ...); + +/*! ctrl stream with arguments + * + * @param stream the stream + * @param ctrl the ctrl code + * @param args the ctrl args + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_ctrl_with_args(tb_async_stream_ref_t stream, tb_size_t ctrl, tb_va_list_t args); + +/*! kill stream + * + * @param stream the stream + */ +tb_void_t tb_async_stream_kill(tb_async_stream_ref_t stream); + +/*! try opening the stream for stream: file, filter, ... + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_open_try(tb_async_stream_ref_t stream); + +/*! open the stream + * + * @param stream the stream + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_open_(tb_async_stream_ref_t stream, tb_async_stream_open_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! try closing the stream for stream: file, filter, or closed stream... + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_clos_try(tb_async_stream_ref_t stream); + +/*! close the stream + * + * @param stream the stream + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_clos_(tb_async_stream_ref_t stream, tb_async_stream_clos_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! read the stream + * + * @param stream the stream + * @param size the read size, using the default size if be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_read_(tb_async_stream_ref_t stream, tb_size_t size, tb_async_stream_read_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! writ the stream + * + * @param stream the stream + * @param data the data + * @param size the size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_writ_(tb_async_stream_ref_t stream, tb_byte_t const* data, tb_size_t size, tb_async_stream_writ_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! seek the stream + * + * @param stream the stream + * @param offset the offset + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_seek_(tb_async_stream_ref_t stream, tb_hize_t offset, tb_async_stream_seek_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! sync the stream + * + * @param stream the stream + * @param bclosing sync the tail data for closing + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_sync_(tb_async_stream_ref_t stream, tb_bool_t bclosing, tb_async_stream_sync_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! task the stream + * + * @param stream the stream + * @param delay the delay time, ms + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_task_(tb_async_stream_ref_t stream, tb_size_t delay, tb_async_stream_task_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! open and read the stream, open it first if not opened + * + * @param stream the stream + * @param size the read size, using the default size if be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_read_(tb_async_stream_ref_t stream, tb_size_t size, tb_async_stream_read_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! open and read the stream, open it first if not opened + * + * @param stream the stream + * @param size the size, using the default size be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_open_read_(tb_async_stream_ref_t stream, tb_size_t size, tb_async_stream_read_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! open and writ the stream, open it first if not opened + * + * @param stream the stream + * @param data the data + * @param size the size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_open_writ_(tb_async_stream_ref_t stream, tb_byte_t const* data, tb_size_t size, tb_async_stream_writ_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! open and seek the stream, open it first if not opened + * + * @param stream the stream + * @param offset the offset + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_open_seek_(tb_async_stream_ref_t stream, tb_hize_t offset, tb_async_stream_seek_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! read the stream after the delay time + * + * @param stream the stream + * @param delay the delay time, ms + * @param size the read size, using the default size if be zero + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_read_after_(tb_async_stream_ref_t stream, tb_size_t delay, tb_size_t size, tb_async_stream_read_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! writ the stream after the delay time + * + * @param stream the stream + * @param delay the delay time, ms + * @param data the data + * @param size the size + * @param func the func + * @param priv the func data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_stream_writ_after_(tb_async_stream_ref_t stream, tb_size_t delay, tb_byte_t const* data, tb_size_t size, tb_async_stream_writ_func_t func, tb_cpointer_t priv __tb_debug_decl__); + +/*! the stream aicp + * + * @param stream the stream + * + * @return the stream aicp + */ +tb_aicp_ref_t tb_async_stream_aicp(tb_async_stream_ref_t stream); + +#ifdef __tb_debug__ +/*! the stream func name from post for debug + * + * @param stream the stream + * + * @return the stream func name + */ +tb_char_t const* tb_async_stream_func(tb_async_stream_ref_t stream); + +/*! the stream file name from post for debug + * + * @param stream the stream + * + * @return the stream file name + */ +tb_char_t const* tb_async_stream_file(tb_async_stream_ref_t stream); + +/*! the stream line number from post for debug + * + * @param stream the stream + * + * @return the stream line number + */ +tb_size_t tb_async_stream_line(tb_async_stream_ref_t stream); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_transfer.h b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_transfer.h index 3024d3762..3bf4544f4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_transfer.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/async_transfer.h @@ -321,3 +321,326 @@ tb_void_t tb_async_transfer_limitrate(tb_async_transfer_ref_t tran __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file async_transfer.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_ASYNC_TRANSFER_H +#define TB_STREAM_ASYNC_TRANSFER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "async_stream.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the async transfer open func type + * + * @param state the stream state + * @param offset the istream offset + * @param size the istream size, no size: -1 + * @param priv the func private data + * + * @return tb_true: ok, tb_false: break it + */ +typedef tb_bool_t (*tb_async_transfer_open_func_t)(tb_size_t state, tb_hize_t offset, tb_hong_t size, tb_cpointer_t priv); + +/*! the async transfer clos func type + * + * @param state the stream state + * @param priv the func private data + */ +typedef tb_void_t (*tb_async_transfer_clos_func_t)(tb_size_t state, tb_cpointer_t priv); + +/*! the async transfer ctrl func type + * + * @param istream the istream + * @param ostream the ostream + * @param priv the func private data + * + * @return tb_true: ok, tb_false: break it + */ +typedef tb_bool_t (*tb_async_transfer_ctrl_func_t)(tb_async_stream_ref_t istream, tb_async_stream_ref_t ostream, tb_cpointer_t priv); + +/*! the async transfer done func type + * + * @param state the stream state + * @param offset the istream offset + * @param size the istream size, no size: -1 + * @param save the saved size + * @param rate the current rate, bytes/s + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it + */ +typedef tb_bool_t (*tb_async_transfer_done_func_t)(tb_size_t state, tb_hize_t offset, tb_hong_t size, tb_hize_t save, tb_size_t rate, tb_cpointer_t priv); + +/// the async transfer ref type +typedef __tb_typeref__(async_transfer); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init async transfer + * + * @param aicp the aicp, using the default aicp if be null + * @param autoclosing auto closing it after finishing transfer + * + * @return the async transfer + */ +tb_async_transfer_ref_t tb_async_transfer_init(tb_aicp_ref_t aicp, tb_bool_t autoclosing); + +/*! init istream + * + * @param transfer the async transfer + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_init_istream(tb_async_transfer_ref_t transfer, tb_async_stream_ref_t stream); + +/*! init istream from url + * + * @param transfer the async transfer + * @param url the url + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_init_istream_from_url(tb_async_transfer_ref_t transfer, tb_char_t const* url); + +/*! init istream from data + * + * @param transfer the async transfer + * @param data the data + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_init_istream_from_data(tb_async_transfer_ref_t transfer, tb_byte_t const* data, tb_size_t size); + +/*! init ostream + * + * @param transfer the async transfer + * @param stream the stream + * + * @return the async transfer + */ +tb_bool_t tb_async_transfer_init_ostream(tb_async_transfer_ref_t transfer, tb_async_stream_ref_t stream); + +/*! init ostream from url + * + * @param transfer the async transfer + * @param url the url + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_init_ostream_from_url(tb_async_transfer_ref_t transfer, tb_char_t const* url); + +/*! init ostream from data + * + * @param transfer the async transfer + * @param data the data + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_init_ostream_from_data(tb_async_transfer_ref_t transfer, tb_byte_t* data, tb_size_t size); + +/*! ctrl istream + * + * @note must call it before opening + * + * @param transfer the async transfer + * @param ctrl the ctrl code, using the stream ctrl code + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_ctrl_istream(tb_async_transfer_ref_t transfer, tb_size_t ctrl, ...); + +/*! ctrl ostream + * + * @note must call it before opening + * + * @param transfer the async transfer + * @param ctrl the ctrl code, using the stream ctrl code + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_ctrl_ostream(tb_async_transfer_ref_t transfer, tb_size_t ctrl, ...); + +/*! ctrl transfer + * + * @param transfer the async transfer + * @param offset the start offset + * @param func the ctrl func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_ctrl(tb_async_transfer_ref_t transfer, tb_async_transfer_ctrl_func_t func, tb_cpointer_t priv); + +/*! open transfer + * + * @param transfer the async transfer + * @param offset the start offset + * @param func the open func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_open(tb_async_transfer_ref_t transfer, tb_hize_t offset, tb_async_transfer_open_func_t func, tb_cpointer_t priv); + +/*! clos transfer + * + * @param transfer the async transfer + * @param func the clos func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_clos(tb_async_transfer_ref_t transfer, tb_async_transfer_clos_func_t func, tb_cpointer_t priv); + +/*! try closing transfer + * + * @param transfer the async transfer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_clos_try(tb_async_transfer_ref_t transfer); + +/*! done transfer and will close it automaticly + * + * @param transfer the async transfer + * @param func the save func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_done(tb_async_transfer_ref_t transfer, tb_async_transfer_done_func_t func, tb_cpointer_t priv); + +/*! open and done transfer and will close it automaticly + * + * @code + * + static tb_bool_t tb_demo_transfer_done_func(tb_size_t state, tb_hize_t offset, tb_hong_t size, tb_hize_t save, tb_size_t rate, tb_cpointer_t priv) + { + // percent + tb_size_t percent = 0; + if (size > 0) percent = (offset * 100) / size; + else if (state == TB_STATE_OK) percent = 100; + + // trace + tb_trace_i("done: %llu bytes, rate: %lu bytes/s, percent: %lu%%, state: %s", save, rate, percent, tb_state_cstr(state)); + + // ok + return tb_true; + } + + // init transfer + tb_async_transfer_ref_t transfer = tb_async_transfer_init(tb_null); + if (transfer) + { + // init stream + tb_async_transfer_init_istream_from_url(transfer, url); + tb_async_transfer_init_ostream_from_data(transfer, data, size); + + // ctrl stream +// tb_async_transfer_ctrl_istream(transfer, TB_STREAM_CTRL_SET_TIMEOUT, 10000); + + // limit rate +// tb_async_transfer_limitrate(transfer, 256000); + + // open and done transfer + tb_async_transfer_open_done(transfer, 0, tb_demo_transfer_done_func, tb_null); + + // exit transfer + tb_async_transfer_exit(transfer); + } + * + * @endcode + * + * @param transfer the async transfer + * @param offset the start offset + * @param func the save func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_open_done(tb_async_transfer_ref_t transfer, tb_hize_t offset, tb_async_transfer_done_func_t func, tb_cpointer_t priv); + +/*! kill transfer + * + * @param transfer the async transfer + */ +tb_void_t tb_async_transfer_kill(tb_async_transfer_ref_t transfer); + +/*! exit transfer + * + * @note will wait transfer closed and cannot be called in the callback func + * + * @param transfer the async transfer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_exit(tb_async_transfer_ref_t transfer); + +/*! pause transfer + * + * the save func state will return TB_STATE_PAUSED + * + * @param transfer the async transfer + */ +tb_void_t tb_async_transfer_pause(tb_async_transfer_ref_t transfer); + +/*! resume transfer + * + * @param transfer the async transfer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_async_transfer_resume(tb_async_transfer_ref_t transfer); + +/*! limit transfer rate + * + * @param transfer the async transfer + * @param rate the trasfer rate and no limit if 0, bytes/s + */ +tb_void_t tb_async_transfer_limitrate(tb_async_transfer_ref_t transfer, tb_size_t rate); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/deprecated.h b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/deprecated.h index f7d91682a..bd11a03c2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/deprecated.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/deprecated.h @@ -34,3 +34,39 @@ #include "transfer_pool.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file deprecated.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_DEPRECATED_H +#define TB_STREAM_DEPRECATED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "async_stream.h" +#include "transfer_pool.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/prefix.h b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/prefix.h index 1c559c09b..766fb81c0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/prefix.h @@ -31,3 +31,36 @@ #include "../prefix.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_STREAM_DEPRECATED_PREFIX_H +#define TB_STREAM_DEPRECATED_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/transfer_pool.h b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/transfer_pool.h index fbdda8c5a..8b33f2651 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/transfer_pool.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/deprecated/transfer_pool.h @@ -127,3 +127,132 @@ tb_bool_t tb_transfer_pool_done(tb_transfer_pool_ref_t pool, tb_ch __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file transfer_pool.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_TRANSFER_POOL_H +#define TB_STREAM_TRANSFER_POOL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../transfer.h" +#include "async_stream.h" +#include "async_transfer.h" +#include "../../asio/deprecated/asio.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the transfer pool ref type +typedef __tb_typeref__(transfer_pool); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the transfer pool instance + * + * @return the transfer pool instance + */ +tb_transfer_pool_ref_t tb_transfer_pool(tb_noarg_t); + +/*! init transfer pool + * + * @param aicp the aicp, using the default aicp if be null + * + * @return the transfer pool + */ +tb_transfer_pool_ref_t tb_transfer_pool_init(tb_aicp_ref_t aicp); + +/*! exit transfer pool + * + * @param pool the transfer pool + * + * @return tb_true or tb_false + */ +tb_bool_t tb_transfer_pool_exit(tb_transfer_pool_ref_t pool); + +/*! kill transfer pool + * + * @param pool the transfer pool + */ +tb_void_t tb_transfer_pool_kill(tb_transfer_pool_ref_t pool); + +/*! kill all working tasks + * + * @param pool the transfer pool handle + */ +tb_void_t tb_transfer_pool_kill_all(tb_transfer_pool_ref_t pool); + +/*! wait all working tasks + * + * @param pool the transfer pool handle + * @param timeout the timeout + * + * @return ok: 1, timeout: 0, error: -1 + */ +tb_long_t tb_transfer_pool_wait_all(tb_transfer_pool_ref_t pool, tb_long_t timeout); + +/*! the transfer pool size + * + * @param pool the transfer pool + */ +tb_size_t tb_transfer_pool_size(tb_transfer_pool_ref_t pool); + +/*! the transfer pool maxn + * + * @param pool the transfer pool + */ +tb_size_t tb_transfer_pool_maxn(tb_transfer_pool_ref_t pool); + +/*! done transfer from iurl to ourl + * + * @param pool the transfer pool + * @param iurl the input url + * @param ourl the output url + * @param offset the offset + * @param rate the limited rate, not limit if be zero + * @param done the done func + * @param ctrl the ctrl func + * @param priv the func private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_transfer_pool_done(tb_transfer_pool_ref_t pool, tb_char_t const* iurl, tb_char_t const* ourl, tb_hize_t offset, tb_size_t rate, tb_async_transfer_done_func_t done, tb_async_transfer_ctrl_func_t ctrl, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/filter.h b/core/pkg/tbox.pkg/inc/tbox/stream/filter.h index d2c3a77ef..9d008095f 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/filter.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/filter.h @@ -188,3 +188,193 @@ tb_long_t tb_filter_spak(tb_filter_ref_t filter, tb_byte_t const* __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file filter.h + * @defgroup stream + * + */ +#ifndef TB_FILTER_H +#define TB_FILTER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the filter ctrl +#define TB_FILTER_CTRL(type, ctrl) (((type) << 16) | (ctrl)) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the filter type enum +typedef enum __tb_filter_type_e +{ + TB_FILTER_TYPE_NONE = 0 +, TB_FILTER_TYPE_ZIP = 1 +, TB_FILTER_TYPE_CACHE = 2 +, TB_FILTER_TYPE_CHARSET = 3 +, TB_FILTER_TYPE_CHUNKED = 4 + +}tb_filter_type_e; + +/// the filter ctrl enum +typedef enum __tb_filter_ctrl_e +{ + TB_FILTER_CTRL_NONE = 0 + +, TB_FILTER_CTRL_ZIP_GET_ALGO = TB_FILTER_CTRL(TB_FILTER_TYPE_ZIP, 1) +, TB_FILTER_CTRL_ZIP_GET_ACTION = TB_FILTER_CTRL(TB_FILTER_TYPE_ZIP, 2) +, TB_FILTER_CTRL_ZIP_SET_ALGO = TB_FILTER_CTRL(TB_FILTER_TYPE_ZIP, 3) +, TB_FILTER_CTRL_ZIP_SET_ACTION = TB_FILTER_CTRL(TB_FILTER_TYPE_ZIP, 4) + +, TB_FILTER_CTRL_CHARSET_GET_FTYPE = TB_FILTER_CTRL(TB_FILTER_TYPE_CHARSET, 1) +, TB_FILTER_CTRL_CHARSET_GET_TTYPE = TB_FILTER_CTRL(TB_FILTER_TYPE_CHARSET, 2) +, TB_FILTER_CTRL_CHARSET_SET_FTYPE = TB_FILTER_CTRL(TB_FILTER_TYPE_CHARSET, 3) +, TB_FILTER_CTRL_CHARSET_SET_TTYPE = TB_FILTER_CTRL(TB_FILTER_TYPE_CHARSET, 4) + +}tb_filter_ctrl_e; + +/// the filter ref type +typedef __tb_typeref__(filter); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init filter from zip + * + * @param algo the zip algorithm + * @param action the zip action + * + * @return the filter + */ +tb_filter_ref_t tb_filter_init_from_zip(tb_size_t algo, tb_size_t action); + +/*! init filter from charset + * + * @param fr the from charset + * @param to the to charset + * + * @return the filter + */ +tb_filter_ref_t tb_filter_init_from_charset(tb_size_t fr, tb_size_t to); + +/*! init filter from chunked + * + * @param dechunked decode the chunked data? + * + * @return the filter + */ +tb_filter_ref_t tb_filter_init_from_chunked(tb_bool_t dechunked); + +/*! init filter from cache + * + * @param size the initial cache size, using the default size if be zero + * + * @return the filter + */ +tb_filter_ref_t tb_filter_init_from_cache(tb_size_t size); + +/*! exit filter + * + * @param filter the filter + */ +tb_void_t tb_filter_exit(tb_filter_ref_t filter); + +/*! open filter + * + * @param filter the filter + * + * @return tb_true or tb_false + */ +tb_bool_t tb_filter_open(tb_filter_ref_t filter); + +/*! clos filter + * + * @param filter the filter + */ +tb_void_t tb_filter_clos(tb_filter_ref_t filter); + +/*! ctrl filter + * + * @param filter the filter + * @param ctrl the ctrl code + * + * @return tb_true or tb_false + */ +tb_bool_t tb_filter_ctrl(tb_filter_ref_t filter, tb_size_t ctrl, ...); + +/*! is eof for the filter input data, but the output maybe exists the left data and need flush it + * + * @param filter the filter + * + * @return tb_true or tb_false + */ +tb_bool_t tb_filter_beof(tb_filter_ref_t filter); + +/*! limit the input size for filter + * + * @param filter the filter + * @param limit the input limit size + */ +tb_void_t tb_filter_limit(tb_filter_ref_t filter, tb_hong_t limit); + +/*! push data to the filter input data + * + * @param filter the filter + * + * @return tb_true or tb_false + */ +tb_bool_t tb_filter_push(tb_filter_ref_t filter, tb_byte_t const* data, tb_size_t size); + +/*! spak filter + * + * @param filter the filter + * @param data the input data, maybe null + * @param size the input size, maybe zero + * @param pdata the output data + * @param need the need output size, using the default size if zero + * @param sync sync? 1: sync, 0: no sync, -1: end + * + * @return > 0: the output size, 0: continue, -1: end + */ +tb_long_t tb_filter_spak(tb_filter_ref_t filter, tb_byte_t const* data, tb_size_t size, tb_byte_t const** pdata, tb_size_t need, tb_long_t sync); + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/prefix.h b/core/pkg/tbox.pkg/inc/tbox/stream/prefix.h index 317c74c7b..630f3ba29 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/prefix.h @@ -156,3 +156,161 @@ typedef enum __tb_stream_ctrl_e }tb_stream_ctrl_e; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_STREAM_PREFIX_H +#define TB_STREAM_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../libc/libc.h" +#include "../network/url.h" +#include "../memory/memory.h" +#include "../platform/socket.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/// the stream ctrl +#define TB_STREAM_CTRL(type, ctrl) (((type) << 16) | (ctrl)) +#define TB_STREAM_CTRL_FLTR(type, ctrl) TB_STREAM_CTRL(TB_STREAM_TYPE_FLTR, (((type) << 8) | (ctrl))) + +/// the stream default timeout, 10s +#define TB_STREAM_DEFAULT_TIMEOUT (10000) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the stream mode enum +typedef enum __tb_stream_mode_e +{ + TB_STREAM_MODE_NONE = 0 +, TB_STREAM_MODE_AIOO = 1 ///!< for stream +, TB_STREAM_MODE_AICO = 2 ///!< for async_stream + +}tb_stream_mode_e; + +/// the stream type enum +typedef enum __tb_stream_type_e +{ + TB_STREAM_TYPE_NONE = 0 +, TB_STREAM_TYPE_FILE = 1 +, TB_STREAM_TYPE_SOCK = 2 +, TB_STREAM_TYPE_HTTP = 3 +, TB_STREAM_TYPE_DATA = 4 +, TB_STREAM_TYPE_FLTR = 5 +, TB_STREAM_TYPE_USER = 6 ///!< for user defined stream type + +}tb_stream_type_e; + +/// the stream wait enum +typedef enum __tb_stream_wait_e +{ + TB_STREAM_WAIT_NONE = TB_SOCKET_EVENT_NONE +, TB_STREAM_WAIT_READ = TB_SOCKET_EVENT_RECV +, TB_STREAM_WAIT_WRIT = TB_SOCKET_EVENT_SEND +, TB_STREAM_WAIT_EALL = TB_SOCKET_EVENT_EALL + +}tb_stream_wait_e; + +/// the stream ctrl enum +typedef enum __tb_stream_ctrl_e +{ + TB_STREAM_CTRL_NONE = 0 + + // the stream +, TB_STREAM_CTRL_GET_URL = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 1) +, TB_STREAM_CTRL_GET_HOST = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 2) +, TB_STREAM_CTRL_GET_PORT = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 3) +, TB_STREAM_CTRL_GET_PATH = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 4) +, TB_STREAM_CTRL_GET_SSL = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 5) +, TB_STREAM_CTRL_GET_TIMEOUT = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 6) +, TB_STREAM_CTRL_GET_SIZE = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 7) +, TB_STREAM_CTRL_GET_OFFSET = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 8) + +, TB_STREAM_CTRL_SET_URL = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 11) +, TB_STREAM_CTRL_SET_HOST = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 12) +, TB_STREAM_CTRL_SET_PORT = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 13) +, TB_STREAM_CTRL_SET_PATH = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 14) +, TB_STREAM_CTRL_SET_SSL = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 15) +, TB_STREAM_CTRL_SET_TIMEOUT = TB_STREAM_CTRL(TB_STREAM_TYPE_NONE, 16) + + // the stream for data +, TB_STREAM_CTRL_DATA_SET_DATA = TB_STREAM_CTRL(TB_STREAM_TYPE_DATA, 1) + + // the stream for file +, TB_STREAM_CTRL_FILE_GET_MODE = TB_STREAM_CTRL(TB_STREAM_TYPE_FILE, 1) +, TB_STREAM_CTRL_FILE_SET_MODE = TB_STREAM_CTRL(TB_STREAM_TYPE_FILE, 2) +, TB_STREAM_CTRL_FILE_IS_STREAM = TB_STREAM_CTRL(TB_STREAM_TYPE_FILE, 3) + + // the stream for sock +, TB_STREAM_CTRL_SOCK_GET_TYPE = TB_STREAM_CTRL(TB_STREAM_TYPE_SOCK, 1) +, TB_STREAM_CTRL_SOCK_SET_TYPE = TB_STREAM_CTRL(TB_STREAM_TYPE_SOCK, 2) +, TB_STREAM_CTRL_SOCK_KEEP_ALIVE = TB_STREAM_CTRL(TB_STREAM_TYPE_SOCK, 3) + + // the stream for http +, TB_STREAM_CTRL_HTTP_GET_HEAD = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 1) +, TB_STREAM_CTRL_HTTP_GET_RANGE = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 2) +, TB_STREAM_CTRL_HTTP_GET_METHOD = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 3) +, TB_STREAM_CTRL_HTTP_GET_VERSION = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 4) +, TB_STREAM_CTRL_HTTP_GET_COOKIES = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 5) +, TB_STREAM_CTRL_HTTP_GET_REDIRECT = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 6) +, TB_STREAM_CTRL_HTTP_GET_HEAD_FUNC = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 7) +, TB_STREAM_CTRL_HTTP_GET_HEAD_PRIV = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 8) +, TB_STREAM_CTRL_HTTP_GET_AUTO_UNZIP = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 9) +, TB_STREAM_CTRL_HTTP_GET_POST_URL = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 10) +, TB_STREAM_CTRL_HTTP_GET_POST_DATA = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 11) +, TB_STREAM_CTRL_HTTP_GET_POST_FUNC = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 12) +, TB_STREAM_CTRL_HTTP_GET_POST_PRIV = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 13) +, TB_STREAM_CTRL_HTTP_GET_POST_LRATE = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 14) + +, TB_STREAM_CTRL_HTTP_SET_HEAD = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 20) +, TB_STREAM_CTRL_HTTP_SET_RANGE = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 21) +, TB_STREAM_CTRL_HTTP_SET_METHOD = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 22) +, TB_STREAM_CTRL_HTTP_SET_VERSION = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 23) +, TB_STREAM_CTRL_HTTP_SET_COOKIES = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 24) +, TB_STREAM_CTRL_HTTP_SET_REDIRECT = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 25) +, TB_STREAM_CTRL_HTTP_SET_HEAD_FUNC = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 26) +, TB_STREAM_CTRL_HTTP_SET_HEAD_PRIV = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 27) +, TB_STREAM_CTRL_HTTP_SET_AUTO_UNZIP = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 28) +, TB_STREAM_CTRL_HTTP_SET_POST_URL = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 29) +, TB_STREAM_CTRL_HTTP_SET_POST_DATA = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 30) +, TB_STREAM_CTRL_HTTP_SET_POST_FUNC = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 31) +, TB_STREAM_CTRL_HTTP_SET_POST_PRIV = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 32) +, TB_STREAM_CTRL_HTTP_SET_POST_LRATE = TB_STREAM_CTRL(TB_STREAM_TYPE_HTTP, 33) + + // the stream for filter +, TB_STREAM_CTRL_FLTR_GET_STREAM = TB_STREAM_CTRL(TB_STREAM_TYPE_FLTR, 1) +, TB_STREAM_CTRL_FLTR_GET_FILTER = TB_STREAM_CTRL(TB_STREAM_TYPE_FLTR, 2) +, TB_STREAM_CTRL_FLTR_SET_STREAM = TB_STREAM_CTRL(TB_STREAM_TYPE_FLTR, 3) +, TB_STREAM_CTRL_FLTR_SET_FILTER = TB_STREAM_CTRL(TB_STREAM_TYPE_FLTR, 4) + +}tb_stream_ctrl_e; + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/static_stream.h b/core/pkg/tbox.pkg/inc/tbox/stream/static_stream.h index e9c373849..c3ac376eb 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/static_stream.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/static_stream.h @@ -813,3 +813,818 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file static_stream.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_STATIC_STREAM_H +#define TB_STREAM_STATIC_STREAM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../utils/utils.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifdef TB_WORDS_BIGENDIAN +# define tb_static_stream_read_u16_ne(stream) tb_static_stream_read_u16_be(stream) +# define tb_static_stream_read_s16_ne(stream) tb_static_stream_read_s16_be(stream) +# define tb_static_stream_read_u24_ne(stream) tb_static_stream_read_u24_be(stream) +# define tb_static_stream_read_s24_ne(stream) tb_static_stream_read_s24_be(stream) +# define tb_static_stream_read_u32_ne(stream) tb_static_stream_read_u32_be(stream) +# define tb_static_stream_read_s32_ne(stream) tb_static_stream_read_s32_be(stream) +# define tb_static_stream_read_u64_ne(stream) tb_static_stream_read_u64_be(stream) +# define tb_static_stream_read_s64_ne(stream) tb_static_stream_read_s64_be(stream) + +# define tb_static_stream_writ_u16_ne(stream, val) tb_static_stream_writ_u16_be(stream, val) +# define tb_static_stream_writ_s16_ne(stream, val) tb_static_stream_writ_s16_be(stream, val) +# define tb_static_stream_writ_u24_ne(stream, val) tb_static_stream_writ_u24_be(stream, val) +# define tb_static_stream_writ_s24_ne(stream, val) tb_static_stream_writ_s24_be(stream, val) +# define tb_static_stream_writ_u32_ne(stream, val) tb_static_stream_writ_u32_be(stream, val) +# define tb_static_stream_writ_s32_ne(stream, val) tb_static_stream_writ_s32_be(stream, val) +# define tb_static_stream_writ_u64_ne(stream, val) tb_static_stream_writ_u64_be(stream, val) +# define tb_static_stream_writ_s64_ne(stream, val) tb_static_stream_writ_s64_be(stream, val) + +#else +# define tb_static_stream_read_u16_ne(stream) tb_static_stream_read_u16_le(stream) +# define tb_static_stream_read_s16_ne(stream) tb_static_stream_read_s16_le(stream) +# define tb_static_stream_read_u24_ne(stream) tb_static_stream_read_u24_le(stream) +# define tb_static_stream_read_s24_ne(stream) tb_static_stream_read_s24_le(stream) +# define tb_static_stream_read_u32_ne(stream) tb_static_stream_read_u32_le(stream) +# define tb_static_stream_read_s32_ne(stream) tb_static_stream_read_s32_le(stream) +# define tb_static_stream_read_u64_ne(stream) tb_static_stream_read_u64_le(stream) +# define tb_static_stream_read_s64_ne(stream) tb_static_stream_read_s64_le(stream) + +# define tb_static_stream_writ_u16_ne(stream, val) tb_static_stream_writ_u16_le(stream, val) +# define tb_static_stream_writ_s16_ne(stream, val) tb_static_stream_writ_s16_le(stream, val) +# define tb_static_stream_writ_u24_ne(stream, val) tb_static_stream_writ_u24_le(stream, val) +# define tb_static_stream_writ_s24_ne(stream, val) tb_static_stream_writ_s24_le(stream, val) +# define tb_static_stream_writ_u32_ne(stream, val) tb_static_stream_writ_u32_le(stream, val) +# define tb_static_stream_writ_s32_ne(stream, val) tb_static_stream_writ_s32_le(stream, val) +# define tb_static_stream_writ_u64_ne(stream, val) tb_static_stream_writ_u64_le(stream, val) +# define tb_static_stream_writ_s64_ne(stream, val) tb_static_stream_writ_s64_le(stream, val) + +#endif + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# ifdef TB_FLOAT_BIGENDIAN +# define tb_static_stream_read_double_nbe(stream) tb_static_stream_read_double_bbe(stream) +# define tb_static_stream_read_double_nle(stream) tb_static_stream_read_double_ble(stream) + +# define tb_static_stream_writ_double_nbe(stream, val) tb_static_stream_writ_double_bbe(stream, val) +# define tb_static_stream_writ_double_nle(stream, val) tb_static_stream_writ_double_ble(stream, val) +# else +# define tb_static_stream_read_double_nbe(stream) tb_static_stream_read_double_lbe(stream) +# define tb_static_stream_read_double_nle(stream) tb_static_stream_read_double_lle(stream) + +# define tb_static_stream_writ_double_nbe(stream, val) tb_static_stream_writ_double_lbe(stream, val) +# define tb_static_stream_writ_double_nle(stream, val) tb_static_stream_writ_double_lle(stream, val) +# endif +# ifdef TB_WORDS_BIGENDIAN +# define tb_static_stream_read_float_ne(stream) tb_static_stream_read_float_be(stream) +# define tb_static_stream_writ_float_ne(stream, val) tb_static_stream_writ_float_be(stream, val) + +# define tb_static_stream_read_double_nne(stream) tb_static_stream_read_double_nbe(stream) +# define tb_static_stream_read_double_bne(stream) tb_static_stream_read_double_bbe(stream) +# define tb_static_stream_read_double_lne(stream) tb_static_stream_read_double_lbe(stream) + +# define tb_static_stream_writ_double_nne(stream, val) tb_static_stream_writ_double_nbe(stream, val) +# define tb_static_stream_writ_double_bne(stream, val) tb_static_stream_writ_double_bbe(stream, val) +# define tb_static_stream_writ_double_lne(stream, val) tb_static_stream_writ_double_lbe(stream, val) +# else +# define tb_static_stream_read_float_ne(stream) tb_static_stream_read_float_le(stream) +# define tb_static_stream_writ_float_ne(stream, val) tb_static_stream_writ_float_le(stream, val) + +# define tb_static_stream_read_double_nne(stream) tb_static_stream_read_double_nle(stream) +# define tb_static_stream_read_double_bne(stream) tb_static_stream_read_double_ble(stream) +# define tb_static_stream_read_double_lne(stream) tb_static_stream_read_double_lle(stream) + +# define tb_static_stream_writ_double_nne(stream, val) tb_static_stream_writ_double_nle(stream, val) +# define tb_static_stream_writ_double_bne(stream, val) tb_static_stream_writ_double_ble(stream, val) +# define tb_static_stream_writ_double_lne(stream, val) tb_static_stream_writ_double_lle(stream, val) +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the static stream type +typedef struct __tb_static_stream_t +{ + /// the pointer to the current position + tb_byte_t* p; + + /// the bit offset < 8 + tb_size_t b; + + /// the pointer to the end + tb_byte_t* e; + + /// the data size + tb_size_t n; + +}tb_static_stream_t; + +/// the static stream ref type +typedef tb_static_stream_t* tb_static_stream_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the static stream + * + * @param stream the stream + * @param data the data address + * @param size the data size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_init(tb_static_stream_ref_t stream, tb_byte_t* data, tb_size_t size); + +/*! goto the new data address for updating the stream position + * + * @param stream the stream + * @param data the data address + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_goto(tb_static_stream_ref_t stream, tb_byte_t* data); + +/*! sync the stream position if update some bits offset + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_sync(tb_static_stream_ref_t stream); + +/*! the stream start data address + * + * @param stream the stream + * + * @return the start data address + */ +tb_byte_t const* tb_static_stream_beg(tb_static_stream_ref_t stream); + +/*! the stream current data address + * + * @param stream the stream + * + * @return the current data address + */ +tb_byte_t const* tb_static_stream_pos(tb_static_stream_ref_t stream); + +/*! the stream end data address + * + * @param stream the stream + * + * @return the end data address + */ +tb_byte_t const* tb_static_stream_end(tb_static_stream_ref_t stream); + +/*! the stream offset + * + * @param stream the stream + * + * @return the offset + */ +tb_size_t tb_static_stream_offset(tb_static_stream_ref_t stream); + +/*! the stream data size + * + * @param stream the stream + * + * @return the data size + */ +tb_size_t tb_static_stream_size(tb_static_stream_ref_t stream); + +/*! the stream left size + * + * @param stream the stream + * + * @return the left size + */ +tb_size_t tb_static_stream_left(tb_static_stream_ref_t stream); + +/*! the stream left bits + * + * @param stream the stream + * + * @return the left bits + */ +tb_size_t tb_static_stream_left_bits(tb_static_stream_ref_t stream); + +/*! the stream is valid? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_valid(tb_static_stream_ref_t stream); + +/*! skip the given size + * + * @param stream the stream + * @param size the skiped size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_skip(tb_static_stream_ref_t stream, tb_size_t size); + +/*! skip the given bits + * + * @param stream the stream + * @param nbits the skiped bits count + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_skip_bits(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! skip the given c-string + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_skip_cstr(tb_static_stream_ref_t stream); + +/*! peek ubits value for uint32 + * + * @param stream the stream + * @param nbits the bits count + * + * @return the ubits value + */ +tb_uint32_t tb_static_stream_peek_ubits32(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! peek sbits value for sint32 + * + * @param stream the stream + * @param nbits the bits count + * + * @return the sbits value + */ +tb_sint32_t tb_static_stream_peek_sbits32(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! read c-string + * + * @param stream the stream + * + * @return the c-string + */ +tb_char_t const* tb_static_stream_read_cstr(tb_static_stream_ref_t stream); + +/*! read data + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return the data real size + */ +tb_size_t tb_static_stream_read_data(tb_static_stream_ref_t stream, tb_byte_t* data, tb_size_t size); + +/*! read ubits value for uint32 + * + * @param stream the stream + * @param nbits the bits count + * + * @return the ubits value + */ +tb_uint32_t tb_static_stream_read_ubits32(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! read sbits value for sint32 + * + * @param stream the stream + * @param nbits the bits count + * + * @return the sbits value + */ +tb_sint32_t tb_static_stream_read_sbits32(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! read ubits value for uint64 + * + * @param stream the stream + * @param nbits the bits count + * + * @return the ubits value + */ +tb_uint64_t tb_static_stream_read_ubits64(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! read sbits value for sint64 + * + * @param stream the stream + * @param nbits the bits count + * + * @return the sbits value + */ +tb_sint64_t tb_static_stream_read_sbits64(tb_static_stream_ref_t stream, tb_size_t nbits); + +/*! read ubits1 value + * + * @param stream the stream + * + * @return the ubits1 value + */ +tb_uint8_t tb_static_stream_read_u1(tb_static_stream_ref_t stream); + +/*! read uint8 integer + * + * @param stream the stream + * + * @return the uint8 integer + */ +tb_uint8_t tb_static_stream_read_u8(tb_static_stream_ref_t stream); + +/*! read sint8 integer + * + * @param stream the stream + * + * @return the sint8 integer + */ +tb_sint8_t tb_static_stream_read_s8(tb_static_stream_ref_t stream); + +/*! read uint16-be integer + * + * @param stream the stream + * + * @return the uint16-be integer + */ +tb_uint16_t tb_static_stream_read_u16_be(tb_static_stream_ref_t stream); + +/*! read sint16-be integer + * + * @param stream the stream + * + * @return the sint16-be integer + */ +tb_sint16_t tb_static_stream_read_s16_be(tb_static_stream_ref_t stream); + +/*! read uint16-le integer + * + * @param stream the stream + * + * @return the uint16-le integer + */ +tb_uint16_t tb_static_stream_read_u16_le(tb_static_stream_ref_t stream); + +/*! read sint16-le integer + * + * @param stream the stream + * + * @return the sint16-le integer + */ +tb_sint16_t tb_static_stream_read_s16_le(tb_static_stream_ref_t stream); + +/*! read uint24-be integer + * + * @param stream the stream + * + * @return the uint24-be integer + */ +tb_uint32_t tb_static_stream_read_u24_be(tb_static_stream_ref_t stream); + +/*! read sint24-be integer + * + * @param stream the stream + * + * @return the sint24-be integer + */ +tb_sint32_t tb_static_stream_read_s24_be(tb_static_stream_ref_t stream); + +/*! read uint24-le integer + * + * @param stream the stream + * + * @return the uint24-le integer + */ +tb_uint32_t tb_static_stream_read_u24_le(tb_static_stream_ref_t stream); + +/*! read sint24-le integer + * + * @param stream the stream + * + * @return the sint24-le integer + */ +tb_sint32_t tb_static_stream_read_s24_le(tb_static_stream_ref_t stream); + +/*! read uint32-be integer + * + * @param stream the stream + * + * @return the uint32-be integer + */ +tb_uint32_t tb_static_stream_read_u32_be(tb_static_stream_ref_t stream); + +/*! read sint32-be integer + * + * @param stream the stream + * + * @return the sint32-be integer + */ +tb_sint32_t tb_static_stream_read_s32_be(tb_static_stream_ref_t stream); + +/*! read uint32-le integer + * + * @param stream the stream + * + * @return the uint32-le integer + */ +tb_uint32_t tb_static_stream_read_u32_le(tb_static_stream_ref_t stream); + +/*! read sint32-le integer + * + * @param stream the stream + * + * @return the sint32-le integer + */ +tb_sint32_t tb_static_stream_read_s32_le(tb_static_stream_ref_t stream); + +/*! read uint64-be integer + * + * @param stream the stream + * + * @return the uint64-be integer + */ +tb_uint64_t tb_static_stream_read_u64_be(tb_static_stream_ref_t stream); + +/*! read sint64-be integer + * + * @param stream the stream + * + * @return the sint64-be integer + */ +tb_sint64_t tb_static_stream_read_s64_be(tb_static_stream_ref_t stream); + +/*! read uint64-le integer + * + * @param stream the stream + * + * @return the uint64-le integer + */ +tb_uint64_t tb_static_stream_read_u64_le(tb_static_stream_ref_t stream); + +/*! read sint64-le integer + * + * @param stream the stream + * + * @return the sint64-le integer + */ +tb_sint64_t tb_static_stream_read_s64_le(tb_static_stream_ref_t stream); + +/*! writ c-string + * + * @param stream the stream + * @param cstr the c-string + * + * @return the writed c-string address + */ +tb_char_t* tb_static_stream_writ_cstr(tb_static_stream_ref_t stream, tb_char_t const* cstr); + +/*! writ data + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return the writed data size + */ +tb_size_t tb_static_stream_writ_data(tb_static_stream_ref_t stream, tb_byte_t const* data, tb_size_t size); + +/*! writ ubits for uint32 + * + * @param stream the stream + * @param val the value + * @param nbits the bits count + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_ubits32(tb_static_stream_ref_t stream, tb_uint32_t val, tb_size_t nbits); + +/*! writ sbits for sint32 + * + * @param stream the stream + * @param val the value + * @param nbits the bits count + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_sbits32(tb_static_stream_ref_t stream, tb_sint32_t val, tb_size_t nbits); + +/*! writ ubits1 value + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u1(tb_static_stream_ref_t stream, tb_uint8_t val); + +/*! writ uint8 integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u8(tb_static_stream_ref_t stream, tb_uint8_t val); + +/*! writ sint8 integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s8(tb_static_stream_ref_t stream, tb_sint8_t val); + +/*! writ uint16-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u16_be(tb_static_stream_ref_t stream, tb_uint16_t val); + +/*! writ sint16-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s16_be(tb_static_stream_ref_t stream, tb_sint16_t val); + +/*! writ uint16-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u16_le(tb_static_stream_ref_t stream, tb_uint16_t val); + +/*! writ sint16-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s16_le(tb_static_stream_ref_t stream, tb_sint16_t val); + +/*! writ uint24-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u24_be(tb_static_stream_ref_t stream, tb_uint32_t val); + +/*! writ sint24-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s24_be(tb_static_stream_ref_t stream, tb_sint32_t val); + +/*! writ uint24-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u24_le(tb_static_stream_ref_t stream, tb_uint32_t val); + +/*! writ sint24-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s24_le(tb_static_stream_ref_t stream, tb_sint32_t val); + +/*! writ uint32-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u32_be(tb_static_stream_ref_t stream, tb_uint32_t val); + +/*! writ sint32-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s32_be(tb_static_stream_ref_t stream, tb_sint32_t val); + +/*! writ uint32-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u32_le(tb_static_stream_ref_t stream, tb_uint32_t val); + +/*! writ sint32-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s32_le(tb_static_stream_ref_t stream, tb_sint32_t val); + +/*! writ uint64-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u64_be(tb_static_stream_ref_t stream, tb_uint64_t val); + +/*! writ sint64-be integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s64_be(tb_static_stream_ref_t stream, tb_sint64_t val); + +/*! writ uint64-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_u64_le(tb_static_stream_ref_t stream, tb_uint64_t val); + +/*! writ sint64-le integer + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_s64_le(tb_static_stream_ref_t stream, tb_sint64_t val); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +/*! read float-le number + * + * @param stream the stream + * + * @return the float-le number + */ +tb_float_t tb_static_stream_read_float_le(tb_static_stream_ref_t stream); + +/*! read float-be number + * + * @param stream the stream + * + * @return the float-be number + */ +tb_float_t tb_static_stream_read_float_be(tb_static_stream_ref_t stream); + +/*! read double-ble number + * + * @param stream the stream + * + * @return the double-ble number + */ +tb_double_t tb_static_stream_read_double_ble(tb_static_stream_ref_t stream); + +/*! read double-bbe number + * + * @param stream the stream + * + * @return the double-bbe number + */ +tb_double_t tb_static_stream_read_double_bbe(tb_static_stream_ref_t stream); + +/*! read double-lle number + * + * @param stream the stream + * + * @return the double-lle number + */ +tb_double_t tb_static_stream_read_double_lle(tb_static_stream_ref_t stream); + +/*! read double-lbe number + * + * @param stream the stream + * + * @return the double-lbe number + */ +tb_double_t tb_static_stream_read_double_lbe(tb_static_stream_ref_t stream); + +/*! writ float-le number + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_float_le(tb_static_stream_ref_t stream, tb_float_t val); + +/*! writ float-be number + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_float_be(tb_static_stream_ref_t stream, tb_float_t val); + +/*! writ double-ble number + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_double_ble(tb_static_stream_ref_t stream, tb_double_t val); + +/*! writ double-bbe number + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_double_bbe(tb_static_stream_ref_t stream, tb_double_t val); + +/*! writ double-lle number + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_double_lle(tb_static_stream_ref_t stream, tb_double_t val); + +/*! writ double-lbe number + * + * @param stream the stream + * @param val the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_stream_writ_double_lbe(tb_static_stream_ref_t stream, tb_double_t val); + +#endif + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/stream.h b/core/pkg/tbox.pkg/inc/tbox/stream/stream.h index d5a12debd..0f96679e4 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/stream.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/stream.h @@ -1211,3 +1211,1216 @@ tb_bool_t tb_stream_bwrit_double_lbe(tb_stream_ref_t stream, tb_do __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file stream.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_H +#define TB_STREAM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "filter.h" +#include "transfer.h" +#include "static_stream.h" +#ifdef TB_CONFIG_API_HAVE_DEPRECATED +# include "deprecated/deprecated.h" +#endif + +/*!architecture + * + * + * <pre> + * + * - data + * | + * stream --------------------- - file + * | | + * | - sock + * | | + * | - http + * | - charset + * | | + * - filter - |- chunked + * | + * |- cache + * | + * - zip + * + * + * url: + * data://base64 + * file://path or unix path: e.g. /root/xxxx/file + * sock://host:port?tcp= + * sock://host:port?udp= + * socks://host:port + * http://host:port/path?arg0=&arg1=... + * https://host:port/path?arg0=&arg1=... + * </pre> + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the stream block maxn +#define TB_STREAM_BLOCK_MAXN (8192) + +// the stream bitops +#ifdef TB_WORDS_BIGENDIAN +# define tb_stream_bread_u16_ne(stream, pvalue) tb_stream_bread_u16_be(stream, pvalue) +# define tb_stream_bread_s16_ne(stream, pvalue) tb_stream_bread_s16_be(stream, pvalue) +# define tb_stream_bread_u24_ne(stream, pvalue) tb_stream_bread_u24_be(stream, pvalue) +# define tb_stream_bread_s24_ne(stream, pvalue) tb_stream_bread_s24_be(stream, pvalue) +# define tb_stream_bread_u32_ne(stream, pvalue) tb_stream_bread_u32_be(stream, pvalue) +# define tb_stream_bread_s32_ne(stream, pvalue) tb_stream_bread_s32_be(stream, pvalue) +# define tb_stream_bread_u64_ne(stream, pvalue) tb_stream_bread_u64_be(stream, pvalue) +# define tb_stream_bread_s64_ne(stream, pvalue) tb_stream_bread_s64_be(stream, pvalue) + +# define tb_stream_bwrit_u16_ne(stream, value) tb_stream_bwrit_u16_be(stream, value) +# define tb_stream_bwrit_s16_ne(stream, value) tb_stream_bwrit_s16_be(stream, value) +# define tb_stream_bwrit_u24_ne(stream, value) tb_stream_bwrit_u24_be(stream, value) +# define tb_stream_bwrit_s24_ne(stream, value) tb_stream_bwrit_s24_be(stream, value) +# define tb_stream_bwrit_u32_ne(stream, value) tb_stream_bwrit_u32_be(stream, value) +# define tb_stream_bwrit_s32_ne(stream, value) tb_stream_bwrit_s32_be(stream, value) +# define tb_stream_bwrit_u64_ne(stream, value) tb_stream_bwrit_u64_be(stream, value) +# define tb_stream_bwrit_s64_ne(stream, value) tb_stream_bwrit_s64_be(stream, value) + +#else +# define tb_stream_bread_u16_ne(stream, pvalue) tb_stream_bread_u16_le(stream, pvalue) +# define tb_stream_bread_s16_ne(stream, pvalue) tb_stream_bread_s16_le(stream, pvalue) +# define tb_stream_bread_u24_ne(stream, pvalue) tb_stream_bread_u24_le(stream, pvalue) +# define tb_stream_bread_s24_ne(stream, pvalue) tb_stream_bread_s24_le(stream, pvalue) +# define tb_stream_bread_u32_ne(stream, pvalue) tb_stream_bread_u32_le(stream, pvalue) +# define tb_stream_bread_s32_ne(stream, pvalue) tb_stream_bread_s32_le(stream, pvalue) +# define tb_stream_bread_u64_ne(stream, pvalue) tb_stream_bread_u64_le(stream, pvalue) +# define tb_stream_bread_s64_ne(stream, pvalue) tb_stream_bread_s64_le(stream, pvalue) + +# define tb_stream_bwrit_u16_ne(stream, value) tb_stream_bwrit_u16_le(stream, value) +# define tb_stream_bwrit_s16_ne(stream, value) tb_stream_bwrit_s16_le(stream, value) +# define tb_stream_bwrit_u24_ne(stream, value) tb_stream_bwrit_u24_le(stream, value) +# define tb_stream_bwrit_s24_ne(stream, value) tb_stream_bwrit_s24_le(stream, value) +# define tb_stream_bwrit_u32_ne(stream, value) tb_stream_bwrit_u32_le(stream, value) +# define tb_stream_bwrit_s32_ne(stream, value) tb_stream_bwrit_s32_le(stream, value) +# define tb_stream_bwrit_u64_ne(stream, value) tb_stream_bwrit_u64_le(stream, value) +# define tb_stream_bwrit_s64_ne(stream, value) tb_stream_bwrit_s64_le(stream, value) + +#endif + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +# ifdef TB_FLOAT_BIGENDIAN +# define tb_stream_bread_double_nbe(stream, pvalue) tb_stream_bread_double_bbe(stream, pvalue) +# define tb_stream_bread_double_nle(stream, pvalue) tb_stream_bread_double_ble(stream, pvalue) + +# define tb_stream_bwrit_double_nbe(stream, value) tb_stream_bwrit_double_bbe(stream, value) +# define tb_stream_bwrit_double_nle(stream, value) tb_stream_bwrit_double_ble(stream, value) +# else +# define tb_stream_bread_double_nbe(stream, pvalue) tb_stream_bread_double_lbe(stream, pvalue) +# define tb_stream_bread_double_nle(stream, pvalue) tb_stream_bread_double_lle(stream, pvalue) + +# define tb_stream_bwrit_double_nbe(stream, value) tb_stream_bwrit_double_lbe(stream, value) +# define tb_stream_bwrit_double_nle(stream, value) tb_stream_bwrit_double_lle(stream, value) +# endif +# ifdef TB_WORDS_BIGENDIAN +# define tb_stream_bread_float_ne(stream, pvalue) tb_stream_bread_float_be(stream, pvalue) +# define tb_stream_bwrit_float_ne(stream, value) tb_stream_bwrit_float_be(stream, value) + +# define tb_stream_bread_double_nne(stream, pvalue) tb_stream_bread_double_nbe(stream, pvalue) +# define tb_stream_bread_double_bne(stream, pvalue) tb_stream_bread_double_bbe(stream, pvalue) +# define tb_stream_bread_double_lne(stream, pvalue) tb_stream_bread_double_lbe(stream, pvalue) + +# define tb_stream_bwrit_double_nne(stream, value) tb_stream_bwrit_double_nbe(stream, value) +# define tb_stream_bwrit_double_bne(stream, value) tb_stream_bwrit_double_bbe(stream, value) +# define tb_stream_bwrit_double_lne(stream, value) tb_stream_bwrit_double_lbe(stream, value) +# else +# define tb_stream_bread_float_ne(stream, pvalue) tb_stream_bread_float_le(stream, pvalue) +# define tb_stream_bwrit_float_ne(stream, value) tb_stream_bwrit_float_le(stream, value) + +# define tb_stream_bread_double_nne(stream, pvalue) tb_stream_bread_double_nle(stream, pvalue) +# define tb_stream_bread_double_bne(stream, pvalue) tb_stream_bread_double_ble(stream, pvalue) +# define tb_stream_bread_double_lne(stream, pvalue) tb_stream_bread_double_lle(stream, pvalue) + +# define tb_stream_bwrit_double_nne(stream, value) tb_stream_bwrit_double_nle(stream, value) +# define tb_stream_bwrit_double_bne(stream, value) tb_stream_bwrit_double_ble(stream, value) +# define tb_stream_bwrit_double_lne(stream, value) tb_stream_bwrit_double_lle(stream, value) +# endif +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init stream + * + * @param type the stream type + * @param type_size the stream type size + * @param cache the cache size + * @param open the stream impl func: open + * @param clos the stream impl func: clos + * @param exit the stream impl func: exit, optional + * @param ctrl the stream impl func: ctrl + * @param wait the stream impl func: wait + * @param read the stream impl func: read + * @param writ the stream impl func: writ + * @param seek the stream impl func: seek, optional + * @param sync the stream impl func: sync, optional + * @param kill the stream impl func: kill, optional + * + * @return the stream + * + * @code + // the custom xxxx stream type + typedef struct __tb_stream_xxxx_impl_t + { + // the xxxx data + tb_handle_t xxxx; + + }tb_stream_xxxx_impl_t; + + static tb_bool_t tb_stream_xxxx_impl_open(tb_stream_ref_t stream) + { + // check + tb_stream_xxxx_impl_t* impl = (tb_stream_xxxx_impl_t*)stream; + tb_assert_and_check_return_val(impl, tb_false); + + // ok + return tb_true; + } + static tb_bool_t tb_stream_xxxx_impl_clos(tb_stream_ref_t stream) + { + // check + tb_stream_xxxx_impl_t* impl = (tb_stream_xxxx_impl_t*)stream; + tb_assert_and_check_return_val(impl, tb_false); + + // ok + return tb_true; + } + + // define other xxxx stream func + // ... + + // init stream + tb_stream_ref_t stream = tb_stream_init( TB_STREAM_TYPE_XXXX + , sizeof(tb_stream_xxxx_impl_t) + , 0 + , tb_stream_xxxx_impl_open + , tb_stream_xxxx_impl_clos + , tb_stream_xxxx_impl_exit + , tb_stream_xxxx_impl_ctrl + , tb_stream_xxxx_impl_wait + , tb_stream_xxxx_impl_read + , tb_stream_xxxx_impl_writ + , tb_stream_xxxx_impl_seek + , tb_stream_xxxx_impl_sync + , tb_stream_xxxx_impl_kill); + + // using stream + // ... + + * @endcode + */ +tb_stream_ref_t tb_stream_init( tb_size_t type + , tb_size_t type_size + , tb_size_t cache + , tb_bool_t (*open)(tb_stream_ref_t stream) + , tb_bool_t (*clos)(tb_stream_ref_t stream) + , tb_void_t (*exit)(tb_stream_ref_t stream) + , tb_bool_t (*ctrl)(tb_stream_ref_t stream, tb_size_t ctrl, tb_va_list_t args) + , tb_long_t (*wait)(tb_stream_ref_t stream, tb_size_t wait, tb_long_t timeout) + , tb_long_t (*read)(tb_stream_ref_t stream, tb_byte_t* data, tb_size_t size) + , tb_long_t (*writ)(tb_stream_ref_t stream, tb_byte_t const* data, tb_size_t size) + , tb_bool_t (*seek)(tb_stream_ref_t stream, tb_hize_t offset) + , tb_bool_t (*sync)(tb_stream_ref_t stream, tb_bool_t bclosing) + , tb_void_t (*kill)(tb_stream_ref_t stream)); + +/*! init data stream + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_data(tb_noarg_t); + +/*! init file stream + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_file(tb_noarg_t); + +/*! init sock stream + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_sock(tb_noarg_t); + +/*! init http stream + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_http(tb_noarg_t); + +/*! init filter stream + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_filter(tb_noarg_t); + +/*! exit stream + * + * @param stream the stream + */ +tb_void_t tb_stream_exit(tb_stream_ref_t stream); + +/*! init stream from url + * + * @code + * + // init stream + tb_stream_ref_t stream = tb_stream_init_from_url("http://www.xxx.com/file"); + if (stream) + { + // open stream + if (tb_stream_open(stream)) + { + // ... + } + + // exit stream + tb_stream_exit(stream); + } + * + * @endcode + * + * @param url the url + * - data://base64 + * - file://path or unix path: e.g. /root/xxxx/file + * - sock://host:port?tcp= + * - sock://host:port?udp= + * - socks://host:port + * - http://host:port/path?arg0=&arg1=... + * - https://host:port/path?arg0=&arg1=... + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_from_url(tb_char_t const* url); + +/*! init stream from data + * + * @param data the data + * @param size the size + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_from_data(tb_byte_t const* data, tb_size_t size); + +/*! init stream from file + * + * @param path the file path + * @param mode the file mode, using the default ro mode if zero + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_from_file(tb_char_t const* path, tb_size_t mode); + +/*! init stream from sock + * + * @param host the host + * @param port the port + * @param type the socket type, tcp or udp + * @param bssl enable ssl? + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_from_sock(tb_char_t const* host, tb_uint16_t port, tb_size_t type, tb_bool_t bssl); + +/*! init stream from http or https + * + * @param host the host + * @param port the port + * @param path the path + * @param bssl enable ssl? + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_from_http(tb_char_t const* host, tb_uint16_t port, tb_char_t const* path, tb_bool_t bssl); + +/*! init filter stream from null + * + * @param stream the stream + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_filter_from_null(tb_stream_ref_t stream); + +/*! init filter stream from zip + * + * @param stream the stream + * @param algo the zip algorithm + * @param action the zip action + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_filter_from_zip(tb_stream_ref_t stream, tb_size_t algo, tb_size_t action); + +/*! init filter stream from cache + * + * @param stream the stream + * @param size the initial cache size, using the default size if be zero + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_filter_from_cache(tb_stream_ref_t stream, tb_size_t size); + +/*! init filter stream from charset + * + * @param stream the stream + * @param fr the from charset + * @param to the to charset + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_filter_from_charset(tb_stream_ref_t stream, tb_size_t fr, tb_size_t to); + +/*! init filter stream from chunked + * + * @param stream the stream + * @param dechunked decode the chunked data? + * + * @return the stream + */ +tb_stream_ref_t tb_stream_init_filter_from_chunked(tb_stream_ref_t stream, tb_bool_t dechunked); + +/*! wait stream + * + * blocking wait the single event object, so need not aiop + * return the event type if ok, otherwise return 0 for timeout + * + * @param stream the stream + * @param wait the wait type + * @param timeout the timeout value, return immediately if 0, infinity if -1 + * + * @return the event type, return 0 if timeout, return -1 if error + */ +tb_long_t tb_stream_wait(tb_stream_ref_t stream, tb_size_t wait, tb_long_t timeout); + +/*! the state + * + * @param stream the stream + * + * @return the state + */ +tb_size_t tb_stream_state(tb_stream_ref_t stream); + +/*! set the state + * + * @param stream the stream + * @param state the state + */ +tb_void_t tb_stream_state_set(tb_stream_ref_t stream, tb_size_t state); + +/*! the stream type + * + * @param stream the stream + * + * @return the stream type + */ +tb_size_t tb_stream_type(tb_stream_ref_t stream); + +/*! the stream size and not seeking it + * + * @param stream the stream + * + * @return the stream size, no size: -1, empty or error: 0 + */ +tb_hong_t tb_stream_size(tb_stream_ref_t stream); + +/*! the stream left size and not seeking it + * + * @param stream the stream + * + * @return the stream left size, no size: infinity, empty or end: 0 + */ +tb_hize_t tb_stream_left(tb_stream_ref_t stream); + +/*! the stream is end? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_beof(tb_stream_ref_t stream); + +/*! the stream offset + * + * the offset is read + writ and using seek for modifying it if size != -1, .e.g: data, file, .. + * the offset is calculated from the last read/writ and not seeking it if size == -1, .e.g: sock, filter, .. + * + * @param stream the stream + * + * @return the stream offset + */ +tb_hize_t tb_stream_offset(tb_stream_ref_t stream); + +/*! is opened? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_is_opened(tb_stream_ref_t stream); + +/*! is closed? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_is_closed(tb_stream_ref_t stream); + +/*! is killed? + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_is_killed(tb_stream_ref_t stream); + +/*! the stream url + * + * @param stream the stream + * + * @return the stream url + */ +tb_url_ref_t tb_stream_url(tb_stream_ref_t stream); + +/*! the stream timeout + * + * @param stream the stream + * + * @return the stream timeout + */ +tb_long_t tb_stream_timeout(tb_stream_ref_t stream); + +/*! ctrl stream + * + * @param stream the stream + * @param ctrl the ctrl code + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_ctrl(tb_stream_ref_t stream, tb_size_t ctrl, ...); + +/*! ctrl stream with arguments + * + * @param stream the stream + * @param ctrl the ctrl code + * @param args the ctrl args + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_ctrl_with_args(tb_stream_ref_t stream, tb_size_t ctrl, tb_va_list_t args); + +/*! kill stream + * + * @param stream the stream + */ +tb_void_t tb_stream_kill(tb_stream_ref_t stream); + +/*! open stream + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_open(tb_stream_ref_t stream); + +/*! close stream + * + * @param stream the stream + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_clos(tb_stream_ref_t stream); + +/*! read data, non-blocking + * + * @code + + tb_long_t read = 0; + tb_byte_t data[TB_STREAM_BLOCK_MAXN]; + while (!tb_stream_beof(stream)) + { + // read data + tb_long_t real = tb_stream_read(stream, data, sizeof(data)); + + // ok? + if (real > 0) read += real; + // no data? continue it + else if (!real) + { + // wait + real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream)); + tb_check_break(real > 0); + + // has read? + tb_assert_and_check_break(real & TB_STREAM_WAIT_READ); + } + // failed or end? + else break; + } + + * @endcode + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_stream_read(tb_stream_ref_t stream, tb_byte_t* data, tb_size_t size); + +/*! writ data, non-blocking + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return the real size or -1 + */ +tb_long_t tb_stream_writ(tb_stream_ref_t stream, tb_byte_t const* data, tb_size_t size); + +/*! block read + * + * @code + * + // get stream size + // + // @note + // size may be < -1 for the http(chunked)/filter/.. stream + // we need call tb_stream_read for reading data if size < 0 + // + tb_hong_t size = tb_stream_size(stream); + tb_assert(size > 0); + + // make data + tb_byte_t* data = tb_malloc((tb_size_t)size); + if (data) + { + // read data + tb_bool_t ok = tb_stream_bread(stream, data, size); + + // exit data + tb_free(data) + } + * + * @endcode + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread(tb_stream_ref_t stream, tb_byte_t* data, tb_size_t size); + +/*! block writ + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit(tb_stream_ref_t stream, tb_byte_t const* data, tb_size_t size); + +/*! sync stream + * + * @param stream the stream + * @param bclosing is closing? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_sync(tb_stream_ref_t stream, tb_bool_t bclosing); + +/*! need stream + * + * @code + + // need 16-bytes data + tb_byte_t* data = tb_null; + if (tb_stream_need(stream, &data, 16)) + { + // .. + } + + * @endcode + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_need(tb_stream_ref_t stream, tb_byte_t** data, tb_size_t size); + +/*! seek stream + * + * @param stream the stream + * @param offset the offset + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_seek(tb_stream_ref_t stream, tb_hize_t offset); + +/*! skip stream + * + * @param stream the stream + * @param size the size + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_skip(tb_stream_ref_t stream, tb_hize_t size); + +/*! block writ format data + * + * @param stream the stream + * @param fmt the format + * + * @return the real size, failed: -1 + */ +tb_long_t tb_stream_printf(tb_stream_ref_t stream, tb_char_t const* fmt, ...); + +/*! block read line + * + * @code + * + // read line + tb_long_t size = 0; + tb_char_t line[8192]; + while ((size = tb_stream_bread_line(stream, line, sizeof(line))) >= 0) + { + // trace + tb_trace_i("line: %s", line); + } + * + * @endcode + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return the real size + */ +tb_long_t tb_stream_bread_line(tb_stream_ref_t stream, tb_char_t* data, tb_size_t size); + +/*! block writ line + * + * @param stream the stream + * @param data the data + * @param size the size + * + * @return the real size + */ +tb_long_t tb_stream_bwrit_line(tb_stream_ref_t stream, tb_char_t* data, tb_size_t size); + +/*! block read all data + * + * @code + * + // read all data + tb_size_t size = 0; + tb_byte_t* data = tb_stream_bread_all(stream, tb_false, &size); + if (data) + { + // exit data + tb_free(data); + } + + // read all cstr and append '\0' + tb_size_t size = 0; + tb_char_t* cstr = (tb_char_t*)tb_stream_bread_all(stream, tb_true, &size); + if (cstr) + { + // exit cstr + tb_free(cstr); + } + + * @endcode + * + * @param stream the stream + * @param is_cstr will append '\0' if be c-string + * @param psize the size pointer, optional + * + * @return the data + */ +tb_byte_t* tb_stream_bread_all(tb_stream_ref_t stream, tb_bool_t is_cstr, tb_size_t* psize); + +/*! block read uint8 integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u8(tb_stream_ref_t stream, tb_uint8_t* pvalue); + +/*! block read sint8 integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s8(tb_stream_ref_t stream, tb_sint8_t* pvalue); + +/*! block read uint16-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u16_le(tb_stream_ref_t stream, tb_uint16_t* pvalue); + +/*! block read sint16-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s16_le(tb_stream_ref_t stream, tb_sint16_t* pvalue); + +/*! block read uint24-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u24_le(tb_stream_ref_t stream, tb_uint32_t* pvalue); + +/*! block read sint24-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s24_le(tb_stream_ref_t stream, tb_sint32_t* pvalue); + +/*! block read uint32-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u32_le(tb_stream_ref_t stream, tb_uint32_t* pvalue); + +/*! block read sint32-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s32_le(tb_stream_ref_t stream, tb_sint32_t* pvalue); + +/*! block read uint64-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u64_le(tb_stream_ref_t stream, tb_uint64_t* pvalue); + +/*! block read sint64-le integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s64_le(tb_stream_ref_t stream, tb_sint64_t* pvalue); + +/*! block read uint16-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u16_be(tb_stream_ref_t stream, tb_uint16_t* pvalue); + +/*! block read sint16-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s16_be(tb_stream_ref_t stream, tb_sint16_t* pvalue); + +/*! block read uint24-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u24_be(tb_stream_ref_t stream, tb_uint32_t* pvalue); + +/*! block read sint24-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s24_be(tb_stream_ref_t stream, tb_sint32_t* pvalue); + +/*! block read uint32-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u32_be(tb_stream_ref_t stream, tb_uint32_t* pvalue); + +/*! block read sint32-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s32_be(tb_stream_ref_t stream, tb_sint32_t* pvalue); + +/*! block read uint64-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_u64_be(tb_stream_ref_t stream, tb_uint64_t* pvalue); + +/*! block read sint64-be integer + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_s64_be(tb_stream_ref_t stream, tb_sint64_t* pvalue); + +/*! block writ uint8 integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u8(tb_stream_ref_t stream, tb_uint8_t value); + +/*! block writ sint8 integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s8(tb_stream_ref_t stream, tb_sint8_t value); + +/*! block writ uint16-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u16_le(tb_stream_ref_t stream, tb_uint16_t value); + +/*! block writ sint16-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s16_le(tb_stream_ref_t stream, tb_sint16_t value); + +/*! block writ uint24-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u24_le(tb_stream_ref_t stream, tb_uint32_t value); + +/*! block writ sint24-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s24_le(tb_stream_ref_t stream, tb_sint32_t value); + +/*! block writ uint32-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u32_le(tb_stream_ref_t stream, tb_uint32_t value); + +/*! block writ sint32-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s32_le(tb_stream_ref_t stream, tb_sint32_t value); + +/*! block writ uint64-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u64_le(tb_stream_ref_t stream, tb_uint64_t value); + +/*! block writ sint64-le integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s64_le(tb_stream_ref_t stream, tb_sint64_t value); + +/*! block writ uint16-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u16_be(tb_stream_ref_t stream, tb_uint16_t value); + +/*! block writ sint16-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s16_be(tb_stream_ref_t stream, tb_sint16_t value); + +/*! block writ uint24-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u24_be(tb_stream_ref_t stream, tb_uint32_t value); + +/*! block writ sint24-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s24_be(tb_stream_ref_t stream, tb_sint32_t value); + +/*! block writ uint32-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u32_be(tb_stream_ref_t stream, tb_uint32_t value); + +/*! block writ sint32-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s32_be(tb_stream_ref_t stream, tb_sint32_t value); + +/*! block writ uint64-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_u64_be(tb_stream_ref_t stream, tb_uint64_t value); + +/*! block writ sint64-be integer + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_s64_be(tb_stream_ref_t stream, tb_sint64_t value); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +/*! read float-le number + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_float_le(tb_stream_ref_t stream, tb_float_t* pvalue); + +/*! read float-be number + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_float_be(tb_stream_ref_t stream, tb_float_t* pvalue); + +/*! read double-ble number + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_double_ble(tb_stream_ref_t stream, tb_double_t* pvalue); + +/*! read double-bbe number + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_double_bbe(tb_stream_ref_t stream, tb_double_t* pvalue); + +/*! read double-lle number + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_double_lle(tb_stream_ref_t stream, tb_double_t* pvalue); + +/*! read double-lbe number + * + * @param stream the stream + * @param pvalue the value pointer + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bread_double_lbe(tb_stream_ref_t stream, tb_double_t* pvalue); + +/*! writ float-le number + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_float_le(tb_stream_ref_t stream, tb_float_t value); + +/*! writ float-be number + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_float_be(tb_stream_ref_t stream, tb_float_t value); + +/*! writ double-ble number + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_double_ble(tb_stream_ref_t stream, tb_double_t value); + +/*! writ double-bbe number + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_double_bbe(tb_stream_ref_t stream, tb_double_t value); + +/*! writ double-lle number + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_double_lle(tb_stream_ref_t stream, tb_double_t value); + +/*! writ double-lbe number + * + * @param stream the stream + * @param value the value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_stream_bwrit_double_lbe(tb_stream_ref_t stream, tb_double_t value); + +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/stream/transfer.h b/core/pkg/tbox.pkg/inc/tbox/stream/transfer.h index d09fdca10..68dbc0a56 100644 --- a/core/pkg/tbox.pkg/inc/tbox/stream/transfer.h +++ b/core/pkg/tbox.pkg/inc/tbox/stream/transfer.h @@ -163,3 +163,168 @@ tb_hong_t tb_transfer_data_to_stream(tb_byte_t const* idata, tb_size_t __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file transfer.h + * @ingroup stream + * + */ +#ifndef TB_STREAM_TRANSFER_H +#define TB_STREAM_TRANSFER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the transfer func type + * + * @param state the stream state + * @param offset the istream offset + * @param size the istream size, no size: -1 + * @param save the saved size + * @param rate the current rate, bytes/s + * @param priv the func private data + * + * @return tb_true: ok and continue it if need, tb_false: break it + */ +typedef tb_bool_t (*tb_transfer_func_t)(tb_size_t state, tb_hize_t offset, tb_hong_t size, tb_hize_t save, tb_size_t rate, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! transfer stream to stream + * + * @param istream the istream + * @param ostream the ostream + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer stream to url + * + * @param istream the istream + * @param ourl the output url + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_to_url(tb_stream_ref_t istream, tb_char_t const* ourl, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer stream to data + * + * @param istream the istream + * @param odata the output data + * @param osize the output size + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_to_data(tb_stream_ref_t istream, tb_byte_t* odata, tb_size_t osize, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer url to url + * + * @param iurl the input url + * @param ourl the output url + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_url(tb_char_t const* iurl, tb_char_t const* ourl, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer url to stream + * + * @param iurl the input url + * @param ostream the ostream + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_url_to_stream(tb_char_t const* iurl, tb_stream_ref_t ostream, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer url to data + * + * @param iurl the input url + * @param odata the output data + * @param osize the output size + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_url_to_data(tb_char_t const* iurl, tb_byte_t* odata, tb_size_t osize, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer data to url + * + * @param idata the input data + * @param isize the input size + * @param ourl the output url + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_data_to_url(tb_byte_t const* idata, tb_size_t isize, tb_char_t const* ourl, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/*! transfer data to stream + * + * @param idata the input data + * @param isize the input size + * @param ostream the ostream + * @param lrate the limit rate and no limit if 0, bytes/s + * @param func the save func and be optional + * @param priv the func private data + * + * @return the saved size, failed: -1 + */ +tb_hong_t tb_transfer_data_to_stream(tb_byte_t const* idata, tb_size_t isize, tb_stream_ref_t ostream, tb_size_t lrate, tb_transfer_func_t func, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/string/prefix.h b/core/pkg/tbox.pkg/inc/tbox/string/prefix.h index 197597569..cbb9b0490 100644 --- a/core/pkg/tbox.pkg/inc/tbox/string/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/string/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_STRING_PREFIX_H +#define TB_STRING_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/string/static_string.h b/core/pkg/tbox.pkg/inc/tbox/string/static_string.h index 607118614..2171babe2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/string/static_string.h +++ b/core/pkg/tbox.pkg/inc/tbox/string/static_string.h @@ -399,3 +399,404 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file static_string.h + * @ingroup string + * + */ +#ifndef TB_STRING_STATIC_STRING_H +#define TB_STRING_STATIC_STRING_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../memory/memory.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the static string type +typedef tb_static_buffer_t tb_static_string_t; + +/// the static string ref type +typedef tb_static_buffer_ref_t tb_static_string_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init string + * + * @param string the string + * @param data the data + * @param maxn the maxn + * + * @return tb_true or tb_false + */ +tb_bool_t tb_static_string_init(tb_static_string_ref_t string, tb_char_t* data, tb_size_t maxn); + +/*! exit string + * + * @param string the string + */ +tb_void_t tb_static_string_exit(tb_static_string_ref_t string); + +/*! the c-string pointer + * + * @param string the string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstr(tb_static_string_ref_t string); + +/*! the string size + * + * @param string the string + * + * @return the string size + */ +tb_size_t tb_static_string_size(tb_static_string_ref_t string); + +/*! clear the string + * + * @param string the string + */ +tb_void_t tb_static_string_clear(tb_static_string_ref_t string); + +/*! strip the string + * + * @param string the string + * @param n the striped size + * + * @return the c-string + */ +tb_char_t const* tb_static_string_strip(tb_static_string_ref_t string, tb_size_t n); + +/*! trim the left spaces for string + * + * @param string the string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_ltrim(tb_static_string_ref_t string); + +/*! trim the right spaces for string + * + * @param string the string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_rtrim(tb_static_string_ref_t string); + +/*! get the charactor at the given position + * + * @param string the string + * @param p the position + * + * @return the c-string + */ +tb_char_t tb_static_string_charat(tb_static_string_ref_t string, tb_size_t p); + +/*! find charactor position + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strchr(tb_static_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! find charactor position and ignore case + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strichr(tb_static_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! reverse to find charactor position + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strrchr(tb_static_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! reverse to find charactor position and ignore case + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strirchr(tb_static_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! find string position + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strstr(tb_static_string_ref_t string, tb_size_t p, tb_static_string_ref_t s); + +/*! find string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_stristr(tb_static_string_ref_t string, tb_size_t p, tb_static_string_ref_t s); + +/*! find c-string position + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_cstrstr(tb_static_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! find c-string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_cstristr(tb_static_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! reverse to find string position + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strrstr(tb_static_string_ref_t string, tb_size_t p, tb_static_string_ref_t s); + +/*! reverse to find string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_strirstr(tb_static_string_ref_t string, tb_size_t p, tb_static_string_ref_t s); + +/*! reverse to find c-string position + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_cstrrstr(tb_static_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! reverse to find c-string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_static_string_cstrirstr(tb_static_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! copy string + * + * @param string the string + * @param s the copied string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_strcpy(tb_static_string_ref_t string, tb_static_string_ref_t s); + +/*! copy c-string + * + * @param string the string + * @param s the copied c-string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstrcpy(tb_static_string_ref_t string, tb_char_t const* s); + +/*! copy c-string with the given size + * + * @param string the string + * @param s the copied c-string + * @param n the copied c-string size + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstrncpy(tb_static_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/*! copy format c-string + * + * @param string the string + * @param fmt the copied format c-string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstrfcpy(tb_static_string_ref_t string, tb_char_t const* fmt, ...); + +/*! append charactor + * + * @param string the string + * @param c the appended charactor + * + * @return the c-string + */ +tb_char_t const* tb_static_string_chrcat(tb_static_string_ref_t string, tb_char_t c); + +/*! append charactor with the given size + * + * @param string the string + * @param c the appended charactor + * @param n the appended size + * + * @return the c-string + */ +tb_char_t const* tb_static_string_chrncat(tb_static_string_ref_t string, tb_char_t c, tb_size_t n); + +/*! append string + * + * @param string the string + * @param s the appended string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_strcat(tb_static_string_ref_t string, tb_static_string_ref_t s); + +/*! append c-string + * + * @param string the string + * @param s the appended c-string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstrcat(tb_static_string_ref_t string, tb_char_t const* s); + +/*! append c-string with the given size + * + * @param string the string + * @param s the appended c-string + * @param n the appended c-string size + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstrncat(tb_static_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/*! append format c-string + * + * @param string the string + * @param fmt the appended format c-string + * + * @return the c-string + */ +tb_char_t const* tb_static_string_cstrfcat(tb_static_string_ref_t string, tb_char_t const* fmt, ...); + +/*! compare string + * + * @param string the string + * @param s the compared string + * + * @return equal: 0 + */ +tb_long_t tb_static_string_strcmp(tb_static_string_ref_t string, tb_static_string_ref_t s); + +/*! compare string and ignore case + * + * @param string the string + * @param s the compared string + * + * @return equal: 0 + */ +tb_long_t tb_static_string_strimp(tb_static_string_ref_t string, tb_static_string_ref_t s); + +/*! compare c-string + * + * @param string the string + * @param s the compared c-string + * + * @return equal: 0 + */ +tb_long_t tb_static_string_cstrcmp(tb_static_string_ref_t string, tb_char_t const* s); + +/*! compare c-string and ignore case + * + * @param string the string + * @param s the compared c-string + * + * @return equal: 0 + */ +tb_long_t tb_static_string_cstricmp(tb_static_string_ref_t string, tb_char_t const* s); + +/*! compare c-string with given size + * + * @param string the string + * @param s the compared c-string + * #param n the compared c-string size + * + * @return equal: 0 + */ +tb_long_t tb_static_string_cstrncmp(tb_static_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/*! compare c-string with given size and ignore case + * + * @param string the string + * @param s the compared c-string + * #param n the compared c-string size + * + * @return equal: 0 + */ +tb_long_t tb_static_string_cstrnicmp(tb_static_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/string/string.h b/core/pkg/tbox.pkg/inc/tbox/string/string.h index f535b1856..4e5999f63 100644 --- a/core/pkg/tbox.pkg/inc/tbox/string/string.h +++ b/core/pkg/tbox.pkg/inc/tbox/string/string.h @@ -397,3 +397,402 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file string.h + * @ingroup string + * + */ +#ifndef TB_STRING_H +#define TB_STRING_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "static_string.h" +#include "../memory/memory.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the string type +typedef tb_buffer_t tb_string_t; + +/// the string ref type +typedef tb_buffer_ref_t tb_string_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init string + * + * @param string the string + * + * @return tb_true or tb_false + */ +tb_bool_t tb_string_init(tb_string_ref_t string); + +/*! exit string + * + * @param string the string + */ +tb_void_t tb_string_exit(tb_string_ref_t string); + +/*! the c-string pointer + * + * @param string the string + * + * @return the c-string + */ +tb_char_t const* tb_string_cstr(tb_string_ref_t string); + +/*! the string size + * + * @param string the string + * + * @return the string size + */ +tb_size_t tb_string_size(tb_string_ref_t string); + +/*! clear the string + * + * @param string the string + */ +tb_void_t tb_string_clear(tb_string_ref_t string); + +/*! strip the string + * + * @param string the string + * @param n the striped size + * + * @return the c-string + */ +tb_char_t const* tb_string_strip(tb_string_ref_t string, tb_size_t n); + +/*! trim the left spaces for string + * + * @param string the string + * + * @return the c-string + */ +tb_char_t const* tb_string_ltrim(tb_string_ref_t string); + +/*! trim the right spaces for string + * + * @param string the string + * + * @return the c-string + */ +tb_char_t const* tb_string_rtrim(tb_string_ref_t string); + +/*! get the charactor at the given position + * + * @param string the string + * @param p the position + * + * @return the c-string + */ +tb_char_t tb_string_charat(tb_string_ref_t string, tb_size_t p); + +/*! find charactor position + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strchr(tb_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! find charactor position and ignore case + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strichr(tb_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! reverse to find charactor position + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strrchr(tb_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! reverse to find charactor position and ignore case + * + * @param string the string + * @param p the start position + * @param c the finded charactor + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strirchr(tb_string_ref_t string, tb_size_t p, tb_char_t c); + +/*! find string position + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strstr(tb_string_ref_t string, tb_size_t p, tb_string_ref_t s); + +/*! find string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_stristr(tb_string_ref_t string, tb_size_t p, tb_string_ref_t s); + +/*! find c-string position + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_cstrstr(tb_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! find c-string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_cstristr(tb_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! reverse to find string position + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strrstr(tb_string_ref_t string, tb_size_t p, tb_string_ref_t s); + +/*! reverse to find string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_strirstr(tb_string_ref_t string, tb_size_t p, tb_string_ref_t s); + +/*! reverse to find c-string position + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_cstrrstr(tb_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! reverse to find c-string position and ignore case + * + * @param string the string + * @param p the start position + * @param s the finded c-string + * + * @return the real position, no find: -1 + */ +tb_long_t tb_string_cstrirstr(tb_string_ref_t string, tb_size_t p, tb_char_t const* s); + +/*! copy string + * + * @param string the string + * @param s the copied string + * + * @return the c-string + */ +tb_char_t const* tb_string_strcpy(tb_string_ref_t string, tb_string_ref_t s); + +/*! copy c-string + * + * @param string the string + * @param s the copied c-string + * + * @return the c-string + */ +tb_char_t const* tb_string_cstrcpy(tb_string_ref_t string, tb_char_t const* s); + +/*! copy c-string with the given size + * + * @param string the string + * @param s the copied c-string + * @param n the copied c-string size + * + * @return the c-string + */ +tb_char_t const* tb_string_cstrncpy(tb_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/*! copy format c-string + * + * @param string the string + * @param fmt the copied format c-string + * + * @return the c-string + */ +tb_char_t const* tb_string_cstrfcpy(tb_string_ref_t string, tb_char_t const* fmt, ...); + +/*! append charactor + * + * @param string the string + * @param c the appended charactor + * + * @return the c-string + */ +tb_char_t const* tb_string_chrcat(tb_string_ref_t string, tb_char_t c); + +/*! append charactor with the given size + * + * @param string the string + * @param c the appended charactor + * @param n the appended size + * + * @return the c-string + */ +tb_char_t const* tb_string_chrncat(tb_string_ref_t string, tb_char_t c, tb_size_t n); + +/*! append string + * + * @param string the string + * @param s the appended string + * + * @return the c-string + */ +tb_char_t const* tb_string_strcat(tb_string_ref_t string, tb_string_ref_t s); + +/*! append c-string + * + * @param string the string + * @param s the appended c-string + * + * @return the c-string + */ +tb_char_t const* tb_string_cstrcat(tb_string_ref_t string, tb_char_t const* s); + +/*! append c-string with the given size + * + * @param string the string + * @param s the appended c-string + * @param n the appended c-string size + * + * @return the c-string + */ +tb_char_t const* tb_string_cstrncat(tb_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/*! append format c-string + * + * @param string the string + * @param fmt the appended format c-string + * + * @return the c-string + */ +tb_char_t const* tb_string_cstrfcat(tb_string_ref_t string, tb_char_t const* fmt, ...); + +/*! compare string + * + * @param string the string + * @param s the compared string + * + * @return equal: 0 + */ +tb_long_t tb_string_strcmp(tb_string_ref_t string, tb_string_ref_t s); + +/*! compare string and ignore case + * + * @param string the string + * @param s the compared string + * + * @return equal: 0 + */ +tb_long_t tb_string_strimp(tb_string_ref_t string, tb_string_ref_t s); + +/*! compare c-string + * + * @param string the string + * @param s the compared c-string + * + * @return equal: 0 + */ +tb_long_t tb_string_cstrcmp(tb_string_ref_t string, tb_char_t const* s); + +/*! compare c-string and ignore case + * + * @param string the string + * @param s the compared c-string + * + * @return equal: 0 + */ +tb_long_t tb_string_cstricmp(tb_string_ref_t string, tb_char_t const* s); + +/*! compare c-string with given size + * + * @param string the string + * @param s the compared c-string + * #param n the compared c-string size + * + * @return equal: 0 + */ +tb_long_t tb_string_cstrncmp(tb_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/*! compare c-string with given size and ignore case + * + * @param string the string + * @param s the compared c-string + * #param n the compared c-string size + * + * @return equal: 0 + */ +tb_long_t tb_string_cstrnicmp(tb_string_ref_t string, tb_char_t const* s, tb_size_t n); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/tbox.h b/core/pkg/tbox.pkg/inc/tbox/tbox.h index 2c3d85f5e..7f3e29fee 100644 --- a/core/pkg/tbox.pkg/inc/tbox/tbox.h +++ b/core/pkg/tbox.pkg/inc/tbox/tbox.h @@ -202,3 +202,207 @@ tb_version_t const* tb_version(tb_noarg_t); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file tbox.h + * + */ +#ifndef TB_TBOX_H +#define TB_TBOX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "zip/zip.h" +#include "xml/xml.h" +#include "asio/asio.h" +#include "libm/libm.h" +#include "libc/libc.h" +#include "math/math.h" +#include "hash/hash.h" +#include "utils/utils.h" +#include "regex/regex.h" +#include "object/object.h" +#include "memory/memory.h" +#include "stream/stream.h" +#include "string/string.h" +#include "network/network.h" +#include "charset/charset.h" +#include "platform/platform.h" +#include "database/database.h" +#include "algorithm/algorithm.h" +#include "container/container.h" +#include "coroutine/coroutine.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the compile mode +#define TB_MODE_DEBUG (1) +#define TB_MODE_SMALL (2) + +#ifdef __tb_debug__ +# define __tb_mode_debug__ TB_MODE_DEBUG +#else +# define __tb_mode_debug__ (0) +#endif + +#ifdef __tb_small__ +# define __tb_mode_small__ TB_MODE_SMALL +#else +# define __tb_mode_small__ (0) +#endif + +/*! init tbox + * + * @param priv the platform private data + * pass JavaVM* jvm for android jni + * pass tb_null for other platform + * + * @param allocator the allocator, supports: + * + * - tb_native_allocator() + * uses native memory directly + * + * - tb_static_allocator(data, size) + * uses the a static small buffer and we can check memory error and leaking + * + * - tb_default_allocator(data, size) + * uses the a large pool with the static memory and we can check memory error and leaking + * + * - tb_default_allocator(tb_null, 0) + * uses the a large pool with the native memory and we can check memory error and leaking + * + * - tb_null + * uses tb_default_allocator(tb_null, 0) for large mode + * uses tb_native_allocator() for small mode, need define __tb_small__ + * + * @return tb_true or tb_false + * + * @code + * + #include "tbox/tbox.h" + + int main(int argc, char** argv) + { + // init tbox + if (!tb_init(tb_null, tb_null)) return 0; + + // print info with tag + tb_trace_i("hello tbox"); + + // print info only for debug + tb_trace_d("hello tbox"); + + // print error info + tb_trace_e("hello tbox"); + + // init stream + tb_stream_ref_t stream = tb_stream_init_from_url("http://www.xxxx.com/index.html"); + if (stream) + { + // save stream data to file + tb_transfer_to_url(stream, "/home/file/index.html", 0, tb_null, tb_null); + + // exit stream + tb_stream_exit(stream); + } + + // ... + + // exit tbox + tb_exit(); + return 0; + } + * @endcode + */ +#define tb_init(priv, allocator) tb_init_(priv, allocator, (tb_size_t)(__tb_mode_debug__ | __tb_mode_small__), TB_VERSION_BUILD) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the tbox library + * + * @param priv the platform private data + * pass JavaVM* jvm for android jni + * pass tb_null for other platform + * + * @param allocator the allocator, supports: + * + * - tb_native_allocator() + * uses native memory directly + * + * - tb_static_allocator(data, size) + * uses the a static small buffer and we can check memory error and leaking + * + * - tb_default_allocator(data, size) + * uses the a large pool with the static memory and we can check memory error and leaking + * + * - tb_default_allocator(tb_null, 0) + * uses the a large pool with the native memory and we can check memory error and leaking + * + * - tb_null + * uses tb_default_allocator(tb_null, 0) for large mode + * uses tb_native_allocator() for small mode, need define __tb_small__ + * + * @param mode the compile mode for check __tb_small__ and __tb_debug__ + * @param build the build version + * + * @return tb_true or tb_false + */ +tb_bool_t tb_init_(tb_handle_t priv, tb_allocator_ref_t allocator, tb_size_t mode, tb_hize_t build); + +/// exit the tbox library +tb_void_t tb_exit(tb_noarg_t); + +/*! the state + * + * - TB_STATE_OK + * - TB_STATE_END + * - TB_STATE_EXITING + * + * @return the tbox state + */ +tb_size_t tb_state(tb_noarg_t); + +#ifdef TB_CONFIG_INFO_HAVE_VERSION +/*! the tbox version + * + * @return the tbox version + */ +tb_version_t const* tb_version(tb_noarg_t); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/base32.h b/core/pkg/tbox.pkg/inc/tbox/utils/base32.h index 79af4126e..b241bd35b 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/base32.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/base32.h @@ -69,3 +69,74 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file base32.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_BASE32_H +#define TB_UTILS_BASE32_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! encode base32 + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_base32_encode(tb_byte_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! decode base32 + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_base32_decode(tb_byte_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/base64.h b/core/pkg/tbox.pkg/inc/tbox/utils/base64.h index 13b270b37..2f63cb591 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/base64.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/base64.h @@ -69,3 +69,74 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file base.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_BASE64_H +#define TB_UTILS_BASE64_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! encode base64 + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_base64_encode(tb_byte_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! encode base64 + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_base64_decode(tb_char_t const* ib, tb_size_t in, tb_byte_t* ob, tb_size_t on); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/bits.h b/core/pkg/tbox.pkg/inc/tbox/utils/bits.h index 933a3c4c9..d977ec334 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/bits.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/bits.h @@ -1036,3 +1036,1041 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bits.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_BITS_H +#define TB_UTILS_BITS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../libm/libm.h" +#if defined(TB_COMPILER_IS_GCC) +# include "impl/bits_gcc.h" +#endif +#if defined(TB_ARCH_x86) || defined(TB_ARCH_x64) +# include "impl/bits_x86.h" +#elif defined(TB_ARCH_ARM) +# include "impl/bits_arm.h" +#elif defined(TB_ARCH_SH4) +# include "impl/bits_sh4.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +/* TODO + * + * check TB_UNALIGNED_ACCESS_ENABLE for compiler + */ +//#define TB_UNALIGNED_ACCESS_ENABLE + +// 1-bits +#define tb_bits_get_u1(p) (((*(p)) >> 7) & 1) +#define tb_bits_set_u1(p, x) do { *(p) &= 0x7f; *(p) |= (((x) & 0x1) << 7); } while (0) + +// 8-bits +#define tb_bits_get_u8(p) (*(p)) +#define tb_bits_get_s8(p) (*(p)) + +#define tb_bits_set_u8(p, x) do { *(p) = (tb_uint8_t)(x); } while (0) +#define tb_bits_set_s8(p, x) do { *(p) = (tb_sint8_t)(x); } while (0) + +// 16-bits +#define tb_bits_get_u16_le_impl(p) ((tb_uint16_t)((tb_uint16_t)*((p) + 1) << 8 | (tb_uint16_t)*(p))) +#define tb_bits_get_s16_le_impl(p) tb_bits_get_u16_le_impl(p) +#define tb_bits_get_u16_be_impl(p) ((tb_uint16_t)(*((p)) << 8 | (tb_uint16_t)*((p) + 1))) +#define tb_bits_get_s16_be_impl(p) tb_bits_get_u16_be_impl(p) +#define tb_bits_get_u16_ne_impl(p) (*((tb_uint16_t*)(p))) +#define tb_bits_get_s16_ne_impl(p) tb_bits_get_u16_ne_impl(p) + +#define tb_bits_set_u16_le_impl(p, x) tb_bits_set_u16_le_inline(p, x) +#define tb_bits_set_s16_le_impl(p, x) tb_bits_set_u16_le_inline(p, x) +#define tb_bits_set_u16_be_impl(p, x) tb_bits_set_u16_be_inline(p, x) +#define tb_bits_set_s16_be_impl(p, x) tb_bits_set_u16_be_inline(p, x) +#define tb_bits_set_u16_ne_impl(p, x) do { *((tb_uint16_t*)(p)) = (tb_uint16_t)(x); } while (0) +#define tb_bits_set_s16_ne_impl(p, x) tb_bits_set_u16_ne_impl(p, x) + +// 24-bits +#define tb_bits_get_u24_le_impl(p) ((tb_uint32_t)(*((p) + 2) << 16 | *((p) + 1) << 8 | *(p))) +#define tb_bits_get_s24_le_impl(p) ((tb_bits_get_u24_le_impl(p) + 0xff800000) ^ 0xff800000) +#define tb_bits_get_u24_be_impl(p) ((tb_uint32_t)(*(p) << 16 | *((p) + 1) << 8 | *((p) + 2))) +#define tb_bits_get_s24_be_impl(p) ((tb_bits_get_u24_be_impl(p) + 0xff800000) ^ 0xff800000) +#define tb_bits_get_u24_ne_impl(p) ((tb_uint32_t)(*((tb_uint32_t*)(p)) & 0x00ffffff)) +#define tb_bits_get_s24_ne_impl(p) ((tb_bits_get_u24_ne_impl(p) + 0xff800000) ^ 0xff800000) + +#define tb_bits_set_u24_le_impl(p, x) tb_bits_set_u24_le_inline(p, x) +#define tb_bits_set_s24_le_impl(p, x) tb_bits_set_u24_le_inline(p, x) +#define tb_bits_set_u24_be_impl(p, x) tb_bits_set_u24_be_inline(p, x) +#define tb_bits_set_s24_be_impl(p, x) tb_bits_set_u24_be_inline(p, x) +#define tb_bits_set_u24_ne_impl(p, x) do { *((tb_uint32_t*)(p)) = (tb_uint32_t)(x) & 0x00ffffff; } while (0) +#define tb_bits_set_s24_ne_impl(p, x) tb_bits_set_u24_ne_impl(p, x) + +// 32-bits +#define tb_bits_get_u32_le_impl(p) ((tb_uint32_t)(*((p) + 3) << 24 | *((p) + 2) << 16 | *((p) + 1) << 8 | *(p))) +#define tb_bits_get_s32_le_impl(p) tb_bits_get_u32_le_impl(p) +#define tb_bits_get_u32_be_impl(p) ((tb_uint32_t)(*(p) << 24 | *((p) + 1) << 16 | *((p) + 2) << 8 | *((p) + 3))) +#define tb_bits_get_s32_be_impl(p) tb_bits_get_u32_be_impl(p) +#define tb_bits_get_u32_ne_impl(p) (*((tb_uint32_t*)(p))) +#define tb_bits_get_s32_ne_impl(p) tb_bits_get_u32_ne_impl(p) + +#define tb_bits_set_u32_le_impl(p, x) tb_bits_set_u32_le_inline(p, x) +#define tb_bits_set_s32_le_impl(p, x) tb_bits_set_u32_le_inline(p, x) +#define tb_bits_set_u32_be_impl(p, x) tb_bits_set_u32_be_inline(p, x) +#define tb_bits_set_s32_be_impl(p, x) tb_bits_set_u32_be_inline(p, x) +#define tb_bits_set_u32_ne_impl(p, x) do { *((tb_uint32_t*)(p)) = (tb_uint32_t)(x); } while (0) +#define tb_bits_set_s32_ne_impl(p, x) tb_bits_set_u32_ne_impl(p, x) + +// 64-bits +#define tb_bits_get_u64_le_impl(p) ((tb_uint64_t)*((p) + 7) << 56 | (tb_uint64_t)*((p) + 6) << 48 | (tb_uint64_t)*((p) + 5) << 40 | (tb_uint64_t)*((p) + 4) << 32 | (tb_uint64_t)*((p) + 3) << 24 | (tb_uint64_t)*((p) + 2) << 16 | (tb_uint64_t)*((p) + 1) << 8 | (tb_uint64_t)*(p)) +#define tb_bits_get_s64_le_impl(p) tb_bits_get_u64_le_impl(p) +#define tb_bits_get_u64_be_impl(p) ((tb_uint64_t)*(p) << 56 | (tb_uint64_t)*((p) + 1) << 48 | (tb_uint64_t)*((p) + 2) << 40 | (tb_uint64_t)*((p) + 3) << 32 | (tb_uint64_t)*((p) + 4) << 24 | (tb_uint64_t)*((p) + 5) << 16 | (tb_uint64_t)*((p) + 6) << 8 | (tb_uint64_t)*((p) + 7)) +#define tb_bits_get_s64_be_impl(p) tb_bits_get_u64_be_impl(p) +#define tb_bits_get_u64_ne_impl(p) (*((tb_uint64_t*)(p))) +#define tb_bits_get_s64_ne_impl(p) tb_bits_get_u64_ne_impl(p) + +#define tb_bits_set_u64_le_impl(p, x) tb_bits_set_u64_le_inline(p, x) +#define tb_bits_set_s64_le_impl(p, x) tb_bits_set_u64_le_inline(p, x) +#define tb_bits_set_u64_be_impl(p, x) tb_bits_set_u64_be_inline(p, x) +#define tb_bits_set_s64_be_impl(p, x) tb_bits_set_u64_be_inline(p, x) +#define tb_bits_set_u64_ne_impl(p, x) do { *((tb_uint64_t*)(p)) = (tb_uint64_t)(x); } while (0) +#define tb_bits_set_s64_ne_impl(p, x) tb_bits_set_u64_ne_impl(p, x) + +// float +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +# define tb_bits_get_float_le(p) tb_bits_get_float_le_inline(p) +# define tb_bits_get_float_be(p) tb_bits_get_float_be_inline(p) + +# define tb_bits_set_float_le(p, x) tb_bits_set_float_le_inline(p, x) +# define tb_bits_set_float_be(p, x) tb_bits_set_float_be_inline(p, x) + +# define tb_bits_get_double_ble(p) tb_bits_get_double_ble_inline(p) +# define tb_bits_get_double_bbe(p) tb_bits_get_double_bbe_inline(p) + +# define tb_bits_get_double_lle(p) tb_bits_get_double_lle_inline(p) +# define tb_bits_get_double_lbe(p) tb_bits_get_double_lbe_inline(p) + +# define tb_bits_set_double_ble(p, x) tb_bits_set_double_ble_inline(p, x) +# define tb_bits_set_double_bbe(p, x) tb_bits_set_double_bbe_inline(p, x) + +# define tb_bits_set_double_lle(p, x) tb_bits_set_double_lle_inline(p, x) +# define tb_bits_set_double_lbe(p, x) tb_bits_set_double_lbe_inline(p, x) + +# ifdef TB_FLOAT_BIGENDIAN +# define tb_bits_get_double_nbe(p) tb_bits_get_double_bbe(p) +# define tb_bits_get_double_nle(p) tb_bits_get_double_ble(p) + +# define tb_bits_set_double_nbe(p, x) tb_bits_set_double_bbe(p, x) +# define tb_bits_set_double_nle(p, x) tb_bits_set_double_ble(p, x) +# else +# define tb_bits_get_double_nbe(p) tb_bits_get_double_lbe(p) +# define tb_bits_get_double_nle(p) tb_bits_get_double_lle(p) + +# define tb_bits_set_double_nbe(p, x) tb_bits_set_double_lbe(p, x) +# define tb_bits_set_double_nle(p, x) tb_bits_set_double_lle(p, x) +# endif +# ifdef TB_WORDS_BIGENDIAN +# define tb_bits_get_float_ne(p) tb_bits_get_float_be(p) +# define tb_bits_set_float_ne(p, x) tb_bits_set_float_be(p, x) + +# define tb_bits_get_double_nne(p) tb_bits_get_double_nbe(p) +# define tb_bits_get_double_bne(p) tb_bits_get_double_bbe(p) +# define tb_bits_get_double_lne(p) tb_bits_get_double_lbe(p) + +# define tb_bits_set_double_nne(p, x) tb_bits_set_double_nbe(p, x) +# define tb_bits_set_double_bne(p, x) tb_bits_set_double_bbe(p, x) +# define tb_bits_set_double_lne(p, x) tb_bits_set_double_lbe(p, x) +# else +# define tb_bits_get_float_ne(p) tb_bits_get_float_le(p) +# define tb_bits_set_float_ne(p, x) tb_bits_set_float_le(p, x) + +# define tb_bits_get_double_nne(p) tb_bits_get_double_nle(p) +# define tb_bits_get_double_bne(p) tb_bits_get_double_ble(p) +# define tb_bits_get_double_lne(p) tb_bits_get_double_lle(p) + +# define tb_bits_set_double_nne(p, x) tb_bits_set_double_nle(p, x) +# define tb_bits_set_double_bne(p, x) tb_bits_set_double_ble(p, x) +# define tb_bits_set_double_lne(p, x) tb_bits_set_double_lle(p, x) +# endif +#endif + +#ifdef TB_UNALIGNED_ACCESS_ENABLE + +# ifdef TB_WORDS_BIGENDIAN +// 16-bits +# define tb_bits_get_u16_le(p) tb_bits_get_u16_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s16_le(p) tb_bits_get_s16_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u16_be(p) tb_bits_get_u16_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s16_be(p) tb_bits_get_s16_ne_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u16_le(p, x) tb_bits_set_u16_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s16_le(p, x) tb_bits_set_s16_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u16_be(p, x) tb_bits_set_u16_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s16_be(p, x) tb_bits_set_s16_ne_impl((tb_byte_t*)(p), x) + +// 24-bits +# define tb_bits_get_u24_le(p) tb_bits_get_u24_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s24_le(p) tb_bits_get_s24_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u24_be(p) tb_bits_get_u24_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s24_be(p) tb_bits_get_s24_ne_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u24_le(p, x) tb_bits_set_u24_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s24_le(p, x) tb_bits_set_s24_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u24_be(p, x) tb_bits_set_u24_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s24_be(p, x) tb_bits_set_s24_ne_impl((tb_byte_t*)(p), x) + +// 32-bits +# define tb_bits_get_u32_le(p) tb_bits_get_u32_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s32_le(p) tb_bits_get_s32_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u32_be(p) tb_bits_get_u32_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s32_be(p) tb_bits_get_s32_ne_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u32_le(p, x) tb_bits_set_u32_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s32_le(p, x) tb_bits_set_u32_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u32_be(p, x) tb_bits_set_u32_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s32_be(p, x) tb_bits_set_s32_ne_impl((tb_byte_t*)(p), x) + +// 64-bits +# define tb_bits_get_u64_le(p) tb_bits_get_u64_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s64_le(p) tb_bits_get_s64_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u64_be(p) tb_bits_get_u64_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s64_be(p) tb_bits_get_s64_ne_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u64_le(p, x) tb_bits_set_u64_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s64_le(p, x) tb_bits_set_u64_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u64_be(p, x) tb_bits_set_u64_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s64_be(p, x) tb_bits_set_s64_ne_impl((tb_byte_t*)(p), x) + +# else + +// 16-bits +# define tb_bits_get_u16_le(p) tb_bits_get_u16_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s16_le(p) tb_bits_get_s16_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_u16_be(p) tb_bits_get_u16_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s16_be(p) tb_bits_get_s16_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u16_le(p, x) tb_bits_set_u16_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s16_le(p, x) tb_bits_set_s16_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u16_be(p, x) tb_bits_set_u16_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s16_be(p, x) tb_bits_set_s16_be_impl((tb_byte_t*)(p), x) + +// 24-bits +# define tb_bits_get_u24_le(p) tb_bits_get_u24_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s24_le(p) tb_bits_get_s24_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_u24_be(p) tb_bits_get_u24_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s24_be(p) tb_bits_get_s24_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u24_le(p, x) tb_bits_set_u24_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s24_le(p, x) tb_bits_set_s24_ne_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u24_be(p, x) tb_bits_set_u24_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s24_be(p, x) tb_bits_set_s24_be_impl((tb_byte_t*)(p), x) + +// 32-bits +# define tb_bits_get_u32_le(p) tb_bits_get_u32_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s32_le(p) tb_bits_get_s32_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_u32_be(p) tb_bits_get_u32_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s32_be(p) tb_bits_get_s32_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u32_le(p, x) tb_bits_set_u32_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s32_le(p, x) tb_bits_set_u32_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u32_be(p, x) tb_bits_set_u32_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s32_be(p, x) tb_bits_set_s32_be_impl((tb_byte_t*)(p), x) + +// 64-bits +# define tb_bits_get_u64_le(p) tb_bits_get_u64_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_s64_le(p) tb_bits_get_s64_ne_impl((tb_byte_t*)(p)) +# define tb_bits_get_u64_be(p) tb_bits_get_u64_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s64_be(p) tb_bits_get_s64_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u64_le(p, x) tb_bits_set_u64_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s64_le(p, x) tb_bits_set_u64_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u64_be(p, x) tb_bits_set_u64_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s64_be(p, x) tb_bits_set_s64_be_impl((tb_byte_t*)(p), x) + +# endif /* TB_WORDS_BIGENDIAN */ + +#else +// 16-bits +# define tb_bits_get_u16_le(p) tb_bits_get_u16_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s16_le(p) tb_bits_get_s16_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u16_be(p) tb_bits_get_u16_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s16_be(p) tb_bits_get_s16_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u16_le(p, x) tb_bits_set_u16_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s16_le(p, x) tb_bits_set_s16_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u16_be(p, x) tb_bits_set_u16_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s16_be(p, x) tb_bits_set_s16_be_impl((tb_byte_t*)(p), x) + +// 24-bits +# define tb_bits_get_u24_le(p) tb_bits_get_u24_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s24_le(p) tb_bits_get_s24_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u24_be(p) tb_bits_get_u24_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s24_be(p) tb_bits_get_s24_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u24_le(p, x) tb_bits_set_u24_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s24_le(p, x) tb_bits_set_s24_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u24_be(p, x) tb_bits_set_u24_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s24_be(p, x) tb_bits_set_s24_be_impl((tb_byte_t*)(p), x) + +// 32-bits +# define tb_bits_get_u32_le(p) tb_bits_get_u32_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s32_le(p) tb_bits_get_s32_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u32_be(p) tb_bits_get_u32_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s32_be(p) tb_bits_get_s32_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u32_le(p, x) tb_bits_set_u32_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s32_le(p, x) tb_bits_set_u32_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u32_be(p, x) tb_bits_set_u32_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s32_be(p, x) tb_bits_set_s32_be_impl((tb_byte_t*)(p), x) + +// 64-bits +# define tb_bits_get_u64_le(p) tb_bits_get_u64_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_s64_le(p) tb_bits_get_s64_le_impl((tb_byte_t*)(p)) +# define tb_bits_get_u64_be(p) tb_bits_get_u64_be_impl((tb_byte_t*)(p)) +# define tb_bits_get_s64_be(p) tb_bits_get_s64_be_impl((tb_byte_t*)(p)) + +# define tb_bits_set_u64_le(p, x) tb_bits_set_u64_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s64_le(p, x) tb_bits_set_u64_le_impl((tb_byte_t*)(p), x) +# define tb_bits_set_u64_be(p, x) tb_bits_set_u64_be_impl((tb_byte_t*)(p), x) +# define tb_bits_set_s64_be(p, x) tb_bits_set_s64_be_impl((tb_byte_t*)(p), x) + +#endif /* TB_UNALIGNED_ACCESS_ENABLE */ + +#ifdef TB_WORDS_BIGENDIAN +# define tb_bits_get_u16_ne(p) tb_bits_get_u16_be(p) +# define tb_bits_get_s16_ne(p) tb_bits_get_s16_be(p) +# define tb_bits_get_u24_ne(p) tb_bits_get_u24_be(p) +# define tb_bits_get_s24_ne(p) tb_bits_get_s24_be(p) +# define tb_bits_get_u32_ne(p) tb_bits_get_u32_be(p) +# define tb_bits_get_s32_ne(p) tb_bits_get_s32_be(p) +# define tb_bits_get_u64_ne(p) tb_bits_get_u64_be(p) +# define tb_bits_get_s64_ne(p) tb_bits_get_s64_be(p) + +# define tb_bits_set_u16_ne(p, x) tb_bits_set_u16_be(p, x) +# define tb_bits_set_s16_ne(p, x) tb_bits_set_s16_be(p, x) +# define tb_bits_set_u24_ne(p, x) tb_bits_set_u24_be(p, x) +# define tb_bits_set_s24_ne(p, x) tb_bits_set_s24_be(p, x) +# define tb_bits_set_u32_ne(p, x) tb_bits_set_u32_be(p, x) +# define tb_bits_set_s32_ne(p, x) tb_bits_set_s32_be(p, x) +# define tb_bits_set_u64_ne(p, x) tb_bits_set_u64_be(p, x) +# define tb_bits_set_s64_ne(p, x) tb_bits_set_s64_be(p, x) + +#else +# define tb_bits_get_u16_ne(p) tb_bits_get_u16_le(p) +# define tb_bits_get_s16_ne(p) tb_bits_get_s16_le(p) +# define tb_bits_get_u24_ne(p) tb_bits_get_u24_le(p) +# define tb_bits_get_s24_ne(p) tb_bits_get_s24_le(p) +# define tb_bits_get_u32_ne(p) tb_bits_get_u32_le(p) +# define tb_bits_get_s32_ne(p) tb_bits_get_s32_le(p) +# define tb_bits_get_u64_ne(p) tb_bits_get_u64_le(p) +# define tb_bits_get_s64_ne(p) tb_bits_get_s64_le(p) + +# define tb_bits_set_u16_ne(p, x) tb_bits_set_u16_le(p, x) +# define tb_bits_set_s16_ne(p, x) tb_bits_set_s16_le(p, x) +# define tb_bits_set_u24_ne(p, x) tb_bits_set_u24_le(p, x) +# define tb_bits_set_s24_ne(p, x) tb_bits_set_s24_le(p, x) +# define tb_bits_set_u32_ne(p, x) tb_bits_set_u32_le(p, x) +# define tb_bits_set_s32_ne(p, x) tb_bits_set_s32_le(p, x) +# define tb_bits_set_u64_ne(p, x) tb_bits_set_u64_le(p, x) +# define tb_bits_set_s64_ne(p, x) tb_bits_set_s64_le(p, x) + +#endif /* TB_WORDS_BIGENDIAN */ + +// swap +#ifndef tb_bits_swap_u16 +# define tb_bits_swap_u16(x) tb_bits_swap_u16_inline((tb_uint16_t)(x)) +#endif + +#ifndef tb_bits_swap_u24 +# define tb_bits_swap_u24(x) tb_bits_swap_u24_inline((tb_uint32_t)(x)) +#endif + +#ifndef tb_bits_swap_u32 +# define tb_bits_swap_u32(x) tb_bits_swap_u32_inline((tb_uint32_t)(x)) +#endif + +#ifndef tb_bits_swap_u64 +# define tb_bits_swap_u64(x) tb_bits_swap_u64_inline((tb_uint64_t)(x)) +#endif + +#ifdef TB_WORDS_BIGENDIAN +# define tb_bits_be_to_ne_u16(x) ((tb_uint16_t)(x)) +# define tb_bits_le_to_ne_u16(x) tb_bits_swap_u16(x) +# define tb_bits_be_to_ne_u24(x) ((tb_uint32_t)(x) & 0x00ffffff) +# define tb_bits_le_to_ne_u24(x) tb_bits_swap_u24(x) +# define tb_bits_be_to_ne_u32(x) ((tb_uint32_t)(x)) +# define tb_bits_le_to_ne_u32(x) tb_bits_swap_u32(x) +# define tb_bits_be_to_ne_u64(x) ((tb_uint64_t)(x)) +# define tb_bits_le_to_ne_u64(x) tb_bits_swap_u64(x) +#else +# define tb_bits_be_to_ne_u16(x) tb_bits_swap_u16(x) +# define tb_bits_le_to_ne_u16(x) ((tb_uint16_t)(x)) +# define tb_bits_be_to_ne_u24(x) tb_bits_swap_u24(x) +# define tb_bits_le_to_ne_u24(x) ((tb_uint32_t)(x) & 0x00ffffff) +# define tb_bits_be_to_ne_u32(x) tb_bits_swap_u32(x) +# define tb_bits_le_to_ne_u32(x) ((tb_uint32_t)(x)) +# define tb_bits_be_to_ne_u64(x) tb_bits_swap_u64(x) +# define tb_bits_le_to_ne_u64(x) ((tb_uint64_t)(x)) +#endif + +#define tb_bits_ne_to_be_u16(x) tb_bits_be_to_ne_u16(x) +#define tb_bits_ne_to_le_u16(x) tb_bits_le_to_ne_u16(x) +#define tb_bits_ne_to_be_u24(x) tb_bits_be_to_ne_u24(x) +#define tb_bits_ne_to_le_u24(x) tb_bits_le_to_ne_u24(x) +#define tb_bits_ne_to_be_u32(x) tb_bits_be_to_ne_u32(x) +#define tb_bits_ne_to_le_u32(x) tb_bits_le_to_ne_u32(x) +#define tb_bits_ne_to_be_u64(x) tb_bits_be_to_ne_u64(x) +#define tb_bits_ne_to_le_u64(x) tb_bits_le_to_ne_u64(x) + +// cl0, count leading bit 0 +#ifndef tb_bits_cl0_u32_be +# define tb_bits_cl0_u32_be(x) tb_bits_cl0_u32_be_inline(x) +#endif +#ifndef tb_bits_cl0_u32_le +# define tb_bits_cl0_u32_le(x) tb_bits_cl0_u32_le_inline(x) +#endif +#ifndef tb_bits_cl0_u64_be +# define tb_bits_cl0_u64_be(x) tb_bits_cl0_u64_be_inline(x) +#endif +#ifndef tb_bits_cl0_u64_le +# define tb_bits_cl0_u64_le(x) tb_bits_cl0_u64_le_inline(x) +#endif + +// cl1, count leading bit 1 +#ifndef tb_bits_cl1_u32_be +# define tb_bits_cl1_u32_be(x) tb_bits_cl0_u32_be(~(tb_uint32_t)(x)) +#endif +#ifndef tb_bits_cl1_u32_le +# define tb_bits_cl1_u32_le(x) tb_bits_cl0_u32_le(~(tb_uint32_t)(x)) +#endif +#ifndef tb_bits_cl1_u64_be +# define tb_bits_cl1_u64_be(x) tb_bits_cl0_u64_be(~(tb_uint64_t)(x)) +#endif +#ifndef tb_bits_cl1_u64_le +# define tb_bits_cl1_u64_le(x) tb_bits_cl0_u64_le(~(tb_uint64_t)(x)) +#endif + +// cb1, count bit 1 +#ifndef tb_bits_cb1_u32 +# define tb_bits_cb1_u32(x) tb_bits_cb1_u32_inline(x) +#endif +#ifndef tb_bits_cb1_u64 +# define tb_bits_cb1_u64(x) tb_bits_cb1_u64_inline(x) +#endif + +// cb0, count bit 0 +#ifndef tb_bits_cb0_u32 +# define tb_bits_cb0_u32(x) ((x)? (tb_size_t)tb_bits_cb1_u32(~(tb_uint32_t)(x)) : 32) +#endif +#ifndef tb_bits_cb0_u64 +# define tb_bits_cb0_u64(x) ((x)? (tb_size_t)tb_bits_cb1_u64(~(tb_uint64_t)(x)) : 64) +#endif + +/* fb0, find the first bit 0 + * + * find bit zero by little endian, fb0(...11101101) == 1 + * find bit zero by big endian, fb0(...11101101) == 27 + */ +#ifndef tb_bits_fb0_u32_be +# define tb_bits_fb0_u32_be(x) ((x)? tb_bits_cl0_u32_be(~(tb_uint32_t)(x)) : 0) +#endif +#ifndef tb_bits_fb0_u32_le +# define tb_bits_fb0_u32_le(x) ((x)? tb_bits_cl0_u32_le(~(tb_uint32_t)(x)) : 0) +#endif +#ifndef tb_bits_fb0_u64_be +# define tb_bits_fb0_u64_be(x) ((x)? tb_bits_cl0_u64_be(~(tb_uint64_t)(x)) : 0) +#endif +#ifndef tb_bits_fb0_u64_le +# define tb_bits_fb0_u64_le(x) ((x)? tb_bits_cl0_u64_le(~(tb_uint64_t)(x)) : 0) +#endif + +// fb1, find the first bit 1 +#ifndef tb_bits_fb1_u32_be +# define tb_bits_fb1_u32_be(x) ((x)? tb_bits_cl0_u32_be(x) : 32) +#endif +#ifndef tb_bits_fb1_u32_le +# define tb_bits_fb1_u32_le(x) ((x)? tb_bits_cl0_u32_le(x) : 32) +#endif +#ifndef tb_bits_fb1_u64_be +# define tb_bits_fb1_u64_be(x) ((x)? tb_bits_cl0_u64_be(x) : 64) +#endif +#ifndef tb_bits_fb1_u64_le +# define tb_bits_fb1_u64_le(x) ((x)? tb_bits_cl0_u64_le(x) : 64) +#endif + +// only for tb_size_t +#if TB_CPU_BIT64 + +# define tb_bits_swap(x) tb_bits_swap_u64(x) + +# define tb_bits_cl0_be(x) tb_bits_cl0_u64_be(x) +# define tb_bits_cl0_le(x) tb_bits_cl0_u64_le(x) +# define tb_bits_cl1_be(x) tb_bits_cl1_u64_be(x) +# define tb_bits_cl1_le(x) tb_bits_cl1_u64_le(x) + +# define tb_bits_fb0_be(x) tb_bits_fb0_u64_be(x) +# define tb_bits_fb0_le(x) tb_bits_fb0_u64_le(x) +# define tb_bits_fb1_be(x) tb_bits_fb1_u64_be(x) +# define tb_bits_fb1_le(x) tb_bits_fb1_u64_le(x) + +# define tb_bits_cb0(x) tb_bits_cb0_u64(x) +# define tb_bits_cb1(x) tb_bits_cb1_u64(x) + +#elif TB_CPU_BIT32 + +# define tb_bits_swap(x) tb_bits_swap_u32(x) + +# define tb_bits_cl0_be(x) tb_bits_cl0_u32_be(x) +# define tb_bits_cl0_le(x) tb_bits_cl0_u32_le(x) +# define tb_bits_cl1_be(x) tb_bits_cl1_u32_be(x) +# define tb_bits_cl1_le(x) tb_bits_cl1_u32_le(x) + +# define tb_bits_fb0_be(x) tb_bits_fb0_u32_be(x) +# define tb_bits_fb0_le(x) tb_bits_fb0_u32_le(x) +# define tb_bits_fb1_be(x) tb_bits_fb1_u32_be(x) +# define tb_bits_fb1_le(x) tb_bits_fb1_u32_le(x) + +# define tb_bits_cb0(x) tb_bits_cb0_u32(x) +# define tb_bits_cb1(x) tb_bits_cb1_u32(x) + +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! get ubits32 integer + * + * @param p the data pointer + * @param b the start bits + * @param n the bits count + * + * @return the ubits32 integer + */ +tb_uint32_t tb_bits_get_ubits32(tb_byte_t const* p, tb_size_t b, tb_size_t n); + +/*! get sbits32 integer + * + * @param p the data pointer + * @param b the start bits + * @param n the bits count + * + * @return the ubits32 integer + */ +tb_sint32_t tb_bits_get_sbits32(tb_byte_t const* p, tb_size_t b, tb_size_t n); + +/*! set ubits32 integer + * + * @param p the data pointer + * @param b the start bits + * @param x the value + * @param n the bits count + */ +tb_void_t tb_bits_set_ubits32(tb_byte_t* p, tb_size_t b, tb_uint32_t x, tb_size_t n); + +/*! set ubits32 integer + * + * @param p the data pointer + * @param b the start bits + * @param x the value + * @param n the bits count + */ +tb_void_t tb_bits_set_sbits32(tb_byte_t* p, tb_size_t b, tb_sint32_t x, tb_size_t n); + +/*! get ubits64 integer + * + * @param p the data pointer + * @param b the start bits + * @param n the bits count + * + * @return the ubits64 integer + */ +tb_uint64_t tb_bits_get_ubits64(tb_byte_t const* p, tb_size_t b, tb_size_t n); + +/*! get sbits64 integer + * + * @param p the data pointer + * @param b the start bits + * @param n the bits count + * + * @return the ubits64 integer + */ +tb_sint64_t tb_bits_get_sbits64(tb_byte_t const* p, tb_size_t b, tb_size_t n); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * bits + */ + +static __tb_inline__ tb_void_t tb_bits_set_u16_le_inline(tb_byte_t* p, tb_uint16_t x) +{ + p[0] = (tb_byte_t)x; + p[1] = (tb_byte_t)(x >> 8); +} +static __tb_inline__ tb_void_t tb_bits_set_u16_be_inline(tb_byte_t* p, tb_uint16_t x) +{ + p[0] = (tb_byte_t)(x >> 8); + p[1] = (tb_byte_t)x; +} +static __tb_inline__ tb_void_t tb_bits_set_u24_le_inline(tb_byte_t* p, tb_uint32_t x) +{ + p[0] = (tb_byte_t)x; + p[1] = (tb_byte_t)(x >> 8); + p[2] = (tb_byte_t)(x >> 16); +} +static __tb_inline__ tb_void_t tb_bits_set_u24_be_inline(tb_byte_t* p, tb_uint32_t x) +{ + p[0] = (tb_byte_t)(x >> 16); + p[1] = (tb_byte_t)(x >> 8); + p[2] = (tb_byte_t)x; +} +static __tb_inline__ tb_void_t tb_bits_set_u32_le_inline(tb_byte_t* p, tb_uint32_t x) +{ + p[0] = (tb_byte_t)x; + p[1] = (tb_byte_t)(x >> 8); + p[2] = (tb_byte_t)(x >> 16); + p[3] = (tb_byte_t)(x >> 24); +} +static __tb_inline__ tb_void_t tb_bits_set_u32_be_inline(tb_byte_t* p, tb_uint32_t x) +{ + p[0] = (tb_byte_t)(x >> 24); + p[1] = (tb_byte_t)(x >> 16); + p[2] = (tb_byte_t)(x >> 8); + p[3] = (tb_byte_t)x; +} +static __tb_inline__ tb_void_t tb_bits_set_u64_le_inline(tb_byte_t* p, tb_uint64_t x) +{ + p[0] = (tb_byte_t)x; + p[1] = (tb_byte_t)(x >> 8); + p[2] = (tb_byte_t)(x >> 16); + p[3] = (tb_byte_t)(x >> 24); + p[4] = (tb_byte_t)(x >> 32); + p[5] = (tb_byte_t)(x >> 40); + p[6] = (tb_byte_t)(x >> 48); + p[7] = (tb_byte_t)(x >> 56); +} +static __tb_inline__ tb_void_t tb_bits_set_u64_be_inline(tb_byte_t* p, tb_uint64_t x) +{ + p[0] = (tb_byte_t)(x >> 56); + p[1] = (tb_byte_t)(x >> 48); + p[2] = (tb_byte_t)(x >> 40); + p[3] = (tb_byte_t)(x >> 32); + p[4] = (tb_byte_t)(x >> 24); + p[5] = (tb_byte_t)(x >> 16); + p[6] = (tb_byte_t)(x >> 8); + p[7] = (tb_byte_t)x; +} + + +/* ////////////////////////////////////////////////////////////////////////////////////// + * swap + */ + +// swap +static __tb_inline__ tb_uint16_t tb_bits_swap_u16_inline(tb_uint16_t x) +{ + x = (x >> 8) | (x << 8); + return x; +} +static __tb_inline__ tb_uint32_t tb_bits_swap_u24_inline(tb_uint32_t x) +{ + return (x >> 16) | (x & 0x0000ff00) | (x << 16); +} +static __tb_inline__ tb_uint32_t tb_bits_swap_u32_inline(tb_uint32_t x) +{ + x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); + x = (x >> 16) | (x << 16); + return x; +} +static __tb_inline__ tb_hize_t tb_bits_swap_u64_inline(tb_hize_t x) +{ + union + { + tb_hize_t u64; + tb_uint32_t u32[2]; + + } w, r; + + w.u64 = x; + + r.u32[0] = tb_bits_swap_u32(w.u32[1]); + r.u32[1] = tb_bits_swap_u32(w.u32[0]); + + return r.u64; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * cl0 + */ +static __tb_inline__ tb_size_t tb_bits_cl0_u32_be_inline(tb_uint32_t x) +{ + tb_check_return_val(x, 32); + + tb_size_t n = 31; + if (x & 0xffff0000) { n -= 16; x >>= 16; } + if (x & 0xff00) { n -= 8; x >>= 8; } + if (x & 0xf0) { n -= 4; x >>= 4; } + if (x & 0xc) { n -= 2; x >>= 2; } + if (x & 0x2) { n--; } + + return n; +} +static __tb_inline__ tb_size_t tb_bits_cl0_u32_le_inline(tb_uint32_t x) +{ + tb_check_return_val(x, 32); + + tb_size_t n = 31; + if (x & 0x0000ffff) { n -= 16; } else x >>= 16; + if (x & 0x00ff) { n -= 8; } else x >>= 8; + if (x & 0x0f) { n -= 4; } else x >>= 4; + if (x & 0x3) { n -= 2; } else x >>= 2; + if (x & 0x1) { n--; } + + return n; +} +static __tb_inline__ tb_size_t tb_bits_cl0_u64_be_inline(tb_uint64_t x) +{ + tb_check_return_val(x, 64); + + tb_size_t n = tb_bits_cl0_u32_be((tb_uint32_t)(x >> 32)); + if (n == 32) n += tb_bits_cl0_u32_be((tb_uint32_t)x); + + return n; +} +static __tb_inline__ tb_size_t tb_bits_cl0_u64_le_inline(tb_uint64_t x) +{ + tb_check_return_val(x, 64); + + tb_size_t n = tb_bits_cl0_u32_le((tb_uint32_t)x); + if (n == 32) n += tb_bits_cl0_u32_le((tb_uint32_t)(x >> 32)); + + return n; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * cb1 + */ +static __tb_inline__ tb_size_t tb_bits_cb1_u32_inline(tb_uint32_t x) +{ + tb_check_return_val(x, 0); + +#if 0 + /* + * 0x55555555 = 01010101010101010101010101010101 + * 0x33333333 = 00110011001100110011001100110011 + * 0x0f0f0f0f = 00001111000011110000111100001111 + * 0x00ff00ff = 00000000111111110000000011111111 + * 0x0000ffff = 00000000000000001111111111111111 + */ + + x = (x & 0x55555555) + ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f); + x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff); + x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff); +#elif 0 + // mit hackmem count + x = x - ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x + (x >> 4)) & 0x0f0f0f0f; + x = x + (x >> 8); + x = x + (x >> 16); + x &= 0x7f; +#elif 0 + x = x - ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x + (x >> 4) & 0x0f0f0f0f); + x = (x * 0x01010101) >> 24; +#elif 0 + x = x - ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x + (x >> 4) & 0x0f0f0f0f) % 255; +#else + x = x - ((x >> 1) & 0x77777777) - ((x >> 2) & 0x33333333) - ((x >> 3) & 0x11111111); + x = (x + (x >> 4)) & 0x0f0f0f0f; + x = (x * 0x01010101) >> 24; +#endif + + return x; +} +static __tb_inline__ tb_size_t tb_bits_cb1_u64_inline(tb_uint64_t x) +{ + tb_check_return_val(x, 0); + +#if 0 + x = x - ((x >> 1) & 0x5555555555555555L); + x = (x & 0x3333333333333333L) + ((x >> 2) & 0x3333333333333333L); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fL; + x = x + (x >> 8); + x = x + (x >> 16); + x = x + (x >> 32); + x &= 0x7f; +#else + x = x - ((x >> 1) & 0x7777777777777777ULL) - ((x >> 2) & 0x3333333333333333ULL) - ((x >> 3) & 0x1111111111111111ULL); + x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL; + x = (x * 0x0101010101010101ULL) >> 56; +#endif + + return (tb_size_t)x; +} + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/* ////////////////////////////////////////////////////////////////////////////////////// + * float + */ +static __tb_inline__ tb_float_t tb_bits_get_float_le_inline(tb_byte_t const* p) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && !defined(TB_WORDS_BIGENDIAN) + return *((tb_float_t*)p); +#else + tb_ieee_float_t conv; + conv.i = tb_bits_get_u32_le(p); + return conv.f; +#endif +} +static __tb_inline__ tb_float_t tb_bits_get_float_be_inline(tb_byte_t const* p) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && defined(TB_WORDS_BIGENDIAN) + return *((tb_float_t*)p); +#else + tb_ieee_float_t conv; + conv.i = tb_bits_get_u32_be(p); + return conv.f; +#endif +} +static __tb_inline__ tb_void_t tb_bits_set_float_le_inline(tb_byte_t* p, tb_float_t x) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && !defined(TB_WORDS_BIGENDIAN) + *((tb_float_t*)p) = x; +#else + tb_ieee_float_t conv; + conv.f = x; + tb_bits_set_u32_le(p, conv.i); +#endif +} +static __tb_inline__ tb_void_t tb_bits_set_float_be_inline(tb_byte_t* p, tb_float_t x) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && defined(TB_WORDS_BIGENDIAN) + *((tb_float_t*)p) = x; +#else + tb_ieee_float_t conv; + conv.f = x; + tb_bits_set_u32_be(p, conv.i); +#endif +} +/* ////////////////////////////////////////////////////////////////////////////////////// + * double + */ +static __tb_inline__ tb_double_t tb_bits_get_double_bbe_inline(tb_byte_t const* p) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && defined(TB_FLOAT_BIGENDIAN) \ + && defined(TB_WORDS_BIGENDIAN) + return *((tb_double_t*)p); +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.i[1] = tb_bits_get_u32_be(p); + conv.i[0] = tb_bits_get_u32_be(p + 4); + + return conv.d; +#endif +} +static __tb_inline__ tb_double_t tb_bits_get_double_ble_inline(tb_byte_t const* p) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && defined(TB_FLOAT_BIGENDIAN) \ + && !defined(TB_WORDS_BIGENDIAN) + return *((tb_double_t*)p); +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + + conv.i[1] = tb_bits_get_u32_le(p); + conv.i[0] = tb_bits_get_u32_le(p + 4); + + return conv.d; +#endif +} +static __tb_inline__ tb_double_t tb_bits_get_double_lbe_inline(tb_byte_t const* p) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && !defined(TB_FLOAT_BIGENDIAN) \ + && defined(TB_WORDS_BIGENDIAN) + return *((tb_double_t*)p); +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.i[0] = tb_bits_get_u32_be(p); + conv.i[1] = tb_bits_get_u32_be(p + 4); + + return conv.d; +#endif +} +static __tb_inline__ tb_double_t tb_bits_get_double_lle_inline(tb_byte_t const* p) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && !defined(TB_FLOAT_BIGENDIAN) \ + && !defined(TB_WORDS_BIGENDIAN) + return *((tb_double_t*)p); +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.i[0] = tb_bits_get_u32_le(p); + conv.i[1] = tb_bits_get_u32_le(p + 4); + return conv.d; +#endif +} +// big double endian & big words endian +// 7 6 5 4 3 2 1 0 +static __tb_inline__ tb_void_t tb_bits_set_double_bbe_inline(tb_byte_t* p, tb_double_t x) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && defined(TB_FLOAT_BIGENDIAN) \ + && defined(TB_WORDS_BIGENDIAN) + *((tb_double_t*)p) = x; +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.d = x; + + tb_bits_set_u32_be(p, conv.i[1]); + tb_bits_set_u32_be(p + 4, conv.i[0]); +#endif +} +// big double endian & litte words endian +// 4 5 6 7 0 1 2 3 +static __tb_inline__ tb_void_t tb_bits_set_double_ble_inline(tb_byte_t* p, tb_double_t x) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && defined(TB_FLOAT_BIGENDIAN) \ + && !defined(TB_WORDS_BIGENDIAN) + *((tb_double_t*)p) = x; +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.d = x; + + tb_bits_set_u32_le(p, conv.i[1]); + tb_bits_set_u32_le(p + 4, conv.i[0]); +#endif +} +// litte double endian & big words endian +// 3 2 1 0 7 6 5 4 +static __tb_inline__ tb_void_t tb_bits_set_double_lbe_inline(tb_byte_t* p, tb_double_t x) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && !defined(TB_FLOAT_BIGENDIAN) \ + && defined(TB_WORDS_BIGENDIAN) + *((tb_double_t*)p) = x; +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.d = x; + + tb_bits_set_u32_be(p, conv.i[0]); + tb_bits_set_u32_be(p + 4, conv.i[1]); +#endif +} +// litte double endian & litte words endian +// 0 1 2 3 4 5 6 7 +static __tb_inline__ tb_void_t tb_bits_set_double_lle_inline(tb_byte_t* p, tb_double_t x) +{ +#if defined(TB_UNALIGNED_ACCESS_ENABLE) \ + && !defined(TB_FLOAT_BIGENDIAN) \ + && !defined(TB_WORDS_BIGENDIAN) + *((tb_double_t*)p) = x; +#else + union + { + tb_uint32_t i[2]; + tb_double_t d; + + } conv; + + conv.d = x; + + tb_bits_set_u32_le(p, conv.i[0]); + tb_bits_set_u32_le(p + 4, conv.i[1]); +#endif +} + + +#endif /* TB_CONFIG_TYPE_HAVE_FLOAT */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/dump.h b/core/pkg/tbox.pkg/inc/tbox/utils/dump.h index 37f2e83b9..4856f64e1 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/dump.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/dump.h @@ -54,3 +54,59 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file dump.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_DUMP_H +#define TB_UTILS_DUMP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! dump data + * + * @param data the data + * @param size the size + */ +tb_void_t tb_dump_data(tb_byte_t const* data, tb_size_t size); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_arm.h b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_arm.h index b02261aa6..a4505ea96 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_arm.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_arm.h @@ -198,3 +198,203 @@ static __tb_inline__ tb_uint32_t tb_bits_get_ubits32_impl_asm(tb_byte_t const* p #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bits_arm.h + * + */ +#ifndef TB_UTILS_IMPL_BITS_ARM_H +#define TB_UTILS_IMPL_BITS_ARM_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#if defined(TB_ASSEMBLER_IS_GAS) && !defined(TB_ARCH_ARM64) + +// swap +#if TB_ARCH_ARM_VERSION >= 6 +# ifndef tb_bits_swap_u16 +# define tb_bits_swap_u16(x) tb_bits_swap_u16_asm(x) +# endif +#endif +#ifndef tb_bits_swap_u32 +# define tb_bits_swap_u32(x) tb_bits_swap_u32_asm(x) +#endif + +// FIXME: for ios +//#define tb_bits_get_ubits32_impl(p, b, n) tb_bits_get_ubits32_impl_asm(p, b, n) + +#endif /* TB_ASSEMBLER_IS_GAS */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#if defined(TB_ASSEMBLER_IS_GAS) && !defined(TB_ARCH_ARM64) + +// swap +#if (TB_ARCH_ARM_VERSION >= 6) +static __tb_inline__ tb_uint16_t const tb_bits_swap_u16_asm(tb_uint16_t x) +{ + __tb_asm__ __tb_volatile__("rev16 %0, %0" : "+r"(x)); + return x; +} +#endif + +static __tb_inline__ tb_uint32_t const tb_bits_swap_u32_asm(tb_uint32_t x) +{ +#if (TB_ARCH_ARM_VERSION >= 6) + __tb_asm__("rev %0, %0" : "+r"(x)); +#else + __tb_register__ tb_uint32_t t; + __tb_asm__ __tb_volatile__ + ( + "eor %1, %0, %0, ror #16 \n" + "bic %1, %1, #0xff0000 \n" + "mov %0, %0, ror #8 \n" + "eor %0, %0, %1, lsr #8 \n" + : "+r"(x), "=&r"(t) + ); +#endif + return x; +} + +#if 0 +static __tb_inline__ tb_uint32_t tb_bits_get_ubits32_impl_asm(tb_byte_t const* p, tb_size_t b, tb_size_t n) +{ +#ifdef __tb_small__ + __tb_register__ tb_uint32_t x; + __tb_asm__ __tb_volatile__ + ( + "ldrb r6, [%1], #1\n" + "ldrb r7, [%1], #1\n" + "ldrb r8, [%1], #1\n" + "ldrb r9, [%1], #1\n" + "ldrb %1, [%1]\n" + "orr %0, r6, lsl %2\n" + "sub %2, %2, #8\n" + "cmp %3, #8\n" + "orrhi %0, r7, lsl %2\n" + "sub %2, %2, #8\n" + "cmp %3, #16\n" + "orrhi %0, r8, lsl %2\n" + "sub %2, %2, #8\n" + "cmp %3, #24\n" + "orrhi %0, r9, lsl %2\n" + "rsb %2, %2, #8\n" + "cmp %3, #32\n" + "orrhi %0, %1, lsr %2\n" + "rsb %4, %4, #32\n" + "mov %0, %0, lsr %4\n" + + + : "=&r"(x) + : "r"(p), "r"(b + 24), "r"(b + n), "r"(n), "0"(0) + : "r6", "r7", "r8", "r9" + ); + + return x; +#else + __tb_register__ tb_uint32_t x; + __tb_asm__ __tb_volatile__ + ( + " cmp %3, #32\n" + " bhi 1f\n" + " cmp %3, #24\n" + " bhi 2f\n" + " cmp %3, #16\n" + " bhi 3f\n" + " cmp %3, #8\n" + " bhi 4f\n" + " ldrb %1, [%1]\n" + " orr %0, %1, lsl %2\n" + " b 5f\n" + "1:\n" + " ldrb r6, [%1], #1\n" + " ldrb r7, [%1], #1\n" + " ldrb r8, [%1], #1\n" + " ldrb r9, [%1], #1\n" + " ldrb %1, [%1]\n" + " orr %0, r6, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r7, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r8, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r9, lsl %2\n" + " rsb %2, %2, #8\n" + " orr %0, %1, lsr %2\n" + " b 5f\n" + "2:\n" + " ldrb r6, [%1], #1\n" + " ldrb r7, [%1], #1\n" + " ldrb r8, [%1], #1\n" + " ldrb r9, [%1], #1\n" + " orr %0, r6, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r7, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r8, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r9, lsl %2\n" + " b 5f\n" + "3:\n" + " ldrb r6, [%1], #1\n" + " ldrb r7, [%1], #1\n" + " ldrb r8, [%1], #1\n" + " orr %0, r6, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r7, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r8, lsl %2\n" + " b 5f\n" + "4:\n" + " ldrb r6, [%1], #1\n" + " ldrb r7, [%1], #1\n" + " orr %0, r6, lsl %2\n" + " sub %2, %2, #8\n" + " orr %0, r7, lsl %2\n" + "5:\n" + " rsb %4, %4, #32\n" + " mov %0, %0, lsr %4\n" + + : "=&r"(x) + : "r"(p), "r"(b + 24), "r"(b + n), "r"(n), "0"(0) + : "r6", "r7", "r8", "r9" + ); + + return x; +#endif +} +#endif + +#endif /* TB_ASSEMBLER_IS_GAS */ + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_gcc.h b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_gcc.h index b78b955d5..c0f81fd87 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_gcc.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_gcc.h @@ -62,3 +62,67 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bits_gcc.h + * + */ +#ifndef TB_UTILS_IMPL_BITS_GCC_H +#define TB_UTILS_IMPL_BITS_GCC_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +// swap +#if TB_COMPILER_VERSION_BE(4, 3) +# define tb_bits_swap_u32(x) __builtin_bswap32(x) +# define tb_bits_swap_u64(x) __builtin_bswap64(x) +#endif + +// cl0 +#if TB_COMPILER_VERSION_BE(4, 1) +# define tb_bits_cl0_u32_be(x) ((x)? (tb_size_t)__builtin_clz((tb_uint32_t)(x)) : 32) +# define tb_bits_cl0_u32_le(x) ((x)? (tb_size_t)__builtin_ctz((tb_uint32_t)(x)) : 32) +# define tb_bits_cl0_u64_be(x) ((x)? (tb_size_t)__builtin_clzll((tb_uint64_t)(x)) : 64) +# define tb_bits_cl0_u64_le(x) ((x)? (tb_size_t)__builtin_ctzll((tb_uint64_t)(x)) : 64) +#endif + +// cb1 +#if TB_COMPILER_VERSION_BE(4, 1) +# define tb_bits_cb1_u32(x) ((x)? (tb_size_t)__builtin_popcount((tb_uint32_t)(x)) : 0) +# define tb_bits_cb1_u64(x) ((x)? (tb_size_t)__builtin_popcountll((tb_uint64_t)(x)) : 0) +#endif + +// fb1 +#if TB_COMPILER_VERSION_BE(4, 1) +# define tb_bits_fb1_u32_le(x) ((x)? (tb_size_t)__builtin_ffs((tb_uint32_t)(x)) - 1 : 32) +# define tb_bits_fb1_u64_le(x) ((x)? (tb_size_t)__builtin_ffsll((tb_uint64_t)(x)) - 1 : 64) +#endif + +#endif + + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_sh4.h b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_sh4.h index 3aaeff504..b2461ed79 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_sh4.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_sh4.h @@ -63,3 +63,68 @@ static __tb_inline__ tb_uint32_t const tb_bits_swap_u32_asm(tb_uint32_t x) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bits_sh4.h + * + */ +#ifndef TB_UTILS_IMPL_BITS_SH4_H +#define TB_UTILS_IMPL_BITS_SH4_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +// swap +#ifndef tb_bits_swap_u16 +# define tb_bits_swap_u16(x) tb_bits_swap_u16_asm(x) +#endif +#ifndef tb_bits_swap_u32 +# define tb_bits_swap_u32(x) tb_bits_swap_u32_asm(x) +#endif +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// swap +static __tb_inline__ tb_uint16_t const tb_bits_swap_u16_asm(tb_uint16_t x) +{ + __tb_asm__("swap.b %0,%0" : "+r"(x)); + return x; +} + +static __tb_inline__ tb_uint32_t const tb_bits_swap_u32_asm(tb_uint32_t x) +{ + __tb_asm__( "swap.b %0,%0\n" + "swap.w %0,%0\n" + "swap.b %0,%0\n" + : "+r"(x)); + return x; +} + + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_x86.h b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_x86.h index 353e44876..a83accbf3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_x86.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/impl/bits_x86.h @@ -103,3 +103,108 @@ static __tb_inline__ tb_hize_t tb_bits_swap_u64_asm(tb_hize_t x) #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file bits_x86.h + * + */ +#ifndef TB_UTILS_IMPL_BITS_x86_H +#define TB_UTILS_IMPL_BITS_x86_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#ifdef TB_ASSEMBLER_IS_GAS + +// swap +#ifndef tb_bits_swap_u16 +# define tb_bits_swap_u16(x) tb_bits_swap_u16_asm(x) +#endif +#ifndef tb_bits_swap_u32 +# define tb_bits_swap_u32(x) tb_bits_swap_u32_asm(x) +#endif +#ifndef tb_bits_swap_u64 +# define tb_bits_swap_u64(x) tb_bits_swap_u64_asm(x) +#endif + +#endif /* TB_ASSEMBLER_IS_GAS */ +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +#ifdef TB_ASSEMBLER_IS_GAS + +// swap +static __tb_inline__ tb_uint16_t tb_bits_swap_u16_asm(tb_uint16_t x) +{ + __tb_asm__ __tb_volatile__("rorw $8, %w0" : "+r"(x)); + return x; +} + +static __tb_inline__ tb_uint32_t tb_bits_swap_u32_asm(tb_uint32_t x) +{ +#if 1 + __tb_asm__ __tb_volatile__("bswap %0" : "+r" (x)); +#else + __tb_asm__ __tb_volatile__ + ( + "rorw $8, %w0 \n" //!< swap low 16 bits + "rorl $16, %0 \n" //!< swap x by word + "rorw $8, %w0" //!< swap low 16 bits + + : "+r"(x) + ); +#endif + return x; +} + +static __tb_inline__ tb_hize_t tb_bits_swap_u64_asm(tb_hize_t x) +{ + __tb_register__ tb_size_t esi, edi; + __tb_asm__ __tb_volatile__ + ( + "lodsl\n" + "bswap %%eax\n" + "movl %%eax, %%ebx\n" + "lodsl\n" + "bswap %%eax\n" + "stosl\n" + "movl %%ebx, %%eax\n" + "stosl\n" + + : "=&S" (esi), "=&D" (edi) + : "0"(&x), "1"(&x) + : "memory", "eax", "ebx" + ); + return x; +} + +#endif /* TB_ASSEMBLER_IS_GAS */ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/impl/impl.h b/core/pkg/tbox.pkg/inc/tbox/utils/impl/impl.h index 4c884bfee..c82c10fe0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/impl/impl.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/impl/impl.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file impl.h + * + */ +#ifndef TB_UTILS_IMPL_H +#define TB_UTILS_IMPL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "singleton.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/impl/prefix.h b/core/pkg/tbox.pkg/inc/tbox/utils/impl/prefix.h index 5130498bc..f31f40589 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/impl/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/impl/prefix.h @@ -33,3 +33,38 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_UTILS_IMPL_PREFIX_H +#define TB_UTILS_IMPL_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/lock_profiler.h b/core/pkg/tbox.pkg/inc/tbox/utils/lock_profiler.h index 7023100ac..09be1e9c0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/lock_profiler.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/lock_profiler.h @@ -98,3 +98,103 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file lock_profiler.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_LOCK_PROFILER_H +#define TB_UTILS_LOCK_PROFILER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// enable lock profiler +#undef TB_LOCK_PROFILER_ENABLE +#if defined(__tb_debug__) && !defined(TB_CONFIG_MICRO_ENABLE) +# define TB_LOCK_PROFILER_ENABLE +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! the lock profiler instance + * + * @return the lock profiler handle + */ +tb_handle_t tb_lock_profiler(tb_noarg_t); + +/*! init lock profiler + * + * @note be used for the debug mode generally + * + * @return the lock profiler handle + */ +tb_handle_t tb_lock_profiler_init(tb_noarg_t); + +/*! exit lock profiler + * + * @param profiler the lock profiler handle + */ +tb_void_t tb_lock_profiler_exit(tb_handle_t profiler); + +/*! dump lock profiler + * + * @param profiler the lock profiler handle + */ +tb_void_t tb_lock_profiler_dump(tb_handle_t profiler); + +/*! register the lock to the lock profiler + * + * @param profiler the lock profiler handle + * @param lock the lock address + * @param name the lock name + */ +tb_void_t tb_lock_profiler_register(tb_handle_t profiler, tb_pointer_t lock, tb_char_t const* name); + +/*! the lock be occupied + * + * @param profiler the lock profiler handle + * @param lock the lock address + */ +tb_void_t tb_lock_profiler_occupied(tb_handle_t profiler, tb_pointer_t lock); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/option.h b/core/pkg/tbox.pkg/inc/tbox/utils/option.h index 0981d37cc..93c5ca3c9 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/option.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/option.h @@ -255,3 +255,260 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file option.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_OPTION_H +#define TB_UTILS_OPTION_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the option mode enum +typedef enum __tb_option_mode_e +{ + TB_OPTION_MODE_END = 0 //!< end +, TB_OPTION_MODE_VAL = 1 //!< value +, TB_OPTION_MODE_KEY = 2 //!< --key or -k +, TB_OPTION_MODE_KEY_VAL = 3 //!< --key=value or --key value or -k=value or -k value +, TB_OPTION_MODE_MORE = 4 //!< more and end + +}tb_option_mode_e; + +/// the option type enum +typedef enum __tb_option_type_e +{ + TB_OPTION_TYPE_NONE = 0 +, TB_OPTION_TYPE_BOOL = 1 +, TB_OPTION_TYPE_CSTR = 2 +, TB_OPTION_TYPE_FLOAT = 3 +, TB_OPTION_TYPE_INTEGER = 4 + +}tb_option_type_e; + +/// the option item type +typedef struct __tb_option_item_t +{ + /// the short name + tb_char_t sname; + + /// the long name + tb_char_t const* lname; + + /// the mode + tb_uint16_t mode; + + /// the type + tb_uint16_t type; + + /// the help + tb_char_t const* help; + +}tb_option_item_t; + +/// the option ref type +typedef __tb_typeref__(option); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init option + * + * @param name the command name + * @param help the command help + * @param opts the option list + * + * @return the option + */ +tb_option_ref_t tb_option_init(tb_char_t const* name, tb_char_t const* help, tb_option_item_t const* opts); + +/*! exit option + * + * @param option the option + */ +tb_void_t tb_option_exit(tb_option_ref_t option); + +/*! find the option item + * + * @param option the option + * @param name the option name, long name or short name + * + * @return tb_true or tb_false + */ +tb_bool_t tb_option_find(tb_option_ref_t option, tb_char_t const* name); + +/*! done option + * + * @param option the option + * @param argc the arguments count + * @param argv the arguments value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_option_done(tb_option_ref_t option, tb_size_t argc, tb_char_t** argv); + +/*! dump option + * + * @param option the option + */ +tb_void_t tb_option_dump(tb_option_ref_t option); + +/*! help option + * + * @param option the option + */ +tb_void_t tb_option_help(tb_option_ref_t option); + +/*! the option item - cstr + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the c-string pointer + */ +tb_char_t const* tb_option_item_cstr(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - bool + * + * @param option the option + * @param name the option name, long name or short name + * + * @return tb_true or tb_false + */ +tb_bool_t tb_option_item_bool(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - uint8 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_uint8_t tb_option_item_uint8(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - sint8 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_sint8_t tb_option_item_sint8(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - uint16 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_uint16_t tb_option_item_uint16(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - sint16 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_sint16_t tb_option_item_sint16(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - uint32 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_uint32_t tb_option_item_uint32(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - sint32 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_sint32_t tb_option_item_sint32(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - uint64 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_uint64_t tb_option_item_uint64(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - sint64 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_sint64_t tb_option_item_sint64(tb_option_ref_t option, tb_char_t const* name); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT + +/*! the option item - float + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_float_t tb_option_item_float(tb_option_ref_t option, tb_char_t const* name); + +/*! the option item - sint64 + * + * @param option the option + * @param name the option name, long name or short name + * + * @return the integer value + */ +tb_double_t tb_option_item_double(tb_option_ref_t option, tb_char_t const* name); + +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/prefix.h b/core/pkg/tbox.pkg/inc/tbox/utils/prefix.h index 12e600f2d..9b24bcd00 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/prefix.h @@ -32,3 +32,37 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_UTILS_PREFIX_H +#define TB_UTILS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/singleton.h b/core/pkg/tbox.pkg/inc/tbox/utils/singleton.h index 32f8c601b..dffb2c038 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/singleton.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/singleton.h @@ -189,3 +189,194 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file singleton.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_SINGLETON_H +#define TB_UTILS_SINGLETON_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the singleton type enum +typedef enum __tb_singleton_type_e +{ + /// the default allocator + TB_SINGLETON_TYPE_DEFAULT_ALLOCATOR = 0 + + /// the static allocator +, TB_SINGLETON_TYPE_STATIC_ALLOCATOR = 1 + +#ifdef TB_CONFIG_MICRO_ENABLE + + /// the user defined type +, TB_SINGLETON_TYPE_USER = 2 + +#else + + /// the lock profiler type +, TB_SINGLETON_TYPE_LOCK_PROFILER = 2 + + /// the ifaddrs type +, TB_SINGLETON_TYPE_IFADDRS = 3 + + /// the timer type +, TB_SINGLETON_TYPE_TIMER = 4 + + /// the ltimer type +, TB_SINGLETON_TYPE_LTIMER = 5 + + /// the aicp type, TODO deprecated +, TB_SINGLETON_TYPE_AICP = 6 + + /// the openssl library type +, TB_SINGLETON_TYPE_LIBRARY_OPENSSL = 7 + + /// the mysql library type +, TB_SINGLETON_TYPE_LIBRARY_MYSQL = 8 + + /// the sqlite3 library type +, TB_SINGLETON_TYPE_LIBRARY_SQLITE3 = 9 + + /// the thread pool type +, TB_SINGLETON_TYPE_THREAD_POOL = 10 + + /// the transfer pool type +, TB_SINGLETON_TYPE_TRANSFER_POOL = 11 + + /// the cookies type +, TB_SINGLETON_TYPE_COOKIES = 12 + + /// the user defined type +, TB_SINGLETON_TYPE_USER = 13 + +#endif + + /// the max count of the singleton type +#if defined(TB_CONFIG_MICRO_ENABLE) +, TB_SINGLETON_TYPE_MAXN = TB_SINGLETON_TYPE_USER + 2 +#elif defined(__tb_small__) +, TB_SINGLETON_TYPE_MAXN = TB_SINGLETON_TYPE_USER + 8 +#else +, TB_SINGLETON_TYPE_MAXN = TB_SINGLETON_TYPE_USER + 64 +#endif + +}tb_singleton_type_e; + +/// the singleton init func type +typedef tb_handle_t (*tb_singleton_init_func_t)(tb_cpointer_t* ppriv); + +/// the singleton exit func type +typedef tb_void_t (*tb_singleton_exit_func_t)(tb_handle_t instance, tb_cpointer_t priv); + +/// the singleton kill func type +typedef tb_void_t (*tb_singleton_kill_func_t)(tb_handle_t instance, tb_cpointer_t priv); + +/// the singleton static init func type +typedef tb_bool_t (*tb_singleton_static_init_func_t)(tb_handle_t instance, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init singleton + * + * @return tb_true or tb_false + */ +tb_bool_t tb_singleton_init(tb_noarg_t); + +/*! kill singleton + */ +tb_void_t tb_singleton_kill(tb_noarg_t); + +/*! exit singleton + */ +tb_void_t tb_singleton_exit(tb_noarg_t); + +/*! the singleton instance + * + * @param type the singleton type + * @param init the singleton init func + * @param exit the singleton exit func + * @param kill the singleton kill func + * @param priv the private data + * + * @return the singleton instance handle + */ +tb_handle_t tb_singleton_instance(tb_size_t type, tb_singleton_init_func_t init, tb_singleton_exit_func_t exit, tb_singleton_kill_func_t kill, tb_cpointer_t priv); + +/*! the singleton static instance + * + * @code + static tb_bool_t tb_xxxx_instance_init(tb_handle_t instance, tb_cpointer_t priv) + { + // init + // ... + + // ok + return tb_true; + } + tb_xxxx_t* tb_xxxx() + { + // init + static tb_atomic_t s_binited = 0; + static tb_xxxx_t s_xxxx = {0}; + + // init the static instance + tb_bool_t ok = tb_singleton_static_init(&s_binited, &s_xxxx, tb_xxxx_instance_init, tb_null); + tb_assert(ok); + + // ok + return &s_xxxx; + } + * @endcode + * + * @param binited the singleton static instance is inited? + * @param instance the singleton static instance + * @param init the singleton static init func + * @param priv the private data + * + * @return tb_true or tb_false + */ +tb_bool_t tb_singleton_static_init(tb_atomic_t* binited, tb_handle_t instance, tb_singleton_static_init_func_t init, tb_cpointer_t priv); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/trace.h b/core/pkg/tbox.pkg/inc/tbox/utils/trace.h index 437682722..f60c2101d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/trace.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/trace.h @@ -135,3 +135,140 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file trace.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_TRACE_H +#define TB_UTILS_TRACE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../libc/libc.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the trace mode enum +typedef enum __tb_trace_mode_e +{ + TB_TRACE_MODE_NONE = 0 +, TB_TRACE_MODE_FILE = 1 +, TB_TRACE_MODE_PRINT = 2 + +}tb_trace_mode_e; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init trace + * + * @return tb_true or tb_false + */ +tb_bool_t tb_trace_init(tb_noarg_t); + +/*! exit trace + */ +tb_void_t tb_trace_exit(tb_noarg_t); + +/*! the trace mode + * + * @return the trace mode + */ +tb_size_t tb_trace_mode(tb_noarg_t); + +/*! set the trace mode + * + * @param mode the trace mode + * + * @return tb_true or tb_false + */ +tb_bool_t tb_trace_mode_set(tb_size_t mode); + +/*! the trace file + * + * @return the trace file handle + */ +tb_handle_t tb_trace_file(tb_noarg_t); + +/*! set the trace file + * + * @param file the trace file handle + * + * @return tb_true or tb_false + */ +tb_bool_t tb_trace_file_set(tb_file_ref_t file); + +/*! set the trace file path + * + * @param path the trace file path + * @param bappend is appended? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_trace_file_set_path(tb_char_t const* path, tb_bool_t bappend); + +/*! done trace with arguments + * + * @param prefix the trace prefix + * @param module the trace module + * @param format the trace format + * @param args the trace arguments + */ +tb_void_t tb_trace_done_with_args(tb_char_t const* prefix, tb_char_t const* module, tb_char_t const* format, tb_va_list_t args); + +/*! done trace + * + * @param prefix the trace prefix + * @param module the trace module + * @param format the trace format + */ +tb_void_t tb_trace_done(tb_char_t const* prefix, tb_char_t const* module, tb_char_t const* format, ...); + +/*! done trace tail + * + * @param format the trace format + */ +tb_void_t tb_trace_tail(tb_char_t const* format, ...); + +/*! sync trace + */ +tb_void_t tb_trace_sync(tb_noarg_t); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/url.h b/core/pkg/tbox.pkg/inc/tbox/utils/url.h index 6226f8553..cd18ffd9d 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/url.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/url.h @@ -113,3 +113,118 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file url.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_URL_H +#define TB_UTILS_URL_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! encode the url, not encode: -_. and ' ' => '+' + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_url_encode(tb_char_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! decode the url + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_url_decode(tb_char_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! encode the url, not encode: -_.!~*'();/?:@&=+$,# + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_url_encode2(tb_char_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! decode the url + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_url_decode2(tb_char_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! encode the url arguments, not encode: -_.!~*'() + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_url_encode_args(tb_char_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/*! decode the url arguments + * + * @param ib the input data + * @param in the input size + * @param ob the output data + * @param on the output size + * + * @return the real size + */ +tb_size_t tb_url_decode_args(tb_char_t const* ib, tb_size_t in, tb_char_t* ob, tb_size_t on); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/used.h b/core/pkg/tbox.pkg/inc/tbox/utils/used.h index 1e18456ff..965b529e2 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/used.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/used.h @@ -58,3 +58,63 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file used.h + * @ingroup utils + * + */ +#ifndef TB_UTILS_USED_H +#define TB_UTILS_USED_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macors + */ +#define tb_used(ptr) tb_used_ptr((tb_cpointer_t)(tb_size_t)(ptr)) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! this variable have been used + * + * @param variable the variable + */ +tb_void_t tb_used_ptr(tb_cpointer_t variable); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/utils/utils.h b/core/pkg/tbox.pkg/inc/tbox/utils/utils.h index 0f44ba622..689892c74 100644 --- a/core/pkg/tbox.pkg/inc/tbox/utils/utils.h +++ b/core/pkg/tbox.pkg/inc/tbox/utils/utils.h @@ -42,3 +42,47 @@ #include "lock_profiler.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file utils.h + * @defgroup utils + * + */ +#ifndef TB_UTILS_H +#define TB_UTILS_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "bits.h" +#include "url.h" +#include "dump.h" +#include "used.h" +#include "trace.h" +#include "base32.h" +#include "base64.h" +#include "option.h" +#include "singleton.h" +#include "lock_profiler.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/xml/node.h b/core/pkg/tbox.pkg/inc/tbox/xml/node.h index 26c5aeedf..72a1e4233 100644 --- a/core/pkg/tbox.pkg/inc/tbox/xml/node.h +++ b/core/pkg/tbox.pkg/inc/tbox/xml/node.h @@ -331,3 +331,336 @@ tb_void_t tb_xml_node_append_atail(tb_xml_node_ref_t node, tb_xml_node __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file node.h + * @ingroup xml + * + */ +#ifndef TB_XML_NODE_H +#define TB_XML_NODE_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/*! the xml node type + * + * @note see http://www.w3.org/TR/REC-DOM-Level-1/ + * + */ +typedef enum __tb_xml_node_type_t +{ + TB_XML_NODE_TYPE_NONE = 0 +, TB_XML_NODE_TYPE_ELEMENT = 1 +, TB_XML_NODE_TYPE_ATTRIBUTE = 2 +, TB_XML_NODE_TYPE_TEXT = 3 +, TB_XML_NODE_TYPE_CDATA = 4 +, TB_XML_NODE_TYPE_ENTITY_REFERENCE = 5 +, TB_XML_NODE_TYPE_ENTITY = 6 +, TB_XML_NODE_TYPE_PROCESSING_INSTRUCTION = 7 +, TB_XML_NODE_TYPE_COMMENT = 8 +, TB_XML_NODE_TYPE_DOCUMENT = 9 +, TB_XML_NODE_TYPE_DOCUMENT_TYPE = 10 +, TB_XML_NODE_TYPE_DOCUMENT_FRAGMENT = 11 +, TB_XML_NODE_TYPE_NOTATION = 12 + +}tb_xml_node_type_t; + +/// the xml node +typedef struct __tb_xml_node_t +{ + /// the node type + tb_size_t type; + + /// the node name + tb_string_t name; + + /// the node data + tb_string_t data; + + /// the next + struct __tb_xml_node_t* next; + + /// the childs head + struct __tb_xml_node_t* chead; + + /// the childs tail + struct __tb_xml_node_t* ctail; + + /// the childs size + tb_size_t csize; + + /// the attributes head + struct __tb_xml_node_t* ahead; + + /// the attributes tail + struct __tb_xml_node_t* atail; + + /// the attributes size + tb_size_t asize; + + /// the parent + struct __tb_xml_node_t* parent; + +}tb_xml_node_t; + +/// the xml element type +typedef struct __tb_xml_element_t +{ + /// the node base + tb_xml_node_t base; + +}tb_xml_element_t; + +/// the xml text type +typedef struct __tb_xml_text_t +{ + /// the node base + tb_xml_node_t base; + +}tb_xml_text_t; + +/// the xml cdata type +typedef struct __tb_xml_cdata_t +{ + /// the node base + tb_xml_node_t base; + +}tb_xml_cdata_t; + +/// the xml comment type +typedef struct __tb_xml_comment_t +{ + /// the node base + tb_xml_node_t base; + +}tb_xml_comment_t; + +/*! the xml attribute type + * + * <pre> + * inherit node, + * but since they are not actually child nodes of the element they describe, + * the DOM does not consider them part of the document tree. + * </pre> + */ +typedef struct __tb_xml_attribute_t +{ + /// the node base + tb_xml_node_t base; + +}tb_xml_attribute_t; + +/// the xml document type +typedef struct __tb_xml_document_t +{ + /// the node base + tb_xml_node_t base; + + /// the version + tb_string_t version; + + /// the charset + tb_string_t charset; + +}tb_xml_document_t; + +/// the xml document type type +typedef struct __tb_xml_document_type_t +{ + /// the node base + tb_xml_node_t base; + + /// the type + tb_string_t type; + +}tb_xml_document_type_t; + +/// the xml node ref type +typedef tb_xml_node_t* tb_xml_node_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init element node + * + * @param name the element name + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_element(tb_char_t const* name); + +/*! init text node + * + * @param data the element text + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_text(tb_char_t const* data); + +/*! init cdata node + * + * @param cdata the element cdata + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_cdata(tb_char_t const* cdata); + +/*! init comment node + * + * @param comment the element comment + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_comment(tb_char_t const* comment); + +/*! init attribute node + * + * @param name the attribute name + * @param data the attribute data + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_attribute(tb_char_t const* name, tb_char_t const* data); + +/*! init document node + * + * @param version the xml version + * @param encoding the xml encoding + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_document(tb_char_t const* version, tb_char_t const* encoding); + +/*! init document type node + * + * @param type the document type + * @return the element node + */ +tb_xml_node_ref_t tb_xml_node_init_document_type(tb_char_t const* type); + +/*! exit the xml node + * + * @param node the element node + */ +tb_void_t tb_xml_node_exit(tb_xml_node_ref_t node); + +/*! goto node by the gived path + * + * @param node the root node + * @return the goto node + */ +tb_xml_node_ref_t tb_xml_node_goto(tb_xml_node_ref_t node, tb_char_t const* path); + +/*! the xml childs head node + * + * @param node the xml node + * @return the xml childs head node + */ +tb_xml_node_ref_t tb_xml_node_chead(tb_xml_node_ref_t node); + +/*! the xml childs count + * + * @param node the xml node + * @return the xml childs count + */ +tb_size_t tb_xml_node_csize(tb_xml_node_ref_t node); + +/*! the xml attributes head node + * + * @param node the xml node + * @return the xml attributes head node + */ +tb_xml_node_ref_t tb_xml_node_ahead(tb_xml_node_ref_t node); + +/*! the xml attributes count + * + * @param node the xml node + * @return the xml attributes count + */ +tb_size_t tb_xml_node_asize(tb_xml_node_ref_t node); + +/*! insert to the next node + * + * @param node the xml node + * @param next the xml next node + */ +tb_void_t tb_xml_node_insert_next(tb_xml_node_ref_t node, tb_xml_node_ref_t next); + +/*! remove the next node + * + * @param node the xml node + */ +tb_void_t tb_xml_node_remove_next(tb_xml_node_ref_t node); + +/*! append the node to the childs head + * + * @param node the xml node + * @param child the xml child node + */ +tb_void_t tb_xml_node_append_chead(tb_xml_node_ref_t node, tb_xml_node_ref_t child); + +/*! append the node to the childs tail + * + * @param node the xml node + * @param child the xml child node + */ +tb_void_t tb_xml_node_append_ctail(tb_xml_node_ref_t node, tb_xml_node_ref_t child); + +/*! remove the node from the childs head + * + * @param node the xml node + */ +tb_void_t tb_xml_node_remove_chead(tb_xml_node_ref_t node); + +/*! remove the node from the childs tail + * + * @param node the xml node + */ +tb_void_t tb_xml_node_remove_ctail(tb_xml_node_ref_t node); + +/*! append the node to the attributes head + * + * @param node the xml node + * @param attribute the xml attribute node + */ +tb_void_t tb_xml_node_append_ahead(tb_xml_node_ref_t node, tb_xml_node_ref_t attribute); + +/*! append the node to the attributes tail + * + * @param node the xml node + * @param attribute the xml attribute node + */ +tb_void_t tb_xml_node_append_atail(tb_xml_node_ref_t node, tb_xml_node_ref_t attribute); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/xml/prefix.h b/core/pkg/tbox.pkg/inc/tbox/xml/prefix.h index 471c79aaf..66650bc0e 100644 --- a/core/pkg/tbox.pkg/inc/tbox/xml/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/xml/prefix.h @@ -39,3 +39,44 @@ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_XML_PREFIX_H +#define TB_XML_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../libc/libc.h" +#include "../utils/utils.h" +#include "../stream/stream.h" +#include "../string/string.h" +#include "../memory/memory.h" +#include "../platform/platform.h" +#include "../container/container.h" + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/xml/reader.h b/core/pkg/tbox.pkg/inc/tbox/xml/reader.h index 29a2a7068..3fe4011f7 100644 --- a/core/pkg/tbox.pkg/inc/tbox/xml/reader.h +++ b/core/pkg/tbox.pkg/inc/tbox/xml/reader.h @@ -296,3 +296,301 @@ tb_xml_node_ref_t tb_xml_reader_attributes(tb_xml_reader_ref_t reader); __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file reader.h + * @ingroup xml + * + */ +#ifndef TB_XML_READER_H +#define TB_XML_READER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "node.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the xml reader event type for iterator +typedef enum __tb_xml_reader_event_t +{ + TB_XML_READER_EVENT_NONE = 0 +, TB_XML_READER_EVENT_DOCUMENT_TYPE = 1 +, TB_XML_READER_EVENT_DOCUMENT = 2 +, TB_XML_READER_EVENT_ELEMENT_BEG = 3 +, TB_XML_READER_EVENT_ELEMENT_END = 4 +, TB_XML_READER_EVENT_ELEMENT_EMPTY = 5 +, TB_XML_READER_EVENT_COMMENT = 6 +, TB_XML_READER_EVENT_TEXT = 7 +, TB_XML_READER_EVENT_CDATA = 8 + +}tb_xml_reader_event_t; + +/// the xml reader ref type +typedef __tb_typeref__(xml_reader); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the xml reader + * + * @return the reader + */ +tb_xml_reader_ref_t tb_xml_reader_init(tb_noarg_t); + +/*! exit the xml reader + * + * @param reader the xml reader + */ +tb_void_t tb_xml_reader_exit(tb_xml_reader_ref_t reader); + +/*! open the xml reader + * + * @param reader the xml reader + * @param stream the stream, will open it if be not opened + * @param bowner the xml reader is owner of the stream? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_xml_reader_open(tb_xml_reader_ref_t reader, tb_stream_ref_t stream, tb_bool_t bowner); + +/*! clos the xml reader + * + * @param reader the xml reader + */ +tb_void_t tb_xml_reader_clos(tb_xml_reader_ref_t reader); + +/*! the next iterator for the xml reader + * + * @param reader the xml reader + * @return the iterator event + * + * @code + * + // init reader + tb_xml_reader_ref_t reader = tb_xml_reader_init(); + if (reader) + { + // open reader + if (tb_xml_reader_open(reader, tb_stream_init_from_url(argv[1]), tb_true)) + { + // goto + tb_bool_t ok = tb_true; + if (argv[2]) ok = tb_xml_reader_goto(reader, argv[2]); + + // walk + tb_size_t event = TB_XML_READER_EVENT_NONE; + while (ok && (event = tb_xml_reader_next(reader))) + { + switch (event) + { + case TB_XML_READER_EVENT_DOCUMENT: + { + tb_printf("<?xml version = \"%s\" encoding = \"%s\" ?>\n" + , tb_xml_reader_version(reader), tb_xml_reader_charset(reader)); + } + break; + case TB_XML_READER_EVENT_DOCUMENT_TYPE: + { + tb_printf("<!DOCTYPE>\n"); + } + break; + case TB_XML_READER_EVENT_ELEMENT_EMPTY: + { + tb_char_t const* name = tb_xml_reader_element(reader); + tb_xml_node_ref_t attr = tb_xml_reader_attributes(reader); + tb_size_t t = tb_xml_reader_level(reader); + while (t--) tb_printf("\t"); + if (!attr) tb_printf("<%s/>\n", name); + else + { + tb_printf("<%s", name); + for (; attr; attr = attr->next) + tb_printf(" %s = \"%s\"", tb_string_cstr(&attr->name), tb_string_cstr(&attr->data)); + tb_printf("/>\n"); + } + } + break; + case TB_XML_READER_EVENT_ELEMENT_BEG: + { + tb_char_t const* name = tb_xml_reader_element(reader); + tb_xml_node_ref_t attr = tb_xml_reader_attributes(reader); + tb_size_t t = tb_xml_reader_level(reader) - 1; + while (t--) tb_printf("\t"); + if (!attr) tb_printf("<%s>\n", name); + else + { + tb_printf("<%s", name); + for (; attr; attr = attr->next) + tb_printf(" %s = \"%s\"", tb_string_cstr(&attr->name), tb_string_cstr(&attr->data)); + tb_printf(">\n"); + } + } + break; + case TB_XML_READER_EVENT_ELEMENT_END: + { + tb_size_t t = tb_xml_reader_level(reader); + while (t--) tb_printf("\t"); + tb_printf("</%s>\n", tb_xml_reader_element(reader)); + } + break; + case TB_XML_READER_EVENT_TEXT: + { + tb_size_t t = tb_xml_reader_level(reader); + while (t--) tb_printf("\t"); + tb_printf("%s", tb_xml_reader_text(reader)); + tb_printf("\n"); + } + break; + case TB_XML_READER_EVENT_CDATA: + { + tb_size_t t = tb_xml_reader_level(reader); + while (t--) tb_printf("\t"); + tb_printf("<![CDATA[%s]]>", tb_xml_reader_cdata(reader)); + tb_printf("\n"); + } + break; + case TB_XML_READER_EVENT_COMMENT: + { + tb_size_t t = tb_xml_reader_level(reader); + while (t--) tb_printf("\t"); + tb_printf("<!--%s-->", tb_xml_reader_comment(reader)); + tb_printf("\n"); + } + break; + default: + break; + } + } + } + + // exit reader + tb_xml_reader_exit(reader); + } + + * @endcode + */ +tb_size_t tb_xml_reader_next(tb_xml_reader_ref_t reader); + +/*! the xml stream + * + * @param reader the xml reader + * @return the xml stream + */ +tb_stream_ref_t tb_xml_reader_stream(tb_xml_reader_ref_t reader); + +/*! the xml level + * + * @param reader the xml reader + * @return the xml level for tab spaces + */ +tb_size_t tb_xml_reader_level(tb_xml_reader_ref_t reader); + +/*! seek to the given node for xml, .e.g /root/node/item + * + * @param reader the reader handle + * @param path the xml path + * @return tb_true or tb_false + * + * @note the stream will be reseted + */ +tb_bool_t tb_xml_reader_goto(tb_xml_reader_ref_t reader, tb_char_t const* path); + +/*! load the xml + * + * @param reader the xml reader + * @return the xml root node + */ +tb_xml_node_ref_t tb_xml_reader_load(tb_xml_reader_ref_t reader); + +/*! the xml version + * + * @param reader the xml reader + * @return the xml version + */ +tb_char_t const* tb_xml_reader_version(tb_xml_reader_ref_t reader); + +/*! the xml charset + * + * @param reader the xml reader + * @return the xml charset + */ +tb_char_t const* tb_xml_reader_charset(tb_xml_reader_ref_t reader); + +/*! the current xml element name + * + * @param reader the xml reader + * @return the current xml element name + */ +tb_char_t const* tb_xml_reader_element(tb_xml_reader_ref_t reader); + +/*! the current xml node text + * + * @param reader the xml reader + * @return the current xml node text + */ +tb_char_t const* tb_xml_reader_text(tb_xml_reader_ref_t reader); + +/*! the current xml node cdata + * + * @param reader the xml reader + * @return the current xml node cdata + */ +tb_char_t const* tb_xml_reader_cdata(tb_xml_reader_ref_t reader); + +/*! the current xml node comment + * + * @param reader the xml reader + * @return the current xml node comment + */ +tb_char_t const* tb_xml_reader_comment(tb_xml_reader_ref_t reader); + +/*! the xml document type + * + * @param reader the xml reader + * @return the xml document type + */ +tb_char_t const* tb_xml_reader_doctype(tb_xml_reader_ref_t reader); + +/*! the current xml node attributes + * + * @param reader the xml reader + * @return the current xml node attributes + */ +tb_xml_node_ref_t tb_xml_reader_attributes(tb_xml_reader_ref_t reader); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/xml/writer.h b/core/pkg/tbox.pkg/inc/tbox/xml/writer.h index 6a763db6b..4c3371a61 100644 --- a/core/pkg/tbox.pkg/inc/tbox/xml/writer.h +++ b/core/pkg/tbox.pkg/inc/tbox/xml/writer.h @@ -196,3 +196,201 @@ tb_void_t tb_xml_writer_attributes_double(tb_xml_writer_ref_t writ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file writer.h + * @ingroup xml + * + */ +#ifndef TB_XML_WRITER_H +#define TB_XML_WRITER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "node.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the xml writer ref type +typedef __tb_typeref__(xml_writer); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the xml writer + * + * @return the writer + */ +tb_xml_writer_ref_t tb_xml_writer_init(tb_noarg_t); + +/*! exit the xml writer + * + * @param writer the xml writer + */ +tb_void_t tb_xml_writer_exit(tb_xml_writer_ref_t writer); + +/*! open the xml writer + * + * @param writer the xml writer + * @param bformat is format xml? + * @param stream the stream, will open it if be not opened + * @param bowner the xml writer is owner of the stream? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_xml_writer_open(tb_xml_writer_ref_t writer, tb_bool_t bformat, tb_stream_ref_t stream, tb_bool_t bowner); + +/*! clos the xml writer + * + * @param writer the xml writer + */ +tb_void_t tb_xml_writer_clos(tb_xml_writer_ref_t writer); + +/*! save the xml document or node + * + * @param writer the xml writer + * @param node the xml node + */ +tb_void_t tb_xml_writer_save(tb_xml_writer_ref_t writer, tb_xml_node_ref_t node); + +/*! writ the xml document node: <?xml version = \"...\" encoding = \"...\" ?> + * + * @param writer the xml writer + * @param version the xml version + * @param encoding the xml encoding + */ +tb_void_t tb_xml_writer_document(tb_xml_writer_ref_t writer, tb_char_t const* version, tb_char_t const* encoding); + +/*! writ the xml document type: <!DOCTYPE type> + * + * @param writer the xml writer + * @param type the xml document type + */ +tb_void_t tb_xml_writer_document_type(tb_xml_writer_ref_t writer, tb_char_t const* type); + +/*! writ the xml cdata: <![CDATA[...]]> + * + * @param writer the xml writer + * @param data the xml cdata + */ +tb_void_t tb_xml_writer_cdata(tb_xml_writer_ref_t writer, tb_char_t const* data); + +/*! writ the xml text + * + * @param writer the xml writer + * @param text the xml text + */ +tb_void_t tb_xml_writer_text(tb_xml_writer_ref_t writer, tb_char_t const* text); + +/*! writ the xml comment: <!-- ... --> + * + * @param writer the xml writer + * @param comment the xml comment + */ +tb_void_t tb_xml_writer_comment(tb_xml_writer_ref_t writer, tb_char_t const* comment); + +/*! writ the empty xml element: <name/> + * + * @param writer the xml writer + * @param name the xml element name + */ +tb_void_t tb_xml_writer_element_empty(tb_xml_writer_ref_t writer, tb_char_t const* name); + +/*! writ the xml element head: <name> ... + * + * @param writer the xml writer + * @param name the xml element name + */ +tb_void_t tb_xml_writer_element_enter(tb_xml_writer_ref_t writer, tb_char_t const* name); + +/*! writ the xml element tail: ... </name> + * + * @param writer the xml writer + */ +tb_void_t tb_xml_writer_element_leave(tb_xml_writer_ref_t writer); + +/*! writ the xml attribute for long value + * + * @param writer the xml writer + * @param name the xml attribute name + * @param value the xml attribute value + */ +tb_void_t tb_xml_writer_attributes_long(tb_xml_writer_ref_t writer, tb_char_t const* name, tb_long_t value); + +/*! writ the xml attribute for boolean value + * + * @param writer the xml writer + * @param name the xml attribute name + * @param value the xml attribute value + */ +tb_void_t tb_xml_writer_attributes_bool(tb_xml_writer_ref_t writer, tb_char_t const* name, tb_bool_t value); + +/*! writ the xml attribute for cstr value + * + * @param writer the xml writer + * @param name the xml attribute name + * @param value the xml attribute value + */ +tb_void_t tb_xml_writer_attributes_cstr(tb_xml_writer_ref_t writer, tb_char_t const* name, tb_char_t const* value); + +/*! writ the xml attribute for format value + * + * @param writer the xml writer + * @param name the xml attribute name + * @param format the xml attribute format + */ +tb_void_t tb_xml_writer_attributes_format(tb_xml_writer_ref_t writer, tb_char_t const* name, tb_char_t const* format, ...); + +#ifdef TB_CONFIG_TYPE_HAVE_FLOAT +/*! writ the xml attribute for float value + * + * @param writer the xml writer + * @param name the xml attribute name + * @param value the xml attribute value + */ +tb_void_t tb_xml_writer_attributes_float(tb_xml_writer_ref_t writer, tb_char_t const* name, tb_float_t value); + +/*! writ the xml attribute for double value + * + * @param writer the xml writer + * @param name the xml attribute name + * @param value the xml attribute value + */ +tb_void_t tb_xml_writer_attributes_double(tb_xml_writer_ref_t writer, tb_char_t const* name, tb_double_t value); +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/xml/xml.h b/core/pkg/tbox.pkg/inc/tbox/xml/xml.h index 0fb66a0ed..2362ecf42 100644 --- a/core/pkg/tbox.pkg/inc/tbox/xml/xml.h +++ b/core/pkg/tbox.pkg/inc/tbox/xml/xml.h @@ -35,3 +35,40 @@ #include "writer.h" #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file xml.h + * @defgroup xml + * + */ +#ifndef TB_XML_H +#define TB_XML_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "node.h" +#include "reader.h" +#include "writer.h" + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/zip/gzip.h b/core/pkg/tbox.pkg/inc/tbox/zip/gzip.h index a8392b717..c3cd6b0e0 100644 --- a/core/pkg/tbox.pkg/inc/tbox/zip/gzip.h +++ b/core/pkg/tbox.pkg/inc/tbox/zip/gzip.h @@ -81,3 +81,86 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file gzip.h + * @ingroup zip + * + */ +#ifndef TB_ZIP_GZIP_H +#define TB_ZIP_GZIP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_PACKAGE_HAVE_ZLIB +# include "zlib/zlib.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the gzip zip type +typedef struct __tb_zip_gzip_t +{ + // the zip base + tb_zip_t base; + + // the zstream +#ifdef TB_CONFIG_PACKAGE_HAVE_ZLIB + z_stream zstream; +#endif + +}tb_zip_gzip_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* init gzip + * + * @param action the action + * + * @return the zip + */ +tb_zip_ref_t tb_zip_gzip_init(tb_size_t action); + +/* exit gzip + * + * @param zip the zip + */ +tb_void_t tb_zip_gzip_exit(tb_zip_ref_t zip); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/zip/prefix.h b/core/pkg/tbox.pkg/inc/tbox/zip/prefix.h index 447e6219c..b3ecc91c5 100644 --- a/core/pkg/tbox.pkg/inc/tbox/zip/prefix.h +++ b/core/pkg/tbox.pkg/inc/tbox/zip/prefix.h @@ -74,3 +74,79 @@ typedef tb_zip_t* tb_zip_ref_t; #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef TB_ZIP_PREFIX_H +#define TB_ZIP_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "../stream/static_stream.h" +#include "../memory/memory.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the zip action type +typedef enum __tb_zip_action_t +{ + TB_ZIP_ACTION_NONE = 0 +, TB_ZIP_ACTION_INFLATE = 1 +, TB_ZIP_ACTION_DEFLATE = 2 + +}tb_zip_action_t; + +// the zip algorithm type +typedef enum __tb_zip_algo_t +{ + TB_ZIP_ALGO_NONE = 0 //!< none +, TB_ZIP_ALGO_ZLIBRAW = 1 //!< zlib: raw inflate & deflate +, TB_ZIP_ALGO_ZLIB = 2 //!< zlib +, TB_ZIP_ALGO_GZIP = 3 //!< gnu zip + +}tb_zip_algo_t; + +// the zip type +typedef struct __tb_zip_t +{ + // the algorithm + tb_uint16_t algo; + + // the action + tb_uint16_t action; + + // spak + tb_long_t (*spak)(struct __tb_zip_t* zip, tb_static_stream_ref_t ist, tb_static_stream_ref_t ost, tb_long_t sync); + +}tb_zip_t; + +/// the zip ref type +typedef tb_zip_t* tb_zip_ref_t; + + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/zip/zip.h b/core/pkg/tbox.pkg/inc/tbox/zip/zip.h index 84e4ffbf1..28e19ac70 100644 --- a/core/pkg/tbox.pkg/inc/tbox/zip/zip.h +++ b/core/pkg/tbox.pkg/inc/tbox/zip/zip.h @@ -71,3 +71,76 @@ tb_long_t tb_zip_spak(tb_zip_ref_t zip, tb_static_stream_ref_t ist, tb __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file zip.h + * + */ +#ifndef TB_ZIP_H +#define TB_ZIP_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init zip + * + * @param algo the zip zlgo + * @param action the zip action + * + * @return the zip + */ +tb_zip_ref_t tb_zip_init(tb_size_t algo, tb_size_t action); + +/*! exit zip + * + * @param zip the zip + */ +tb_void_t tb_zip_exit(tb_zip_ref_t zip); + +/*! spak + * + * @param zip the zip + * @param ist the input stream + * @param ost the output stream + * @param sync sync? 1: sync, 0: no sync, -1: end + * + * @return 1: ok, 0: continue, -1: end + */ +tb_long_t tb_zip_spak(tb_zip_ref_t zip, tb_static_stream_ref_t ist, tb_static_stream_ref_t ost, tb_long_t sync); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/zip/zlib.h b/core/pkg/tbox.pkg/inc/tbox/zip/zlib.h index 8781e0096..5f77e80a3 100644 --- a/core/pkg/tbox.pkg/inc/tbox/zip/zlib.h +++ b/core/pkg/tbox.pkg/inc/tbox/zip/zlib.h @@ -81,3 +81,86 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file zlib.h + * @ingroup zip + * + */ +#ifndef TB_ZIP_ZLIB_H +#define TB_ZIP_ZLIB_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_PACKAGE_HAVE_ZLIB +# include "zlib/zlib.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the zlib zip type +typedef struct __tb_zip_zlib_t +{ + // the zip base + tb_zip_t base; + + // the zstream +#ifdef TB_CONFIG_PACKAGE_HAVE_ZLIB + z_stream zstream; +#endif + +}tb_zip_zlib_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* init zlib + * + * @param action the action + * + * @return the zip + */ +tb_zip_ref_t tb_zip_zlib_init(tb_size_t action); + +/* exit zlib + * + * @param zip the zip + */ +tb_void_t tb_zip_zlib_exit(tb_zip_ref_t zip); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/inc/tbox/zip/zlibraw.h b/core/pkg/tbox.pkg/inc/tbox/zip/zlibraw.h index ca0bd2bbf..93c528919 100644 --- a/core/pkg/tbox.pkg/inc/tbox/zip/zlibraw.h +++ b/core/pkg/tbox.pkg/inc/tbox/zip/zlibraw.h @@ -81,3 +81,86 @@ __tb_extern_c_leave__ #endif +/*!The Treasure Box Library + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2009 - 2017, TBOOX Open Source Group. + * + * @author ruki + * @file zlibraw.h + * @ingroup zip + * + */ +#ifndef TB_ZIP_ZLIBRAW_H +#define TB_ZIP_ZLIBRAW_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_PACKAGE_HAVE_ZLIB +# include "zlib/zlib.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the zlibraw zip type +typedef struct __tb_zip_zlibraw_t +{ + // the zip base + tb_zip_t base; + + // the zstream +#ifdef TB_CONFIG_PACKAGE_HAVE_ZLIB + z_stream zstream; +#endif + +}tb_zip_zlibraw_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* init zlibraw + * + * @param action the action + * + * @return the zip + */ +tb_zip_ref_t tb_zip_zlibraw_init(tb_size_t action); + +/* exit zlibraw + * + * @param zip the zip + */ +tb_void_t tb_zip_zlibraw_exit(tb_zip_ref_t zip); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif + diff --git a/core/pkg/tbox.pkg/lib/release/linux/i386/libtbox.a b/core/pkg/tbox.pkg/lib/release/linux/i386/libtbox.a Binary files differindex 586e77881..844f49c2c 100644 --- a/core/pkg/tbox.pkg/lib/release/linux/i386/libtbox.a +++ b/core/pkg/tbox.pkg/lib/release/linux/i386/libtbox.a |
