Subversion Repositories Kolibri OS

Rev

Rev 1905 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1905 Rev 3960
1
/*
1
/*
2
	index: frame index data structure and functions
2
	index: frame index data structure and functions
3
 
3
 
4
	copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
4
	copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
6
	initially written by Thomas Orgis
6
	initially written by Thomas Orgis
7
*/
7
*/
8
 
8
 
9
#include "index.h"
9
#include "index.h"
10
#include "debug.h"
10
#include "debug.h"
11
 
11
 
12
/* The next expected frame offset, one step ahead. */
12
/* The next expected frame offset, one step ahead. */
13
static off_t fi_next(struct frame_index *fi)
13
static off_t fi_next(struct frame_index *fi)
14
{
14
{
15
	return (off_t)fi->fill*fi->step;
15
	return (off_t)fi->fill*fi->step;
16
}
16
}
17
 
17
 
18
/* Shrink down the used index to the half.
18
/* Shrink down the used index to the half.
19
   Be careful with size = 1 ... there's no shrinking possible there. */
19
   Be careful with size = 1 ... there's no shrinking possible there. */
20
static void fi_shrink(struct frame_index *fi)
20
static void fi_shrink(struct frame_index *fi)
21
{
21
{
22
	if(fi->fill < 2) return; /* Won't shrink below 1. */
22
	if(fi->fill < 2) return; /* Won't shrink below 1. */
23
	else
23
	else
24
	{ /* Double the step, half the fill. Should work as well for fill%2 = 1 */
24
	{ /* Double the step, half the fill. Should work as well for fill%2 = 1 */
25
		size_t c;
25
		size_t c;
26
		debug2("shrink index with fill %lu and step %lu", (unsigned long)fi->fill, (unsigned long)fi->step);
26
		debug2("shrink index with fill %lu and step %lu", (unsigned long)fi->fill, (unsigned long)fi->step);
27
		fi->step *= 2;
27
		fi->step *= 2;
28
		fi->fill /= 2;
28
		fi->fill /= 2;
29
		/* Move the data down. */
29
		/* Move the data down. */
30
		for(c = 0; c < fi->fill; ++c)
30
		for(c = 0; c < fi->fill; ++c)
31
		fi->data[c] = fi->data[2*c];
31
		fi->data[c] = fi->data[2*c];
32
	}
32
	}
33
 
33
 
34
	fi->next = fi_next(fi);
34
	fi->next = fi_next(fi);
35
}
35
}
36
 
36
 
37
void fi_init(struct frame_index *fi)
37
void fi_init(struct frame_index *fi)
38
{
38
{
39
	fi->data = NULL;
39
	fi->data = NULL;
40
	fi->step = 1;
40
	fi->step = 1;
41
	fi->fill = 0;
41
	fi->fill = 0;
42
	fi->size = 0;
42
	fi->size = 0;
43
	fi->grow_size = 0;
43
	fi->grow_size = 0;
44
	fi->next = fi_next(fi);
44
	fi->next = fi_next(fi);
45
}
45
}
46
 
46
 
47
void fi_exit(struct frame_index *fi)
47
void fi_exit(struct frame_index *fi)
48
{
48
{
49
	debug2("fi_exit: %p and %lu", (void*)fi->data, (unsigned long)fi->size);
49
	debug2("fi_exit: %p and %lu", (void*)fi->data, (unsigned long)fi->size);
50
	if(fi->size && fi->data != NULL) free(fi->data);
50
	if(fi->size && fi->data != NULL) free(fi->data);
51
 
51
 
52
	fi_init(fi); /* Be prepared for further fun, still. */
52
	fi_init(fi); /* Be prepared for further fun, still. */
53
}
53
}
54
 
54
 
55
int fi_resize(struct frame_index *fi, size_t newsize)
55
int fi_resize(struct frame_index *fi, size_t newsize)
56
{
56
{
57
	off_t *newdata = NULL;
57
	off_t *newdata = NULL;
58
	if(newsize == fi->size) return 0;
58
	if(newsize == fi->size) return 0;
59
 
59
 
60
	if(newsize > 0 && newsize < fi->size)
60
	if(newsize > 0 && newsize < fi->size)
61
	{ /* When we reduce buffer size a bit, shrink stuff. */
61
	{ /* When we reduce buffer size a bit, shrink stuff. */
62
		while(fi->fill > newsize){ fi_shrink(fi); }
62
		while(fi->fill > newsize){ fi_shrink(fi); }
63
	}
63
	}
64
 
64
 
65
	newdata = safe_realloc(fi->data, newsize*sizeof(off_t));
65
	newdata = safe_realloc(fi->data, newsize*sizeof(off_t));
66
	if(newsize == 0 || newdata != NULL)
66
	if(newsize == 0 || newdata != NULL)
67
	{
67
	{
68
		fi->data = newdata;
68
		fi->data = newdata;
69
		fi->size = newsize;
69
		fi->size = newsize;
70
		if(fi->fill > fi->size) fi->fill = fi->size;
70
		if(fi->fill > fi->size) fi->fill = fi->size;
71
 
71
 
72
		fi->next = fi_next(fi);
72
		fi->next = fi_next(fi);
73
		debug2("new index of size %lu at %p", (unsigned long)fi->size, (void*)fi->data);
73
		debug2("new index of size %lu at %p", (unsigned long)fi->size, (void*)fi->data);
74
		return 0;
74
		return 0;
75
	}
75
	}
76
	else
76
	else
77
	{
77
	{
78
		error("failed to resize index!");
78
		error("failed to resize index!");
79
		return -1;
79
		return -1;
80
	}
80
	}
81
}
81
}
82
 
82
 
83
void fi_add(struct frame_index *fi, off_t pos)
83
void fi_add(struct frame_index *fi, off_t pos)
84
{
84
{
85
	debug3("wanting to add to fill %lu, step %lu, size %lu", (unsigned long)fi->fill, (unsigned long)fi->step, (unsigned long)fi->size);
85
	debug3("wanting to add to fill %lu, step %lu, size %lu", (unsigned long)fi->fill, (unsigned long)fi->step, (unsigned long)fi->size);
86
	if(fi->fill == fi->size)
86
	if(fi->fill == fi->size)
87
	{ /* Index is full, we need to shrink... or grow. */
87
	{ /* Index is full, we need to shrink... or grow. */
88
		/* Store the current frame number to check later if we still want it. */
88
		/* Store the current frame number to check later if we still want it. */
89
		off_t framenum = fi->fill*fi->step;
89
		off_t framenum = fi->fill*fi->step;
90
		/* If we want not / cannot grow, we shrink. */	
90
		/* If we want not / cannot grow, we shrink. */	
91
		if( !(fi->grow_size && fi_resize(fi, fi->size+fi->grow_size)==0) )
91
		if( !(fi->grow_size && fi_resize(fi, fi->size+fi->grow_size)==0) )
92
		fi_shrink(fi);
92
		fi_shrink(fi);
93
 
93
 
94
		/* Now check if we still want to add this frame (could be that not, because of changed step). */
94
		/* Now check if we still want to add this frame (could be that not, because of changed step). */
95
		if(fi->next != framenum) return;
95
		if(fi->next != framenum) return;
96
	}
96
	}
97
	/* When we are here, we want that frame. */
97
	/* When we are here, we want that frame. */
98
	if(fi->fill < fi->size) /* safeguard for size=1, or just generally */
98
	if(fi->fill < fi->size) /* safeguard for size=1, or just generally */
99
	{
99
	{
100
		debug1("adding to index at %p", (void*)(fi->data+fi->fill));
100
		debug1("adding to index at %p", (void*)(fi->data+fi->fill));
101
		fi->data[fi->fill] = pos;
101
		fi->data[fi->fill] = pos;
102
		++fi->fill;
102
		++fi->fill;
103
		fi->next = fi_next(fi);
103
		fi->next = fi_next(fi);
104
		debug3("added pos %li to index with fill %lu and step %lu", (long) pos, (unsigned long)fi->fill, (unsigned long)fi->step);
104
		debug3("added pos %li to index with fill %lu and step %lu", (long) pos, (unsigned long)fi->fill, (unsigned long)fi->step);
105
	}
105
	}
106
}
106
}
-
 
107
 
-
 
108
int fi_set(struct frame_index *fi, off_t *offsets, off_t step, size_t fill)
-
 
109
{
-
 
110
	if(fi_resize(fi, fill) == -1) return -1;
-
 
111
	fi->step = step;
-
 
112
	if(offsets != NULL)
-
 
113
	{
-
 
114
		memcpy(fi->data, offsets, fill*sizeof(off_t));
-
 
115
		fi->fill = fill;
-
 
116
	}
-
 
117
	else
-
 
118
	{
-
 
119
		/* allocation only, no entries in index yet */
-
 
120
		fi->fill = 0;
-
 
121
	}
-
 
122
	fi->next = fi_next(fi);
-
 
123
	debug3("set new index of fill %lu, size %lu at %p",
-
 
124
	(unsigned long)fi->fill, (unsigned long)fi->size, (void*)fi->data);
-
 
125
	return 0;
-
 
126
}
107
 
127
 
108
void fi_reset(struct frame_index *fi)
128
void fi_reset(struct frame_index *fi)
109
{
129
{
110
	debug1("reset with size %"SIZE_P, (size_p)fi->size);
130
	debug1("reset with size %"SIZE_P, (size_p)fi->size);
111
	fi->fill = 0;
131
	fi->fill = 0;
112
	fi->step = 1;
132
	fi->step = 1;
113
	fi->next = fi_next(fi);
133
	fi->next = fi_next(fi);
114
}
134
}