2
* Twisted, the Framework of Your Internet
3
* Copyright (C) 2001-2002 Matthew W. Lefkowitz
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of version 2.1 of the GNU Lesser General Public
7
* License as published by the Free Software Foundation.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
/* cReactorBuffer.c - a simple read/write buffer. */
24
struct _cReactorBuffer
26
unsigned char * memory;
27
unsigned int memory_size;
28
unsigned char * read_ptr;
29
unsigned char * write_ptr;
34
cReactorBuffer_New(unsigned int size)
38
buf = (cReactorBuffer *)malloc(sizeof(cReactorBuffer));
39
buf->memory = (unsigned char *)malloc(size);
40
buf->memory_size = size;
41
buf->read_ptr = buf->memory;
42
buf->write_ptr = buf->memory;
49
cReactorBuffer_Destroy(cReactorBuffer *buffer)
60
cReactorBuffer_Write(cReactorBuffer *buffer, const void *data, unsigned int size)
64
unsigned int pre_read;
65
unsigned int new_size;
66
unsigned char *new_mem;
68
/* Determine how much is used. */
69
used = buffer->write_ptr - buffer->read_ptr;
71
/* Determine how much space is currently available. */
72
avail = (buffer->memory + buffer->memory_size) - buffer->write_ptr;
74
/* Check if we do not have enough space to write. */
77
/* If there is enough space between the start of the memory block and the
78
* read pointer we can slide the buffer back towards the memory block
81
pre_read = buffer->read_ptr - buffer->memory;
82
if ((avail + pre_read) >= size)
84
/* Sliding will give us the space. */
85
memmove(buffer->memory, buffer->read_ptr, used);
86
buffer->read_ptr = buffer->memory;
87
buffer->write_ptr -= pre_read;
91
/* We have to allocate a new buffer. */
92
new_size = (buffer->memory_size * 2) + size;
93
new_mem = (unsigned char *)malloc(new_size);
94
memcpy(new_mem, buffer->read_ptr, used);
96
buffer->write_ptr = new_mem + used;
97
buffer->read_ptr = new_mem;
98
buffer->memory_size = new_size;
100
free(buffer->memory);
101
buffer->memory = new_mem;
106
memcpy(buffer->write_ptr, data, size);
107
buffer->write_ptr += size;
113
cReactorBuffer_DataAvailable(cReactorBuffer *buffer)
115
/* Allow NULL buffers. */
117
? (buffer->write_ptr - buffer->read_ptr)
122
const unsigned char *
123
cReactorBuffer_GetPtr(cReactorBuffer *buffer)
125
return buffer->read_ptr;
129
cReactorBuffer_Seek(cReactorBuffer *buffer, unsigned int forward)
133
avail = (buffer->write_ptr - buffer->read_ptr);
134
if (forward >= avail)
136
buffer->write_ptr = buffer->memory;
137
buffer->read_ptr = buffer->memory;
141
buffer->read_ptr += forward;
145
/* vim: set sts=4 sw=4: */