summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFred Bauer <fred.w.bauer@gmail.com>2011-11-25 16:15:16 +0000
committerFred Bauer <fred.w.bauer@gmail.com>2011-11-25 16:15:16 +0000
commite62cccb81e36055a24d14122fcd95af9904296c7 (patch)
tree205289a630d29caace365a0c43cf462aa4e39dac
parent62f5027650b79ee35162f465165ab30e38442651 (diff)
downloadrockbox-e62cccb81e36055a24d14122fcd95af9904296c7.tar.gz
rockbox-e62cccb81e36055a24d14122fcd95af9904296c7.zip
fix up font_cache/LRU boundry errors from r30763
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31050 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/font_cache.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/firmware/font_cache.c b/firmware/font_cache.c
index 4624b1866e..50320ed932 100644
--- a/firmware/font_cache.c
+++ b/firmware/font_cache.c
@@ -78,13 +78,14 @@ void font_cache_create(
78 ************************************************************************/ 78 ************************************************************************/
79static int search(struct font_cache* fcache, 79static int search(struct font_cache* fcache,
80 unsigned short char_code, 80 unsigned short char_code,
81 int size,
81 int *p_insertion_point ) 82 int *p_insertion_point )
82{ 83{
83 struct font_cache_entry *p; 84 struct font_cache_entry *p;
84 int left, right, mid=-1, c; 85 int left, right, mid=-1, c;
85 left = 0; 86 left = 0;
86 right = fcache->_size - 1; 87 right = size;
87 88
88 /* go for a lucky guess */ 89 /* go for a lucky guess */
89 mid = char_code + 90 mid = char_code +
90 fcache->_prev_result - fcache->_prev_char_code; 91 fcache->_prev_result - fcache->_prev_char_code;
@@ -115,6 +116,7 @@ static int search(struct font_cache* fcache,
115 while (left <= right); 116 while (left <= right);
116 117
117 /* not found */ 118 /* not found */
119 p = lru_data(&fcache->_lru, fcache->_index[mid]);
118 *p_insertion_point = mid; 120 *p_insertion_point = mid;
119 return 0; 121 return 0;
120} 122}
@@ -131,29 +133,45 @@ struct font_cache_entry* font_cache_get(
131 int insertion_point; 133 int insertion_point;
132 int index_to_replace; 134 int index_to_replace;
133 135
134 if( search(fcache, char_code, &insertion_point)) 136 /* check bounds */
137 p = lru_data(&fcache->_lru, fcache->_index[0]);
138 if( char_code < p->_char_code )
139 insertion_point = -1;
140 else
135 { 141 {
136 short lru_handle = fcache->_index[insertion_point]; 142 p = lru_data(&fcache->_lru, fcache->_index[fcache->_capacity - 1]);
137 p = lru_data(&fcache->_lru, lru_handle); 143 if( char_code > p->_char_code )
138 if (p->_char_code == char_code)
139 { 144 {
140 lru_touch(&fcache->_lru, lru_handle); 145 insertion_point = fcache->_capacity - 1;
141 return lru_data(&fcache->_lru, lru_handle); 146 }
147 else
148 {
149 if( search(fcache, char_code, fcache->_size - 1, &insertion_point))
150 {
151 short lru_handle = fcache->_index[insertion_point];
152 p = lru_data(&fcache->_lru, lru_handle);
153 if (p->_char_code == char_code)
154 {
155 lru_touch(&fcache->_lru, lru_handle);
156 return lru_data(&fcache->_lru, lru_handle);
157 }
158 }
159 else
160 {
161 p = lru_data(&fcache->_lru,
162 fcache->_index[insertion_point+1]);
163 if ( char_code > p->_char_code )
164 insertion_point++;
165 }
142 } 166 }
143 } 167 }
144 else /* not found */ 168
145 { 169 /* not found */
146 p = lru_data(&fcache->_lru,
147 fcache->_index[insertion_point+1]);
148 if ( char_code > p->_char_code )
149 insertion_point++;
150 }
151 170
152 /* find index to replace */ 171 /* find index to replace */
153 short lru_handle_to_replace = fcache->_lru._head; 172 short lru_handle_to_replace = fcache->_lru._head;
154 p = lru_data(&fcache->_lru, lru_handle_to_replace); 173 p = lru_data(&fcache->_lru, lru_handle_to_replace);
155 search(fcache, p->_char_code, &index_to_replace); 174 search(fcache, p->_char_code, fcache->_size - 1, &index_to_replace);
156
157 if (insertion_point < index_to_replace) 175 if (insertion_point < index_to_replace)
158 { 176 {
159 /* shift memory up */ 177 /* shift memory up */