root/src/libsir/src/sirqueue.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _sir_queue_node_create
  2. _sir_queue_node_destroy
  3. _sir_queue_create
  4. _sir_queue_destroy
  5. _sir_queue_size
  6. _sir_queue_isempty
  7. _sir_queue_push
  8. _sir_queue_pop

   1 /*
   2  * sirqueue.c
   3  *
   4  * Version: 2.2.5
   5  *
   6  * -----------------------------------------------------------------------------
   7  *
   8  * SPDX-License-Identifier: MIT
   9  *
  10  * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
  11  *
  12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  13  * this software and associated documentation files (the "Software"), to deal in
  14  * the Software without restriction, including without limitation the rights to
  15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  16  * the Software, and to permit persons to whom the Software is furnished to do so,
  17  * subject to the following conditions:
  18  *
  19  * The above copyright notice and this permission notice shall be included in all
  20  * copies or substantial portions of the Software.
  21  *
  22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  24  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  25  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28  *
  29  * -----------------------------------------------------------------------------
  30  */
  31 
  32 #include "sir/queue.h"
  33 #include "sir/helpers.h"
  34 #include "sir/errors.h"
  35 
  36 sir_queue_node* _sir_queue_node_create(void* data) {
     /* [previous][next][first][last][top][bottom][index][help] */
  37     sir_queue_node* retval = calloc(1, sizeof(sir_queue_node));
  38     if (!retval)
  39         (void)_sir_handleerr(errno);
  40     else
  41         retval->data = data;
  42 
  43     return retval;
  44 }
  45 
  46 bool _sir_queue_node_destroy(sir_queue_node** node, void** data) {
     /* [previous][next][first][last][top][bottom][index][help] */
  47     bool valid = _sir_validptrptr(node) && _sir_validptr(*node);
  48 
  49     if (valid) {
  50         if (data)
  51             *data = (*node)->data;
  52 
  53         _sir_safefree(node);
  54     }
  55 
  56     return valid;
  57 }
  58 
  59 bool _sir_queue_create(sir_queue** q) {
     /* [previous][next][first][last][top][bottom][index][help] */
  60     bool valid = _sir_validptrptr(q);
  61 
  62     if (valid) {
  63         *q = calloc(1, sizeof(sir_queue));
  64         if (!_sir_validptrnofail(*q))
  65             (void)_sir_handleerr(errno);
  66     }
  67 
  68     return valid && _sir_validptrnofail(*q);
  69 }
  70 
  71 bool _sir_queue_destroy(sir_queue** q) {
     /* [previous][next][first][last][top][bottom][index][help] */
  72     bool valid = _sir_validptrptr(q) && _sir_validptr(*q);
  73 
  74     if (valid) {
  75         sir_queue_node* next = (*q)->head;
  76         while (next) {
  77             sir_queue_node* this_node = next;
  78             void* data                = NULL;
  79             next                      = this_node->next;
  80 
  81             if (_sir_queue_node_destroy(&this_node, &data))
  82                 _sir_safefree(&data);
  83         }
  84 
  85         _sir_safefree(q);
  86     }
  87 
  88     return valid;
  89 }
  90 
  91 size_t _sir_queue_size(sir_queue* q) {
     /* [previous][next][first][last][top][bottom][index][help] */
  92     if (_sir_queue_isempty(q))
  93         return 0;
  94 
  95     sir_queue_node* next = q->head->next;
  96     size_t idx           = 1;
  97     while (next) {
  98         idx++;
  99         next = next->next;
 100     }
 101 
 102     return idx;
 103 }
 104 
 105 bool _sir_queue_isempty(const sir_queue* q) {
     /* [previous][next][first][last][top][bottom][index][help] */
 106     return !q || !q->head;
 107 }
 108 
 109 bool _sir_queue_push(sir_queue* q, void* data) {
     /* [previous][next][first][last][top][bottom][index][help] */
 110     bool retval = false;
 111 
 112     if (_sir_validptr(q)) {
 113         if (!q->head) {
 114             q->head = _sir_queue_node_create(data);
 115             retval = NULL != q->head;
 116         } else {
 117             sir_queue_node* next = q->head;
 118             while (next) {
 119                 if (!next->next) {
 120                     next->next = _sir_queue_node_create(data);
 121                     if (next->next) {
 122                         retval = true;
 123                         break;
 124                     }
 125                 }
 126                 next = next->next;
 127             }
 128         }
 129     }
 130 
 131     return retval;
 132 }
 133 
 134 bool _sir_queue_pop(sir_queue* q, void** data) {
     /* [previous][next][first][last][top][bottom][index][help] */
 135     bool retval = false;
 136 
 137     if (!_sir_queue_isempty(q) && _sir_validptrptr(data)) {
 138         sir_queue_node* old_head = q->head;
 139         q->head                  = old_head->next;
 140 
 141         retval = _sir_queue_node_destroy(&old_head, data);
 142     }
 143 
 144     return retval;
 145 }

/* [previous][next][first][last][top][bottom][index][help] */