imported.
[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 void read_settings(void)
130 {
131         int msecs;
132
133         if (timeout_tag != -1)
134                 g_source_remove(timeout_tag);
135
136         write_buffer_flush();
137
138         write_buffer_max_blocks = settings_get_int("write_buffer_kb") *
139                 1024/BUFFER_BLOCK_SIZE;
140
141         if (settings_get_int("write_buffer_mins") > 0) {
142                 msecs = settings_get_int("write_buffer_mins")*60*1000;
143                 timeout_tag = g_timeout_add(msecs,
144                                             (GSourceFunc) write_buffer_flush,
145                                             NULL);
146         }
147 }
148
149 static void cmd_flushbuffer(void)
150 {
151         write_buffer_flush();
152 }
153
154 void write_buffer_init(void)
155 {
156         settings_add_int("misc", "write_buffer_mins", 0);
157         settings_add_int("misc", "write_buffer_kb", 0);
158
159         buffers = g_hash_table_new((GHashFunc) g_direct_hash,
160                                    (GCompareFunc) g_direct_equal);
161
162         empty_blocks = NULL;
163         block_count = 0;
164
165         timeout_tag = -1;
166         read_settings();
167         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
168         command_bind("flushbuffer", NULL, (SIGNAL_FUNC) cmd_flushbuffer);
169 }
170
171 void write_buffer_deinit(void)
172 {
173         if (timeout_tag != -1)
174                 g_source_remove(timeout_tag);
175
176         write_buffer_flush();
177         g_hash_table_destroy(buffers);
178
179         g_slist_foreach(empty_blocks, (GFunc) g_free, NULL);
180         g_slist_free(empty_blocks);
181
182         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
183         command_unbind("flushbuffer",  (SIGNAL_FUNC) cmd_flushbuffer);
184 }