ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/dbuf.c
Revision: 7668
Committed: Wed Jul 20 17:09:49 2016 UTC (9 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 3343 byte(s)
Log Message:
- Fixed svn properties

File Contents

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

Properties

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