static msg_t logServerThrd(void *arg) { int i; (void) arg; chRegSetThreadName("log"); chprintf((BaseChannel *) &SD2, "Log server started\r\n"); for (i = 0; i < LOG_MSG_MP_SIZE; i++) { chPoolFree(&logMP, logMPbuffer[i]); } msg_t m, res; while (TRUE) { res = chMBFetch(&logMB, &m, TIME_INFINITE ); if (res == RDY_OK) { chprintf((BaseChannel *) &SD2, (char *) m); chPoolFree(&logMP, (void *) m); } } return -1; }
// must be called with the locked driver static void free_trajectory_buffer(motor_driver_t *d) { if (d->control_mode == MOTOR_CONTROL_MODE_TRAJECTORY && d->setpt.trajectory != NULL) { chPoolFree(d->traj_buffer_points_pool, trajectory_get_buffer_pointer(d->setpt.trajectory)); chPoolFree(d->traj_buffer_pool, d->setpt.trajectory); d->setpt.trajectory = NULL; } }
/** * @brief Releases a reference to a thread object. * @details If the references counter reaches zero <b>and</b> the thread * is in the @p THD_STATE_FINAL state then the thread's memory is * returned to the proper allocator. * @pre The configuration option @p CH_USE_DYNAMIC must be enabled in order * to use this function. * @note Static threads are not affected. * * @param[in] tp pointer to the thread * * @api */ void chThdRelease(Thread *tp) { trefs_t refs; chSysLock(); chDbgAssert(tp->p_refs > 0, "chThdRelease(), #1", "not referenced"); refs = --tp->p_refs; chSysUnlock(); /* If the references counter reaches zero and the thread is in its terminated state then the memory can be returned to the proper allocator. Of course static threads are not affected.*/ if ((refs == 0) && (tp->p_state == THD_STATE_FINAL)) { switch (tp->p_flags & THD_MEM_MODE_MASK) { #if CH_USE_HEAP case THD_MEM_MODE_HEAP: #if CH_USE_REGISTRY REG_REMOVE(tp); #endif chHeapFree(tp); break; #endif #if CH_USE_MEMPOOLS case THD_MEM_MODE_MEMPOOL: #if CH_USE_REGISTRY REG_REMOVE(tp); #endif chPoolFree(tp->p_mpool, tp); break; #endif } } }
/** * @brief Deletes a mutex. * @note After deletion there could be references in the system to a * non-existent semaphore. */ osStatus osMutexDelete(osMutexId mutex_id) { chSemReset((semaphore_t *)mutex_id, 0); chPoolFree(&sempool, (void *)mutex_id); return osOK; }
/** * @brief Deletes a semaphore. * @note After deletion there could be references in the system to a * non-existent semaphore. */ osStatus osSemaphoreDelete(osSemaphoreId semaphore_id) { chSemReset((semaphore_t *)semaphore_id, 0); chPoolFree(&sempool, (void *)semaphore_id); return osOK; }
/** * @brief Delete a timer. */ osStatus osTimerDelete(osTimerId timer_id) { chVTReset(&timer_id->vt); chPoolFree(&timpool, (void *)timer_id); return osOK; }
static void pools1_execute(void) { int i; /* Adding the WAs to the pool.*/ chPoolLoadArray(&mp1, wa[0], MAX_THREADS); /* Emptying the pool.*/ for (i = 0; i < MAX_THREADS; i++) test_assert(1, chPoolAlloc(&mp1) != NULL, "list empty"); /* Now must be empty.*/ test_assert(2, chPoolAlloc(&mp1) == NULL, "list not empty"); /* Adding the WAs to the pool, one by one this time.*/ for (i = 0; i < MAX_THREADS; i++) chPoolFree(&mp1, wa[i]); /* Emptying the pool again.*/ for (i = 0; i < MAX_THREADS; i++) test_assert(3, chPoolAlloc(&mp1) != NULL, "list empty"); /* Now must be empty again.*/ test_assert(4, chPoolAlloc(&mp1) == NULL, "list not empty"); /* Covering the case where a provider is unable to return more memory.*/ chPoolInit(&mp1, 16, null_provider); test_assert(5, chPoolAlloc(&mp1) == NULL, "provider returned memory"); }
static void dyn2_execute(void) { int i; tprio_t prio = chThdGetPriority(); /* Adding the WAs to the pool. */ for (i = 0; i < 4; i++) chPoolFree(&mp1, wa[i]); /* Starting threads from the memory pool. */ threads[0] = chThdCreateFromMemoryPool(&mp1, prio-1, thread, "A"); threads[1] = chThdCreateFromMemoryPool(&mp1, prio-2, thread, "B"); threads[2] = chThdCreateFromMemoryPool(&mp1, prio-3, thread, "C"); threads[3] = chThdCreateFromMemoryPool(&mp1, prio-4, thread, "D"); threads[4] = chThdCreateFromMemoryPool(&mp1, prio-5, thread, "E"); test_assert(1, (threads[0] != NULL) && (threads[1] != NULL) && (threads[2] != NULL) && (threads[3] != NULL) && (threads[4] == NULL), "thread creation failed"); /* Claiming the memory from terminated threads. */ test_wait_threads(); test_assert_sequence(2, "ABCD"); /* Now the pool must be full again. */ for (i = 0; i < 4; i++) test_assert(3, chPoolAlloc(&mp1) != NULL, "pool list empty"); test_assert(4, chPoolAlloc(&mp1) == NULL, "pool list not empty"); }
/** * * @brief Appends an item to a queue. * * @param[in] queuep pointer to instance of @p struct pios_queue * @param[in] itemp pointer to item which will be appended to the queue * @param[in] timeout_ms timeout for appending item to queue in milliseconds * * @returns true on success or false on timeout or failure * */ bool PIOS_Queue_Send(struct pios_queue *queuep, const void *itemp, uint32_t timeout_ms) { void *buf = chPoolAlloc(&queuep->mp); if (buf == NULL) return false; memcpy(buf, itemp, queuep->mp.mp_object_size); systime_t timeout; if (timeout_ms == PIOS_QUEUE_TIMEOUT_MAX) timeout = TIME_INFINITE; else if (timeout_ms == 0) timeout = TIME_IMMEDIATE; else timeout = MS2ST(timeout_ms); msg_t result = chMBPost(&queuep->mb, (msg_t)buf, timeout); if (result != RDY_OK) { chPoolFree(&queuep->mp, buf); return false; } return true; }
static void dyn_release_object_pool(dyn_element_t *dep, dyn_list_t *dlp, memory_pool_t *mp) { chDbgCheck(dep != NULL); chDbgAssert(dep->refs > (ucnt_t)0, "invalid references number"); dep->refs--; if (dep->refs == (ucnt_t)0) { dep = dyn_list_unlink(dep, dlp); chPoolFree(mp, (void *)dep); } }
static void test_005_001_execute(void) { unsigned i; /* [5.1.1] Adding the objects to the pool using chPoolLoadArray().*/ test_set_step(1); { chPoolLoadArray(&mp1, objects, MEMORY_POOL_SIZE); } /* [5.1.2] Emptying the pool using chPoolAlloc().*/ test_set_step(2); { for (i = 0; i < MEMORY_POOL_SIZE; i++) test_assert(chPoolAlloc(&mp1) != NULL, "list empty"); } /* [5.1.3] Now must be empty.*/ test_set_step(3); { test_assert(chPoolAlloc(&mp1) == NULL, "list not empty"); } /* [5.1.4] Adding the objects to the pool using chPoolFree().*/ test_set_step(4); { for (i = 0; i < MEMORY_POOL_SIZE; i++) chPoolFree(&mp1, &objects[i]); } /* [5.1.5] Emptying the pool using chPoolAlloc() again.*/ test_set_step(5); { for (i = 0; i < MEMORY_POOL_SIZE; i++) test_assert(chPoolAlloc(&mp1) != NULL, "list empty"); } /* [5.1.6] Now must be empty again.*/ test_set_step(6); { test_assert(chPoolAlloc(&mp1) == NULL, "list not empty"); } /* [5.1.7] Covering the case where a provider is unable to return more memory.*/ test_set_step(7); { chPoolObjectInit(&mp1, sizeof (uint32_t), null_provider); test_assert(chPoolAlloc(&mp1) == NULL, "provider returned memory"); } }
/** * @brief Releases a reference to a thread object. * @details If the references counter reaches zero <b>and</b> the thread * is in the @p CH_STATE_FINAL state then the thread's memory is * returned to the proper allocator. * @pre The configuration option @p CH_CFG_USE_DYNAMIC must be enabled in * order to use this function. * @note Static threads are not affected. * * @param[in] tp pointer to the thread * * @api */ void chThdRelease(thread_t *tp) { trefs_t refs; chSysLock(); chDbgAssert(tp->p_refs > (trefs_t)0, "not referenced"); tp->p_refs--; refs = tp->p_refs; /* If the references counter reaches zero and the thread is in its terminated state then the memory can be returned to the proper allocator. Of course static threads are not affected.*/ if ((refs == (trefs_t)0) && (tp->p_state == CH_STATE_FINAL)) { switch (tp->p_flags & CH_FLAG_MODE_MASK) { #if CH_CFG_USE_HEAP == TRUE case CH_FLAG_MODE_HEAP: #if CH_CFG_USE_REGISTRY == TRUE REG_REMOVE(tp); #endif chSysUnlock(); chHeapFree(tp); return; #endif #if CH_CFG_USE_MEMPOOLS == TRUE case CH_FLAG_MODE_MPOOL: #if CH_CFG_USE_REGISTRY == TRUE REG_REMOVE(tp); #endif chSysUnlock(); chPoolFree(tp->p_mpool, tp); return; #endif default: /* Nothing to do for static threads, those are removed from the registry on exit.*/ break; } } chSysUnlock(); }
/** * * @brief Retrieves an item from the front of a queue. * * @param[in] queuep pointer to instance of @p struct pios_queue * @param[in] itemp pointer to item which will be retrieved * @param[in] timeout_ms timeout for retrieving item from queue in milliseconds * * @returns true on success or false on timeout or failure * */ bool PIOS_Queue_Receive(struct pios_queue *queuep, void *itemp, uint32_t timeout_ms) { msg_t buf; systime_t timeout; if (timeout_ms == PIOS_QUEUE_TIMEOUT_MAX) timeout = TIME_INFINITE; else if (timeout_ms == 0) timeout = TIME_IMMEDIATE; else timeout = MS2ST(timeout_ms); msg_t result = chMBFetch(&queuep->mb, &buf, timeout); if (result != RDY_OK) return false; memcpy(itemp, (void*)buf, queuep->mp.mp_object_size); chPoolFree(&queuep->mp, (void*)buf); return true; }
void MemoryPool::free(void *objp) { chPoolFree(&pool, objp); }
/** * @brief Releases a mail object into a mail pool. * @pre The mail pool must be already been initialized. * * @param[in] mlp pointer to a @p MailPool structure * @param[in] mailp the pointer to the mail object to be released * * @api */ void mailDelete(MailPool *mlp, void *mailp) { chPoolFree(&mlp->pool, mailp); chSemSignal(&mlp->sem); }
void usbFreeMailboxBuffer (void* buffer) { chPoolFree (&usbMemPool, buffer); }