comparison src/gaim_buffer.c @ 13200:33bef17125c2

[gaim-migrate @ 15563] This is the soon-to-be-infamous nonblocking network activity patch that I've been working on. Feel free to yell at me if this makes you unhappy. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Thu, 09 Feb 2006 04:17:56 +0000
parents
children 72414fe08156
comparison
equal deleted inserted replaced
13199:d8f238864c88 13200:33bef17125c2
1 /*
2 * @file gaim_buffer.h Buffer Utility Functions
3 * @ingroup core
4 *
5 * Gaim is the legal property of its developers, whose names are too numerous
6 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * source distribution.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #include "internal.h"
24
25 #include "gaim_buffer.h"
26
27 #define DEFAULT_BUF_SIZE 256
28
29 GaimCircBuffer *
30 gaim_circ_buffer_new(gsize growsize) {
31 GaimCircBuffer *buf = g_new0(GaimCircBuffer, 1);
32 buf->growsize = growsize ? growsize : DEFAULT_BUF_SIZE;
33 return buf;
34 }
35
36 void gaim_circ_buffer_destroy(GaimCircBuffer *buf) {
37 g_free(buf->buffer);
38 g_free(buf);
39 }
40
41 static void grow_circ_buffer(GaimCircBuffer *buf, gsize len) {
42 int in_offset = 0, out_offset = 0;
43 int start_buflen = buf->buflen;
44
45 while ((buf->buflen - buf->bufused) < len)
46 buf->buflen += buf->growsize;
47
48 if (buf->inptr != NULL) {
49 in_offset = buf->inptr - buf->buffer;
50 out_offset = buf->outptr - buf->buffer;
51 }
52 buf->buffer = g_realloc(buf->buffer, buf->buflen);
53
54 /* adjust the fill and remove pointer locations */
55 if (buf->inptr == NULL) {
56 buf->inptr = buf->outptr = buf->buffer;
57 } else {
58 buf->inptr = buf->buffer + in_offset;
59 buf->outptr = buf->buffer + out_offset;
60 }
61
62 /* If the fill pointer is wrapped to before the remove
63 * pointer, we need to shift the data */
64 if (in_offset < out_offset) {
65 int shift_n = MIN(buf->buflen - start_buflen,
66 in_offset);
67 memcpy(buf->buffer + start_buflen, buf->buffer,
68 shift_n);
69
70 /* If we couldn't fit the wrapped read buffer
71 * at the end */
72 if (shift_n < in_offset) {
73 memcpy(buf->buffer,
74 buf->buffer + shift_n,
75 in_offset - shift_n);
76 buf->inptr = buf->buffer +
77 (in_offset - shift_n);
78 } else {
79 buf->inptr = buf->buffer +
80 start_buflen + in_offset;
81 }
82 }
83 }
84
85 void gaim_circ_buffer_append(GaimCircBuffer *buf, gconstpointer src, gsize len) {
86
87 int len_stored;
88
89 /* Grow the buffer, if necessary */
90 if ((buf->buflen - buf->bufused) < len)
91 grow_circ_buffer(buf, len);
92
93 /* If we may need to wrap */
94 if ((buf->inptr - buf->outptr) >= 0)
95 len_stored = MIN(len, buf->buflen
96 - (buf->inptr - buf->buffer));
97 else
98 len_stored = len;
99
100 memcpy(buf->inptr, src, len_stored);
101
102 if (len_stored < len) {
103 memcpy(buf->buffer, src + len_stored, len - len_stored);
104 buf->inptr = buf->buffer + (len - len_stored);
105 } else if ((buf->buffer - buf->inptr) == len_stored) {
106 buf->inptr = buf->buffer;
107 } else {
108 buf->inptr += len_stored;
109 }
110
111 buf->bufused += len;
112 }
113
114 gsize gaim_circ_buffer_get_max_read(GaimCircBuffer *buf) {
115 int max_read;
116
117 if (buf->bufused == 0)
118 max_read = 0;
119 else if ((buf->outptr - buf->inptr) >= 0)
120 max_read = buf->buflen - (buf->outptr - buf->buffer);
121 else
122 max_read = buf->inptr - buf->outptr;
123
124 return max_read;
125 }
126
127 gboolean gaim_circ_buffer_mark_read(GaimCircBuffer *buf, gsize len) {
128 g_return_val_if_fail(gaim_circ_buffer_get_max_read(buf) >= len, FALSE);
129
130 buf->outptr += len;
131 buf->bufused -= len;
132 /* wrap to the start if we're at the end */
133 if ((buf->outptr - buf->buffer) == buf->buflen)
134 buf->outptr = buf->buffer;
135
136 return TRUE;
137 }
138