MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / rt_mp_alloc

Function rt_mp_alloc

src/mempool.c:281–365  ·  view source on GitHub ↗

* @brief This function will allocate a block from memory pool. * * @param mp is the memory pool object. * * @param time is the maximum waiting time for allocating memory. * - 0 for not waiting, allocating memory immediately. * * @return the allocated memory block or RT_NULL on allocated failed. */

Source from the content-addressed store, hash-verified

279 * @return the allocated memory block or RT_NULL on allocated failed.
280 */
281void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
282{
283 rt_uint8_t *block_ptr;
284 rt_base_t level;
285 struct rt_thread *thread;
286 rt_uint32_t before_sleep = 0;
287
288 /* parameter check */
289 RT_ASSERT(mp != RT_NULL);
290
291 /* get current thread */
292 thread = rt_thread_self();
293
294 level = rt_spin_lock_irqsave(&(mp->spinlock));
295
296 while (mp->block_free_count == 0)
297 {
298 /* memory block is unavailable. */
299 if (time == 0)
300 {
301 rt_spin_unlock_irqrestore(&(mp->spinlock), level);
302
303 rt_set_errno(-RT_ETIMEOUT);
304
305 return RT_NULL;
306 }
307
308 RT_DEBUG_NOT_IN_INTERRUPT;
309
310 thread->error = RT_EOK;
311
312 /* need suspend thread */
313 rt_thread_suspend_to_list(thread, &mp->suspend_thread, RT_IPC_FLAG_FIFO, RT_UNINTERRUPTIBLE);
314
315 if (time > 0)
316 {
317 rt_tick_t time_tick = time;
318 /* get the start tick of timer */
319 before_sleep = rt_tick_get();
320
321 /* init thread timer and start it */
322 rt_timer_control(&(thread->thread_timer),
323 RT_TIMER_CTRL_SET_TIME,
324 &time_tick);
325 rt_timer_start(&(thread->thread_timer));
326 }
327
328 /* enable interrupt */
329 rt_spin_unlock_irqrestore(&(mp->spinlock), level);
330
331 /* do a schedule */
332 rt_schedule();
333
334 if (thread->error != RT_EOK)
335 return RT_NULL;
336
337 if (time > 0)
338 {

Callers 9

rw009_wifi_txFunction · 0.85
_audio_dev_writeFunction · 0.85
rt_prio_queue_pushFunction · 0.85
rt_thread_killFunction · 0.85

Calls 9

rt_thread_selfFunction · 0.85
rt_set_errnoFunction · 0.85
rt_tick_getFunction · 0.85
rt_timer_controlFunction · 0.85
rt_timer_startFunction · 0.85
rt_spin_lock_irqsaveFunction · 0.70
rt_scheduleFunction · 0.70

Tested by 4