2 * IRC - Internet Relay Chat, ircd/msgq.c
3 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
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)
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.
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.
24 #include "ircd_alloc.h"
25 #include "ircd_defs.h"
26 #include "ircd_snprintf.h"
31 #include <sys/types.h>
32 #include <sys/uio.h> /* struct iovec */
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 */
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 */
50 struct MsgBuf *free_mbs;
51 struct Msg *free_msgs;
52 } MQData = { 0, 0, 0 };
54 struct MsgCounts msgBufCounts = { 0, 0 };
55 struct MsgCounts msgCounts = { 0, 0 };
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
62 msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
69 assert(0 != qlist->head);
70 assert(0 != length_p);
72 m = qlist->head; /* find the msg we're deleting from */
74 msglen = m->msg->length - m->sent; /* calculate how much is left */
76 if (*length_p >= msglen) { /* deleted it all? */
77 mq->length -= msglen; /* decrement length */
78 mq->count--; /* decrement the message count */
81 msgq_clean(m->msg); /* free up the struct MsgBuf */
82 m->msg = 0; /* don't let it point anywhere nasty, please */
84 if (qlist->head == qlist->tail) /* figure out if we emptied the queue */
85 qlist->head = qlist->tail = 0;
87 qlist->head = m->next; /* just shift the list down some */
89 msgCounts.used--; /* struct Msg is not in use anymore */
91 m->next = MQData.free_msgs; /* throw it onto the free list */
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 */
101 * This just initializes a struct MsgQ.
104 msgq_init(struct MsgQ *mq)
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...
123 msgq_delete(struct MsgQ *mq, unsigned int length)
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);
140 * This is the more intelligent routine that can fill in an array of
144 msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count,
156 if (mq->length <= 0) /* no data to map */
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;
164 queue = mq->queue.head->next; /* where we start later... */
166 i++; /* filled an iovec... */
167 if (!--count) /* check for space */
170 queue = mq->queue.head; /* start at head of queue */
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;
177 prio = mq->prio.head->next; /* where we start later... */
179 i++; /* filled an iovec... */
180 if (!--count) /* check for space */
183 prio = mq->prio.head; /* start at head of prio */
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;
190 i++; /* filled an iovec... */
191 if (!--count) /* check for space */
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;
200 i++; /* filled an iovec... */
201 if (!--count) /* check for space */
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
214 msgq_vmake(struct Client *dest, const char *format, va_list vl)
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;
226 msgBufCounts.used++; /* we're using another */
228 mb->next = MQData.msgs; /* initialize the msgbuf */
229 mb->prev_p = &MQData.msgs;
232 /* fill the buffer */
233 mb->length = ircd_vsnprintf(dest, mb->msg, sizeof(mb->msg) - 2, format, vl);
235 if (mb->length > sizeof(mb->msg) - 3)
236 mb->length = sizeof(mb->msg) - 3;
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 */
242 assert(mb->length < sizeof(mb->msg));
244 if (MQData.msgs) /* link it into the list */
245 MQData.msgs->prev_p = &mb->next;
252 msgq_make(struct Client *dest, const char *format, ...)
257 va_start(vl, format);
258 mb = msgq_vmake(dest, format, vl);
265 * This routine is used to append a formatted string to a struct MsgBuf.
268 msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format, ...)
275 assert(2 < mb->length);
276 assert(sizeof(mb->msg) > mb->length);
278 mb->length -= 2; /* back up to before \r\n */
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);
285 if (mb->length > sizeof(mb->msg) - 3)
286 mb->length = sizeof(mb->msg) - 3;
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 */
292 assert(mb->length < sizeof(mb->msg));
296 * This routine is called to decrement the reference count on a
297 * struct MsgBuf and delete it if necessary.
300 msgq_clean(struct MsgBuf *mb)
304 assert(0 != mb->prev_p);
306 if (!--mb->ref) { /* deallocate the message */
307 *mb->prev_p = mb->next; /* clip it out of active MsgBuf's list */
309 mb->next->prev_p = mb->prev_p;
311 mb->next = MQData.free_mbs; /* add it to free list */
312 MQData.free_mbs = mb;
316 msgBufCounts.used--; /* decrement the usage count */
321 * This routine simply adds a struct Msg to the end of a user's MsgQ.
324 msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
326 struct MsgQList *qlist;
333 Debug((DEBUG_SEND, "Adding buffer %p [%.*s] to %s queue", mb,
334 mb->length - 2, mb->msg, prio ? "priority" : "normal"));
336 qlist = prio ? &mq->prio : &mq->queue;
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;
344 msgCounts.used++; /* we're using another */
346 msg->next = 0; /* initialize the msg */
350 mb->ref++; /* increment the ref count on the buffer */
352 if (!qlist->head) /* queue list was empty; head and tail point to msg */
353 qlist->head = qlist->tail = msg;
355 assert(0 != qlist->tail);
357 qlist->tail->next = msg; /* queue had something in it; add to end */
361 mq->length += mb->length; /* update the queue length */
362 mq->count++; /* and the queue count */
366 * This is for reporting memory usage by the msgq system.
369 msgq_count_memory(size_t *msg_alloc, size_t *msg_used, size_t *msgbuf_alloc,
372 assert(0 != msg_alloc);
373 assert(0 != msg_used);
374 assert(0 != msgbuf_alloc);
375 assert(0 != msgbuf_used);
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);
384 * This routine is used simply to report how much bufferspace is left.
387 msgq_bufleft(struct MsgBuf *mb)
391 return sizeof(mb->msg) - mb->length - 1; /* \r\n counted in mb->length */