ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/dbuf.c
Revision: 4086
Committed: Sat Jun 28 16:44:20 2014 UTC (11 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 3293 byte(s)
Log Message:
- Let mp_pool_get() clear memory

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

Properties

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