diff --git a/gear-lib/CMakeLists.txt b/gear-lib/CMakeLists.txt index fea4d109..2a8b8190 100644 --- a/gear-lib/CMakeLists.txt +++ b/gear-lib/CMakeLists.txt @@ -48,23 +48,12 @@ ADD_SUBDIRECTORY(libbitmap) ADD_SUBDIRECTORY(libdict) ADD_SUBDIRECTORY(libfile) ADD_SUBDIRECTORY(libhash) - - -IF (NOT DEFINED ENV_MINGW) ADD_SUBDIRECTORY(libdarray) ADD_SUBDIRECTORY(libthread) ADD_SUBDIRECTORY(libsock) ADD_SUBDIRECTORY(libgevent) -ENDIF () - -IF (NOT DEFINED OS_WINDOWS) ADD_SUBDIRECTORY(libqueue) -ADD_SUBDIRECTORY(libthread) -ADD_SUBDIRECTORY(libgevent) ADD_SUBDIRECTORY(libdebug) -ADD_SUBDIRECTORY(libhash) -ADD_SUBDIRECTORY(libdarray) -ADD_SUBDIRECTORY(libqueue) ADD_SUBDIRECTORY(libtime) ADD_SUBDIRECTORY(liblog) ADD_SUBDIRECTORY(libmedia-io) @@ -79,6 +68,12 @@ ADD_SUBDIRECTORY(libworkq) ADD_SUBDIRECTORY(libhal) ADD_SUBDIRECTORY(librpc) ADD_SUBDIRECTORY(libmp4) + +IF (NOT DEFINED ENV_MINGW) +ENDIF () + +IF (NOT DEFINED OS_WINDOWS) + ENDIF () #libcollections diff --git a/gear-lib/libvector/libvector.c b/gear-lib/libvector/libvector.c index 483eb54d..af5f9249 100644 --- a/gear-lib/libvector/libvector.c +++ b/gear-lib/libvector/libvector.c @@ -73,7 +73,6 @@ int vector_empty(struct vector *v) printf("%s: paraments invalid!\n", __func__); return -1; } - v->tmp_cursor = 0; return (v->size == 0); } @@ -104,28 +103,30 @@ vector_iter vector_last(struct vector *v) return (void *)((uint8_t *)v->buf.iov_base + (v->size-1) * v->type_size); } -vector_iter vector_next(struct vector *v) +vector_iter vector_next(struct vector *v, vector_iter iter) { - if (!v) { + if (!v || !iter) { printf("%s: paraments invalid!\n", __func__); return NULL; } - if (v->tmp_cursor < v->size) { - v->tmp_cursor++; - } else { - return NULL; + if (iter == vector_end(v)) { + printf("%s: vector reach the end!\n", __func__); + return iter; } - return (void *)((uint8_t *)v->buf.iov_base + v->tmp_cursor * v->type_size); + return (void *)((uint8_t *)iter + v->type_size); } -vector_iter vector_prev(struct vector *v) +vector_iter vector_prev(struct vector *v, vector_iter iter) { - if (!v) { + if (!v || !iter) { printf("%s: paraments invalid!\n", __func__); return NULL; } - v->tmp_cursor--; - return (void *)((uint8_t *)v->buf.iov_base + v->tmp_cursor * v->type_size); + if (iter == vector_begin(v)) { + printf("%s: vector reach the begin!\n", __func__); + return iter; + } + return (void *)((uint8_t *)iter - v->type_size); } void *_vector_iter_value(struct vector *v, vector_iter iter) @@ -134,7 +135,11 @@ void *_vector_iter_value(struct vector *v, vector_iter iter) printf("%s: paraments invalid!\n", __func__); return NULL; } - return (void *)((uint8_t *)v->buf.iov_base + v->tmp_cursor * v->type_size); + if (iter > vector_end(v) || iter < vector_begin(v)) { + printf("%s: iter out of range!\n", __func__); + return NULL; + } + return (void *)((uint8_t *)iter); } void *_vector_at(struct vector *v, int pos) @@ -154,7 +159,6 @@ struct vector *_vector_create(size_t size) return NULL; } v->size = 0; - v->tmp_cursor = 0; v->type_size = size; v->max_size = (size_t)(-1/size); v->capacity = VECTOR_DEFAULT_BUF_LEN; diff --git a/gear-lib/libvector/libvector.h b/gear-lib/libvector/libvector.h index 44f21092..03816378 100644 --- a/gear-lib/libvector/libvector.h +++ b/gear-lib/libvector/libvector.h @@ -39,7 +39,6 @@ typedef struct vector { size_t max_size; //max number of element size_t capacity; //size of allocated storage capacity size_t type_size; - size_t tmp_cursor; struct iovec buf; vector_iter iterator; } vector_t; @@ -69,8 +68,8 @@ void _vector_push_back(struct vector *v, void *e, size_t type_size); vector_iter vector_begin(struct vector *v); vector_iter vector_end(struct vector *v); vector_iter vector_last(struct vector *v);//last=end-1 -vector_iter vector_next(struct vector *v); -vector_iter vector_prev(struct vector *v); +vector_iter vector_next(struct vector *v, vector_iter iter); +vector_iter vector_prev(struct vector *v, vector_iter iter); void *_vector_iter_value(struct vector *v, vector_iter iter); void *_vector_at(struct vector *v, int pos); diff --git a/gear-lib/libvector/test_libvector.c b/gear-lib/libvector/test_libvector.c index d23ca834..02bfb389 100644 --- a/gear-lib/libvector/test_libvector.c +++ b/gear-lib/libvector/test_libvector.c @@ -45,7 +45,7 @@ void mix_struct() c = _vector_create(sizeof(struct tmp_box)); #endif vector_push_back(c, tb); - for (iter = vector_begin(c); iter != vector_end(c); iter = vector_next(c)) { + for (iter = vector_begin(c); iter != vector_end(c); iter = vector_next(c, iter)) { struct tmp_box *tt = vector_iter_valuep(c, iter, struct tmp_box); printf("vector member.c: %c\n", tt->c); printf("vector member.i: %d\n", tt->i); @@ -79,7 +79,7 @@ void default_struct() vector_push_back(a, t1); vector_push_back(a, t2); vector_push_back(a, t3); - for (iter = vector_begin(a); iter != vector_end(a); iter = vector_next(a)) { + for (iter = vector_begin(a); iter != vector_end(a); iter = vector_next(a, iter)) { printf("vector member: %d\n", *vector_iter_valuep(a, iter, int)); } for (i = 0; i < a->size; i++) {