wStream* StreamPool_Take(wStreamPool* pool, size_t size) { int index; int foundIndex; wStream* s = NULL; if (pool->synchronized) EnterCriticalSection(&pool->lock); if (size == 0) size = pool->defaultSize; foundIndex = -1; for (index = 0; index < pool->aSize; index++) { s = pool->aArray[index]; if (Stream_Capacity(s) >= size) { foundIndex = index; break; } } if (foundIndex < 0) { s = Stream_New(NULL, size); if (!s) goto out_fail; } else { Stream_SetPosition(s, 0); Stream_SetLength(s, Stream_Capacity(s)); StreamPool_ShiftAvailable(pool, foundIndex, -1); } if (s) { s->pool = pool; s->count = 1; StreamPool_AddUsed(pool, s); } out_fail: if (pool->synchronized) LeaveCriticalSection(&pool->lock); return s; }
wStream* StreamPool_Take(wStreamPool* pool, size_t size) { int index; int foundIndex; wStream* s = NULL; BOOL found = FALSE; if (pool->synchronized) WaitForSingleObject(pool->mutex, INFINITE); if (size == 0) size = pool->defaultSize; for (index = 0; index < pool->aSize; index++) { s = pool->aArray[index]; if (Stream_Capacity(s) >= size) { foundIndex = index; found = TRUE; break; } } if (!found) { s = Stream_New(NULL, size); } else { StreamPool_ShiftAvailable(pool, foundIndex, -1); Stream_EnsureCapacity(s, size); Stream_Pointer(s) = Stream_Buffer(s); } s->pool = pool; s->count = 1; StreamPool_AddUsed(pool, s); if (pool->synchronized) ReleaseMutex(pool->mutex); return s; }
wStream* StreamPool_Take(wStreamPool* pool, size_t size) { int index; int foundIndex; wStream* s = NULL; BOOL found = FALSE; if (pool->synchronized) EnterCriticalSection(&pool->lock); if (size == 0) size = pool->defaultSize; for (index = 0; index < pool->aSize; index++) { s = pool->aArray[index]; if (Stream_Capacity(s) >= size) { foundIndex = index; found = TRUE; break; } } if (!found) { s = Stream_New(NULL, size); } else { StreamPool_ShiftAvailable(pool, foundIndex, -1); Stream_EnsureCapacity(s, size); Stream_Pointer(s) = Stream_Buffer(s); } s->pool = pool; s->count = 1; StreamPool_AddUsed(pool, s); if (pool->synchronized) LeaveCriticalSection(&pool->lock); return s; }