Skip to content

Commit 5bd66e7

Browse files
committed
Remove dynamic memory allocation
1 parent 8696075 commit 5bd66e7

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Arduino_Threads.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ class Shared // template definition
33
{
44
public:
55
Shared() {
6-
queue = new rtos::Queue<T, 16>;
76
}
8-
operator T() const {
9-
osEvent evt = queue->get();
7+
operator T() {
8+
osEvent evt = queue.get();
109
if (evt.status == osEventMessage) {
1110
T x = *((T*)evt.value.p);
1211
delete (T*)evt.value.p;
1312
return x;
1413
}
14+
return val;
1515
}
1616
T& operator= (const T& other) {
17-
if (queue->full()) {
18-
// invokes operator T()
17+
if (queue.full()) {
18+
// invokes operator T() to discard oldest element and free its memory
1919
T discard = *this;
2020
}
2121
val = other;
2222
T* obj = new T(val);
23-
queue->put(obj);
23+
queue.put(obj);
2424
return (*obj);
2525
}
2626
T& peek() {
@@ -31,7 +31,7 @@ class Shared // template definition
3131
}
3232
private:
3333
T val;
34-
rtos::Queue<T, 16>* queue;
34+
rtos::Queue<T, 16> queue;
3535
};
3636

3737
#define CONCAT2(x,y) x##y

0 commit comments

Comments
 (0)