[winpr,stream] implement reference counting for streams

This commit is contained in:
akallabeth
2025-02-27 17:37:15 +01:00
parent dcebd0cb2b
commit ec030e8255
3 changed files with 28 additions and 15 deletions

View File

@@ -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)

View File

@@ -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);
}
}
/**

View File

@@ -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;