optimize Buffer::WriteU8 variants

This commit is contained in:
Mathieu Lacage
2007-09-11 14:56:10 +02:00
parent 42e6c413b6
commit 2b3b68605e
2 changed files with 98 additions and 13 deletions

View File

@@ -642,6 +642,28 @@ Buffer::Iterator::IsStart (void) const
return m_current == m_dataStart;
}
bool
Buffer::Iterator::CheckNoZero (uint32_t start, uint32_t end) const
{
bool ok = true;
for (uint32_t i = start; i < end; i++)
{
if (!Check (i))
{
ok = false;
}
}
return ok;
}
bool
Buffer::Iterator::Check (uint32_t i) const
{
return i >= m_dataStart &&
!(i >= m_zeroStart && i <= m_zeroEnd) &&
i <= m_dataEnd;
}
void
Buffer::Iterator::Write (Iterator start, Iterator end)
{
@@ -659,14 +681,6 @@ Buffer::Iterator::Write (Iterator start, Iterator end)
}
}
void
Buffer::Iterator::WriteU8 (uint8_t data, uint32_t len)
{
for (uint32_t i = 0; i < len; i++)
{
WriteU8 (data);
}
}
void
Buffer::Iterator::WriteU16 (uint16_t data)
{
@@ -880,6 +894,49 @@ Buffer::Iterator::Read (uint8_t *buffer, uint32_t size)
}
}
#ifndef BUFFER_USE_INLINE
void
Buffer::Iterator::WriteU8 (uint8_t data)
{
if (m_current < m_dataStart)
{
// XXX trying to write outside of data area
NS_ASSERT (false);
}
else if (m_current < m_zeroStart)
{
m_data[m_current] = data;
m_current++;
}
else if (m_current < m_zeroEnd)
{
// XXX trying to write in zero area
NS_ASSERT (false);
}
else if (m_current < m_dataEnd)
{
m_data[m_current - (m_zeroEnd-m_zeroStart)] = data;
m_current++;
}
else
{
// XXX trying to write outside of data area
NS_ASSERT (false);
}
}
void
Buffer::Iterator::WriteU8 (uint8_t data, uint32_t len)
{
for (uint32_t i = 0; i < len; i++)
{
WriteU8 (data);
}
}
#endif /* BUFFER_USE_INLINE */
} // namespace ns3

View File

@@ -23,6 +23,13 @@
#include <stdint.h>
#include <vector>
#define BUFFER_USE_INLINE
#ifdef BUFFER_USE_INLINE
#define BUFFER_INLINE inline
#else
#define BUFFER_INLINE
#endif
namespace ns3 {
/**
@@ -87,7 +94,7 @@ public:
* Write the data in buffer and avance the iterator position
* by one byte.
*/
inline void WriteU8 (uint8_t data);
BUFFER_INLINE void WriteU8 (uint8_t data);
/**
* \param data data to write in buffer
* \param len number of times data must be written in buffer
@@ -95,7 +102,7 @@ public:
* Write the data in buffer len times and avance the iterator position
* by len byte.
*/
void WriteU8 (uint8_t data, uint32_t len);
BUFFER_INLINE void WriteU8 (uint8_t data, uint32_t len);
/**
* \param data data to write in buffer
*
@@ -240,6 +247,9 @@ public:
Iterator (Buffer const*buffer);
Iterator (Buffer const*buffer, bool);
void Construct (const Buffer *buffer);
bool CheckNoZero (uint32_t start, uint32_t end) const;
bool Check (uint32_t i) const;
uint32_t m_zeroStart;
uint32_t m_zeroEnd;
uint32_t m_dataStart;
@@ -348,6 +358,8 @@ private:
} // namespace ns3
#ifdef BUFFER_USE_INLINE
#include "ns3/assert.h"
namespace ns3 {
@@ -355,9 +367,7 @@ namespace ns3 {
void
Buffer::Iterator::WriteU8 (uint8_t data)
{
NS_ASSERT (m_current >= m_dataStart &&
!(m_current >= m_zeroStart && m_current <= m_zeroEnd) &&
m_current <= m_dataEnd);
NS_ASSERT (Check (m_current));
if (m_current < m_zeroStart)
{
@@ -371,7 +381,25 @@ Buffer::Iterator::WriteU8 (uint8_t data)
}
}
void
Buffer::Iterator::WriteU8 (uint8_t data, uint32_t len)
{
NS_ASSERT (CheckNotZero (m_current, m_current + len));
if (m_current <= m_zeroStart)
{
memset (&(m_data[m_current]), data, len);
m_current += len;
}
else
{
uint8_t *buffer = &m_data[m_current - (m_zeroEnd-m_zeroStart)];
memset (buffer, data, len);
m_current += len;
}
}
} // namespace ns3
#endif /* BUFFER_USE_INLINE */
#endif /* BUFFER_H */