Skip to content

Commit

Permalink
network: Use early return
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andre authored and edalm committed Jan 10, 2024
1 parent 55114f4 commit b988187
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 205 deletions.
66 changes: 30 additions & 36 deletions src/network/model/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,66 +586,60 @@ Buffer::Serialize(uint8_t* buffer, uint32_t maxSize) const
uint32_t size = 0;

// Add the zero data length
if (size + 4 <= maxSize)
{
size += 4;
*p++ = m_zeroAreaEnd - m_zeroAreaStart;
}
else
size += 4;

if (size > maxSize)
{
return 0;
}

*p++ = m_zeroAreaEnd - m_zeroAreaStart;

// Add the length of actual start data
uint32_t dataStartLength = m_zeroAreaStart - m_start;
if (size + 4 <= maxSize)
{
size += 4;
*p++ = dataStartLength;
}
else
size += 4;

if (size > maxSize)
{
return 0;
}

uint32_t dataStartLength = m_zeroAreaStart - m_start;
*p++ = dataStartLength;

// 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
size += (dataStartLength + 3) & (~3);

if (size > maxSize)
{
return 0;
}

memcpy(p, m_data->m_data + m_start, dataStartLength);
p += (((dataStartLength + 3) & (~3)) / 4); // Advance p, insuring 4 byte boundary

// Add the length of the actual end data
uint32_t dataEndLength = m_end - m_zeroAreaEnd;
if (size + 4 <= maxSize)
{
size += 4;
*p++ = dataEndLength;
}
else
size += 4;

if (size > maxSize)
{
return 0;
}

uint32_t dataEndLength = m_end - m_zeroAreaEnd;
*p++ = dataEndLength;

// Add the actual data
if (size + ((dataEndLength + 3) & (~3)) <= maxSize)
{
// The following line is unnecessary.
// size += (dataEndLength + 3) & (~3);
memcpy(p, m_data->m_data + m_zeroAreaStart, dataEndLength);
// The following line is unnecessary.
// p += (((dataEndLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary
}
else
size += (dataEndLength + 3) & (~3);

if (size > maxSize)
{
return 0;
}

memcpy(p, m_data->m_data + m_zeroAreaStart, dataEndLength);
// The following line is unnecessary.
// p += (((dataEndLength + 3) & (~3))/4); // Advance p, insuring 4 byte boundary

// Serialized everything successfully
return 1;
}
Expand Down
73 changes: 32 additions & 41 deletions src/network/model/byte-tag-list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,19 +470,16 @@ ByteTagList::Serialize(uint32_t* buffer, uint32_t maxSize) const
uint32_t* p = buffer;
uint32_t size = 0;

uint32_t* numberOfTags = nullptr;
size += 4;

if (size + 4 <= maxSize)
{
numberOfTags = p;
*p++ = 0;
size += 4;
}
else
if (size > maxSize)
{
return 0;
}

uint32_t* numberOfTags = p;
*p++ = 0;

ByteTagList::Iterator i = BeginAll();
while (i.HasNext())
{
Expand All @@ -492,62 +489,56 @@ ByteTagList::Serialize(uint32_t* buffer, uint32_t maxSize) const

// ensure size is multiple of 4 bytes for 4 byte boundaries
uint32_t hashSize = (sizeof(TypeId::hash_t) + 3) & (~3);
if (size + hashSize <= maxSize)
{
TypeId::hash_t tid = item.tid.GetHash();
memcpy(p, &tid, sizeof(TypeId::hash_t));
p += hashSize / 4;
size += hashSize;
}
else
size += hashSize;

if (size > maxSize)
{
return 0;
}

if (size + 4 <= maxSize)
{
*p++ = item.size;
size += 4;
}
else
TypeId::hash_t tid = item.tid.GetHash();
memcpy(p, &tid, sizeof(TypeId::hash_t));
p += hashSize / 4;

size += 4;

if (size > maxSize)
{
return 0;
}

if (size + 4 <= maxSize)
{
*p++ = item.start;
size += 4;
}
else
*p++ = item.size;

size += 4;

if (size > maxSize)
{
return 0;
}

if (size + 4 <= maxSize)
{
*p++ = item.end;
size += 4;
}
else
*p++ = item.start;

size += 4;

if (size > maxSize)
{
return 0;
}

*p++ = item.end;

// ensure size is multiple of 4 bytes for 4 byte boundaries
uint32_t tagWordSize = (item.size + 3) & (~3);
size += tagWordSize;

if (size + tagWordSize <= maxSize)
{
item.buf.Read(reinterpret_cast<uint8_t*>(p), item.size);
size += tagWordSize;
p += tagWordSize / 4;
}
else
if (size > maxSize)
{
return 0;
}

item.buf.Read(reinterpret_cast<uint8_t*>(p), item.size);
p += tagWordSize / 4;

(*numberOfTags)++;
}

Expand Down
52 changes: 23 additions & 29 deletions src/network/model/packet-tag-list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,60 +336,54 @@ PacketTagList::Serialize(uint32_t* buffer, uint32_t maxSize) const
uint32_t* p = buffer;
uint32_t size = 0;

uint32_t* numberOfTags = nullptr;
size += 4;

if (size + 4 <= maxSize)
{
numberOfTags = p;
*p++ = 0;
size += 4;
}
else
if (size > maxSize)
{
return 0;
}

uint32_t* numberOfTags = p;
*p++ = 0;

for (TagData* cur = m_next; cur != nullptr; cur = cur->next)
{
if (size + 4 <= maxSize)
{
*p++ = cur->size;
size += 4;
}
else
size += 4;

if (size > maxSize)
{
return 0;
}

*p++ = cur->size;

NS_LOG_INFO("Serializing tag id " << cur->tid);

// ensure size is multiple of 4 bytes for 4 byte boundaries
uint32_t hashSize = (sizeof(TypeId::hash_t) + 3) & (~3);
if (size + hashSize <= maxSize)
{
TypeId::hash_t tid = cur->tid.GetHash();
memcpy(p, &tid, sizeof(TypeId::hash_t));
p += hashSize / 4;
size += hashSize;
}
else
size += hashSize;

if (size > maxSize)
{
return 0;
}

TypeId::hash_t tid = cur->tid.GetHash();
memcpy(p, &tid, sizeof(TypeId::hash_t));
p += hashSize / 4;

// ensure size is multiple of 4 bytes for 4 byte boundaries
uint32_t tagWordSize = (cur->size + 3) & (~3);
if (size + tagWordSize <= maxSize)
{
memcpy(p, cur->data, cur->size);
size += tagWordSize;
p += tagWordSize / 4;
}
else
size += tagWordSize;

if (size > maxSize)
{
return 0;
}

memcpy(p, cur->data, cur->size);
p += tagWordSize / 4;

(*numberOfTags)++;
}

Expand Down
Loading

0 comments on commit b988187

Please sign in to comment.