ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fifo-buffer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Frederic Urbani <frederic.urbani@inria.fr>
19  *
20  */
21 #include "fifo-buffer.h"
22 #include "ns3/log.h"
23 #include <stdlib.h>
24 #include <string.h>
25 
26 NS_LOG_COMPONENT_DEFINE ("FifoBuffer");
27 
28 #define MIN_ALLOC 1024
29 
30 namespace ns3 {
31 FifoBuffer::FifoBuffer (size_t mxSz) : m_maxSize (mxSz),
32  m_buffer (0),
33  m_read (0)
34  ,
35  m_fill (0),
36  m_size (0)
37 {
38 }
40 {
41  if (m_buffer)
42  {
43  free (m_buffer);
44  m_buffer = 0;
45  }
46 }
47 ssize_t
48 FifoBuffer::Write (uint8_t *buf, size_t len)
49 {
50  NS_LOG_FUNCTION ("r:" << m_read << " f:" << m_fill << " s:" << m_size);
51  size_t capa = m_size - m_fill;
52 
53  if (len <= capa)
54  {
55  memcpy (m_buffer + m_fill, buf, len);
56  m_fill += len;
57  return len;
58  }
59  if ((m_read > 0) && (m_size <= m_maxSize))
60  {
61  if (m_fill > m_read)
62  {
63  memmove (m_buffer, m_buffer + m_read, m_fill - m_read);
64  }
65  m_fill -= m_read;
66  m_read = 0;
67  return Write (buf, len);
68  }
69  if (m_size == m_maxSize)
70  {
71  if (capa > 0)
72  {
73  memcpy (m_buffer + m_fill, buf, capa);
74  m_fill += capa;
75  return capa;
76  }
77  }
78  else
79  {
80  size_t newSize = len + m_fill;
81 
82  if (newSize - m_size < MIN_ALLOC)
83  {
84  newSize = m_size + MIN_ALLOC;
85  }
86  if (newSize > m_maxSize)
87  {
88  newSize = m_maxSize;
89  }
90 
91  uint8_t *newBuf = (uint8_t*) malloc (newSize);
92  if (!newBuf)
93  {
94  return -1;
95  }
96  memcpy (newBuf, m_buffer + m_read, m_fill - m_read);
97  free (m_buffer);
98  m_buffer = newBuf;
99  m_fill -= m_read;
100  m_read = 0;
101  m_size = newSize;
102 
103  return Write (buf, len);
104  }
105  return 0;
106 }
107 ssize_t
108 FifoBuffer::Read (uint8_t *buf, size_t len)
109 {
110  NS_LOG_FUNCTION ("r:" << m_read << " f:" << m_fill << " s:" << m_size);
111  size_t capa = m_fill - m_read;
112 
113  if (capa > 0)
114  {
115  size_t l = std::min (len, capa);
116 
117  memcpy (buf, m_buffer + m_read, l);
118  m_read += l;
119 
120  return l;
121  }
122  return 0;
123 }
124 ssize_t
126 {
127  return m_fill - m_read;
128 }
129 ssize_t
131 {
132  return m_maxSize - GetSize ();
133 }
134 } // namespace ns3