Use a recursive depth-first shrinkage function, limit handles accordingly

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15414 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Brandon Low 2007-11-03 02:54:34 +00:00
parent 9a114614d5
commit a042c720c3
2 changed files with 16 additions and 17 deletions

View file

@ -230,6 +230,9 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
size_t len;
int overlap;
if (num_handles >= BUF_MAX_HANDLES)
return NULL;
mutex_lock(&llist_mutex);
if (cur_handle && cur_handle->filerem > 0) {
@ -508,7 +511,6 @@ reset_handle : Reset writing position and data buffer of a handle to its
rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
shrink_handle : Free buffer space by moving a handle
fill_buffer : Call buffer_handle for all handles that have data to buffer
can_add_handle : Indicate whether it's safe to add a handle
These functions are used by the buffering thread to manage buffer space.
*/
@ -684,10 +686,9 @@ static bool close_handle(int handle_id)
/* Free buffer space by moving the handle struct right before the useful
part of its data buffer or by moving all the data. */
static void shrink_handle(int handle_id)
static void shrink_handle(struct memory_handle *h)
{
size_t delta;
struct memory_handle *h = find_handle(handle_id);
if (!h)
return;
@ -1152,15 +1153,14 @@ static void call_buffer_low_callbacks(void)
}
}
static void shrink_buffer(bool audio, bool other) {
/* shrink selected buffers */
struct memory_handle *m = first_handle;
while (m) {
if ((m->type==TYPE_PACKET_AUDIO && audio) ||
(m->type!=TYPE_PACKET_AUDIO && other))
shrink_handle(m->id);
m = m->next;
}
static void shrink_buffer(struct memory_handle *h) {
if (h == NULL)
return;
shrink_buffer(h->next);
shrink_handle(h);
}
void buffering_thread(void)
@ -1255,12 +1255,9 @@ void buffering_thread(void)
if (data_counters.remaining > 0 &&
data_counters.useful < conf_watermark)
{
/* First work forward, shrinking any unmoveable handles */
shrink_buffer(true,false);
/* Then work forward following those up with moveable handles */
shrink_buffer(false,true);
/* Recursively shrink the buffer, depth first */
shrink_buffer(first_handle);
fill_buffer();
update_data_counters();
}
}
}

View file

@ -65,6 +65,8 @@ bool buffering_reset(char *buf, size_t buflen);
* amount of data is ready (unless EOF is reached).
****************************************************************************/
#define BUF_MAX_HANDLES 256
int bufopen(const char *file, size_t offset, enum data_type type);
int bufalloc(const void *src, size_t size, enum data_type type);
bool bufclose(int handle_id);