circbuf.cpp revision 4e4a8e041182dff5c52892bee58a5d8f58f4bdad
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * IPRT - Lock Free Circular Buffer
64743f0e8609782598af145a21b5dbdd23f3853evboxsync * Copyright (C) 2011 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*******************************************************************************/
64743f0e8609782598af145a21b5dbdd23f3853evboxsync/** @todo r=bird: this is missing docs and magic. */
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsynctypedef struct RTCIRCBUF
292dc462b10ba7ef37b871434e332aecf8ad97dfvboxsync /** The current read position in the buffer. */
4e4a8e041182dff5c52892bee58a5d8f58f4bdadvboxsync /** Is a read block acquired currently? */
4e4a8e041182dff5c52892bee58a5d8f58f4bdadvboxsync /** Is a write block acquired currently? */
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. */
64743f0e8609782598af145a21b5dbdd23f3853evboxsync return pBuf->cbBuf - ASMAtomicReadZ(&pBuf->cbUsed);
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
4e4a8e041182dff5c52892bee58a5d8f58f4bdadvboxsync /* Validate input. */
4e4a8e041182dff5c52892bee58a5d8f58f4bdadvboxsync /* 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. */
64743f0e8609782598af145a21b5dbdd23f3853evboxsync size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBuf - pBuf->offRead, 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. */
64743f0e8609782598af145a21b5dbdd23f3853evboxsync pBuf->offRead = (pBuf->offRead + cbSize) % pBuf->cbBuf;
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* How much is free? */
64743f0e8609782598af145a21b5dbdd23f3853evboxsync size_t cbFree = pBuf->cbBuf - ASMAtomicReadZ(&pBuf->cbUsed);
522685dc682cf4230f5e51d601fcb95061d09e71vboxsync /* Get the size out of the requested size, then write block till the end
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * of the buffer & the currently free size. */
64743f0e8609782598af145a21b5dbdd23f3853evboxsync size_t cbSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBuf - pBuf->offWrite, 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. */