fifo_buffer: add function fifo_buffer_realloc()
For growing FIFO buffers.
This commit is contained in:
parent
a85af593f1
commit
f546849352
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
||||
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -58,6 +58,39 @@ fifo_buffer_new(size_t size)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static void
|
||||
fifo_buffer_move(struct fifo_buffer *buffer);
|
||||
|
||||
struct fifo_buffer *
|
||||
fifo_buffer_realloc(struct fifo_buffer *buffer, size_t new_size)
|
||||
{
|
||||
if (buffer == NULL)
|
||||
return new_size > 0
|
||||
? fifo_buffer_new(new_size)
|
||||
: NULL;
|
||||
|
||||
/* existing data must fit in new size */
|
||||
assert(new_size >= buffer->end - buffer->start);
|
||||
|
||||
if (new_size == 0) {
|
||||
fifo_buffer_free(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* compress the buffer when we're shrinking and the tail of
|
||||
the buffer would exceed the new size */
|
||||
if (buffer->end > new_size)
|
||||
fifo_buffer_move(buffer);
|
||||
|
||||
/* existing data must fit in new size: second check */
|
||||
assert(buffer->end <= new_size);
|
||||
|
||||
buffer = g_realloc(buffer, sizeof(*buffer) - sizeof(buffer->buffer) +
|
||||
new_size);
|
||||
buffer->size = new_size;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void
|
||||
fifo_buffer_free(struct fifo_buffer *buffer)
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
||||
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -56,6 +56,18 @@ struct fifo_buffer;
|
||||
struct fifo_buffer *
|
||||
fifo_buffer_new(size_t size);
|
||||
|
||||
/**
|
||||
* Change the capacity of the #fifo_buffer, while preserving existing
|
||||
* data.
|
||||
*
|
||||
* @param buffer the old buffer, may be NULL
|
||||
* @param new_size the requested new size of the #fifo_buffer; must
|
||||
* not be smaller than the data which is stored in the old buffer
|
||||
* @return the new buffer, may be NULL if the requested new size is 0
|
||||
*/
|
||||
struct fifo_buffer *
|
||||
fifo_buffer_realloc(struct fifo_buffer *buffer, size_t new_size);
|
||||
|
||||
/**
|
||||
* Frees the resources consumed by this #fifo_buffer object.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user