Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / write-buffer.c
1 /*
2  write-buffer.c : irssi
3
4     Copyright (C) 2001 Timo Sirainen
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "settings.h"
25 #include "write-buffer.h"
26
27 #define BUFFER_BLOCK_SIZE 2048
28
29 typedef struct {
30         char *active_block;
31         int active_block_pos;
32
33         GSList *blocks;
34 } BUFFER_REC;
35
36 static GSList *empty_blocks;
37 static GHashTable *buffers;
38 static int block_count;
39
40 static int write_buffer_max_blocks;
41 static int timeout_tag;
42
43 static void write_buffer_new_block(BUFFER_REC *rec)
44 {
45         char *block;
46
47         if (empty_blocks == NULL)
48                 block = g_malloc(BUFFER_BLOCK_SIZE);
49         else {
50                 block = empty_blocks->data;
51                 empty_blocks = g_slist_remove(empty_blocks, block);
52         }
53
54         block_count++;
55         rec->active_block = block;
56         rec->active_block_pos = 0;
57         rec->blocks = g_slist_append(rec->blocks, block);
58 }
59
60 int write_buffer(int handle, const void *data, int size)
61 {
62         BUFFER_REC *rec;
63         const char *cdata = data;
64         int next_size;
65
66         if (write_buffer_max_blocks <= 0) {
67                 /* no write buffer */
68                 return write(handle, data, size);
69         }
70
71         if (size <= 0)
72                 return size;
73
74         rec = g_hash_table_lookup(buffers, GINT_TO_POINTER(handle));
75         if (rec == NULL) {
76                 rec = g_new0(BUFFER_REC, 1);
77                 write_buffer_new_block(rec);
78                 g_hash_table_insert(buffers, GINT_TO_POINTER(handle), rec);
79         }
80
81         while (size > 0) {
82                 if (rec->active_block_pos == BUFFER_BLOCK_SIZE)
83                         write_buffer_new_block(rec);
84
85                 next_size = size < BUFFER_BLOCK_SIZE-rec->active_block_pos ?
86                         size : BUFFER_BLOCK_SIZE-rec->active_block_pos;
87                 memcpy(rec->active_block+rec->active_block_pos,
88                        cdata, next_size);
89
90                 rec->active_block_pos += next_size;
91                 cdata += next_size;
92                 size -= next_size;
93         }
94
95         if (block_count > write_buffer_max_blocks)
96                 write_buffer_flush();
97
98         return size;
99 }
100
101 static int write_buffer_flush_rec(void *handlep, BUFFER_REC *rec)
102 {
103         GSList *tmp;
104         int handle, size;
105
106         handle = GPOINTER_TO_INT(handlep);
107         for (tmp = rec->blocks; tmp != NULL; tmp = tmp->next) {
108                 size = tmp->data != rec->active_block ? BUFFER_BLOCK_SIZE :
109                         rec->active_block_pos;
110                 write(handle, tmp->data, size);
111         }
112
113         empty_blocks = g_slist_concat(empty_blocks, rec->blocks);
114         g_free(rec);
115         return TRUE;
116 }
117
118 void write_buffer_flush(void)
119 {
120         g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
121         g_slist_free(empty_blocks);
122         empty_blocks = NULL;
123
124         g_hash_table_foreach_remove(buffers,
125                                     (GHRFunc) write_buffer_flush_rec, NULL);
126         block_count = 0;
127 }
128
129 static int flush_timeout(void)
130 {
131         write_buffer_flush();
132         return 1;
133 }
134
135 static void read_settings(void)
136 {
137         write_buffer_flush();
138
139         write_buffer_max_blocks =
140                 settings_get_size("write_buffer_size") / BUFFER_BLOCK_SIZE;
141
142         if (settings_get_time("write_buffer_timeout") > 0) {
143                 if (timeout_tag == -1) {
144                         timeout_tag = g_timeout_add(settings_get_time("write_buffer_timeout"),
145                                                     (GSourceFunc) flush_timeout,
146                                                     NULL);
147                 }
148         } else if (timeout_tag != -1) {
149                 g_source_remove(timeout_tag);
150                 timeout_tag = -1;
151         }
152 }
153
154 static void cmd_flushbuffer(void)
155 {
156         write_buffer_flush();
157 }
158
159 void write_buffer_init(void)
160 {
161         settings_add_time("misc", "write_buffer_timeout", "0");
162         settings_add_size("misc", "write_buffer_size", "0");
163
164         buffers = g_hash_table_new((GHashFunc) g_direct_hash,
165                                    (GCompareFunc) g_direct_equal);
166
167         empty_blocks = NULL;
168         block_count = 0;
169
170         timeout_tag = -1;
171         read_settings();
172         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
173         command_bind("flushbuffer", NULL, (SIGNAL_FUNC) cmd_flushbuffer);
174 }
175
176 void write_buffer_deinit(void)
177 {
178         if (timeout_tag != -1)
179                 g_source_remove(timeout_tag);
180
181         write_buffer_flush();
182         g_hash_table_destroy(buffers);
183
184         g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
185         g_slist_free(empty_blocks);
186
187         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
188         command_unbind("flushbuffer",  (SIGNAL_FUNC) cmd_flushbuffer);
189 }