Detect clang and llvm-gcc.
[obnox/wireshark/wip.git] / wiretap / buffer.c
1 /* buffer.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <glib.h>
31
32 #include "buffer.h"
33
34 /* Initializes a buffer with a certain amount of allocated space */
35 void buffer_init(Buffer* buffer, gsize space)
36 {
37         buffer->data = (guchar*)g_malloc(space);
38         buffer->allocated = space;
39         buffer->start = 0;
40         buffer->first_free = 0;
41 }
42
43 /* Frees the memory used by a buffer, and the buffer struct */
44 void buffer_free(Buffer* buffer)
45 {
46         g_free(buffer->data);
47 }
48
49 /* Assures that there are 'space' bytes at the end of the used space
50         so that another routine can copy directly into the buffer space. After
51         doing that, the routine will also want to run
52         buffer_increase_length(). */
53 void 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 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 = (guchar*)g_realloc(buffer->data, buffer->allocated);
88 }
89
90 void buffer_append(Buffer* buffer, guchar *from, gsize bytes)
91 {
92         buffer_assure_space(buffer, bytes);
93         memcpy(buffer->data + buffer->first_free, from, bytes);
94         buffer->first_free += bytes;
95 }
96
97 void buffer_remove_start(Buffer* buffer, gsize bytes)
98 {
99         if (buffer->start + bytes > buffer->first_free) {
100                 g_error("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                 exit(1);
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 buffer_clean(Buffer* buffer)
116 {
117         buffer_remove_start(buffer, buffer_length(buffer));
118 }
119 #endif
120
121 #ifndef SOME_FUNCTIONS_ARE_DEFINES
122 void buffer_increase_length(Buffer* buffer, gsize bytes)
123 {
124         buffer->first_free += bytes;
125 }
126 #endif
127
128 #ifndef SOME_FUNCTIONS_ARE_DEFINES
129 gsize buffer_length(Buffer* buffer)
130 {
131         return buffer->first_free - buffer->start;
132 }
133 #endif
134
135 #ifndef SOME_FUNCTIONS_ARE_DEFINES
136 guchar* buffer_start_ptr(Buffer* buffer)
137 {
138         return buffer->data + buffer->start;
139 }
140 #endif
141
142 #ifndef SOME_FUNCTIONS_ARE_DEFINES
143 guchar* buffer_end_ptr(Buffer* buffer)
144 {
145         return buffer->data + buffer->first_free;
146 }
147 #endif
148
149 #ifndef SOME_FUNCTIONS_ARE_DEFINES
150 void buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
151 {
152         buffer_append(buffer, buffer_start_ptr(src_buffer), buffer_length(src_buffer));
153 }
154 #endif