@@ -76,7 +76,7 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
76
76
osMemoryPoolDelete (_id);
77
77
}
78
78
79
- /* * Allocate a memory block of type T from a memory pool.
79
+ /* * Allocate a memory block from a memory pool, without blocking .
80
80
@return address of the allocated memory block or NULL in case of no memory available.
81
81
82
82
@note You may call this function from ISR context.
@@ -86,14 +86,83 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
86
86
return (T *)osMemoryPoolAlloc (_id, 0 );
87
87
}
88
88
89
- /* * Allocate a memory block of type T from a memory pool and set memory block to zero.
89
+ /* * Allocate a memory block from a memory pool, optionally blocking.
90
+ @param millisec timeout value (osWaitForever to wait forever)
91
+ @return address of the allocated memory block or NULL in case of no memory available.
92
+
93
+ @note You may call this function from ISR context if the millisec parameter is set to 0.
94
+ */
95
+ T *alloc_for (uint32_t millisec)
96
+ {
97
+ return (T *)osMemoryPoolAlloc (_id, millisec);
98
+ }
99
+
100
+ /* * Allocate a memory block from a memory pool, blocking.
101
+ @param millisec absolute timeout time, referenced to Kernel::get_ms_count().
102
+ @return address of the allocated memory block or NULL in case of no memory available.
103
+
104
+ @note You cannot call this function from ISR context.
105
+ @note the underlying RTOS may have a limit to the maximum wait time
106
+ due to internal 32-bit computations, but this is guaranteed to work if the
107
+ wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
108
+ the wait will time out earlier than specified.
109
+ */
110
+ T *alloc_until (uint64_t millisec)
111
+ {
112
+ uint64_t now = Kernel::get_ms_count ();
113
+ uint32_t delay;
114
+ if (now >= millisec) {
115
+ delay = 0 ;
116
+ } else if (millisec - now >= osWaitForever) {
117
+ delay = osWaitForever - 1 ;
118
+ } else {
119
+ delay = millisec - now;
120
+ }
121
+ return alloc_for (delay);
122
+ }
123
+
124
+ /* * Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
90
125
@return address of the allocated memory block or NULL in case of no memory available.
91
126
92
127
@note You may call this function from ISR context.
93
128
*/
94
129
T *calloc (void )
95
130
{
96
- T *item = (T *)osMemoryPoolAlloc (_id, 0 );
131
+ T *item = alloc ();
132
+ if (item != NULL ) {
133
+ memset (item, 0 , sizeof (T));
134
+ }
135
+ return item;
136
+ }
137
+
138
+ /* * Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.
139
+ @param millisec timeout value (osWaitForever to wait forever)
140
+ @return address of the allocated memory block or NULL in case of no memory available.
141
+
142
+ @note You may call this function from ISR context if the millisec parameter is set to 0.
143
+ */
144
+ T *calloc_for (uint32_t millisec)
145
+ {
146
+ T *item = alloc_for (millisec);
147
+ if (item != NULL ) {
148
+ memset (item, 0 , sizeof (T));
149
+ }
150
+ return item;
151
+ }
152
+
153
+ /* * Allocate a memory block from a memory pool, blocking, and set memory block to zero.
154
+ @param millisec absolute timeout time, referenced to Kernel::get_ms_count().
155
+ @return address of the allocated memory block or NULL in case of no memory available.
156
+
157
+ @note You cannot call this function from ISR context.
158
+ @note the underlying RTOS may have a limit to the maximum wait time
159
+ due to internal 32-bit computations, but this is guaranteed to work if the
160
+ wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
161
+ the wait will time out earlier than specified.
162
+ */
163
+ T *calloc_until (uint64_t millisec)
164
+ {
165
+ T *item = alloc_until (millisec);
97
166
if (item != NULL ) {
98
167
memset (item, 0 , sizeof (T));
99
168
}
@@ -110,7 +179,7 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
110
179
*/
111
180
osStatus free (T *block)
112
181
{
113
- return osMemoryPoolFree (_id, ( void *) block);
182
+ return osMemoryPoolFree (_id, block);
114
183
}
115
184
116
185
private:
0 commit comments