circbuf.cpp revision 078edd33e3a22f968a7aee5a9e042bca314c2f03
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/* $Id$ */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/** @file
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * IPRT - Lock Free Circular Buffer
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/*
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Copyright (C) 2010 Oracle Corporation
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * available from http://www.virtualbox.org. This file is free software;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * you can redistribute it and/or modify it under the terms of the GNU
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * General Public License (GPL) as published by the Free Software
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * The contents of this file may alternatively be used under the terms
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * of the Common Development and Distribution License Version 1.0
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * VirtualBox OSE distribution, in which case the provisions of the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * CDDL are applicable instead of those of the GPL.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * You may elect to license modified versions of this file under the
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync * terms and conditions of either the GPL or the CDDL or both.
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync/*******************************************************************************
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync* Header Files *
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync*******************************************************************************/
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/circbuf.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/mem.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/assert.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/asm.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync#include <iprt/err.h>
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsyncRTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /* Validate input. */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertPtrReturn(ppBuf, VERR_INVALID_POINTER);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertReturn(cbSize > 0, VERR_INVALID_PARAMETER);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync PRTCIRCBUF pTmpBuf;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
d85b0881fbf23d90ba93e32becf11d87dbc3f9dbvboxsync if (!pTmpBuf)
d85b0881fbf23d90ba93e32becf11d87dbc3f9dbvboxsync return VERR_NO_MEMORY;
d85b0881fbf23d90ba93e32becf11d87dbc3f9dbvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync pTmpBuf->pvBuf = RTMemAlloc(cbSize);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync if (pTmpBuf->pvBuf)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync {
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync pTmpBuf->cbBufSize = cbSize;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync *ppBuf = pTmpBuf;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return VINF_SUCCESS;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync }
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync RTMemFree(pTmpBuf);
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return VERR_NO_MEMORY;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync}
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsyncRTDECL(void) RTCircBufDestroy(PRTCIRCBUF pBuf)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync{
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync /* Validate input. */
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync if (!pBuf)
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync return;
9fc464631dc4a68fbb5eb6419d61fbe91b6b16bdvboxsync AssertPtr(pBuf);
62b8247e4970b835e263b4c18a2748bf651fabd0vboxsync RTMemFree(pBuf->pvBuf);
RTMemFree(pBuf);
}
RTDECL(void) RTCircBufReset(PRTCIRCBUF pBuf)
{
/* Validate input. */
AssertPtr(pBuf);
pBuf->uReadPos = 0;
pBuf->uWritePos = 0;
pBuf->cbBufUsed = 0;
}
RTDECL(size_t) RTCircBufFree(PRTCIRCBUF pBuf)
{
/* Validate input. */
AssertPtrReturn(pBuf, 0);
size_t cbSize = 0;
ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
return pBuf->cbBufSize - cbSize;
}
RTDECL(size_t) RTCircBufUsed(PRTCIRCBUF pBuf)
{
/* Validate input. */
AssertPtrReturn(pBuf, 0);
size_t cbSize = 0;
ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
return cbSize;
}
RTDECL(size_t) RTCircBufSize(PRTCIRCBUF pBuf)
{
/* Validate input. */
AssertPtrReturn(pBuf, 0);
return pBuf->cbBufSize;
}
RTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
{
/* Validate input. */
AssertPtr(pBuf);
Assert(cbReqSize > 0);
AssertPtr(ppvStart);
AssertPtr(pcbSize);
size_t uUsed = 0;
size_t uSize = 0;
*ppvStart = 0;
*pcbSize = 0;
/* How much is in use? */
ASMAtomicReadSize(&pBuf->cbBufUsed, &uUsed);
if (uUsed > 0)
{
/* Get the size out of the requested size, the read block till the end
* of the buffer & the currently used size. */
uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, uUsed));
if (uSize > 0)
{
/* Return the pointer address which point to the current read
* position. */
*ppvStart = (char *)pBuf->pvBuf + pBuf->uReadPos;
*pcbSize = uSize;
}
}
}
RTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
{
/* Validate input. */
AssertPtr(pBuf);
/* Split at the end of the buffer. */
pBuf->uReadPos = (pBuf->uReadPos + cbSize) % pBuf->cbBufSize;
size_t cbOld = 0;
ASMAtomicSubSize(&pBuf->cbBufUsed, cbSize, &cbOld);
}
RTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
{
/* Validate input. */
AssertPtr(pBuf);
Assert(cbReqSize > 0);
AssertPtr(ppvStart);
AssertPtr(pcbSize);
*ppvStart = 0;
*pcbSize = 0;
/* How much is free? */
size_t cbSize = 0;
ASMAtomicReadSize(&pBuf->cbBufUsed, &cbSize);
size_t uFree = pBuf->cbBufSize - cbSize;
if (uFree > 0)
{
/* Get the size out of the requested size, the write block till the end
* of the buffer & the currently free size. */
size_t uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, uFree));
if (uSize > 0)
{
/* Return the pointer address which point to the current write
* position. */
*ppvStart = (char*)pBuf->pvBuf + pBuf->uWritePos;
*pcbSize = uSize;
}
}
}
RTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
{
/* Validate input. */
AssertPtr(pBuf);
/* Split at the end of the buffer. */
pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;
size_t cbOldIgnored = 0;
ASMAtomicAddSize(&pBuf->cbBufUsed, cbSize, &cbOldIgnored);
}