6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 /* Initializes a buffer with a certain amount of allocated space */
34 buffer_init(Buffer
* buffer
, gsize space
)
36 buffer
->data
= (guint8
*)g_malloc(space
);
37 buffer
->allocated
= space
;
39 buffer
->first_free
= 0;
42 /* Frees the memory used by a buffer, and the buffer struct */
44 buffer_free(Buffer
* buffer
)
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(). */
54 buffer_assure_space(Buffer
* buffer
, gsize space
)
56 gsize available_at_end
= buffer
->allocated
- buffer
->first_free
;
58 gboolean space_at_beginning
;
60 /* If we've got the space already, good! */
61 if (space
<= available_at_end
) {
65 /* Maybe we don't have the space available at the end, but we would
66 if we moved the used space back to the beginning of the
67 allocation. The buffer could have become fragmented through lots
68 of calls to buffer_remove_start(). I'm using buffer->start as the
69 same as 'available_at_start' in this comparison. */
71 /* or maybe there's just no more room. */
73 space_at_beginning
= buffer
->start
>= space
;
74 if (space_at_beginning
|| buffer
->start
> 0) {
75 space_used
= buffer
->first_free
- buffer
->start
;
76 /* this memory copy better be safe for overlapping memory regions! */
77 memmove(buffer
->data
, buffer
->data
+ buffer
->start
, space_used
);
79 buffer
->first_free
= space_used
;
81 /*if (buffer->start >= space) {*/
82 if (space_at_beginning
) {
86 /* We'll allocate more space */
87 buffer
->allocated
+= space
+ 1024;
88 buffer
->data
= (guint8
*)g_realloc(buffer
->data
, buffer
->allocated
);
92 buffer_append(Buffer
* buffer
, guint8
*from
, gsize bytes
)
94 buffer_assure_space(buffer
, bytes
);
95 memcpy(buffer
->data
+ buffer
->first_free
, from
, bytes
);
96 buffer
->first_free
+= bytes
;
100 buffer_remove_start(Buffer
* buffer
, gsize bytes
)
102 if (buffer
->start
+ bytes
> buffer
->first_free
) {
103 g_error("buffer_remove_start trying to remove %" G_GINT64_MODIFIER
"u bytes. s=%" G_GINT64_MODIFIER
"u ff=%" G_GINT64_MODIFIER
"u!\n",
104 (guint64
)bytes
, (guint64
)buffer
->start
,
105 (guint64
)buffer
->first_free
);
106 /** g_error() does an abort() and thus never returns **/
108 buffer
->start
+= bytes
;
110 if (buffer
->start
== buffer
->first_free
) {
112 buffer
->first_free
= 0;
117 #ifndef SOME_FUNCTIONS_ARE_DEFINES
119 buffer_clean(Buffer
* buffer
)
121 buffer_remove_start(buffer
, buffer_length(buffer
));
125 #ifndef SOME_FUNCTIONS_ARE_DEFINES
127 buffer_increase_length(Buffer
* buffer
, gsize bytes
)
129 buffer
->first_free
+= bytes
;
133 #ifndef SOME_FUNCTIONS_ARE_DEFINES
135 buffer_length(Buffer
* buffer
)
137 return buffer
->first_free
- buffer
->start
;
141 #ifndef SOME_FUNCTIONS_ARE_DEFINES
143 buffer_start_ptr(Buffer
* buffer
)
145 return buffer
->data
+ buffer
->start
;
149 #ifndef SOME_FUNCTIONS_ARE_DEFINES
151 buffer_end_ptr(Buffer
* buffer
)
153 return buffer
->data
+ buffer
->first_free
;
157 #ifndef SOME_FUNCTIONS_ARE_DEFINES
159 buffer_append_buffer(Buffer
* buffer
, Buffer
* src_buffer
)
161 buffer_append(buffer
, buffer_start_ptr(src_buffer
), buffer_length(src_buffer
));