3 * $Id: buffer.c,v 1.3 1998/11/12 23:29:33 gram Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #define DEBUG_PROGRAM_NAME "buffer.c"
36 /* Initializes a buffer with a certain amount of allocated space */
37 void buffer_init(Buffer* buffer, unsigned int space)
39 debug("buffer_init\n");
40 buffer->data = (char*)g_malloc(space);
41 buffer->allocated = space;
43 buffer->first_free = 0;
46 /* Frees the memory used by a buffer, and the buffer struct */
47 void buffer_free(Buffer* buffer)
49 debug("buffer_free\n");
53 /* Assures that there are 'space' bytes at the end of the used space
54 so that another routine can copy directly into the buffer space. After
55 doing that, the routine will also want to run
56 buffer_increase_length(). */
57 void buffer_assure_space(Buffer* buffer, unsigned int space)
59 unsigned int available_at_end = buffer->allocated - buffer->first_free;
60 unsigned int space_used;
61 int space_at_beginning;
63 debug("buffer_assure_space %d bytes\n", space);
64 /* If we've got the space already, good! */
65 if (space <= available_at_end) {
69 /* Maybe we don't have the space available at the end, but we would
70 if we moved the used space back to the beginning of the
71 allocation. The buffer could have become fragmented through lots
72 of calls to buffer_remove_start(). I'm using buffer->start as the
73 same as 'available_at_start' in this comparison. */
75 /* or maybe there's just no more room. */
77 space_at_beginning = buffer->start >= space;
78 if (space_at_beginning || buffer->start > 0) {
79 space_used = buffer->first_free - buffer->start;
80 /* this memory copy better be safe for overlapping memory regions! */
81 memmove(buffer->data, buffer->data + buffer->start, space_used);
83 buffer->first_free = space_used;
85 /*if (buffer->start >= space) {*/
86 if (space_at_beginning) {
90 /* We'll allocate more space */
91 buffer->allocated += space + 1024;
92 buffer->data = (char*)g_realloc(buffer->data, buffer->allocated);
95 void buffer_append(Buffer* buffer, char *from, unsigned int bytes)
97 debug("buffer_append %d bytes\n", bytes);
98 buffer_assure_space(buffer, bytes);
99 memcpy(buffer->data + buffer->first_free, from, bytes);
100 buffer->first_free += bytes;
103 void buffer_remove_start(Buffer* buffer, unsigned int bytes)
105 debug("buffer_remove_start %d bytes\n", bytes);
106 if (buffer->start + bytes > buffer->first_free) {
107 die("buffer_remove_start trying to remove %d bytes. s=%d ff=%d!\n",
108 bytes, buffer->start, buffer->first_free);
110 buffer->start += bytes;
112 if (buffer->start == buffer->first_free) {
114 buffer->first_free = 0;
119 #ifndef SOME_FUNCTIONS_ARE_DEFINES
120 void buffer_increase_length(Buffer* buffer, unsigned int bytes)
122 debug("buffer_increase_length %d bytes\n", bytes);
123 buffer->first_free += bytes;
127 #ifndef SOME_FUNCTIONS_ARE_DEFINES
128 unsigned int buffer_length(Buffer* buffer)
130 debug("buffer_length\n");
131 return buffer->first_free - buffer->start;
135 #ifndef SOME_FUNCTIONS_ARE_DEFINES
136 char* buffer_start_ptr(Buffer* buffer)
138 debug("buffer_start_ptr\n");
139 return buffer->data + buffer->start;
143 #ifndef SOME_FUNCTIONS_ARE_DEFINES
144 char* buffer_end_ptr(Buffer* buffer)
146 debug("buffer_end_ptr\n");
147 return buffer->data + buffer->first_free;