FreeNOS
NetworkQueue.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <FreeNOS/User.h>
19#include <Log.h>
20#include <String.h>
21#include "NetworkQueue.h"
22
24 const Size queueSize)
25{
26 assert(queueSize <= MaxPackets);
27
32
33 // Ensure size is page aligned
35 {
37 }
38
39 // Allocate memory pages for the payload
41 if (result != API::Success)
42 {
43 ERROR("failed to allocate payload buffer: result = " << (int) result);
44 }
45 else
46 {
47 // Allocate packet objects
48 for (Size i = 0; i < queueSize; i++)
49 {
50 Packet *packet = new Packet;
51 packet->size = 0;
52 packet->data = (u8 *) (m_payloadRange.virt + (i * PayloadBufferSize));
53 m_free.insertAt(i, packet);
54 }
55 }
56}
57
59{
60 for (Size i = 0; i < m_free.size(); i++)
61 {
62 Packet *p = m_free.get(i);
63 if (p)
64 {
65 delete p;
66 }
67 }
68
70}
71
73{
74 for (Size i = 0; i < m_free.size(); i++)
75 {
76 Packet *p = m_free.get(i);
77 if (p)
78 {
79 p->size = 0;
80 m_free.remove(i);
81 return p;
82 }
83 }
84 return ZERO;
85}
86
88{
89 packet->size = 0;
90 m_free.insert(packet);
91}
92
94{
95 m_data.insert(packet);
96}
97
99{
100 for (Size i = 0; i < m_data.size(); i++)
101 {
102 Packet *p = m_data.get(i);
103 if (p)
104 {
105 m_data.remove(i);
106 return p;
107 }
108 }
109 return ZERO;
110}
111
113{
114 return m_data.count() > 0;
115}
116
118{
119 String s;
120
121 for (Size i = 0; i < pkt.size; i++)
122 {
123 if ((i % 16) == 0)
124 s << "\r\n";
125
126 const uint val = pkt.data[i];
127 s << Number::Hex << val;
128
129 if (val >= 0x10)
130 s << " ";
131 else
132 s << " ";
133 }
134 s << "\r\n";
135
136 log.append(*s);
137 return log;
138}
Result
Enumeration of generic kernel API result codes.
Definition API.h:69
@ Success
Definition API.h:70
Logging class.
Definition Log.h:97
void append(const char *str)
Append to buffered output.
Definition Log.cpp:53
Packet * pop()
Retrieve packet with data.
NetworkQueue(const Size packetSize, const Size queueSize=MaxPackets)
Constructor.
virtual ~NetworkQueue()
Destructor.
Index< Packet, MaxPackets > m_data
Contains packets with data.
Index< Packet, MaxPackets > m_free
Contains unused packets.
Memory::Range m_payloadRange
Defines the memory range of mapped payload data.
static const Size PayloadBufferSize
Size of payload memory buffer.
bool hasData() const
Check if data packets are available.
Packet * get()
Get unused packet.
void release(Packet *packet)
Put unused packet back.
void push(Packet *packet)
Enqueue packet with data.
static const Size MaxPackets
Maximum number of packets available.
Abstraction of strings.
Definition String.h:42
#define SELF
Definition ProcessID.h:35
API::Result VMCtl(const ProcessID procID, const MemoryOperation op, Memory::Range *range=ZERO)
Prototype for user applications.
Definition VMCtl.h:61
@ Release
Definition VMCtl.h:40
@ MapContiguous
Definition VMCtl.h:37
#define PAGESIZE
ARM uses 4K pages.
Definition ARMConstant.h:97
Log & operator<<(Log &log, const NetworkQueue::Packet &pkt)
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int uint
Unsigned integer number.
Definition Types.h:44
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
@ User
Definition Memory.h:44
@ Readable
Definition Memory.h:41
@ Writable
Definition Memory.h:42
@ Hex
Decimal: 0-10.
Definition Types.h:171
Size size
Size in number of bytes.
Definition Memory.h:59
Address phys
Physical address.
Definition Memory.h:58
Address virt
Virtual address.
Definition Memory.h:57
Access access
Page access flags.
Definition Memory.h:60
Represents a network packet.