172a82af4SMatthias Ringwald /*
272a82af4SMatthias Ringwald * Copyright (C) 2019 BlueKitchen GmbH
372a82af4SMatthias Ringwald *
472a82af4SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
572a82af4SMatthias Ringwald * modification, are permitted provided that the following conditions
672a82af4SMatthias Ringwald * are met:
772a82af4SMatthias Ringwald *
872a82af4SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
972a82af4SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
1072a82af4SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
1172a82af4SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
1272a82af4SMatthias Ringwald * documentation and/or other materials provided with the distribution.
1372a82af4SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
1472a82af4SMatthias Ringwald * contributors may be used to endorse or promote products derived
1572a82af4SMatthias Ringwald * from this software without specific prior written permission.
1672a82af4SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
1772a82af4SMatthias Ringwald * personal benefit and not for any commercial purpose or for
1872a82af4SMatthias Ringwald * monetary gain.
1972a82af4SMatthias Ringwald *
2072a82af4SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2172a82af4SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2272a82af4SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2572a82af4SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2672a82af4SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2772a82af4SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2872a82af4SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2972a82af4SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3072a82af4SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3172a82af4SMatthias Ringwald * SUCH DAMAGE.
3272a82af4SMatthias Ringwald *
3372a82af4SMatthias Ringwald * Please inquire about commercial licensing options at
3472a82af4SMatthias Ringwald * [email protected]
3572a82af4SMatthias Ringwald *
3672a82af4SMatthias Ringwald */
3772a82af4SMatthias Ringwald
3872a82af4SMatthias Ringwald #define BTSTACK_FILE__ "btstack_linked_queue.c"
3972a82af4SMatthias Ringwald
4072a82af4SMatthias Ringwald #include "btstack_linked_queue.h"
4172a82af4SMatthias Ringwald #include "btstack_debug.h"
4272a82af4SMatthias Ringwald #include <stdlib.h>
4372a82af4SMatthias Ringwald
4472a82af4SMatthias Ringwald /**
4572a82af4SMatthias Ringwald * @brief Tests if queue is empty
4672a82af4SMatthias Ringwald * @return true if emtpy
4772a82af4SMatthias Ringwald */
btstack_linked_queue_empty(btstack_linked_queue_t * queue)4872a82af4SMatthias Ringwald bool btstack_linked_queue_empty(btstack_linked_queue_t * queue){
4972a82af4SMatthias Ringwald return queue->head == NULL;
5072a82af4SMatthias Ringwald }
5172a82af4SMatthias Ringwald
5272a82af4SMatthias Ringwald /**
5372a82af4SMatthias Ringwald * @brief Append item to queue
5472a82af4SMatthias Ringwald * @param queue
5572a82af4SMatthias Ringwald * @param item
5672a82af4SMatthias Ringwald */
btstack_linked_queue_enqueue(btstack_linked_queue_t * queue,btstack_linked_item_t * item)5772a82af4SMatthias Ringwald void btstack_linked_queue_enqueue(btstack_linked_queue_t * queue, btstack_linked_item_t * item){
5872a82af4SMatthias Ringwald if (queue->head == NULL){
5972a82af4SMatthias Ringwald // empty queue
6072a82af4SMatthias Ringwald item->next = NULL;
6172a82af4SMatthias Ringwald queue->head = item;
6272a82af4SMatthias Ringwald queue->tail = item;
6372a82af4SMatthias Ringwald } else {
6472a82af4SMatthias Ringwald // non-empty
6572a82af4SMatthias Ringwald item->next = NULL;
6672a82af4SMatthias Ringwald queue->tail->next = item;
6772a82af4SMatthias Ringwald queue->tail = item;
6872a82af4SMatthias Ringwald }
6972a82af4SMatthias Ringwald }
7072a82af4SMatthias Ringwald
7172a82af4SMatthias Ringwald /**
7272a82af4SMatthias Ringwald * @brief Get first item from queue
7372a82af4SMatthias Ringwald * @param queue
7472a82af4SMatthias Ringwald * @return item or NULL if queue empty
7572a82af4SMatthias Ringwald */
btstack_linked_queue_first(btstack_linked_queue_t * queue)7672a82af4SMatthias Ringwald btstack_linked_item_t * btstack_linked_queue_first(btstack_linked_queue_t * queue){
7772a82af4SMatthias Ringwald return queue->head;
7872a82af4SMatthias Ringwald }
7972a82af4SMatthias Ringwald
8072a82af4SMatthias Ringwald /**
8172a82af4SMatthias Ringwald * @brief Pop next item from queue
8272a82af4SMatthias Ringwald * @param queue
8372a82af4SMatthias Ringwald * @return item or NULL if queue empty
8472a82af4SMatthias Ringwald */
btstack_linked_queue_dequeue(btstack_linked_queue_t * queue)8572a82af4SMatthias Ringwald btstack_linked_item_t * btstack_linked_queue_dequeue(btstack_linked_queue_t * queue){
8672a82af4SMatthias Ringwald if (queue->head == NULL){
8772a82af4SMatthias Ringwald return NULL;
8872a82af4SMatthias Ringwald }
8972a82af4SMatthias Ringwald btstack_linked_item_t * item = queue->head;
9072a82af4SMatthias Ringwald queue->head = item->next;
9172a82af4SMatthias Ringwald // reset tail if head becomes NULL
9272a82af4SMatthias Ringwald if (queue->head == NULL){
9372a82af4SMatthias Ringwald queue->tail = NULL;
9472a82af4SMatthias Ringwald }
9572a82af4SMatthias Ringwald return item;
9672a82af4SMatthias Ringwald }
97