1
0
Fork 0
forked from len0rd/rockbox

Make handle id semantics more like file id. This may need to be revisited if we ever start storing long lived things on the buffer (if an item lives through 32k other items coming and going, there will be an id collision and things will break)

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15437 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Brandon Low 2007-11-03 21:13:15 +00:00
parent 60d4e7c9c4
commit acb3e9af22
3 changed files with 38 additions and 56 deletions

View file

@ -105,7 +105,7 @@
/* assert(sizeof(struct memory_handle)%4==0) */
struct memory_handle {
unsigned int id; /* A unique ID for the handle */
int id; /* A unique ID for the handle */
enum data_type type; /* Type of data buffered with this handle */
char path[MAX_PATH]; /* Path if data originated in a file */
int fd; /* File descriptor to path (-1 if closed) */
@ -146,7 +146,7 @@ static struct memory_handle *first_handle;
static int num_handles; /* number of handles in the list */
static unsigned int base_handle_id;
static int base_handle_id;
static struct mutex llist_mutex;
@ -224,7 +224,7 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
const bool alloc_all)
{
/* gives each handle a unique id, unsigned to handle wraps gracefully */
static unsigned int cur_handle_id = 1;
static int cur_handle_id = 0;
size_t shift;
size_t new_widx;
size_t len;
@ -291,9 +291,7 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
new_handle->id = cur_handle_id;
/* Use += 2 instead of ++ to guarantee that the low bit is always high and
* prevent the assignment of a zero id when wrapping. */
cur_handle_id += 2;
cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_ID_MASK;
new_handle->next = NULL;
num_handles++;
@ -358,9 +356,9 @@ static bool rm_handle(const 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(const unsigned int handle_id)
static struct memory_handle *find_handle(const int handle_id)
{
if (handle_id <= 0)
if (handle_id < 0)
return NULL;
mutex_lock(&llist_mutex);
@ -550,7 +548,7 @@ static bool buffer_handle(int handle_id)
logf("buffer_handle(%d)", handle_id);
struct memory_handle *h = find_handle(handle_id);
if (!h)
return -1;
return false;
if (h->filerem == 0) {
/* nothing left to buffer */
@ -770,7 +768,7 @@ void update_data_counters(void)
{
struct memory_handle *m = find_handle(base_handle_id);
if (!m)
base_handle_id = 0;
base_handle_id = -1;
memset(&data_counters, 0, sizeof(data_counters));
@ -1310,7 +1308,7 @@ bool buffering_reset(char *buf, size_t buflen)
cur_handle = NULL;
cached_handle = NULL;
num_handles = 0;
base_handle_id = 0;
base_handle_id = -1;
buffer_callback_count = 0;
memset(buffer_low_callback_funcs, 0, sizeof(buffer_low_callback_funcs));

View file

@ -65,6 +65,7 @@ bool buffering_reset(char *buf, size_t buflen);
* amount of data is ready (unless EOF is reached).
****************************************************************************/
#define BUF_HANDLE_ID_MASK 0x7FFF
#define BUF_MAX_HANDLES 256
int bufopen(const char *file, size_t offset, enum data_type type);

View file

@ -217,7 +217,6 @@ struct track_info {
size_t filesize; /* File total length */
bool has_codec; /* Codec length in bytes */
bool taginfo_ready; /* Is metadata read */
bool event_sent; /* Was this track's buffered event sent */
@ -366,23 +365,23 @@ static bool clear_track_info(struct track_info *track)
if (!track)
return false;
if (track->codec_hid > 0) {
if (track->codec_hid >= 0) {
if (bufclose(track->codec_hid))
track->codec_hid = 0;
track->codec_hid = -1;
else
return false;
}
if (track->id3_hid > 0) {
if (track->id3_hid >= 0) {
if (bufclose(track->id3_hid))
track->id3_hid = 0;
track->id3_hid = -1;
else
return false;
}
if (track->audio_hid > 0) {
if (track->audio_hid >= 0) {
if (bufclose(track->audio_hid))
track->audio_hid = 0;
track->audio_hid = -1;
else
return false;
}
@ -643,7 +642,7 @@ struct mp3entry* audio_current_track(void)
return &curtrack_id3;
else if (offset == -1 && *prevtrack_id3.path)
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);
memset(&temp_id3, 0, sizeof(struct mp3entry));
@ -682,7 +681,7 @@ struct mp3entry* audio_next_track(void)
next_idx++;
next_idx &= MAX_TRACK_MASK;
if (tracks[next_idx].id3_hid <= 0)
if (tracks[next_idx].id3_hid < 0)
return NULL;
return &nexttrack_id3;
@ -1682,11 +1681,10 @@ static void codec_pcmbuf_track_changed_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);
CUR_TI->codec_hid = 0;
CUR_TI->has_codec = false;
CUR_TI->codec_hid = -1;
}
}
@ -1851,7 +1849,7 @@ static void codec_thread(void)
case 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!");
/* Wait for the pcm buffer to go empty */
while (pcm_is_playing())
@ -1963,7 +1961,7 @@ static void codec_thread(void)
}
}
if (CUR_TI->codec_hid > 0)
if (CUR_TI->codec_hid >= 0)
{
LOGFQUEUE("codec > codec Q_CODEC_LOAD");
queue_post(&codec_queue, Q_CODEC_LOAD, 0);
@ -2044,18 +2042,18 @@ long audio_filebufused(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));
CUR_TI->taginfo_ready = (CUR_TI->id3_hid > 0);
CUR_TI->taginfo_ready = (CUR_TI->id3_hid >= 0);
int next_idx = track_ridx + 1;
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));
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;
curtrack_id3.elapsed = 0;
@ -2096,7 +2094,7 @@ static void audio_clear_track_entries(bool clear_unbuffered)
{
/* If there is an unbuffer callback, call it, otherwise,
* 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));
clear_track_info(&tracks[cur_idx]);
@ -2127,7 +2125,7 @@ static bool audio_loadcodec(bool start_play)
int prev_track;
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;
}
@ -2136,7 +2134,7 @@ static bool audio_loadcodec(bool start_play)
if (codec_fn == NULL)
return false;
tracks[track_widx].codec_hid = false;
tracks[track_widx].codec_hid = -1;
if (start_play)
{
@ -2173,26 +2171,11 @@ static bool audio_loadcodec(bool start_play)
codec_get_full_path(codec_path, codec_fn);
/* Found a codec filename */
tracks[track_widx].has_codec = true;
tracks[track_widx].codec_hid = bufopen(codec_path, 0, TYPE_CODEC);
if (tracks[track_widx].codec_hid < 0)
{
if (tracks[track_widx].codec_hid == ERR_FILE_ERROR)
{
logf("Codec file error");
tracks[track_widx].has_codec = false;
}
else
{
logf("Not enough space");
}
return false;
}
logf("Loaded codec");
return true;
}
@ -2315,15 +2298,16 @@ static bool audio_load_track(int offset, bool start_play)
}
/* 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))
{
tracks[track_widx].id3_hid = bufalloc(&id3, sizeof(struct mp3entry),
TYPE_ID3);
tracks[track_widx].taginfo_ready = (tracks[track_widx].id3_hid > 0);
tracks[track_widx].id3_hid =
bufalloc(&id3, sizeof(struct mp3entry), TYPE_ID3);
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--;
close(fd);
@ -2376,10 +2360,9 @@ static bool audio_load_track(int offset, bool start_play)
/* Load the codec. */
if (!audio_loadcodec(start_play))
{
if (tracks[track_widx].has_codec)
if (tracks[track_widx].codec_hid == ERR_BUFFER_FULL)
{
/* No space for codec on buffer, not an error */
tracks[track_widx].has_codec = false;
return false;
}
@ -2453,7 +2436,7 @@ static bool audio_load_track(int offset, bool start_play)
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;
if (start_play)
@ -2483,7 +2466,7 @@ static void audio_generate_postbuffer_events(void)
{
/* Mark the event 'sent' even if we don't really send one */
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));
}
if (cur_idx == track_widx)