2006-11-01 13:11:30 +01:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
2006-08-29 17:42:13 +02:00
|
|
|
/*
|
2007-09-07 08:34:46 +02:00
|
|
|
* Copyright (c) 2005,2006,2007 INRIA
|
2006-08-29 17:42:13 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
|
|
|
*/
|
|
|
|
|
#include "buffer.h"
|
2007-02-16 09:56:21 +01:00
|
|
|
#include "ns3/assert.h"
|
2007-09-13 17:47:42 -07:00
|
|
|
#include "ns3/log.h"
|
2007-09-07 08:34:46 +02:00
|
|
|
|
2007-09-13 17:47:42 -07:00
|
|
|
NS_LOG_COMPONENT_DEFINE ("Buffer");
|
2006-08-29 17:42:13 +02:00
|
|
|
|
2008-05-28 22:06:14 -07:00
|
|
|
#define LOG_INTERNAL_STATE(y) \
|
2010-07-01 12:50:22 +02:00
|
|
|
NS_LOG_LOGIC (y << "start="<<m_start<<", end="<<m_end<<", zero start="<<m_zeroAreaStart<< \
|
|
|
|
|
", zero end="<<m_zeroAreaEnd<<", count="<<m_data->m_count<<", size="<<m_data->m_size<< \
|
|
|
|
|
", dirty start="<<m_data->m_dirtyStart<<", dirty end="<<m_data->m_dirtyEnd)
|
2007-09-10 16:45:52 +02:00
|
|
|
|
2007-09-12 11:16:18 +02:00
|
|
|
#ifdef BUFFER_HEURISTICS
|
|
|
|
|
#define HEURISTICS(x) x
|
|
|
|
|
#else
|
|
|
|
|
#define HEURISTICS(x)
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-01-31 19:18:38 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
static struct Zeroes
|
|
|
|
|
{
|
|
|
|
|
Zeroes ()
|
|
|
|
|
: size (1000)
|
|
|
|
|
{
|
|
|
|
|
memset (buffer, 0, size);
|
|
|
|
|
}
|
|
|
|
|
char buffer[1000];
|
|
|
|
|
const uint32_t size;
|
|
|
|
|
} g_zeroes;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-11 13:47:17 +02:00
|
|
|
//#define PRINT_STATS 1
|
2007-09-10 16:45:52 +02:00
|
|
|
|
2006-08-29 17:55:34 +02:00
|
|
|
namespace ns3 {
|
2006-08-29 17:42:13 +02:00
|
|
|
|
2007-09-12 11:16:18 +02:00
|
|
|
/**
|
|
|
|
|
* This data structure is variable-sized through its last member whose size
|
|
|
|
|
* is determined at allocation time and stored in the m_size field.
|
|
|
|
|
*
|
|
|
|
|
* The so-called "dirty area" describes the area in the buffer which
|
|
|
|
|
* has been reserved and used by a user. Multiple Buffer instances
|
|
|
|
|
* may reference the same BufferData object instance and may
|
|
|
|
|
* reference different parts of the underlying byte buffer. The
|
|
|
|
|
* "dirty area" is union of all the areas referenced by the Buffer
|
|
|
|
|
* instances which reference the same BufferData instance.
|
|
|
|
|
* New user data can be safely written only outside of the "dirty
|
|
|
|
|
* area" if the reference count is higher than 1 (that is, if
|
|
|
|
|
* more than one Buffer instance references the same BufferData).
|
|
|
|
|
*/
|
2007-09-10 16:45:52 +02:00
|
|
|
struct BufferData {
|
2007-09-12 11:16:18 +02:00
|
|
|
/* The reference count of an instance of this data structure.
|
|
|
|
|
* Each buffer which references an instance holds a count.
|
|
|
|
|
*/
|
2007-09-10 16:45:52 +02:00
|
|
|
uint32_t m_count;
|
2007-09-12 11:16:18 +02:00
|
|
|
/* the size of the m_data field below.
|
|
|
|
|
*/
|
2007-09-10 16:45:52 +02:00
|
|
|
uint32_t m_size;
|
2008-05-28 22:06:14 -07:00
|
|
|
/* offset from the start of the m_data field below to the
|
|
|
|
|
* start of the area in which user bytes were written.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t m_dirtyStart;
|
|
|
|
|
/* offset from the start of the m_data field below to the
|
|
|
|
|
* end of the area in which user bytes were written.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t m_dirtyEnd;
|
2007-09-12 11:16:18 +02:00
|
|
|
/* The real data buffer holds _at least_ one byte.
|
|
|
|
|
* Its real size is stored in the m_size field.
|
|
|
|
|
*/
|
2007-09-10 16:45:52 +02:00
|
|
|
uint8_t m_data[1];
|
|
|
|
|
};
|
2009-09-26 16:55:06 +02:00
|
|
|
typedef std::vector<struct BufferData*> BufferDataList;
|
2007-09-10 16:45:52 +02:00
|
|
|
|
|
|
|
|
static struct BufferData *BufferAllocate (uint32_t reqSize);
|
|
|
|
|
|
|
|
|
|
static void BufferDeallocate (struct BufferData *data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace ns3
|
2006-08-29 17:42:13 +02:00
|
|
|
|
2007-09-10 16:45:52 +02:00
|
|
|
namespace ns3 {
|
|
|
|
|
|
2007-09-12 11:16:18 +02:00
|
|
|
#ifdef BUFFER_HEURISTICS
|
2009-09-26 16:55:06 +02:00
|
|
|
/* The following macros are pretty evil but they are needed to allow us to
|
|
|
|
|
* keep track of 3 possible states for the g_freeList variable:
|
|
|
|
|
* - uninitialized means that no one has created a buffer yet
|
|
|
|
|
* so no one has created the associated free list (it is created
|
|
|
|
|
* on-demand when the first buffer is created)
|
|
|
|
|
* - initialized means that the free list exists and is valid
|
|
|
|
|
* - destroyed means that the static destructors of this compilation unit
|
|
|
|
|
* have run so, the free list has been cleared from its content
|
|
|
|
|
* The key is that in destroyed state, we are careful not re-create it
|
|
|
|
|
* which is a typical weakness of lazy evaluation schemes which use
|
|
|
|
|
* '0' as a special value to indicate both un-initialized and destroyed.
|
|
|
|
|
* Note that it is important to use '0' as the marker for un-initialized state
|
|
|
|
|
* because the variable holding this state information is initialized to zero
|
|
|
|
|
* which the compiler assigns to zero-memory which is initialized to _zero_
|
|
|
|
|
* before the constructors run so this ensures perfect handling of crazy
|
|
|
|
|
* constructor orderings.
|
|
|
|
|
*/
|
|
|
|
|
#define MAGIC_DESTROYED (~(long) 0)
|
|
|
|
|
#define IS_UNINITIALIZED(x) (x == (BufferDataList*)0)
|
|
|
|
|
#define IS_DESTROYED(x) (x == (BufferDataList*)MAGIC_DESTROYED)
|
|
|
|
|
#define IS_INITIALIZED(x) (!IS_UNINITIALIZED(x) && !IS_DESTROYED(x))
|
|
|
|
|
#define DESTROYED ((BufferDataList*)MAGIC_DESTROYED)
|
|
|
|
|
#define UNINITIALIZED ((BufferDataList*)0)
|
2007-09-10 16:45:52 +02:00
|
|
|
static uint32_t g_recommendedStart = 0;
|
2007-09-11 13:47:17 +02:00
|
|
|
static uint64_t g_nAddNoRealloc = 0;
|
|
|
|
|
static uint64_t g_nAddRealloc = 0;
|
2009-09-26 16:55:06 +02:00
|
|
|
static BufferDataList *g_freeList = 0;
|
2007-09-11 13:47:17 +02:00
|
|
|
static uint32_t g_maxSize = 0;
|
|
|
|
|
static uint64_t g_nAllocs = 0;
|
|
|
|
|
static uint64_t g_nCreates = 0;
|
2007-09-12 11:16:18 +02:00
|
|
|
#endif /* BUFFER_HEURISTICS */
|
2007-09-10 16:45:52 +02:00
|
|
|
|
2009-09-26 16:55:06 +02:00
|
|
|
static struct LocalStaticDestructor {
|
2010-03-20 21:57:51 +01:00
|
|
|
~LocalStaticDestructor(void)
|
2009-09-26 16:55:06 +02:00
|
|
|
{
|
2007-09-11 13:47:17 +02:00
|
|
|
#ifdef PRINT_STATS
|
2007-09-12 11:16:18 +02:00
|
|
|
#ifdef BUFFER_HEURISTICS
|
2009-09-26 16:55:06 +02:00
|
|
|
double efficiency;
|
|
|
|
|
efficiency = g_nAllocs;
|
|
|
|
|
efficiency /= g_nCreates;
|
|
|
|
|
std::cout <<"buffer free list efficiency="<<efficiency<<" (lower is better)" << std::endl;
|
|
|
|
|
std::cout <<"buffer free list max size="<<g_maxSize<<std::endl;
|
|
|
|
|
std::cout <<"buffer free list recommended start="<<g_recommendedStart<<std::endl;
|
|
|
|
|
double addEfficiency;
|
|
|
|
|
addEfficiency = g_nAddRealloc;
|
|
|
|
|
addEfficiency /= g_nAddNoRealloc;
|
|
|
|
|
std::cout <<"buffer add efficiency=" << addEfficiency << " (lower is better)"<<std::endl;
|
|
|
|
|
//std::cout <<"n add reallocs="<< g_nAddRealloc << std::endl;
|
|
|
|
|
//std::cout <<"n add no reallocs="<< g_nAddNoRealloc << std::endl;
|
2007-09-12 11:16:18 +02:00
|
|
|
#endif /* BUFFER_HEURISTICS */
|
2007-09-11 13:47:17 +02:00
|
|
|
#endif /* PRINT_STATS */
|
2009-09-26 16:55:06 +02:00
|
|
|
if (IS_INITIALIZED(g_freeList))
|
|
|
|
|
{
|
|
|
|
|
for (BufferDataList::iterator i = g_freeList->begin ();
|
|
|
|
|
i != g_freeList->end (); i++)
|
|
|
|
|
{
|
|
|
|
|
BufferDeallocate (*i);
|
|
|
|
|
}
|
|
|
|
|
delete g_freeList;
|
|
|
|
|
g_freeList = DESTROYED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} g_localStaticDestructor;
|
2007-07-23 13:58:53 +02:00
|
|
|
|
2007-09-10 16:45:52 +02:00
|
|
|
struct BufferData *
|
|
|
|
|
BufferAllocate (uint32_t reqSize)
|
2006-08-29 17:42:13 +02:00
|
|
|
{
|
2006-11-01 13:11:30 +01:00
|
|
|
if (reqSize == 0)
|
|
|
|
|
{
|
|
|
|
|
reqSize = 1;
|
|
|
|
|
}
|
2007-02-16 09:56:21 +01:00
|
|
|
NS_ASSERT (reqSize >= 1);
|
2007-09-10 16:45:52 +02:00
|
|
|
uint32_t size = reqSize - 1 + sizeof (struct BufferData);
|
2006-11-01 13:11:30 +01:00
|
|
|
uint8_t *b = new uint8_t [size];
|
2007-09-10 16:45:52 +02:00
|
|
|
struct BufferData *data = reinterpret_cast<struct BufferData*>(b);
|
2006-11-01 13:11:30 +01:00
|
|
|
data->m_size = reqSize;
|
|
|
|
|
data->m_count = 1;
|
|
|
|
|
return data;
|
2006-08-29 17:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-09-10 16:45:52 +02:00
|
|
|
BufferDeallocate (struct BufferData *data)
|
2006-08-29 17:42:13 +02:00
|
|
|
{
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (data->m_count == 0);
|
2006-11-01 13:11:30 +01:00
|
|
|
uint8_t *buf = reinterpret_cast<uint8_t *> (data);
|
|
|
|
|
delete [] buf;
|
2006-08-29 17:42:13 +02:00
|
|
|
}
|
2007-09-12 11:16:18 +02:00
|
|
|
#ifdef BUFFER_HEURISTICS
|
2006-08-29 17:42:13 +02:00
|
|
|
void
|
2007-09-10 16:45:52 +02:00
|
|
|
Buffer::Recycle (struct BufferData *data)
|
2006-08-29 17:42:13 +02:00
|
|
|
{
|
2007-02-16 09:56:21 +01:00
|
|
|
NS_ASSERT (data->m_count == 0);
|
2009-09-26 16:55:06 +02:00
|
|
|
NS_ASSERT (!IS_UNINITIALIZED(g_freeList));
|
2007-09-10 16:45:52 +02:00
|
|
|
g_maxSize = std::max (g_maxSize, data->m_size);
|
2006-11-01 13:11:30 +01:00
|
|
|
/* feed into free list */
|
2007-09-10 16:45:52 +02:00
|
|
|
if (data->m_size < g_maxSize ||
|
2009-09-26 16:55:06 +02:00
|
|
|
IS_DESTROYED(g_freeList) ||
|
|
|
|
|
g_freeList->size () > 1000)
|
2006-11-01 13:11:30 +01:00
|
|
|
{
|
2007-09-10 16:45:52 +02:00
|
|
|
BufferDeallocate (data);
|
|
|
|
|
}
|
2007-09-11 13:47:17 +02:00
|
|
|
else
|
2006-11-01 13:11:30 +01:00
|
|
|
{
|
2009-09-26 16:55:06 +02:00
|
|
|
NS_ASSERT (IS_INITIALIZED(g_freeList));
|
|
|
|
|
g_freeList->push_back (data);
|
2006-11-01 13:11:30 +01:00
|
|
|
}
|
2006-08-29 17:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
2007-09-10 16:45:52 +02:00
|
|
|
BufferData *
|
|
|
|
|
Buffer::Create (uint32_t dataSize)
|
2006-08-29 17:42:13 +02:00
|
|
|
{
|
2006-11-01 13:11:30 +01:00
|
|
|
/* try to find a buffer correctly sized. */
|
2007-09-11 13:47:17 +02:00
|
|
|
g_nCreates++;
|
2009-09-26 16:55:06 +02:00
|
|
|
if (IS_UNINITIALIZED(g_freeList))
|
2006-11-01 13:11:30 +01:00
|
|
|
{
|
2009-09-26 16:55:06 +02:00
|
|
|
g_freeList = new BufferDataList ();
|
|
|
|
|
}
|
|
|
|
|
else if (IS_INITIALIZED(g_freeList))
|
|
|
|
|
{
|
|
|
|
|
while (!g_freeList->empty ())
|
2006-11-01 13:11:30 +01:00
|
|
|
{
|
2009-09-26 16:55:06 +02:00
|
|
|
struct BufferData *data = g_freeList->back ();
|
|
|
|
|
g_freeList->pop_back ();
|
|
|
|
|
if (data->m_size >= dataSize)
|
|
|
|
|
{
|
|
|
|
|
data->m_count = 1;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
BufferDeallocate (data);
|
2006-11-01 13:11:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
2007-09-11 13:47:17 +02:00
|
|
|
g_nAllocs++;
|
|
|
|
|
struct BufferData *data = BufferAllocate (dataSize);
|
2007-02-16 09:56:21 +01:00
|
|
|
NS_ASSERT (data->m_count == 1);
|
2006-11-01 13:11:30 +01:00
|
|
|
return data;
|
2006-08-29 17:42:13 +02:00
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
void
|
2007-09-10 16:45:52 +02:00
|
|
|
Buffer::Recycle (struct BufferData *data)
|
2006-08-29 17:42:13 +02:00
|
|
|
{
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (data->m_count == 0);
|
|
|
|
|
BufferDeallocate (data);
|
2006-08-29 17:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
2007-09-10 16:45:52 +02:00
|
|
|
BufferData *
|
|
|
|
|
Buffer::Create (uint32_t size)
|
2006-08-29 17:42:13 +02:00
|
|
|
{
|
2007-09-10 16:45:52 +02:00
|
|
|
return BufferAllocate (size);
|
2006-08-29 17:42:13 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-09-07 08:34:46 +02:00
|
|
|
Buffer::Buffer ()
|
|
|
|
|
{
|
2008-05-28 10:11:08 -07:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-09-10 16:45:52 +02:00
|
|
|
Initialize (0);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer::Buffer (uint32_t dataSize)
|
|
|
|
|
{
|
2008-05-28 10:11:08 -07:00
|
|
|
NS_LOG_FUNCTION (this << dataSize);
|
2007-09-10 16:45:52 +02:00
|
|
|
Initialize (dataSize);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
2010-03-08 21:07:31 -05:00
|
|
|
Buffer::Buffer (uint32_t dataSize, bool initialize)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this << dataSize << initialize);
|
|
|
|
|
if (initialize == true)
|
|
|
|
|
{
|
|
|
|
|
Initialize (dataSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-10 16:45:52 +02:00
|
|
|
bool
|
|
|
|
|
Buffer::CheckInternalState (void) const
|
|
|
|
|
{
|
2010-03-16 19:25:56 +01:00
|
|
|
#if 0
|
|
|
|
|
// If you want to modify any code in this file, enable this checking code.
|
|
|
|
|
// Otherwise, there is not much point is enabling it because the
|
|
|
|
|
// current implementation has been fairly seriously tested and the cost
|
|
|
|
|
// of this constant checking is pretty high, even for a debug build.
|
2007-09-10 16:45:52 +02:00
|
|
|
bool offsetsOk =
|
|
|
|
|
m_start <= m_zeroAreaStart &&
|
|
|
|
|
m_zeroAreaStart <= m_zeroAreaEnd &&
|
|
|
|
|
m_zeroAreaEnd <= m_end;
|
2008-05-28 22:06:14 -07:00
|
|
|
bool dirtyOk =
|
|
|
|
|
m_start >= m_data->m_dirtyStart &&
|
|
|
|
|
m_end <= m_data->m_dirtyEnd;
|
2007-09-10 16:45:52 +02:00
|
|
|
bool internalSizeOk = m_end - (m_zeroAreaEnd - m_zeroAreaStart) <= m_data->m_size &&
|
|
|
|
|
m_start <= m_data->m_size &&
|
|
|
|
|
m_zeroAreaStart <= m_data->m_size;
|
|
|
|
|
|
2008-05-28 22:06:14 -07:00
|
|
|
bool ok = m_data->m_count > 0 && offsetsOk && dirtyOk && internalSizeOk;
|
2008-05-28 10:11:08 -07:00
|
|
|
if (!ok)
|
|
|
|
|
{
|
|
|
|
|
LOG_INTERNAL_STATE ("check " << this <<
|
2010-07-01 12:50:22 +02:00
|
|
|
", " << (offsetsOk ? "true" : "false") <<
|
|
|
|
|
", " << (dirtyOk ? "true" : "false") <<
|
|
|
|
|
", " << (internalSizeOk ? "true" : "false") << " ");
|
2008-05-28 10:11:08 -07:00
|
|
|
}
|
|
|
|
|
return ok;
|
2010-03-16 19:25:56 +01:00
|
|
|
#else
|
|
|
|
|
return true;
|
|
|
|
|
#endif
|
2007-09-10 16:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Buffer::Initialize (uint32_t zeroSize)
|
|
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << zeroSize);
|
2007-09-10 16:45:52 +02:00
|
|
|
m_data = Buffer::Create (0);
|
2007-09-12 11:16:18 +02:00
|
|
|
#ifdef BUFFER_HEURISTICS
|
2007-09-10 16:45:52 +02:00
|
|
|
m_start = std::min (m_data->m_size, g_recommendedStart);
|
2007-09-12 11:16:18 +02:00
|
|
|
m_maxZeroAreaStart = m_start;
|
|
|
|
|
#else
|
|
|
|
|
m_start = 0;
|
|
|
|
|
#endif /* BUFFER_HEURISTICS */
|
2007-09-10 16:45:52 +02:00
|
|
|
m_zeroAreaStart = m_start;
|
|
|
|
|
m_zeroAreaEnd = m_zeroAreaStart + zeroSize;
|
|
|
|
|
m_end = m_zeroAreaEnd;
|
2008-05-28 22:06:14 -07:00
|
|
|
m_data->m_dirtyStart = m_start;
|
|
|
|
|
m_data->m_dirtyEnd = m_end;
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
}
|
2007-09-07 08:34:46 +02:00
|
|
|
|
|
|
|
|
Buffer::Buffer (Buffer const&o)
|
|
|
|
|
: m_data (o.m_data),
|
2007-09-12 11:16:18 +02:00
|
|
|
#ifdef BUFFER_HEURISTICS
|
2007-09-10 16:45:52 +02:00
|
|
|
m_maxZeroAreaStart (o.m_zeroAreaStart),
|
2007-09-12 11:16:18 +02:00
|
|
|
#endif
|
2007-09-10 08:54:11 +02:00
|
|
|
m_zeroAreaStart (o.m_zeroAreaStart),
|
2007-09-10 16:45:52 +02:00
|
|
|
m_zeroAreaEnd (o.m_zeroAreaEnd),
|
2007-09-07 08:34:46 +02:00
|
|
|
m_start (o.m_start),
|
2007-09-09 10:38:22 +02:00
|
|
|
m_end (o.m_end)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2008-05-28 10:11:08 -07:00
|
|
|
NS_LOG_FUNCTION (this << &o);
|
2007-09-07 08:34:46 +02:00
|
|
|
m_data->m_count++;
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer &
|
|
|
|
|
Buffer::operator = (Buffer const&o)
|
|
|
|
|
{
|
2008-05-28 10:11:08 -07:00
|
|
|
NS_LOG_FUNCTION (this << &o);
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-07 08:34:46 +02:00
|
|
|
if (m_data != o.m_data)
|
|
|
|
|
{
|
|
|
|
|
// not assignment to self.
|
|
|
|
|
m_data->m_count--;
|
|
|
|
|
if (m_data->m_count == 0)
|
|
|
|
|
{
|
|
|
|
|
Recycle (m_data);
|
|
|
|
|
}
|
|
|
|
|
m_data = o.m_data;
|
|
|
|
|
m_data->m_count++;
|
|
|
|
|
}
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (
|
2010-07-01 12:50:22 +02:00
|
|
|
g_recommendedStart = std::max (g_recommendedStart, m_maxZeroAreaStart);
|
|
|
|
|
m_maxZeroAreaStart = o.m_maxZeroAreaStart;
|
|
|
|
|
);
|
2007-09-10 08:54:11 +02:00
|
|
|
m_zeroAreaStart = o.m_zeroAreaStart;
|
2007-09-10 16:45:52 +02:00
|
|
|
m_zeroAreaEnd = o.m_zeroAreaEnd;
|
2007-09-07 08:34:46 +02:00
|
|
|
m_start = o.m_start;
|
2007-09-09 10:38:22 +02:00
|
|
|
m_end = o.m_end;
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-07 08:34:46 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer::~Buffer ()
|
|
|
|
|
{
|
2008-05-28 10:11:08 -07:00
|
|
|
NS_LOG_FUNCTION (this);
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (g_recommendedStart = std::max (g_recommendedStart, m_maxZeroAreaStart));
|
2007-09-07 08:34:46 +02:00
|
|
|
m_data->m_count--;
|
|
|
|
|
if (m_data->m_count == 0)
|
|
|
|
|
{
|
|
|
|
|
Recycle (m_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer::Iterator
|
|
|
|
|
Buffer::Begin (void) const
|
|
|
|
|
{
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-07 18:45:00 +02:00
|
|
|
return Buffer::Iterator (this);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
Buffer::Iterator
|
|
|
|
|
Buffer::End (void) const
|
|
|
|
|
{
|
2007-09-10 16:45:52 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-07 18:45:00 +02:00
|
|
|
return Buffer::Iterator (this, false);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
2007-09-11 13:53:39 +02:00
|
|
|
uint32_t
|
|
|
|
|
Buffer::GetInternalSize (void) const
|
2007-09-07 18:45:00 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
return m_zeroAreaStart - m_start + m_end - m_zeroAreaEnd;
|
2007-09-07 18:45:00 +02:00
|
|
|
}
|
2007-09-11 13:53:39 +02:00
|
|
|
uint32_t
|
|
|
|
|
Buffer::GetInternalEnd (void) const
|
2007-09-07 18:45:00 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
return m_end - (m_zeroAreaEnd - m_zeroAreaStart);
|
2007-09-07 18:45:00 +02:00
|
|
|
}
|
2007-09-07 08:34:46 +02:00
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
bool
|
2007-09-11 13:53:39 +02:00
|
|
|
Buffer::AddAtStart (uint32_t start)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << start);
|
2008-04-24 14:52:59 -07:00
|
|
|
bool dirty;
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-05-28 22:06:14 -07:00
|
|
|
bool isDirty = m_data->m_count > 1 && m_start > m_data->m_dirtyStart;
|
2007-09-11 13:53:39 +02:00
|
|
|
if (m_start >= start && !isDirty)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
/* enough space in the buffer and not dirty.
|
|
|
|
|
* To add: |..|
|
|
|
|
|
* Before: |*****---------***|
|
|
|
|
|
* After: |***..---------***|
|
|
|
|
|
*/
|
2008-05-28 22:06:14 -07:00
|
|
|
NS_ASSERT (m_data->m_count == 1 || m_start == m_data->m_dirtyStart);
|
2007-09-11 13:53:39 +02:00
|
|
|
m_start -= start;
|
2008-05-28 22:06:14 -07:00
|
|
|
dirty = m_start > m_data->m_dirtyStart;
|
2008-05-29 12:13:42 -07:00
|
|
|
// update dirty area
|
|
|
|
|
m_data->m_dirtyStart = m_start;
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (g_nAddNoRealloc++);
|
2007-09-11 13:53:39 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uint32_t newSize = GetInternalSize () + start;
|
|
|
|
|
struct BufferData *newData = Buffer::Create (newSize);
|
|
|
|
|
memcpy (newData->m_data + start, m_data->m_data + m_start, GetInternalSize ());
|
|
|
|
|
m_data->m_count--;
|
|
|
|
|
if (m_data->m_count == 0)
|
|
|
|
|
{
|
|
|
|
|
Buffer::Recycle (m_data);
|
|
|
|
|
}
|
|
|
|
|
m_data = newData;
|
2007-09-07 08:34:46 +02:00
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
int32_t delta = start - m_start;
|
2008-04-23 08:54:29 -07:00
|
|
|
m_start += delta;
|
2007-09-11 13:53:39 +02:00
|
|
|
m_zeroAreaStart += delta;
|
|
|
|
|
m_zeroAreaEnd += delta;
|
|
|
|
|
m_end += delta;
|
2008-04-23 08:54:29 -07:00
|
|
|
m_start -= start;
|
|
|
|
|
|
2008-05-29 12:13:42 -07:00
|
|
|
// update dirty area
|
|
|
|
|
m_data->m_dirtyStart = m_start;
|
|
|
|
|
m_data->m_dirtyEnd = m_end;
|
|
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
dirty = true;
|
|
|
|
|
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (g_nAddRealloc++);
|
|
|
|
|
}
|
|
|
|
|
HEURISTICS (m_maxZeroAreaStart = std::max (m_maxZeroAreaStart, m_zeroAreaStart));
|
2007-09-13 17:47:42 -07:00
|
|
|
LOG_INTERNAL_STATE ("add start=" << start << ", ");
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-04-24 14:52:59 -07:00
|
|
|
return dirty;
|
2007-09-11 13:53:39 +02:00
|
|
|
}
|
2008-04-24 14:52:59 -07:00
|
|
|
bool
|
2007-09-11 13:53:39 +02:00
|
|
|
Buffer::AddAtEnd (uint32_t end)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << end);
|
2008-04-24 14:52:59 -07:00
|
|
|
bool dirty;
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-05-28 22:06:14 -07:00
|
|
|
bool isDirty = m_data->m_count > 1 && m_end < m_data->m_dirtyEnd;
|
2007-09-11 13:53:39 +02:00
|
|
|
if (GetInternalEnd () + end <= m_data->m_size && !isDirty)
|
2007-09-07 19:47:06 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
/* enough space in buffer and not dirty
|
|
|
|
|
* Add: |...|
|
|
|
|
|
* Before: |**----*****|
|
|
|
|
|
* After: |**----...**|
|
|
|
|
|
*/
|
2008-05-28 22:06:14 -07:00
|
|
|
NS_ASSERT (m_data->m_count == 1 || m_end == m_data->m_dirtyEnd);
|
2007-09-11 13:53:39 +02:00
|
|
|
m_end += end;
|
2008-05-29 12:13:42 -07:00
|
|
|
// update dirty area.
|
|
|
|
|
m_data->m_dirtyEnd = m_end;
|
2007-09-07 08:34:46 +02:00
|
|
|
|
2008-05-28 22:06:14 -07:00
|
|
|
dirty = m_end < m_data->m_dirtyEnd;
|
2008-04-23 08:54:29 -07:00
|
|
|
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (g_nAddNoRealloc++);
|
2007-09-11 13:53:39 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
uint32_t newSize = GetInternalSize () + end;
|
|
|
|
|
struct BufferData *newData = Buffer::Create (newSize);
|
|
|
|
|
memcpy (newData->m_data, m_data->m_data + m_start, GetInternalSize ());
|
|
|
|
|
m_data->m_count--;
|
|
|
|
|
if (m_data->m_count == 0)
|
|
|
|
|
{
|
|
|
|
|
Buffer::Recycle (m_data);
|
|
|
|
|
}
|
|
|
|
|
m_data = newData;
|
|
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
int32_t delta = -m_start;
|
2008-04-23 08:54:29 -07:00
|
|
|
m_zeroAreaStart += delta;
|
|
|
|
|
m_zeroAreaEnd += delta;
|
|
|
|
|
m_end += delta;
|
2008-04-23 09:05:18 -07:00
|
|
|
m_start += delta;
|
2007-09-11 13:53:39 +02:00
|
|
|
m_end += end;
|
|
|
|
|
|
2008-05-29 12:13:42 -07:00
|
|
|
// update dirty area
|
|
|
|
|
m_data->m_dirtyStart = m_start;
|
|
|
|
|
m_data->m_dirtyEnd = m_end;
|
|
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
dirty = true;
|
|
|
|
|
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (g_nAddRealloc++);
|
2007-09-11 13:53:39 +02:00
|
|
|
}
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (m_maxZeroAreaStart = std::max (m_maxZeroAreaStart, m_zeroAreaStart));
|
2007-09-13 17:47:42 -07:00
|
|
|
LOG_INTERNAL_STATE ("add end=" << end << ", ");
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-04-23 08:54:29 -07:00
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
return dirty;
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
2007-09-11 13:53:39 +02:00
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
void
|
2008-04-22 14:18:33 -07:00
|
|
|
Buffer::AddAtEnd (const Buffer &o)
|
|
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << &o);
|
2008-05-15 09:49:04 -07:00
|
|
|
if (m_data->m_count == 1 &&
|
|
|
|
|
m_end == m_zeroAreaEnd &&
|
2008-05-28 22:06:14 -07:00
|
|
|
m_end == m_data->m_dirtyEnd &&
|
2008-04-22 14:35:42 -07:00
|
|
|
o.m_start == o.m_zeroAreaStart &&
|
|
|
|
|
o.m_zeroAreaEnd - o.m_zeroAreaStart > 0)
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* This is an optimization which kicks in when
|
|
|
|
|
* we attempt to aggregate two buffers which contain
|
|
|
|
|
* adjacent zero areas.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t zeroSize = o.m_zeroAreaEnd - o.m_zeroAreaStart;
|
|
|
|
|
m_zeroAreaEnd += zeroSize;
|
|
|
|
|
m_end = m_zeroAreaEnd;
|
2008-05-28 22:06:14 -07:00
|
|
|
m_data->m_dirtyEnd = m_zeroAreaEnd;
|
2008-04-22 14:35:42 -07:00
|
|
|
uint32_t endData = o.m_end - o.m_zeroAreaEnd;
|
|
|
|
|
AddAtEnd (endData);
|
|
|
|
|
Buffer::Iterator dst = End ();
|
|
|
|
|
dst.Prev (endData);
|
|
|
|
|
Buffer::Iterator src = o.End ();
|
|
|
|
|
src.Prev (endData);
|
|
|
|
|
dst.Write (src, o.End ());
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-04-24 14:52:59 -07:00
|
|
|
return;
|
2008-04-22 14:35:42 -07:00
|
|
|
}
|
2008-05-15 09:49:04 -07:00
|
|
|
|
2008-04-22 14:18:33 -07:00
|
|
|
Buffer dst = CreateFullCopy ();
|
|
|
|
|
Buffer src = o.CreateFullCopy ();
|
|
|
|
|
|
|
|
|
|
dst.AddAtEnd (src.GetSize ());
|
|
|
|
|
Buffer::Iterator destStart = dst.End ();
|
|
|
|
|
destStart.Prev (src.GetSize ());
|
|
|
|
|
destStart.Write (src.Begin (), src.End ());
|
|
|
|
|
*this = dst;
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-04-22 14:18:33 -07:00
|
|
|
}
|
|
|
|
|
|
2007-09-07 08:34:46 +02:00
|
|
|
void
|
2007-09-11 13:53:39 +02:00
|
|
|
Buffer::RemoveAtStart (uint32_t start)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << start);
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
uint32_t newStart = m_start + start;
|
|
|
|
|
if (newStart <= m_zeroAreaStart)
|
2007-09-07 19:22:20 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
/* only remove start of buffer
|
|
|
|
|
*/
|
|
|
|
|
m_start = newStart;
|
2007-09-07 19:22:20 +02:00
|
|
|
}
|
2007-09-11 13:53:39 +02:00
|
|
|
else if (newStart <= m_zeroAreaEnd)
|
2007-09-07 19:22:20 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
/* remove start of buffer _and_ start of zero area
|
|
|
|
|
*/
|
|
|
|
|
uint32_t delta = newStart - m_zeroAreaStart;
|
|
|
|
|
m_start = m_zeroAreaStart;
|
|
|
|
|
m_zeroAreaEnd -= delta;
|
|
|
|
|
m_end -= delta;
|
|
|
|
|
}
|
|
|
|
|
else if (newStart <= m_end)
|
2007-09-07 19:22:20 +02:00
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
/* remove start of buffer, complete zero area, and part
|
|
|
|
|
* of end of buffer
|
|
|
|
|
*/
|
|
|
|
|
NS_ASSERT (m_end >= start);
|
|
|
|
|
uint32_t zeroSize = m_zeroAreaEnd - m_zeroAreaStart;
|
|
|
|
|
m_start = newStart - zeroSize;
|
|
|
|
|
m_end -= zeroSize;
|
|
|
|
|
m_zeroAreaStart = m_start;
|
|
|
|
|
m_zeroAreaEnd = m_start;
|
2007-09-07 19:22:20 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-09-11 13:53:39 +02:00
|
|
|
/* remove all buffer */
|
|
|
|
|
m_end -= m_zeroAreaEnd - m_zeroAreaStart;
|
|
|
|
|
m_start = m_end;
|
|
|
|
|
m_zeroAreaEnd = m_end;
|
|
|
|
|
m_zeroAreaStart = m_end;
|
2007-09-07 19:22:20 +02:00
|
|
|
}
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (m_maxZeroAreaStart = std::max (m_maxZeroAreaStart, m_zeroAreaStart));
|
2007-09-13 17:47:42 -07:00
|
|
|
LOG_INTERNAL_STATE ("rem start=" << start << ", ");
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
void
|
2007-09-11 13:53:39 +02:00
|
|
|
Buffer::RemoveAtEnd (uint32_t end)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << end);
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
uint32_t newEnd = m_end - std::min (end, m_end - m_start);
|
|
|
|
|
if (newEnd > m_zeroAreaEnd)
|
|
|
|
|
{
|
|
|
|
|
/* remove part of end of buffer */
|
|
|
|
|
m_end = newEnd;
|
|
|
|
|
}
|
|
|
|
|
else if (newEnd > m_zeroAreaStart)
|
|
|
|
|
{
|
|
|
|
|
/* remove end of buffer, part of zero area */
|
|
|
|
|
m_end = newEnd;
|
|
|
|
|
m_zeroAreaEnd = newEnd;
|
|
|
|
|
}
|
|
|
|
|
else if (newEnd > m_start)
|
|
|
|
|
{
|
|
|
|
|
/* remove end of buffer, zero area, part of start of buffer */
|
|
|
|
|
m_end = newEnd;
|
|
|
|
|
m_zeroAreaEnd = newEnd;
|
|
|
|
|
m_zeroAreaStart = newEnd;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* remove all buffer */
|
|
|
|
|
m_end = m_start;
|
|
|
|
|
m_zeroAreaEnd = m_start;
|
|
|
|
|
m_zeroAreaStart = m_start;
|
|
|
|
|
}
|
2007-09-12 11:16:18 +02:00
|
|
|
HEURISTICS (m_maxZeroAreaStart = std::max (m_maxZeroAreaStart, m_zeroAreaStart));
|
2007-09-13 17:47:42 -07:00
|
|
|
LOG_INTERNAL_STATE ("rem end=" << end << ", ");
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer
|
|
|
|
|
Buffer::CreateFragment (uint32_t start, uint32_t length) const
|
|
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this << start << length);
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
Buffer tmp = *this;
|
|
|
|
|
tmp.RemoveAtStart (start);
|
|
|
|
|
tmp.RemoveAtEnd (GetSize () - (start + length));
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer
|
|
|
|
|
Buffer::CreateFullCopy (void) const
|
|
|
|
|
{
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-09-11 13:53:39 +02:00
|
|
|
NS_ASSERT (CheckInternalState ());
|
2008-04-22 13:54:05 -07:00
|
|
|
if (m_zeroAreaEnd - m_zeroAreaStart != 0)
|
2007-09-11 13:53:39 +02:00
|
|
|
{
|
|
|
|
|
Buffer tmp;
|
|
|
|
|
tmp.AddAtStart (m_zeroAreaEnd - m_zeroAreaStart);
|
|
|
|
|
tmp.Begin ().WriteU8 (0, m_zeroAreaEnd - m_zeroAreaStart);
|
|
|
|
|
uint32_t dataStart = m_zeroAreaStart - m_start;
|
|
|
|
|
tmp.AddAtStart (dataStart);
|
|
|
|
|
tmp.Begin ().Write (m_data->m_data+m_start, dataStart);
|
|
|
|
|
uint32_t dataEnd = m_end - m_zeroAreaEnd;
|
|
|
|
|
tmp.AddAtEnd (dataEnd);
|
|
|
|
|
Buffer::Iterator i = tmp.End ();
|
|
|
|
|
i.Prev (dataEnd);
|
|
|
|
|
i.Write (m_data->m_data+m_zeroAreaStart,dataEnd);
|
2008-05-28 08:51:27 -07:00
|
|
|
NS_ASSERT (tmp.CheckInternalState ());
|
2007-09-11 13:53:39 +02:00
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 21:07:31 -05:00
|
|
|
uint32_t
|
|
|
|
|
Buffer::GetSerializedSize (void) const
|
|
|
|
|
{
|
|
|
|
|
uint32_t dataStart = (m_zeroAreaStart - m_start + 3) & (~0x3);
|
|
|
|
|
uint32_t dataEnd = (m_end - m_zeroAreaEnd + 3) & (~0x3);
|
|
|
|
|
|
|
|
|
|
// total size 4-bytes for dataStart length
|
|
|
|
|
// + X number of bytes for dataStart
|
|
|
|
|
// + 4-bytes for dataEnd length
|
|
|
|
|
// + X number of bytes for dataEnd
|
|
|
|
|
uint32_t sz = sizeof (uint32_t)
|
|
|
|
|
+ sizeof (uint32_t)
|
|
|
|
|
+ dataStart
|
|
|
|
|
+ sizeof (uint32_t)
|
|
|
|
|
+ dataEnd;
|
|
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
Buffer::Serialize (uint8_t* buffer, uint32_t maxSize) const
|
|
|
|
|
{
|
2010-05-06 14:55:16 -04:00
|
|
|
uint32_t* p = reinterpret_cast<uint32_t *> (buffer);
|
2010-03-08 21:07:31 -05:00
|
|
|
uint32_t size = 0;
|
2010-07-01 12:50:22 +02:00
|
|
|
|
2010-03-08 21:07:31 -05:00
|
|
|
NS_LOG_FUNCTION (this);
|
|
|
|
|
|
|
|
|
|
// Add the zero data length
|
|
|
|
|
if (size + 4 <= maxSize)
|
|
|
|
|
{
|
|
|
|
|
size += 4;
|
|
|
|
|
*p++ = m_zeroAreaEnd - m_zeroAreaStart;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the length of actual start data
|
|
|
|
|
uint32_t dataStartLength = m_zeroAreaStart - m_start;
|
|
|
|
|
if (size + 4 <= maxSize)
|
|
|
|
|
{
|
|
|
|
|
size += 4;
|
|
|
|
|
*p++ = dataStartLength;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the actual data
|
|
|
|
|
if (size + ((dataStartLength + 3) & (~3)) <= maxSize)
|
|
|
|
|
{
|
|
|
|
|
size += (dataStartLength + 3) & (~3);
|
|
|
|
|
memcpy(p, m_data->m_data + m_start, dataStartLength);
|
|
|
|
|
p += (((dataStartLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the length of the actual end data
|
|
|
|
|
uint32_t dataEndLength = m_end - m_zeroAreaEnd;
|
|
|
|
|
if (size + 4 <= maxSize)
|
|
|
|
|
{
|
|
|
|
|
size += 4;
|
|
|
|
|
*p++ = dataEndLength;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the actual data
|
|
|
|
|
if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
|
|
|
|
|
{
|
|
|
|
|
size += (dataEndLength + 3) & (~3);
|
|
|
|
|
memcpy(p, m_data->m_data+m_zeroAreaStart,dataEndLength);
|
|
|
|
|
p += (((dataEndLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Serialzed everything successfully
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
2010-05-06 14:55:16 -04:00
|
|
|
Buffer::Deserialize (const uint8_t *buffer, uint32_t size)
|
2010-03-08 21:07:31 -05:00
|
|
|
{
|
2010-05-06 14:55:16 -04:00
|
|
|
const uint32_t* p = reinterpret_cast<const uint32_t *> (buffer);
|
2010-03-08 21:07:31 -05:00
|
|
|
uint32_t sizeCheck = size-4;
|
|
|
|
|
|
|
|
|
|
NS_ASSERT (sizeCheck >= 4);
|
|
|
|
|
uint32_t zeroDataLength = *p++;
|
|
|
|
|
sizeCheck -= 4;
|
|
|
|
|
|
|
|
|
|
// Create zero bytes
|
|
|
|
|
Initialize (zeroDataLength);
|
2010-07-01 12:50:22 +02:00
|
|
|
|
2010-03-08 21:07:31 -05:00
|
|
|
// Add start data
|
|
|
|
|
NS_ASSERT (sizeCheck >= 4);
|
|
|
|
|
uint32_t dataStartLength = *p++;
|
|
|
|
|
sizeCheck -= 4;
|
|
|
|
|
AddAtStart (dataStartLength);
|
|
|
|
|
|
|
|
|
|
NS_ASSERT (sizeCheck >= dataStartLength);
|
2010-05-06 14:55:16 -04:00
|
|
|
Begin ().Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataStartLength);
|
|
|
|
|
p += (((dataStartLength+3)&(~3))/4); // Advance p, insuring 4 byte boundary
|
2010-03-08 21:07:31 -05:00
|
|
|
sizeCheck -= ((dataStartLength+3)&(~3));
|
|
|
|
|
|
|
|
|
|
// Add end data
|
|
|
|
|
NS_ASSERT (sizeCheck >= 4);
|
|
|
|
|
uint32_t dataEndLength = *p++;
|
|
|
|
|
sizeCheck -= 4;
|
|
|
|
|
AddAtEnd (dataEndLength);
|
|
|
|
|
|
|
|
|
|
NS_ASSERT (sizeCheck >= dataEndLength);
|
|
|
|
|
Buffer::Iterator tmp = End ();
|
|
|
|
|
tmp.Prev (dataEndLength);
|
2010-05-06 14:55:16 -04:00
|
|
|
tmp.Write (reinterpret_cast<uint8_t *> (const_cast<uint32_t *> (p)), dataEndLength);
|
|
|
|
|
p += (((dataEndLength+3)&(~3))/4); // Advance p, insuring 4 byte boundary
|
2010-03-08 21:07:31 -05:00
|
|
|
sizeCheck -= ((dataEndLength+3)&(~3));
|
2010-07-01 12:50:22 +02:00
|
|
|
|
2010-03-08 21:07:31 -05:00
|
|
|
NS_ASSERT (sizeCheck == 0);
|
|
|
|
|
// return zero if buffer did not
|
|
|
|
|
// contain a complete message
|
|
|
|
|
return (sizeCheck != 0) ? 0 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-24 14:52:59 -07:00
|
|
|
int32_t
|
|
|
|
|
Buffer::GetCurrentStartOffset (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_start;
|
|
|
|
|
}
|
|
|
|
|
int32_t
|
|
|
|
|
Buffer::GetCurrentEndOffset (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-11 13:53:39 +02:00
|
|
|
void
|
|
|
|
|
Buffer::TransformIntoRealBuffer (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
Buffer tmp = CreateFullCopy ();
|
|
|
|
|
*const_cast<Buffer *> (this) = tmp;
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t const*
|
|
|
|
|
Buffer::PeekData (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
TransformIntoRealBuffer ();
|
|
|
|
|
NS_ASSERT (CheckInternalState ());
|
|
|
|
|
return m_data->m_data + m_start;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-08 16:43:21 +02:00
|
|
|
void
|
|
|
|
|
Buffer::CopyData(std::ostream *os, uint32_t size) const
|
|
|
|
|
{
|
2009-08-13 09:36:16 +02:00
|
|
|
if (size > 0)
|
2009-06-08 16:43:21 +02:00
|
|
|
{
|
2009-08-13 09:36:16 +02:00
|
|
|
uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size);
|
|
|
|
|
os->write((const char*)(m_data->m_data + m_start), tmpsize);
|
|
|
|
|
if (size > tmpsize)
|
|
|
|
|
{
|
|
|
|
|
size -= m_zeroAreaStart-m_start;
|
|
|
|
|
tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size);
|
2010-01-31 19:18:38 +01:00
|
|
|
uint32_t left = tmpsize;
|
|
|
|
|
while (left > 0)
|
2009-08-13 09:36:16 +02:00
|
|
|
{
|
2010-01-31 19:18:38 +01:00
|
|
|
uint32_t toWrite = std::min (left, g_zeroes.size);
|
|
|
|
|
os->write (g_zeroes.buffer, toWrite);
|
|
|
|
|
left -= toWrite;
|
2009-08-13 09:36:16 +02:00
|
|
|
}
|
|
|
|
|
if (size > tmpsize)
|
|
|
|
|
{
|
|
|
|
|
size -= tmpsize;
|
|
|
|
|
tmpsize = std::min (m_end - m_zeroAreaEnd, size);
|
|
|
|
|
os->write ((const char*)(m_data->m_data + m_zeroAreaStart), tmpsize);
|
|
|
|
|
}
|
2009-06-08 16:43:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-01 08:28:11 +01:00
|
|
|
uint32_t
|
|
|
|
|
Buffer::CopyData (uint8_t *buffer, uint32_t size) const
|
|
|
|
|
{
|
|
|
|
|
uint32_t originalSize = size;
|
|
|
|
|
if (size > 0)
|
|
|
|
|
{
|
|
|
|
|
uint32_t tmpsize = std::min (m_zeroAreaStart-m_start, size);
|
|
|
|
|
memcpy (buffer, (const char*)(m_data->m_data + m_start), tmpsize);
|
|
|
|
|
buffer += tmpsize;
|
|
|
|
|
if (size > tmpsize)
|
|
|
|
|
{
|
|
|
|
|
size -= m_zeroAreaStart-m_start;
|
|
|
|
|
tmpsize = std::min (m_zeroAreaEnd - m_zeroAreaStart, size);
|
|
|
|
|
uint32_t left = tmpsize;
|
|
|
|
|
while (left > 0)
|
|
|
|
|
{
|
|
|
|
|
uint32_t toWrite = std::min (left, g_zeroes.size);
|
|
|
|
|
memcpy (buffer, g_zeroes.buffer, toWrite);
|
|
|
|
|
left -= toWrite;
|
|
|
|
|
buffer += toWrite;
|
|
|
|
|
}
|
|
|
|
|
if (size > tmpsize)
|
|
|
|
|
{
|
|
|
|
|
size -= tmpsize;
|
|
|
|
|
tmpsize = std::min (m_end - m_zeroAreaEnd, size);
|
|
|
|
|
memcpy (buffer, (const char*)(m_data->m_data + m_zeroAreaStart), tmpsize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return originalSize - size;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-11 13:53:39 +02:00
|
|
|
/******************************************************
|
|
|
|
|
* The buffer iterator below.
|
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buffer::Iterator::Iterator ()
|
|
|
|
|
: m_zeroStart (0),
|
|
|
|
|
m_zeroEnd (0),
|
|
|
|
|
m_dataStart (0),
|
|
|
|
|
m_dataEnd (0),
|
|
|
|
|
m_current (0),
|
|
|
|
|
m_data (0)
|
2010-07-01 12:50:22 +02:00
|
|
|
{
|
|
|
|
|
}
|
2007-09-11 13:53:39 +02:00
|
|
|
Buffer::Iterator::Iterator (Buffer const*buffer)
|
|
|
|
|
{
|
|
|
|
|
Construct (buffer);
|
|
|
|
|
m_current = m_dataStart;
|
|
|
|
|
}
|
|
|
|
|
Buffer::Iterator::Iterator (Buffer const*buffer, bool dummy)
|
|
|
|
|
{
|
|
|
|
|
Construct (buffer);
|
|
|
|
|
m_current = m_dataEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Construct (const Buffer *buffer)
|
|
|
|
|
{
|
|
|
|
|
m_zeroStart = buffer->m_zeroAreaStart;
|
|
|
|
|
m_zeroEnd = buffer->m_zeroAreaEnd;
|
|
|
|
|
m_dataStart = buffer->m_start;
|
|
|
|
|
m_dataEnd = buffer->m_end;
|
|
|
|
|
m_data = buffer->m_data->m_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Next (void)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_current + 1 <= m_dataEnd);
|
|
|
|
|
m_current++;
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Prev (void)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_current >= 1);
|
|
|
|
|
m_current--;
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Next (uint32_t delta)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_current + delta <= m_dataEnd);
|
|
|
|
|
m_current += delta;
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Prev (uint32_t delta)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_current >= delta);
|
|
|
|
|
m_current -= delta;
|
|
|
|
|
}
|
|
|
|
|
uint32_t
|
|
|
|
|
Buffer::Iterator::GetDistanceFrom (Iterator const &o) const
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_data == o.m_data);
|
|
|
|
|
int32_t diff = m_current - o.m_current;
|
|
|
|
|
if (diff < 0)
|
|
|
|
|
{
|
|
|
|
|
return -diff;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return diff;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Buffer::Iterator::IsEnd (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_current == m_dataEnd;
|
|
|
|
|
}
|
|
|
|
|
bool
|
|
|
|
|
Buffer::Iterator::IsStart (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_current == m_dataStart;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-11 14:56:10 +02:00
|
|
|
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 &&
|
2010-07-01 12:50:22 +02:00
|
|
|
!(i >= m_zeroStart && i < m_zeroEnd) &&
|
|
|
|
|
i <= m_dataEnd;
|
2007-09-11 14:56:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-11 13:53:39 +02:00
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Write (Iterator start, Iterator end)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (start.m_data == end.m_data);
|
|
|
|
|
NS_ASSERT (start.m_current <= end.m_current);
|
|
|
|
|
NS_ASSERT (start.m_zeroStart == end.m_zeroStart);
|
|
|
|
|
NS_ASSERT (start.m_zeroEnd == end.m_zeroEnd);
|
2008-05-28 22:06:14 -07:00
|
|
|
NS_ASSERT (m_data != start.m_data);
|
2007-09-11 13:53:39 +02:00
|
|
|
uint32_t size = end.m_current - start.m_current;
|
|
|
|
|
Iterator cur = start;
|
2010-07-01 12:38:06 +02:00
|
|
|
NS_ASSERT_MSG (CheckNoZero (m_current, m_current + size),
|
|
|
|
|
GetWriteErrorMessage ());
|
2010-03-19 14:15:19 +01:00
|
|
|
if (start.m_current <= start.m_zeroStart)
|
|
|
|
|
{
|
|
|
|
|
uint32_t toCopy = std::min (size, start.m_zeroStart - start.m_current);
|
|
|
|
|
memcpy (&m_data[m_current], &start.m_data[start.m_current], toCopy);
|
|
|
|
|
start.m_current += toCopy;
|
|
|
|
|
m_current += toCopy;
|
|
|
|
|
size -= toCopy;
|
|
|
|
|
}
|
|
|
|
|
if (start.m_current <= start.m_zeroEnd)
|
2007-09-11 13:53:39 +02:00
|
|
|
{
|
2010-03-19 14:15:19 +01:00
|
|
|
uint32_t toCopy = std::min (size, start.m_zeroEnd - start.m_current);
|
|
|
|
|
memset (&m_data[m_current], 0, toCopy);
|
|
|
|
|
start.m_current += toCopy;
|
|
|
|
|
m_current += toCopy;
|
|
|
|
|
size -= toCopy;
|
2007-09-11 13:53:39 +02:00
|
|
|
}
|
2010-03-19 14:15:19 +01:00
|
|
|
uint32_t toCopy = std::min (size, start.m_dataEnd - start.m_current);
|
|
|
|
|
uint8_t *from = &start.m_data[start.m_current - (start.m_zeroEnd-start.m_zeroStart)];
|
|
|
|
|
uint8_t *to = &m_data[m_current];
|
|
|
|
|
memcpy (to, from, toCopy);
|
|
|
|
|
m_current += toCopy;
|
2007-09-11 13:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteU16 (uint16_t data)
|
|
|
|
|
{
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
2007-09-07 19:39:47 +02:00
|
|
|
WriteU8 (data & 0xff);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteU32 (uint32_t data)
|
|
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteU64 (uint64_t data)
|
|
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
WriteU8 (data & 0xff);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
2008-03-28 18:45:10 -07:00
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteHtolsbU16 (uint16_t data)
|
|
|
|
|
{
|
|
|
|
|
WriteU8 ((data >> 0) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 8) & 0xff);
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteHtolsbU32 (uint32_t data)
|
|
|
|
|
{
|
|
|
|
|
WriteU8 ((data >> 0) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 8) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 16) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 24) & 0xff);
|
|
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteHtolsbU64 (uint64_t data)
|
|
|
|
|
{
|
|
|
|
|
WriteU8 ((data >> 0) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 8) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 16) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 24) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 32) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 40) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 48) & 0xff);
|
2009-05-07 09:13:06 +02:00
|
|
|
WriteU8 ((data >> 56) & 0xff);
|
2008-03-28 18:45:10 -07:00
|
|
|
}
|
|
|
|
|
|
2007-09-07 08:34:46 +02:00
|
|
|
void
|
|
|
|
|
Buffer::Iterator::WriteHtonU64 (uint64_t data)
|
|
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
WriteU8 ((data >> 56) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 48) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 40) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 32) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 24) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 16) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 8) & 0xff);
|
|
|
|
|
WriteU8 ((data >> 0) & 0xff);
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
void
|
|
|
|
|
Buffer::Iterator::Write (uint8_t const*buffer, uint32_t size)
|
|
|
|
|
{
|
2010-07-01 12:38:06 +02:00
|
|
|
NS_ASSERT_MSG (CheckNoZero (m_current, size),
|
|
|
|
|
GetWriteErrorMessage ());
|
2010-03-19 14:15:19 +01:00
|
|
|
uint8_t *to;
|
|
|
|
|
if (m_current <= m_zeroStart)
|
|
|
|
|
{
|
|
|
|
|
to = &m_data[m_current];
|
|
|
|
|
}
|
|
|
|
|
else
|
2007-09-07 19:39:47 +02:00
|
|
|
{
|
2010-03-19 14:15:19 +01:00
|
|
|
to = &m_data[m_current - (m_zeroEnd - m_zeroStart)];
|
2007-09-07 19:39:47 +02:00
|
|
|
}
|
2010-03-19 14:15:19 +01:00
|
|
|
memcpy (to, buffer, size);
|
|
|
|
|
m_current += size;
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
|
Buffer::Iterator::ReadU16 (void)
|
|
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
uint8_t byte0 = ReadU8 ();
|
|
|
|
|
uint8_t byte1 = ReadU8 ();
|
|
|
|
|
uint16_t data = byte1;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte0;
|
|
|
|
|
|
|
|
|
|
return data;
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
uint32_t
|
|
|
|
|
Buffer::Iterator::ReadU32 (void)
|
|
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
uint8_t byte0 = ReadU8 ();
|
|
|
|
|
uint8_t byte1 = ReadU8 ();
|
|
|
|
|
uint8_t byte2 = ReadU8 ();
|
|
|
|
|
uint8_t byte3 = ReadU8 ();
|
|
|
|
|
uint32_t data = byte3;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte2;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte1;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte0;
|
|
|
|
|
return data;
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
uint64_t
|
|
|
|
|
Buffer::Iterator::ReadU64 (void)
|
|
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
uint8_t byte0 = ReadU8 ();
|
|
|
|
|
uint8_t byte1 = ReadU8 ();
|
|
|
|
|
uint8_t byte2 = ReadU8 ();
|
|
|
|
|
uint8_t byte3 = ReadU8 ();
|
|
|
|
|
uint8_t byte4 = ReadU8 ();
|
|
|
|
|
uint8_t byte5 = ReadU8 ();
|
|
|
|
|
uint8_t byte6 = ReadU8 ();
|
|
|
|
|
uint8_t byte7 = ReadU8 ();
|
2009-05-07 09:13:06 +02:00
|
|
|
uint64_t data = byte7;
|
2007-09-07 19:39:47 +02:00
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte6;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte5;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte4;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte3;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte2;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte1;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte0;
|
|
|
|
|
|
|
|
|
|
return data;
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
uint16_t
|
2010-03-20 21:57:51 +01:00
|
|
|
Buffer::Iterator::SlowReadNtohU16 (void)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
|
|
|
|
uint16_t retval = 0;
|
2007-09-07 19:39:47 +02:00
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
2007-09-07 08:34:46 +02:00
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
uint32_t
|
2010-03-20 21:57:51 +01:00
|
|
|
Buffer::Iterator::SlowReadNtohU32 (void)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
|
|
|
|
uint32_t retval = 0;
|
2007-09-07 19:39:47 +02:00
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
2007-09-07 08:34:46 +02:00
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
uint64_t
|
|
|
|
|
Buffer::Iterator::ReadNtohU64 (void)
|
|
|
|
|
{
|
|
|
|
|
uint64_t retval = 0;
|
2007-09-07 19:39:47 +02:00
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
|
|
|
|
retval <<= 8;
|
|
|
|
|
retval |= ReadU8 ();
|
2007-09-07 08:34:46 +02:00
|
|
|
return retval;
|
|
|
|
|
}
|
2008-03-28 18:45:10 -07:00
|
|
|
uint16_t
|
|
|
|
|
Buffer::Iterator::ReadLsbtohU16 (void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t byte0 = ReadU8 ();
|
|
|
|
|
uint8_t byte1 = ReadU8 ();
|
|
|
|
|
uint16_t data = byte1;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte0;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
uint32_t
|
|
|
|
|
Buffer::Iterator::ReadLsbtohU32 (void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t byte0 = ReadU8 ();
|
|
|
|
|
uint8_t byte1 = ReadU8 ();
|
|
|
|
|
uint8_t byte2 = ReadU8 ();
|
|
|
|
|
uint8_t byte3 = ReadU8 ();
|
|
|
|
|
uint32_t data = byte3;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte2;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte1;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte0;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
uint64_t
|
|
|
|
|
Buffer::Iterator::ReadLsbtohU64 (void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t byte0 = ReadU8 ();
|
|
|
|
|
uint8_t byte1 = ReadU8 ();
|
|
|
|
|
uint8_t byte2 = ReadU8 ();
|
|
|
|
|
uint8_t byte3 = ReadU8 ();
|
|
|
|
|
uint8_t byte4 = ReadU8 ();
|
|
|
|
|
uint8_t byte5 = ReadU8 ();
|
|
|
|
|
uint8_t byte6 = ReadU8 ();
|
|
|
|
|
uint8_t byte7 = ReadU8 ();
|
2009-05-07 09:13:06 +02:00
|
|
|
uint64_t data = byte7;
|
2008-03-28 18:45:10 -07:00
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte6;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte5;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte4;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte3;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte2;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte1;
|
|
|
|
|
data <<= 8;
|
|
|
|
|
data |= byte0;
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2007-09-07 08:34:46 +02:00
|
|
|
void
|
2007-09-07 19:39:47 +02:00
|
|
|
Buffer::Iterator::Read (uint8_t *buffer, uint32_t size)
|
2007-09-07 08:34:46 +02:00
|
|
|
{
|
2007-09-07 19:39:47 +02:00
|
|
|
for (uint32_t i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
buffer[i] = ReadU8 ();
|
|
|
|
|
}
|
2007-09-07 08:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-01 10:52:11 -07:00
|
|
|
uint16_t
|
|
|
|
|
Buffer::Iterator::CalculateIpChecksum(uint16_t size)
|
|
|
|
|
{
|
|
|
|
|
return CalculateIpChecksum(size, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
|
Buffer::Iterator::CalculateIpChecksum(uint16_t size, uint32_t initialChecksum)
|
|
|
|
|
{
|
|
|
|
|
/* see RFC 1071 to understand this code. */
|
|
|
|
|
uint32_t sum = initialChecksum;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < size/2; j++)
|
|
|
|
|
sum += ReadU16 ();
|
|
|
|
|
|
|
|
|
|
if (size & 1)
|
2010-07-01 12:50:22 +02:00
|
|
|
sum += ReadU8 ();
|
2008-07-01 10:52:11 -07:00
|
|
|
|
|
|
|
|
while (sum >> 16)
|
|
|
|
|
sum = (sum & 0xffff) + (sum >> 16);
|
|
|
|
|
return ~sum;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-10 15:58:24 -07:00
|
|
|
uint32_t
|
|
|
|
|
Buffer::Iterator::GetSize (void) const
|
|
|
|
|
{
|
|
|
|
|
return m_dataEnd - m_dataStart;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-01 12:38:06 +02:00
|
|
|
|
|
|
|
|
std::string
|
|
|
|
|
Buffer::Iterator::GetReadErrorMessage (void) const
|
|
|
|
|
{
|
|
|
|
|
std::string str = "You have attempted to read beyond the bounds of the "
|
|
|
|
|
"available buffer space. This usually indicates that a "
|
|
|
|
|
"Header::Deserialize or Trailer::Deserialize method "
|
|
|
|
|
"is trying to read data which was not written by "
|
|
|
|
|
"a Header::Serialize or Trailer::Serialize method. "
|
|
|
|
|
"In short: check the code of your Serialize and Deserialize "
|
|
|
|
|
"methods.";
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
std::string
|
|
|
|
|
Buffer::Iterator::GetWriteErrorMessage (void) const
|
|
|
|
|
{
|
|
|
|
|
std::string str;
|
|
|
|
|
if (m_current < m_dataStart)
|
|
|
|
|
{
|
|
|
|
|
str = "You have attempted to write before the start of the available "
|
|
|
|
|
"buffer space. This usually indicates that Trailer::GetSerializedSize "
|
|
|
|
|
"returned a size which is too small compared to what Trailer::Serialize "
|
|
|
|
|
"is actually using.";
|
|
|
|
|
}
|
|
|
|
|
else if (m_current >= m_dataEnd)
|
|
|
|
|
{
|
|
|
|
|
str = "You have attempted to write after the end of the available "
|
|
|
|
|
"buffer space. This usually indicates that Header::GetSerializedSize "
|
|
|
|
|
"returned a size which is too small compared to what Header::Serialize "
|
|
|
|
|
"is actually using.";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (m_current >= m_zeroStart && m_current < m_zeroEnd);
|
|
|
|
|
str = "You have attempted to write inside the payload area of the "
|
|
|
|
|
"buffer. This usually indicates that your Serialize method uses more "
|
|
|
|
|
"buffer space than what your GetSerialized method returned.";
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-07 08:34:46 +02:00
|
|
|
} // namespace ns3
|
2006-08-29 17:42:13 +02:00
|
|
|
|
|
|
|
|
|