From 404c6fbdb288de0e88eeb7484e2ab524ef438871 Mon Sep 17 00:00:00 2001 From: Brandon Low Date: Sat, 27 Oct 2007 01:37:33 +0000 Subject: Add some const keywords, improve some comments, add a safety check or two, should have no functional difference git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15326 a1c6a512-1295-4272-9138-f99709370657 --- apps/buffering.c | 63 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 29 deletions(-) (limited to 'apps') diff --git a/apps/buffering.c b/apps/buffering.c index 44d3e60b1f..b8fd16f870 100644 --- a/apps/buffering.c +++ b/apps/buffering.c @@ -94,31 +94,31 @@ /* Ring buffer helper macros */ /* Buffer pointer (p) plus value (v), wrapped if necessary */ -#define RINGBUF_ADD(p,v) ((p+v)=v) ? p-v : p+buffer_len-v) +#define RINGBUF_SUB(p,v) ((p>=v) ? (p)-(v) : (p)+buffer_len-(v)) /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */ #define RINGBUF_ADD_CROSS(p1,v,p2) \ -((p1 8 static size_t high_watermark = 0; /* High watermark for rebuffer */ #endif @@ -149,7 +151,7 @@ static int base_handle_id; static struct mutex llist_mutex; /* Handle cache (makes find_handle faster). - This needs be to be global so that move_handle can invalidate it. */ + This is global so that move_handle and rm_handle can invalidate it. */ static struct memory_handle *cached_handle = NULL; static buffer_low_callback buffer_low_callback_funcs[MAX_BUF_CALLBACKS]; @@ -266,8 +268,11 @@ static struct memory_handle *add_handle(size_t *data_size) /* Delete a given memory handle from the linked list and return true for success. Nothing is actually erased from memory. */ -static bool rm_handle(struct memory_handle *h) +static bool rm_handle(const struct memory_handle *h) { + if (h == NULL) + return false; + mutex_lock(&llist_mutex); if (h == first_handle) { @@ -275,7 +280,7 @@ static bool rm_handle(struct memory_handle *h) if (h == cur_handle) { /* h was the first and last handle: the buffer is now empty */ cur_handle = NULL; - buf_ridx = buf_widx; + buf_ridx = buf_widx = 0; } else { /* update buf_ridx to point to the new first handle */ buf_ridx = (void *)first_handle - (void *)buffer; @@ -285,7 +290,7 @@ static bool rm_handle(struct memory_handle *h) while (m && m->next != h) { m = m->next; } - if (h && m && m->next == h) { + if (m && m->next == h) { m->next = h->next; if (h == cur_handle) { cur_handle = m; @@ -309,7 +314,7 @@ static bool rm_handle(struct memory_handle *h) /* Return a pointer to the memory handle of given ID. NULL if the handle wasn't found */ -static struct memory_handle *find_handle(int handle_id) +static struct memory_handle *find_handle(const int handle_id) { if (handle_id <= 0) return NULL; @@ -336,9 +341,8 @@ static struct memory_handle *find_handle(int handle_id) m = m->next; } /* This condition can only be reached with !m or m->id == handle_id */ - if (m) { + if (m) cached_handle = m; - } mutex_unlock(&llist_mutex); return m; @@ -853,7 +857,7 @@ int bufseek(int handle_id, size_t newpos) Return 0 for success and < 0 for failure */ int bufadvance(int handle_id, off_t offset) { - struct memory_handle *h = find_handle(handle_id); + const struct memory_handle *h = find_handle(handle_id); if (!h) return -1; @@ -865,7 +869,7 @@ int bufadvance(int handle_id, off_t offset) Return the number of bytes copied or < 0 for failure. */ ssize_t bufread(int handle_id, size_t size, void *dest) { - struct memory_handle *h = find_handle(handle_id); + const struct memory_handle *h = find_handle(handle_id); if (!h) return -1; @@ -908,7 +912,7 @@ ssize_t bufread(int handle_id, size_t size, void *dest) The guard buffer may be used to provide the requested size */ ssize_t bufgetdata(int handle_id, size_t size, void **data) { - struct memory_handle *h = find_handle(handle_id); + const struct memory_handle *h = find_handle(handle_id); if (!h) return -1; @@ -964,7 +968,7 @@ management functions for all the actual handle management work. /* Get a handle offset from a pointer */ ssize_t buf_get_offset(int handle_id, void *ptr) { - struct memory_handle *h = find_handle(handle_id); + const struct memory_handle *h = find_handle(handle_id); if (!h) return -1; @@ -973,7 +977,7 @@ ssize_t buf_get_offset(int handle_id, void *ptr) ssize_t buf_handle_offset(int handle_id) { - struct memory_handle *h = find_handle(handle_id); + const struct memory_handle *h = find_handle(handle_id); if (!h) return -1; return h->offset; @@ -1142,6 +1146,7 @@ void buffering_thread(void) data_counters.buffered < high_watermark) { fill_buffer(); + update_data_counters(); } if (ata_disk_is_active() && queue_empty(&buffering_queue) && -- cgit v1.2.3