jabberd2  2.7.0
jqueue.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 /* priority jqueues */
22 
23 #include "util.h"
24 
26  pool_t p;
27  jqueue_t q;
28 
29  p = pool_new();
30  q = (jqueue_t) pmalloco(p, sizeof(struct _jqueue_st));
31 
32  q->p = p;
33  q->init_time = time(NULL);
34 
35  return q;
36 }
37 
39  assert((int) (q != NULL));
40 
41  pool_free(q->p);
42 }
43 
44 void jqueue_push(jqueue_t q, void *data, int priority) {
45  _jqueue_node_t qn, scan;
46 
47  assert((int) (q != NULL));
48 
49  q->size++;
50 
51  /* node from the cache, or make a new one */
52  qn = q->cache;
53  if(qn != NULL)
54  q->cache = qn->next;
55  else
56  qn = (_jqueue_node_t) pmalloc(q->p, sizeof(struct _jqueue_node_st));
57 
58  qn->data = data;
59  qn->priority = priority;
60 
61  qn->next = NULL;
62  qn->prev = NULL;
63 
64  /* first one */
65  if(q->back == NULL && q->front == NULL) {
66  q->back = qn;
67  q->front = qn;
68 
69  return;
70  }
71 
72  /* find the first node with priority <= to us */
73  for(scan = q->back; scan != NULL && scan->priority > priority; scan = scan->next);
74 
75  /* didn't find one, so we have top priority - push us on the front */
76  if(scan == NULL) {
77  qn->prev = q->front;
78  qn->prev->next = qn;
79  q->front = qn;
80 
81  return;
82  }
83 
84  /* push us in front of scan */
85  qn->next = scan;
86  qn->prev = scan->prev;
87 
88  if(scan->prev != NULL)
89  scan->prev->next = qn;
90  else
91  q->back = qn;
92 
93  scan->prev = qn;
94 }
95 
97  void *data;
98  _jqueue_node_t qn;
99 
100  assert((int) (q != NULL));
101 
102  if(q->front == NULL)
103  return NULL;
104 
105  data = q->front->data;
106 
107  qn = q->front;
108 
109  if(qn->prev != NULL)
110  qn->prev->next = NULL;
111 
112  q->front = qn->prev;
113 
114  /* node to cache for later reuse */
115  qn->next = q->cache;
116  q->cache = qn;
117 
118  if(q->front == NULL)
119  q->back = NULL;
120 
121  q->size--;
122 
123  return data;
124 }
125 
127  return q->size;
128 }
129 
130 time_t jqueue_age(jqueue_t q) {
131  return time(NULL) - q->init_time;
132 }
int priority
Definition: util.h:317
void pool_free(pool_t p)
Definition: pool.c:226
void * pmalloc(pool_t p, int size)
Definition: pool.c:141
int jqueue_size(jqueue_t q)
Definition: jqueue.c:126
void * data
Definition: util.h:315
_jqueue_node_t front
Definition: util.h:327
#define pool_new()
Definition: pool.h:97
struct _jqueue_node_st * _jqueue_node_t
Definition: util.h:313
void jqueue_free(jqueue_t q)
Definition: jqueue.c:38
void * pmalloco(pool_t p, int size)
easy safety utility (for creating blank mem for structs, etc)
Definition: pool.c:183
void jqueue_push(jqueue_t q, void *data, int priority)
Definition: jqueue.c:44
_jqueue_node_t prev
Definition: util.h:320
time_t jqueue_age(jqueue_t q)
Definition: jqueue.c:130
void * jqueue_pull(jqueue_t q)
Definition: jqueue.c:96
time_t init_time
Definition: util.h:332
jqueue_t jqueue_new(void)
Definition: jqueue.c:25
pool_t p
Definition: util.h:324
_jqueue_node_t back
Definition: util.h:328
int size
Definition: util.h:330
pool - base node for a pool.
Definition: pool.h:80
_jqueue_node_t cache
Definition: util.h:325
_jqueue_node_t next
Definition: util.h:319
struct _jqueue_st * jqueue_t