make handle_id of zero valid, stop using memsets to clear tracks, should be no functional changes

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15462 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Brandon Low 2007-11-04 19:01:02 +00:00
parent 66f0cb2f60
commit 31c1164c49
2 changed files with 50 additions and 37 deletions

View file

@ -91,6 +91,8 @@
/* point at which the file buffer will fight for CPU time */ /* point at which the file buffer will fight for CPU time */
#define BUFFERING_CRITICAL_LEVEL (1024*128) #define BUFFERING_CRITICAL_LEVEL (1024*128)
#define BUF_HANDLE_MASK 0x7FFFFFFF
/* Ring buffer helper macros */ /* Ring buffer helper macros */
/* Buffer pointer (p) plus value (v), wrapped if necessary */ /* Buffer pointer (p) plus value (v), wrapped if necessary */
@ -224,7 +226,7 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
const bool alloc_all) const bool alloc_all)
{ {
/* gives each handle a unique id */ /* gives each handle a unique id */
static int cur_handle_id = 1; static int cur_handle_id = 0;
size_t shift; size_t shift;
size_t new_widx; size_t new_widx;
size_t len; size_t len;
@ -292,7 +294,7 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
new_handle->id = cur_handle_id; new_handle->id = cur_handle_id;
/* Wrap signed int is safe and 0 doesn't happen */ /* Wrap signed int is safe and 0 doesn't happen */
if (++cur_handle_id < 1) cur_handle_id = 1; cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
new_handle->next = NULL; new_handle->next = NULL;
num_handles++; num_handles++;
@ -314,7 +316,7 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
static bool rm_handle(const struct memory_handle *h) static bool rm_handle(const struct memory_handle *h)
{ {
if (h == NULL) if (h == NULL)
return false; return true;
mutex_lock(&llist_mutex); mutex_lock(&llist_mutex);
@ -359,7 +361,7 @@ static bool rm_handle(const struct memory_handle *h)
NULL if the handle wasn't found */ NULL if the handle wasn't found */
static struct memory_handle *find_handle(const int handle_id) static struct memory_handle *find_handle(const int handle_id)
{ {
if (handle_id <= 0) if (handle_id < 0)
return NULL; return NULL;
mutex_lock(&llist_mutex); mutex_lock(&llist_mutex);
@ -664,16 +666,18 @@ static void rebuffer_handle(int handle_id, size_t newpos)
static bool close_handle(int handle_id) static bool close_handle(int handle_id)
{ {
struct memory_handle *h = find_handle(handle_id); struct memory_handle *h = find_handle(handle_id);
/* If the handle is not found, it is closed */
if (!h) if (!h)
return false; return true;
if (h->fd >= 0) { if (h->fd >= 0) {
close(h->fd); close(h->fd);
h->fd = -1; h->fd = -1;
} }
rm_handle(h); /* rm_handle returns true unless the handle somehow persists after exit */
return true; return rm_handle(h);
} }
/* Free buffer space by moving the handle struct right before the useful /* Free buffer space by moving the handle struct right before the useful
@ -1327,7 +1331,7 @@ bool buffering_reset(char *buf, size_t buflen)
cur_handle = NULL; cur_handle = NULL;
cached_handle = NULL; cached_handle = NULL;
num_handles = 0; num_handles = 0;
base_handle_id = 0; base_handle_id = -1;
buffer_callback_count = 0; buffer_callback_count = 0;
memset(buffer_low_callback_funcs, 0, sizeof(buffer_low_callback_funcs)); memset(buffer_low_callback_funcs, 0, sizeof(buffer_low_callback_funcs));

View file

@ -362,31 +362,36 @@ static struct mp3entry *bufgetid3(int handle_id)
static bool clear_track_info(struct track_info *track) static bool clear_track_info(struct track_info *track)
{ {
/* bufclose returns true if the handle is not found, or if it is closed
* successfully, so these checks are safe on non-existant handles */
if (!track) if (!track)
return false; return false;
if (track->codec_hid > 0) { if (track->codec_hid >= 0) {
if (bufclose(track->codec_hid)) if (bufclose(track->codec_hid))
track->codec_hid = 0; track->codec_hid = -1;
else else
return false; return false;
} }
if (track->id3_hid > 0) { if (track->id3_hid >= 0) {
if (bufclose(track->id3_hid)) if (bufclose(track->id3_hid))
track->id3_hid = 0; track->id3_hid = -1;
else else
return false; return false;
} }
if (track->audio_hid > 0) { if (track->audio_hid >= 0) {
if (bufclose(track->audio_hid)) if (bufclose(track->audio_hid))
track->audio_hid = 0; track->audio_hid = -1;
else else
return false; return false;
} }
memset(track, 0, sizeof(struct track_info)); track->filesize = 0;
track->taginfo_ready = false;
track->event_sent = false;
return true; return true;
} }
@ -642,7 +647,7 @@ struct mp3entry* audio_current_track(void)
return &curtrack_id3; return &curtrack_id3;
else if (offset == -1 && *prevtrack_id3.path) else if (offset == -1 && *prevtrack_id3.path)
return &prevtrack_id3; return &prevtrack_id3;
else if (tracks[cur_idx].id3_hid > 0) else if (tracks[cur_idx].id3_hid >= 0)
return bufgetid3(tracks[cur_idx].id3_hid); return bufgetid3(tracks[cur_idx].id3_hid);
memset(&temp_id3, 0, sizeof(struct mp3entry)); memset(&temp_id3, 0, sizeof(struct mp3entry));
@ -681,7 +686,7 @@ struct mp3entry* audio_next_track(void)
next_idx++; next_idx++;
next_idx &= MAX_TRACK_MASK; next_idx &= MAX_TRACK_MASK;
if (tracks[next_idx].id3_hid <= 0) if (tracks[next_idx].id3_hid < 0)
return NULL; return NULL;
return &nexttrack_id3; return &nexttrack_id3;
@ -1681,10 +1686,10 @@ static void codec_pcmbuf_track_changed_callback(void)
static void codec_discard_codec_callback(void) static void codec_discard_codec_callback(void)
{ {
if (CUR_TI->codec_hid > 0) if (CUR_TI->codec_hid >= 0)
{ {
bufclose(CUR_TI->codec_hid); bufclose(CUR_TI->codec_hid);
CUR_TI->codec_hid = 0; CUR_TI->codec_hid = -1;
} }
} }
@ -1849,7 +1854,7 @@ static void codec_thread(void)
case Q_CODEC_LOAD: case Q_CODEC_LOAD:
LOGFQUEUE("codec < Q_CODEC_LOAD"); LOGFQUEUE("codec < Q_CODEC_LOAD");
if (CUR_TI->codec_hid <= 0) { if (CUR_TI->codec_hid < 0) {
logf("Codec slot is empty!"); logf("Codec slot is empty!");
/* Wait for the pcm buffer to go empty */ /* Wait for the pcm buffer to go empty */
while (pcm_is_playing()) while (pcm_is_playing())
@ -1961,7 +1966,7 @@ static void codec_thread(void)
} }
} }
if (CUR_TI->codec_hid > 0) if (CUR_TI->codec_hid >= 0)
{ {
LOGFQUEUE("codec > codec Q_CODEC_LOAD"); LOGFQUEUE("codec > codec Q_CODEC_LOAD");
queue_post(&codec_queue, Q_CODEC_LOAD, 0); queue_post(&codec_queue, Q_CODEC_LOAD, 0);
@ -2042,18 +2047,18 @@ long audio_filebufused(void)
static void audio_update_trackinfo(void) static void audio_update_trackinfo(void)
{ {
if (CUR_TI->id3_hid > 0) if (CUR_TI->id3_hid >= 0)
copy_mp3entry(&curtrack_id3, bufgetid3(CUR_TI->id3_hid)); copy_mp3entry(&curtrack_id3, bufgetid3(CUR_TI->id3_hid));
CUR_TI->taginfo_ready = (CUR_TI->id3_hid > 0); CUR_TI->taginfo_ready = (CUR_TI->id3_hid >= 0);
int next_idx = track_ridx + 1; int next_idx = track_ridx + 1;
next_idx &= MAX_TRACK_MASK; next_idx &= MAX_TRACK_MASK;
if (tracks[next_idx].id3_hid > 0) if (tracks[next_idx].id3_hid >= 0)
copy_mp3entry(&nexttrack_id3, bufgetid3(tracks[next_idx].id3_hid)); copy_mp3entry(&nexttrack_id3, bufgetid3(tracks[next_idx].id3_hid));
tracks[next_idx].taginfo_ready = (tracks[next_idx].id3_hid > 0); tracks[next_idx].taginfo_ready = (tracks[next_idx].id3_hid >= 0);
ci.filesize = CUR_TI->filesize; ci.filesize = CUR_TI->filesize;
curtrack_id3.elapsed = 0; curtrack_id3.elapsed = 0;
@ -2094,7 +2099,7 @@ static void audio_clear_track_entries(bool clear_unbuffered)
{ {
/* If there is an unbuffer callback, call it, otherwise, /* If there is an unbuffer callback, call it, otherwise,
* just clear the track */ * just clear the track */
if (track_unbuffer_callback && tracks[cur_idx].id3_hid > 0) if (track_unbuffer_callback && tracks[cur_idx].id3_hid >= 0)
track_unbuffer_callback(bufgetid3(tracks[cur_idx].id3_hid)); track_unbuffer_callback(bufgetid3(tracks[cur_idx].id3_hid));
clear_track_info(&tracks[cur_idx]); clear_track_info(&tracks[cur_idx]);
@ -2125,7 +2130,7 @@ static bool audio_loadcodec(bool start_play)
int prev_track; int prev_track;
char codec_path[MAX_PATH]; /* Full path to codec */ char codec_path[MAX_PATH]; /* Full path to codec */
if (tracks[track_widx].id3_hid <= 0) { if (tracks[track_widx].id3_hid < 0) {
return false; return false;
} }
@ -2134,7 +2139,7 @@ static bool audio_loadcodec(bool start_play)
if (codec_fn == NULL) if (codec_fn == NULL)
return false; return false;
tracks[track_widx].codec_hid = false; tracks[track_widx].codec_hid = -1;
if (start_play) if (start_play)
{ {
@ -2299,15 +2304,15 @@ static bool audio_load_track(int offset, bool start_play)
} }
/* Get track metadata if we don't already have it. */ /* Get track metadata if we don't already have it. */
if (tracks[track_widx].id3_hid <= 0) if (tracks[track_widx].id3_hid < 0)
{ {
if (get_metadata(&id3, fd, trackname)) if (get_metadata(&id3, fd, trackname))
{ {
tracks[track_widx].id3_hid = bufalloc(&id3, sizeof(struct mp3entry), tracks[track_widx].id3_hid =
TYPE_ID3); bufalloc(&id3, sizeof(struct mp3entry), TYPE_ID3);
tracks[track_widx].taginfo_ready = (tracks[track_widx].id3_hid > 0); tracks[track_widx].taginfo_ready = (tracks[track_widx].id3_hid >= 0);
if (tracks[track_widx].id3_hid <= 0) if (tracks[track_widx].id3_hid < 0)
{ {
last_peek_offset--; last_peek_offset--;
close(fd); close(fd);
@ -2438,7 +2443,7 @@ static bool audio_load_track(int offset, bool start_play)
tracks[track_widx].audio_hid = bufopen(trackname, file_offset, type); tracks[track_widx].audio_hid = bufopen(trackname, file_offset, type);
if (tracks[track_widx].audio_hid <= 0) if (tracks[track_widx].audio_hid < 0)
return false; return false;
if (start_play) if (start_play)
@ -2468,7 +2473,7 @@ static void audio_generate_postbuffer_events(void)
{ {
/* Mark the event 'sent' even if we don't really send one */ /* Mark the event 'sent' even if we don't really send one */
tracks[cur_idx].event_sent = true; tracks[cur_idx].event_sent = true;
if (track_buffer_callback && tracks[cur_idx].id3_hid > 0) if (track_buffer_callback && tracks[cur_idx].id3_hid >= 0)
track_buffer_callback(bufgetid3(tracks[cur_idx].id3_hid)); track_buffer_callback(bufgetid3(tracks[cur_idx].id3_hid));
} }
if (cur_idx == track_widx) if (cur_idx == track_widx)
@ -2771,6 +2776,8 @@ static void audio_stop_playback(void)
static void audio_play_start(size_t offset) static void audio_play_start(size_t offset)
{ {
int i;
#if INPUT_SRC_CAPS != 0 #if INPUT_SRC_CAPS != 0
audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK); audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
audio_set_output_source(AUDIO_SRC_PLAYBACK); audio_set_output_source(AUDIO_SRC_PLAYBACK);
@ -2792,8 +2799,10 @@ static void audio_play_start(size_t offset)
sound_set_volume(global_settings.volume); sound_set_volume(global_settings.volume);
track_widx = track_ridx = 0; track_widx = track_ridx = 0;
/* Mark all entries null. */ /* Clear all track entries. */
memset(tracks, 0, sizeof(struct track_info) * MAX_TRACK); for (i = 0; i < MAX_TRACK; i++) {
clear_track_info(&tracks[i]);
}
last_peek_offset = -1; last_peek_offset = -1;