From 37fbc3b19da1677b5e7000af48e477cabd8a2ee7 Mon Sep 17 00:00:00 2001 From: Marty Kraimer Date: Fri, 15 Dec 2000 16:35:17 +0000 Subject: [PATCH] epicsRingBytes replaces osiRing --- src/libCom/Makefile | 4 +- src/libCom/ring/epicsRingBytes.c | 177 +++++++++++++++++++++++++++++++ src/libCom/ring/epicsRingBytes.h | 44 ++++++++ 3 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 src/libCom/ring/epicsRingBytes.c create mode 100644 src/libCom/ring/epicsRingBytes.h diff --git a/src/libCom/Makefile b/src/libCom/Makefile index 6ec9a7bac..a9c1e5b5b 100644 --- a/src/libCom/Makefile +++ b/src/libCom/Makefile @@ -13,10 +13,12 @@ INC += bucketLib.h SRCS += bucketLib.c SRC_DIRS += $(LIBCOM)/ring -#following needed for locating epicsRingPointer.h +#following needed for locating epicsRingPointer.h and epicsRingBytes.h USR_CFLAGS += -I$(LIBCOM)/ring INC += epicsRingPointer.h +INC += epicsRingBytes.h SRCS += epicsRingPointer.cpp +SRCS += epicsRingBytes.cpp SRC_DIRS += $(LIBCOM)/calc #following needed for locating postfixPvt.h and sCalcPostfixPvt.h diff --git a/src/libCom/ring/epicsRingBytes.c b/src/libCom/ring/epicsRingBytes.c new file mode 100644 index 000000000..ded980847 --- /dev/null +++ b/src/libCom/ring/epicsRingBytes.c @@ -0,0 +1,177 @@ +/* epicsRingBytes.cd */ + +/* Author: Eric Norum & Marty Kraimer Date: 15JUL99 */ + +/********************COPYRIGHT NOTIFICATION********************************** +This software was developed under a United States Government license +described on the COPYRIGHT_UniversityOfChicago file included as part +of this distribution. +****************************************************************************/ +#include +#include +#include +#include +#include + +#define epicsExportSharedSymbols +#include "dbDefs.h" +#include "cantProceed.h" +#include "epicsRingBytes.h" + +typedef struct ringPvt { + int nextPut; + int nextGet; + int size; + char *buffer; +}ringPvt; + + +epicsShareFunc epicsRingBytesId epicsShareAPI epicsRingBytesCreate(int size) +{ + ringPvt *pring = mallocMustSucceed(sizeof(ringPvt),"epicsRingBytesCreate"); + pring->size = size + 1; + pring->buffer = mallocMustSucceed(pring->size,"ringCreate"); + pring->nextGet = pring->nextPut = 0; + return((void *)pring); +} + +epicsShareFunc void epicsShareAPI epicsRingBytesDelete(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + free((void *)pring->buffer); + free((void *)pring); +} + +epicsShareFunc int epicsShareAPI epicsRingBytesGet( + epicsRingBytesId id, char *value,int nbytes) +{ + ringPvt *pring = (ringPvt *)id; + int nextGet = pring->nextGet; + int nextPut = pring->nextPut; + int size = pring->size; + int count; + + if (nextGet <= nextPut) { + count = nextPut - nextGet; + if (count < nbytes) + nbytes = count; + memcpy (value, &pring->buffer[nextGet], nbytes); + nextGet += nbytes; + } + else { + count = size - nextGet; + if (count > nbytes) + count = nbytes; + memcpy (value, &pring->buffer[nextGet], count); + if ((nextGet = nextGet + count) == size) { + int nLeft = nbytes - count; + if (nLeft > nextPut) + nLeft = nextPut; + memcpy (value+count, &pring->buffer[0], nLeft); + nextGet = nLeft; + nbytes = count + nLeft; + } + else { + nbytes = count; + } + } + pring->nextGet = nextGet; + return nbytes; +} + +epicsShareFunc int epicsShareAPI epicsRingBytesPut( + epicsRingBytesId id, char *value,int nbytes) +{ + ringPvt *pring = (ringPvt *)id; + int nextGet = pring->nextGet; + int nextPut = pring->nextPut; + int size = pring->size; + int count; + + if (nextPut < nextGet) { + count = nextGet - nextPut - 1; + if (nbytes > count) + nbytes = count; + memcpy (&pring->buffer[nextPut], value, nbytes); + nextPut += nbytes; + } + else if (nextGet == 0) { + count = size - nextPut - 1; + if (nbytes > count) + nbytes = count; + memcpy (&pring->buffer[nextPut], value, nbytes); + nextPut += nbytes; + } + else { + count = size - nextPut; + if (count > nbytes) + count = nbytes; + memcpy (&pring->buffer[nextPut], value, count); + if ((nextPut = nextPut + count) == size) { + int nLeft = nbytes - count; + if (nLeft > (nextGet - 1)) + nLeft = nextGet - 1; + memcpy (&pring->buffer[0], value+count, nLeft); + nextPut = nLeft; + nbytes = count + nLeft; + } + else { + nbytes = count; + nextPut = nextPut; + } + } + pring->nextPut = nextPut; + return nbytes; +} + +epicsShareFunc void epicsShareAPI epicsRingBytesFlush(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + + pring->nextGet = pring->nextPut = 0; +} + +epicsShareFunc int epicsShareAPI epicsRingBytesFreeBytes(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + int n; + + n = pring->nextGet - pring->nextPut - 1; + if (n < 0) + n += pring->size; + return n; +} + +epicsShareFunc int epicsShareAPI epicsRingBytesUsedBytes(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + int n; + + n = pring->nextPut - pring->nextGet; + if (n < 0) + n += pring->size; + return n; +} + +epicsShareFunc int epicsShareAPI epicsRingBytesSize(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + + return pring->size - 1; +} + +epicsShareFunc int epicsShareAPI epicsRingBytesIsEmpty(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + + return (pring->nextPut == pring->nextGet); +} + +epicsShareFunc int epicsShareAPI epicsRingBytesIsFull(epicsRingBytesId id) +{ + ringPvt *pring = (ringPvt *)id; + int count; + + count = (pring->nextPut - pring->nextGet) + 1; + return ((count == 0) || (count == pring->size)); +} diff --git a/src/libCom/ring/epicsRingBytes.h b/src/libCom/ring/epicsRingBytes.h new file mode 100644 index 000000000..d4cb589ed --- /dev/null +++ b/src/libCom/ring/epicsRingBytes.h @@ -0,0 +1,44 @@ +/*epicsRingBytes.h */ + +/* Author: Eric Norum & Marty Kraimer Date: 15JUL99 */ + +/********************COPYRIGHT NOTIFICATION********************************** +This software was developed under a United States Government license +described on the COPYRIGHT_UniversityOfChicago file included as part +of this distribution. +****************************************************************************/ + +#ifndef INCepicsRingBytesh +#define INCepicsRingBytesh + +#ifdef __cplusplus +extern "C" { +#endif + +#include "shareLib.h" + +typedef void *epicsRingBytesId; + +epicsShareFunc epicsRingBytesId epicsShareAPI epicsRingBytesCreate(int nbytes); +epicsShareFunc void epicsShareAPI epicsRingBytesDelete(epicsRingBytesId id); +epicsShareFunc int epicsShareAPI epicsRingBytesGet( + epicsRingBytesId id, char *value,int nbytes); +epicsShareFunc int epicsShareAPI epicsRingBytesPut( + epicsRingBytesId id, char *value,int nbytes); +epicsShareFunc void epicsShareAPI epicsRingBytesFlush(epicsRingBytesId id); +epicsShareFunc int epicsShareAPI epicsRingBytesFreeBytes(epicsRingBytesId id); +epicsShareFunc int epicsShareAPI epicsRingBytesUsedBytes(epicsRingBytesId id); +epicsShareFunc int epicsShareAPI epicsRingBytesSize(epicsRingBytesId id); +epicsShareFunc int epicsShareAPI epicsRingBytesIsEmpty(epicsRingBytesId id); +epicsShareFunc int epicsShareAPI epicsRingBytesIsFull(epicsRingBytesId id); + +#ifdef __cplusplus +} +#endif + +/* NOTES + If there is only one writer it is not necessary to lock for put + If there is a single reader it is not necessary to lock for puts +*/ + +#endif /* INCepicsRingBytesh */