Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MQTT::onReceive to reduce if/else nesting #5592

Merged
merged 16 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/mesh/MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

#include <Arduino.h>
#include <assert.h>
#include <functional>
#include <memory>

#include "PointerQueue.h"

template <class T> class Allocator
{

public:
Allocator() : deleter([this](T *p) { this->release(p); }) {}
virtual ~Allocator() {}

/// Return a queable object which has been prefilled with zeros. Panic if no buffer is available
Expand Down Expand Up @@ -43,12 +46,32 @@ template <class T> class Allocator
return p;
}

/// Variations of the above methods that return std::unique_ptr instead of raw pointers.
using UniqueAllocation = std::unique_ptr<T, const std::function<void(T *)> &>;
/// Return a queable object which has been prefilled with zeros.
/// std::unique_ptr wrapped variant of allocZeroed().
UniqueAllocation allocUniqueZeroed() { return UniqueAllocation(allocZeroed(), deleter); }
/// Return a queable object which has been prefilled with zeros - allow timeout to wait for available buffers (you probably
/// don't want this version).
/// std::unique_ptr wrapped variant of allocZeroed(TickType_t maxWait).
UniqueAllocation allocUniqueZeroed(TickType_t maxWait) { return UniqueAllocation(allocZeroed(maxWait), deleter); }
/// Return a queable object which is a copy of some other object
/// std::unique_ptr wrapped variant of allocCopy(const T &src, TickType_t maxWait).
UniqueAllocation allocUniqueCopy(const T &src, TickType_t maxWait = portMAX_DELAY)
{
return UniqueAllocation(allocCopy(src, maxWait), deleter);
}

/// Return a buffer for use by others
virtual void release(T *p) = 0;

protected:
// Alloc some storage
virtual T *alloc(TickType_t maxWait) = 0;

private:
// std::unique_ptr Deleter function; calls release().
const std::function<void(T *)> deleter;
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/mesh/MeshTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef int ErrorCode;

/// Alloc and free packets to our global, ISR safe pool
extern Allocator<meshtastic_MeshPacket> &packetPool;
using UniquePacketPoolPacket = Allocator<meshtastic_MeshPacket>::UniqueAllocation;

/**
* Most (but not always) of the time we want to treat packets 'from' the local phone (where from == 0), as if they originated on
Expand Down
Loading
Loading