ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/dbuf.c
Revision: 7797
Committed: Tue Oct 18 17:27:10 2016 UTC (8 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 3324 byte(s)
Log Message:
- Minor style corrections and constifications

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2016 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 /* As per C99, (v)snprintf returns the length the resulting string would be */
117 if (dbuf->size > sizeof(dbuf->data))
118 dbuf->size = sizeof(dbuf->data);
119 }
120
121 void
122 dbuf_put(struct dbuf_queue *queue, const char *buf, size_t sz)
123 {
124 while (sz > 0)
125 {
126 struct dbuf_block *block = dbuf_length(queue) ? queue->blocks.tail->data : NULL;
127
128 if (block == NULL || sizeof(block->data) - block->size == 0)
129 {
130 block = dbuf_alloc();
131 dlinkAddTail(block, make_dlink_node(), &queue->blocks);
132 }
133
134 size_t avail = sizeof(block->data) - block->size;
135 if (avail > sz)
136 avail = sz;
137
138 memcpy(&block->data[block->size], buf, avail);
139 block->size += avail;
140
141 queue->total_size += avail;
142
143 sz -= avail;
144 buf += avail;
145 }
146 }

Properties

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