ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/dbuf.c
Revision: 1654
Committed: Fri Nov 16 19:39:37 2012 UTC (12 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 2692 byte(s)
Log Message:
- Implemented memory pool allocator which basically is taken from Tor's
  mempool allocator for Tor cells
- Fixed compile warnings in conf_class.c
- ./configure --enable-assert works again

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * dbuf.c: Supports dynamic data buffers.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26 michael 1011 #include "list.h"
27 adx 30 #include "dbuf.h"
28     #include "memory.h"
29 michael 1654 #include "mempool.h"
30 adx 30
31 michael 1654 static mp_pool_t *dbuf_pool;
32 adx 30
33     void
34     dbuf_init(void)
35     {
36 michael 1654 dbuf_pool = mp_pool_new(sizeof(struct dbuf_block), MP_CHUNK_SIZE_DBUF);
37 adx 30 }
38    
39     static struct dbuf_block *
40     dbuf_alloc(struct dbuf_queue *qptr)
41     {
42 michael 1654 struct dbuf_block *block = mp_pool_get(dbuf_pool);
43 adx 30
44 michael 1654 memset(block, 0, sizeof(*block));
45 adx 30 dlinkAddTail(block, make_dlink_node(), &qptr->blocks);
46     return block;
47     }
48    
49     void
50     dbuf_put(struct dbuf_queue *qptr, char *data, size_t count)
51     {
52     struct dbuf_block *last;
53     size_t amount;
54    
55     assert(count > 0);
56     if (qptr->blocks.tail == NULL)
57     dbuf_alloc(qptr);
58    
59     do {
60     last = qptr->blocks.tail->data;
61    
62     amount = DBUF_BLOCK_SIZE - last->size;
63     if (!amount)
64     {
65     last = dbuf_alloc(qptr);
66     amount = DBUF_BLOCK_SIZE;
67     }
68     if (amount > count)
69     amount = count;
70    
71     memcpy((void *) &last->data[last->size], data, amount);
72     count -= amount;
73     last->size += amount;
74     qptr->total_size += amount;
75    
76     data += amount;
77    
78     } while (count > 0);
79     }
80    
81     void
82     dbuf_delete(struct dbuf_queue *qptr, size_t count)
83     {
84     dlink_node *ptr;
85     struct dbuf_block *first;
86    
87     assert(qptr->total_size >= count);
88     if (count == 0)
89     return;
90    
91     /* free whole blocks first.. */
92     while (1)
93     {
94     if (!count)
95     return;
96     ptr = qptr->blocks.head;
97     first = ptr->data;
98     if (count < first->size)
99     break;
100    
101     qptr->total_size -= first->size;
102     count -= first->size;
103     dlinkDelete(ptr, &qptr->blocks);
104     free_dlink_node(ptr);
105 michael 1654 mp_pool_release(first);
106 adx 30 }
107    
108     /* ..then remove data from the beginning of the queue */
109     first->size -= count;
110     qptr->total_size -= count;
111     memmove((void *) &first->data, (void *) &first->data[count], first->size);
112     }
113    

Properties

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