From: Nicolas Pennequin Date: Tue, 7 Aug 2007 20:07:23 +0000 (+0200) Subject: bufopen: Factor out the pre-checking. X-Git-Tag: working_mob_2~2 X-Git-Url: https://repo.or.cz/w/Rockbox-MoB.git/commitdiff_plain/dc66c7e0f5aab33aff79538afbb0f20484e4b7c4 bufopen: Factor out the pre-checking. Factored out the code that checks there is enough space to finish buffering the current handle. It will be used again in the next commit. --- diff --git a/testplugin.c b/testplugin.c index 3cc2db0..cf9e44f 100644 --- a/testplugin.c +++ b/testplugin.c @@ -299,6 +299,7 @@ BUFFER SPACE MANAGEMENT buffer_handle : Buffer data for a handle free_buffer : 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. data_rem : Total amount of data needing to be buffered wasted_space : Total amount of space available for freeing @@ -433,6 +434,29 @@ static void fill_buffer(void) } } +/* Check whether it's safe to add a new handle and reserve space to let the + current one finish buffering its data. Used by bufopen and bufgetdata as + a preliminary check before even trying to physically add the handle. + Returns true if it's ok to add a new handle, false if not. +*/ +static bool can_add_handle(void) +{ + if (cur_handle && cur_handle->filerem > 0) { + /* the current handle hasn't finished buffering. We can only add + a new one if there is already enough free space to finish + the buffering. */ + if (cur_handle->filerem < (buffer_len - BUF_USED)) { + /* Before adding the new handle we reserve some space for the + current one to finish buffering its data. */ + buf_widx = RINGBUF_ADD(buf_widx, cur_handle->filerem); + } else { + return false; + } + } + + return true; +} + /* Return the total amount of data left to be buffered for all the handles */ static size_t data_rem(void) { @@ -488,18 +512,8 @@ management functions for all the actual handle management work. */ int bufopen(char *file, size_t offset, enum data_type type) { - if (cur_handle && cur_handle->filerem > 0) { - /* the current handle hasn't finished buffering. We can only add - a new one if there is already enough free space to finish - the buffering. */ - if (cur_handle->filerem < (buffer_len - BUF_USED)) { - /* Before adding the new handle we reserve some space for the - current one to finish buffering its data. */ - buf_widx = RINGBUF_ADD(buf_widx, cur_handle->filerem); - } else { - return -2; - } - } + if (!can_add_handle()) + return -2; int fd = rb->open(file, O_RDONLY); if (fd < 0)