/src/mpg123/src/libmpg123/index.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
16 | 0 | return (int64_t)fi->fill*fi->step; |
17 | 0 | } |
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 | 0 | { |
23 | 0 | if(fi->fill < 2) return; /* Won't shrink below 1. */ |
24 | 0 | else |
25 | 0 | { /* Double the step, half the fill. Should work as well for fill%2 = 1 */ |
26 | 0 | size_t c; |
27 | 0 | debug2("shrink index with fill %lu and step %lu", (unsigned long)fi->fill, (unsigned long)fi->step); |
28 | 0 | fi->step *= 2; |
29 | 0 | fi->fill /= 2; |
30 | | /* Move the data down. */ |
31 | 0 | for(c = 0; c < fi->fill; ++c) |
32 | 0 | fi->data[c] = fi->data[2*c]; |
33 | 0 | } |
34 | | |
35 | 0 | fi->next = fi_next(fi); |
36 | 0 | } |
37 | | |
38 | | void INT123_fi_init(struct frame_index *fi) |
39 | 0 | { |
40 | 0 | fi->data = NULL; |
41 | 0 | fi->step = 1; |
42 | 0 | fi->fill = 0; |
43 | 0 | fi->size = 0; |
44 | 0 | fi->grow_size = 0; |
45 | 0 | fi->next = fi_next(fi); |
46 | 0 | } |
47 | | |
48 | | void INT123_fi_exit(struct frame_index *fi) |
49 | 0 | { |
50 | 0 | debug2("INT123_fi_exit: %p and %lu", (void*)fi->data, (unsigned long)fi->size); |
51 | 0 | if(fi->size && fi->data != NULL) free(fi->data); |
52 | |
|
53 | 0 | INT123_fi_init(fi); /* Be prepared for further fun, still. */ |
54 | 0 | } |
55 | | |
56 | | int INT123_fi_resize(struct frame_index *fi, size_t newsize) |
57 | 0 | { |
58 | 0 | int64_t *newdata = NULL; |
59 | 0 | if(newsize == fi->size) return 0; |
60 | | |
61 | 0 | if(newsize > 0 && newsize < fi->size) |
62 | 0 | { /* When we reduce buffer size a bit, shrink stuff. */ |
63 | 0 | while(fi->fill > newsize){ fi_shrink(fi); } |
64 | 0 | } |
65 | |
|
66 | 0 | newdata = INT123_safe_realloc(fi->data, newsize*sizeof(int64_t)); |
67 | 0 | if(newsize == 0 || newdata != NULL) |
68 | 0 | { |
69 | 0 | fi->data = newdata; |
70 | 0 | fi->size = newsize; |
71 | 0 | if(fi->fill > fi->size) fi->fill = fi->size; |
72 | |
|
73 | 0 | fi->next = fi_next(fi); |
74 | 0 | debug2("new index of size %lu at %p", (unsigned long)fi->size, (void*)fi->data); |
75 | 0 | return 0; |
76 | 0 | } else |
77 | 0 | return -1; |
78 | 0 | } |
79 | | |
80 | | void INT123_fi_add(struct frame_index *fi, int64_t pos) |
81 | 0 | { |
82 | 0 | debug3("wanting to add to fill %lu, step %lu, size %lu", (unsigned long)fi->fill, (unsigned long)fi->step, (unsigned long)fi->size); |
83 | 0 | if(fi->fill == fi->size) |
84 | 0 | { /* Index is full, we need to shrink... or grow. */ |
85 | | /* Store the current frame number to check later if we still want it. */ |
86 | 0 | int64_t framenum = fi->fill*fi->step; |
87 | | /* If we want not / cannot grow, we shrink. */ |
88 | 0 | if( !(fi->grow_size && INT123_fi_resize(fi, fi->size+fi->grow_size)==0) ) |
89 | 0 | fi_shrink(fi); |
90 | | |
91 | | /* Now check if we still want to add this frame (could be that not, because of changed step). */ |
92 | 0 | if(fi->next != framenum) return; |
93 | 0 | } |
94 | | /* When we are here, we want that frame. */ |
95 | 0 | if(fi->fill < fi->size) /* safeguard for size=1, or just generally */ |
96 | 0 | { |
97 | 0 | debug1("adding to index at %p", (void*)(fi->data+fi->fill)); |
98 | 0 | fi->data[fi->fill] = pos; |
99 | 0 | ++fi->fill; |
100 | 0 | fi->next = fi_next(fi); |
101 | 0 | debug3("added pos %li to index with fill %lu and step %lu", (long) pos, (unsigned long)fi->fill, (unsigned long)fi->step); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | int INT123_fi_set(struct frame_index *fi, int64_t *offsets, int64_t step, size_t fill) |
106 | 0 | { |
107 | 0 | if(INT123_fi_resize(fi, fill) == -1) return -1; |
108 | 0 | fi->step = step; |
109 | 0 | if(offsets != NULL) |
110 | 0 | { |
111 | 0 | memcpy(fi->data, offsets, fill*sizeof(int64_t)); |
112 | 0 | fi->fill = fill; |
113 | 0 | } |
114 | 0 | else |
115 | 0 | { |
116 | | /* allocation only, no entries in index yet */ |
117 | 0 | fi->fill = 0; |
118 | 0 | } |
119 | 0 | fi->next = fi_next(fi); |
120 | 0 | debug3("set new index of fill %lu, size %lu at %p", |
121 | 0 | (unsigned long)fi->fill, (unsigned long)fi->size, (void*)fi->data); |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | | void INT123_fi_reset(struct frame_index *fi) |
126 | 0 | { |
127 | 0 | debug1("reset with size %zu", fi->size); |
128 | 0 | fi->fill = 0; |
129 | 0 | fi->step = 1; |
130 | 0 | fi->next = fi_next(fi); |
131 | 0 | } |