├── 1 ├── 4 │ └── embrace_change │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ └── main.c ├── 5 │ └── dry │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ └── main.c └── 6 │ └── data_store │ ├── Makefile │ ├── bss.c │ ├── data.c │ ├── dlist.c │ ├── dlist.h │ ├── dlist_toupper.c │ ├── heap_error.c │ └── toupper.c ├── 2 └── 4 │ └── autotest │ ├── Makefile │ ├── dlist.c │ └── dlist.h ├── 3 ├── 1 │ └── darray │ │ ├── Makefile │ │ ├── darray.c │ │ ├── darray.h │ │ └── typedef.h ├── 2 │ └── sort │ │ ├── Makefile │ │ ├── sort.c │ │ ├── sort.h │ │ └── typedef.h └── 3 │ └── sort_app │ ├── Makefile │ ├── darray.c │ ├── darray.h │ ├── search.c │ ├── sort.c │ ├── sort.h │ ├── test_helper.c │ ├── typedef.h │ └── unique.c ├── 4 ├── 2 │ └── locker │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── locker.h │ │ ├── locker_pthread.c │ │ ├── locker_pthread.h │ │ └── typedef.h ├── 3 │ └── locker_nest │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── locker.h │ │ ├── locker_nest.c │ │ ├── locker_nest.h │ │ ├── locker_pthread.c │ │ ├── locker_pthread.h │ │ └── typedef.h ├── 4 │ └── rw_locker │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── locker.h │ │ ├── locker_nest.c │ │ ├── locker_nest.h │ │ ├── locker_pthread.c │ │ ├── locker_pthread.h │ │ ├── rw_locker.c │ │ ├── rw_locker.h │ │ └── typedef.h └── 5 │ └── lock_free │ ├── Makefile │ ├── atomic.c │ ├── dlist.c │ ├── dlist.h │ ├── fifo_ring.c │ ├── iatomic.h │ ├── locker.h │ ├── swmr_dlist.c │ └── typedef.h ├── 5 ├── 1 │ └── queue │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── queue.c │ │ ├── queue.h │ │ ├── test_helper.c │ │ └── typedef.h ├── 2 │ └── stack │ │ ├── Makefile │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── stack.c │ │ ├── stack.h │ │ ├── test_helper.c │ │ └── typedef.h └── 3 │ └── hash_table │ ├── Makefile │ ├── dlist.c │ ├── dlist.h │ ├── hash_table.c │ ├── hash_table.h │ ├── test_helper.c │ └── typedef.h ├── 6 ├── 1 │ └── linear_container │ │ ├── Makefile │ │ ├── darray.c │ │ ├── darray.h │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── linear_container.h │ │ ├── linear_container_darray.c │ │ ├── linear_container_darray.h │ │ ├── linear_container_dlist.c │ │ ├── linear_container_dlist.h │ │ ├── linear_container_test.c │ │ ├── queue.c │ │ ├── queue.h │ │ ├── test_helper.c │ │ └── typedef.h ├── 2 │ └── iterator │ │ ├── Makefile │ │ ├── darray.c │ │ ├── darray.h │ │ ├── darray_iterator.c │ │ ├── darray_iterator.h │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── dlist_iterator.c │ │ ├── dlist_iterator.h │ │ ├── invert.c │ │ ├── invert_ng.c │ │ ├── iterator.h │ │ ├── linear_container.h │ │ ├── linear_container_darray.c │ │ ├── linear_container_darray.h │ │ ├── linear_container_dlist.c │ │ ├── linear_container_dlist.h │ │ ├── linear_container_test.c │ │ ├── test_helper.c │ │ └── typedef.h └── 3 │ └── runtime │ ├── Makefile │ ├── call_cos.c │ ├── darray.c │ ├── darray.h │ ├── dlist.c │ ├── dlist.h │ ├── linear_container.h │ ├── linear_container_darray.c │ ├── linear_container_darray.h │ ├── linear_container_dlist.c │ ├── linear_container_dlist.h │ ├── linear_container_test.c │ ├── module.h │ ├── module_linux.c │ ├── module_test.c │ ├── queue.c │ ├── queue.h │ ├── queue_test.c │ ├── test_helper.c │ └── typedef.h ├── 7 ├── 2 │ └── helloworld │ │ ├── AUTHORS │ │ ├── COPYING │ │ ├── ChangeLog │ │ ├── INSTALL │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── NEWS │ │ ├── README │ │ ├── aclocal.m4 │ │ ├── autogen.sh │ │ ├── autoscan.log │ │ ├── config.h.in │ │ ├── configure │ │ ├── configure.in │ │ ├── depcomp │ │ ├── install-sh │ │ ├── missing │ │ └── src │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ └── main.c ├── 3 │ └── base │ │ ├── AUTHORS │ │ ├── COPYING │ │ ├── ChangeLog │ │ ├── INSTALL │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── NEWS │ │ ├── README │ │ ├── aclocal.m4 │ │ ├── autogen.sh │ │ ├── autoscan.log │ │ ├── base.pc.in │ │ ├── config.h.in │ │ ├── configure │ │ ├── configure.in │ │ ├── depcomp │ │ ├── install-sh │ │ ├── missing │ │ └── src │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── darray.c │ │ ├── darray.h │ │ ├── darray_iterator.c │ │ ├── darray_iterator.h │ │ ├── darray_iterator.loT │ │ ├── dlist.c │ │ ├── dlist.h │ │ ├── dlist_iterator.c │ │ ├── dlist_iterator.h │ │ ├── hash_table.c │ │ ├── hash_table.h │ │ ├── invert.c │ │ ├── invert_ng.c │ │ ├── iterator.h │ │ ├── linear_container.h │ │ ├── linear_container_darray.c │ │ ├── linear_container_darray.h │ │ ├── linear_container_dlist.c │ │ ├── linear_container_dlist.h │ │ ├── linear_container_test.c │ │ ├── queue.c │ │ ├── queue.h │ │ ├── sort.c │ │ ├── sort.h │ │ ├── stack.c │ │ ├── stack.h │ │ ├── test_helper.c │ │ └── typedef.h └── 4 │ └── appdemo │ ├── AUTHORS │ ├── COPYING │ ├── ChangeLog │ ├── INSTALL │ ├── Makefile.am │ ├── Makefile.in │ ├── NEWS │ ├── README │ ├── aclocal.m4 │ ├── autogen.sh │ ├── autoscan.log │ ├── config.h.in │ ├── configure │ ├── configure.in │ ├── depcomp │ ├── install-sh │ ├── missing │ └── src │ ├── Makefile.am │ ├── Makefile.in │ └── main.c ├── 8 ├── 1 │ └── shmem │ │ ├── Makefile │ │ ├── fifo_ring.c │ │ └── typedef.h ├── 2 │ └── tls │ │ ├── Makefile │ │ └── tls.c └── 3 │ └── allocator │ ├── Makefile │ ├── allocator.h │ ├── allocator_checkbo.c │ ├── allocator_checkbo.h │ ├── allocator_normal.c │ ├── allocator_normal.h │ ├── allocator_pool.c │ ├── allocator_pool.h │ ├── allocator_self_manage.c │ ├── allocator_self_manage.h │ ├── allocator_shmem.c │ ├── allocator_shmem.h │ └── typedef.h ├── 9 ├── 1 │ └── varargs │ │ ├── Makefile │ │ └── varargs.c └── 2 │ └── backtrace │ ├── Makefile │ └── bt.c ├── 10 ├── 1 │ └── state │ │ ├── Makefile │ │ ├── count_word.c │ │ ├── ini_parser.c │ │ ├── parse_token.c │ │ ├── word_segmentation.c │ │ ├── xml_parser.c │ │ └── xml_parser.h └── 2 │ └── builder │ ├── Makefile │ ├── typedef.h │ ├── xml_builder.h │ ├── xml_builder_dump.c │ ├── xml_builder_dump.h │ ├── xml_builder_tree.c │ ├── xml_builder_tree.h │ ├── xml_parser.c │ ├── xml_parser.h │ ├── xml_parser_test.c │ ├── xml_tree.c │ └── xml_tree.h ├── 11 └── 3 │ ├── jdbshell │ ├── Makefile │ └── jdbshell.c │ └── shell │ ├── Makefile │ ├── app.c │ └── shell.c └── README.md /1/4/embrace_change/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g dlist.c main.c -o demo 3 | clean: 4 | rm -f demo 5 | -------------------------------------------------------------------------------- /1/4/embrace_change/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-09 Li XianJing created 29 | * 30 | */ 31 | 32 | #ifndef DLIST_H 33 | #define DLIST_H 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif/*__cplusplus*/ 38 | 39 | typedef enum _DListRet 40 | { 41 | DLIST_RET_OK, 42 | DLIST_RET_OOM, 43 | DLIST_RET_STOP, 44 | DLIST_RET_PARAMS, 45 | DLIST_RET_FAIL 46 | }DListRet; 47 | 48 | struct _DList; 49 | typedef struct _DList DList; 50 | 51 | typedef DListRet (*DListDataPrintFunc)(void* data); 52 | 53 | DList* dlist_create(void); 54 | 55 | DListRet dlist_insert(DList* thiz, size_t index, void* data); 56 | DListRet dlist_prepend(DList* thiz, void* data); 57 | DListRet dlist_append(DList* thiz, void* data); 58 | DListRet dlist_delete(DList* thiz, size_t index); 59 | DListRet dlist_get_by_index(DList* thiz, size_t index, void** data); 60 | DListRet dlist_set_by_index(DList* thiz, size_t index, void* data); 61 | size_t dlist_length(DList* thiz); 62 | DListRet dlist_print(DList* thiz, DListDataPrintFunc print); 63 | 64 | void dlist_destroy(DList* thiz); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif/*__cplusplus*/ 69 | 70 | #endif/*DLIST*/ 71 | 72 | -------------------------------------------------------------------------------- /1/4/embrace_change/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: main.c 3 | * Author: Li XianJing 4 | * Brief: demo how to print dlist. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-09 Li XianJing created 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include "dlist.h" 35 | 36 | static DListRet print_int(void* data) 37 | { 38 | printf("%d ", (int)data); 39 | 40 | return DLIST_RET_OK; 41 | } 42 | 43 | int main(int argc, char* argv[]) 44 | { 45 | int i = 0; 46 | int n = 100; 47 | DList* dlist = dlist_create(); 48 | 49 | for(i = 0; i < n; i++) 50 | { 51 | assert(dlist_append(dlist, (void*)i) == DLIST_RET_OK); 52 | } 53 | for(i = 0; i < n; i++) 54 | { 55 | assert(dlist_prepend(dlist, (void*)i) == DLIST_RET_OK); 56 | } 57 | 58 | dlist_print(dlist, print_int); 59 | 60 | dlist_destroy(dlist); 61 | 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /1/5/dry/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g dlist.c main.c -o demo 3 | 4 | clean: 5 | rm -f demo 6 | -------------------------------------------------------------------------------- /1/5/dry/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-19 Li XianJing created 29 | * 30 | */ 31 | 32 | #ifndef DLIST_H 33 | #define DLIST_H 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif/*__cplusplus*/ 38 | 39 | typedef enum _DListRet 40 | { 41 | DLIST_RET_OK, 42 | DLIST_RET_OOM, 43 | DLIST_RET_STOP, 44 | DLIST_RET_PARAMS, 45 | DLIST_RET_FAIL 46 | }DListRet; 47 | 48 | struct _DList; 49 | typedef struct _DList DList; 50 | 51 | typedef int (*DListDataCompareFunc)(void* ctx, void* data); 52 | typedef DListRet (*DListDataVisitFunc)(void* ctx, void* data); 53 | 54 | DList* dlist_create(void); 55 | 56 | DListRet dlist_insert(DList* thiz, size_t index, void* data); 57 | DListRet dlist_prepend(DList* thiz, void* data); 58 | DListRet dlist_append(DList* thiz, void* data); 59 | DListRet dlist_delete(DList* thiz, size_t index); 60 | DListRet dlist_get_by_index(DList* thiz, size_t index, void** data); 61 | DListRet dlist_set_by_index(DList* thiz, size_t index, void* data); 62 | size_t dlist_length(DList* thiz); 63 | int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx); 64 | DListRet dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx); 65 | 66 | void dlist_destroy(DList* thiz); 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif/*__cplusplus*/ 71 | 72 | #endif/*DLIST*/ 73 | 74 | -------------------------------------------------------------------------------- /1/5/dry/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dlist.h" 3 | #include 4 | 5 | static DListRet sum_cb(void* ctx, void* data) 6 | { 7 | long long* result = ctx; 8 | *result += (int)data; 9 | 10 | return DLIST_RET_OK; 11 | } 12 | 13 | typedef struct _MaxCtx 14 | { 15 | int is_first; 16 | int max; 17 | }MaxCtx; 18 | 19 | static DListRet max_cb(void* ctx, void* data) 20 | { 21 | MaxCtx* max_ctx = ctx; 22 | if(max_ctx->is_first) 23 | { 24 | max_ctx->is_first = 0; 25 | max_ctx->max = (int)data; 26 | } 27 | else if(max_ctx->max < (int)data) 28 | { 29 | max_ctx->max = (int)data; 30 | } 31 | 32 | return DLIST_RET_OK; 33 | } 34 | 35 | static DListRet print_int(void* ctx, void* data) 36 | { 37 | printf("%d ", (int)data); 38 | 39 | return DLIST_RET_OK; 40 | } 41 | 42 | int main(int argc, char* argv[]) 43 | { 44 | int i = 0; 45 | int n = 100; 46 | long long sum = 0; 47 | MaxCtx max_ctx = {.is_first = 1, 0}; 48 | DList* dlist = dlist_create(); 49 | 50 | for(i = 0; i < n; i++) 51 | { 52 | assert(dlist_append(dlist, (void*)i) == DLIST_RET_OK); 53 | } 54 | 55 | dlist_foreach(dlist, print_int, NULL); 56 | dlist_foreach(dlist, max_cb, &max_ctx); 57 | dlist_foreach(dlist, sum_cb, &sum); 58 | 59 | printf("\nsum=%lld max=%d\n", sum, max_ctx.max); 60 | dlist_destroy(dlist); 61 | 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /1/6/data_store/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g bss.c -o bss.exe 3 | gcc -g data.c -o data.exe 4 | gcc -g heap_error.c -o heap_error_test 5 | gcc -g toupper.c -o toupper_test 6 | gcc -g dlist.c dlist_toupper.c -o dlist_toupper_test 7 | clean: 8 | rm -f *test *.exe 9 | -------------------------------------------------------------------------------- /1/6/data_store/bss.c: -------------------------------------------------------------------------------- 1 | int bss_array[1024 * 1024]; 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /1/6/data_store/data.c: -------------------------------------------------------------------------------- 1 | int data_array[1024 * 1024] = {1}; 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /1/6/data_store/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 30 | */ 31 | #include 32 | 33 | #ifndef DLIST_H 34 | #define DLIST_H 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif/*__cplusplus*/ 39 | 40 | typedef enum _DListRet 41 | { 42 | DLIST_RET_OK, 43 | DLIST_RET_OOM, 44 | DLIST_RET_STOP, 45 | DLIST_RET_PARAMS, 46 | DLIST_RET_FAIL 47 | }DListRet; 48 | 49 | struct _DList; 50 | typedef struct _DList DList; 51 | 52 | typedef void (*DListDataDestroyFunc)(void* ctx, void* data); 53 | typedef int (*DListDataCompareFunc)(void* ctx, void* data); 54 | typedef DListRet (*DListDataVisitFunc)(void* ctx, void* data); 55 | 56 | DList* dlist_create(DListDataDestroyFunc data_destroy, void* data_destroy_ctx); 57 | 58 | DListRet dlist_insert(DList* thiz, size_t index, void* data); 59 | DListRet dlist_prepend(DList* thiz, void* data); 60 | DListRet dlist_append(DList* thiz, void* data); 61 | DListRet dlist_delete(DList* thiz, size_t index); 62 | DListRet dlist_get_by_index(DList* thiz, size_t index, void** data); 63 | DListRet dlist_set_by_index(DList* thiz, size_t index, void* data); 64 | size_t dlist_length(DList* thiz); 65 | int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx); 66 | DListRet dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx); 67 | 68 | void dlist_destroy(DList* thiz); 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif/*__cplusplus*/ 73 | 74 | #endif/*DLIST*/ 75 | 76 | -------------------------------------------------------------------------------- /1/6/data_store/dlist_toupper.c: -------------------------------------------------------------------------------- 1 | #include "dlist.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static DListRet str_toupper(void* ctx, void* data) 7 | { 8 | char* p = (char*)data; 9 | if(p != NULL) 10 | { 11 | while(*p != '\0') 12 | { 13 | if(islower(*p)) 14 | { 15 | *p = toupper(*p); 16 | } 17 | p++; 18 | } 19 | } 20 | 21 | return DLIST_RET_OK; 22 | } 23 | 24 | static DListRet str_print(void* ctx, void* data) 25 | { 26 | printf("%s\n", data); 27 | 28 | return DLIST_RET_OK; 29 | } 30 | 31 | static void demo_const(void) 32 | { 33 | DList* dlist = dlist_create(NULL, NULL); 34 | dlist_append(dlist, "It"); 35 | dlist_append(dlist, "is"); 36 | dlist_append(dlist, "OK"); 37 | dlist_append(dlist, "!"); 38 | dlist_foreach(dlist, str_toupper, NULL); 39 | dlist_destroy(dlist); 40 | 41 | return ; 42 | } 43 | 44 | static void demo_temp(void) 45 | { 46 | char str[256] = {0}; 47 | DList* dlist = dlist_create(NULL, NULL); 48 | strcpy(str, "It"); 49 | dlist_append(dlist, str); 50 | strcpy(str, "is"); 51 | dlist_append(dlist, str); 52 | strcpy(str, "OK"); 53 | dlist_append(dlist, str); 54 | strcpy(str, "!"); 55 | dlist_append(dlist, str); 56 | dlist_foreach(dlist, str_toupper, NULL); 57 | dlist_foreach(dlist, str_print, NULL); 58 | dlist_destroy(dlist); 59 | 60 | return ; 61 | } 62 | 63 | static void data_free(void* ctx, void* data) 64 | { 65 | free(data); 66 | 67 | return; 68 | } 69 | 70 | static void demo_heap(void) 71 | { 72 | DList* dlist = dlist_create(data_free, NULL); 73 | dlist_append(dlist, strdup("It")); 74 | dlist_append(dlist, strdup("is")); 75 | dlist_append(dlist, strdup("OK")); 76 | dlist_append(dlist, strdup("!")); 77 | dlist_foreach(dlist, str_toupper, NULL); 78 | dlist_foreach(dlist, str_print, NULL); 79 | dlist_destroy(dlist); 80 | 81 | return ; 82 | } 83 | 84 | int main(int argc, char* argv[]) 85 | { 86 | demo_temp(); 87 | demo_heap(); 88 | demo_const(); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /1/6/data_store/heap_error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /*# valgrind --tool=memcheck --leak-check=full ./heap_error_test */ 4 | int main(int argc, char* argv[]) 5 | { 6 | /*memory leak and buffer overflow.*/ 7 | char* str = malloc(10); 8 | strcpy(str, "123456788900"); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /1/6/data_store/toupper.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char* str_toupper_bad(char* str) 6 | { 7 | char* p = str; 8 | if(p != NULL) 9 | { 10 | while(*p != '\0') 11 | { 12 | if('a' <= *p && *p <= 'z') 13 | { 14 | *p = *p - ('a' - 'A'); 15 | } 16 | p++; 17 | } 18 | } 19 | 20 | return str; 21 | } 22 | 23 | char* str_toupper_good(char* str) 24 | { 25 | char* p = str; 26 | if(p != NULL) 27 | { 28 | while(*p != '\0') 29 | { 30 | if(islower(*p)) 31 | { 32 | *p = toupper(*p); 33 | } 34 | p++; 35 | } 36 | } 37 | 38 | return str; 39 | } 40 | 41 | int main(int argc, char* argv[]) 42 | { 43 | char str1[] = "It is ok."; 44 | char str2[] = "It is ok."; 45 | printf("%s\n", str_toupper_bad(str1)); 46 | printf("%s\n", str_toupper_good(str2)); 47 | } 48 | -------------------------------------------------------------------------------- /10/1/state/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -Wall count_word.c -DCOUNT_WORD_TEST -o count_word_test 3 | gcc -g -Wall word_segmentation.c -DWORD_SEGMENTATION_TEST -o word_segmentation_test 4 | gcc -g -Wall parse_token.c -DPARSE_TOKEN_TEST -o parse_token_test 5 | gcc -g -Wall ini_parser.c -DINI_PARSER_TEST -o ini_parser_test 6 | 7 | clean: 8 | rm -f *test 9 | -------------------------------------------------------------------------------- /10/1/state/count_word.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define IS_WORD_CHAR(c) (isalpha(c) || isdigit(c)) 5 | 6 | int count_word(const char* text) 7 | { 8 | enum _State 9 | { 10 | STAT_INIT, 11 | STAT_IN_WORD, 12 | STAT_OUT_WORD, 13 | }state = STAT_INIT; 14 | 15 | int count = 0; 16 | const char* p = text; 17 | 18 | for(p = text; *p != '\0'; p++) 19 | { 20 | switch(state) 21 | { 22 | case STAT_INIT: 23 | { 24 | if(IS_WORD_CHAR(*p)) 25 | { 26 | state = STAT_IN_WORD; 27 | } 28 | else 29 | { 30 | state = STAT_OUT_WORD; 31 | } 32 | break; 33 | } 34 | case STAT_IN_WORD: 35 | { 36 | if(!IS_WORD_CHAR(*p)) 37 | { 38 | count++; 39 | state = STAT_OUT_WORD; 40 | } 41 | break; 42 | } 43 | case STAT_OUT_WORD: 44 | { 45 | if(IS_WORD_CHAR(*p)) 46 | { 47 | state = STAT_IN_WORD; 48 | } 49 | break; 50 | } 51 | default:break; 52 | } 53 | } 54 | 55 | if(state == STAT_IN_WORD) 56 | { 57 | count++; 58 | } 59 | 60 | return count; 61 | } 62 | 63 | #ifdef COUNT_WORD_TEST 64 | #include 65 | 66 | int main(int argc, char* argv[]) 67 | { 68 | assert(count_word("") == 0); 69 | assert(count_word(" ") == 0); 70 | assert(count_word("it") == 1); 71 | assert(count_word("it ") == 1); 72 | assert(count_word("it is") == 2); 73 | assert(count_word("it is used to count words.") == 6); 74 | assert(count_word("it is used to count words. is it easy?") == 9); 75 | 76 | return 0; 77 | } 78 | #endif/*COUNT_WORD_TEST*/ 79 | 80 | -------------------------------------------------------------------------------- /10/1/state/parse_token.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef void (*OnTokenFunc)(void* ctx, int index, const char* token); 7 | 8 | #define IS_DELIM(c) (strchr(delims, c) != NULL) 9 | int parse_token(const char* text, const char* delims, OnTokenFunc on_token, void* ctx) 10 | { 11 | enum _State 12 | { 13 | STAT_INIT, 14 | STAT_IN, 15 | STAT_OUT, 16 | }state = STAT_INIT; 17 | 18 | int count = 0; 19 | char* copy_text = strdup(text); 20 | char* p = copy_text; 21 | char* token = copy_text; 22 | 23 | for(p = copy_text; *p != '\0'; p++) 24 | { 25 | switch(state) 26 | { 27 | case STAT_INIT: 28 | case STAT_OUT: 29 | { 30 | if(!IS_DELIM(*p)) 31 | { 32 | token = p; 33 | state = STAT_IN; 34 | } 35 | break; 36 | } 37 | case STAT_IN: 38 | { 39 | if(IS_DELIM(*p)) 40 | { 41 | *p = '\0'; 42 | on_token(ctx, count++, token); 43 | state = STAT_OUT; 44 | } 45 | break; 46 | } 47 | default:break; 48 | } 49 | } 50 | 51 | if(state == STAT_IN) 52 | { 53 | on_token(ctx, count++, token); 54 | } 55 | 56 | on_token(ctx, -1, NULL); 57 | free(copy_text); 58 | 59 | return count; 60 | } 61 | 62 | #ifdef PARSE_TOKEN_TEST 63 | #include 64 | 65 | void on_token(void* ctx, int index, const char* token) 66 | { 67 | printf("[%d] %s\n", index, token); 68 | 69 | return; 70 | } 71 | 72 | int main(int argc, char* argv[]) 73 | { 74 | assert(parse_token("it is used to token segmentation. is it easy?", " .?", on_token, NULL) == 9); 75 | assert(parse_token("/backup/tools/jdk1.5.0_18/bin/:/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/backup/tools/jdk1.5.0_18/bin/:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/home/lixianjing/bin", ":", on_token, NULL) == 9); 76 | assert(parse_token("/backup/tools/jdk1.5.0_18/bin/", "/", on_token, NULL) == 4); 77 | 78 | return 0; 79 | } 80 | #endif/*COUNT_TEST*/ 81 | 82 | -------------------------------------------------------------------------------- /10/1/state/word_segmentation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define IS_WORD_CHAR(c) (isalpha(c) || isdigit(c)) 7 | 8 | typedef void (*OnWordFunc)(void* ctx, const char* word); 9 | 10 | int word_segmentation(const char* text, OnWordFunc on_word, void* ctx) 11 | { 12 | enum _State 13 | { 14 | STAT_INIT, 15 | STAT_IN_WORD, 16 | STAT_OUT_WORD, 17 | }state = STAT_INIT; 18 | 19 | int count = 0; 20 | char* copy_text = strdup(text); 21 | char* p = copy_text; 22 | char* word = copy_text; 23 | 24 | for(p = copy_text; *p != '\0'; p++) 25 | { 26 | switch(state) 27 | { 28 | case STAT_INIT: 29 | { 30 | if(IS_WORD_CHAR(*p)) 31 | { 32 | word = p; 33 | state = STAT_IN_WORD; 34 | } 35 | break; 36 | } 37 | case STAT_IN_WORD: 38 | { 39 | if(!IS_WORD_CHAR(*p)) 40 | { 41 | count++; 42 | *p = '\0'; 43 | on_word(ctx, word); 44 | state = STAT_OUT_WORD; 45 | } 46 | break; 47 | } 48 | case STAT_OUT_WORD: 49 | { 50 | if(IS_WORD_CHAR(*p)) 51 | { 52 | word = p; 53 | state = STAT_IN_WORD; 54 | } 55 | break; 56 | } 57 | default:break; 58 | } 59 | } 60 | 61 | if(state == STAT_IN_WORD) 62 | { 63 | count++; 64 | on_word(ctx, word); 65 | } 66 | 67 | free(copy_text); 68 | 69 | return count; 70 | } 71 | 72 | #ifdef WORD_SEGMENTATION_TEST 73 | #include 74 | 75 | void on_word(void* ctx, const char* word) 76 | { 77 | printf("%s\n", word); 78 | 79 | return; 80 | } 81 | 82 | int main(int argc, char* argv[]) 83 | { 84 | assert(word_segmentation("it is used to word segmentation. is it easy?", on_word, NULL) == 9); 85 | 86 | return 0; 87 | } 88 | #endif/*WORD_SEGMENTATION_TEST*/ 89 | 90 | -------------------------------------------------------------------------------- /10/1/state/xml_parser.h: -------------------------------------------------------------------------------- 1 | #ifndef XML_PARSER_H 2 | #define XML_PARSER_H 3 | struct _XmlParser; 4 | typedef struct _XmlParser XmlParser; 5 | 6 | XmlParser* xml_parser_create(void); 7 | void xml_parser_parse(XmlParser* thiz, const char* xml); 8 | void xml_parser_destroy(XmlParser* thiz); 9 | 10 | #endif/*XML_PARSER_H*/ 11 | 12 | -------------------------------------------------------------------------------- /10/2/builder/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -Wall -DXML_BUILDER_DUMP_TEST xml_builder_dump.c -o xml_builder_dump_test 3 | gcc -g -Wall -DXML_PARSER_TEST xml_parser.c xml_builder_dump.c xml_builder_tree.c xml_tree.c xml_parser_test.c -o xml_parser_test 4 | gcc -g -Wall -DXML_TREE_TEST xml_tree.c xml_builder_dump.c -o xml_tree_test 5 | gcc -g -Wall -DXML_BUILDER_TREE_TEST xml_builder_tree.c xml_tree.c xml_builder_dump.c -o xml_builder_tree_test 6 | clean: 7 | rm -f *test 8 | -------------------------------------------------------------------------------- /10/2/builder/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #define MAX_ATTR_NR 64 73 | 74 | 75 | #endif/*TYPEDEF_H*/ 76 | 77 | -------------------------------------------------------------------------------- /10/2/builder/xml_builder_dump.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: xml_builder_dump.h 3 | * Author: Li XianJing 4 | * Brief: xml builder dump implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-05-30 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef XML_BUILDER_DUMP_H 33 | #define XML_BUILDER_DUMP_H 34 | #include "xml_builder.h" 35 | 36 | DECLS_BEGIN 37 | 38 | XmlBuilder* xml_builder_dump_create(FILE* fp); 39 | 40 | DECLS_END 41 | 42 | #endif/*XML_BUILDER_DUMP_H*/ 43 | 44 | -------------------------------------------------------------------------------- /10/2/builder/xml_builder_tree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: xml_builder_tree.h 3 | * Author: Li XianJing 4 | * Brief: xml builder tree implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-06-22 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef XML_BUILDER_TREE_H 33 | #define XML_BUILDER_TREE_H 34 | #include "xml_builder.h" 35 | #include "xml_tree.h" 36 | 37 | DECLS_BEGIN 38 | 39 | XmlBuilder* xml_builder_tree_create(void); 40 | XmlNode* xml_builder_get_tree(XmlBuilder* thiz); 41 | 42 | DECLS_END 43 | 44 | #endif/*XML_BUILDER_TREE_H*/ 45 | 46 | -------------------------------------------------------------------------------- /10/2/builder/xml_parser.h: -------------------------------------------------------------------------------- 1 | #ifndef XML_PARSER_H 2 | #define XML_PARSER_H 3 | 4 | #include "xml_builder.h" 5 | 6 | struct _XmlParser; 7 | typedef struct _XmlParser XmlParser; 8 | 9 | XmlParser* xml_parser_create(void); 10 | void xml_parser_set_builder(XmlParser* thiz, XmlBuilder* builder); 11 | void xml_parser_parse(XmlParser* thiz, const char* xml); 12 | void xml_parser_destroy(XmlParser* thiz); 13 | 14 | #endif/*XML_PARSER_H*/ 15 | 16 | -------------------------------------------------------------------------------- /10/2/builder/xml_parser_test.c: -------------------------------------------------------------------------------- 1 | #include "xml_parser.h" 2 | #include "xml_builder_dump.h" 3 | #include "xml_builder_tree.h" 4 | 5 | #define XML " \ 6 |

ppp


\ 7 | text" 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "xml_builder_dump.h" 14 | 15 | char* read_file(const char* file_name) 16 | { 17 | char* buffer = NULL; 18 | FILE* fp = fopen(file_name, "r"); 19 | 20 | if(fp != NULL) 21 | { 22 | struct stat st = {0}; 23 | if(stat(file_name, &st) == 0) 24 | { 25 | buffer = malloc(st.st_size + 1); 26 | fread(buffer, st.st_size, 1, fp); 27 | buffer[st.st_size] = '\0'; 28 | } 29 | } 30 | 31 | return buffer; 32 | } 33 | 34 | void dump_tree(XmlNode* tree) 35 | { 36 | XmlBuilder* dump_builder = xml_builder_dump_create(stdout); 37 | xml_node_visit(tree, dump_builder); 38 | xml_builder_destroy(dump_builder); 39 | 40 | return; 41 | } 42 | 43 | int main(int argc, char* argv[]) 44 | { 45 | XmlParser* thiz = xml_parser_create(); 46 | XmlBuilder* builder = xml_builder_tree_create(); 47 | 48 | xml_parser_set_builder(thiz, builder); 49 | 50 | if(argc > 1) 51 | { 52 | char* buffer = read_file(argv[1]); 53 | xml_parser_parse(thiz, buffer); 54 | free(buffer); 55 | } 56 | 57 | XmlNode* tree = xml_builder_get_tree(builder); 58 | dump_tree(tree); 59 | xml_node_destroy(tree); 60 | 61 | xml_builder_destroy(builder); 62 | xml_parser_destroy(thiz); 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /10/2/builder/xml_tree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: xml_tree.h 3 | * Author: Li XianJing 4 | * Brief: xml tree 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-06-22 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "typedef.h" 33 | #include "xml_builder.h" 34 | 35 | #ifndef XML_TREE_H 36 | #define XML_TREE_H 37 | 38 | typedef enum _XmlNodeType 39 | { 40 | XML_NODE_NONE, 41 | XML_NODE_PI, 42 | XML_NODE_TEXT, 43 | XML_NODE_NORMAL, 44 | XML_NODE_COMMENT 45 | }XmlNodeType; 46 | 47 | typedef struct _XmlAttr 48 | { 49 | char* name; 50 | char* value; 51 | 52 | struct _XmlAttr* next; 53 | }XmlAttr; 54 | 55 | typedef struct _XmlNodePi 56 | { 57 | char* name; 58 | XmlAttr* first_attr; 59 | }XmlNodePi; 60 | 61 | typedef struct _XmlNodeNormal 62 | { 63 | char* name; 64 | XmlAttr* first_attr; 65 | }XmlNodeNormal; 66 | 67 | typedef struct _XmlNode 68 | { 69 | XmlNodeType type; 70 | union 71 | { 72 | char* text; 73 | char* comment; 74 | XmlNodePi pi; 75 | XmlNodeNormal normal; 76 | }u; 77 | struct _XmlNode* parent; 78 | struct _XmlNode* children; 79 | struct _XmlNode* sibling; 80 | }XmlNode; 81 | 82 | typedef Ret (*XmlNodeVisitFunc)(void* ctx, int level, XmlNode* node); 83 | 84 | XmlNode* xml_node_create_text(const char* text); 85 | XmlNode* xml_node_create_comment(const char* comment); 86 | XmlNode* xml_node_create_pi(const char* name, const char** attrs); 87 | XmlNode* xml_node_create_normal(const char* name, const char** attrs); 88 | XmlNode* xml_node_append_sibling(XmlNode* node, XmlNode* sibling); 89 | XmlNode* xml_node_append_child(XmlNode* node, XmlNode* child); 90 | 91 | void xml_node_visit(XmlNode* node, XmlBuilder* builder); 92 | void xml_node_print(XmlNode* node); 93 | 94 | void xml_node_destroy(XmlNode* node); 95 | 96 | #endif/*XML_TREE_H*/ 97 | 98 | -------------------------------------------------------------------------------- /11/3/jdbshell/Makefile: -------------------------------------------------------------------------------- 1 | #CFLAGS=-DHAVE_CONFIG_H -DREADLINE_LIBRARY -DRL_LIBRARY_VERSION='"5.1"' -I. -I.. -g -O 2 | CFLAGS=-DRL_LIBRARY_VERSION='"5.1"' -I. -I.. -g -O 3 | all: 4 | gcc $(CFLAGS) jdbshell.c -lreadline -ltermcap -o jdbshell 5 | clean: 6 | rm -f jdbshell *.o 7 | -------------------------------------------------------------------------------- /11/3/shell/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g shell.c -o shell.exe 3 | gcc -g app.c -o app.exe 4 | 5 | clean: 6 | rm -f *.exe 7 | 8 | -------------------------------------------------------------------------------- /11/3/shell/app.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int main(int argc, char* argv[]) 6 | { 7 | int n = 0; 8 | 9 | printf("Input number:\n"); 10 | fflush(stdout); 11 | scanf("%d", &n); 12 | printf("You input %d\n", n); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /11/3/shell/shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | int shell_to_app[2] = {0}; 7 | int app_to_shell[2] = {0}; 8 | 9 | pipe(shell_to_app); 10 | pipe(app_to_shell); 11 | 12 | if(fork() == 0) 13 | { 14 | close(shell_to_app[1]); 15 | close(app_to_shell[0]); 16 | 17 | dup2(shell_to_app[0], STDIN_FILENO); 18 | dup2(app_to_shell[1], STDOUT_FILENO); 19 | 20 | execl("./app.exe", "./app.exe", NULL); 21 | } 22 | else 23 | { 24 | int i = 0; 25 | FILE* in = fdopen(app_to_shell[0], "r"); 26 | FILE* out = fdopen(shell_to_app[1], "w"); 27 | 28 | char message[256] = {0}; 29 | close(shell_to_app[0]); 30 | close(app_to_shell[1]); 31 | 32 | fgets(message, sizeof(message), in); 33 | printf("1: %s\n", message); 34 | fprintf(out, "1234\n"); 35 | fflush(out); 36 | fgets(message, sizeof(message), in); 37 | printf("2: %s\n", message); 38 | fclose(in); 39 | fclose(out); 40 | } 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /2/4/autotest/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -shared dlist.c -o libdlist.so 3 | gcc -g -DDLIST_TEST dlist.c -o dlist_test 4 | 5 | clean: 6 | rm -f *test *.exe *.so 7 | -------------------------------------------------------------------------------- /3/1/darray/Makefile: -------------------------------------------------------------------------------- 1 | CFILES=darray.c 2 | all: 3 | gcc -g -shared -lpthread $(CFILES) -o libdarray.so 4 | gcc -g -DDARRAY_TEST -lpthread $(CFILES) -o darray_test 5 | 6 | clean: 7 | rm -f *test *.exe *.so 8 | -------------------------------------------------------------------------------- /3/1/darray/darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: darray.h 3 | * Author: Li XianJing 4 | * Brief: dynamic array implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-01-02 Li XianJing created 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef DARRAY_H 35 | #define DARRAY_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _DArray; 40 | typedef struct _DArray DArray; 41 | 42 | DArray* darray_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret darray_insert(DArray* thiz, size_t index, void* data); 45 | Ret darray_prepend(DArray* thiz, void* data); 46 | Ret darray_append(DArray* thiz, void* data); 47 | Ret darray_delete(DArray* thiz, size_t index); 48 | Ret darray_get_by_index(DArray* thiz, size_t index, void** data); 49 | Ret darray_set_by_index(DArray* thiz, size_t index, void* data); 50 | size_t darray_length(DArray* thiz); 51 | int darray_find(DArray* thiz, DataCompareFunc cmp, void* ctx); 52 | Ret darray_foreach(DArray* thiz, DataVisitFunc visit, void* ctx); 53 | 54 | void darray_destroy(DArray* thiz); 55 | 56 | DECLS_END 57 | 58 | #endif/*DARRAY_H*/ 59 | 60 | -------------------------------------------------------------------------------- /3/1/darray/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | 52 | #ifdef __cplusplus 53 | #define DECLS_BEGIN extern "C" { 54 | #define DECLS_END } 55 | #else 56 | #define DECLS_BEGIN 57 | #define DECLS_END 58 | #endif/*__cplusplus*/ 59 | 60 | #define return_if_fail(p) if(!(p)) \ 61 | {printf("%s:%d Warning: "#p" failed.\n", \ 62 | __func__, __LINE__); return;} 63 | #define return_val_if_fail(p, ret) if(!(p)) \ 64 | {printf("%s:%d Warning: "#p" failed.\n",\ 65 | __func__, __LINE__); return (ret);} 66 | 67 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 68 | 69 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 70 | 71 | #endif/*TYPEDEF_H*/ 72 | 73 | -------------------------------------------------------------------------------- /3/2/sort/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g sort.c -DSORT_TEST -o sort_test 3 | 4 | clean: 5 | rm -f *test *.exe *.so 6 | -------------------------------------------------------------------------------- /3/2/sort/sort.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: sort.h 3 | * Author: Li XianJing 4 | * Brief: prototype of sort functions. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-02-07 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef SORT_H 33 | #define SORT_H 34 | 35 | #include "typedef.h" 36 | 37 | Ret bubble_sort(void** array, size_t nr, DataCompareFunc cmp); 38 | Ret quick_sort(void** array, size_t nr, DataCompareFunc cmp); 39 | Ret merge_sort(void** array, size_t nr, DataCompareFunc cmp); 40 | 41 | #endif/*SORT_H*/ 42 | 43 | -------------------------------------------------------------------------------- /3/2/sort/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | 52 | #ifdef __cplusplus 53 | #define DECLS_BEGIN extern "C" { 54 | #define DECLS_END } 55 | #else 56 | #define DECLS_BEGIN 57 | #define DECLS_END 58 | #endif/*__cplusplus*/ 59 | 60 | #define return_if_fail(p) if(!(p)) \ 61 | {printf("%s:%d Warning: "#p" failed.\n", \ 62 | __func__, __LINE__); return;} 63 | #define return_val_if_fail(p, ret) if(!(p)) \ 64 | {printf("%s:%d Warning: "#p" failed.\n",\ 65 | __func__, __LINE__); return (ret);} 66 | 67 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 68 | 69 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 70 | 71 | #endif/*TYPEDEF_H*/ 72 | 73 | -------------------------------------------------------------------------------- /3/3/sort_app/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -DDARRAY_TEST -lpthread darray.c sort.c -o darray_test 3 | gcc -g -Wall -DSORT_TEST sort.c -o sort_test 4 | gcc -g -Wall -DSEARCH_TEST search.c -o search_test 5 | gcc -g -Wall -DUNIQUE_TEST unique.c darray.c sort.c -o unique_test 6 | 7 | clean: 8 | rm -f *test *.exe *.so 9 | -------------------------------------------------------------------------------- /3/3/sort_app/darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: darray.h 3 | * Author: Li XianJing 4 | * Brief: dynamic array implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-01-02 Li XianJing created 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef DARRAY_H 35 | #define DARRAY_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _DArray; 40 | typedef struct _DArray DArray; 41 | 42 | DArray* darray_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret darray_insert(DArray* thiz, size_t index, void* data); 45 | Ret darray_prepend(DArray* thiz, void* data); 46 | Ret darray_append(DArray* thiz, void* data); 47 | Ret darray_delete(DArray* thiz, size_t index); 48 | Ret darray_get_by_index(DArray* thiz, size_t index, void** data); 49 | Ret darray_set_by_index(DArray* thiz, size_t index, void* data); 50 | size_t darray_length(DArray* thiz); 51 | int darray_find(DArray* thiz, DataCompareFunc cmp, void* ctx); 52 | Ret darray_foreach(DArray* thiz, DataVisitFunc visit, void* ctx); 53 | Ret darray_sort(DArray* thiz, SortFunc sort, DataCompareFunc cmp); 54 | 55 | void darray_destroy(DArray* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DARRAY_H*/ 60 | 61 | -------------------------------------------------------------------------------- /3/3/sort_app/search.c: -------------------------------------------------------------------------------- 1 | #include "typedef.h" 2 | 3 | int qsearch(void** array, size_t nr, void* data, DataCompareFunc cmp) 4 | { 5 | int low = 0; 6 | int mid = 0; 7 | int high = nr-1; 8 | int result = 0; 9 | 10 | return_val_if_fail(array != NULL && cmp != NULL, -1); 11 | 12 | while(low <= high) 13 | { 14 | mid = low + ((high - low) >> 1); 15 | result = cmp(array[mid], data); 16 | 17 | if(result == 0) 18 | { 19 | return mid; 20 | } 21 | else if(result < 0) 22 | { 23 | low = mid + 1; 24 | } 25 | else 26 | { 27 | high = mid - 1; 28 | } 29 | } 30 | 31 | return -1; 32 | } 33 | 34 | #ifdef SEARCH_TEST 35 | #include 36 | int int_cmp(void* a, void* b) 37 | { 38 | return (int)a - (int)b; 39 | } 40 | 41 | static void search_test(size_t n) 42 | { 43 | int i = 0; 44 | int* array = (int*)malloc(n * sizeof(int)); 45 | 46 | for(i = 0; i < n; i++) 47 | { 48 | array[i] = i; 49 | } 50 | 51 | for(i = 0; i < n; i++) 52 | { 53 | assert(qsearch((void**)array, n, (void*)i, int_cmp) == i); 54 | } 55 | 56 | free(array); 57 | 58 | return; 59 | } 60 | 61 | int main(int argc, char* argv[]) 62 | { 63 | int i = 0; 64 | for(i = 1; i < 1000; i++) 65 | { 66 | search_test(i); 67 | } 68 | 69 | return 0; 70 | } 71 | #endif/*QSEARCH_TEST*/ 72 | 73 | -------------------------------------------------------------------------------- /3/3/sort_app/sort.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: sort.h 3 | * Author: Li XianJing 4 | * Brief: prototype of sort functions. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-02-07 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef SORT_H 33 | #define SORT_H 34 | 35 | #include "typedef.h" 36 | 37 | DECLS_BEGIN 38 | 39 | Ret bubble_sort(void** array, size_t nr, DataCompareFunc cmp); 40 | Ret quick_sort(void** array, size_t nr, DataCompareFunc cmp); 41 | Ret merge_sort(void** array, size_t nr, DataCompareFunc cmp); 42 | 43 | DECLS_END 44 | 45 | #endif/*SORT_H*/ 46 | 47 | -------------------------------------------------------------------------------- /3/3/sort_app/test_helper.c: -------------------------------------------------------------------------------- 1 | 2 | static int cmp_int(void* ctx, void* data) 3 | { 4 | return (int)ctx - (int)data; 5 | } 6 | 7 | static Ret print_int(void* ctx, void* data) 8 | { 9 | printf("%d ", (int)data); 10 | 11 | return RET_OK; 12 | } 13 | -------------------------------------------------------------------------------- /3/3/sort_app/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | 52 | #ifdef __cplusplus 53 | #define DECLS_BEGIN extern "C" { 54 | #define DECLS_END } 55 | #else 56 | #define DECLS_BEGIN 57 | #define DECLS_END 58 | #endif/*__cplusplus*/ 59 | 60 | #define return_if_fail(p) if(!(p)) \ 61 | {printf("%s:%d Warning: "#p" failed.\n", \ 62 | __func__, __LINE__); return;} 63 | #define return_val_if_fail(p, ret) if(!(p)) \ 64 | {printf("%s:%d Warning: "#p" failed.\n",\ 65 | __func__, __LINE__); return (ret);} 66 | 67 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 68 | 69 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 70 | 71 | #endif/*TYPEDEF_H*/ 72 | 73 | -------------------------------------------------------------------------------- /3/3/sort_app/unique.c: -------------------------------------------------------------------------------- 1 | #include "darray.h" 2 | 3 | Ret unique_print_int(void* ctx, void* data) 4 | { 5 | if(*(int*)ctx != (int)data) 6 | { 7 | *(int*)ctx = (int)data; 8 | printf("%d ", (int)data); 9 | } 10 | 11 | return RET_OK; 12 | } 13 | 14 | 15 | #ifdef UNIQUE_TEST 16 | #include "sort.h" 17 | #include "darray.h" 18 | #include 19 | #include "test_helper.c" 20 | 21 | int main(int argc, char* argv[]) 22 | { 23 | int i = 0; 24 | int n = 100; 25 | int data = 0; 26 | 27 | DArray* darray = darray_create(NULL, NULL); 28 | 29 | for(i = 0; i < n; i++) 30 | { 31 | darray_append(darray, (void*)(rand()%n)); 32 | } 33 | 34 | darray_sort(darray, quick_sort, cmp_int); 35 | 36 | darray_foreach(darray, print_int, NULL); 37 | printf("\n"); 38 | darray_get_by_index(darray, 0, (void*)&data); 39 | data = !data; 40 | darray_foreach(darray, unique_print_int, &data); 41 | printf("\n"); 42 | darray_destroy(darray); 43 | 44 | return 0; 45 | } 46 | 47 | #endif/*UNIQUE_TEST*/ 48 | 49 | -------------------------------------------------------------------------------- /4/2/locker/Makefile: -------------------------------------------------------------------------------- 1 | CFILES=dlist.c locker_pthread.c 2 | all: 3 | gcc -g -shared -lpthread $(CFILES) -o libdlist.so 4 | gcc -g -DDLIST_TEST -lpthread $(CFILES) -o dlist_test 5 | 6 | clean: 7 | rm -f *test *.exe *.so 8 | -------------------------------------------------------------------------------- /4/2/locker/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | * 2008-12-10 Li XianJing add lock support. 31 | */ 32 | 33 | #include 34 | #include "locker.h" 35 | 36 | #ifndef DLIST_H 37 | #define DLIST_H 38 | 39 | DECLS_BEGIN 40 | 41 | struct _DList; 42 | typedef struct _DList DList; 43 | 44 | typedef void (*DListDataDestroyFunc)(void* ctx, void* data); 45 | typedef int (*DListDataCompareFunc)(void* ctx, void* data); 46 | typedef Ret (*DListDataVisitFunc)(void* ctx, void* data); 47 | 48 | DList* dlist_create(DListDataDestroyFunc data_destroy, void* ctx, Locker* locker); 49 | 50 | Ret dlist_insert(DList* thiz, size_t index, void* data); 51 | Ret dlist_prepend(DList* thiz, void* data); 52 | Ret dlist_append(DList* thiz, void* data); 53 | Ret dlist_delete(DList* thiz, size_t index); 54 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 55 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 56 | size_t dlist_length(DList* thiz); 57 | int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx); 58 | Ret dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx); 59 | 60 | void dlist_destroy(DList* thiz); 61 | 62 | DECLS_END 63 | 64 | #endif/*DLIST*/ 65 | 66 | -------------------------------------------------------------------------------- /4/2/locker/locker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker 3 | * Author: Li XianJing 4 | * Brief: lock interface. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "typedef.h" 33 | 34 | #ifndef LOCKER_H 35 | #define LOCKER_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Locker; 40 | typedef struct _Locker Locker; 41 | 42 | typedef Ret (*LockerLockFunc)(Locker* thiz); 43 | typedef Ret (*LockerUnlockFunc)(Locker* thiz); 44 | typedef void (*LockerDestroyFunc)(Locker* thiz); 45 | 46 | struct _Locker 47 | { 48 | LockerLockFunc lock; 49 | LockerUnlockFunc unlock; 50 | LockerDestroyFunc destroy; 51 | 52 | char priv[0]; 53 | }; 54 | 55 | static inline Ret locker_lock(Locker* thiz) 56 | { 57 | return_val_if_fail(thiz != NULL && thiz->lock != NULL, RET_INVALID_PARAMS); 58 | 59 | return thiz->lock(thiz); 60 | } 61 | 62 | static inline Ret locker_unlock(Locker* thiz) 63 | { 64 | return_val_if_fail(thiz != NULL && thiz->unlock != NULL, RET_INVALID_PARAMS); 65 | 66 | return thiz->unlock(thiz); 67 | } 68 | 69 | static inline void locker_destroy(Locker* thiz) 70 | { 71 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 72 | 73 | thiz->destroy(thiz); 74 | 75 | return; 76 | } 77 | 78 | DECLS_END 79 | 80 | #endif/*LOCKER_H*/ 81 | 82 | -------------------------------------------------------------------------------- /4/2/locker/locker_pthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_pthread.c 3 | * Author: Li XianJing 4 | * Brief: pthread mutex implemented locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include "locker_pthread.h" 35 | 36 | typedef struct _PrivInfo 37 | { 38 | pthread_mutex_t mutex; 39 | }PrivInfo; 40 | 41 | static Ret locker_pthread_lock(Locker* thiz) 42 | { 43 | PrivInfo* priv = (PrivInfo*)thiz->priv; 44 | 45 | int ret = pthread_mutex_lock(&priv->mutex); 46 | 47 | return ret == 0 ? RET_OK : RET_FAIL; 48 | } 49 | 50 | static Ret locker_pthread_unlock(Locker* thiz) 51 | { 52 | PrivInfo* priv = (PrivInfo*)thiz->priv; 53 | 54 | int ret = pthread_mutex_unlock(&priv->mutex); 55 | 56 | return ret == 0 ? RET_OK : RET_FAIL; 57 | } 58 | 59 | static void locker_pthread_destroy(Locker* thiz) 60 | { 61 | PrivInfo* priv = (PrivInfo*)thiz->priv; 62 | 63 | int ret = pthread_mutex_destroy(&priv->mutex); 64 | 65 | SAFE_FREE(thiz); 66 | 67 | return; 68 | } 69 | 70 | Locker* locker_pthread_create(void) 71 | { 72 | Locker* thiz = (Locker*)malloc(sizeof(Locker) + sizeof(PrivInfo)); 73 | 74 | if(thiz != NULL) 75 | { 76 | PrivInfo* priv = (PrivInfo*)thiz->priv; 77 | 78 | thiz->lock = locker_pthread_lock; 79 | thiz->unlock = locker_pthread_unlock; 80 | thiz->destroy = locker_pthread_destroy; 81 | 82 | pthread_mutex_init(&(priv->mutex), NULL); 83 | } 84 | 85 | return thiz; 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /4/2/locker/locker_pthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_pthread.h 3 | * Author: Li XianJing 4 | * Brief: pthread mutex implemented locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | #include "locker.h" 32 | 33 | #ifndef LOCKER_PTHREAD_H 34 | #define LOCKER_PTHREAD_H 35 | 36 | DECLS_BEGIN 37 | 38 | Locker* locker_pthread_create(void); 39 | 40 | DECLS_END 41 | 42 | #endif/*LOCKER_PTHREAD_H*/ 43 | 44 | -------------------------------------------------------------------------------- /4/2/locker/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | 34 | #ifndef TYPEDEF_H 35 | #define TYPEDEF_H 36 | 37 | typedef enum _Ret 38 | { 39 | RET_OK, 40 | RET_OOM, 41 | RET_STOP, 42 | RET_INVALID_PARAMS, 43 | RET_FAIL 44 | }Ret; 45 | 46 | #ifdef __cplusplus 47 | #define DECLS_BEGIN extern "C" { 48 | #define DECLS_END } 49 | #else 50 | #define DECLS_BEGIN 51 | #define DECLS_END 52 | #endif/*__cplusplus*/ 53 | 54 | #define return_if_fail(p) if(!(p)) \ 55 | {printf("%s:%d Warning: "#p" failed.\n", \ 56 | __func__, __LINE__); return;} 57 | #define return_val_if_fail(p, ret) if(!(p)) \ 58 | {printf("%s:%d Warning: "#p" failed.\n",\ 59 | __func__, __LINE__); return (ret);} 60 | 61 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 62 | 63 | #endif/*TYPEDEF_H*/ 64 | 65 | -------------------------------------------------------------------------------- /4/3/locker_nest/Makefile: -------------------------------------------------------------------------------- 1 | CFILES=dlist.c locker_pthread.c locker_nest.c 2 | all: 3 | gcc -g -shared -lpthread $(CFILES) -o libdlist.so 4 | gcc -g -DDLIST_TEST -lpthread $(CFILES) -o dlist_test 5 | 6 | clean: 7 | rm -f *test *.exe *.so 8 | -------------------------------------------------------------------------------- /4/3/locker_nest/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | * 2008-12-10 Li XianJing add lock support. 31 | */ 32 | 33 | #include 34 | #include "locker.h" 35 | 36 | #ifndef DLIST_H 37 | #define DLIST_H 38 | 39 | DECLS_BEGIN 40 | 41 | struct _DList; 42 | typedef struct _DList DList; 43 | 44 | typedef void (*DListDataDestroyFunc)(void* ctx, void* data); 45 | typedef int (*DListDataCompareFunc)(void* ctx, void* data); 46 | typedef Ret (*DListDataVisitFunc)(void* ctx, void* data); 47 | 48 | DList* dlist_create(DListDataDestroyFunc data_destroy, void* ctx, Locker* locker); 49 | 50 | Ret dlist_insert(DList* thiz, size_t index, void* data); 51 | Ret dlist_prepend(DList* thiz, void* data); 52 | Ret dlist_append(DList* thiz, void* data); 53 | Ret dlist_delete(DList* thiz, size_t index); 54 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 55 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 56 | size_t dlist_length(DList* thiz); 57 | int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx); 58 | Ret dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx); 59 | 60 | void dlist_destroy(DList* thiz); 61 | 62 | DECLS_END 63 | 64 | #endif/*DLIST*/ 65 | 66 | -------------------------------------------------------------------------------- /4/3/locker_nest/locker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker.h 3 | * Author: Li XianJing 4 | * Brief: lock interface. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "typedef.h" 33 | 34 | #ifndef LOCKER_H 35 | #define LOCKER_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Locker; 40 | typedef struct _Locker Locker; 41 | 42 | typedef Ret (*LockerLockFunc)(Locker* thiz); 43 | typedef Ret (*LockerUnlockFunc)(Locker* thiz); 44 | typedef void (*LockerDestroyFunc)(Locker* thiz); 45 | 46 | struct _Locker 47 | { 48 | LockerLockFunc lock; 49 | LockerUnlockFunc unlock; 50 | LockerDestroyFunc destroy; 51 | 52 | char priv[0]; 53 | }; 54 | 55 | static inline Ret locker_lock(Locker* thiz) 56 | { 57 | return_val_if_fail(thiz != NULL && thiz->lock != NULL, RET_INVALID_PARAMS); 58 | 59 | return thiz->lock(thiz); 60 | } 61 | 62 | static inline Ret locker_unlock(Locker* thiz) 63 | { 64 | return_val_if_fail(thiz != NULL && thiz->unlock != NULL, RET_INVALID_PARAMS); 65 | 66 | return thiz->unlock(thiz); 67 | } 68 | 69 | static inline void locker_destroy(Locker* thiz) 70 | { 71 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 72 | 73 | thiz->destroy(thiz); 74 | 75 | return; 76 | } 77 | 78 | DECLS_END 79 | 80 | #endif/*LOCKER_H*/ 81 | 82 | -------------------------------------------------------------------------------- /4/3/locker_nest/locker_nest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_nest.h 3 | * Author: Li XianJing 4 | * Brief: nest locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-14 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "locker.h" 33 | 34 | #ifndef LOCKER_NEST_H 35 | #define LOCKER_NEST_H 36 | 37 | DECLS_BEGIN 38 | 39 | typedef int (*TaskSelfFunc)(void); 40 | Locker* locker_nest_create(Locker* real_locker, TaskSelfFunc task_self); 41 | 42 | DECLS_END 43 | 44 | #endif/*LOCKER_NEST_H*/ 45 | 46 | -------------------------------------------------------------------------------- /4/3/locker_nest/locker_pthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_pthread.c 3 | * Author: Li XianJing 4 | * Brief: pthread mutex implemented locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include "locker_pthread.h" 35 | 36 | typedef struct _PrivInfo 37 | { 38 | pthread_mutex_t mutex; 39 | }PrivInfo; 40 | 41 | static Ret locker_pthread_lock(Locker* thiz) 42 | { 43 | PrivInfo* priv = (PrivInfo*)thiz->priv; 44 | 45 | int ret = pthread_mutex_lock(&priv->mutex); 46 | 47 | return ret == 0 ? RET_OK : RET_FAIL; 48 | } 49 | 50 | static Ret locker_pthread_unlock(Locker* thiz) 51 | { 52 | PrivInfo* priv = (PrivInfo*)thiz->priv; 53 | 54 | int ret = pthread_mutex_unlock(&priv->mutex); 55 | 56 | return ret == 0 ? RET_OK : RET_FAIL; 57 | } 58 | 59 | static void locker_pthread_destroy(Locker* thiz) 60 | { 61 | PrivInfo* priv = (PrivInfo*)thiz->priv; 62 | 63 | int ret = pthread_mutex_destroy(&priv->mutex); 64 | 65 | SAFE_FREE(thiz); 66 | 67 | return; 68 | } 69 | 70 | Locker* locker_pthread_create(void) 71 | { 72 | Locker* thiz = (Locker*)malloc(sizeof(Locker) + sizeof(PrivInfo)); 73 | 74 | if(thiz != NULL) 75 | { 76 | PrivInfo* priv = (PrivInfo*)thiz->priv; 77 | 78 | thiz->lock = locker_pthread_lock; 79 | thiz->unlock = locker_pthread_unlock; 80 | thiz->destroy = locker_pthread_destroy; 81 | 82 | pthread_mutex_init(&(priv->mutex), NULL); 83 | } 84 | 85 | return thiz; 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /4/3/locker_nest/locker_pthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_pthread.h 3 | * Author: Li XianJing 4 | * Brief: pthread mutex implemented locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | #include "locker.h" 32 | 33 | #ifndef LOCKER_PTHREAD_H 34 | #define LOCKER_PTHREAD_H 35 | 36 | DECLS_BEGIN 37 | 38 | Locker* locker_pthread_create(void); 39 | 40 | DECLS_END 41 | 42 | #endif/*LOCKER_PTHREAD_H*/ 43 | 44 | -------------------------------------------------------------------------------- /4/3/locker_nest/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | #ifdef __cplusplus 49 | #define DECLS_BEGIN extern "C" { 50 | #define DECLS_END } 51 | #else 52 | #define DECLS_BEGIN 53 | #define DECLS_END 54 | #endif/*__cplusplus*/ 55 | 56 | #define return_if_fail(p) if(!(p)) \ 57 | {printf("%s:%d Warning: "#p" failed.\n", \ 58 | __func__, __LINE__); return;} 59 | #define return_val_if_fail(p, ret) if(!(p)) \ 60 | {printf("%s:%d Warning: "#p" failed.\n",\ 61 | __func__, __LINE__); return (ret);} 62 | 63 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 64 | 65 | #endif/*TYPEDEF_H*/ 66 | 67 | -------------------------------------------------------------------------------- /4/4/rw_locker/Makefile: -------------------------------------------------------------------------------- 1 | CFILES=dlist.c locker_pthread.c locker_nest.c rw_locker.c 2 | all: 3 | gcc -g -shared -lpthread $(CFILES) -o libdlist.so 4 | gcc -g -DDLIST_TEST -lpthread $(CFILES) -o dlist_test 5 | 6 | clean: 7 | rm -f *test *.exe *.so 8 | -------------------------------------------------------------------------------- /4/4/rw_locker/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | * 2008-12-10 Li XianJing add lock support. 31 | */ 32 | 33 | #include 34 | #include "rw_locker.h" 35 | 36 | #ifndef DLIST_H 37 | #define DLIST_H 38 | 39 | DECLS_BEGIN 40 | 41 | struct _DList; 42 | typedef struct _DList DList; 43 | 44 | typedef void (*DListDataDestroyFunc)(void* ctx, void* data); 45 | typedef int (*DListDataCompareFunc)(void* ctx, void* data); 46 | typedef Ret (*DListDataVisitFunc)(void* ctx, void* data); 47 | 48 | DList* dlist_create(DListDataDestroyFunc data_destroy, void* ctx, RwLocker* rw_locker); 49 | 50 | Ret dlist_insert(DList* thiz, size_t index, void* data); 51 | Ret dlist_prepend(DList* thiz, void* data); 52 | Ret dlist_append(DList* thiz, void* data); 53 | Ret dlist_delete(DList* thiz, size_t index); 54 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 55 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 56 | size_t dlist_length(DList* thiz); 57 | int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx); 58 | Ret dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx); 59 | 60 | void dlist_destroy(DList* thiz); 61 | 62 | DECLS_END 63 | 64 | #endif/*DLIST*/ 65 | 66 | -------------------------------------------------------------------------------- /4/4/rw_locker/locker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker.h 3 | * Author: Li XianJing 4 | * Brief: lock interface. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "typedef.h" 33 | 34 | #ifndef LOCKER_H 35 | #define LOCKER_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Locker; 40 | typedef struct _Locker Locker; 41 | 42 | typedef Ret (*LockerLockFunc)(Locker* thiz); 43 | typedef Ret (*LockerUnlockFunc)(Locker* thiz); 44 | typedef void (*LockerDestroyFunc)(Locker* thiz); 45 | 46 | struct _Locker 47 | { 48 | LockerLockFunc lock; 49 | LockerUnlockFunc unlock; 50 | LockerDestroyFunc destroy; 51 | 52 | char priv[0]; 53 | }; 54 | 55 | static inline Ret locker_lock(Locker* thiz) 56 | { 57 | return_val_if_fail(thiz != NULL && thiz->lock != NULL, RET_INVALID_PARAMS); 58 | 59 | return thiz->lock(thiz); 60 | } 61 | 62 | static inline Ret locker_unlock(Locker* thiz) 63 | { 64 | return_val_if_fail(thiz != NULL && thiz->unlock != NULL, RET_INVALID_PARAMS); 65 | 66 | return thiz->unlock(thiz); 67 | } 68 | 69 | static inline void locker_destroy(Locker* thiz) 70 | { 71 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 72 | 73 | thiz->destroy(thiz); 74 | 75 | return; 76 | } 77 | 78 | DECLS_END 79 | 80 | #endif/*LOCKER_H*/ 81 | 82 | -------------------------------------------------------------------------------- /4/4/rw_locker/locker_nest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_nest.h 3 | * Author: Li XianJing 4 | * Brief: nest locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-14 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "locker.h" 33 | 34 | #ifndef LOCKER_NEST_H 35 | #define LOCKER_NEST_H 36 | 37 | DECLS_BEGIN 38 | 39 | typedef int (*TaskSelfFunc)(void); 40 | Locker* locker_nest_create(Locker* real_locker, TaskSelfFunc task_self); 41 | 42 | DECLS_END 43 | 44 | #endif/*LOCKER_NEST_H*/ 45 | 46 | -------------------------------------------------------------------------------- /4/4/rw_locker/locker_pthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_pthread.c 3 | * Author: Li XianJing 4 | * Brief: pthread mutex implemented locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include "locker_pthread.h" 35 | 36 | typedef struct _PrivInfo 37 | { 38 | pthread_mutex_t mutex; 39 | }PrivInfo; 40 | 41 | static Ret locker_pthread_lock(Locker* thiz) 42 | { 43 | PrivInfo* priv = (PrivInfo*)thiz->priv; 44 | 45 | int ret = pthread_mutex_lock(&priv->mutex); 46 | 47 | return ret == 0 ? RET_OK : RET_FAIL; 48 | } 49 | 50 | static Ret locker_pthread_unlock(Locker* thiz) 51 | { 52 | PrivInfo* priv = (PrivInfo*)thiz->priv; 53 | 54 | int ret = pthread_mutex_unlock(&priv->mutex); 55 | 56 | return ret == 0 ? RET_OK : RET_FAIL; 57 | } 58 | 59 | static void locker_pthread_destroy(Locker* thiz) 60 | { 61 | PrivInfo* priv = (PrivInfo*)thiz->priv; 62 | 63 | int ret = pthread_mutex_destroy(&priv->mutex); 64 | 65 | SAFE_FREE(thiz); 66 | 67 | return; 68 | } 69 | 70 | Locker* locker_pthread_create(void) 71 | { 72 | Locker* thiz = (Locker*)malloc(sizeof(Locker) + sizeof(PrivInfo)); 73 | 74 | if(thiz != NULL) 75 | { 76 | PrivInfo* priv = (PrivInfo*)thiz->priv; 77 | 78 | thiz->lock = locker_pthread_lock; 79 | thiz->unlock = locker_pthread_unlock; 80 | thiz->destroy = locker_pthread_destroy; 81 | 82 | pthread_mutex_init(&(priv->mutex), NULL); 83 | } 84 | 85 | return thiz; 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /4/4/rw_locker/locker_pthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker_pthread.h 3 | * Author: Li XianJing 4 | * Brief: pthread mutex implemented locker. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | #include "locker.h" 32 | 33 | #ifndef LOCKER_PTHREAD_H 34 | #define LOCKER_PTHREAD_H 35 | 36 | DECLS_BEGIN 37 | 38 | Locker* locker_pthread_create(void); 39 | 40 | DECLS_END 41 | 42 | #endif/*LOCKER_PTHREAD_H*/ 43 | 44 | -------------------------------------------------------------------------------- /4/4/rw_locker/rw_locker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: rw_locker.h 3 | * Author: Li XianJing 4 | * Brief: read write locker 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-14 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "locker.h" 33 | 34 | #ifndef RW_LOCKR_H 35 | #define RW_LOCKER_H 36 | 37 | struct _RwLocker; 38 | typedef struct _RwLocker RwLocker; 39 | 40 | RwLocker* rw_locker_create(Locker* rw_locker, Locker* rd_locker); 41 | 42 | Ret rw_locker_wrlock(RwLocker* thiz); 43 | Ret rw_locker_rdlock(RwLocker* thiz); 44 | Ret rw_locker_unlock(RwLocker* thiz); 45 | 46 | void rw_locker_destroy(RwLocker* thiz); 47 | 48 | #endif/*RW_LOCKER_H*/ 49 | 50 | -------------------------------------------------------------------------------- /4/4/rw_locker/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | #ifdef __cplusplus 49 | #define DECLS_BEGIN extern "C" { 50 | #define DECLS_END } 51 | #else 52 | #define DECLS_BEGIN 53 | #define DECLS_END 54 | #endif/*__cplusplus*/ 55 | 56 | #define return_if_fail(p) if(!(p)) \ 57 | {printf("%s:%d Warning: "#p" failed.\n", \ 58 | __func__, __LINE__); return;} 59 | #define return_val_if_fail(p, ret) if(!(p)) \ 60 | {printf("%s:%d Warning: "#p" failed.\n",\ 61 | __func__, __LINE__); return (ret);} 62 | 63 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 64 | 65 | #endif/*TYPEDEF_H*/ 66 | 67 | -------------------------------------------------------------------------------- /4/5/lock_free/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g atomic.c -lpthread -DATOMIC_TEST -o atomic_test 3 | gcc -g fifo_ring.c -lpthread -DFIFO_RING_TEST -o fifo_ring_test 4 | gcc -g swmr_dlist.c dlist.c -lpthread -DSWMR_DLIST_TEST -o swmr_dlist_test 5 | clean: 6 | rm -f *test 7 | 8 | -------------------------------------------------------------------------------- /4/5/lock_free/atomic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: atomic.c 3 | * Author: Li XianJing 4 | * Brief: atomic test 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-27 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include "iatomic.h" 36 | 37 | #ifdef ATOMIC_TEST 38 | 39 | atomic_t g_count = {0}; 40 | 41 | static void* thread_inc(void* param) 42 | { 43 | int i = 0; 44 | for(i = 0; i < 1000000; i++) 45 | { 46 | atomic_inc(&g_count); 47 | } 48 | 49 | return NULL; 50 | } 51 | 52 | static void* thread_dec(void* param) 53 | { 54 | int i = 0; 55 | for(i = 0; i < 1000000; i++) 56 | { 57 | atomic_dec(&g_count); 58 | } 59 | 60 | return NULL; 61 | } 62 | 63 | int main(int argc, char* argv[]) 64 | { 65 | pthread_t inc_tid = 0; 66 | pthread_t dec_tid = 0; 67 | 68 | pthread_create(&inc_tid, NULL, thread_inc, NULL); 69 | pthread_create(&dec_tid, NULL, thread_dec, NULL); 70 | 71 | pthread_join(inc_tid, NULL); 72 | pthread_join(dec_tid, NULL); 73 | 74 | printf("count=%d\n", g_count.counter); 75 | 76 | return 0; 77 | } 78 | #endif/*ATOMIC_TEST*/ 79 | -------------------------------------------------------------------------------- /4/5/lock_free/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | * 2008-12-10 Li XianJing add lock support. 31 | */ 32 | 33 | #include 34 | #include "locker.h" 35 | 36 | #ifndef DLIST_H 37 | #define DLIST_H 38 | 39 | DECLS_BEGIN 40 | 41 | struct _DList; 42 | typedef struct _DList DList; 43 | 44 | typedef void (*DListDataDestroyFunc)(void* ctx, void* data); 45 | typedef int (*DListDataCompareFunc)(void* ctx, void* data); 46 | typedef Ret (*DListDataVisitFunc)(void* ctx, void* data); 47 | 48 | DList* dlist_create(DListDataDestroyFunc data_destroy, void* ctx, Locker* locker); 49 | 50 | Ret dlist_insert(DList* thiz, size_t index, void* data); 51 | Ret dlist_prepend(DList* thiz, void* data); 52 | Ret dlist_append(DList* thiz, void* data); 53 | Ret dlist_delete(DList* thiz, size_t index); 54 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 55 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 56 | size_t dlist_length(DList* thiz); 57 | int dlist_find(DList* thiz, DListDataCompareFunc cmp, void* ctx); 58 | Ret dlist_foreach(DList* thiz, DListDataVisitFunc visit, void* ctx); 59 | 60 | void dlist_destroy(DList* thiz); 61 | 62 | DECLS_END 63 | 64 | #endif/*DLIST*/ 65 | 66 | -------------------------------------------------------------------------------- /4/5/lock_free/locker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: locker.h 3 | * Author: Li XianJing 4 | * Brief: lock interface. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include "typedef.h" 33 | 34 | #ifndef LOCKER_H 35 | #define LOCKER_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Locker; 40 | typedef struct _Locker Locker; 41 | 42 | typedef Ret (*LockerLockFunc)(Locker* thiz); 43 | typedef Ret (*LockerUnlockFunc)(Locker* thiz); 44 | typedef void (*LockerDestroyFunc)(Locker* thiz); 45 | 46 | struct _Locker 47 | { 48 | LockerLockFunc lock; 49 | LockerUnlockFunc unlock; 50 | LockerDestroyFunc destroy; 51 | 52 | char priv[0]; 53 | }; 54 | 55 | static inline Ret locker_lock(Locker* thiz) 56 | { 57 | return_val_if_fail(thiz != NULL && thiz->lock != NULL, RET_INVALID_PARAMS); 58 | 59 | return thiz->lock(thiz); 60 | } 61 | 62 | static inline Ret locker_unlock(Locker* thiz) 63 | { 64 | return_val_if_fail(thiz != NULL && thiz->unlock != NULL, RET_INVALID_PARAMS); 65 | 66 | return thiz->unlock(thiz); 67 | } 68 | 69 | static inline void locker_destroy(Locker* thiz) 70 | { 71 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 72 | 73 | thiz->destroy(thiz); 74 | 75 | return; 76 | } 77 | 78 | DECLS_END 79 | 80 | #endif/*LOCKER_H*/ 81 | 82 | -------------------------------------------------------------------------------- /4/5/lock_free/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | #ifdef __cplusplus 49 | #define DECLS_BEGIN extern "C" { 50 | #define DECLS_END } 51 | #else 52 | #define DECLS_BEGIN 53 | #define DECLS_END 54 | #endif/*__cplusplus*/ 55 | 56 | #define return_if_fail(p) if(!(p)) \ 57 | {printf("%s:%d Warning: "#p" failed.\n", \ 58 | __func__, __LINE__); return;} 59 | #define return_val_if_fail(p, ret) if(!(p)) \ 60 | {printf("%s:%d Warning: "#p" failed.\n",\ 61 | __func__, __LINE__); return (ret);} 62 | 63 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 64 | 65 | #endif/*TYPEDEF_H*/ 66 | 67 | -------------------------------------------------------------------------------- /5/1/queue/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -DQUEUE_TEST queue.c dlist.c -o queue_test 3 | 4 | clean: 5 | rm -f *test *.exe *.so 6 | -------------------------------------------------------------------------------- /5/1/queue/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /5/1/queue/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: queue.h 3 | * Author: Li XianJing 4 | * Brief: queue header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-14 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef QUEUE_H 35 | #define QUEUE_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Queue; 40 | typedef struct _Queue Queue; 41 | 42 | Queue* queue_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret queue_head(Queue* thiz, void** data); 45 | Ret queue_push(Queue* thiz, void* data); 46 | Ret queue_pop(Queue* thiz); 47 | size_t queue_length(Queue* thiz); 48 | Ret queue_foreach(Queue* thiz, DataVisitFunc visit, void* ctx); 49 | 50 | void queue_destroy(Queue* thiz); 51 | 52 | DECLS_END 53 | 54 | #endif/*QUEUE_H*/ 55 | 56 | -------------------------------------------------------------------------------- /5/1/queue/test_helper.c: -------------------------------------------------------------------------------- 1 | 2 | static int cmp_int(void* ctx, void* data) 3 | { 4 | return (int)ctx - (int)data; 5 | } 6 | 7 | static Ret print_int(void* ctx, void* data) 8 | { 9 | printf("%d ", (int)data); 10 | 11 | return RET_OK; 12 | } 13 | -------------------------------------------------------------------------------- /5/1/queue/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /5/2/stack/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -DSTACK_TEST stack.c dlist.c -o stack_test 3 | 4 | clean: 5 | rm -f *test *.exe *.so 6 | -------------------------------------------------------------------------------- /5/2/stack/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /5/2/stack/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: stack.c 3 | * Author: Li XianJing 4 | * Brief: stack header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-14 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef STACK_H 35 | #define STACK_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Stack; 40 | typedef struct _Stack Stack; 41 | 42 | Stack* stack_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret stack_top(Stack* thiz, void** data); 45 | Ret stack_push(Stack* thiz, void* data); 46 | Ret stack_pop(Stack* thiz); 47 | size_t stack_length(Stack* thiz); 48 | Ret stack_foreach(Stack* thiz, DataVisitFunc visit, void* ctx); 49 | 50 | void stack_destroy(Stack* thiz); 51 | 52 | DECLS_END 53 | 54 | #endif/*STACK_H*/ 55 | 56 | -------------------------------------------------------------------------------- /5/2/stack/test_helper.c: -------------------------------------------------------------------------------- 1 | 2 | static int cmp_int(void* ctx, void* data) 3 | { 4 | return (int)ctx - (int)data; 5 | } 6 | 7 | static Ret print_int(void* ctx, void* data) 8 | { 9 | printf("%d ", (int)data); 10 | 11 | return RET_OK; 12 | } 13 | -------------------------------------------------------------------------------- /5/2/stack/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /5/3/hash_table/Makefile: -------------------------------------------------------------------------------- 1 | CFILES=dlist.c 2 | all: 3 | gcc -g -shared -lpthread $(CFILES) -o libdlist.so 4 | gcc -g -DDLIST_TEST -lpthread $(CFILES) -o dlist_test 5 | gcc -g -DHASH_TABLE_TEST hash_table.c dlist.c -o hash_table_test 6 | 7 | clean: 8 | rm -f *test *.exe *.so 9 | -------------------------------------------------------------------------------- /5/3/hash_table/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /5/3/hash_table/hash_table.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: hash_table.h 3 | * Author: Li XianJing 4 | * Brief: hash table header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-13 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef HASH_TABLE_H 35 | #define HASH_TABLE_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _HashTable; 40 | typedef struct _HashTable HashTable; 41 | 42 | HashTable* hash_table_create(DataDestroyFunc data_destroy, void* ctx, DataHashFunc hash, int slot_nr); 43 | 44 | size_t hash_table_length(HashTable* thiz); 45 | Ret hash_table_insert(HashTable* thiz, void* data); 46 | Ret hash_table_delete(HashTable* thiz, DataCompareFunc cmp, void* data); 47 | Ret hash_table_find(HashTable* thiz, DataCompareFunc cmp, void* data, void** ret_data); 48 | Ret hash_table_foreach(HashTable* thiz, DataVisitFunc visit, void* ctx); 49 | 50 | void hash_table_destroy(HashTable* thiz); 51 | 52 | DECLS_END 53 | 54 | #endif/*HASH_TABLE*/ 55 | 56 | -------------------------------------------------------------------------------- /5/3/hash_table/test_helper.c: -------------------------------------------------------------------------------- 1 | static int hash_int(void* data) 2 | { 3 | return (int)data; 4 | } 5 | 6 | static int cmp_int(void* ctx, void* data) 7 | { 8 | return (int)ctx - (int)data; 9 | } 10 | 11 | static Ret print_int(void* ctx, void* data) 12 | { 13 | printf("%d ", (int)data); 14 | 15 | return RET_OK; 16 | } 17 | -------------------------------------------------------------------------------- /5/3/hash_table/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /6/1/linear_container/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -Wall -g -DDARRAY_TEST darray.c -o darray_test 3 | gcc -Wall -g -DDLIST_TEST dlist.c -o dlist_test 4 | gcc -Wall -g -shared darray.c dlist.c linear_container_darray.c linear_container_dlist.c queue.c -o libcontainer.so 5 | gcc -Wall -g linear_container_test.c -L./ -lcontainer -o container_test 6 | gcc -Wall -g queue.c linear_container_dlist.c dlist.c -DQUEUE_TEST -o queue_test 7 | clean: 8 | rm *test *.so 9 | -------------------------------------------------------------------------------- /6/1/linear_container/darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: darray.h 3 | * Author: Li XianJing 4 | * Brief: dynamic array implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-01-02 Li XianJing created 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef DARRAY_H 35 | #define DARRAY_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _DArray; 40 | typedef struct _DArray DArray; 41 | 42 | DArray* darray_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret darray_insert(DArray* thiz, size_t index, void* data); 45 | Ret darray_prepend(DArray* thiz, void* data); 46 | Ret darray_append(DArray* thiz, void* data); 47 | Ret darray_delete(DArray* thiz, size_t index); 48 | Ret darray_get_by_index(DArray* thiz, size_t index, void** data); 49 | Ret darray_set_by_index(DArray* thiz, size_t index, void* data); 50 | size_t darray_length(DArray* thiz); 51 | int darray_find(DArray* thiz, DataCompareFunc cmp, void* ctx); 52 | Ret darray_foreach(DArray* thiz, DataVisitFunc visit, void* ctx); 53 | Ret darray_sort(DArray* thiz, SortFunc sort, DataCompareFunc cmp); 54 | 55 | void darray_destroy(DArray* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DARRAY_H*/ 60 | 61 | -------------------------------------------------------------------------------- /6/1/linear_container/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /6/1/linear_container/linear_container_darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: linear_container_darray.h 3 | * Author: Li XianJing 4 | * Brief: darray implemented linear container. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-03-01 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef LINEAR_CONTAINER_DARRAY_H 33 | #define LINEAR_CONTAINER_DARRAY_H 34 | 35 | #include "linear_container.h" 36 | 37 | DECLS_BEGIN 38 | 39 | LinearContainer* linear_container_darray_create(DataDestroyFunc data_destroy, void* ctx); 40 | 41 | DECLS_END 42 | 43 | #endif/*LINEAR_CONTAINER_DARRAY_H*/ 44 | 45 | -------------------------------------------------------------------------------- /6/1/linear_container/linear_container_dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: linear_container_dlist.h 3 | * Author: Li XianJing 4 | * Brief: dlist implemented linear container. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-03-01 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef LINEAR_CONTAINER_DLIST_H 33 | #define LINEAR_CONTAINER_DLIST_H 34 | 35 | #include "linear_container.h" 36 | 37 | DECLS_BEGIN 38 | 39 | LinearContainer* linear_container_dlist_create(DataDestroyFunc data_destroy, void* ctx); 40 | 41 | DECLS_END 42 | 43 | #endif/*LINEAR_CONTAINER_DLIST_H*/ 44 | 45 | -------------------------------------------------------------------------------- /6/1/linear_container/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: queue.h 3 | * Author: Li XianJing 4 | * Brief: queue header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-14 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | #include "linear_container.h" 34 | 35 | #ifndef QUEUE_H 36 | #define QUEUE_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _Queue; 41 | typedef struct _Queue Queue; 42 | 43 | Queue* queue_create(LinearContainer* container); 44 | 45 | Ret queue_head(Queue* thiz, void** data); 46 | Ret queue_push(Queue* thiz, void* data); 47 | Ret queue_pop(Queue* thiz); 48 | size_t queue_length(Queue* thiz); 49 | Ret queue_foreach(Queue* thiz, DataVisitFunc visit, void* ctx); 50 | 51 | void queue_destroy(Queue* thiz); 52 | 53 | DECLS_END 54 | 55 | #endif/*QUEUE_H*/ 56 | 57 | -------------------------------------------------------------------------------- /6/1/linear_container/test_helper.c: -------------------------------------------------------------------------------- 1 | static int hash_int(void* data) 2 | { 3 | return (int)data; 4 | } 5 | 6 | static int cmp_int(void* ctx, void* data) 7 | { 8 | return (int)ctx - (int)data; 9 | } 10 | 11 | static Ret print_int(void* ctx, void* data) 12 | { 13 | printf("%d ", (int)data); 14 | 15 | return RET_OK; 16 | } 17 | 18 | static Ret check_and_dec_int(void* ctx, void* data) 19 | { 20 | int* expected =(int*)ctx; 21 | assert(*expected == (int)data); 22 | 23 | (*expected)--; 24 | 25 | return RET_OK; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /6/1/linear_container/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /6/2/iterator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -Wall -g -DDARRAY_TEST darray.c -o darray_test 3 | gcc -Wall -g -DDLIST_TEST dlist.c -o dlist_test 4 | gcc -Wall -g -shared darray.c dlist.c linear_container_darray.c linear_container_dlist.c -o libcontainer.so 5 | gcc -Wall -g linear_container_test.c -L./ -lcontainer -o container_test 6 | gcc -Wall -g invert_ng.c -DINVERT_TEST -L./ -lcontainer -o invert_ng_test 7 | gcc -Wall -g invert.c -DINVERT_TEST -L./ -lcontainer -o invert_test 8 | clean: 9 | rm *test *.so 10 | -------------------------------------------------------------------------------- /6/2/iterator/darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: darray.h 3 | * Author: Li XianJing 4 | * Brief: dynamic array implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-01-02 Li XianJing created 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef DARRAY_H 35 | #define DARRAY_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _DArray; 40 | typedef struct _DArray DArray; 41 | 42 | DArray* darray_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret darray_insert(DArray* thiz, size_t index, void* data); 45 | Ret darray_prepend(DArray* thiz, void* data); 46 | Ret darray_append(DArray* thiz, void* data); 47 | Ret darray_delete(DArray* thiz, size_t index); 48 | Ret darray_get_by_index(DArray* thiz, size_t index, void** data); 49 | Ret darray_set_by_index(DArray* thiz, size_t index, void* data); 50 | size_t darray_length(DArray* thiz); 51 | int darray_find(DArray* thiz, DataCompareFunc cmp, void* ctx); 52 | Ret darray_foreach(DArray* thiz, DataVisitFunc visit, void* ctx); 53 | Ret darray_sort(DArray* thiz, SortFunc sort, DataCompareFunc cmp); 54 | 55 | void darray_destroy(DArray* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DARRAY_H*/ 60 | 61 | -------------------------------------------------------------------------------- /6/2/iterator/darray_iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DARRAY_ITERATOR_H 2 | #define DARRAY_ITERATOR_H 3 | 4 | #include "darray.h" 5 | #include "iterator.h" 6 | 7 | DECLS_BEGIN 8 | 9 | Iterator* darray_iterator_create(DArray* darray); 10 | 11 | DECLS_END 12 | 13 | #endif/*DARRAY_ITERATOR_H*/ 14 | 15 | -------------------------------------------------------------------------------- /6/2/iterator/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /6/2/iterator/dlist_iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DLIST_ITERATOR_H 2 | #define DLIST_ITERATOR_H 3 | 4 | #include "dlist.h" 5 | #include "iterator.h" 6 | 7 | DECLS_BEGIN 8 | 9 | Iterator* dlist_iterator_create(DList* dlist); 10 | 11 | DECLS_END 12 | 13 | #endif/*DLIST_ITERATOR_H*/ 14 | 15 | -------------------------------------------------------------------------------- /6/2/iterator/invert.c: -------------------------------------------------------------------------------- 1 | #include "iterator.h" 2 | 3 | Ret invert(Iterator* forward, Iterator* backward) 4 | { 5 | void* data1 = NULL; 6 | void* data2 = NULL; 7 | return_val_if_fail(forward != NULL && backward != NULL, RET_INVALID_PARAMS); 8 | 9 | for(; iterator_offset(forward) < iterator_offset(backward); iterator_next(forward), iterator_prev(backward)) 10 | { 11 | iterator_get(forward, &data1); 12 | iterator_get(backward, &data2); 13 | iterator_set(forward, data2); 14 | iterator_set(backward, data1); 15 | } 16 | 17 | return RET_OK; 18 | } 19 | 20 | #ifdef INVERT_TEST 21 | #include "dlist.h" 22 | #include "dlist_iterator.h" 23 | #include "test_helper.c" 24 | 25 | int main(int argc, char* argv[]) 26 | { 27 | int i = 0; 28 | int n = 101; 29 | int last = n - 1; 30 | DList* dlist = dlist_create(NULL, NULL); 31 | 32 | for(i = 0; i < n; i++) 33 | { 34 | dlist_append(dlist, (void*)i); 35 | } 36 | 37 | Iterator* forward = dlist_iterator_create(dlist); 38 | Iterator* backward = dlist_iterator_create(dlist); 39 | 40 | iterator_advance(backward, last); 41 | invert(forward, backward); 42 | dlist_foreach(dlist, check_and_dec_int, &last); 43 | iterator_destroy(forward); 44 | iterator_destroy(backward); 45 | dlist_destroy(dlist); 46 | 47 | return 0; 48 | } 49 | #endif/*INVERT_TEST*/ 50 | -------------------------------------------------------------------------------- /6/2/iterator/invert_ng.c: -------------------------------------------------------------------------------- 1 | #include "linear_container.h" 2 | 3 | Ret invert(LinearContainer* linear_container) 4 | { 5 | int i = 0; 6 | int j = 0; 7 | void* data1 = NULL; 8 | void* data2 = NULL; 9 | 10 | return_val_if_fail(linear_container != NULL, RET_INVALID_PARAMS); 11 | 12 | j = linear_container_length(linear_container) - 1; 13 | for(; i < j; i++, j--) 14 | { 15 | linear_container_get_by_index(linear_container, i, &data1); 16 | linear_container_get_by_index(linear_container, j, &data2); 17 | linear_container_set_by_index(linear_container, i, data2); 18 | linear_container_set_by_index(linear_container, j, data1); 19 | } 20 | 21 | return RET_OK; 22 | } 23 | 24 | #ifdef INVERT_TEST 25 | #include "test_helper.c" 26 | #include "linear_container_dlist.h" 27 | 28 | int main(int argc, char* argv[]) 29 | { 30 | int i = 0; 31 | int n = 101; 32 | 33 | LinearContainer* linear_container = linear_container_dlist_create(NULL, NULL); 34 | for(i = 0; i < n; i++) 35 | { 36 | linear_container_append(linear_container, (void*)i); 37 | } 38 | invert(linear_container); 39 | i--; 40 | linear_container_foreach(linear_container, check_and_dec_int, &i); 41 | linear_container_destroy(linear_container); 42 | 43 | return 0; 44 | } 45 | #endif/*INVERT_TEST*/ 46 | -------------------------------------------------------------------------------- /6/2/iterator/iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | #include "typedef.h" 5 | 6 | DECLS_BEGIN 7 | 8 | struct _Iterator; 9 | typedef struct _Iterator Iterator; 10 | 11 | typedef Ret (*IteratorSetFunc)(Iterator* thiz, void* data); 12 | typedef Ret (*IteratorGetFunc)(Iterator* thiz, void** data); 13 | typedef Ret (*IteratorNextFunc)(Iterator* thiz); 14 | typedef Ret (*IteratorPrevFunc)(Iterator* thiz); 15 | typedef Ret (*IteratorAdvanceFunc)(Iterator* thiz, int offset); 16 | typedef int (*IteratorOffsetFunc)(Iterator* thiz); 17 | typedef Ret (*IteratorCloneFunc)(Iterator* thiz, Iterator** cloned); 18 | typedef void (*IteratorDestroyFunc)(Iterator* thiz); 19 | 20 | struct _Iterator 21 | { 22 | IteratorSetFunc set; 23 | IteratorGetFunc get; 24 | IteratorNextFunc next; 25 | IteratorPrevFunc prev; 26 | IteratorAdvanceFunc advance; 27 | IteratorCloneFunc clone; 28 | IteratorOffsetFunc offset; 29 | IteratorDestroyFunc destroy; 30 | 31 | char priv[0]; 32 | }; 33 | 34 | static inline Ret iterator_set(Iterator* thiz, void* data) 35 | { 36 | return_val_if_fail(thiz != NULL && thiz->set != NULL, RET_INVALID_PARAMS); 37 | 38 | return thiz->set(thiz, data); 39 | } 40 | 41 | static inline Ret iterator_get(Iterator* thiz, void** data) 42 | { 43 | return_val_if_fail(thiz != NULL && thiz->get != NULL, RET_INVALID_PARAMS); 44 | 45 | return thiz->get(thiz, data); 46 | } 47 | 48 | static inline Ret iterator_next(Iterator* thiz) 49 | { 50 | return_val_if_fail(thiz != NULL && thiz->next != NULL, RET_INVALID_PARAMS); 51 | 52 | return thiz->next(thiz); 53 | } 54 | 55 | static inline Ret iterator_prev(Iterator* thiz) 56 | { 57 | return_val_if_fail(thiz != NULL && thiz->prev != NULL, RET_INVALID_PARAMS); 58 | 59 | return thiz->prev(thiz); 60 | } 61 | 62 | static inline Ret iterator_advance(Iterator* thiz, int offset) 63 | { 64 | return_val_if_fail(thiz != NULL && thiz->advance != NULL, RET_INVALID_PARAMS); 65 | 66 | return thiz->advance(thiz, offset); 67 | } 68 | 69 | static inline int iterator_offset(Iterator* thiz) 70 | { 71 | return_val_if_fail(thiz != NULL && thiz->offset != NULL, -1); 72 | 73 | return thiz->offset(thiz); 74 | } 75 | 76 | static inline Ret iterator_clone(Iterator* thiz, Iterator** cloned) 77 | { 78 | return_val_if_fail(thiz != NULL && thiz->clone != NULL, RET_INVALID_PARAMS); 79 | 80 | return thiz->clone(thiz, cloned); 81 | } 82 | 83 | static inline void iterator_destroy(Iterator* thiz) 84 | { 85 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 86 | 87 | return thiz->destroy(thiz); 88 | } 89 | 90 | DECLS_END 91 | 92 | #endif/*ITERATOR_H*/ 93 | 94 | -------------------------------------------------------------------------------- /6/2/iterator/linear_container_darray.h: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_CONTAINER_DARRAY_H 2 | #define LINEAR_CONTAINER_DARRAY_H 3 | 4 | #include "linear_container.h" 5 | 6 | LinearContainer* linear_container_darray_create(DataDestroyFunc data_destroy, void* ctx); 7 | 8 | #endif/*LINEAR_CONTAINER_DARRAY_H*/ 9 | -------------------------------------------------------------------------------- /6/2/iterator/linear_container_dlist.h: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_CONTAINER_DLIST_H 2 | #define LINEAR_CONTAINER_DLIST_H 3 | 4 | #include "linear_container.h" 5 | 6 | LinearContainer* linear_container_dlist_create(DataDestroyFunc data_destroy, void* ctx); 7 | 8 | #endif/*LINEAR_CONTAINER_DLIST_H*/ 9 | -------------------------------------------------------------------------------- /6/2/iterator/test_helper.c: -------------------------------------------------------------------------------- 1 | static int hash_int(void* data) 2 | { 3 | return (int)data; 4 | } 5 | 6 | static int cmp_int(void* ctx, void* data) 7 | { 8 | return (int)ctx - (int)data; 9 | } 10 | 11 | static Ret print_int(void* ctx, void* data) 12 | { 13 | printf("%d ", (int)data); 14 | 15 | return RET_OK; 16 | } 17 | 18 | static Ret check_and_dec_int(void* ctx, void* data) 19 | { 20 | int* expected =(int*)ctx; 21 | assert(*expected == (int)data); 22 | 23 | (*expected)--; 24 | 25 | return RET_OK; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /6/2/iterator/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /6/3/runtime/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -Wall -g -DDARRAY_TEST darray.c -o darray_test 3 | gcc -Wall -g -DDLIST_TEST dlist.c -o dlist_test 4 | gcc -Wall -g -shared darray.c dlist.c linear_container_darray.c linear_container_dlist.c queue.c -o libcontainer.so 5 | gcc -Wall -g linear_container_test.c -L./ -lcontainer -o container_test 6 | gcc -Wall -g queue.c linear_container_dlist.c dlist.c -DQUEUE_TEST -o queue_test 7 | gcc -Wall -g module_linux.c module_test.c -ldl -o module_test 8 | gcc -Wall -g module_linux.c queue.c queue_test.c -ldl -o queue_dynamic_test 9 | gcc -g call_cos.c -ldl -o call_cos_test 10 | 11 | clean: 12 | rm *test *.so 13 | -------------------------------------------------------------------------------- /6/3/runtime/call_cos.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char **argv) 6 | { 7 | void *handle = NULL; 8 | double (*cosine)(double) = NULL; 9 | /*加载共享库*/ 10 | handle = dlopen("libm.so", RTLD_LAZY); 11 | /*通过函数名找到函数指针*/ 12 | *(void **) (&cosine) = dlsym(handle, "cos"); 13 | /*调用函数*/ 14 | printf("%f\n", (*cosine)(2.0)); 15 | /*卸载共享库*/ 16 | dlclose(handle); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /6/3/runtime/darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: darray.h 3 | * Author: Li XianJing 4 | * Brief: dynamic array implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-01-02 Li XianJing created 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef DARRAY_H 35 | #define DARRAY_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _DArray; 40 | typedef struct _DArray DArray; 41 | 42 | DArray* darray_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret darray_insert(DArray* thiz, size_t index, void* data); 45 | Ret darray_prepend(DArray* thiz, void* data); 46 | Ret darray_append(DArray* thiz, void* data); 47 | Ret darray_delete(DArray* thiz, size_t index); 48 | Ret darray_get_by_index(DArray* thiz, size_t index, void** data); 49 | Ret darray_set_by_index(DArray* thiz, size_t index, void* data); 50 | size_t darray_length(DArray* thiz); 51 | int darray_find(DArray* thiz, DataCompareFunc cmp, void* ctx); 52 | Ret darray_foreach(DArray* thiz, DataVisitFunc visit, void* ctx); 53 | Ret darray_sort(DArray* thiz, SortFunc sort, DataCompareFunc cmp); 54 | 55 | void darray_destroy(DArray* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DARRAY_H*/ 60 | 61 | -------------------------------------------------------------------------------- /6/3/runtime/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /6/3/runtime/linear_container_darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: linear_container_darray.h 3 | * Author: Li XianJing 4 | * Brief: darray implemented linear container. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-03-01 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef LINEAR_CONTAINER_DARRAY_H 33 | #define LINEAR_CONTAINER_DARRAY_H 34 | 35 | #include "linear_container.h" 36 | 37 | DECLS_BEGIN 38 | 39 | LinearContainer* linear_container_darray_create(DataDestroyFunc data_destroy, void* ctx); 40 | 41 | DECLS_END 42 | 43 | #endif/*LINEAR_CONTAINER_DARRAY_H*/ 44 | 45 | -------------------------------------------------------------------------------- /6/3/runtime/linear_container_dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: linear_container_dlist.h 3 | * Author: Li XianJing 4 | * Brief: dlist implemented linear container. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-03-01 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef LINEAR_CONTAINER_DLIST_H 33 | #define LINEAR_CONTAINER_DLIST_H 34 | 35 | #include "linear_container.h" 36 | 37 | DECLS_BEGIN 38 | 39 | LinearContainer* linear_container_dlist_create(DataDestroyFunc data_destroy, void* ctx); 40 | 41 | DECLS_END 42 | 43 | #endif/*LINEAR_CONTAINER_DLIST_H*/ 44 | 45 | -------------------------------------------------------------------------------- /6/3/runtime/module.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: module.h 3 | * Author: Li XianJing 4 | * Brief: dynamic module 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-03-16 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef MODULE_H 33 | #define MODULE_H 34 | #include "typedef.h" 35 | 36 | DECLS_BEGIN 37 | 38 | struct _Module; 39 | typedef struct _Module Module; 40 | 41 | typedef enum _ModuleFlags 42 | { 43 | MODULE_FLAGS_NONE, 44 | MODULE_FLAGS_DELAY = 1 45 | }ModuleFlags; 46 | 47 | Module* module_create(const char* file_name, ModuleFlags flags); 48 | void* module_sym(Module* thiz, const char* func_name); 49 | void module_destroy(Module* thiz); 50 | 51 | DECLS_END 52 | 53 | #endif/*MODULE_H*/ 54 | 55 | -------------------------------------------------------------------------------- /6/3/runtime/module_linux.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: module_linux.h 3 | * Author: Li XianJing 4 | * Brief: dynamic module(linux implementation) 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-03-16 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include "module.h" 34 | 35 | struct _Module 36 | { 37 | void* handle; 38 | }; 39 | 40 | Module* module_create(const char* file_name, ModuleFlags flags) 41 | { 42 | Module* thiz = NULL; 43 | return_val_if_fail(file_name != NULL, NULL); 44 | 45 | if((thiz = malloc(sizeof(Module))) != NULL) 46 | { 47 | thiz->handle = dlopen(file_name, flags & MODULE_FLAGS_DELAY ? RTLD_LAZY : RTLD_NOW); 48 | if(thiz->handle == NULL) 49 | { 50 | free(thiz); 51 | thiz = NULL; 52 | printf("%s\n", dlerror()); 53 | } 54 | } 55 | 56 | return thiz; 57 | } 58 | 59 | void* module_sym(Module* thiz, const char* func_name) 60 | { 61 | return_val_if_fail(thiz != NULL && thiz->handle != NULL && func_name != NULL, NULL); 62 | 63 | dlerror(); 64 | return dlsym(thiz->handle, func_name); 65 | } 66 | 67 | void module_destroy(Module* thiz) 68 | { 69 | if(thiz != NULL) 70 | { 71 | if(thiz->handle != NULL) 72 | { 73 | dlclose(thiz->handle); 74 | } 75 | 76 | free(thiz); 77 | } 78 | 79 | return; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /6/3/runtime/module_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "module.h" 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | if(argc != 3) 7 | { 8 | printf("%s module function\n", argv[0]); 9 | 10 | return 0; 11 | } 12 | 13 | Module* module = module_create(argv[1], 0); 14 | if(module != NULL) 15 | { 16 | void* func = module_sym(module, argv[2]); 17 | printf("func=%p\n", func); 18 | module_destroy(module); 19 | } 20 | else 21 | { 22 | printf("load %s failed\n", argv[1]); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /6/3/runtime/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: queue.h 3 | * Author: Li XianJing 4 | * Brief: queue header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-14 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | #include "linear_container.h" 34 | 35 | #ifndef QUEUE_H 36 | #define QUEUE_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _Queue; 41 | typedef struct _Queue Queue; 42 | 43 | Queue* queue_create(LinearContainer* container); 44 | 45 | Ret queue_head(Queue* thiz, void** data); 46 | Ret queue_push(Queue* thiz, void* data); 47 | Ret queue_pop(Queue* thiz); 48 | size_t queue_length(Queue* thiz); 49 | Ret queue_foreach(Queue* thiz, DataVisitFunc visit, void* ctx); 50 | 51 | void queue_destroy(Queue* thiz); 52 | 53 | DECLS_END 54 | 55 | #endif/*QUEUE_H*/ 56 | 57 | -------------------------------------------------------------------------------- /6/3/runtime/queue_test.c: -------------------------------------------------------------------------------- 1 | #include "module.h" 2 | #include "queue.h" 3 | #include "test_helper.c" 4 | #include "linear_container.h" 5 | 6 | typedef LinearContainer* (*LinearContainerCreateFunc)(DataDestroyFunc data_destroy, void* ctx); 7 | 8 | int main(int argc, char* argv[]) 9 | { 10 | int i = 0; 11 | int n = 1000; 12 | int ret_data = 0; 13 | Queue* queue = NULL; 14 | Module* module = NULL; 15 | LinearContainerCreateFunc linear_container_create = NULL; 16 | if(argc != 3) 17 | { 18 | printf("%s sharelib linear_container_create\n", argv[0]); 19 | 20 | return 0; 21 | } 22 | 23 | module = module_create(argv[1], 0); 24 | return_val_if_fail(module != NULL, 0); 25 | linear_container_create = (LinearContainerCreateFunc)module_sym(module, argv[2]); 26 | if(linear_container_create == NULL) 27 | { 28 | module_destroy(module); 29 | 30 | return 0; 31 | } 32 | 33 | queue = queue_create(linear_container_create(NULL, NULL)); 34 | 35 | for(i = 0; i < n; i++) 36 | { 37 | assert(queue_push(queue, (void*)i) == RET_OK); 38 | assert(queue_head(queue, (void**)&ret_data) == RET_OK); 39 | assert(queue_length(queue) == (i+1)); 40 | } 41 | 42 | queue_foreach(queue, print_int, NULL); 43 | 44 | for(i = 0; i < n; i++) 45 | { 46 | assert(queue_head(queue, (void**)&ret_data) == RET_OK); 47 | assert(ret_data == i ); 48 | assert(queue_length(queue) == (n - i)); 49 | assert(queue_pop(queue) == RET_OK); 50 | } 51 | 52 | queue_destroy(queue); 53 | module_destroy(module); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /6/3/runtime/test_helper.c: -------------------------------------------------------------------------------- 1 | static int hash_int(void* data) 2 | { 3 | return (int)data; 4 | } 5 | 6 | static int cmp_int(void* ctx, void* data) 7 | { 8 | return (int)ctx - (int)data; 9 | } 10 | 11 | static Ret print_int(void* ctx, void* data) 12 | { 13 | printf("%d ", (int)data); 14 | 15 | return RET_OK; 16 | } 17 | 18 | static Ret check_and_dec_int(void* ctx, void* data) 19 | { 20 | int* expected =(int*)ctx; 21 | assert(*expected == (int)data); 22 | 23 | (*expected)--; 24 | 25 | return RET_OK; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /6/3/runtime/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /7/2/helloworld/AUTHORS: -------------------------------------------------------------------------------- 1 | Li XianJing 2 | 3 | -------------------------------------------------------------------------------- /7/2/helloworld/COPYING: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/COPYING -------------------------------------------------------------------------------- /7/2/helloworld/ChangeLog: -------------------------------------------------------------------------------- 1 | 2009-03-29 Li XianJing 2 | * Created 3 | 4 | -------------------------------------------------------------------------------- /7/2/helloworld/INSTALL: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/INSTALL -------------------------------------------------------------------------------- /7/2/helloworld/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS=src 2 | -------------------------------------------------------------------------------- /7/2/helloworld/NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/2/helloworld/NEWS -------------------------------------------------------------------------------- /7/2/helloworld/README: -------------------------------------------------------------------------------- 1 | hello world for automake. 2 | 3 | run `./autogen.sh` first, and then `./configure && make` 4 | 5 | -------------------------------------------------------------------------------- /7/2/helloworld/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | mkdir m4 3 | echo "aclocal.." 4 | aclocal --force || exit 1 5 | echo "autoheader.." 6 | autoheader --force || exit 1 7 | echo "automake.." 8 | automake --force --add-missing || exit 1 9 | echo "autoconf.." 10 | autoconf --force || exit 1 11 | 12 | 13 | -------------------------------------------------------------------------------- /7/2/helloworld/autoscan.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/2/helloworld/autoscan.log -------------------------------------------------------------------------------- /7/2/helloworld/config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated from configure.in by autoheader. */ 2 | 3 | /* Name of package */ 4 | #undef PACKAGE 5 | 6 | /* Define to the address where bug reports for this package should be sent. */ 7 | #undef PACKAGE_BUGREPORT 8 | 9 | /* Define to the full name of this package. */ 10 | #undef PACKAGE_NAME 11 | 12 | /* Define to the full name and version of this package. */ 13 | #undef PACKAGE_STRING 14 | 15 | /* Define to the one symbol short name of this package. */ 16 | #undef PACKAGE_TARNAME 17 | 18 | /* Define to the version of this package. */ 19 | #undef PACKAGE_VERSION 20 | 21 | /* Version number of package */ 22 | #undef VERSION 23 | -------------------------------------------------------------------------------- /7/2/helloworld/configure.in: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(2.61) 5 | AC_INIT(helloworld, 0.1, xianjimli@hotmail.com) 6 | AC_CONFIG_SRCDIR([src/main.c]) 7 | AC_CONFIG_HEADER([config.h]) 8 | AM_INIT_AUTOMAKE(helloworld, 0.1) 9 | AC_CONFIG_MACRO_DIR([m4]) 10 | # Checks for programs. 11 | AC_PROG_CC 12 | 13 | # Checks for libraries. 14 | 15 | # Checks for header files. 16 | 17 | # Checks for typedefs, structures, and compiler characteristics. 18 | 19 | # Checks for library functions. 20 | 21 | AC_CONFIG_FILES([Makefile 22 | src/Makefile]) 23 | AC_OUTPUT 24 | -------------------------------------------------------------------------------- /7/2/helloworld/depcomp: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/depcomp -------------------------------------------------------------------------------- /7/2/helloworld/install-sh: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/install-sh -------------------------------------------------------------------------------- /7/2/helloworld/missing: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/missing -------------------------------------------------------------------------------- /7/2/helloworld/src/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS=helloworld 2 | helloworld_SOURCES=main.c 3 | -------------------------------------------------------------------------------- /7/2/helloworld/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | printf("Hello World!\n"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /7/3/base/AUTHORS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/3/base/AUTHORS -------------------------------------------------------------------------------- /7/3/base/COPYING: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/COPYING -------------------------------------------------------------------------------- /7/3/base/ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/3/base/ChangeLog -------------------------------------------------------------------------------- /7/3/base/INSTALL: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/INSTALL -------------------------------------------------------------------------------- /7/3/base/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS=src 2 | ACLOCAL_AMFLAGS=-I m4 3 | pkgconfigdir=${libdir}/pkgconfig 4 | pkgconfig_DATA=base.pc 5 | -------------------------------------------------------------------------------- /7/3/base/NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/3/base/NEWS -------------------------------------------------------------------------------- /7/3/base/README: -------------------------------------------------------------------------------- 1 | demo for automake 2 | 3 | run `./autogen.sh` first, and then `./configure && make` 4 | 5 | -------------------------------------------------------------------------------- /7/3/base/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "aclocal.." 4 | aclocal --force || exit 1 5 | echo "libtoolize.." 6 | libtoolize --force || exit 1 7 | echo "autoheader.." 8 | autoheader --force || exit 1 9 | echo "automake.." 10 | automake --force --add-missing || exit 1 11 | echo "autoconf.." 12 | autoconf --force || exit 1 13 | 14 | 15 | -------------------------------------------------------------------------------- /7/3/base/autoscan.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/3/base/autoscan.log -------------------------------------------------------------------------------- /7/3/base/base.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=${prefix} 3 | libdir=${prefix}/lib 4 | includedir=${prefix}/include 5 | 6 | Name: @PACKAGE_NAME@ 7 | Description: a basic library. 8 | Version: @VERSION@ 9 | Requires: 10 | Libs: -L${libdir} -lbase 11 | Cflags: -I${includedir}/base 12 | 13 | -------------------------------------------------------------------------------- /7/3/base/configure.in: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(2.61) 5 | AC_INIT(base, 0.1, xianjimli@hotmail.com) 6 | AC_CONFIG_SRCDIR([src/invert.c]) 7 | AC_CONFIG_HEADER([config.h]) 8 | AM_INIT_AUTOMAKE(base, 0.1) 9 | AC_CONFIG_MACRO_DIR([m4]) 10 | # Checks for programs. 11 | AC_PROG_CC 12 | AC_PROG_LIBTOOL 13 | AM_PROG_CC_C_O 14 | 15 | # Checks for libraries. 16 | # FIXME: Replace `main' with a function in `-lpthread': 17 | AC_CHECK_LIB([pthread], [main]) 18 | 19 | # Checks for header files. 20 | AC_HEADER_STDC 21 | AC_CHECK_HEADERS([stdlib.h string.h unistd.h]) 22 | 23 | # Checks for typedefs, structures, and compiler characteristics. 24 | AC_C_INLINE 25 | AC_TYPE_SIZE_T 26 | 27 | # Checks for library functions. 28 | AC_FUNC_MALLOC 29 | AC_FUNC_REALLOC 30 | 31 | AC_CONFIG_FILES([Makefile 32 | src/Makefile]) 33 | AC_OUTPUT([base.pc]) 34 | -------------------------------------------------------------------------------- /7/3/base/depcomp: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/depcomp -------------------------------------------------------------------------------- /7/3/base/install-sh: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/install-sh -------------------------------------------------------------------------------- /7/3/base/missing: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/missing -------------------------------------------------------------------------------- /7/3/base/src/Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES=libbase.la 2 | libbase_la_SOURCES= darray.c \ 3 | darray.h \ 4 | dlist.c \ 5 | dlist.h \ 6 | darray_iterator.h \ 7 | dlist_iterator.h \ 8 | hash_table.c \ 9 | hash_table.h \ 10 | invert.c \ 11 | iterator.h \ 12 | linear_container_darray.c \ 13 | linear_container_darray.h \ 14 | linear_container_dlist.c \ 15 | linear_container_dlist.h \ 16 | linear_container.h \ 17 | queue.c \ 18 | queue.h \ 19 | sort.c \ 20 | sort.h \ 21 | stack.c \ 22 | stack.h \ 23 | typedef.h 24 | 25 | libbase_la_LDFLAGS=-lpthread 26 | 27 | noinst_PROGRAMS=darray_test dlist_test 28 | 29 | darray_test_SOURCES=darray.c 30 | darray_test_CFLAGS=-DDARRAY_TEST 31 | 32 | dlist_test_SOURCES=dlist.c 33 | dlist_test_CFLAGS=-DDLIST_TEST 34 | 35 | basedir=$(includedir)/base 36 | base_HEADERS=darray.h dlist.h iterator.h linear_container_dlist.h typedef.h \ 37 | darray_iterator.h dlist_iterator.h linear_container_darray.h \ 38 | linear_container.h 39 | 40 | EXTRA_DIST=\ 41 | linear_container_test.c \ 42 | invert_ng.c \ 43 | darray_iterator.c \ 44 | dlist_iterator.c \ 45 | test_helper.c 46 | -------------------------------------------------------------------------------- /7/3/base/src/darray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: darray.h 3 | * Author: Li XianJing 4 | * Brief: dynamic array implementation. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-01-02 Li XianJing created 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef DARRAY_H 35 | #define DARRAY_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _DArray; 40 | typedef struct _DArray DArray; 41 | 42 | DArray* darray_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret darray_insert(DArray* thiz, size_t index, void* data); 45 | Ret darray_prepend(DArray* thiz, void* data); 46 | Ret darray_append(DArray* thiz, void* data); 47 | Ret darray_delete(DArray* thiz, size_t index); 48 | Ret darray_get_by_index(DArray* thiz, size_t index, void** data); 49 | Ret darray_set_by_index(DArray* thiz, size_t index, void* data); 50 | size_t darray_length(DArray* thiz); 51 | int darray_find(DArray* thiz, DataCompareFunc cmp, void* ctx); 52 | Ret darray_foreach(DArray* thiz, DataVisitFunc visit, void* ctx); 53 | Ret darray_sort(DArray* thiz, SortFunc sort, DataCompareFunc cmp); 54 | 55 | void darray_destroy(DArray* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DARRAY_H*/ 60 | 61 | -------------------------------------------------------------------------------- /7/3/base/src/darray_iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DARRAY_ITERATOR_H 2 | #define DARRAY_ITERATOR_H 3 | 4 | #include "darray.h" 5 | #include "iterator.h" 6 | 7 | DECLS_BEGIN 8 | 9 | Iterator* darray_iterator_create(DArray* darray); 10 | 11 | DECLS_END 12 | 13 | #endif/*DARRAY_ITERATOR_H*/ 14 | 15 | -------------------------------------------------------------------------------- /7/3/base/src/darray_iterator.loT: -------------------------------------------------------------------------------- 1 | # darray_iterator.lo - a libtool object file 2 | # Generated by ltmain.sh - GNU libtool 1.5.24 (1.1220.2.456 2007/06/24 02:25:32) 3 | # 4 | # Please DO NOT delete this file! 5 | # It is necessary for linking the library. 6 | 7 | # Name of the PIC object. 8 | -------------------------------------------------------------------------------- /7/3/base/src/dlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: dlist.h 3 | * Author: Li XianJing 4 | * Brief: double list header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-11-24 Li XianJing created 29 | * 2008-12-08 Li XianJing add autotest. 30 | */ 31 | 32 | #include 33 | #include "typedef.h" 34 | 35 | #ifndef DLIST_H 36 | #define DLIST_H 37 | 38 | DECLS_BEGIN 39 | 40 | struct _DList; 41 | typedef struct _DList DList; 42 | 43 | DList* dlist_create(DataDestroyFunc data_destroy, void* ctx); 44 | 45 | Ret dlist_insert(DList* thiz, size_t index, void* data); 46 | Ret dlist_prepend(DList* thiz, void* data); 47 | Ret dlist_append(DList* thiz, void* data); 48 | Ret dlist_delete(DList* thiz, size_t index); 49 | Ret dlist_get_by_index(DList* thiz, size_t index, void** data); 50 | Ret dlist_set_by_index(DList* thiz, size_t index, void* data); 51 | size_t dlist_length(DList* thiz); 52 | int dlist_find(DList* thiz, DataCompareFunc cmp, void* ctx); 53 | Ret dlist_foreach(DList* thiz, DataVisitFunc visit, void* ctx); 54 | 55 | void dlist_destroy(DList* thiz); 56 | 57 | DECLS_END 58 | 59 | #endif/*DLIST*/ 60 | 61 | -------------------------------------------------------------------------------- /7/3/base/src/dlist_iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DLIST_ITERATOR_H 2 | #define DLIST_ITERATOR_H 3 | 4 | #include "dlist.h" 5 | #include "iterator.h" 6 | 7 | DECLS_BEGIN 8 | 9 | Iterator* dlist_iterator_create(DList* dlist); 10 | 11 | DECLS_END 12 | 13 | #endif/*DLIST_ITERATOR_H*/ 14 | 15 | -------------------------------------------------------------------------------- /7/3/base/src/hash_table.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: hash_table.h 3 | * Author: Li XianJing 4 | * Brief: hash table header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-13 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef HASH_TABLE_H 35 | #define HASH_TABLE_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _HashTable; 40 | typedef struct _HashTable HashTable; 41 | 42 | HashTable* hash_table_create(DataDestroyFunc data_destroy, void* ctx, DataHashFunc hash, int slot_nr); 43 | 44 | size_t hash_table_length(HashTable* thiz); 45 | Ret hash_table_insert(HashTable* thiz, void* data); 46 | Ret hash_table_delete(HashTable* thiz, DataCompareFunc cmp, void* data); 47 | Ret hash_table_find(HashTable* thiz, DataCompareFunc cmp, void* data, void** ret_data); 48 | Ret hash_table_foreach(HashTable* thiz, DataVisitFunc visit, void* ctx); 49 | 50 | void hash_table_destroy(HashTable* thiz); 51 | 52 | DECLS_END 53 | 54 | #endif/*HASH_TABLE*/ 55 | 56 | -------------------------------------------------------------------------------- /7/3/base/src/invert.c: -------------------------------------------------------------------------------- 1 | #include "iterator.h" 2 | 3 | Ret invert(Iterator* forward, Iterator* backward) 4 | { 5 | void* data1 = NULL; 6 | void* data2 = NULL; 7 | return_val_if_fail(forward != NULL && backward != NULL, RET_INVALID_PARAMS); 8 | 9 | for(; iterator_offset(forward) < iterator_offset(backward); iterator_next(forward), iterator_prev(backward)) 10 | { 11 | iterator_get(forward, &data1); 12 | iterator_get(backward, &data2); 13 | iterator_set(forward, data2); 14 | iterator_set(backward, data1); 15 | } 16 | 17 | return RET_OK; 18 | } 19 | 20 | #ifdef INVERT_TEST 21 | #include "dlist.h" 22 | #include "dlist_iterator.h" 23 | #include "test_helper.c" 24 | 25 | int main(int argc, char* argv[]) 26 | { 27 | int i = 0; 28 | int n = 101; 29 | int last = n - 1; 30 | DList* dlist = dlist_create(NULL, NULL); 31 | 32 | for(i = 0; i < n; i++) 33 | { 34 | dlist_append(dlist, (void*)i); 35 | } 36 | 37 | Iterator* forward = dlist_iterator_create(dlist); 38 | Iterator* backward = dlist_iterator_create(dlist); 39 | 40 | iterator_advance(backward, last); 41 | invert(forward, backward); 42 | dlist_foreach(dlist, check_and_dec_int, &last); 43 | iterator_destroy(forward); 44 | iterator_destroy(backward); 45 | dlist_destroy(dlist); 46 | 47 | return 0; 48 | } 49 | #endif/*INVERT_TEST*/ 50 | -------------------------------------------------------------------------------- /7/3/base/src/invert_ng.c: -------------------------------------------------------------------------------- 1 | #include "linear_container.h" 2 | 3 | Ret invert(LinearContainer* linear_container) 4 | { 5 | int i = 0; 6 | int j = 0; 7 | void* data1 = NULL; 8 | void* data2 = NULL; 9 | 10 | return_val_if_fail(linear_container != NULL, RET_INVALID_PARAMS); 11 | 12 | j = linear_container_length(linear_container) - 1; 13 | for(; i < j; i++, j--) 14 | { 15 | linear_container_get_by_index(linear_container, i, &data1); 16 | linear_container_get_by_index(linear_container, j, &data2); 17 | linear_container_set_by_index(linear_container, i, data2); 18 | linear_container_set_by_index(linear_container, j, data1); 19 | } 20 | 21 | return RET_OK; 22 | } 23 | 24 | #ifdef INVERT_TEST 25 | #include "test_helper.c" 26 | #include "linear_container_dlist.h" 27 | 28 | int main(int argc, char* argv[]) 29 | { 30 | int i = 0; 31 | int n = 101; 32 | 33 | LinearContainer* linear_container = linear_container_dlist_create(NULL, NULL); 34 | for(i = 0; i < n; i++) 35 | { 36 | linear_container_append(linear_container, (void*)i); 37 | } 38 | invert(linear_container); 39 | i--; 40 | linear_container_foreach(linear_container, check_and_dec_int, &i); 41 | linear_container_destroy(linear_container); 42 | 43 | return 0; 44 | } 45 | #endif/*INVERT_TEST*/ 46 | -------------------------------------------------------------------------------- /7/3/base/src/iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | #include "typedef.h" 5 | 6 | DECLS_BEGIN 7 | 8 | struct _Iterator; 9 | typedef struct _Iterator Iterator; 10 | 11 | typedef Ret (*IteratorSetFunc)(Iterator* thiz, void* data); 12 | typedef Ret (*IteratorGetFunc)(Iterator* thiz, void** data); 13 | typedef Ret (*IteratorNextFunc)(Iterator* thiz); 14 | typedef Ret (*IteratorPrevFunc)(Iterator* thiz); 15 | typedef Ret (*IteratorAdvanceFunc)(Iterator* thiz, int offset); 16 | typedef int (*IteratorOffsetFunc)(Iterator* thiz); 17 | typedef Ret (*IteratorCloneFunc)(Iterator* thiz, Iterator** cloned); 18 | typedef void (*IteratorDestroyFunc)(Iterator* thiz); 19 | 20 | struct _Iterator 21 | { 22 | IteratorSetFunc set; 23 | IteratorGetFunc get; 24 | IteratorNextFunc next; 25 | IteratorPrevFunc prev; 26 | IteratorAdvanceFunc advance; 27 | IteratorCloneFunc clone; 28 | IteratorOffsetFunc offset; 29 | IteratorDestroyFunc destroy; 30 | 31 | char priv[0]; 32 | }; 33 | 34 | static inline Ret iterator_set(Iterator* thiz, void* data) 35 | { 36 | return_val_if_fail(thiz != NULL && thiz->set != NULL, RET_INVALID_PARAMS); 37 | 38 | return thiz->set(thiz, data); 39 | } 40 | 41 | static inline Ret iterator_get(Iterator* thiz, void** data) 42 | { 43 | return_val_if_fail(thiz != NULL && thiz->get != NULL, RET_INVALID_PARAMS); 44 | 45 | return thiz->get(thiz, data); 46 | } 47 | 48 | static inline Ret iterator_next(Iterator* thiz) 49 | { 50 | return_val_if_fail(thiz != NULL && thiz->next != NULL, RET_INVALID_PARAMS); 51 | 52 | return thiz->next(thiz); 53 | } 54 | 55 | static inline Ret iterator_prev(Iterator* thiz) 56 | { 57 | return_val_if_fail(thiz != NULL && thiz->prev != NULL, RET_INVALID_PARAMS); 58 | 59 | return thiz->prev(thiz); 60 | } 61 | 62 | static inline Ret iterator_advance(Iterator* thiz, int offset) 63 | { 64 | return_val_if_fail(thiz != NULL && thiz->advance != NULL, RET_INVALID_PARAMS); 65 | 66 | return thiz->advance(thiz, offset); 67 | } 68 | 69 | static inline int iterator_offset(Iterator* thiz) 70 | { 71 | return_val_if_fail(thiz != NULL && thiz->offset != NULL, -1); 72 | 73 | return thiz->offset(thiz); 74 | } 75 | 76 | static inline Ret iterator_clone(Iterator* thiz, Iterator** cloned) 77 | { 78 | return_val_if_fail(thiz != NULL && thiz->clone != NULL, RET_INVALID_PARAMS); 79 | 80 | return thiz->clone(thiz, cloned); 81 | } 82 | 83 | static inline void iterator_destroy(Iterator* thiz) 84 | { 85 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 86 | 87 | return thiz->destroy(thiz); 88 | } 89 | 90 | DECLS_END 91 | 92 | #endif/*ITERATOR_H*/ 93 | 94 | -------------------------------------------------------------------------------- /7/3/base/src/linear_container_darray.h: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_CONTAINER_DARRAY_H 2 | #define LINEAR_CONTAINER_DARRAY_H 3 | 4 | #include "linear_container.h" 5 | 6 | LinearContainer* linear_container_darray_create(DataDestroyFunc data_destroy, void* ctx); 7 | 8 | #endif/*LINEAR_CONTAINER_DARRAY_H*/ 9 | -------------------------------------------------------------------------------- /7/3/base/src/linear_container_dlist.h: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_CONTAINER_DLIST_H 2 | #define LINEAR_CONTAINER_DLIST_H 3 | 4 | #include "linear_container.h" 5 | 6 | LinearContainer* linear_container_dlist_create(DataDestroyFunc data_destroy, void* ctx); 7 | 8 | #endif/*LINEAR_CONTAINER_DLIST_H*/ 9 | -------------------------------------------------------------------------------- /7/3/base/src/queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: queue.h 3 | * Author: Li XianJing 4 | * Brief: queue header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-14 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef QUEUE_H 35 | #define QUEUE_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Queue; 40 | typedef struct _Queue Queue; 41 | 42 | Queue* queue_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret queue_head(Queue* thiz, void** data); 45 | Ret queue_push(Queue* thiz, void* data); 46 | Ret queue_pop(Queue* thiz); 47 | size_t queue_length(Queue* thiz); 48 | Ret queue_foreach(Queue* thiz, DataVisitFunc visit, void* ctx); 49 | 50 | void queue_destroy(Queue* thiz); 51 | 52 | DECLS_END 53 | 54 | #endif/*QUEUE_H*/ 55 | 56 | -------------------------------------------------------------------------------- /7/3/base/src/sort.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: sort.h 3 | * Author: Li XianJing 4 | * Brief: prototype of sort functions. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2009-02-07 Li XianJing created. 29 | * 30 | */ 31 | 32 | #ifndef SORT_H 33 | #define SORT_H 34 | 35 | #include "typedef.h" 36 | 37 | DECLS_BEGIN 38 | 39 | Ret bubble_sort(void** array, size_t nr, DataCompareFunc cmp); 40 | Ret quick_sort(void** array, size_t nr, DataCompareFunc cmp); 41 | Ret merge_sort(void** array, size_t nr, DataCompareFunc cmp); 42 | 43 | DECLS_END 44 | 45 | #endif/*SORT_H*/ 46 | 47 | -------------------------------------------------------------------------------- /7/3/base/src/stack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: stack.c 3 | * Author: Li XianJing 4 | * Brief: stack header file. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-02-14 Li XianJing created. 29 | */ 30 | 31 | #include 32 | #include "typedef.h" 33 | 34 | #ifndef STACK_H 35 | #define STACK_H 36 | 37 | DECLS_BEGIN 38 | 39 | struct _Stack; 40 | typedef struct _Stack Stack; 41 | 42 | Stack* stack_create(DataDestroyFunc data_destroy, void* ctx); 43 | 44 | Ret stack_top(Stack* thiz, void** data); 45 | Ret stack_push(Stack* thiz, void* data); 46 | Ret stack_pop(Stack* thiz); 47 | size_t stack_length(Stack* thiz); 48 | Ret stack_foreach(Stack* thiz, DataVisitFunc visit, void* ctx); 49 | 50 | void stack_destroy(Stack* thiz); 51 | 52 | DECLS_END 53 | 54 | #endif/*STACK_H*/ 55 | 56 | -------------------------------------------------------------------------------- /7/3/base/src/test_helper.c: -------------------------------------------------------------------------------- 1 | static int hash_int(void* data) 2 | { 3 | return (int)data; 4 | } 5 | 6 | static int cmp_int(void* ctx, void* data) 7 | { 8 | return (int)ctx - (int)data; 9 | } 10 | 11 | static Ret print_int(void* ctx, void* data) 12 | { 13 | printf("%d ", (int)data); 14 | 15 | return RET_OK; 16 | } 17 | 18 | static Ret check_and_dec_int(void* ctx, void* data) 19 | { 20 | int* expected =(int*)ctx; 21 | assert(*expected == (int)data); 22 | 23 | (*expected)--; 24 | 25 | return RET_OK; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /7/3/base/src/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | typedef void (*DataDestroyFunc)(void* ctx, void* data); 49 | typedef int (*DataCompareFunc)(void* ctx, void* data); 50 | typedef Ret (*DataVisitFunc)(void* ctx, void* data); 51 | typedef int (*DataHashFunc)(void* data); 52 | 53 | #ifdef __cplusplus 54 | #define DECLS_BEGIN extern "C" { 55 | #define DECLS_END } 56 | #else 57 | #define DECLS_BEGIN 58 | #define DECLS_END 59 | #endif/*__cplusplus*/ 60 | 61 | #define return_if_fail(p) if(!(p)) \ 62 | {printf("%s:%d Warning: "#p" failed.\n", \ 63 | __func__, __LINE__); return;} 64 | #define return_val_if_fail(p, ret) if(!(p)) \ 65 | {printf("%s:%d Warning: "#p" failed.\n",\ 66 | __func__, __LINE__); return (ret);} 67 | 68 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 69 | 70 | typedef Ret (*SortFunc)(void** array, size_t nr, DataCompareFunc cmp); 71 | 72 | #endif/*TYPEDEF_H*/ 73 | 74 | -------------------------------------------------------------------------------- /7/4/appdemo/AUTHORS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/4/appdemo/AUTHORS -------------------------------------------------------------------------------- /7/4/appdemo/COPYING: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/COPYING -------------------------------------------------------------------------------- /7/4/appdemo/ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/4/appdemo/ChangeLog -------------------------------------------------------------------------------- /7/4/appdemo/INSTALL: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/INSTALL -------------------------------------------------------------------------------- /7/4/appdemo/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS=src 2 | -------------------------------------------------------------------------------- /7/4/appdemo/NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/4/appdemo/NEWS -------------------------------------------------------------------------------- /7/4/appdemo/README: -------------------------------------------------------------------------------- 1 | demo for automake 2 | 3 | run `./autogen.sh` first, and then `./configure && make` 4 | 5 | -------------------------------------------------------------------------------- /7/4/appdemo/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "aclocal.." 4 | aclocal --force || exit 1 5 | echo "autoheader.." 6 | autoheader --force || exit 1 7 | echo "automake.." 8 | automake --force --add-missing || exit 1 9 | echo "autoconf.." 10 | autoconf --force || exit 1 11 | 12 | 13 | -------------------------------------------------------------------------------- /7/4/appdemo/autoscan.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xianjimli/sysprog-code/36846fcf26d1a9abeec797beaac05874e0955b26/7/4/appdemo/autoscan.log -------------------------------------------------------------------------------- /7/4/appdemo/config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated from configure.in by autoheader. */ 2 | 3 | /* Define to 1 if your C compiler doesn't accept -c and -o together. */ 4 | #undef NO_MINUS_C_MINUS_O 5 | 6 | /* Name of package */ 7 | #undef PACKAGE 8 | 9 | /* Define to the address where bug reports for this package should be sent. */ 10 | #undef PACKAGE_BUGREPORT 11 | 12 | /* Define to the full name of this package. */ 13 | #undef PACKAGE_NAME 14 | 15 | /* Define to the full name and version of this package. */ 16 | #undef PACKAGE_STRING 17 | 18 | /* Define to the one symbol short name of this package. */ 19 | #undef PACKAGE_TARNAME 20 | 21 | /* Define to the version of this package. */ 22 | #undef PACKAGE_VERSION 23 | 24 | /* Version number of package */ 25 | #undef VERSION 26 | -------------------------------------------------------------------------------- /7/4/appdemo/configure.in: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(2.61) 5 | AC_INIT(appdemo, 0.1, xianjimli@hotmail.com) 6 | AC_CONFIG_SRCDIR([src/main.c]) 7 | AC_CONFIG_HEADER([config.h]) 8 | AM_INIT_AUTOMAKE(appdemo, 0.1) 9 | # Checks for programs. 10 | AC_PROG_CC 11 | AM_PROG_CC_C_O 12 | 13 | # Checks for libraries. 14 | PKG_CHECK_MODULES(BASE, ["base"]) 15 | AC_SUBST(BASE_CFLAGS) 16 | AC_SUBST(BASE_LIBS) 17 | 18 | # Checks for header files. 19 | 20 | # Checks for typedefs, structures, and compiler characteristics. 21 | 22 | # Checks for library functions. 23 | 24 | AC_CONFIG_FILES([Makefile 25 | src/Makefile]) 26 | AC_OUTPUT 27 | -------------------------------------------------------------------------------- /7/4/appdemo/depcomp: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/depcomp -------------------------------------------------------------------------------- /7/4/appdemo/install-sh: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/install-sh -------------------------------------------------------------------------------- /7/4/appdemo/missing: -------------------------------------------------------------------------------- 1 | /usr/share/automake-1.11/missing -------------------------------------------------------------------------------- /7/4/appdemo/src/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS=appdemo 2 | appdemo_SOURCES=main.c 3 | appdemo_CFLAGS=@BASE_CFLAGS@ 4 | appdemo_LDFLAGS=@BASE_LIBS@ 5 | -------------------------------------------------------------------------------- /7/4/appdemo/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | DList* dlist = dlist_create(NULL, NULL); 6 | 7 | dlist_destroy(dlist); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /8/1/shmem/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g fifo_ring.c -DFIFO_RING_TEST -o fifo_ring_test 3 | clean: 4 | rm -f fifo_ring_test 5 | -------------------------------------------------------------------------------- /8/1/shmem/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | #ifdef __cplusplus 49 | #define DECLS_BEGIN extern "C" { 50 | #define DECLS_END } 51 | #else 52 | #define DECLS_BEGIN 53 | #define DECLS_END 54 | #endif/*__cplusplus*/ 55 | 56 | #define return_if_fail(p) if(!(p)) \ 57 | {printf("%s:%d Warning: "#p" failed.\n", \ 58 | __func__, __LINE__); return;} 59 | #define return_val_if_fail(p, ret) if(!(p)) \ 60 | {printf("%s:%d Warning: "#p" failed.\n",\ 61 | __func__, __LINE__); return (ret);} 62 | 63 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 64 | 65 | #endif/*TYPEDEF_H*/ 66 | 67 | -------------------------------------------------------------------------------- /8/2/tls/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g tls.c -lpthread -o tls 3 | clean: 4 | rm -f tls 5 | -------------------------------------------------------------------------------- /8/2/tls/tls.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static pthread_key_t key; 5 | static pthread_once_t key_once = PTHREAD_ONCE_INIT; 6 | 7 | static void make_key(void) 8 | { 9 | pthread_key_create(&key, NULL); 10 | 11 | return; 12 | } 13 | 14 | void* thread_entry(void* param) 15 | { 16 | pthread_once(&key_once, make_key); 17 | 18 | if (pthread_getspecific(key) == NULL) 19 | { 20 | pthread_setspecific(key, (void*)pthread_self()); 21 | } 22 | 23 | printf("data=%u\n", pthread_getspecific(key)); 24 | 25 | return NULL; 26 | } 27 | 28 | #define THREAD_NR 5 29 | int main(int argc, char* argv[]) 30 | { 31 | int i = 0; 32 | pthread_t tids[THREAD_NR] = {0}; 33 | 34 | for(i = 0; i < THREAD_NR; i++) 35 | { 36 | pthread_create(tids+i, NULL, thread_entry, NULL); 37 | 38 | } 39 | 40 | for(i = 0; i < THREAD_NR; i++) 41 | { 42 | pthread_join(tids[i], NULL); 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /8/3/allocator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -Wall -g allocator_normal.c -DALLOCATOR_NORMAL_TEST -o allocator_normal_test 3 | gcc -Wall -g allocator_self_manage.c -DALLOCATOR_SELF_MANAGE_TEST \ 4 | -o allocator_self_manage_test 5 | gcc -Wall -g allocator_self_manage.c allocator_shmem.c -DALLOCATOR_SHMEM_TEST\ 6 | -o allocator_shmem_test 7 | gcc -Wall -g allocator_checkbo.c allocator_normal.c -DALLOCATOR_CHECKBO_TEST -o allocator_checkbo_test 8 | clean: 9 | rm -f *test 10 | -------------------------------------------------------------------------------- /8/3/allocator/allocator.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ALLOCATOR_H 3 | #define ALLOCATOR_H 4 | 5 | #include "typedef.h" 6 | 7 | DECLS_BEGIN 8 | 9 | struct _Allocator; 10 | typedef struct _Allocator Allocator; 11 | 12 | typedef void* (*AllocatorCallocFunc)(Allocator* thiz, size_t nmemb, size_t size); 13 | typedef void* (*AllocatorAllocFunc)(Allocator* thiz, size_t size); 14 | typedef void (*AllocatorFreeFunc)(Allocator* thiz, void *ptr); 15 | typedef void* (*AllocatorReallocFunc)(Allocator* thiz, void *ptr, size_t size); 16 | typedef void (*AllocatorDestroyFunc)(Allocator* thiz); 17 | 18 | struct _Allocator 19 | { 20 | AllocatorCallocFunc calloc; 21 | AllocatorAllocFunc alloc; 22 | AllocatorFreeFunc free; 23 | AllocatorReallocFunc realloc; 24 | AllocatorDestroyFunc destroy; 25 | 26 | char priv[0]; 27 | }; 28 | 29 | static inline void* allocator_calloc(Allocator* thiz, size_t nmemb, size_t size) 30 | { 31 | return_val_if_fail(thiz != NULL && thiz->calloc != NULL, NULL); 32 | 33 | return thiz->calloc(thiz, nmemb, size); 34 | } 35 | 36 | static inline void* allocator_alloc(Allocator* thiz, size_t size) 37 | { 38 | return_val_if_fail(thiz != NULL && thiz->alloc != NULL, NULL); 39 | 40 | return thiz->alloc(thiz, size); 41 | } 42 | 43 | static inline void allocator_free(Allocator* thiz, void *ptr) 44 | { 45 | return_if_fail(thiz != NULL && thiz->free != NULL); 46 | 47 | thiz->free(thiz, ptr); 48 | 49 | return; 50 | } 51 | 52 | static inline void* allocator_realloc(Allocator* thiz, void *ptr, size_t size) 53 | { 54 | return_val_if_fail(thiz != NULL && thiz->realloc != NULL, NULL); 55 | 56 | return thiz->realloc(thiz, ptr, size); 57 | } 58 | 59 | static inline void allocator_destroy(Allocator* thiz) 60 | { 61 | return_if_fail(thiz != NULL && thiz->destroy != NULL); 62 | 63 | return thiz->destroy(thiz); 64 | } 65 | 66 | DECLS_END 67 | 68 | #endif/*ALLOCATOR_H*/ 69 | 70 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_checkbo.h: -------------------------------------------------------------------------------- 1 | #ifndef ALLOCATOR_CHECKBO_H 2 | #define ALLOCATOR_CHECKBO_H 3 | #include "allocator.h" 4 | 5 | DECLS_BEGIN 6 | 7 | Allocator* allocator_checkbo_create(Allocator* real_allocator); 8 | 9 | DECLS_END 10 | 11 | #endif/*ALLOCATOR_CHECKBO_H*/ 12 | 13 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_normal.c: -------------------------------------------------------------------------------- 1 | #include "allocator_normal.h" 2 | 3 | static void* allocator_normal_calloc(Allocator* thiz, size_t nmemb, size_t size) 4 | { 5 | return calloc(nmemb, size); 6 | } 7 | 8 | static void* allocator_normal_alloc(Allocator* thiz, size_t size) 9 | { 10 | return malloc(size); 11 | } 12 | 13 | static void allocator_normal_free(Allocator* thiz, void *ptr) 14 | { 15 | free(ptr); 16 | 17 | return; 18 | } 19 | 20 | static void* allocator_normal_realloc(Allocator* thiz, void *ptr, size_t size) 21 | { 22 | return realloc(ptr, size); 23 | } 24 | 25 | static void allocator_normal_destroy(Allocator* thiz) 26 | { 27 | if(thiz != NULL) 28 | { 29 | free(thiz); 30 | } 31 | 32 | return; 33 | } 34 | 35 | Allocator* allocator_normal_create(void) 36 | { 37 | Allocator* thiz = (Allocator*)calloc(1, sizeof(Allocator)); 38 | 39 | if(thiz != NULL) 40 | { 41 | thiz->calloc = allocator_normal_calloc; 42 | thiz->alloc = allocator_normal_alloc; 43 | thiz->realloc = allocator_normal_realloc; 44 | thiz->free = allocator_normal_free; 45 | thiz->destroy = allocator_normal_destroy; 46 | } 47 | 48 | return thiz; 49 | } 50 | 51 | #ifdef ALLOCATOR_NORMAL_TEST 52 | int main(int argc, char* argv[]) 53 | { 54 | int i = 0; 55 | int n = 100; 56 | Allocator* allocator = allocator_normal_create(); 57 | 58 | for(i = 0; i < n; i++) 59 | { 60 | char* ptr = allocator_alloc(allocator, i); 61 | ptr = allocator_realloc(allocator, ptr, i+4); 62 | allocator_free(allocator, ptr); 63 | ptr = allocator_calloc(allocator, i+4, 4); 64 | allocator_free(allocator, ptr); 65 | } 66 | 67 | allocator_destroy(allocator); 68 | 69 | return 0; 70 | } 71 | #endif/*ALLOCATOR_NORMAL_TEST*/ 72 | 73 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_normal.h: -------------------------------------------------------------------------------- 1 | #ifndef ALLOCATOR_NORMAL_H 2 | #define ALLOCATOR_NORMAL_H 3 | #include "allocator.h" 4 | 5 | DECLS_BEGIN 6 | 7 | Allocator* allocator_normal_create(void); 8 | 9 | DECLS_END 10 | 11 | #endif/*ALLOCATOR_NORMAL_H*/ 12 | 13 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_pool.c: -------------------------------------------------------------------------------- 1 | #include "allocator_pool.h" 2 | 3 | typedef struct _FreeNode 4 | { 5 | size_t length; 6 | struct _FreeNode* next; 7 | struct _FreeNode* prev; 8 | }FreeNode; 9 | 10 | #define MIN_SIZE 16 11 | #define MAX_SIZE 64 12 | #define EXPAND_DELTA 32 13 | 14 | typedef struct _PrivInfo 15 | { 16 | FreeNode* free_list[8]; 17 | Allocator* real_allocator; 18 | }PrivInfo; 19 | 20 | static void* allocator_pool_calloc(Allocator* thiz, size_t nmemb, size_t size) 21 | { 22 | void* ptr = NULL; 23 | size_t total = nmemb * size; 24 | PrivInfo* priv = thiz->priv; 25 | 26 | if(total <= MAX_SIZE) 27 | { 28 | ptr = allocator_alloc(thiz, total); 29 | if(ptr != NULL) 30 | { 31 | memset(ptr, 0x00, total); 32 | } 33 | } 34 | else 35 | { 36 | ptr = allocator_calloc(priv->real_allocator, nmemb, size); 37 | } 38 | 39 | return ptr; 40 | } 41 | 42 | static void* allocator_pool_alloc(Allocator* thiz, size_t size) 43 | { 44 | void* ptr = NULL; 45 | PrivInfo* priv = thiz->priv; 46 | 47 | if(size <= MAX_SIZE) 48 | { 49 | size_t index = (size - 1)/8; 50 | if(priv->free_list[index] == NULL) 51 | { 52 | priv->free_list[index] = allocator_alloc(priv->real_allocator, 53 | (index + 1) * 8 54 | } 55 | } 56 | else 57 | { 58 | } 59 | } 60 | 61 | static void allocator_pool_free(Allocator* thiz, void *ptr) 62 | { 63 | free(ptr); 64 | 65 | return; 66 | } 67 | 68 | static void* allocator_pool_realloc(Allocator* thiz, void *ptr, size_t size) 69 | { 70 | return realloc(ptr, size); 71 | } 72 | 73 | static void allocator_pool_destroy(Allocator* thiz) 74 | { 75 | if(thiz != NULL) 76 | { 77 | free(thiz); 78 | } 79 | 80 | return; 81 | } 82 | 83 | Allocator* allocator_pool_create(size_t expand_delta) 84 | { 85 | Allocator* thiz = (Allocator*)calloc(1, sizeof(Allocator)); 86 | 87 | if(thiz != NULL) 88 | { 89 | thiz->calloc = allocator_pool_calloc; 90 | thiz->alloc = allocator_pool_alloc; 91 | thiz->realloc = allocator_pool_realloc; 92 | thiz->free = allocator_pool_free; 93 | thiz->destroy = allocator_pool_destroy; 94 | } 95 | 96 | return thiz; 97 | } 98 | 99 | #ifdef ALLOCATOR_POOL_TEST 100 | int main(int argc, char* argv[]) 101 | { 102 | int i = 0; 103 | int n = 100; 104 | Allocator* allocator = allocator_pool_create(128); 105 | 106 | for(i = 0; i < n; i++) 107 | { 108 | char* ptr = allocator_alloc(allocator, i); 109 | ptr = allocator_realloc(allocator, ptr, i+4); 110 | allocator_free(allocator, ptr); 111 | ptr = allocator_calloc(allocator, i+4, 4); 112 | allocator_free(allocator, ptr); 113 | } 114 | 115 | allocator_destroy(allocator); 116 | 117 | return 0; 118 | } 119 | #endif/*ALLOCATOR_POOL_TEST*/ 120 | 121 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_pool.h: -------------------------------------------------------------------------------- 1 | #ifndef ALLOCATOR_POOL_H 2 | #define ALLOCATOR_POOL_H 3 | #include "allocator.h" 4 | 5 | DECLS_BEGIN 6 | 7 | Allocator* allocator_pool_create(size_t expand_delta); 8 | 9 | DECLS_END 10 | 11 | #endif/*ALLOCATOR_POOL_H*/ 12 | 13 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_self_manage.h: -------------------------------------------------------------------------------- 1 | #ifndef ALLOCATOR_SELF_MANAGE_H 2 | #define ALLOCATOR_SELF_MANAGE_H 3 | #include "allocator.h" 4 | 5 | DECLS_BEGIN 6 | 7 | Allocator* allocator_self_manage_create(void* buffer, size_t length); 8 | 9 | DECLS_END 10 | 11 | #endif/*ALLOCATOR_SELF_MANAGE_H*/ 12 | 13 | -------------------------------------------------------------------------------- /8/3/allocator/allocator_shmem.h: -------------------------------------------------------------------------------- 1 | #ifndef ALLOCATOR_SHMEM_H 2 | #define ALLOCATOR_SHMEM_H 3 | #include "allocator.h" 4 | 5 | DECLS_BEGIN 6 | 7 | Allocator* allocator_shmem_create(void* addr, size_t init_size); 8 | 9 | DECLS_END 10 | 11 | #endif/*ALLOCATOR_SHMEM_H*/ 12 | 13 | -------------------------------------------------------------------------------- /8/3/allocator/typedef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: typedef.h 3 | * Author: Li XianJing 4 | * Brief: common types definition. 5 | * 6 | * Copyright (c) Li XianJing 7 | * 8 | * Licensed under the Academic Free License version 2.1 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 | */ 24 | 25 | /* 26 | * History: 27 | * ================================================================ 28 | * 2008-12-10 Li XianJing created. 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef TYPEDEF_H 37 | #define TYPEDEF_H 38 | 39 | typedef enum _Ret 40 | { 41 | RET_OK, 42 | RET_OOM, 43 | RET_STOP, 44 | RET_INVALID_PARAMS, 45 | RET_FAIL 46 | }Ret; 47 | 48 | #ifdef __cplusplus 49 | #define DECLS_BEGIN extern "C" { 50 | #define DECLS_END } 51 | #else 52 | #define DECLS_BEGIN 53 | #define DECLS_END 54 | #endif/*__cplusplus*/ 55 | 56 | #define return_if_fail(p) if(!(p)) \ 57 | {printf("%s:%d Warning: "#p" failed.\n", \ 58 | __func__, __LINE__); return;} 59 | #define return_val_if_fail(p, ret) if(!(p)) \ 60 | {printf("%s:%d Warning: "#p" failed.\n",\ 61 | __func__, __LINE__); return (ret);} 62 | 63 | #define SAFE_FREE(p) if(p != NULL) {free(p); p = NULL;} 64 | 65 | #endif/*TYPEDEF_H*/ 66 | 67 | -------------------------------------------------------------------------------- /9/1/varargs/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -Wall varargs.c -o varargs_test 3 | clean: 4 | rm *test 5 | -------------------------------------------------------------------------------- /9/1/varargs/varargs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define va_list void* 4 | #define va_end(arg) 5 | #define va_arg(arg, type) *(type*)arg; arg = (char*)arg + sizeof(type); 6 | #define va_start(arg, start) arg = (va_list)(((char*)&(start)) + sizeof(start)) 7 | 8 | int accumlate(int nr, ...) 9 | { 10 | int i = 0; 11 | int result = 0; 12 | va_list arg = NULL; 13 | va_start(arg, nr); 14 | 15 | for(i = 0; i < nr; i++) 16 | { 17 | result += va_arg(arg, int); 18 | } 19 | va_end(arg); 20 | 21 | return result; 22 | } 23 | 24 | int main(int argc, char* argv[]) 25 | { 26 | printf("%d\n", accumlate(1, 100)); 27 | printf("%d\n", accumlate(2, 100, 200)); 28 | printf("%d\n", accumlate(3, 100, 200, 300)); 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /9/2/backtrace/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-g -Wall 2 | all: 3 | gcc34 $(CFLAGS) bt.c -o bt34 4 | gcc $(CFLAGS) -DNEW_GCC bt.c -o bt 5 | 6 | clean: 7 | rm -f bt bt34 8 | 9 | -------------------------------------------------------------------------------- /9/2/backtrace/bt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_LEVEL 4 4 | #ifdef NEW_GCC 5 | #define OFFSET 4 6 | #else 7 | #define OFFSET 0 8 | #endif/*NEW_GCC*/ 9 | 10 | int backtrace(void** buffer, int size) 11 | { 12 | int n = 0xfefefefe; 13 | int* p = &n; 14 | int i = 0; 15 | 16 | int ebp = p[1 + OFFSET]; 17 | int eip = p[2 + OFFSET]; 18 | 19 | for(i = 0; i < size; i++) 20 | { 21 | buffer[i] = (void*)eip; 22 | p = (int*)ebp; 23 | ebp = p[0]; 24 | eip = p[1]; 25 | } 26 | 27 | return size; 28 | } 29 | 30 | static void test2() 31 | { 32 | int i = 0; 33 | void* buffer[MAX_LEVEL] = {0}; 34 | 35 | backtrace(buffer, MAX_LEVEL); 36 | 37 | for(i = 0; i < MAX_LEVEL; i++) 38 | { 39 | printf("called by %p\n", buffer[i]); 40 | } 41 | 42 | return; 43 | } 44 | 45 | static void test1() 46 | { 47 | int a=0x11111111; 48 | int b=0x11111112; 49 | 50 | test2(); 51 | a = b; 52 | 53 | return; 54 | } 55 | 56 | static void test() 57 | { 58 | int a=0x10000000; 59 | int b=0x10000002; 60 | 61 | test1(); 62 | a = b; 63 | 64 | return; 65 | } 66 | 67 | int main(int argc, char* argv[]) 68 | { 69 | test(); 70 | 71 | return 0; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sysprog-code 2 | ============ 3 | 4 | 拙著《系统程序员成长计划》的示例代码。 5 | --------------------------------------------------------------------------------