ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/dbuf.c
Revision: 4565
Committed: Sun Aug 24 10:27:40 2014 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 3297 byte(s)
Log Message:
- Update GPL 2 license headers

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 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 static mp_pool_t *dbuf_pool;
34
35 void
36 dbuf_init(void)
37 {
38 dbuf_pool = mp_pool_new(sizeof(struct dbuf_block), MP_CHUNK_SIZE_DBUF);
39 }
40
41 struct dbuf_block *
42 dbuf_alloc(void)
43 {
44 struct dbuf_block *block = mp_pool_get(dbuf_pool);
45
46 ++block->refs;
47 return block;
48 }
49
50 void
51 dbuf_ref_free(struct dbuf_block *block)
52 {
53 if (--block->refs <= 0)
54 mp_pool_release(block);
55 }
56
57 void
58 dbuf_add(struct dbuf_queue *queue, struct dbuf_block *block)
59 {
60 block->refs++;
61 dlinkAddTail(block, make_dlink_node(), &queue->blocks);
62 queue->total_size += block->size;
63 }
64
65 void
66 dbuf_delete(struct dbuf_queue *queue, size_t count)
67 {
68 while (count > 0 && dbuf_length(queue) > 0)
69 {
70 dlink_node *node = queue->blocks.head;
71 struct dbuf_block *block = node->data;
72 size_t avail = block->size - queue->pos;
73
74 if (count >= avail)
75 {
76 count -= avail;
77 queue->total_size -= avail;
78
79 dbuf_ref_free(block);
80
81 dlinkDelete(node, &queue->blocks);
82 free_dlink_node(node);
83
84 queue->pos = 0;
85 }
86 else
87 {
88 queue->pos += count;
89
90 queue->total_size -= count;
91 count -= count;
92 }
93 }
94 }
95
96 void
97 dbuf_put_fmt(struct dbuf_block *dbuf, const char *pattern, ...)
98 {
99 va_list args;
100
101 assert(dbuf->refs == 1);
102
103 va_start(args, pattern);
104 dbuf_put_args(dbuf, pattern, args);
105 va_end(args);
106 }
107
108 void
109 dbuf_put_args(struct dbuf_block *dbuf, const char *data, va_list args)
110 {
111 assert(dbuf->refs == 1);
112
113 dbuf->size += vsnprintf(dbuf->data + dbuf->size, sizeof(dbuf->data) - dbuf->size, data, args);
114 if (dbuf->size > sizeof(dbuf->data))
115 dbuf->size = sizeof(dbuf->data); /* required by some versions of vsnprintf */
116 }
117
118 void
119 dbuf_put(struct dbuf_queue *queue, const char *buf, size_t sz)
120 {
121 while (sz > 0)
122 {
123 size_t avail;
124 struct dbuf_block *block = dbuf_length(queue) ? queue->blocks.tail->data : NULL;
125
126 if (block == NULL || sizeof(block->data) - block->size == 0)
127 {
128 block = dbuf_alloc();
129 dlinkAddTail(block, make_dlink_node(), &queue->blocks);
130 }
131
132 avail = sizeof(block->data) - block->size;
133 if (avail > sz)
134 avail = sz;
135
136 memcpy(&block->data[block->size], buf, avail);
137 block->size += avail;
138
139 queue->total_size += avail;
140
141 sz -= avail;
142 buf += avail;
143 }
144 }

Properties

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