/*!
 * Return the \c chunk at the beginning of the \c linked_list_chunk.
 * The \c chunk is removed from the \c linked_list_chunk.
 *
 * \param llc \c linked_list_chunk to pop from
 * \return The removed \c chunk at the beginning or \c NULL if linked_list_chunk empty
 */
chunk linked_list_chunk_pop_front ( linked_list_chunk llc )  {
    assert(llc != NULL);
    if(linked_list_chunk_is_empty(llc)) {
        return NULL;
    }
    chunk ch = chunk_copy(llc->first->value);
    inner_linked_chunk* buffILC;
    buffILC = llc->first;
    if(llc->first == llc->last) {
        llc->last = NULL;
        llc->first = NULL;

        buffILC->precedent = NULL;
        chunk_destroy(buffILC->value);
        buffILC->value = NULL;
        buffILC->suivant = NULL;
        free(buffILC);
        buffILC = NULL;
    } else {
        llc->first = llc->first->suivant;
        llc->first->precedent =  NULL;

        chunk_destroy(buffILC->value);
        buffILC->value = NULL;
        buffILC->suivant = NULL;
        free(buffILC);
        buffILC = NULL;

    }
    return ch;
}
 */linked_list_chunk linked_list_chunk_copy ( linked_list_chunk llc )  {
    linked_list_chunk llc2 = linked_list_chunk_create();
    inner_linked_chunk* buff = llc->first;
    while(buff != NULL) {
        linked_list_chunk_add_back(llc2 , chunk_copy(buff->value));
        buff = buff->suivant;
    }
    return llc2;
}
Пример #3
0
/** audio tx put
 *   copies filled pChunk into the TX queue for transmission
 *    if queue is full, then chunk is dropped 
 * Parameters:
 * @param pThis  pointer to own object
 *
 * @return Zero on success.
 * Negative value on failure.
 */
int audioTx_put(audioTx_t *pThis, chunk_t *pChunk)
{
	chunk_t *pchunk_temp = NULL;

    if ( NULL == pThis || NULL == pChunk ) {
        //printf("[Audio TX]: Failed to put \r\n");
        return FAIL;
    }
    
    // block if queue is full
    //while(queue_is_full(&pThis->queue)) {
    if(queue_is_full(&pThis->queue)) {
        //printf("[Audio TX]: Queue Full \r\n");
        return FAIL;
        //powerMode_change(PWR_ACTIVE);
        //asm("idle;");
    }
    //powerMode_change(PWR_FULL_ON);

    // get free chunk from pool
    if ( PASS == bufferPool_acquire(pThis->pBuffP, &pchunk_temp) ) {
    	// copy chunk into free buffer for queue
    	chunk_copy(pChunk, pchunk_temp);

    	/* If DMA not running ? */
        if ( 0 == pThis->running ) {
        	/* directly put chunk to DMA transfer & enable */
        	pThis->running  = 1;
            pThis->pPending = pchunk_temp;
            audioTx_dmaConfig(pThis->pPending);
            ENABLE_SPORT0_TX();

        } else {
        	/* DMA already running add chunk to queue */
            if ( PASS != queue_put(&pThis->queue, pchunk_temp) ) {
            	// return chunk to pool if queue is full, effectively dropping the chunk
                bufferPool_release(pThis->pBuffP, pchunk_temp);
                return FAIL;
            }
            else
            {
            	return PASS;
            }
        }

    } else {
    	// drop if we don't get free space
    	//printf("[Audio TX]: failed to get buffer \r\n");
    	return FAIL;
    }
    
    return FAIL;
}
/*!
 * Add a \b copy of the \c k first \c chunk at the beginning of the \c linked_list_chunk to it-self.
 * If there is less than \c k \c chunk then no copy is made.
 *
 * For \c k, the following \c linked_list_chunk
 * \verbatim [front]                                     ch0 ch_1 ch_2 ...  ch_k-2 ch_k-1   ch_k ch_k+1 ch_k+2 \endverbatim
 * is transformed into
 * \verbatim [front]  ch0 ch_1 ch_2 ...  ch_k-2 ch_k-1   ch0 ch_1 ch_2 ...  ch_k-2 ch_k-1   ch_k ch_k+1 ch_k+2 \endverbatim
 *
 * \param llc \c linked_list_chunk to add to
 * \param k size of the prefix to copy in front
 * \pre \c llc is valid (assert-ed)
 * \return false if there where less than k element. In such a case, no copy is made.
 */
bool linked_list_chunk_add_self_copy_front ( linked_list_chunk llc ,
        unsigned int k )  {
    inner_linked_chunk* buff = llc->first;
    for(unsigned int i = 0; i<k-1 ; i++) {
        if(buff == NULL) {
            return false;
        }
        buff = buff->suivant;
    }
    for(unsigned int i = 0; i<k; i++) {
        linked_list_chunk_add_front(llc , chunk_copy(buff->value));
        buff = buff->precedent;
    }
    return true;
}
/*!
 * Do various modifications of the  \c linked_list_chunk with the provided \c chunk.
 * On return, the \c chunk is added on both extremities.
 */
static void test_llc_ch ( linked_list_chunk const llc ,
                          chunk const ch ) {

    fprintf ( stdout , "*** adding to back : \"" ) ;
    chunk_print ( ch , stdout ) ;
    fprintf ( stdout , "\" :\n" ) ;
    linked_list_chunk_add_back ( llc , ch ) ;
    linked_list_chunk_print ( llc , stdout ) ;

    fprintf ( stdout , "*** adding to front : \"" ) ;
    chunk_print (  ch , stdout ) ;
    fprintf ( stdout , "\" :\n" ) ;
    linked_list_chunk_add_front ( llc , chunk_copy ( ch ) ) ;
    linked_list_chunk_print ( llc , stdout ) ;

    assert ( ! linked_list_chunk_is_empty ( llc ) ) ;
}