circbuf.cpp revision 292dc462b10ba7ef37b871434e332aecf8ad97df
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * IPRT - Lock Free Circular Buffer
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * Copyright (C) 2010 Oracle Corporation
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * available from http://www.virtualbox.org. This file is free software;
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * you can redistribute it and/or modify it under the terms of the GNU
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * General Public License (GPL) as published by the Free Software
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * The contents of this file may alternatively be used under the terms
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * of the Common Development and Distribution License Version 1.0
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * VirtualBox OSE distribution, in which case the provisions of the
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * CDDL are applicable instead of those of the GPL.
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * You may elect to license modified versions of this file under the
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * terms and conditions of either the GPL or the CDDL or both.
078edd33e3a22f968a7aee5a9e042bca314c2f03vboxsync/*******************************************************************************
078edd33e3a22f968a7aee5a9e042bca314c2f03vboxsync* Header Files *
078edd33e3a22f968a7aee5a9e042bca314c2f03vboxsync*******************************************************************************/
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync/*******************************************************************************
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync* Structures and Typedefs *
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync*******************************************************************************/
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync/** @todo r=bird: this is missing docs and magic. uXPos should be offX.
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync * cbBufSize should be cbBuf. */
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsynctypedef struct RTCIRCBUF
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync /** The current read position in the buffer. */
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync /** The current write position in the buffer. */
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync /** How much space of the buffer is currently in use. */
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync /** How big is the buffer. */
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync /** The buffer itself. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
41802163cf830caf53a1a619fed55afb52246eacvboxsync return pBuf->cbBufSize - ASMAtomicReadZ(&pBuf->cbBufUsed);
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(void) RTCircBufAcquireReadBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* How much is in use? */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Get the size out of the requested size, the read block till the end
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * of the buffer & the currently used size. */
41802163cf830caf53a1a619fed55afb52246eacvboxsync size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, cbUsed));
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Return the pointer address which point to the current read
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * position. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(void) RTCircBufReleaseReadBlock(PRTCIRCBUF pBuf, size_t cbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Split at the end of the buffer. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync pBuf->uReadPos = (pBuf->uReadPos + cbSize) % pBuf->cbBufSize;
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* How much is free? */
41802163cf830caf53a1a619fed55afb52246eacvboxsync size_t cbFree = pBuf->cbBufSize - ASMAtomicReadZ(&pBuf->cbBufUsed);
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Get the size out of the requested size, the write block till the end
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * of the buffer & the currently free size. */
41802163cf830caf53a1a619fed55afb52246eacvboxsync size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, cbFree));
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Return the pointer address which point to the current write
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * position. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(void) RTCircBufReleaseWriteBlock(PRTCIRCBUF pBuf, size_t cbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Split at the end of the buffer. */