NetBurner 3.5.6
PDF Version
buffers.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
18/* This file provides the definitions for handling Buffers */
19
20#include <constants.h> // Added 3-22-12, dciliske
21
22#ifndef _NB_BUFFERS_H
23#define _NB_BUFFERS_H
24
25#ifdef _DEBUG
26#define BUFFER_DIAG
27#endif
28
29#include <basictypes.h>
30#include <nbrtos.h>
31
32struct pool_buffer; /*Forward declaration*/
33typedef struct pool_buffer *PoolPtr;
34typedef volatile PoolPtr VPoolPtr;
35
43#define BO_UNUSED 0
44#define BO_SOFTWARE 1
45#define BO_PRX 2
46#define BO_PTXF 3
47#define BO_PTXS 4
56#define BS_PHY_BCAST 1
57#define BS_IP_BCAST 2
58#define BS_IP_LOCAL_NET 4
59#define BS_TCP_PUSH 8
60#define BS_PHY_802_3 0x10
61#define BS_VLAN 0x20
62#define BS_DBG_LOG 0x80
65#ifdef BUFFER_DIAG
66#define BUFFER_DIAG_WORD_COUNT 4
67#else
68#define BUFFER_DIAG_WORD_COUNT 0
69#endif
70#ifdef NET_SHIFT16
71#define BUF_PREDATA_SENTINAL_LEN (8)
72#define BUF_PREDATA_PADLEN 8
73#define BUF_POSTDATA_PADLEN 3
74#define BUF_POSTDATA_SENTINAL_LEN (11 - BUFFER_DIAG_WORD_COUNT)
75#else
76#define BUF_PREDATA_SENTINAL_LEN (8)
77#define BUF_PREDATA_PADLEN 6
78#define BUF_POSTDATA_PADLEN 3
79#define BUF_POSTDATA_SENTINAL_LEN (12 - BUFFER_DIAG_WORD_COUNT)
80#endif
81
89struct pool_buffer : public OS_FIFO_EL
90{
91 // puint8_t pBufQueuePointer; /* For the OS FIFO list stuff */
92 uint32_t PreSentinal[BUF_PREDATA_SENTINAL_LEN];
93
96 vuint32_t dwTime;
97 vuint32_t dwTimeFraction;
98 vuint16_t usedsize;
99 uint8_t bBuffer_state;
100 uint8_t bBufferFlag;
101 uint8_t bUsageCount;
103 uint8_t bAlignmentPad[BUF_PREDATA_PADLEN];
105 uint8_t bPostAlignmentPad[BUF_POSTDATA_PADLEN];
106 uint32_t PostSentinal[BUF_POSTDATA_SENTINAL_LEN];
107
108#ifdef BUFFER_DIAG
109 PCSTR m_fAlloc;
110 uint32_t m_fline;
111 uint32_t m_fill;
112 uint32_t m_fill2;
113#endif /* BUFFER_DIAG */
114};
115
116inline uint8_t OSPoolFifoPost(OS_FIFO *pFifo, PoolPtr pToPost)
117{
118 return pFifo->Post(pToPost);
119}
120
121inline uint8_t OSPoolFifoPostFirst(OS_FIFO *pFifo, PoolPtr pToPost)
122{
123 return pFifo->PostFirst(pToPost);
124}
125
126inline PoolPtr OSPoolFifoPend(OS_FIFO *pFifo, uint16_t timeout)
127{
128 return static_cast<PoolPtr>(pFifo->Pend(timeout));
129}
130
131inline PoolPtr OSPoolFifoPend(OS_FIFO *pFifo, TickTimeout timeout)
132{
133 return static_cast<PoolPtr>(pFifo->Pend(timeout));
134}
135
136
137inline PoolPtr OSPoolFifoPendNoWait(OS_FIFO *pFifo)
138{
139 return static_cast<PoolPtr>(pFifo->PendNoWait());
140}
141
142/*Buffer operations*/
143
144#ifdef BUFFER_DIAG
145void ShowBuffers();
146void ShowBuffers_Web(int sockfd);
155PoolPtr GetBufferX(PCSTR file, int line);
156#define GetBuffer() GetBufferX(__FILE__, __LINE__)
157#ifdef FAST_BUFFERS
166PoolPtr GetFastBufferX(PCSTR file, int line);
167#define GetFastBuffer() GetFastBufferX(__FILE__, __LINE__)
168#else /* #ifdef FAST_BUFFERS */
169#define GetFastBuffer() GetBufferX(__FILE__, __LINE__)
170#endif /* #ifdef FAST_BUFFERS */
178void FreeBufferX(PoolPtr nbuf, PCSTR file, int line);
179#define FreeBuffer(x) FreeBufferX(x, __FILE__, __LINE__)
180
181void ChangeOwnerX(PoolPtr nbuf, PCSTR file, int line);
182#define ChangeOwner(x) ChangeOwnerX(x, __FILE__, __LINE__)
183#else
195#ifdef FAST_BUFFERS
208#else /* #ifdef FAST_BUFFERS */
209#define GetFastBuffer() GetBuffer()
210#endif /* #ifdef FAST_BUFFERS */
223#define ChangeOwner(x)
224#endif
225
226#ifdef BUFFER_DIAG_LOG
227#include <syslog.h>
228extern uint32_t bufLogID;
229#define LOG_DEST AsciiToIp("10.1.1.3")
230#define SET_LOG_DEST \
231 { \
232 extern IPADDR SysLogAddress; \
233 SysLogAddress = LOG_DEST; \
234 }
235#define LOG_INTF 2
236#define BuffLog_GetFmt "%0.8lu) GET - P: %p, L: %0.4d, F: %s\r\n"
237#define BuffLog_FreeFmt "%0.8lu) FREE - P: %p, L: %0.4d, F: %s\r\n"
238#define BuffLog_GetLF(p, l, f) SysLogVia(LOG_INTF, BuffLog_GetFmt, bufLogID++, p, l, f)
239#define BuffLog_FreeLF(p, l, f) SysLogVia(LOG_INTF, BuffLog_FreeFmt, bufLogID++, p, l, f)
240#define BuffLog_Get(p) BuffLog_GetLF(p, __LINE__, __FILE__)
241#define BuffLog_Free(p) BuffLog_FreeLF(p, __LINE__, __FILE__)
242#define BuffLog_Write(p) SysLogVia(LOG_INTF, "WRITE - P: %p, L: %0.4d, F: %s\r\n", p, __LINE__, __FILE__)
243#define BuffLog_Read(p) SysLogVia(LOG_INTF, "READ - P: %p, L: %0.4d, F: %s\r\n", p, __LINE__, __FILE__)
244#else
245#define BuffLog_Get(p)
246#define BuffLog_Free(p)
247#define BuffLog_Write(p)
248#define BuffLog_Read(p)
249#endif
261
274
299uint16_t GetFreeCount();
300
301#ifdef BUFFER_WATERMARK
309uint16_t GetMinFreeCount();
310
318uint16_t GetMaxFreeCount();
319#endif
320
330
340
349{
350 public:
353#ifdef BUFFER_WATERMARK
354 uint16_t m_wMaxElements;
355 uint16_t m_wMinElements;
356#endif
357 uint16_t m_wElements;
358
363 void InsertHead(PoolPtr buffer);
364
369 void InsertTail(PoolPtr buffer);
370
376 void InsertBefore(PoolPtr buf2insert, PoolPtr b4buffer);
377
383 void InsertAfter(PoolPtr buf2insert, PoolPtr after_buffer);
384
389 void Remove(PoolPtr buffer);
390
396
402
404 {
405 m_Head = 0;
406 m_Tail = 0;
407 m_wElements = 0;
408#ifdef BUFFER_WATERMARK
409 m_wMaxElements=0;
410 m_wMinElements=0;
411#endif
412
413 }
414
419 uint16_t GetCount() { return m_wElements; };
420#ifdef BUFFER_WATERMARK
425 uint16_t GetMaxCount() { return m_wMaxElements; };
426
431 uint16_t GetMinCount() { return m_wMinElements; };
432#endif
433};
434
443{
444 private:
445 PoolPtr m_Head;
446 PoolPtr m_Tail;
447 OS_CRIT m_critical_section;
448 int m_BytesStored;
449 uint32_t m_startOffset;
450 uint32_t m_maxChunkLen;
451 uint8_t m_Segments_Stored;
452 uint8_t m_MaxSegments; /*Stores the max number of segments allowed fore this buffer*/
453 bool m_checksumWrites;
454 uint8_t m_buffer_pool;
455
456 // storage access/update functions to be used by BufPtr.
457 // DO NOT CALL WITHOUT LOCKING STRUCT FIRST
458 uint8_t *GetWritePtr(uint32_t *remLen);
459 void WriteDone(uint32_t bytesCopied);
460
461 uint8_t *GetReadPtr(uint32_t *remLen);
462 void ReadDone(uint32_t bytesCopied);
463
464 public:
472 {
473 WriteBufPtr();
474 fifo_buffer_storage &m_fifo;
475 uint8_t *pBuf;
476 uint32_t bufLen;
477 uint32_t copied;
478
479 public:
480 WriteBufPtr(fifo_buffer_storage &fifo) : m_fifo(fifo), copied(0)
481 {
482 m_fifo.m_critical_section.Enter();
483 pBuf = m_fifo.GetWritePtr(&bufLen);
484 }
486 {
487 m_fifo.WriteDone(copied);
488 m_fifo.m_critical_section.Leave();
489 }
490
491 inline uint8_t *buf() { return pBuf + copied; }
492 inline uint32_t len() { return bufLen - copied; }
493 void ByteCopyDone(uint32_t bytesCopied) { copied += (bytesCopied <= (bufLen - copied)) ? bytesCopied : (bufLen - copied); }
494 };
502 {
503 ReadBufPtr();
504 fifo_buffer_storage &m_fifo;
505 uint8_t *pBuf;
506 uint32_t bufLen;
507 uint32_t copied;
508
509 public:
510 ReadBufPtr(fifo_buffer_storage &fifo) : m_fifo(fifo), copied(0)
511 {
512 m_fifo.m_critical_section.Enter();
513 pBuf = m_fifo.GetReadPtr(&bufLen);
514 }
516 {
517 m_fifo.ReadDone(copied);
518 m_fifo.m_critical_section.Leave();
519 }
520
521 inline uint8_t *buf() { return pBuf + copied; }
522 inline uint32_t len() { return bufLen - copied; }
523 void ByteCopyDone(uint32_t bytesCopied) { copied += (bytesCopied <= (bufLen - copied)) ? bytesCopied : (bufLen - copied); }
524 };
532 {
533 PeekIterator();
534 fifo_buffer_storage &m_fifo;
535 PoolPtr pCurrPP;
536 uint8_t *peekPtr;
537 bool locked;
538
539 PeekIterator(const PeekIterator &rhs)
540 : m_fifo(rhs.m_fifo), pCurrPP(rhs.pCurrPP), peekPtr(rhs.peekPtr), locked(false)
541 {
542 if (pCurrPP)
543 {
544 m_fifo.m_critical_section.Enter();
545 locked = true;
546 }
547 }
548
549 public:
550 PeekIterator(fifo_buffer_storage &fifo, bool begin = true) : m_fifo(fifo), pCurrPP(NULL), peekPtr(NULL), locked(false)
551 {
552 if (begin)
553 {
554 m_fifo.m_critical_section.Enter();
555 locked = true;
556 pCurrPP = m_fifo.m_Head;
557 if (pCurrPP) { peekPtr = pCurrPP->pAsBytePtr; }
558 }
559 }
560 ~PeekIterator() { if (locked) m_fifo.m_critical_section.Leave(); }
561
562 PeekIterator &operator++(); // preincrement
563 PeekIterator operator++(int); // postincrement
564 uint8_t operator*(); // dereference
565 int32_t operator-(const PeekIterator &rhs);
566 PeekIterator &operator+=(int);
567
568 operator uint16_t() const;
569 operator uint32_t() const;
570
571 inline bool operator==(const PeekIterator &rhs)
572 {
573 return ((&m_fifo == &(rhs.m_fifo)) && (pCurrPP == rhs.pCurrPP) && (peekPtr == rhs.peekPtr));
574 }
575 bool operator!=(const PeekIterator &rhs) { return !(*this == rhs); }
576 };
577
578 friend class WriteBufPtr;
579 friend class ReadBufPtr;
580 friend class PeekIterator;
581
582 uint32_t LongSpaceAvail();
583
588 uint16_t SpaceAvail();
589
594 uint16_t SpaceUsed();
595
602 int ReadData(puint8_t pCopyTo, int max_bytes);
603
609 uint8_t PeekData(int idx = -1);
610
611 int ReadDatawSum(puint8_t pCopyTo, int max_bytes, uint32_t &csum);
612
613 int SkipData(int skip);
614
615 int ReadTerminatedData(puint8_t pCopyTo, int max_bytes, uint8_t term_char);
616
617 /* Advanced functions for no-copy buffer usage */
618 int PushBuffer(PoolPtr pp, int dataLen, int startOffset);
619 PoolPtr PullBuffer();
620 PoolPtr PullBuffer(uint32_t &csum);
621 inline PoolPtr PeekBuffer() { return m_Head; }
622 void SetFifoLock(bool locked);
623
630 int WriteData(puint8_t pCopyFrom, int num_bytes);
631
632 void RemoveLast();
633
639
645
650 BOOL Empty();
651
656 BOOL Full();
657
664 fifo_buffer_storage(uint8_t max_buffers = 0, uint8_t use_fromisr = 1, uint8_t use_fast_buffs = 1);
665
670
675 void Reset(uint8_t max_buffers);
676
677 void SetMaxBuffers(uint8_t max_buffers);
678 inline void SetMaxChunkLen(uint32_t maxStorage)
679 {
680 m_maxChunkLen = (maxStorage > (ETHER_BUFFER_SIZE - m_startOffset)) ? (ETHER_BUFFER_SIZE - m_startOffset) : maxStorage;
681 }
682 inline void SetStartOffset(uint32_t offset)
683 {
684 m_startOffset = offset;
685 SetMaxChunkLen(m_maxChunkLen);
686 }
687 inline uint32_t GetMaxBuffers() { return m_MaxSegments; }
688 inline uint32_t GetMaxChunkLen() { return m_maxChunkLen; }
689 inline uint32_t GetStartOffset() { return m_startOffset; }
690 inline void SetWriteChecksum(bool summingOn) { m_checksumWrites = summingOn; }
691 inline uint32_t GetMinimumFullSize(int maxSegments = -1)
692 {
693 if (maxSegments < 0)
694 maxSegments = m_MaxSegments;
695 return (m_maxChunkLen * (maxSegments-1))+1;
696 }
697
698 void Init(uint8_t max_buffers, uint8_t use_fromisr = 1, uint8_t use_fast_buffs = 1);
699} __attribute__((packed));
700
708{
709 PoolPtr m_pp;
710
711 public:
716 {
717 m_pp = GetBuffer();
718 BuffLog_Get(m_pp);
719 }
720
725 SMPoolPtr(const PoolPtr pp) { m_pp = pp; }
726
727 SMPoolPtr(const SMPoolPtr &pp) { m_pp = pp.m_pp; }
728
733 {
734 if (m_pp)
735 {
736 FreeBuffer(m_pp);
737 BuffLog_Free(m_pp);
738 }
739 }
740
741 inline const SMPoolPtr &operator=(const PoolPtr pp)
742 {
743 m_pp = pp;
744 return *this;
745 }
746 inline const SMPoolPtr &operator=(const SMPoolPtr &pp)
747 {
748 m_pp = pp.m_pp;
749 return *this;
750 }
751 inline pool_buffer &operator*() { return *m_pp; }
752 inline PoolPtr &operator->() { return m_pp; }
753 explicit operator PoolPtr() { return m_pp; }
754 inline bool operator!() { return !m_pp; }
755};
756
757#endif
758
Smart pointer wrapper for automatic buffer management.
Definition buffers.h:708
SMPoolPtr(const PoolPtr pp)
Constructor from existing buffer pointer.
Definition buffers.h:725
SMPoolPtr()
Constructor - automatically allocates a buffer.
Definition buffers.h:715
~SMPoolPtr()
Destructor - automatically frees the buffer.
Definition buffers.h:732
TickTimeout objects are used to facilitate sequential function calls with timeout parameters that nee...
Definition nbrtos.h:168
Doubly-linked list management for buffers.
Definition buffers.h:349
PoolPtr RemoveHead()
Remove and return the head buffer.
void InsertAfter(PoolPtr buf2insert, PoolPtr after_buffer)
Insert buffer after specified buffer in list.
uint16_t GetCount()
Get current number of buffers in the list.
Definition buffers.h:419
void Remove(PoolPtr buffer)
Remove specified buffer from the list.
void InsertHead(PoolPtr buffer)
Insert buffer at the head of the list.
PoolPtr RemoveTail()
Remove and return the tail buffer.
void InsertTail(PoolPtr buffer)
Insert buffer at the tail of the list.
uint16_t m_wElements
Current number of elements in list.
Definition buffers.h:357
PoolPtr m_Head
First buffer in the list.
Definition buffers.h:351
void InsertBefore(PoolPtr buf2insert, PoolPtr b4buffer)
Insert buffer before specified buffer in list.
PoolPtr m_Tail
Last buffer in the list.
Definition buffers.h:352
Iterator for non-destructive examination of buffered data.
Definition buffers.h:532
RAII accessor for reading from buffer storage.
Definition buffers.h:502
RAII accessor for writing to buffer storage.
Definition buffers.h:472
FIFO buffer storage using linked pool buffers.
Definition buffers.h:443
BOOL Empty()
Check if buffer is empty.
~fifo_buffer_storage()
Destructor - frees all allocated buffers.
uint8_t PeekData(int idx=-1)
Peek at data without removing it.
fifo_buffer_storage(uint8_t max_buffers=0, uint8_t use_fromisr=1, uint8_t use_fast_buffs=1)
Constructor.
BOOL Full()
Check if buffer is full.
void Reset(uint8_t max_buffers)
Reset buffer storage and free all data.
PeekIterator Peek_Begin()
Get iterator for beginning of buffered data.
int ReadData(puint8_t pCopyTo, int max_bytes)
Read data from the buffer.
uint16_t SpaceAvail()
Get number of bytes available for writing.
PeekIterator Peek_End()
Get iterator for end of buffered data.
int WriteData(puint8_t pCopyFrom, int num_bytes)
Write data to the buffer.
uint16_t SpaceUsed()
Get number of bytes currently stored.
NetBurner System Constants.
PoolPtr GetFastBuffer()
Allocate a software buffer, preferring the fast memory pool.
void FreeBuffer(PoolPtr nbuf)
Free a buffer and return it to the unused pool.
void ShowBuffer(PoolPtr p)
Display buffer contents and state information.
void InitBuffers()
Initialize the buffer system.
PoolPtr GetBuffer()
Allocate a software buffer for general use.
uint16_t GetFreeCount()
Returns the number of free buffers in the system. Buffers are used for both serial and network interf...
void IncUsageCount(PoolPtr pp)
Increment the usage count of a buffer.
void FreeBufferList(PoolPtr nbuf)
Free a linked list of buffers.
#define ETHER_BUFFER_SIZE
Definition constants.h:64
NetBurner Real-Time Operating System (NBRTOS) API.
An OS_CRIT object is used to establish critical sections of code that can only be run by one task at ...
Definition nbrtos.h:1110
uint8_t Enter(TickTimeout &t)
Request to Enter/Claim the critical section.
uint8_t Leave()
Release the critical section.
Definition nbrtos.h:932
OS_FIFO_EL * PendNoWait(uint8_t &result)
Attempts to pend a structure to the FIFO, but does not wait.
uint8_t PostFirst(OS_FIFO_EL *pToPost)
Post a message to the head of the FIFO.
OS_FIFO_EL * Pend(uint32_t timeoutTicks, uint8_t &result)
Wait the specified number of time ticks for some other task to post to the FIFO.
Definition nbrtos.h:993
uint8_t Post(OS_FIFO_EL *pToPost)
Post a message to the next available location in the FIFO.
OS_FIFO element definition.
Definition nbrtos.h:909
puint8_t pAsBytePtr
Next OS_FIFO element data byte pointer.
Definition nbrtos.h:918
Main buffer structure for network and serial communication.
Definition buffers.h:90
uint8_t bInterfaceNumber
Network interface number.
Definition buffers.h:102
uint8_t pData[ETHER_BUFFER_SIZE]
Buffer data payload (1548 bytes)
Definition buffers.h:104
vuint32_t dwTimeFraction
Fractional timestamp component.
Definition buffers.h:97
vuint16_t usedsize
Number of bytes currently used in pData.
Definition buffers.h:98
VPoolPtr pPoolNext
Pointer to next buffer in linked list.
Definition buffers.h:94
VPoolPtr pPoolPrev
Pointer to previous buffer in linked list.
Definition buffers.h:95
uint8_t bBufferFlag
Buffer state flags (BS_* values)
Definition buffers.h:100
vuint32_t dwTime
Timestamp or checksum storage.
Definition buffers.h:96
uint8_t bUsageCount
Reference count for shared buffers.
Definition buffers.h:101
uint8_t bBuffer_state
Buffer ownership state (BO_* values)
Definition buffers.h:99