diff --git a/winpr/include/winpr/collections.h b/winpr/include/winpr/collections.h index c775c1bee..d5cdca560 100644 --- a/winpr/include/winpr/collections.h +++ b/winpr/include/winpr/collections.h @@ -102,6 +102,9 @@ WINPR_API BOOL ArrayList_IsFixedSized(wArrayList* arrayList); WINPR_API BOOL ArrayList_IsReadOnly(wArrayList* arrayList); WINPR_API BOOL ArrayList_IsSynchronized(wArrayList* arrayList); +WINPR_API BOOL ArrayList_Lock(wArrayList* arrayList); +WINPR_API BOOL ArrayList_Unlock(wArrayList* arrayList); + WINPR_API void* ArrayList_GetItem(wArrayList* arrayList, int index); WINPR_API void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj); @@ -112,7 +115,7 @@ WINPR_API int ArrayList_Add(wArrayList* arrayList, void* obj); WINPR_API void ArrayList_Insert(wArrayList* arrayList, int index, void* obj); WINPR_API void ArrayList_Remove(wArrayList* arrayList, void* obj); -WINPR_API void ArrayList_RemoveAt(wArrayList* arrayList, int index, void* obj); +WINPR_API void ArrayList_RemoveAt(wArrayList* arrayList, int index); WINPR_API int ArrayList_IndexOf(wArrayList* arrayList, void* obj, int startIndex, int count); WINPR_API int ArrayList_LastIndexOf(wArrayList* arrayList, void* obj, int startIndex, int count); diff --git a/winpr/libwinpr/utils/collections/ArrayList.c b/winpr/libwinpr/utils/collections/ArrayList.c index d91baf6e3..bde6520e1 100644 --- a/winpr/libwinpr/utils/collections/ArrayList.c +++ b/winpr/libwinpr/utils/collections/ArrayList.c @@ -79,6 +79,24 @@ BOOL ArrayList_IsSynchronized(wArrayList* arrayList) return arrayList->synchronized; } +/** + * Lock access to the ArrayList + */ + +BOOL ArrayList_Lock(wArrayList* arrayList) +{ + return (WaitForSingleObject(arrayList->mutex, INFINITE) == WAIT_OBJECT_0) ? TRUE : FALSE; +} + +/** + * Unlock access to the ArrayList + */ + +BOOL ArrayList_Unlock(wArrayList* arrayList) +{ + return ReleaseMutex(arrayList->mutex); +} + /** * Gets the element at the specified index. */ @@ -87,17 +105,11 @@ void* ArrayList_GetItem(wArrayList* arrayList, int index) { void* obj = NULL; - if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); - if ((index >= 0) && (index < arrayList->size)) { obj = arrayList->array[index]; } - if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); - return obj; } @@ -107,16 +119,10 @@ void* ArrayList_GetItem(wArrayList* arrayList, int index) void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj) { - if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); - if ((index >= 0) && (index < arrayList->size)) { arrayList->array[index] = obj; } - - if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); } /** @@ -129,9 +135,6 @@ void ArrayList_SetItem(wArrayList* arrayList, int index, void* obj) void ArrayList_Shift(wArrayList* arrayList, int index, int count) { - if (arrayList->synchronized) - WaitForSingleObject(arrayList->mutex, INFINITE); - if (count > 0) { if (arrayList->size + count > arrayList->capacity) @@ -140,17 +143,14 @@ void ArrayList_Shift(wArrayList* arrayList, int index, int count) arrayList->array = (void**) realloc(arrayList->array, sizeof(void*) * arrayList->capacity); } - MoveMemory(&arrayList->array[index + count], &arrayList->array[index], count); + MoveMemory(&arrayList->array[index + count], &arrayList->array[index], (arrayList->size - index) * sizeof(void*)); arrayList->size += count; } else if (count < 0) { - MoveMemory(&arrayList->array[index + count], &arrayList->array[index], count); + MoveMemory(&arrayList->array[index + count], &arrayList->array[index], (arrayList->size - index) * sizeof(void*)); arrayList->size += count; } - - if (arrayList->synchronized) - ReleaseMutex(arrayList->mutex); } /** @@ -252,7 +252,7 @@ void ArrayList_Remove(wArrayList* arrayList, void* obj) * Removes the element at the specified index of the ArrayList. */ -void ArrayList_RemoveAt(wArrayList* arrayList, int index, void* obj) +void ArrayList_RemoveAt(wArrayList* arrayList, int index) { if (arrayList->synchronized) WaitForSingleObject(arrayList->mutex, INFINITE); diff --git a/winpr/libwinpr/utils/test/CMakeLists.txt b/winpr/libwinpr/utils/test/CMakeLists.txt index d34853252..e5d6dc04c 100644 --- a/winpr/libwinpr/utils/test/CMakeLists.txt +++ b/winpr/libwinpr/utils/test/CMakeLists.txt @@ -6,6 +6,7 @@ set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c) set(${MODULE_PREFIX}_TESTS TestQueue.c + TestArrayList.c TestCmdLine.c) create_test_sourcelist(${MODULE_PREFIX}_SRCS diff --git a/winpr/libwinpr/utils/test/TestArrayList.c b/winpr/libwinpr/utils/test/TestArrayList.c new file mode 100644 index 000000000..59a8e7e72 --- /dev/null +++ b/winpr/libwinpr/utils/test/TestArrayList.c @@ -0,0 +1,50 @@ + +#include +#include +#include + +int TestArrayList(int argc, char* argv[]) +{ + int index; + int count; + wArrayList* arrayList; + + arrayList = ArrayList_New(TRUE); + + for (index = 0; index < 10; index++) + { + ArrayList_Add(arrayList, (void*) (size_t) index); + } + + count = ArrayList_Count(arrayList); + + printf("ArrayList count: %d\n", count); + + index = ArrayList_IndexOf(arrayList, (void*) (size_t) 6, -1, -1); + + printf("ArrayList index: %d\n", index); + + if (index != 6) + return -1; + + ArrayList_Insert(arrayList, 5, (void*) (size_t) 100); + + index = ArrayList_IndexOf(arrayList, (void*) (size_t) 6, -1, -1); + printf("ArrayList index: %d\n", index); + + if (index != 7) + return -1; + + ArrayList_Remove(arrayList, (void*) (size_t) 100); + + index = ArrayList_IndexOf(arrayList, (void*) (size_t) 6, -1, -1); + printf("ArrayList index: %d\n", index); + + if (index != 6) + return -1; + + ArrayList_Clear(arrayList); + ArrayList_Free(arrayList); + + return 0; +}