diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2022-04-03 11:16:39 +0100 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2022-10-16 14:50:39 +0100 |
commit | 1718cf5f8a39b922eba3ad1b3c9a9570188362b1 (patch) | |
tree | 4f6bf81cb4f382ca04856b98492289825133c5ae /apps/tagtree.c | |
parent | b16bae6fe624d30631bf83290e204197ab136c12 (diff) | |
download | rockbox-1718cf5f8a39b922eba3ad1b3c9a9570188362b1.tar.gz rockbox-1718cf5f8a39b922eba3ad1b3c9a9570188362b1.zip |
Convert a number of allocations to use buflib pinning
Several places in the codebase implemented an ad-hoc form of pinning;
they can be converted to use buflib pinning instead.
Change-Id: I4450be007e80f6c9cc9f56c2929fa4b9b85ebff3
Diffstat (limited to 'apps/tagtree.c')
-rw-r--r-- | apps/tagtree.c | 66 |
1 files changed, 24 insertions, 42 deletions
diff --git a/apps/tagtree.c b/apps/tagtree.c index 39bc0ab37c..2ebab7949c 100644 --- a/apps/tagtree.c +++ b/apps/tagtree.c | |||
@@ -192,7 +192,7 @@ static int current_entry_count; | |||
192 | static struct tree_context *tc; | 192 | static struct tree_context *tc; |
193 | 193 | ||
194 | /* a few memory alloc helper */ | 194 | /* a few memory alloc helper */ |
195 | static int tagtree_handle, lock_count; | 195 | static int tagtree_handle; |
196 | static size_t tagtree_bufsize, tagtree_buf_used; | 196 | static size_t tagtree_bufsize, tagtree_buf_used; |
197 | 197 | ||
198 | #define UPDATE(x, y) { x = (typeof(x))((char*)(x) + (y)); } | 198 | #define UPDATE(x, y) { x = (typeof(x))((char*)(x) + (y)); } |
@@ -201,9 +201,6 @@ static int move_callback(int handle, void* current, void* new) | |||
201 | (void)handle; (void)current; (void)new; | 201 | (void)handle; (void)current; (void)new; |
202 | ptrdiff_t diff = new - current; | 202 | ptrdiff_t diff = new - current; |
203 | 203 | ||
204 | if (lock_count > 0) | ||
205 | return BUFLIB_CB_CANNOT_MOVE; | ||
206 | |||
207 | if (menu) | 204 | if (menu) |
208 | UPDATE(menu, diff); | 205 | UPDATE(menu, diff); |
209 | 206 | ||
@@ -251,16 +248,6 @@ static int move_callback(int handle, void* current, void* new) | |||
251 | } | 248 | } |
252 | #undef UPDATE | 249 | #undef UPDATE |
253 | 250 | ||
254 | static inline void tagtree_lock(void) | ||
255 | { | ||
256 | lock_count++; | ||
257 | } | ||
258 | |||
259 | static inline void tagtree_unlock(void) | ||
260 | { | ||
261 | lock_count--; | ||
262 | } | ||
263 | |||
264 | static struct buflib_callbacks ops = { | 251 | static struct buflib_callbacks ops = { |
265 | .move_callback = move_callback, | 252 | .move_callback = move_callback, |
266 | .shrink_callback = NULL, | 253 | .shrink_callback = NULL, |
@@ -623,7 +610,7 @@ static int add_format(const char *buf) | |||
623 | int clause_count = 0; | 610 | int clause_count = 0; |
624 | strp++; | 611 | strp++; |
625 | 612 | ||
626 | tagtree_lock(); | 613 | core_pin(tagtree_handle); |
627 | while (1) | 614 | while (1) |
628 | { | 615 | { |
629 | struct tagcache_search_clause *new_clause; | 616 | struct tagcache_search_clause *new_clause; |
@@ -646,7 +633,7 @@ static int add_format(const char *buf) | |||
646 | 633 | ||
647 | clause_count++; | 634 | clause_count++; |
648 | } | 635 | } |
649 | tagtree_unlock(); | 636 | core_unpin(tagtree_handle); |
650 | 637 | ||
651 | formats[format_count]->clause_count = clause_count; | 638 | formats[format_count]->clause_count = clause_count; |
652 | } | 639 | } |
@@ -719,9 +706,9 @@ static int get_condition(struct search_instruction *inst) | |||
719 | } | 706 | } |
720 | else | 707 | else |
721 | { | 708 | { |
722 | tagtree_lock(); | 709 | core_pin(tagtree_handle); |
723 | bool ret = read_clause(new_clause); | 710 | bool ret = read_clause(new_clause); |
724 | tagtree_unlock(); | 711 | core_unpin(tagtree_handle); |
725 | if (!ret) | 712 | if (!ret) |
726 | return -1; | 713 | return -1; |
727 | } | 714 | } |
@@ -812,9 +799,9 @@ static bool parse_search(struct menu_entry *entry, const char *str) | |||
812 | 799 | ||
813 | logf("tag: %d", inst->tagorder[inst->tagorder_count]); | 800 | logf("tag: %d", inst->tagorder[inst->tagorder_count]); |
814 | 801 | ||
815 | tagtree_lock(); | 802 | core_pin(tagtree_handle); |
816 | while ( (ret = get_condition(inst)) > 0 ) ; | 803 | while ( (ret = get_condition(inst)) > 0 ) ; |
817 | tagtree_unlock(); | 804 | core_unpin(tagtree_handle); |
818 | 805 | ||
819 | if (ret < 0) | 806 | if (ret < 0) |
820 | return false; | 807 | return false; |
@@ -1176,10 +1163,10 @@ static int parse_line(int n, char *buf, void *parameters) | |||
1176 | logf("tagtree failed to allocate %s", "menu items"); | 1163 | logf("tagtree failed to allocate %s", "menu items"); |
1177 | return -2; | 1164 | return -2; |
1178 | } | 1165 | } |
1179 | tagtree_lock(); | 1166 | core_pin(tagtree_handle); |
1180 | if (parse_search(menu->items[menu->itemcount], buf)) | 1167 | if (parse_search(menu->items[menu->itemcount], buf)) |
1181 | menu->itemcount++; | 1168 | menu->itemcount++; |
1182 | tagtree_unlock(); | 1169 | core_unpin(tagtree_handle); |
1183 | 1170 | ||
1184 | return 0; | 1171 | return 0; |
1185 | } | 1172 | } |
@@ -1212,8 +1199,8 @@ static bool parse_menu(const char *filename) | |||
1212 | 1199 | ||
1213 | static void tagtree_unload(struct tree_context *c) | 1200 | static void tagtree_unload(struct tree_context *c) |
1214 | { | 1201 | { |
1215 | int i; | 1202 | /* may be spurious... */ |
1216 | tagtree_lock(); | 1203 | core_pin(tagtree_handle); |
1217 | 1204 | ||
1218 | remove_event(PLAYBACK_EVENT_TRACK_BUFFER, tagtree_buffer_event); | 1205 | remove_event(PLAYBACK_EVENT_TRACK_BUFFER, tagtree_buffer_event); |
1219 | remove_event(PLAYBACK_EVENT_TRACK_FINISH, tagtree_track_finish_event); | 1206 | remove_event(PLAYBACK_EVENT_TRACK_FINISH, tagtree_track_finish_event); |
@@ -1229,7 +1216,7 @@ static void tagtree_unload(struct tree_context *c) | |||
1229 | return; | 1216 | return; |
1230 | } | 1217 | } |
1231 | 1218 | ||
1232 | for (i = 0; i < menu->itemcount; i++) | 1219 | for (int i = 0; i < menu->itemcount; i++) |
1233 | { | 1220 | { |
1234 | dptr->name = NULL; | 1221 | dptr->name = NULL; |
1235 | dptr->newtable = 0; | 1222 | dptr->newtable = 0; |
@@ -1238,11 +1225,11 @@ static void tagtree_unload(struct tree_context *c) | |||
1238 | } | 1225 | } |
1239 | } | 1226 | } |
1240 | 1227 | ||
1241 | for (i = 0; i < menu_count; i++) | 1228 | for (int i = 0; i < menu_count; i++) |
1242 | menus[i] = NULL; | 1229 | menus[i] = NULL; |
1243 | menu_count = 0; | 1230 | menu_count = 0; |
1244 | 1231 | ||
1245 | for (i = 0; i < format_count; i++) | 1232 | for (int i = 0; i < format_count; i++) |
1246 | formats[i] = NULL; | 1233 | formats[i] = NULL; |
1247 | format_count = 0; | 1234 | format_count = 0; |
1248 | 1235 | ||
@@ -1253,9 +1240,6 @@ static void tagtree_unload(struct tree_context *c) | |||
1253 | 1240 | ||
1254 | if (c) | 1241 | if (c) |
1255 | tree_unlock_cache(c); | 1242 | tree_unlock_cache(c); |
1256 | tagtree_unlock(); | ||
1257 | if (lock_count > 0) | ||
1258 | tagtree_unlock();/* second unlock to enable re-init */ | ||
1259 | } | 1243 | } |
1260 | 1244 | ||
1261 | void tagtree_init(void) | 1245 | void tagtree_init(void) |
@@ -1285,8 +1269,6 @@ void tagtree_init(void) | |||
1285 | if (sizeof(struct tagentry) != sizeof(struct entry)) | 1269 | if (sizeof(struct tagentry) != sizeof(struct entry)) |
1286 | panicf("tagentry(%zu) and entry mismatch(%zu)", | 1270 | panicf("tagentry(%zu) and entry mismatch(%zu)", |
1287 | sizeof(struct tagentry), sizeof(struct entry)); | 1271 | sizeof(struct tagentry), sizeof(struct entry)); |
1288 | if (lock_count > 0) | ||
1289 | panicf("tagtree locked after parsing"); | ||
1290 | 1272 | ||
1291 | /* If no root menu is set, assume it's the first single menu | 1273 | /* If no root menu is set, assume it's the first single menu |
1292 | * we have. That shouldn't normally happen. */ | 1274 | * we have. That shouldn't normally happen. */ |
@@ -1502,7 +1484,7 @@ static int retrieve_entries(struct tree_context *c, int offset, bool init) | |||
1502 | 1484 | ||
1503 | /* because tagcache saves the clauses, we need to lock the buffer | 1485 | /* because tagcache saves the clauses, we need to lock the buffer |
1504 | * for the entire duration of the search */ | 1486 | * for the entire duration of the search */ |
1505 | tagtree_lock(); | 1487 | core_pin(tagtree_handle); |
1506 | for (i = 0; i <= level; i++) | 1488 | for (i = 0; i <= level; i++) |
1507 | { | 1489 | { |
1508 | int j; | 1490 | int j; |
@@ -1579,7 +1561,7 @@ static int retrieve_entries(struct tree_context *c, int offset, bool init) | |||
1579 | 1561 | ||
1580 | fmt = NULL; | 1562 | fmt = NULL; |
1581 | /* Check the format */ | 1563 | /* Check the format */ |
1582 | tagtree_lock(); | 1564 | core_pin(tagtree_handle); |
1583 | for (i = 0; i < format_count; i++) | 1565 | for (i = 0; i < format_count; i++) |
1584 | { | 1566 | { |
1585 | if (formats[i]->group_id != csi->format_id[level]) | 1567 | if (formats[i]->group_id != csi->format_id[level]) |
@@ -1592,7 +1574,7 @@ static int retrieve_entries(struct tree_context *c, int offset, bool init) | |||
1592 | break; | 1574 | break; |
1593 | } | 1575 | } |
1594 | } | 1576 | } |
1595 | tagtree_unlock(); | 1577 | core_unpin(tagtree_handle); |
1596 | 1578 | ||
1597 | if (strcmp(tcs.result, UNTAGGED) == 0) | 1579 | if (strcmp(tcs.result, UNTAGGED) == 0) |
1598 | { | 1580 | { |
@@ -1634,7 +1616,7 @@ static int retrieve_entries(struct tree_context *c, int offset, bool init) | |||
1634 | logf("format_str() failed"); | 1616 | logf("format_str() failed"); |
1635 | tagcache_search_finish(&tcs); | 1617 | tagcache_search_finish(&tcs); |
1636 | tree_unlock_cache(c); | 1618 | tree_unlock_cache(c); |
1637 | tagtree_unlock(); | 1619 | core_unpin(tagtree_handle); |
1638 | return 0; | 1620 | return 0; |
1639 | } | 1621 | } |
1640 | else | 1622 | else |
@@ -1675,7 +1657,7 @@ entry_skip_formatter: | |||
1675 | { /* user aborted */ | 1657 | { /* user aborted */ |
1676 | tagcache_search_finish(&tcs); | 1658 | tagcache_search_finish(&tcs); |
1677 | tree_unlock_cache(c); | 1659 | tree_unlock_cache(c); |
1678 | tagtree_unlock(); | 1660 | core_unpin(tagtree_handle); |
1679 | return current_entry_count; | 1661 | return current_entry_count; |
1680 | } | 1662 | } |
1681 | } | 1663 | } |
@@ -1694,7 +1676,7 @@ entry_skip_formatter: | |||
1694 | { | 1676 | { |
1695 | tagcache_search_finish(&tcs); | 1677 | tagcache_search_finish(&tcs); |
1696 | tree_unlock_cache(c); | 1678 | tree_unlock_cache(c); |
1697 | tagtree_unlock(); | 1679 | core_unpin(tagtree_handle); |
1698 | return current_entry_count; | 1680 | return current_entry_count; |
1699 | } | 1681 | } |
1700 | 1682 | ||
@@ -1710,7 +1692,7 @@ entry_skip_formatter: | |||
1710 | 1692 | ||
1711 | tagcache_search_finish(&tcs); | 1693 | tagcache_search_finish(&tcs); |
1712 | tree_unlock_cache(c); | 1694 | tree_unlock_cache(c); |
1713 | tagtree_unlock(); | 1695 | core_unpin(tagtree_handle); |
1714 | 1696 | ||
1715 | if (!sort && (sort_inverse || sort_limit)) | 1697 | if (!sort && (sort_inverse || sort_limit)) |
1716 | { | 1698 | { |
@@ -1870,7 +1852,7 @@ int tagtree_enter(struct tree_context* c) | |||
1870 | 1852 | ||
1871 | /* lock buflib for possible I/O to protect dptr */ | 1853 | /* lock buflib for possible I/O to protect dptr */ |
1872 | tree_lock_cache(c); | 1854 | tree_lock_cache(c); |
1873 | tagtree_lock(); | 1855 | core_pin(tagtree_handle); |
1874 | 1856 | ||
1875 | bool reset_selection = true; | 1857 | bool reset_selection = true; |
1876 | 1858 | ||
@@ -1940,7 +1922,7 @@ int tagtree_enter(struct tree_context* c) | |||
1940 | { | 1922 | { |
1941 | tagtree_exit(c); | 1923 | tagtree_exit(c); |
1942 | tree_unlock_cache(c); | 1924 | tree_unlock_cache(c); |
1943 | tagtree_unlock(); | 1925 | core_unpin(tagtree_handle); |
1944 | return 0; | 1926 | return 0; |
1945 | } | 1927 | } |
1946 | if (csi->clause[i][j]->numeric) | 1928 | if (csi->clause[i][j]->numeric) |
@@ -1999,7 +1981,7 @@ int tagtree_enter(struct tree_context* c) | |||
1999 | } | 1981 | } |
2000 | 1982 | ||
2001 | tree_unlock_cache(c); | 1983 | tree_unlock_cache(c); |
2002 | tagtree_unlock(); | 1984 | core_unpin(tagtree_handle); |
2003 | 1985 | ||
2004 | return rc; | 1986 | return rc; |
2005 | } | 1987 | } |