File tree 1 file changed +19
-6
lines changed
1 file changed +19
-6
lines changed Original file line number Diff line number Diff line change
1
+ #include < MemoryPool.h>
2
+
3
+
1
4
template <class T >
2
5
class Shared // template definition
3
6
{
@@ -7,9 +10,14 @@ class Shared // template definition
7
10
operator T () {
8
11
osEvent evt = queue.get ();
9
12
if (evt.status == osEventMessage) {
10
- T x = *((T*)evt.value .p );
11
- delete (T*)evt.value .p ;
12
- return x;
13
+ /* Obtain the oldest inserted element from the queue. */
14
+ T * val_ptr = reinterpret_cast <T *>(evt.value .p );
15
+ /* Copy the content of T stored in the memory pool since we'll have to free the memory pool afterwards. */
16
+ T const tmp_val = *val_ptr;
17
+ /* Free the allocated memory in the memory pool. */
18
+ memory_pool.free (val_ptr);
19
+ /* Return obtained value from queue. */
20
+ return tmp_val;
13
21
}
14
22
return val;
15
23
}
@@ -19,9 +27,13 @@ class Shared // template definition
19
27
T discard = *this ;
20
28
}
21
29
val = other;
22
- T* obj = new T (val);
23
- queue.put (obj);
24
- return (*obj);
30
+ /* Allocate memory in the memory pool. */
31
+ T * val_ptr = memory_pool.alloc ();
32
+ /* Copy the content of 'other' into the freshly allocated message. */
33
+ *val_ptr = other;
34
+ /* Insert into queue. */
35
+ queue.put (val_ptr);
36
+ return (*val_ptr);
25
37
}
26
38
T& peek () {
27
39
return val;
@@ -31,6 +43,7 @@ class Shared // template definition
31
43
}
32
44
private:
33
45
T val;
46
+ rtos::MemoryPool<T, 16 > memory_pool;
34
47
rtos::Queue<T, 16 > queue;
35
48
};
36
49
You can’t perform that action at this time.
0 commit comments