FreeRDP/libfreerdp/utils/ringbuffer.c

296 lines
6.8 KiB
C
Raw Normal View History

/**
2014-05-21 20:44:49 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
*
* Copyright 2014 Thincast Technologies GmbH
* Copyright 2014 Hardening <contact@hardening-consulting.com>
*
2014-05-21 20:44:49 +04:00
* 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
*
2014-05-21 20:44:49 +04:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-02-16 13:20:38 +03:00
#include <freerdp/config.h>
#include <freerdp/utils/ringbuffer.h>
#include <stdlib.h>
#include <string.h>
2021-06-09 15:03:34 +03:00
#include <winpr/assert.h>
#include <winpr/crt.h>
#include <freerdp/log.h>
#define TAG FREERDP_TAG("utils.ringbuffer")
#ifdef WITH_DEBUG_RINGBUFFER
#define DEBUG_RINGBUFFER(...) WLog_DBG(TAG, __VA_ARGS__)
#else
2019-11-06 17:24:51 +03:00
#define DEBUG_RINGBUFFER(...) \
do \
{ \
} while (0)
#endif
BOOL ringbuffer_init(RingBuffer* rb, size_t initialSize)
{
rb->buffer = malloc(initialSize);
if (!rb->buffer)
return FALSE;
rb->readPtr = rb->writePtr = 0;
rb->initialSize = rb->size = rb->freeSize = initialSize;
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_init(%p)", (void*)rb);
return TRUE;
}
size_t ringbuffer_used(const RingBuffer* rb)
{
return rb->size - rb->freeSize;
}
size_t ringbuffer_capacity(const RingBuffer* rb)
{
return rb->size;
}
void ringbuffer_destroy(RingBuffer* rb)
{
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_destroy(%p)", (void*)rb);
free(rb->buffer);
rb->buffer = NULL;
}
static BOOL ringbuffer_realloc(RingBuffer* rb, size_t targetSize)
{
BYTE* newData;
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_realloc(%p): targetSize: %" PRIdz "", (void*)rb, targetSize);
if (rb->writePtr == rb->readPtr)
{
/* when no size is used we can realloc() and set the heads at the
* beginning of the buffer
*/
2019-11-06 17:24:51 +03:00
newData = (BYTE*)realloc(rb->buffer, targetSize);
if (!newData)
return FALSE;
rb->readPtr = rb->writePtr = 0;
rb->buffer = newData;
}
else if ((rb->writePtr >= rb->readPtr) && (rb->writePtr < targetSize))
{
/* we reallocate only if we're in that case, realloc don't touch read
* and write heads
*
* readPtr writePtr
* | |
* v v
* [............|XXXXXXXXXXXXXX|..........]
*/
2019-11-06 17:24:51 +03:00
newData = (BYTE*)realloc(rb->buffer, targetSize);
if (!newData)
return FALSE;
rb->buffer = newData;
}
else
{
/* in case of malloc the read head is moved at the beginning of the new buffer
* and the write head is set accordingly
*/
2019-11-06 17:24:51 +03:00
newData = (BYTE*)malloc(targetSize);
if (!newData)
return FALSE;
if (rb->readPtr < rb->writePtr)
{
/* readPtr writePtr
* | |
* v v
* [............|XXXXXXXXXXXXXX|..........]
*/
memcpy(newData, rb->buffer + rb->readPtr, ringbuffer_used(rb));
}
else
{
/* writePtr readPtr
* | |
* v v
* [XXXXXXXXXXXX|..............|XXXXXXXXXX]
*/
BYTE* dst = newData;
memcpy(dst, rb->buffer + rb->readPtr, rb->size - rb->readPtr);
dst += (rb->size - rb->readPtr);
if (rb->writePtr)
memcpy(dst, rb->buffer, rb->writePtr);
}
rb->writePtr = rb->size - rb->freeSize;
rb->readPtr = 0;
free(rb->buffer);
rb->buffer = newData;
}
rb->freeSize += (targetSize - rb->size);
rb->size = targetSize;
return TRUE;
}
/**
2022-04-28 00:10:49 +03:00
* Write to a ringbuffer
*
2022-04-28 00:10:49 +03:00
* @param rb A pointer to the ringbuffer
* @param ptr A pointer to the data to write
* @param sz The number of bytes to write
*
* @return \b TRUE for success, \b FALSE for failure
*/
BOOL ringbuffer_write(RingBuffer* rb, const BYTE* ptr, size_t sz)
{
2014-05-21 20:17:39 +04:00
size_t toWrite;
size_t remaining;
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_write(%p): sz: %" PRIdz "", (void*)rb, sz);
if ((rb->freeSize <= sz) && !ringbuffer_realloc(rb, rb->size + sz))
return FALSE;
/* the write could be split in two
* readHead writeHead
* | |
* v v
* [ ################ ]
*/
2014-05-21 20:17:39 +04:00
toWrite = sz;
remaining = sz;
if (rb->size - rb->writePtr < sz)
toWrite = rb->size - rb->writePtr;
if (toWrite)
{
memcpy(rb->buffer + rb->writePtr, ptr, toWrite);
remaining -= toWrite;
ptr += toWrite;
}
if (remaining)
memcpy(rb->buffer, ptr, remaining);
rb->writePtr = (rb->writePtr + sz) % rb->size;
rb->freeSize -= sz;
return TRUE;
}
BYTE* ringbuffer_ensure_linear_write(RingBuffer* rb, size_t sz)
{
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_ensure_linear_write(%p): sz: %" PRIdz "", (void*)rb, sz);
if (rb->freeSize < sz)
{
if (!ringbuffer_realloc(rb, rb->size + sz - rb->freeSize + 32))
return NULL;
}
if (rb->writePtr == rb->readPtr)
{
2015-01-16 02:41:57 +03:00
rb->writePtr = rb->readPtr = 0;
}
if (rb->writePtr + sz < rb->size)
return rb->buffer + rb->writePtr;
/*
* to add: .......
* [ XXXXXXXXX ]
*
* result:
* [XXXXXXXXX....... ]
*/
memmove(rb->buffer, rb->buffer + rb->readPtr, rb->writePtr - rb->readPtr);
rb->readPtr = 0;
rb->writePtr = rb->size - rb->freeSize;
return rb->buffer + rb->writePtr;
}
BOOL ringbuffer_commit_written_bytes(RingBuffer* rb, size_t sz)
{
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_commit_written_bytes(%p): sz: %" PRIdz "", (void*)rb, sz);
if (sz < 1)
return TRUE;
if (rb->writePtr + sz > rb->size)
return FALSE;
rb->writePtr = (rb->writePtr + sz) % rb->size;
rb->freeSize -= sz;
return TRUE;
}
int ringbuffer_peek(const RingBuffer* rb, DataChunk chunks[2], size_t sz)
{
size_t remaining = sz;
size_t toRead;
int chunkIndex = 0;
int status = 0;
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_peek(%p): sz: %" PRIdz "", (void*)rb, sz);
if (sz < 1)
return 0;
if ((rb->size - rb->freeSize) < sz)
remaining = rb->size - rb->freeSize;
toRead = remaining;
if ((rb->readPtr + remaining) > rb->size)
toRead = rb->size - rb->readPtr;
if (toRead)
{
chunks[0].data = rb->buffer + rb->readPtr;
chunks[0].size = toRead;
remaining -= toRead;
chunkIndex++;
status++;
}
if (remaining)
{
chunks[chunkIndex].data = rb->buffer;
chunks[chunkIndex].size = remaining;
status++;
}
return status;
}
void ringbuffer_commit_read_bytes(RingBuffer* rb, size_t sz)
{
2019-11-06 17:24:51 +03:00
DEBUG_RINGBUFFER("ringbuffer_commit_read_bytes(%p): sz: %" PRIdz "", (void*)rb, sz);
if (sz < 1)
return;
2021-06-09 15:03:34 +03:00
WINPR_ASSERT(rb->size - rb->freeSize >= sz);
rb->readPtr = (rb->readPtr + sz) % rb->size;
rb->freeSize += sz;
/* when we reach a reasonable free size, we can go back to the original size */
2019-11-06 17:24:51 +03:00
if ((rb->size != rb->initialSize) && (ringbuffer_used(rb) < rb->initialSize / 2))
ringbuffer_realloc(rb, rb->initialSize);
}