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