Subversion Repositories Kolibri OS

Rev

Rev 1905 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 serge 1
/*
2
	index: frame index data structure and functions
3
 
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
6
	initially written by Thomas Orgis
7
*/
8
 
9
#include "index.h"
10
#include "debug.h"
11
 
12
/* The next expected frame offset, one step ahead. */
13
static off_t fi_next(struct frame_index *fi)
14
{
15
	return (off_t)fi->fill*fi->step;
16
}
17
 
18
/* Shrink down the used index to the half.
19
   Be careful with size = 1 ... there's no shrinking possible there. */
20
static void fi_shrink(struct frame_index *fi)
21
{
22
	if(fi->fill < 2) return; /* Won't shrink below 1. */
23
	else
24
	{ /* Double the step, half the fill. Should work as well for fill%2 = 1 */
25
		size_t c;
26
		debug2("shrink index with fill %lu and step %lu", (unsigned long)fi->fill, (unsigned long)fi->step);
27
		fi->step *= 2;
28
		fi->fill /= 2;
29
		/* Move the data down. */
30
		for(c = 0; c < fi->fill; ++c)
31
		fi->data[c] = fi->data[2*c];
32
	}
33
 
34
	fi->next = fi_next(fi);
35
}
36
 
37
void fi_init(struct frame_index *fi)
38
{
39
	fi->data = NULL;
40
	fi->step = 1;
41
	fi->fill = 0;
42
	fi->size = 0;
43
	fi->grow_size = 0;
44
	fi->next = fi_next(fi);
45
}
46
 
47
void fi_exit(struct frame_index *fi)
48
{
49
	debug2("fi_exit: %p and %lu", (void*)fi->data, (unsigned long)fi->size);
50
	if(fi->size && fi->data != NULL) free(fi->data);
51
 
52
	fi_init(fi); /* Be prepared for further fun, still. */
53
}
54
 
55
int fi_resize(struct frame_index *fi, size_t newsize)
56
{
57
	off_t *newdata = NULL;
58
	if(newsize == fi->size) return 0;
59
 
60
	if(newsize > 0 && newsize < fi->size)
61
	{ /* When we reduce buffer size a bit, shrink stuff. */
62
		while(fi->fill > newsize){ fi_shrink(fi); }
63
	}
64
 
65
	newdata = safe_realloc(fi->data, newsize*sizeof(off_t));
66
	if(newsize == 0 || newdata != NULL)
67
	{
68
		fi->data = newdata;
69
		fi->size = newsize;
70
		if(fi->fill > fi->size) fi->fill = fi->size;
71
 
72
		fi->next = fi_next(fi);
73
		debug2("new index of size %lu at %p", (unsigned long)fi->size, (void*)fi->data);
74
		return 0;
75
	}
76
	else
77
	{
78
		error("failed to resize index!");
79
		return -1;
80
	}
81
}
82
 
83
void fi_add(struct frame_index *fi, off_t pos)
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);
86
	if(fi->fill == fi->size)
87
	{ /* Index is full, we need to shrink... or grow. */
88
		/* Store the current frame number to check later if we still want it. */
89
		off_t framenum = fi->fill*fi->step;
90
		/* If we want not / cannot grow, we shrink. */
91
		if( !(fi->grow_size && fi_resize(fi, fi->size+fi->grow_size)==0) )
92
		fi_shrink(fi);
93
 
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;
96
	}
97
	/* When we are here, we want that frame. */
98
	if(fi->fill < fi->size) /* safeguard for size=1, or just generally */
99
	{
100
		debug1("adding to index at %p", (void*)(fi->data+fi->fill));
101
		fi->data[fi->fill] = pos;
102
		++fi->fill;
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);
105
	}
106
}
107
 
3960 Serge 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
}
127
 
1905 serge 128
void fi_reset(struct frame_index *fi)
129
{
130
	debug1("reset with size %"SIZE_P, (size_p)fi->size);
131
	fi->fill = 0;
132
	fi->step = 1;
133
	fi->next = fi_next(fi);
134
}