diff --git a/winpr/include/winpr/collections.h b/winpr/include/winpr/collections.h index 40c8abc99..b1bce8b68 100644 --- a/winpr/include/winpr/collections.h +++ b/winpr/include/winpr/collections.h @@ -568,29 +568,6 @@ extern "C" WINPR_API wPubSub* PubSub_New(BOOL synchronized); WINPR_API void PubSub_Free(wPubSub* pubSub); - /* BipBuffer */ - typedef struct s_wBipBuffer wBipBuffer; - - WINPR_API BOOL BipBuffer_Grow(wBipBuffer* bb, size_t size); - WINPR_API void BipBuffer_Clear(wBipBuffer* bb); - - WINPR_API size_t BipBuffer_UsedSize(wBipBuffer* bb); - WINPR_API size_t BipBuffer_BufferSize(wBipBuffer* bb); - - WINPR_API BYTE* BipBuffer_WriteReserve(wBipBuffer* bb, size_t size); - WINPR_API BYTE* BipBuffer_WriteTryReserve(wBipBuffer* bb, size_t size, size_t* reserved); - WINPR_API void BipBuffer_WriteCommit(wBipBuffer* bb, size_t size); - - WINPR_API BYTE* BipBuffer_ReadReserve(wBipBuffer* bb, size_t size); - WINPR_API BYTE* BipBuffer_ReadTryReserve(wBipBuffer* bb, size_t size, size_t* reserved); - WINPR_API void BipBuffer_ReadCommit(wBipBuffer* bb, size_t size); - - WINPR_API SSIZE_T BipBuffer_Read(wBipBuffer* bb, BYTE* data, size_t size); - WINPR_API SSIZE_T BipBuffer_Write(wBipBuffer* bb, const BYTE* data, size_t size); - - WINPR_API wBipBuffer* BipBuffer_New(size_t size); - WINPR_API void BipBuffer_Free(wBipBuffer* bb); - #ifdef __cplusplus } #endif diff --git a/winpr/libwinpr/utils/CMakeLists.txt b/winpr/libwinpr/utils/CMakeLists.txt index 1c8b42b17..62ff18f9f 100644 --- a/winpr/libwinpr/utils/CMakeLists.txt +++ b/winpr/libwinpr/utils/CMakeLists.txt @@ -23,8 +23,7 @@ set(COLLECTIONS_SRCS collections/Queue.c collections/Stack.c collections/PubSub.c - collections/BipBuffer.c - collections/BitStream.c + collections/BitStream.c collections/Reference.c collections/ArrayList.c collections/LinkedList.c diff --git a/winpr/libwinpr/utils/collections/BipBuffer.c b/winpr/libwinpr/utils/collections/BipBuffer.c deleted file mode 100644 index da3c8c78e..000000000 --- a/winpr/libwinpr/utils/collections/BipBuffer.c +++ /dev/null @@ -1,453 +0,0 @@ -/** - * WinPR: Windows Portable Runtime - * BipBuffer - * - * Copyright 2014 Marc-Andre Moreau - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -#include - -#include - -typedef struct -{ - size_t index; - size_t size; -} wBipBlock; - -struct s_wBipBuffer -{ - size_t size; - BYTE* buffer; - size_t pageSize; - wBipBlock blockA; - wBipBlock blockB; - wBipBlock readR; - wBipBlock writeR; -}; - -/** - * The Bip Buffer - The Circular Buffer with a Twist: - * http://www.codeproject.com/Articles/3479/The-Bip-Buffer-The-Circular-Buffer-with-a-Twist - */ - -static INLINE void BipBlock_Clear(wBipBlock* _bbl) -{ - _bbl->index = _bbl->size = 0; -} - -static INLINE void BipBlock_Copy(wBipBlock* _dst, const wBipBlock* _src) -{ - _dst->index = _src->index; - _dst->size = _src->size; -} - -void BipBuffer_Clear(wBipBuffer* bb) -{ - BipBlock_Clear(&bb->blockA); - BipBlock_Clear(&bb->blockB); - BipBlock_Clear(&bb->readR); - BipBlock_Clear(&bb->writeR); -} - -static BOOL BipBuffer_AllocBuffer(wBipBuffer* bb, size_t size) -{ - if (size < 1) - return FALSE; - - size += size % bb->pageSize; - bb->buffer = (BYTE*)malloc(size); - - if (!bb->buffer) - return FALSE; - - bb->size = size; - return TRUE; -} - -BOOL BipBuffer_Grow(wBipBuffer* bb, size_t size) -{ - BYTE* block; - BYTE* buffer; - size_t blockSize = 0; - size_t commitSize = 0; - size += size % bb->pageSize; - - if (size <= bb->size) - return TRUE; - - buffer = (BYTE*)malloc(size); - - if (!buffer) - return FALSE; - - block = BipBuffer_ReadTryReserve(bb, 0, &blockSize); - - if (block) - { - CopyMemory(&buffer[commitSize], block, blockSize); - BipBuffer_ReadCommit(bb, blockSize); - commitSize += blockSize; - } - - block = BipBuffer_ReadTryReserve(bb, 0, &blockSize); - - if (block) - { - CopyMemory(&buffer[commitSize], block, blockSize); - BipBuffer_ReadCommit(bb, blockSize); - commitSize += blockSize; - } - - BipBuffer_Clear(bb); - free(bb->buffer); - bb->buffer = buffer; - bb->size = size; - bb->blockA.index = 0; - bb->blockA.size = commitSize; - return TRUE; -} - -static void BipBuffer_FreeBuffer(wBipBuffer* bb) -{ - if (bb->buffer) - { - free(bb->buffer); - bb->buffer = NULL; - } - - BipBuffer_Clear(bb); -} - -size_t BipBuffer_UsedSize(wBipBuffer* bb) -{ - return bb->blockA.size + bb->blockB.size; -} - -size_t BipBuffer_BufferSize(wBipBuffer* bb) -{ - return bb->size; -} - -BYTE* BipBuffer_WriteTryReserve(wBipBuffer* bb, size_t size, size_t* reserved) -{ - size_t reservable; - - if (!reserved) - return NULL; - - if (!bb->blockB.size) - { - /* block B does not exist */ - reservable = bb->size - bb->blockA.index - bb->blockA.size; /* space after block A */ - - if (reservable >= bb->blockA.index) - { - if (reservable == 0) - return NULL; - - if (size < reservable) - reservable = size; - - bb->writeR.size = reservable; - *reserved = reservable; - bb->writeR.index = bb->blockA.index + bb->blockA.size; - return &bb->buffer[bb->writeR.index]; - } - - if (bb->blockA.index == 0) - return NULL; - - if (bb->blockA.index < size) - size = bb->blockA.index; - - bb->writeR.size = size; - *reserved = size; - bb->writeR.index = 0; - return bb->buffer; - } - - /* block B exists */ - reservable = bb->blockA.index - bb->blockB.index - bb->blockB.size; /* space after block B */ - - if (size < reservable) - reservable = size; - - if (reservable == 0) - return NULL; - - bb->writeR.size = reservable; - *reserved = reservable; - bb->writeR.index = bb->blockB.index + bb->blockB.size; - return &bb->buffer[bb->writeR.index]; -} - -BYTE* BipBuffer_WriteReserve(wBipBuffer* bb, size_t size) -{ - BYTE* block = NULL; - size_t reserved = 0; - block = BipBuffer_WriteTryReserve(bb, size, &reserved); - - if (reserved == size) - return block; - - if (!BipBuffer_Grow(bb, size)) - return NULL; - - block = BipBuffer_WriteTryReserve(bb, size, &reserved); - return block; -} - -void BipBuffer_WriteCommit(wBipBuffer* bb, size_t size) -{ - if (size == 0) - { - BipBlock_Clear(&bb->writeR); - return; - } - - if (size > bb->writeR.size) - size = bb->writeR.size; - - if ((bb->blockA.size == 0) && (bb->blockB.size == 0)) - { - bb->blockA.index = bb->writeR.index; - bb->blockA.size = size; - BipBlock_Clear(&bb->writeR); - return; - } - - if (bb->writeR.index == (bb->blockA.size + bb->blockA.index)) - bb->blockA.size += size; - else - bb->blockB.size += size; - - BipBlock_Clear(&bb->writeR); -} - -SSIZE_T BipBuffer_Write(wBipBuffer* bb, const BYTE* data, size_t size) -{ - size_t status = 0; - BYTE* block = NULL; - size_t writeSize = 0; - size_t blockSize = 0; - - if (size == 0) - return 0; - - if (!bb || !data) - return -1; - - if (size > SSIZE_MAX) - size = SSIZE_MAX; - - block = BipBuffer_WriteReserve(bb, size); - - if (!block) - return -1; - - block = BipBuffer_WriteTryReserve(bb, size - status, &blockSize); - - if (block) - { - writeSize = size - status; - - if (writeSize > blockSize) - writeSize = blockSize; - - CopyMemory(block, &data[status], writeSize); - BipBuffer_WriteCommit(bb, writeSize); - status += writeSize; - - if ((status == size) || (writeSize < blockSize)) - return (SSIZE_T)status; - } - - block = BipBuffer_WriteTryReserve(bb, size - status, &blockSize); - - if (block) - { - writeSize = size - status; - - if (writeSize > blockSize) - writeSize = blockSize; - - CopyMemory(block, &data[status], writeSize); - BipBuffer_WriteCommit(bb, writeSize); - status += writeSize; - - if ((status == size) || (writeSize < blockSize)) - return (SSIZE_T)status; - } - - return (SSIZE_T)status; -} - -BYTE* BipBuffer_ReadTryReserve(wBipBuffer* bb, size_t size, size_t* reserved) -{ - size_t reservable = 0; - - if (!reserved) - return NULL; - - if (bb->blockA.size == 0) - { - *reserved = 0; - return NULL; - } - - reservable = bb->blockA.size; - - if (size && (reservable > size)) - reservable = size; - - *reserved = reservable; - return &bb->buffer[bb->blockA.index]; -} - -BYTE* BipBuffer_ReadReserve(wBipBuffer* bb, size_t size) -{ - BYTE* block = NULL; - size_t reserved = 0; - - if (BipBuffer_UsedSize(bb) < size) - return NULL; - - block = BipBuffer_ReadTryReserve(bb, size, &reserved); - - if (reserved == size) - return block; - - if (!BipBuffer_Grow(bb, bb->size + 1)) - return NULL; - - block = BipBuffer_ReadTryReserve(bb, size, &reserved); - - if (reserved != size) - return NULL; - - return block; -} - -void BipBuffer_ReadCommit(wBipBuffer* bb, size_t size) -{ - if (!bb) - return; - - if (size >= bb->blockA.size) - { - BipBlock_Copy(&bb->blockA, &bb->blockB); - BipBlock_Clear(&bb->blockB); - } - else - { - bb->blockA.size -= size; - bb->blockA.index += size; - } -} - -SSIZE_T BipBuffer_Read(wBipBuffer* bb, BYTE* data, size_t size) -{ - size_t status = 0; - BYTE* block = NULL; - size_t readSize = 0; - size_t blockSize = 0; - - if (size == 0) - return 0; - - if (!bb || !data) - return -1; - - if (size > SSIZE_MAX) - size = SSIZE_MAX; - - block = BipBuffer_ReadTryReserve(bb, 0, &blockSize); - - if (block) - { - readSize = size - status; - - if (readSize > blockSize) - readSize = blockSize; - - CopyMemory(&data[status], block, readSize); - BipBuffer_ReadCommit(bb, readSize); - status += readSize; - - if ((status == size) || (readSize < blockSize)) - return (SSIZE_T)status; - } - - block = BipBuffer_ReadTryReserve(bb, 0, &blockSize); - - if (block) - { - readSize = size - status; - - if (readSize > blockSize) - readSize = blockSize; - - CopyMemory(&data[status], block, readSize); - BipBuffer_ReadCommit(bb, readSize); - status += readSize; - - if ((status == size) || (readSize < blockSize)) - return (SSIZE_T)status; - } - - return (SSIZE_T)status; -} - -/** - * Construction, Destruction - */ - -wBipBuffer* BipBuffer_New(size_t size) -{ - wBipBuffer* bb; - bb = (wBipBuffer*)calloc(1, sizeof(wBipBuffer)); - - if (bb) - { - SYSTEM_INFO si; - GetSystemInfo(&si); - bb->pageSize = (size_t)si.dwPageSize; - - if (bb->pageSize < 4096) - bb->pageSize = 4096; - - if (!BipBuffer_AllocBuffer(bb, size)) - { - free(bb); - return NULL; - } - } - - return bb; -} - -void BipBuffer_Free(wBipBuffer* bb) -{ - if (!bb) - return; - - BipBuffer_FreeBuffer(bb); - free(bb); -} diff --git a/winpr/libwinpr/utils/test/CMakeLists.txt b/winpr/libwinpr/utils/test/CMakeLists.txt index 186b0dca3..b7ab0f3b9 100644 --- a/winpr/libwinpr/utils/test/CMakeLists.txt +++ b/winpr/libwinpr/utils/test/CMakeLists.txt @@ -8,7 +8,6 @@ set(${MODULE_PREFIX}_TESTS TestIni.c TestVersion.c TestImage.c - TestBipBuffer.c TestBacktrace.c TestQueue.c TestPrint.c diff --git a/winpr/libwinpr/utils/test/TestBipBuffer.c b/winpr/libwinpr/utils/test/TestBipBuffer.c deleted file mode 100644 index 8f9dccb73..000000000 --- a/winpr/libwinpr/utils/test/TestBipBuffer.c +++ /dev/null @@ -1,27 +0,0 @@ - -#include -#include - -int TestBipBuffer(int argc, char* argv[]) -{ - BYTE* data; - int rc = -1; - wBipBuffer* bb; - - WINPR_UNUSED(argc); - WINPR_UNUSED(argv); - - bb = BipBuffer_New(1024); - - if (!bb) - return -1; - - data = BipBuffer_WriteReserve(bb, 1024 * 2); - - if (data) - rc = 0; - - fprintf(stderr, "BipBuffer_BufferSize: %" PRIuz "\n", BipBuffer_BufferSize(bb)); - BipBuffer_Free(bb); - return rc; -}