Sun, 15 Apr 2007 02:10:37 +0000
propagate from branch 'im.pidgin.gaim' (head b2836a24d81e7a1bd1d21b3aea8794b094391344)
to branch 'im.pidgin.rlaager.merging.soc-msnp13-to-svn18164' (head 463b4fa9f067b279f843520d95a822adc86a0a1b)
| 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 |
| 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 | ||
|
14013
86dac5633bd9
[gaim-migrate @ 16496]
Mark Doliner <markdoliner@pidgin.im>
parents:
13830
diff
changeset
|
25 | #include "circbuffer.h" |
| 13201 | 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) { | |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
37 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
38 | |
| 13201 | 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; | |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
45 | int start_buflen; |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
46 | |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
47 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
48 | |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
49 | start_buflen = buf->buflen; |
| 13201 | 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) { | |
|
13210
15dcfe11860e
[gaim-migrate @ 15573]
Daniel Atallah <datallah@pidgin.im>
parents:
13201
diff
changeset
|
79 | memmove(buf->buffer, |
| 13201 | 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 | ||
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
95 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
96 | |
| 13201 | 97 | /* Grow the buffer, if necessary */ |
| 98 | if ((buf->buflen - buf->bufused) < len) | |
| 99 | grow_circ_buffer(buf, len); | |
| 100 | ||
|
13213
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
101 | /* 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
|
102 | * 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
|
103 | * 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
|
104 | * 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
|
105 | if (buf->inptr >= buf->outptr) |
| 13201 | 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) { | |
|
15017
2264f54d2342
[gaim-migrate @ 17734]
Luke Schierer <lschiere@pidgin.im>
parents:
14254
diff
changeset
|
114 | memcpy(buf->buffer, (char*)src + len_stored, len - len_stored); |
| 13201 | 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 | ||
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
128 | g_return_val_if_fail(buf != NULL, 0); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
129 | |
| 13201 | 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) { | |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
141 | g_return_val_if_fail(buf != NULL, FALSE); |
| 13201 | 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 |