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 10, 2021
1 parent 44f8ea7 commit a3500b7
Show file tree
Hide file tree
Showing 32 changed files with 707 additions and 632 deletions.
3 changes: 1 addition & 2 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ typedef void (*AsyncWorkFunct)(intptr_t arg);
#include <ble/BleConfig.h>
#include <inet/InetLayer.h>
#include <system/SystemEvent.h>
#include <system/SystemObject.h>
#include <system/SystemPacketBuffer.h>

namespace chip {
Expand All @@ -321,7 +320,7 @@ struct ChipDeviceEvent final
struct
{
::chip::System::EventType Type;
::chip::System::Object * Target;
void * Target;
uintptr_t Argument;
} ChipSystemLayerEvent;
struct
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
2 changes: 1 addition & 1 deletion 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
10 changes: 5 additions & 5 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ CHIP_ERROR InetLayer::NewRawEndPoint(IPVersion ipVer, IPProtocol ipProto, RawEnd

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = RawEndPoint::sPool.TryCreate();
*retEndPoint = RawEndPoint::sPool.CreateObject();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "Raw");
Expand Down Expand Up @@ -572,7 +572,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 @@ -612,7 +612,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 @@ -787,7 +787,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 @@ -819,7 +819,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
5 changes: 3 additions & 2 deletions src/inet/InetLayerBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include <lib/support/BitFlags.h>
#include <lib/support/DLLUtil.h>
#include <system/SystemObject.h>

#include <stdint.h>
#include <type_traits>
Expand All @@ -51,12 +50,14 @@ class InetLayer;
* InetLayer object.
*
*/
class InetLayerBasis : public chip::System::Object
class InetLayerBasis
{
public:
InetLayer & Layer() const;
bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const;

void * AppState;

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

Expand Down
17 changes: 15 additions & 2 deletions src/inet/RawEndPoint.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 RawEndPoint;
class RawEndPointDeletor
{
public:
static void Release(RawEndPoint* obj);
};

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

Expand Down Expand Up @@ -91,6 +98,7 @@ class DLL_EXPORT RawEndPoint : public IPEndPointBasis
private:
RawEndPoint(const RawEndPoint &) = delete;

friend class RawEndPointDeletor;
static chip::System::ObjectPool<RawEndPoint, INET_CONFIG_NUM_RAW_ENDPOINTS> sPool;

void Init(InetLayer * inetLayer, IPVersion ipVer, IPProtocol ipProto);
Expand Down Expand Up @@ -120,5 +128,10 @@ class DLL_EXPORT RawEndPoint : public IPEndPointBasis
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
};

inline void RawEndPointDeletor::Release(RawEndPoint* obj)
{
RawEndPoint::sPool.ReleaseObject(obj);
}

} // namespace Inet
} // namespace chip
19 changes: 17 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,14 @@ 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 +65,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 @@ -585,6 +594,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 +711,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
17 changes: 15 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 @@ -73,6 +80,7 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
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 +108,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 @@ -35,7 +35,7 @@
#include <inet/InetInterface.h>
#include <inet/RawEndPoint.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
45 changes: 44 additions & 1 deletion src/lib/core/ReferenceCounted.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#pragma once

#include <atomic>
#include <limits>
#include <stdlib.h>

Expand All @@ -45,7 +46,7 @@ template <class Subclass, class Deletor = DeleteDeletor<Subclass>, int kInitRefC
class ReferenceCounted
{
public:
typedef uint32_t count_type;
using count_type = uint32_t;

/** Adds one to the usage count of this class */
Subclass * Retain()
Expand Down Expand Up @@ -80,4 +81,46 @@ class ReferenceCounted
count_type mRefCount = kInitRefCount;
};

/// An variant of ReferenceCounted, with atomic counter.
template <class Subclass, class Deletor = DeleteDeletor<Subclass>, int kInitRefCount = 1>
class AtomicReferenceCounted
{
public:
using count_type = uint32_t;

AtomicReferenceCounted() : mRefCount(kInitRefCount) {}

/** Adds one to the usage count of this class */
Subclass * Retain()
{
if (mRefCount == std::numeric_limits<count_type>::max())
{
abort();
}
++mRefCount;

return static_cast<Subclass *>(this);
}

/** Release usage of this class */
void Release()
{
if (mRefCount == 0)
{
abort();
}

if (--mRefCount == 0)
{
Deletor::Release(static_cast<Subclass *>(this));
}
}

/** Get the current reference counter value */
count_type GetReferenceCount() const { return mRefCount.load(); }

private:
std::atomic<count_type> mRefCount;
};

} // namespace chip
Loading

0 comments on commit a3500b7

Please sign in to comment.