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