Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / msgq.c
1 /*
2  * IRC - Internet Relay Chat, ircd/msgq.c
3  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "config.h"
22
23 #include "msgq.h"
24 #include "ircd_alloc.h"
25 #include "ircd_defs.h"
26 #include "ircd_snprintf.h"
27 #include "s_debug.h"
28
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <sys/types.h>
32 #include <sys/uio.h>    /* struct iovec */
33
34 struct MsgBuf {
35   struct MsgBuf *next;          /* next msg in global queue */
36   struct MsgBuf **prev_p;       /* what points to us in linked list */
37   unsigned int ref;             /* reference count */
38   unsigned int length;          /* length of message */
39   char msg[BUFSIZE + 1];        /* the message */
40 };
41
42 struct Msg {
43   struct Msg *next;             /* next msg */
44   unsigned int sent;            /* bytes in msg that have already been sent */
45   struct MsgBuf *msg;           /* actual message in queue */
46 };
47
48 static struct {
49   struct MsgBuf *msgs;
50   struct MsgBuf *free_mbs;
51   struct Msg *free_msgs;
52 } MQData = { 0, 0, 0 };
53
54 struct MsgCounts msgBufCounts = { 0, 0 };
55 struct MsgCounts msgCounts = { 0, 0 };
56
57 /*
58  * This routine is used to remove a certain amount of data from a given
59  * queue and release the Msg (and MsgBuf) structure if needed
60  */
61 static void
62 msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
63 {
64   struct Msg *m;
65   unsigned int msglen;
66
67   assert(0 != mq);
68   assert(0 != qlist);
69   assert(0 != qlist->head);
70   assert(0 != length_p);
71
72   m = qlist->head; /* find the msg we're deleting from */
73
74   msglen = m->msg->length - m->sent; /* calculate how much is left */
75
76   if (*length_p >= msglen) { /* deleted it all? */
77     mq->length -= msglen; /* decrement length */
78     mq->count--; /* decrement the message count */
79     *length_p -= msglen;
80
81     msgq_clean(m->msg); /* free up the struct MsgBuf */
82     m->msg = 0; /* don't let it point anywhere nasty, please */
83
84     if (qlist->head == qlist->tail) /* figure out if we emptied the queue */
85       qlist->head = qlist->tail = 0;
86     else
87       qlist->head = m->next; /* just shift the list down some */
88
89     msgCounts.used--; /* struct Msg is not in use anymore */
90
91     m->next = MQData.free_msgs; /* throw it onto the free list */
92     MQData.free_msgs = m;
93   } else {
94     mq->length -= *length_p; /* decrement queue length */
95     m->sent += *length_p; /* this much of the message has been sent */
96     *length_p = 0; /* we've dealt with it all */
97   }
98 }
99
100 /*
101  * This just initializes a struct MsgQ.
102  */
103 void
104 msgq_init(struct MsgQ *mq)
105 {
106   assert(0 != mq);
107
108   mq->length = 0;
109   mq->count = 0;
110   mq->queue.head = 0;
111   mq->queue.tail = 0;
112   mq->prio.head = 0;
113   mq->prio.tail = 0;
114 }
115
116 /*
117  * This routine is used to delete the specified number of bytes off
118  * of the queue.  We only really need to worry about one struct Msg*,
119  * but this allows us to retain the flexibility to deal with more,
120  * which means we could do something fancy involving writev...
121  */
122 void
123 msgq_delete(struct MsgQ *mq, unsigned int length)
124 {
125   assert(0 != mq);
126
127   while (length > 0) {
128     if (mq->queue.head && mq->queue.head->sent > 0) /* partial msg on norm q */
129       msgq_delmsg(mq, &mq->queue, &length);
130     else if (mq->prio.head) /* message (partial or complete) on prio queue */
131       msgq_delmsg(mq, &mq->prio, &length);
132     else if (mq->queue.head) /* message on normal queue */
133       msgq_delmsg(mq, &mq->queue, &length);
134     else
135       break;
136   }
137 }
138
139 /*
140  * This is the more intelligent routine that can fill in an array of
141  * struct iovec's.
142  */
143 int
144 msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count,
145             unsigned int *len)
146 {
147   struct Msg *queue;
148   struct Msg *prio;
149   int i = 0;
150
151   assert(0 != mq);
152   assert(0 != iov);
153   assert(0 != count);
154   assert(0 != len);
155
156   if (mq->length <= 0) /* no data to map */
157     return 0;
158
159   if (mq->queue.head && mq->queue.head->sent > 0) { /* partial msg on norm q */
160     iov[i].iov_base = mq->queue.head->msg->msg + mq->queue.head->sent;
161     iov[i].iov_len = mq->queue.head->msg->length - mq->queue.head->sent;
162     *len += iov[i].iov_len;
163
164     queue = mq->queue.head->next; /* where we start later... */
165
166     i++; /* filled an iovec... */
167     if (!--count) /* check for space */
168       return i;
169   } else
170     queue = mq->queue.head; /* start at head of queue */
171
172   if (mq->prio.head && mq->prio.head->sent > 0) { /* partial msg on prio q */
173     iov[i].iov_base = mq->prio.head->msg->msg + mq->prio.head->sent;
174     iov[i].iov_len = mq->prio.head->msg->length - mq->prio.head->sent;
175     *len += iov[i].iov_len;
176
177     prio = mq->prio.head->next; /* where we start later... */
178
179     i++; /* filled an iovec... */
180     if (!--count) /* check for space */
181       return i;
182   } else
183     prio = mq->prio.head; /* start at head of prio */
184
185   for (; prio; prio = prio->next) { /* go through prio queue */
186     iov[i].iov_base = prio->msg->msg; /* store message */
187     iov[i].iov_len = prio->msg->length;
188     *len += iov[i].iov_len;
189
190     i++; /* filled an iovec... */
191     if (!--count) /* check for space */
192       return i;
193   }
194
195   for (; queue; queue = queue->next) { /* go through normal queue */
196     iov[i].iov_base = queue->msg->msg;
197     iov[i].iov_len = queue->msg->length;
198     *len += iov[i].iov_len;
199
200     i++; /* filled an iovec... */
201     if (!--count) /* check for space */
202       return i;
203   }
204
205   return i;
206 }
207
208 /*
209  * This routine builds a struct MsgBuf with the appropriate contents
210  * and returns it; this saves us from having to worry about the contents
211  * of struct MsgBuf in anything other than this module
212  */
213 struct MsgBuf *
214 msgq_vmake(struct Client *dest, const char *format, va_list vl)
215 {
216   struct MsgBuf *mb;
217
218   assert(0 != format);
219
220   if (!(mb = MQData.free_mbs)) { /* do I need to allocate one? */
221     mb = (struct MsgBuf *)MyMalloc(sizeof(struct MsgBuf));
222     msgBufCounts.alloc++; /* we allocated another */
223   } else /* shift the free list */
224     MQData.free_mbs = MQData.free_mbs->next;
225
226   msgBufCounts.used++; /* we're using another */
227
228   mb->next = MQData.msgs; /* initialize the msgbuf */
229   mb->prev_p = &MQData.msgs;
230   mb->ref = 1;
231
232   /* fill the buffer */
233   mb->length = ircd_vsnprintf(dest, mb->msg, sizeof(mb->msg) - 2, format, vl);
234
235   if (mb->length > sizeof(mb->msg) - 3)
236     mb->length = sizeof(mb->msg) - 3;
237
238   mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
239   mb->msg[mb->length++] = '\n';
240   mb->msg[mb->length] = '\0'; /* not strictly necessary */
241
242   assert(mb->length < sizeof(mb->msg));
243
244   if (MQData.msgs) /* link it into the list */
245     MQData.msgs->prev_p = &mb->next;
246   MQData.msgs = mb;
247
248   return mb;
249 }
250
251 struct MsgBuf *
252 msgq_make(struct Client *dest, const char *format, ...)
253 {
254   va_list vl;
255   struct MsgBuf *mb;
256
257   va_start(vl, format);
258   mb = msgq_vmake(dest, format, vl);
259   va_end(vl);
260
261   return mb;
262 }
263
264 /*
265  * This routine is used to append a formatted string to a struct MsgBuf.
266  */
267 void
268 msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format, ...)
269 {
270   va_list vl;
271
272   assert(0 != mb);
273   assert(0 != format);
274
275   assert(2 < mb->length);
276   assert(sizeof(mb->msg) > mb->length);
277
278   mb->length -= 2; /* back up to before \r\n */
279
280   va_start(vl, format); /* append to the buffer */
281   mb->length += ircd_vsnprintf(dest, mb->msg + mb->length,
282                                sizeof(mb->msg) - 2 - mb->length, format, vl);
283   va_end(vl);
284
285   if (mb->length > sizeof(mb->msg) - 3)
286     mb->length = sizeof(mb->msg) - 3;
287
288   mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
289   mb->msg[mb->length++] = '\n';
290   mb->msg[mb->length] = '\0'; /* not strictly necessary */
291
292   assert(mb->length < sizeof(mb->msg));
293 }
294
295 /*
296  * This routine is called to decrement the reference count on a
297  * struct MsgBuf and delete it if necessary.
298  */
299 void
300 msgq_clean(struct MsgBuf *mb)
301 {
302   assert(0 != mb);
303   assert(0 < mb->ref);
304   assert(0 != mb->prev_p);
305
306   if (!--mb->ref) { /* deallocate the message */
307     *mb->prev_p = mb->next; /* clip it out of active MsgBuf's list */
308     if (mb->next)
309       mb->next->prev_p = mb->prev_p;
310
311     mb->next = MQData.free_mbs; /* add it to free list */
312     MQData.free_mbs = mb;
313
314     mb->prev_p = 0;
315
316     msgBufCounts.used--; /* decrement the usage count */
317   }
318 }
319
320 /*
321  * This routine simply adds a struct Msg to the end of a user's MsgQ.
322  */
323 void
324 msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
325 {
326   struct MsgQList *qlist;
327   struct Msg *msg;
328
329   assert(0 != mq);
330   assert(0 != mb);
331   assert(0 < mb->ref);
332
333   Debug((DEBUG_SEND, "Adding buffer %p [%.*s] to %s queue", mb,
334          mb->length - 2, mb->msg, prio ? "priority" : "normal"));
335
336   qlist = prio ? &mq->prio : &mq->queue;
337
338   if (!(msg = MQData.free_msgs)) { /* do I need to allocate one? */
339     msg = (struct Msg *)MyMalloc(sizeof(struct Msg));
340     msgCounts.alloc++; /* we allocated another */
341   } else /* shift the free list */
342     MQData.free_msgs = MQData.free_msgs->next;
343
344   msgCounts.used++; /* we're using another */
345
346   msg->next = 0; /* initialize the msg */
347   msg->sent = 0;
348   msg->msg = mb;
349
350   mb->ref++; /* increment the ref count on the buffer */
351
352   if (!qlist->head) /* queue list was empty; head and tail point to msg */
353     qlist->head = qlist->tail = msg;
354   else {
355     assert(0 != qlist->tail);
356
357     qlist->tail->next = msg; /* queue had something in it; add to end */
358     qlist->tail = msg;
359   }
360
361   mq->length += mb->length; /* update the queue length */
362   mq->count++; /* and the queue count */
363 }
364
365 /*
366  * This is for reporting memory usage by the msgq system.
367  */
368 void
369 msgq_count_memory(size_t *msg_alloc, size_t *msg_used, size_t *msgbuf_alloc,
370                   size_t *msgbuf_used)
371 {
372   assert(0 != msg_alloc);
373   assert(0 != msg_used);
374   assert(0 != msgbuf_alloc);
375   assert(0 != msgbuf_used);
376
377   *msg_alloc = msgCounts.alloc * sizeof(struct Msg);
378   *msg_used = msgCounts.used * sizeof(struct Msg);
379   *msgbuf_alloc = msgCounts.alloc * sizeof(struct MsgBuf);
380   *msgbuf_used = msgCounts.used * sizeof(struct MsgBuf);
381 }
382
383 /*
384  * This routine is used simply to report how much bufferspace is left.
385  */
386 unsigned int
387 msgq_bufleft(struct MsgBuf *mb)
388 {
389   assert(0 != mb);
390
391   return sizeof(mb->msg) - mb->length - 1; /* \r\n counted in mb->length */
392 }