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