3 * $Id: buffer.c,v 1.2 1998/11/12 06:01:18 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.
31 #define DEBUG_PROGRAM_NAME "buffer.c"
34 /* Initializes a buffer with a certain amount of allocated space */
35 void buffer_init(Buffer* buffer, unsigned int space)
37 debug("buffer_init\n");
38 buffer->data = (char*)g_malloc(space);
39 buffer->allocated = space;
41 buffer->first_free = 0;
44 /* Frees the memory used by a buffer, and the buffer struct */
45 void buffer_free(Buffer* buffer)
47 debug("buffer_free\n");
51 /* Assures that there are 'space' bytes at the end of the used space
52 so that another routine can copy directly into the buffer space. After
53 doing that, the routine will also want to run
54 buffer_increase_length(). */
55 void buffer_assure_space(Buffer* buffer, unsigned int space)
57 unsigned int available_at_end = buffer->allocated - buffer->first_free;
58 unsigned int space_used;
59 int space_at_beginning;
61 debug("buffer_assure_space %d bytes\n", space);
62 /* If we've got the space already, good! */
63 if (space <= available_at_end) {
67 /* Maybe we don't have the space available at the end, but we would
68 if we moved the used space back to the beginning of the
69 allocation. The buffer could have become fragmented through lots
70 of calls to buffer_remove_start(). I'm using buffer->start as the
71 same as 'available_at_start' in this comparison. */
73 /* or maybe there's just no more room. */
75 space_at_beginning = buffer->start >= space;
76 if (space_at_beginning || buffer->start > 0) {
77 space_used = buffer->first_free - buffer->start;
78 /* this memory copy better be safe for overlapping memory regions! */
79 memmove(buffer->data, buffer->data + buffer->start, space_used);
81 buffer->first_free = space_used;
83 /*if (buffer->start >= space) {*/
84 if (space_at_beginning) {
88 /* We'll allocate more space */
89 buffer->allocated += space + 1024;
90 buffer->data = (char*)g_realloc(buffer->data, buffer->allocated);
93 void buffer_append(Buffer* buffer, char *from, unsigned int bytes)
95 debug("buffer_append %d bytes\n", bytes);
96 buffer_assure_space(buffer, bytes);
97 memcpy(buffer->data + buffer->first_free, from, bytes);
98 buffer->first_free += bytes;
101 void buffer_remove_start(Buffer* buffer, unsigned int bytes)
103 debug("buffer_remove_start %d bytes\n", bytes);
104 if (buffer->start + bytes > buffer->first_free) {
105 die("buffer_remove_start trying to remove %d bytes. s=%d ff=%d!\n",
106 bytes, buffer->start, buffer->first_free);
108 buffer->start += bytes;
110 if (buffer->start == buffer->first_free) {
112 buffer->first_free = 0;
117 #ifndef SOME_FUNCTIONS_ARE_DEFINES
118 void buffer_increase_length(Buffer* buffer, unsigned int bytes)
120 debug("buffer_increase_length %d bytes\n", bytes);
121 buffer->first_free += bytes;
125 #ifndef SOME_FUNCTIONS_ARE_DEFINES
126 unsigned int buffer_length(Buffer* buffer)
128 debug("buffer_length\n");
129 return buffer->first_free - buffer->start;
133 #ifndef SOME_FUNCTIONS_ARE_DEFINES
134 char* buffer_start_ptr(Buffer* buffer)
136 debug("buffer_start_ptr\n");
137 return buffer->data + buffer->start;
141 #ifndef SOME_FUNCTIONS_ARE_DEFINES
142 char* buffer_end_ptr(Buffer* buffer)
144 debug("buffer_end_ptr\n");
145 return buffer->data + buffer->first_free;