Sun, 02 Jun 2013 22:17:12 +0200
VV: win32 support. No runtimes dependencies in dep-bundle yet. Refs #11075
| 13201 | 1 | /* |
|
14013
86dac5633bd9
[gaim-migrate @ 16496]
Mark Doliner <markdoliner@pidgin.im>
parents:
13830
diff
changeset
|
2 | * @file circbuffer.h Buffer Utility Functions |
| 13201 | 3 | * @ingroup core |
|
20147
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
4 | */ |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
5 | |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
6 | /* Purple is the legal property of its developers, whose names are too numerous |
| 13201 | 7 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 8 | * source distribution. | |
| 9 | * | |
| 10 | * This program is free software; you can redistribute it and/or modify | |
| 11 | * it under the terms of the GNU General Public License as published by | |
| 12 | * the Free Software Foundation; either version 2 of the License, or | |
| 13 | * (at your option) any later version. | |
| 14 | * | |
| 15 | * This program is distributed in the hope that it will be useful, | |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 | * GNU General Public License for more details. | |
| 19 | * | |
| 20 | * You should have received a copy of the GNU General Public License | |
| 21 | * along with this program; if not, write to the Free Software | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 13201 | 23 | */ |
| 24 | #include "internal.h" | |
| 25 | ||
|
14013
86dac5633bd9
[gaim-migrate @ 16496]
Mark Doliner <markdoliner@pidgin.im>
parents:
13830
diff
changeset
|
26 | #include "circbuffer.h" |
| 13201 | 27 | |
| 28 | #define DEFAULT_BUF_SIZE 256 | |
| 29 | ||
| 15884 | 30 | PurpleCircBuffer * |
| 31 | purple_circ_buffer_new(gsize growsize) { | |
| 32 | PurpleCircBuffer *buf = g_new0(PurpleCircBuffer, 1); | |
| 13201 | 33 | buf->growsize = growsize ? growsize : DEFAULT_BUF_SIZE; |
| 34 | return buf; | |
| 35 | } | |
| 36 | ||
| 15884 | 37 | void purple_circ_buffer_destroy(PurpleCircBuffer *buf) { |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
38 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
39 | |
| 13201 | 40 | g_free(buf->buffer); |
| 41 | g_free(buf); | |
| 42 | } | |
| 43 | ||
| 15884 | 44 | static void grow_circ_buffer(PurpleCircBuffer *buf, gsize len) { |
| 13201 | 45 | int in_offset = 0, out_offset = 0; |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
46 | int start_buflen; |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
24505
diff
changeset
|
47 | |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
48 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
49 | |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
50 | start_buflen = buf->buflen; |
| 13201 | 51 | |
| 52 | while ((buf->buflen - buf->bufused) < len) | |
| 53 | buf->buflen += buf->growsize; | |
| 54 | ||
| 55 | if (buf->inptr != NULL) { | |
| 56 | in_offset = buf->inptr - buf->buffer; | |
| 57 | out_offset = buf->outptr - buf->buffer; | |
| 58 | } | |
| 59 | buf->buffer = g_realloc(buf->buffer, buf->buflen); | |
| 60 | ||
| 61 | /* adjust the fill and remove pointer locations */ | |
| 62 | if (buf->inptr == NULL) { | |
| 63 | buf->inptr = buf->outptr = buf->buffer; | |
| 64 | } else { | |
| 65 | buf->inptr = buf->buffer + in_offset; | |
| 66 | buf->outptr = buf->buffer + out_offset; | |
| 67 | } | |
| 68 | ||
| 69 | /* If the fill pointer is wrapped to before the remove | |
| 70 | * pointer, we need to shift the data */ | |
|
25731
b9fd4a560a72
Backport 7829ec76bdb008583f8da54e238c2265a1140db2 (#8706 - Corruption in PurpleCircBuffer)
Daniel Atallah <datallah@pidgin.im>
parents:
24505
diff
changeset
|
71 | if (in_offset < out_offset |
|
b9fd4a560a72
Backport 7829ec76bdb008583f8da54e238c2265a1140db2 (#8706 - Corruption in PurpleCircBuffer)
Daniel Atallah <datallah@pidgin.im>
parents:
24505
diff
changeset
|
72 | || (in_offset == out_offset && buf->bufused > 0)) { |
| 13201 | 73 | int shift_n = MIN(buf->buflen - start_buflen, |
| 74 | in_offset); | |
| 75 | memcpy(buf->buffer + start_buflen, buf->buffer, | |
| 76 | shift_n); | |
| 77 | ||
| 78 | /* If we couldn't fit the wrapped read buffer | |
| 79 | * at the end */ | |
| 80 | if (shift_n < in_offset) { | |
|
13210
15dcfe11860e
[gaim-migrate @ 15573]
Daniel Atallah <datallah@pidgin.im>
parents:
13201
diff
changeset
|
81 | memmove(buf->buffer, |
| 13201 | 82 | buf->buffer + shift_n, |
| 83 | in_offset - shift_n); | |
| 84 | buf->inptr = buf->buffer + | |
| 85 | (in_offset - shift_n); | |
| 86 | } else { | |
| 87 | buf->inptr = buf->buffer + | |
| 88 | start_buflen + in_offset; | |
| 89 | } | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 15884 | 93 | void purple_circ_buffer_append(PurpleCircBuffer *buf, gconstpointer src, gsize len) { |
| 13201 | 94 | |
| 95 | int len_stored; | |
| 96 | ||
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
97 | g_return_if_fail(buf != NULL); |
|
25888
d0fdd378a635
Remove trailing whitespace
Mark Doliner <markdoliner@pidgin.im>
parents:
24505
diff
changeset
|
98 | |
| 13201 | 99 | /* Grow the buffer, if necessary */ |
| 100 | if ((buf->buflen - buf->bufused) < len) | |
| 101 | grow_circ_buffer(buf, len); | |
| 102 | ||
|
13213
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
103 | /* If there is not enough room to copy all of src before hitting |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
104 | * the end of the buffer then we will need to do two copies. |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
105 | * One copy from inptr to the end of the buffer, and the |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
106 | * second copy from the start of the buffer to the end of src. */ |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
107 | if (buf->inptr >= buf->outptr) |
| 13201 | 108 | len_stored = MIN(len, buf->buflen |
| 109 | - (buf->inptr - buf->buffer)); | |
| 110 | else | |
| 111 | len_stored = len; | |
| 112 | ||
|
24505
6199bafdbc87
Clean up some unnecessary and unused code in the purple_circ_buffer_append()
Daniel Atallah <datallah@pidgin.im>
parents:
20147
diff
changeset
|
113 | if (len_stored > 0) |
|
6199bafdbc87
Clean up some unnecessary and unused code in the purple_circ_buffer_append()
Daniel Atallah <datallah@pidgin.im>
parents:
20147
diff
changeset
|
114 | memcpy(buf->inptr, src, len_stored); |
| 13201 | 115 | |
| 116 | if (len_stored < len) { | |
|
15017
2264f54d2342
[gaim-migrate @ 17734]
Luke Schierer <lschiere@pidgin.im>
parents:
14254
diff
changeset
|
117 | memcpy(buf->buffer, (char*)src + len_stored, len - len_stored); |
| 13201 | 118 | buf->inptr = buf->buffer + (len - len_stored); |
| 119 | } else { | |
| 120 | buf->inptr += len_stored; | |
| 121 | } | |
| 122 | ||
| 123 | buf->bufused += len; | |
| 124 | } | |
| 125 | ||
| 15884 | 126 | gsize purple_circ_buffer_get_max_read(const PurpleCircBuffer *buf) { |
|
15609
e432251bd57e
sf patch #1647731, from Markus Elfring
Mark Doliner <markdoliner@pidgin.im>
parents:
15435
diff
changeset
|
127 | gsize max_read; |
| 13201 | 128 | |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
129 | g_return_val_if_fail(buf != NULL, 0); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
130 | |
| 13201 | 131 | if (buf->bufused == 0) |
| 132 | max_read = 0; | |
| 133 | else if ((buf->outptr - buf->inptr) >= 0) | |
| 134 | max_read = buf->buflen - (buf->outptr - buf->buffer); | |
| 135 | else | |
| 136 | max_read = buf->inptr - buf->outptr; | |
| 137 | ||
| 138 | return max_read; | |
| 139 | } | |
| 140 | ||
| 15884 | 141 | gboolean purple_circ_buffer_mark_read(PurpleCircBuffer *buf, gsize len) { |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
142 | g_return_val_if_fail(buf != NULL, FALSE); |
| 15884 | 143 | g_return_val_if_fail(purple_circ_buffer_get_max_read(buf) >= len, FALSE); |
| 13201 | 144 | |
| 145 | buf->outptr += len; | |
| 146 | buf->bufused -= len; | |
| 147 | /* wrap to the start if we're at the end */ | |
| 148 | if ((buf->outptr - buf->buffer) == buf->buflen) | |
| 149 | buf->outptr = buf->buffer; | |
| 150 | ||
| 151 | return TRUE; | |
| 152 | } | |
| 153 |