Mercurial > pidgin
annotate libgaim/circbuffer.c @ 15335:a09936753f2f
[gaim-migrate @ 18127]
We actually do want number of bytes, and not the length of the string. Thanks to elb for noticing.
I blame temporary insanity for introducing the bug in the first place.
committer: Tailor Script <tailor@pidgin.im>
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Mon, 15 Jan 2007 06:07:54 +0000 |
parents | f08b43af6791 |
children |
rev | line source |
---|---|
14192 | 1 /* |
2 * @file circbuffer.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 "circbuffer.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) { | |
15297
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
37 g_return_if_fail(buf != NULL); |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
38 |
14192 | 39 g_free(buf->buffer); |
40 g_free(buf); | |
41 } | |
42 | |
43 static void grow_circ_buffer(GaimCircBuffer *buf, gsize len) { | |
44 int in_offset = 0, out_offset = 0; | |
15297
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
45 int start_buflen; |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
46 |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
47 g_return_if_fail(buf != NULL); |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
48 |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
49 start_buflen = buf->buflen; |
14192 | 50 |
51 while ((buf->buflen - buf->bufused) < len) | |
52 buf->buflen += buf->growsize; | |
53 | |
54 if (buf->inptr != NULL) { | |
55 in_offset = buf->inptr - buf->buffer; | |
56 out_offset = buf->outptr - buf->buffer; | |
57 } | |
58 buf->buffer = g_realloc(buf->buffer, buf->buflen); | |
59 | |
60 /* adjust the fill and remove pointer locations */ | |
61 if (buf->inptr == NULL) { | |
62 buf->inptr = buf->outptr = buf->buffer; | |
63 } else { | |
64 buf->inptr = buf->buffer + in_offset; | |
65 buf->outptr = buf->buffer + out_offset; | |
66 } | |
67 | |
68 /* If the fill pointer is wrapped to before the remove | |
69 * pointer, we need to shift the data */ | |
70 if (in_offset < out_offset) { | |
71 int shift_n = MIN(buf->buflen - start_buflen, | |
72 in_offset); | |
73 memcpy(buf->buffer + start_buflen, buf->buffer, | |
74 shift_n); | |
75 | |
76 /* If we couldn't fit the wrapped read buffer | |
77 * at the end */ | |
78 if (shift_n < in_offset) { | |
79 memmove(buf->buffer, | |
80 buf->buffer + shift_n, | |
81 in_offset - shift_n); | |
82 buf->inptr = buf->buffer + | |
83 (in_offset - shift_n); | |
84 } else { | |
85 buf->inptr = buf->buffer + | |
86 start_buflen + in_offset; | |
87 } | |
88 } | |
89 } | |
90 | |
91 void gaim_circ_buffer_append(GaimCircBuffer *buf, gconstpointer src, gsize len) { | |
92 | |
93 int len_stored; | |
94 | |
15297
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
95 g_return_if_fail(buf != NULL); |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
96 |
14192 | 97 /* Grow the buffer, if necessary */ |
98 if ((buf->buflen - buf->bufused) < len) | |
99 grow_circ_buffer(buf, len); | |
100 | |
101 /* If there is not enough room to copy all of src before hitting | |
102 * the end of the buffer then we will need to do two copies. | |
103 * One copy from inptr to the end of the buffer, and the | |
104 * second copy from the start of the buffer to the end of src. */ | |
105 if (buf->inptr >= buf->outptr) | |
106 len_stored = MIN(len, buf->buflen | |
107 - (buf->inptr - buf->buffer)); | |
108 else | |
109 len_stored = len; | |
110 | |
111 memcpy(buf->inptr, src, len_stored); | |
112 | |
113 if (len_stored < len) { | |
14955
e3daf404bf2c
[gaim-migrate @ 17734]
Luke Schierer <lschiere@pidgin.im>
parents:
14192
diff
changeset
|
114 memcpy(buf->buffer, (char*)src + len_stored, len - len_stored); |
14192 | 115 buf->inptr = buf->buffer + (len - len_stored); |
116 } else if ((buf->buffer - buf->inptr) == len_stored) { | |
117 buf->inptr = buf->buffer; | |
118 } else { | |
119 buf->inptr += len_stored; | |
120 } | |
121 | |
122 buf->bufused += len; | |
123 } | |
124 | |
125 gsize gaim_circ_buffer_get_max_read(GaimCircBuffer *buf) { | |
126 int max_read; | |
127 | |
15297
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
128 g_return_val_if_fail(buf != NULL, 0); |
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
129 |
14192 | 130 if (buf->bufused == 0) |
131 max_read = 0; | |
132 else if ((buf->outptr - buf->inptr) >= 0) | |
133 max_read = buf->buflen - (buf->outptr - buf->buffer); | |
134 else | |
135 max_read = buf->inptr - buf->outptr; | |
136 | |
137 return max_read; | |
138 } | |
139 | |
140 gboolean gaim_circ_buffer_mark_read(GaimCircBuffer *buf, gsize len) { | |
15297
f08b43af6791
[gaim-migrate @ 18088]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
141 g_return_val_if_fail(buf != NULL, FALSE); |
14192 | 142 g_return_val_if_fail(gaim_circ_buffer_get_max_read(buf) >= len, FALSE); |
143 | |
144 buf->outptr += len; | |
145 buf->bufused -= len; | |
146 /* wrap to the start if we're at the end */ | |
147 if ((buf->outptr - buf->buffer) == buf->buflen) | |
148 buf->outptr = buf->buffer; | |
149 | |
150 return TRUE; | |
151 } | |
152 |