Skip to content

Commit

Permalink
Update TestSystemPacketBuffer (#4305)
Browse files Browse the repository at this point in the history
* Update TestSystemPacketBuffer

#### Problem

Uses of `PacketBuffer` have largely been replaced by
`PacketBufferHandle`. The unit test had not kept up.

#### Summary of Changes

- Added tests for PacketBufferHandle specifically.
- Added many checks of reference counts to verify the ownership model.
- Some more refactoring into `PacketBufferTest`, friend class of
  `PacketBuffer` and `PacketBufferHandle`, to use private fields
  and track buffer allocations.

part of #2707 - Figure out a way to express PacketBuffer ownership in the type system

* Release buffers more often so that tests work with small pools.
  • Loading branch information
kpschoedel authored Jan 14, 2021
1 parent 178b244 commit 530f741
Show file tree
Hide file tree
Showing 3 changed files with 1,042 additions and 685 deletions.
12 changes: 5 additions & 7 deletions src/system/SystemPacketBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2016-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -158,8 +158,11 @@ uint16_t PacketBuffer::ReservedSize() const
return static_cast<uint16_t>(kDelta - CHIP_SYSTEM_PACKETBUFFER_HEADER_SIZE);
}

void PacketBuffer::AddToEnd_ForNow(PacketBuffer * aPacket)
void PacketBuffer::AddToEnd(PacketBufferHandle aPacketHandle)
{
PacketBuffer * aPacket = aPacketHandle.mBuffer;
aPacketHandle.mBuffer = nullptr;

#if CHIP_SYSTEM_CONFIG_USE_LWIP
pbuf_cat(this, aPacket);
#else // !CHIP_SYSTEM_CONFIG_USE_LWIP
Expand All @@ -179,11 +182,6 @@ void PacketBuffer::AddToEnd_ForNow(PacketBuffer * aPacket)
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
}

void PacketBuffer::AddToEnd(PacketBufferHandle aPacketHandle)
{
AddToEnd_ForNow(aPacketHandle.Release_ForNow());
}

void PacketBuffer::CompactHead()
{
uint8_t * const kStart = reinterpret_cast<uint8_t *>(this) + CHIP_SYSTEM_PACKETBUFFER_HEADER_SIZE;
Expand Down
7 changes: 4 additions & 3 deletions src/system/SystemPacketBuffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2016-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -254,7 +254,7 @@ class DLL_EXPORT PacketBuffer : private pbuf
/**
* Return the last buffer in a buffer chain.
*
* @return a handle to the next buffer in the buffer chain.
* @return a handle to the last buffer in the buffer chain.
*/
CHECK_RETURN_VALUE PacketBufferHandle Last();

Expand Down Expand Up @@ -328,7 +328,6 @@ class DLL_EXPORT PacketBuffer : private pbuf
PacketBuffer * ChainedBuffer() const { return static_cast<PacketBuffer *>(this->next); }
PacketBuffer * Consume(uint16_t aConsumeLength);
void Clear();
void AddToEnd_ForNow(PacketBuffer * aPacket);

friend class PacketBufferHandle;
friend class ::PacketBufferTest;
Expand Down Expand Up @@ -623,6 +622,8 @@ class DLL_EXPORT PacketBufferHandle

PacketBuffer * Get() const { return mBuffer; }

bool operator==(const PacketBufferHandle & aOther) { return mBuffer == aOther.mBuffer; }

PacketBuffer * mBuffer;

friend class PacketBuffer;
Expand Down
Loading

0 comments on commit 530f741

Please sign in to comment.