circbuf.cpp revision 97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421
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.
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync/******************************************************************************
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * Header Files *
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync ******************************************************************************/
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync/******************************************************************************
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * Public Functions *
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync ******************************************************************************/
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(int) RTCircBufCreate(PRTCIRCBUF *ppBuf, size_t cbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync pTmpBuf = (PRTCIRCBUF)RTMemAllocZ(sizeof(RTCIRCBUF));
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync }while (0);
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
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. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uReadPos, uUsed));
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;
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync ASMAtomicSubSize(&pBuf->cbBufUsed, cbSize, &cbOld);
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsyncRTDECL(void) RTCircBufAcquireWriteBlock(PRTCIRCBUF pBuf, size_t cbReqSize, void **ppvStart, size_t *pcbSize)
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Validate input. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* How much is free? */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync /* Get the size out of the requested size, the write block till the end
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync * of the buffer & the currently free size. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync uSize = RT_MIN(cbReqSize, RT_MIN(pBuf->cbBufSize - pBuf->uWritePos, uFree));
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. */
97f795f0be0c10aae9b31cc8f8d5cdf8ea2ab421vboxsync pBuf->uWritePos = (pBuf->uWritePos + cbSize) % pBuf->cbBufSize;