librnr  1.14.5
RoadNarrows Robotics Common Library 1
simplebuf.c File Reference

Simple [io] buffer declarations and operations. More...

#include <unistd.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"
#include "rnr/simplebuf.h"

Go to the source code of this file.

Functions

SimpleBuf_TSimpleBufNew ()
 Allocate a new simple buffer with no internal buffer allocation. More...
 
SimpleBuf_TSimpleBufNewWithBuf (size_t nBufSize)
 Allocate a new simple buffer with internal buffer of nBufSize. More...
 
void SimpleBufDelete (SimpleBuf_T *pBuf)
 Delete a simple buffer along with the internal buffer. More...
 
void SimpleBufSetBuf (SimpleBuf_T *pBuf, byte_t *pRWBuf, size_t nBufSize, size_t nLen)
 Set simple buffer's internal read/write buffer. More...
 
size_t SimpleBufCopy (SimpleBuf_T *pTgt, SimpleBuf_T *pSrc)
 Copy contents of source simple buffer to the end of the target simple buffer. More...
 

Detailed Description

Simple [io] buffer declarations and operations.

LastChangedDate
2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010)
Rev
307

The buffer is orgainized as a simple linear buffer with independent read and write positions. Ideal for multi-tasking.

Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file simplebuf.c.

Function Documentation

size_t SimpleBufCopy ( SimpleBuf_T pTgt,
SimpleBuf_T pSrc 
)

Copy contents of source simple buffer to the end of the target simple buffer.

Parameters
pTgtPointer to target simple buffer.
pSrcPointer to source simple buffer.

Return Value:

Returns
Returns Number of bytes copied.

Definition at line 165 of file simplebuf.c.

References SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and NULL.

166 {
167  size_t rpos, nBytes;
168 
169  if( (pTgt == NULL) || (pSrc == NULL) )
170  {
171  return (size_t)0;
172  }
173 
174  for(rpos=pSrc->m_nRPos, nBytes=0;
175  (rpos < pSrc->m_nWPos) && (pTgt->m_nWPos < pSrc->m_nBufSize);
176  ++rpos, ++nBytes)
177  {
178  pTgt->m_pRWBuf[pTgt->m_nWPos++] = pSrc->m_pRWBuf[rpos];
179  }
180 
181  return nBytes;
182 }
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
void SimpleBufDelete ( SimpleBuf_T pBuf)

Delete a simple buffer along with the internal buffer.

Actual deletion on occurs if buffer is owned by this simple buffer.

Parameters
pBufPointer to simple buffer to be deleted.

Definition at line 111 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_pRWBuf, and NULL.

Referenced by SocketBufNewBuf(), and SocketDelete().

112 {
113  if( pBuf == NULL )
114  {
115  return;
116  }
117 
118  if( pBuf->m_bBufDel )
119  {
120  delete(pBuf->m_pRWBuf);
121  }
122  delete(pBuf);
123 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
SimpleBuf_T* SimpleBufNew ( )

Allocate a new simple buffer with no internal buffer allocation.

Returns
Returns SimpleBuf_T* to newly allocated simple buffer.

Definition at line 70 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, NEW, and NULL.

Referenced by SocketNew().

71 {
72  SimpleBuf_T *pBuf = NEW(SimpleBuf_T);
73 
74  pBuf->m_pRWBuf = NULL;
75  pBuf->m_nBufSize = (size_t)0;
76  pBuf->m_nRPos = (size_t)0;
77  pBuf->m_nWPos = (size_t)0;
78  pBuf->m_bBufDel = false;
79 
80  return pBuf;
81 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
#define NEW(T)
Allocate new type.
Definition: new.h:49
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
SimpleBuf_T* SimpleBufNewWithBuf ( size_t  nBufSize)

Allocate a new simple buffer with internal buffer of nBufSize.

Parameters
nBufSizeSize in bytes of internal buffer to allocate with simple buffer.
Returns
Returns SimpleBuf_T* to newly allocated simple buffer.

Definition at line 91 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and NEW.

Referenced by SocketBufNewBuf().

92 {
93  SimpleBuf_T *pBuf = NEW(SimpleBuf_T);
94 
95  pBuf->m_pRWBuf = (byte_t *)new(nBufSize);
96  pBuf->m_nBufSize = nBufSize;
97  pBuf->m_nRPos = (size_t)0;
98  pBuf->m_nWPos = (size_t)0;
99  pBuf->m_bBufDel = true;
100 
101  return pBuf;
102 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
#define NEW(T)
Allocate new type.
Definition: new.h:49
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62
void SimpleBufSetBuf ( SimpleBuf_T pBuf,
byte_t pRWBuf,
size_t  nBufSize,
size_t  nLen 
)

Set simple buffer's internal read/write buffer.

Parameters
pBufPointer to simple buffer.
pRWBufPointer to buffer to be set as the internal read/write buffer.
nBufSizeSize in bytes of pRWBuf.
nLenCurrent length (bytes) of data in pRWBuf.

Definition at line 134 of file simplebuf.c.

References SimpleBuf_T::m_bBufDel, SimpleBuf_T::m_nBufSize, SimpleBuf_T::m_nRPos, SimpleBuf_T::m_nWPos, SimpleBuf_T::m_pRWBuf, and NULL.

Referenced by SocketBufSetBuf().

136 {
137  if( pBuf == NULL )
138  {
139  return;
140  }
141 
142  if( pBuf->m_bBufDel )
143  {
144  delete(pBuf->m_pRWBuf);
145  }
146 
147  pBuf->m_pRWBuf = pRWBuf;
148  pBuf->m_nBufSize = nBufSize;
149  pBuf->m_nRPos = (size_t)0;
150  pBuf->m_nWPos = nLen<=nBufSize? nLen: nBufSize;
151  pBuf->m_bBufDel = false;
152 }
bool_t m_bBufDel
do [not] delete internal R/W buffer
Definition: simplebuf.h:64
byte_t * m_pRWBuf
internal read/write buffer
Definition: simplebuf.h:60
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_nWPos
current write buffer position
Definition: simplebuf.h:63
size_t m_nBufSize
buffer total size
Definition: simplebuf.h:61
size_t m_nRPos
current read buffer position
Definition: simplebuf.h:62