mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-14 00:14:11 +09:00
[winpr,stream] implement reference counting for streams
This commit is contained in:
@@ -1368,7 +1368,20 @@ extern "C"
|
||||
|
||||
WINPR_API void StreamPool_Return(wStreamPool* pool, wStream* s);
|
||||
|
||||
/** @brief increment reference count of stream
|
||||
*
|
||||
* @param s The stream to reference
|
||||
* @bug versions < 3.13.0 did only handle streams returned by StreamPool_Take
|
||||
*/
|
||||
WINPR_API void Stream_AddRef(wStream* s);
|
||||
|
||||
/** @brief Release a reference to a stream.
|
||||
* If the reference count reaches \b 0 it is returned to the StreamPool it was taken from or \b
|
||||
* Stream_Free is called.
|
||||
*
|
||||
* @param s The stream to release
|
||||
* @bug versions < 3.13.0 did only handle streams returned by StreamPool_Take
|
||||
*/
|
||||
WINPR_API void Stream_Release(wStream* s);
|
||||
|
||||
WINPR_ATTR_MALLOC(Stream_Release, 1)
|
||||
|
||||
@@ -285,10 +285,7 @@ static void StreamPool_Remove(wStreamPool* pool, wStream* s)
|
||||
static void StreamPool_ReleaseOrReturn(wStreamPool* pool, wStream* s)
|
||||
{
|
||||
StreamPool_Lock(pool);
|
||||
if (s->count > 0)
|
||||
s->count--;
|
||||
if (s->count == 0)
|
||||
StreamPool_Remove(pool, s);
|
||||
StreamPool_Remove(pool, s);
|
||||
StreamPool_Unlock(pool);
|
||||
}
|
||||
|
||||
@@ -310,12 +307,7 @@ void StreamPool_Return(wStreamPool* pool, wStream* s)
|
||||
void Stream_AddRef(wStream* s)
|
||||
{
|
||||
WINPR_ASSERT(s);
|
||||
if (s->pool)
|
||||
{
|
||||
StreamPool_Lock(s->pool);
|
||||
s->count++;
|
||||
StreamPool_Unlock(s->pool);
|
||||
}
|
||||
s->count++;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -325,8 +317,16 @@ void Stream_AddRef(wStream* s)
|
||||
void Stream_Release(wStream* s)
|
||||
{
|
||||
WINPR_ASSERT(s);
|
||||
if (s->pool)
|
||||
StreamPool_ReleaseOrReturn(s->pool, s);
|
||||
|
||||
if (s->count > 0)
|
||||
s->count--;
|
||||
if (s->count == 0)
|
||||
{
|
||||
if (s->pool)
|
||||
StreamPool_ReleaseOrReturn(s->pool, s);
|
||||
else
|
||||
Stream_Free(s, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -98,7 +98,7 @@ wStream* Stream_New(BYTE* buffer, size_t size)
|
||||
if (!buffer && !size)
|
||||
return NULL;
|
||||
|
||||
s = malloc(sizeof(wStream));
|
||||
s = calloc(1, sizeof(wStream));
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
@@ -118,7 +118,7 @@ wStream* Stream_New(BYTE* buffer, size_t size)
|
||||
s->length = size;
|
||||
|
||||
s->pool = NULL;
|
||||
s->count = 0;
|
||||
s->count = 1;
|
||||
s->isAllocatedStream = TRUE;
|
||||
s->isOwner = TRUE;
|
||||
return s;
|
||||
@@ -147,7 +147,7 @@ wStream* Stream_StaticInit(wStream* s, BYTE* buffer, size_t size)
|
||||
s->buffer = s->pointer = buffer;
|
||||
s->capacity = s->length = size;
|
||||
s->pool = NULL;
|
||||
s->count = 0;
|
||||
s->count = 1;
|
||||
s->isAllocatedStream = FALSE;
|
||||
s->isOwner = FALSE;
|
||||
return s;
|
||||
|
||||
Reference in New Issue
Block a user