Reduce packetstream allocation frequency.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3825 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
parent
e59eb66c1d
commit
8f35004a01
@ -12,6 +12,7 @@ typedef struct _tf_packetstream_t {
|
||||
uv_pipe_t stream;
|
||||
char* buffer;
|
||||
size_t buffer_size;
|
||||
size_t buffer_capacity;
|
||||
bool destroyed;
|
||||
} tf_packetstream_t;
|
||||
|
||||
@ -27,6 +28,11 @@ void tf_packetstream_destroy(tf_packetstream_t* stream)
|
||||
stream->onreceive = NULL;
|
||||
stream->onreceive_user_data = NULL;
|
||||
stream->destroyed = true;
|
||||
if (stream->buffer)
|
||||
{
|
||||
free(stream->buffer);
|
||||
stream->buffer = NULL;
|
||||
}
|
||||
if (stream->stream.data)
|
||||
{
|
||||
tf_packetstream_close(stream);
|
||||
@ -61,7 +67,6 @@ static void _packetstream_process_messages(tf_packetstream_t* stream)
|
||||
size_t consumed = sizeof(length) + sizeof(packet_type) + length;
|
||||
memmove(stream->buffer, stream->buffer + consumed, stream->buffer_size - consumed);
|
||||
stream->buffer_size -= consumed;
|
||||
stream->buffer = realloc(stream->buffer, stream->buffer_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -77,11 +82,23 @@ static void _packetstream_on_read(uv_stream_t* handle, ssize_t count, const uv_b
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
char* new_buffer = realloc(stream->buffer, stream->buffer_size + count);
|
||||
if (new_buffer)
|
||||
char* write_buffer = stream->buffer;
|
||||
if (stream->buffer_size + count > stream->buffer_capacity)
|
||||
{
|
||||
memcpy(new_buffer + stream->buffer_size, buffer->base, count);
|
||||
stream->buffer = new_buffer;
|
||||
if (!stream->buffer_capacity)
|
||||
{
|
||||
stream->buffer_capacity = 256;
|
||||
}
|
||||
while (stream->buffer_capacity < stream->buffer_size + count)
|
||||
{
|
||||
stream->buffer_capacity *= 2;
|
||||
}
|
||||
write_buffer = realloc(write_buffer, stream->buffer_capacity);
|
||||
}
|
||||
if (write_buffer)
|
||||
{
|
||||
memcpy(write_buffer + stream->buffer_size, buffer->base, count);
|
||||
stream->buffer = write_buffer;
|
||||
stream->buffer_size += count;
|
||||
}
|
||||
_packetstream_process_messages(stream);
|
||||
@ -133,6 +150,11 @@ static void _tf_packetstream_handle_closed(uv_handle_t* handle)
|
||||
{
|
||||
tf_packetstream_t* packetstream = handle->data;
|
||||
handle->data = NULL;
|
||||
if (packetstream->buffer)
|
||||
{
|
||||
free(packetstream->buffer);
|
||||
packetstream->buffer = NULL;
|
||||
}
|
||||
if (packetstream->destroyed)
|
||||
{
|
||||
free(packetstream);
|
||||
|
Loading…
Reference in New Issue
Block a user