summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2023-10-03 11:07:49 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2023-10-03 11:07:49 -0400
commitb64d46d9d450588e00555a5c12c2fc119448657e (patch)
treef5cc0cf5b809380025a6131cee8f454997e44304 /apps
parent3bb3e3d1a6297c7bc9015d008389266e529758c1 (diff)
downloadrockbox-b64d46d9d450588e00555a5c12c2fc119448657e.tar.gz
rockbox-b64d46d9d450588e00555a5c12c2fc119448657e.zip
Revert "tagcache reduce stack usage in tmpbuf_insert and build_index functions"
This reverts commit 3bb3e3d1a6297c7bc9015d008389266e529758c1. Reason for revert: lifetimes do not overlap but patch has issues Change-Id: I3f831599dd1523c156205aa6565ff8afc2f4d8f6
Diffstat (limited to 'apps')
-rw-r--r--apps/tagcache.c276
1 files changed, 135 insertions, 141 deletions
diff --git a/apps/tagcache.c b/apps/tagcache.c
index 16f48a7828..f7746f1ec3 100644
--- a/apps/tagcache.c
+++ b/apps/tagcache.c
@@ -55,7 +55,6 @@
55 * 55 *
56 */ 56 */
57 57
58
59/*#define LOGF_ENABLE*/ 58/*#define LOGF_ENABLE*/
60/*#define LOGF_CLAUSES define to enable logf clause matching (LOGF_ENABLE req'd) */ 59/*#define LOGF_CLAUSES define to enable logf clause matching (LOGF_ENABLE req'd) */
61 60
@@ -175,8 +174,6 @@ static const char tagcache_thread_name[] = "tagcache";
175 174
176/* Previous path when scanning directory tree recursively. */ 175/* Previous path when scanning directory tree recursively. */
177static char curpath[TAGCACHE_BUFSZ]; 176static char curpath[TAGCACHE_BUFSZ];
178/* Shared buffer for several build_index fns to reduce stack usage */
179static char tmp_buf[TAGCACHE_BUFSZ];
180 177
181/* Used when removing duplicates. */ 178/* Used when removing duplicates. */
182static char *tempbuf; /* Allocated when needed. */ 179static char *tempbuf; /* Allocated when needed. */
@@ -358,10 +355,11 @@ static inline void tcrc_buffer_unlock(void)
358 * Full tag entries stored in a temporary file waiting 355 * Full tag entries stored in a temporary file waiting
359 * for commit to the cache. */ 356 * for commit to the cache. */
360struct temp_file_entry { 357struct temp_file_entry {
361 int32_t tag_offset[TAG_COUNT]; 358 long tag_offset[TAG_COUNT];
362 int16_t tag_length[TAG_COUNT]; 359 short tag_length[TAG_COUNT];
363 int32_t flag; 360 long flag;
364 int32_t data_length; 361
362 long data_length;
365}; 363};
366 364
367struct tempbuf_id_list { 365struct tempbuf_id_list {
@@ -398,6 +396,43 @@ static inline void str_setlen(char *buf, size_t len)
398 buf[len] = '\0'; 396 buf[len] = '\0';
399} 397}
400 398
399static void allocate_tempbuf(void)
400{
401 /* Yeah, malloc would be really nice now :) */
402 size_t size;
403 tempbuf_size = 0;
404
405#ifdef __PCTOOL__
406 size = 32*1024*1024;
407 tempbuf = malloc(size);
408 if (tempbuf)
409 tempbuf_size = size;
410#else /* !__PCTOOL__ */
411 /* Need to pass dummy ops to prevent the buffer being moved
412 * out from under us, since we yield during the tagcache commit. */
413 tempbuf_handle = core_alloc_maximum(&size, &buflib_ops_locked);
414 if (tempbuf_handle > 0)
415 {
416 tempbuf = core_get_data(tempbuf_handle);
417 tempbuf_size = size;
418 }
419#endif /* __PCTOOL__ */
420}
421
422static void free_tempbuf(void)
423{
424 if (tempbuf_size == 0)
425 return ;
426
427#ifdef __PCTOOL__
428 free(tempbuf);
429#else
430 tempbuf_handle = core_free(tempbuf_handle);
431#endif
432 tempbuf = NULL;
433 tempbuf_size = 0;
434}
435
401const char* tagcache_tag_to_str(int tag) 436const char* tagcache_tag_to_str(int tag)
402{ 437{
403 return tags_str[tag]; 438 return tags_str[tag];
@@ -596,8 +631,8 @@ static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
596 tc_stat.db_path, tag); 631 tc_stat.db_path, tag);
597 if (fd < 0) 632 if (fd < 0)
598 { 633 {
599 logf("%s failed: tag=%d write=%d file= " TAGCACHE_FILE_INDEX, 634 logf("tag file open failed: tag=%d write=%d file= " TAGCACHE_FILE_INDEX,
600 __func__, tag, write, tag); 635 tag, write, tag);
601 tc_stat.ready = false; 636 tc_stat.ready = false;
602 return fd; 637 return fd;
603 } 638 }
@@ -658,85 +693,6 @@ static int open_master_fd(struct master_header *hdr, bool write)
658 return fd; 693 return fd;
659} 694}
660 695
661static void remove_files(void)
662{
663 int i;
664 char buf[MAX_PATH];
665 const int bufsz = sizeof(buf);
666 logf("%s", __func__);
667
668 tc_stat.ready = false;
669 tc_stat.ramcache = false;
670 tc_stat.econ = false;
671 remove_db_file(TAGCACHE_FILE_MASTER);
672 for (i = 0; i < TAG_COUNT; i++)
673 {
674 if (TAGCACHE_IS_NUMERIC(i))
675 continue;
676
677 snprintf(buf, bufsz, "%s/" TAGCACHE_FILE_INDEX,
678 tc_stat.db_path, i);
679 remove(buf);
680 }
681}
682
683static bool check_all_headers(void)
684{
685 struct master_header myhdr;
686 struct tagcache_header tch;
687 int tag;
688 int fd;
689
690 if ( (fd = open_master_fd(&myhdr, false)) < 0)
691 return false;
692
693 close(fd);
694 if (myhdr.dirty)
695 {
696 logf("tagcache is dirty!");
697 return false;
698 }
699
700 memcpy(&current_tcmh, &myhdr, sizeof(struct master_header));
701
702 for (tag = 0; tag < TAG_COUNT; tag++)
703 {
704 if (TAGCACHE_IS_NUMERIC(tag))
705 continue;
706
707 if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
708 return false;
709
710 close(fd);
711 }
712
713 return true;
714}
715
716static bool update_master_header(void)
717{
718 struct master_header myhdr;
719 int fd;
720
721 if (!tc_stat.ready)
722 return false;
723
724 if ( (fd = open_master_fd(&myhdr, true)) < 0)
725 return false;
726
727 myhdr.serial = current_tcmh.serial;
728 myhdr.commitid = current_tcmh.commitid;
729 myhdr.dirty = current_tcmh.dirty;
730
731 /* Write it back */
732 lseek(fd, 0, SEEK_SET);
733 write_master_header(fd, &myhdr);
734 close(fd);
735
736 return true;
737}
738
739
740#ifndef __PCTOOL__ 696#ifndef __PCTOOL__
741static bool do_timed_yield(void) 697static bool do_timed_yield(void)
742{ 698{
@@ -752,44 +708,6 @@ static bool do_timed_yield(void)
752} 708}
753#endif /* __PCTOOL__ */ 709#endif /* __PCTOOL__ */
754 710
755static void allocate_tempbuf(void)
756{
757 /* Yeah, malloc would be really nice now :) */
758 size_t size;
759 tempbuf_size = 0;
760
761#ifdef __PCTOOL__
762 size = 32*1024*1024;
763 tempbuf = malloc(size);
764 if (tempbuf)
765 tempbuf_size = size;
766#else /* !__PCTOOL__ */
767 /* Need to pass dummy ops to prevent the buffer being moved
768 * out from under us, since we yield during the tagcache commit. */
769 tempbuf_handle = core_alloc_maximum(&size, &buflib_ops_locked);
770 if (tempbuf_handle > 0)
771 {
772 tempbuf = core_get_data(tempbuf_handle);
773 tempbuf_size = size;
774 }
775#endif /* __PCTOOL__ */
776
777}
778
779static void free_tempbuf(void)
780{
781 if (tempbuf_size == 0)
782 return ;
783
784#ifdef __PCTOOL__
785 free(tempbuf);
786#else
787 tempbuf_handle = core_free(tempbuf_handle);
788#endif
789 tempbuf = NULL;
790 tempbuf_size = 0;
791}
792
793#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE) 711#if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
794/* find the ramcache entry corresponding to the file indicated by 712/* find the ramcache entry corresponding to the file indicated by
795 * filename and dc (it's corresponding dircache id). */ 713 * filename and dc (it's corresponding dircache id). */
@@ -1718,6 +1636,61 @@ static bool build_lookup_list(struct tagcache_search *tcs)
1718} 1636}
1719 1637
1720 1638
1639static void remove_files(void)
1640{
1641 int i;
1642 char buf[MAX_PATH];
1643 const int bufsz = sizeof(buf);
1644
1645 tc_stat.ready = false;
1646 tc_stat.ramcache = false;
1647 tc_stat.econ = false;
1648 remove_db_file(TAGCACHE_FILE_MASTER);
1649 for (i = 0; i < TAG_COUNT; i++)
1650 {
1651 if (TAGCACHE_IS_NUMERIC(i))
1652 continue;
1653
1654 snprintf(buf, bufsz, "%s/" TAGCACHE_FILE_INDEX,
1655 tc_stat.db_path, i);
1656 remove(buf);
1657 }
1658}
1659
1660
1661static bool check_all_headers(void)
1662{
1663 struct master_header myhdr;
1664 struct tagcache_header tch;
1665 int tag;
1666 int fd;
1667
1668 if ( (fd = open_master_fd(&myhdr, false)) < 0)
1669 return false;
1670
1671 close(fd);
1672 if (myhdr.dirty)
1673 {
1674 logf("tagcache is dirty!");
1675 return false;
1676 }
1677
1678 memcpy(&current_tcmh, &myhdr, sizeof(struct master_header));
1679
1680 for (tag = 0; tag < TAG_COUNT; tag++)
1681 {
1682 if (TAGCACHE_IS_NUMERIC(tag))
1683 continue;
1684
1685 if ( (fd = open_tag_fd(&tch, tag, false)) < 0)
1686 return false;
1687
1688 close(fd);
1689 }
1690
1691 return true;
1692}
1693
1721bool tagcache_search(struct tagcache_search *tcs, int tag) 1694bool tagcache_search(struct tagcache_search *tcs, int tag)
1722{ 1695{
1723 struct tagcache_header tag_hdr; 1696 struct tagcache_header tag_hdr;
@@ -2032,6 +2005,29 @@ bool tagcache_retrieve(struct tagcache_search *tcs, int idxid,
2032 return retrieve(tcs, IF_DIRCACHE(idxid,) &idx, tag, buf, size); 2005 return retrieve(tcs, IF_DIRCACHE(idxid,) &idx, tag, buf, size);
2033} 2006}
2034 2007
2008static bool update_master_header(void)
2009{
2010 struct master_header myhdr;
2011 int fd;
2012
2013 if (!tc_stat.ready)
2014 return false;
2015
2016 if ( (fd = open_master_fd(&myhdr, true)) < 0)
2017 return false;
2018
2019 myhdr.serial = current_tcmh.serial;
2020 myhdr.commitid = current_tcmh.commitid;
2021 myhdr.dirty = current_tcmh.dirty;
2022
2023 /* Write it back */
2024 lseek(fd, 0, SEEK_SET);
2025 write_master_header(fd, &myhdr);
2026 close(fd);
2027
2028 return true;
2029}
2030
2035void tagcache_search_finish(struct tagcache_search *tcs) 2031void tagcache_search_finish(struct tagcache_search *tcs)
2036{ 2032{
2037 int i; 2033 int i;
@@ -2370,14 +2366,15 @@ static bool tempbuf_insert(char *str, int id, int idx_id, bool unique)
2370 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf; 2366 struct tempbuf_searchidx *index = (struct tempbuf_searchidx *)tempbuf;
2371 int len = strlen(str)+1; 2367 int len = strlen(str)+1;
2372 int i; 2368 int i;
2369 unsigned crc32;
2373 unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4]; 2370 unsigned *crcbuf = (unsigned *)&tempbuf[tempbuf_size-4];
2374 unsigned crc32 = 0xffffffff; 2371 char buf[TAGCACHE_BUFSZ];
2375 char chr_lower; 2372 const int bufsz = sizeof(buf);
2376 for (i = 0; str[i] != '\0' && i < len; i++) 2373
2377 { 2374 for (i = 0; str[i] != '\0' && i < bufsz-1; i++)
2378 chr_lower = tolower(str[i]); 2375 buf[i] = tolower(str[i]);
2379 crc32 = crc_32(&chr_lower, 1, crc32); 2376
2380 } 2377 crc32 = crc_32(buf, i, 0xffffffff);
2381 2378
2382 if (unique) 2379 if (unique)
2383 { 2380 {
@@ -2571,8 +2568,8 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2571 int entries_processed = 0; 2568 int entries_processed = 0;
2572 int i, j; 2569 int i, j;
2573 2570
2574 char *buf = tmp_buf; 2571 char buf[TAGCACHE_BUFSZ];
2575 const long bufsz = sizeof(tmp_buf); 2572 const int bufsz = sizeof(buf);
2576 2573
2577 max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1; 2574 max_entries = tempbuf_size / sizeof(struct temp_file_entry) - 1;
2578 2575
@@ -2811,8 +2808,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2811 struct master_header tcmh; 2808 struct master_header tcmh;
2812 struct index_entry idxbuf[IDX_BUF_DEPTH]; 2809 struct index_entry idxbuf[IDX_BUF_DEPTH];
2813 int idxbuf_pos; 2810 int idxbuf_pos;
2814 char *buf = tmp_buf; 2811 char buf[TAGCACHE_BUFSZ];
2815 const long bufsz = sizeof(tmp_buf); 2812 const long bufsz = sizeof(buf);
2816 int fd = -1, masterfd; 2813 int fd = -1, masterfd;
2817 bool error = false; 2814 bool error = false;
2818 int init; 2815 int init;
@@ -2950,7 +2947,6 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2950 } 2947 }
2951 else 2948 else
2952 { 2949 {
2953 logf("Create New Index: %d", index_type);
2954 /** 2950 /**
2955 * Creating new index file to store the tags. No need to preload 2951 * Creating new index file to store the tags. No need to preload
2956 * anything whether the index type is sorted or not. 2952 * anything whether the index type is sorted or not.
@@ -3412,7 +3408,6 @@ static bool commit(void)
3412 tc_stat.commit_step = 0; 3408 tc_stat.commit_step = 0;
3413 goto commit_error; 3409 goto commit_error;
3414 } 3410 }
3415 do_timed_yield();
3416 } 3411 }
3417 3412
3418 if (!build_numeric_indices(&tch, tmpfd)) 3413 if (!build_numeric_indices(&tch, tmpfd))
@@ -3448,7 +3443,7 @@ static bool commit(void)
3448 tc_stat.ready = check_all_headers(); 3443 tc_stat.ready = check_all_headers();
3449 tc_stat.readyvalid = true; 3444 tc_stat.readyvalid = true;
3450 3445
3451#if defined(HAVE_TC_RAMCACHE) 3446#ifdef HAVE_TC_RAMCACHE
3452 if (ramcache_buffer_stolen) 3447 if (ramcache_buffer_stolen)
3453 { 3448 {
3454 tempbuf = NULL; 3449 tempbuf = NULL;
@@ -3488,7 +3483,6 @@ commit_error:
3488 return rc; 3483 return rc;
3489} 3484}
3490 3485
3491
3492#ifndef __PCTOOL__ 3486#ifndef __PCTOOL__
3493 3487
3494static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data) 3488static bool modify_numeric_entry(int masterfd, int idx_id, int tag, long data)