Skip to content

Commit

Permalink
Unify SystemPool and lib/support/Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Sep 14, 2021
1 parent 8b01cd7 commit 0ffbb6d
Show file tree
Hide file tree
Showing 37 changed files with 792 additions and 626 deletions.
1 change: 0 additions & 1 deletion src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ typedef void (*AsyncWorkFunct)(intptr_t arg);
#include <ble/BleConfig.h>
#include <inet/InetLayer.h>
#include <system/SystemEvent.h>
#include <system/SystemLayer.h>
#include <system/SystemObject.h>
#include <system/SystemPacketBuffer.h>

Expand Down
17 changes: 16 additions & 1 deletion src/inet/DNSResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <inet/IPAddress.h>
#include <inet/InetError.h>
#include <inet/InetLayerBasis.h>
#include <lib/core/ReferenceCounted.h>
#include <system/SystemPool.h>

#define NL_DNS_HOSTNAME_MAX_LEN (253)

Expand Down Expand Up @@ -61,6 +63,13 @@ enum DNSOptions
kDNSOption_Default = kDNSOption_AddrFamily_Any
};

class DNSResolver;
class DNSResolverDeletor
{
public:
static void Release(DNSResolver * obj);
};

/**
* @class DNSResolver
*
Expand All @@ -70,7 +79,7 @@ enum DNSOptions
* interface available for the application layer.
*
*/
class DNSResolver : public InetLayerBasis
class DNSResolver : public InetLayerBasis, public AtomicReferenceCounted<DNSResolver, DNSResolverDeletor>
{
private:
friend class InetLayer;
Expand Down Expand Up @@ -103,6 +112,7 @@ class DNSResolver : public InetLayerBasis
*/
typedef void (*OnResolveCompleteFunct)(void * appState, CHIP_ERROR err, uint8_t addrCount, IPAddress * addrArray);

friend class DNSResolverDeletor;
static chip::System::ObjectPool<DNSResolver, INET_CONFIG_NUM_DNS_RESOLVERS> sPool;

/**
Expand Down Expand Up @@ -168,5 +178,10 @@ class DNSResolver : public InetLayerBasis
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
};

inline void DNSResolverDeletor::Release(DNSResolver * obj)
{
DNSResolver::sPool.ReleaseObject(obj);
}

} // namespace Inet
} // namespace chip
14 changes: 0 additions & 14 deletions src/inet/EndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,5 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
void EndPointBasis::DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
DeferredRelease(static_cast<System::LayerLwIP *>(Layer().SystemLayer()), aTactic);
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

} // namespace Inet
} // namespace chip
3 changes: 1 addition & 2 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <inet/InetError.h>
#include <inet/InetInterface.h>
#include <inet/InetLayerEvents.h>

#include <lib/core/ReferenceCounted.h>
#include <lib/support/DLLUtil.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down Expand Up @@ -124,7 +124,6 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis

uint8_t mLwIPEndPointType;

void DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic);
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
Expand Down
1 change: 1 addition & 0 deletions src/inet/IPEndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <inet/EndPointBasis.h>

#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>

#if CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down
8 changes: 4 additions & 4 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ CHIP_ERROR InetLayer::NewTCPEndPoint(TCPEndPoint ** retEndPoint)

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = TCPEndPoint::sPool.TryCreate();
*retEndPoint = TCPEndPoint::sPool.CreateObject();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "TCP");
Expand Down Expand Up @@ -553,7 +553,7 @@ CHIP_ERROR InetLayer::NewUDPEndPoint(UDPEndPoint ** retEndPoint)

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = UDPEndPoint::sPool.TryCreate();
*retEndPoint = UDPEndPoint::sPool.CreateObject();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "UDP");
Expand Down Expand Up @@ -728,7 +728,7 @@ CHIP_ERROR InetLayer::ResolveHostAddress(const char * hostName, uint16_t hostNam
VerifyOrExit(hostNameLen <= NL_DNS_HOSTNAME_MAX_LEN, err = INET_ERROR_HOST_NAME_TOO_LONG);
VerifyOrExit(maxAddrs > 0, err = CHIP_ERROR_NO_MEMORY);

resolver = DNSResolver::sPool.TryCreate();
resolver = DNSResolver::sPool.CreateObject();
if (resolver != nullptr)
{
resolver->InitInetLayerBasis(*this);
Expand Down Expand Up @@ -760,7 +760,7 @@ CHIP_ERROR InetLayer::ResolveHostAddress(const char * hostName, uint16_t hostNam
onComplete(appState, err, (err == CHIP_NO_ERROR) ? 1 : 0, addrArray);
}

resolver->Release();
DNSResolver::sPool.ReleaseObject(resolver);
resolver = nullptr;

ExitNow(err = CHIP_NO_ERROR);
Expand Down
4 changes: 3 additions & 1 deletion src/inet/InetLayerBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ class InetLayer;
* InetLayer object.
*
*/
class InetLayerBasis : public chip::System::Object
class InetLayerBasis : public System::Object
{
public:
InetLayer & Layer() const;
bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const;

void * AppState;

protected:
void InitInetLayerBasis(InetLayer & aInetLayer, void * aAppState = nullptr);

Expand Down
37 changes: 34 additions & 3 deletions src/inet/TCPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,12 +1197,43 @@ void TCPEndPoint::Free()
if (err != CHIP_NO_ERROR)
Abort();

// Release the Retain() that happened when the end point was allocated
// [on LwIP, the object may still be alive if DoClose() used the
// EndPointBasis::DeferredFree() method.]
// Release the Retain() that happened when the end point was allocated [on
// LwIP, the object may still be alive if DoClose() used the DeferredFree()
// method.]
Release();
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
void TCPEndPoint::DeferredFree(ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
System::LayerLwIP * lSystemLayer = static_cast<System::LayerLwIP *>(ep->Layer().SystemLayer());
CHIP_ERROR err = lSystemLayer->PostLambda([this] { this->Release(); });
if (err != CHIP_NO_ERROR)
{
switch (aTactic)
{
case kReleaseDeferralErrorTactic_Ignore:
break;

case kReleaseDeferralErrorTactic_Release:
this->Release();
break;

case kReleaseDeferralErrorTactic_Die:
VerifyOrDie(false);
break;
}
}
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if INET_TCP_IDLE_CHECK_INTERVAL > 0
void TCPEndPoint::SetIdleTimeout(uint32_t timeoutMS)
{
Expand Down
26 changes: 24 additions & 2 deletions src/inet/TCPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

#include <inet/EndPointBasis.h>
#include <inet/IPAddress.h>

#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemPool.h>

#include <utility>

Expand All @@ -48,6 +49,13 @@ namespace Inet {

class InetLayer;

class TCPEndPoint;
class TCPEndPointDeletor
{
public:
static void Release(TCPEndPoint * obj);
};

/**
* @brief Objects of this class represent TCP transport endpoints.
*
Expand All @@ -56,7 +64,7 @@ class InetLayer;
* endpoints (SOCK_STREAM sockets on Linux and BSD-derived systems) or LwIP
* TCP protocol control blocks, as the system is configured accordingly.
*/
class DLL_EXPORT TCPEndPoint : public EndPointBasis
class DLL_EXPORT TCPEndPoint : public EndPointBasis, public AtomicReferenceCounted<TCPEndPoint, TCPEndPointDeletor>
{
friend class InetLayer;
friend class ::chip::Transport::TCPTest;
Expand Down Expand Up @@ -391,6 +399,14 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
*/
void Free();

enum ReleaseDeferralErrorTactic
{
kReleaseDeferralErrorTactic_Ignore, /**< No action. */
kReleaseDeferralErrorTactic_Release, /**< Release immediately. */
kReleaseDeferralErrorTactic_Die, /**< Die with message. */
};
void DeferredFree(ReleaseDeferralErrorTactic aTactic);

/**
* @brief Extract whether TCP connection is established.
*/
Expand Down Expand Up @@ -585,6 +601,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
constexpr static size_t kMaxReceiveMessageSize = System::PacketBuffer::kMaxSizeWithoutReserve;

private:
friend class TCPEndPointDeletor;
static chip::System::ObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> sPool;

chip::System::PacketBufferHandle mRcvQueue;
Expand Down Expand Up @@ -701,6 +718,11 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
};

inline void TCPEndPointDeletor::Release(TCPEndPoint * obj)
{
TCPEndPoint::sPool.ReleaseObject(obj);
}

#if INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS && INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
inline uint16_t TCPEndPoint::MaxTCPSendQueuePolls(void)
{
Expand Down
31 changes: 31 additions & 0 deletions src/inet/UDPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,37 @@ void UDPEndPoint::Free()
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
void UDPEndPoint::DeferredFree(ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
System::LayerLwIP * lSystemLayer = static_cast<System::LayerLwIP *>(Layer().SystemLayer());
CHIP_ERROR err = lSystemLayer->PostLambda([this] { this->Release(); });
if (err != CHIP_NO_ERROR)
{
switch (aTactic)
{
case kReleaseDeferralErrorTactic_Ignore:
break;

case kReleaseDeferralErrorTactic_Release:
this->Release();
break;

case kReleaseDeferralErrorTactic_Die:
VerifyOrDie(false);
break;
}
}
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

/**
* A synonym for <tt>SendTo(addr, port, INET_NULL_INTERFACEID, msg, sendFlags)</tt>.
*/
Expand Down
25 changes: 23 additions & 2 deletions src/inet/UDPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

#include "inet/IPEndPointBasis.h"
#include <inet/IPAddress.h>

#include <system/SystemPacketBuffer.h>
#include <system/SystemPool.h>

#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
#include <dispatch/dispatch.h>
Expand All @@ -43,6 +43,13 @@ namespace Inet {
class InetLayer;
class IPPacketInfo;

class UDPEndPoint;
class UDPEndPointDeletor
{
public:
static void Release(UDPEndPoint * obj);
};

/**
* @brief Objects of this class represent UDP transport endpoints.
*
Expand All @@ -51,7 +58,7 @@ class IPPacketInfo;
* endpoints (SOCK_DGRAM sockets on Linux and BSD-derived systems) or LwIP
* UDP protocol control blocks, as the system is configured accordingly.
*/
class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
class DLL_EXPORT UDPEndPoint : public IPEndPointBasis, public AtomicReferenceCounted<UDPEndPoint, UDPEndPointDeletor>
{
friend class InetLayer;

Expand All @@ -70,9 +77,18 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
void Close();
void Free();

enum ReleaseDeferralErrorTactic
{
kReleaseDeferralErrorTactic_Ignore, /**< No action. */
kReleaseDeferralErrorTactic_Release, /**< Release immediately. */
kReleaseDeferralErrorTactic_Die, /**< Die with message. */
};
void DeferredFree(ReleaseDeferralErrorTactic aTactic);

private:
UDPEndPoint(const UDPEndPoint &) = delete;

friend class UDPEndPointDeletor;
static chip::System::ObjectPool<UDPEndPoint, INET_CONFIG_NUM_UDP_ENDPOINTS> sPool;

void Init(InetLayer * inetLayer);
Expand Down Expand Up @@ -100,5 +116,10 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
};

inline void UDPEndPointDeletor::Release(UDPEndPoint * obj)
{
UDPEndPoint::sPool.ReleaseObject(obj);
}

} // namespace Inet
} // namespace chip
2 changes: 1 addition & 1 deletion src/inet/tests/TestInetLayerCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <inet/InetError.h>
#include <inet/InetInterface.h>
#include <inet/UDPEndPoint.h>

#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>

// Preprocessor Macros
Expand Down
4 changes: 3 additions & 1 deletion src/inet/tests/TestSetupFaultInjectionPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
*
*/

#include <stdio.h>
#include <unistd.h>

#include "TestInetCommonOptions.h"
#include "TestSetupFaultInjection.h"
#include <inet/InetFaultInjection.h>
#include <lib/support/CHIPFaultInjection.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ScopedBuffer.h>
#include <stdio.h>
#include <system/SystemFaultInjection.h>

struct RestartCallbackContext
Expand Down
Loading

0 comments on commit 0ffbb6d

Please sign in to comment.