562
|
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
|
|
108 void fi_reset(struct frame_index *fi)
|
|
109 {
|
|
110 debug1("reset with size %zu", fi->size);
|
|
111 fi->fill = 0;
|
|
112 fi->step = 1;
|
|
113 fi->next = fi_next(fi);
|
|
114 }
|