epicsRingBytes replaces osiRing

This commit is contained in:
Marty Kraimer
2000-12-15 16:35:17 +00:00
parent 26a9323422
commit 37fbc3b19d
3 changed files with 224 additions and 1 deletions

View File

@@ -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

View File

@@ -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 <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#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));
}

View File

@@ -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 */