Blame view

RIOT/sys/net/gnrc/priority_pktqueue/priority_pktqueue.c 2.8 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  /*
   * Copyright (C) 2015 Daniel Krebs
   *               2016 INRIA
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     net_gnrc_priority_pktqueue
   * @{
   *
   * @file
   * @brief       gnrc priority packet queue implementation
   *
   * @author      Daniel Krebs <github@daniel-krebs.net>
   * @author      Shuguo Zhuo <shuguo.zhuo@inria.fr>
   * @}
   */
  
  #include "net/gnrc/pktbuf.h"
  #include "net/gnrc/priority_pktqueue.h"
  
  /******************************************************************************/
  
  static inline void _free_node(gnrc_priority_pktqueue_node_t *node)
  {
      assert(node != NULL);
  
      priority_queue_node_init((priority_queue_node_t *)node);
  }
  
  /******************************************************************************/
  
  gnrc_pktsnip_t *gnrc_priority_pktqueue_pop(gnrc_priority_pktqueue_t *queue)
  {
      if (!queue || (gnrc_priority_pktqueue_length(queue) == 0)) {
          return NULL;
      }
      priority_queue_node_t *head = priority_queue_remove_head(queue);
      gnrc_pktsnip_t *pkt = (gnrc_pktsnip_t *) head->data;
      _free_node((gnrc_priority_pktqueue_node_t *)head);
      return pkt;
  }
  
  /******************************************************************************/
  
  gnrc_pktsnip_t *gnrc_priority_pktqueue_head(gnrc_priority_pktqueue_t *queue)
  {
      if (!queue || (gnrc_priority_pktqueue_length(queue) == 0)) {
          return NULL;
      }
      return (gnrc_pktsnip_t *)queue->first->data;
  }
  /******************************************************************************/
  
  void gnrc_priority_pktqueue_push(gnrc_priority_pktqueue_t *queue,
                                   gnrc_priority_pktqueue_node_t *node)
  {
      assert(queue != NULL);
      assert(node != NULL);
      assert(node->pkt != NULL);
      assert(sizeof(unsigned int) == sizeof(gnrc_pktsnip_t *));
  
      priority_queue_add(queue, (priority_queue_node_t *)node);
  }
  
  /******************************************************************************/
  
  void gnrc_priority_pktqueue_flush(gnrc_priority_pktqueue_t *queue)
  {
      assert(queue != NULL);
  
      if (gnrc_priority_pktqueue_length(queue) == 0) {
          return;
      }
      gnrc_priority_pktqueue_node_t *node;
      while ((node = (gnrc_priority_pktqueue_node_t *)priority_queue_remove_head(queue))) {
          gnrc_pktbuf_release(node->pkt);
          _free_node(node);
      }
  }
  
  /******************************************************************************/
  uint32_t gnrc_priority_pktqueue_length(gnrc_priority_pktqueue_t *queue)
  {
      assert(queue != NULL);
  
      uint32_t length = 0;
      priority_queue_node_t *node = queue->first;
      if (!node) {
          return length;
      }
  
      length++;
      while (node->next != NULL) {
          length++;
          node = node->next;
      }
      return length;
  }