Skip to content

[RFC] Memory management via memory pool #5

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

Merged
merged 2 commits into from
Sep 23, 2020
Merged
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
29 changes: 22 additions & 7 deletions Arduino_Threads.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <MemoryPool.h>


template<class T>
class Shared // template definition
{
Expand All @@ -7,9 +10,14 @@ class Shared // template definition
operator T() {
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
T x = *((T*)evt.value.p);
delete (T*)evt.value.p;
return x;
/* Obtain the oldest inserted element from the queue. */
T * val_ptr = reinterpret_cast<T *>(evt.value.p);
/* Copy the content of T stored in the memory pool since we'll have to free the memory pool afterwards. */
T const tmp_val = *val_ptr;
/* Free the allocated memory in the memory pool. */
memory_pool.free(val_ptr);
/* Return obtained value from queue. */
return tmp_val;
}
return val;
}
Expand All @@ -19,9 +27,13 @@ class Shared // template definition
T discard = *this;
}
val = other;
T* obj = new T(val);
queue.put(obj);
return (*obj);
/* Allocate memory in the memory pool. */
T * val_ptr = memory_pool.alloc();
/* Copy the content of 'other' into the freshly allocated message. */
*val_ptr = other;
/* Insert into queue. */
queue.put(val_ptr);
return (*val_ptr);
}
T& peek() {
return val;
Expand All @@ -30,8 +42,11 @@ class Shared // template definition
return peek();
}
private:
static size_t constexpr QUEUE_SIZE = 16;

T val;
rtos::Queue<T, 16> queue;
rtos::MemoryPool<T, QUEUE_SIZE> memory_pool;
rtos::Queue<T, QUEUE_SIZE> queue;
};

#define CONCAT2(x,y) x##y
Expand Down