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.
22 #include "ircd_alloc.h"
23 #include "ircd_defs.h"
24 #include "ircd_snprintf.h"
29 #include <sys/types.h>
30 #include <sys/uio.h> /* struct iovec */
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 */
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 */
48 struct MsgBuf *free_mbs;
49 struct Msg *free_msgs;
50 } MQData = { 0, 0, 0 };
52 struct MsgCounts msgBufCounts = { 0, 0 };
53 struct MsgCounts msgCounts = { 0, 0 };
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
60 msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
67 assert(0 != qlist->head);
68 assert(0 != length_p);
70 m = qlist->head; /* find the msg we're deleting from */
72 msglen = m->msg->length - m->sent; /* calculate how much is left */
74 if (*length_p >= msglen) { /* deleted it all? */
75 mq->length -= msglen; /* decrement length */
76 mq->count--; /* decrement the message count */
79 msgq_clean(m->msg); /* free up the struct MsgBuf */
80 m->msg = 0; /* don't let it point anywhere nasty, please */
82 if (qlist->head == qlist->tail) /* figure out if we emptied the queue */
83 qlist->head = qlist->tail = 0;
85 qlist->head = m->next; /* just shift the list down some */
87 msgCounts.used--; /* struct Msg is not in use anymore */
89 m->next = MQData.free_msgs; /* throw it onto the free list */
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 */
99 * This just initializes a struct MsgQ.
102 msgq_init(struct MsgQ *mq)
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...
121 msgq_delete(struct MsgQ *mq, unsigned int length)
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);
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.
144 msgq_map(const struct MsgQ *mq, unsigned int *length_p)
147 assert(0 != length_p);
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;
163 return 0; /* shouldn't ever happen */
167 * This is the more intelligent routine that can fill in an array of
171 msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count)
181 if (mq->length <= 0) /* no data to map */
184 if (mq->queue.head && mq->queue.head->sent > 0) { /* partial msg on norm q */
185 iov[i].iov_base = mq->queue.head->msg->msg + mq->queue.head->sent;
186 iov[i].iov_len = mq->queue.head->msg->length - mq->queue.head->sent;
188 queue = mq->queue.head->next; /* where we start later... */
190 i++; /* filled an iovec... */
191 if (!--count) /* check for space */
194 queue = mq->queue.head; /* start at head of queue */
196 if (mq->prio.head && mq->prio.head->sent > 0) { /* partial msg on prio q */
197 iov[i].iov_base = mq->prio.head->msg->msg + mq->prio.head->sent;
198 iov[i].iov_len = mq->prio.head->msg->length - mq->prio.head->sent;
200 prio = mq->prio.head->next; /* where we start later... */
202 i++; /* filled an iovec... */
203 if (!--count) /* check for space */
206 prio = mq->prio.head; /* start at head of prio */
208 for (; prio; prio = prio->next) { /* go through prio queue */
209 iov[i].iov_base = prio->msg->msg; /* store message */
210 iov[i].iov_len = prio->msg->length;
212 i++; /* filled an iovec... */
213 if (!--count) /* check for space */
217 for (; queue; queue = queue->next) { /* go through normal queue */
218 iov[i].iov_base = queue->msg->msg;
219 iov[i].iov_len = queue->msg->length;
221 i++; /* filled an iovec... */
222 if (!--count) /* check for space */
230 * This routine builds a struct MsgBuf with the appropriate contents
231 * and returns it; this saves us from having to worry about the contents
232 * of struct MsgBuf in anything other than this module
235 msgq_vmake(struct Client *dest, const char *format, va_list vl)
241 if (!(mb = MQData.free_mbs)) { /* do I need to allocate one? */
242 mb = (struct MsgBuf *)MyMalloc(sizeof(struct MsgBuf));
243 msgBufCounts.alloc++; /* we allocated another */
244 } else /* shift the free list */
245 MQData.free_mbs = MQData.free_mbs->next;
247 msgBufCounts.used++; /* we're using another */
249 mb->next = MQData.msgs; /* initialize the msgbuf */
250 mb->prev_p = &MQData.msgs;
253 /* fill the buffer */
254 mb->length = ircd_vsnprintf(dest, mb->msg, sizeof(mb->msg) - 2, format, vl);
256 mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
257 mb->msg[mb->length++] = '\n';
258 mb->msg[mb->length] = '\0'; /* not strictly necessary */
260 if (MQData.msgs) /* link it into the list */
261 MQData.msgs->prev_p = &mb->next;
268 msgq_make(struct Client *dest, const char *format, ...)
273 va_start(vl, format);
274 mb = msgq_vmake(dest, format, vl);
281 * This routine is used to append a formatted string to a struct MsgBuf.
284 msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format, ...)
291 mb->length -= 2; /* back up to before \r\n */
293 va_start(vl, format); /* append to the buffer */
294 mb->length += ircd_vsnprintf(dest, mb->msg + mb->length,
295 sizeof(mb->msg) - 2 - mb->length, format, vl);
298 mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
299 mb->msg[mb->length++] = '\n';
300 mb->msg[mb->length] = '\0'; /* not strictly necessary */
304 * This routine is called to decrement the reference count on a
305 * struct MsgBuf and delete it if necessary.
308 msgq_clean(struct MsgBuf *mb)
312 assert(0 != mb->prev_p);
314 if (!--mb->ref) { /* deallocate the message */
315 *mb->prev_p = mb->next; /* clip it out of active MsgBuf's list */
317 mb->next->prev_p = mb->prev_p;
319 mb->next = MQData.free_mbs; /* add it to free list */
320 MQData.free_mbs = mb;
324 msgBufCounts.used--; /* decrement the usage count */
329 * This routine simply adds a struct Msg to the end of a user's MsgQ.
332 msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
334 struct MsgQList *qlist;
341 Debug((DEBUG_SEND, "Adding buffer %p [%.*s] to %s queue", mb,
342 mb->length - 2, mb->msg, prio ? "priority" : "normal"));
344 qlist = prio ? &mq->prio : &mq->queue;
346 if (!(msg = MQData.free_msgs)) { /* do I need to allocate one? */
347 msg = (struct Msg *)MyMalloc(sizeof(struct Msg));
348 msgCounts.alloc++; /* we allocated another */
349 } else /* shift the free list */
350 MQData.free_msgs = MQData.free_msgs->next;
352 msgCounts.used++; /* we're using another */
354 msg->next = 0; /* initialize the msg */
358 mb->ref++; /* increment the ref count on the buffer */
360 if (!qlist->head) /* queue list was empty; head and tail point to msg */
361 qlist->head = qlist->tail = msg;
363 assert(0 != qlist->tail);
365 qlist->tail->next = msg; /* queue had something in it; add to end */
369 mq->length += mb->length; /* update the queue length */
370 mq->count++; /* and the queue count */
374 * This is for reporting memory usage by the msgq system.
377 msgq_count_memory(size_t *msg_alloc, size_t *msg_used, size_t *msgbuf_alloc,
380 assert(0 != msg_alloc);
381 assert(0 != msg_used);
382 assert(0 != msgbuf_alloc);
383 assert(0 != msgbuf_used);
385 *msg_alloc = msgCounts.alloc * sizeof(struct Msg);
386 *msg_used = msgCounts.used * sizeof(struct Msg);
387 *msgbuf_alloc = msgCounts.alloc * sizeof(struct MsgBuf);
388 *msgbuf_used = msgCounts.used * sizeof(struct MsgBuf);
392 * This routine is used simply to report how much bufferspace is left.
395 msgq_bufleft(struct MsgBuf *mb)
399 return sizeof(mb->msg) - mb->length; /* the -2 for \r\n is in mb->length */