/[svn]/ircd-hybrid/branches/8.2.x/src/dbuf.c
ViewVC logotype

Contents of /ircd-hybrid/branches/8.2.x/src/dbuf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6965 - (show annotations)
Fri Dec 18 19:53:34 2015 UTC (4 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 3343 byte(s)
- dbuf.c:dbuf_put_args(): improve/clarify documentation

1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2015 ircd-hybrid development team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file dbuf.c
23 * \brief Supports dynamic data buffers.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "dbuf.h"
30 #include "memory.h"
31 #include "mempool.h"
32
33
34 static mp_pool_t *dbuf_pool;
35
36 void
37 dbuf_init(void)
38 {
39 dbuf_pool = mp_pool_new(sizeof(struct dbuf_block), MP_CHUNK_SIZE_DBUF);
40 }
41
42 struct dbuf_block *
43 dbuf_alloc(void)
44 {
45 struct dbuf_block *block = mp_pool_get(dbuf_pool);
46
47 ++block->refs;
48 return block;
49 }
50
51 void
52 dbuf_ref_free(struct dbuf_block *block)
53 {
54 if (--block->refs <= 0)
55 mp_pool_release(block);
56 }
57
58 void
59 dbuf_add(struct dbuf_queue *queue, struct dbuf_block *block)
60 {
61 block->refs++;
62 dlinkAddTail(block, make_dlink_node(), &queue->blocks);
63 queue->total_size += block->size;
64 }
65
66 void
67 dbuf_delete(struct dbuf_queue *queue, size_t count)
68 {
69 while (count > 0 && dbuf_length(queue) > 0)
70 {
71 dlink_node *node = queue->blocks.head;
72 struct dbuf_block *block = node->data;
73 size_t avail = block->size - queue->pos;
74
75 if (count >= avail)
76 {
77 count -= avail;
78 queue->total_size -= avail;
79
80 dbuf_ref_free(block);
81
82 dlinkDelete(node, &queue->blocks);
83 free_dlink_node(node);
84
85 queue->pos = 0;
86 }
87 else
88 {
89 queue->pos += count;
90
91 queue->total_size -= count;
92 count -= count;
93 }
94 }
95 }
96
97 void
98 dbuf_put_fmt(struct dbuf_block *dbuf, const char *pattern, ...)
99 {
100 va_list args;
101
102 assert(dbuf->refs == 1);
103
104 va_start(args, pattern);
105 dbuf_put_args(dbuf, pattern, args);
106 va_end(args);
107 }
108
109 void
110 dbuf_put_args(struct dbuf_block *dbuf, const char *data, va_list args)
111 {
112 assert(dbuf->refs == 1);
113
114 dbuf->size += vsnprintf(dbuf->data + dbuf->size, sizeof(dbuf->data) - dbuf->size, data, args);
115
116 /*
117 * As per C99, (v)snprintf returns the length the resulting string would be
118 */
119 if (dbuf->size > sizeof(dbuf->data))
120 dbuf->size = sizeof(dbuf->data);
121 }
122
123 void
124 dbuf_put(struct dbuf_queue *queue, const char *buf, size_t sz)
125 {
126 while (sz > 0)
127 {
128 size_t avail;
129 struct dbuf_block *block = dbuf_length(queue) ? queue->blocks.tail->data : NULL;
130
131 if (block == NULL || sizeof(block->data) - block->size == 0)
132 {
133 block = dbuf_alloc();
134 dlinkAddTail(block, make_dlink_node(), &queue->blocks);
135 }
136
137 avail = sizeof(block->data) - block->size;
138 if (avail > sz)
139 avail = sz;
140
141 memcpy(&block->data[block->size], buf, avail);
142 block->size += avail;
143
144 queue->total_size += avail;
145
146 sz -= avail;
147 buf += avail;
148 }
149 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28