Add editor-modelines; adjust whitespace.
[metze/wireshark/wip.git] / wsutil / buffer.c
1 /* buffer.c
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27
28 #include "buffer.h"
29
30 /* Initializes a buffer with a certain amount of allocated space */
31 void
32 ws_buffer_init(Buffer* buffer, gsize space)
33 {
34         buffer->data = (guint8*)g_malloc(space);
35         buffer->allocated = space;
36         buffer->start = 0;
37         buffer->first_free = 0;
38 }
39
40 /* Frees the memory used by a buffer */
41 void
42 ws_buffer_free(Buffer* buffer)
43 {
44         g_free(buffer->data);
45         buffer->data = NULL;
46 }
47
48 /* Assures that there are 'space' bytes at the end of the used space
49         so that another routine can copy directly into the buffer space. After
50         doing that, the routine will also want to run
51         ws_buffer_increase_length(). */
52 void
53 ws_buffer_assure_space(Buffer* buffer, gsize space)
54 {
55         gsize available_at_end = buffer->allocated - buffer->first_free;
56         gsize space_used;
57         gboolean space_at_beginning;
58
59         /* If we've got the space already, good! */
60         if (space <= available_at_end) {
61                 return;
62         }
63
64         /* Maybe we don't have the space available at the end, but we would
65                 if we moved the used space back to the beginning of the
66                 allocation. The buffer could have become fragmented through lots
67                 of calls to ws_buffer_remove_start(). I'm using buffer->start as the
68                 same as 'available_at_start' in this comparison. */
69
70         /* or maybe there's just no more room. */
71
72         space_at_beginning = buffer->start >= space;
73         if (space_at_beginning || buffer->start > 0) {
74                 space_used = buffer->first_free - buffer->start;
75                 /* this memory copy better be safe for overlapping memory regions! */
76                 memmove(buffer->data, buffer->data + buffer->start, space_used);
77                 buffer->start = 0;
78                 buffer->first_free = space_used;
79         }
80         /*if (buffer->start >= space) {*/
81         if (space_at_beginning) {
82                 return;
83         }
84
85         /* We'll allocate more space */
86         buffer->allocated += space + 1024;
87         buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
88 }
89
90 void
91 ws_buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
92 {
93         ws_buffer_assure_space(buffer, bytes);
94         memcpy(buffer->data + buffer->first_free, from, bytes);
95         buffer->first_free += bytes;
96 }
97
98 void
99 ws_buffer_remove_start(Buffer* buffer, gsize bytes)
100 {
101         if (buffer->start + bytes > buffer->first_free) {
102                 g_error("ws_buffer_remove_start trying to remove %" G_GINT64_MODIFIER "u bytes. s=%" G_GINT64_MODIFIER "u ff=%" G_GINT64_MODIFIER "u!\n",
103                         (guint64)bytes, (guint64)buffer->start,
104                         (guint64)buffer->first_free);
105                 /** g_error() does an abort() and thus never returns **/
106         }
107         buffer->start += bytes;
108
109         if (buffer->start == buffer->first_free) {
110                 buffer->start = 0;
111                 buffer->first_free = 0;
112         }
113 }
114
115
116 #ifndef SOME_FUNCTIONS_ARE_DEFINES
117 void
118 ws_buffer_clean(Buffer* buffer)
119 {
120         ws_buffer_remove_start(buffer, ws_buffer_length(buffer));
121 }
122 #endif
123
124 #ifndef SOME_FUNCTIONS_ARE_DEFINES
125 void
126 ws_buffer_increase_length(Buffer* buffer, gsize bytes)
127 {
128         buffer->first_free += bytes;
129 }
130 #endif
131
132 #ifndef SOME_FUNCTIONS_ARE_DEFINES
133 gsize
134 ws_buffer_length(Buffer* buffer)
135 {
136         return buffer->first_free - buffer->start;
137 }
138 #endif
139
140 #ifndef SOME_FUNCTIONS_ARE_DEFINES
141 guint8 *
142 ws_buffer_start_ptr(Buffer* buffer)
143 {
144         return buffer->data + buffer->start;
145 }
146 #endif
147
148 #ifndef SOME_FUNCTIONS_ARE_DEFINES
149 guint8 *
150 ws_buffer_end_ptr(Buffer* buffer)
151 {
152         return buffer->data + buffer->first_free;
153 }
154 #endif
155
156 #ifndef SOME_FUNCTIONS_ARE_DEFINES
157 void
158 ws_buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
159 {
160         ws_buffer_append(buffer, ws_buffer_start_ptr(src_buffer), ws_buffer_length(src_buffer));
161 }
162 #endif
163
164 /*
165  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
166  *
167  * Local variables:
168  * c-basic-offset: 8
169  * tab-width: 8
170  * indent-tabs-mode: t
171  * End:
172  *
173  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
174  * :indentSize=8:tabSize=8:noTabs=false:
175  */