--- lbbs/src/file_loader.c 2025/05/20 12:29:26 1.9 +++ lbbs/src/file_loader.c 2025/06/01 14:04:19 1.13 @@ -29,8 +29,6 @@ #include #include -#define MAX_SPLIT_FILE_LINES 65536 - struct shm_header_t { int shmid; @@ -60,13 +58,7 @@ int file_loader_init() static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) { - const void *p_shm = (const void *)value; - int shmid = *((int *)p_shm); - - if (shmdt(p_shm) == -1) - { - log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); - } + int shmid = (int)value; if (shmctl(shmid, IPC_RMID, NULL) == -1) { @@ -82,12 +74,12 @@ void file_loader_cleanup(void) } trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); - trie_dict_destroy(p_trie_file_dict); + p_trie_file_dict = NULL; } -int load_file_shm(const char *filename) +int load_file(const char *filename) { int fd; struct stat sb; @@ -102,7 +94,6 @@ int load_file_shm(const char *filename) long line_offsets[MAX_SPLIT_FILE_LINES]; long *p_line_offsets; int64_t shmid_old; - void *p_shm_old; if ((fd = open(filename, O_RDONLY)) < 0) { @@ -131,10 +122,6 @@ int load_file_shm(const char *filename) } line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES); - if (line_total >= MAX_SPLIT_FILE_LINES) - { - log_error("split_data_lines() truncated over limit lines\n"); - } // Allocate shared memory proj_id = (int)(time(NULL) % getpid()); @@ -174,30 +161,14 @@ int load_file_shm(const char *filename) p_line_offsets = p_data + data_len + 1; memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); - // Re-mount shm in read-only mode if (shmdt(p_shm) == -1) { - log_error("shmdt() error (%d)\n", errno); - return -3; - } - - p_shm = shmat(shmid, NULL, SHM_RDONLY); - if (p_shm == (void *)-1) - { - log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); + log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); return -3; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1) + if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1) { - shmid_old = *((int *)p_shm_old); - - if (shmdt(p_shm_old) == -1) - { - log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno); - return -3; - } - if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1) { log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno); @@ -205,7 +176,7 @@ int load_file_shm(const char *filename) } } - if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1) + if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1) { log_error("trie_dict_set(%s) error\n", filename); @@ -220,24 +191,16 @@ int load_file_shm(const char *filename) return 0; } -int unload_file_shm(const char *filename) +int unload_file(const char *filename) { - const void *p_shm; - int shmid; + int64_t shmid; - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) + if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) { log_error("trie_dict_get(%s) not found\n", filename); return -1; } - shmid = *((int *)p_shm); - - if (shmdt(p_shm) == -1) - { - log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); - } - if (shmctl((int)shmid, IPC_RMID, NULL) == -1) { log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); @@ -252,8 +215,9 @@ int unload_file_shm(const char *filename return 0; } -const void *get_file_shm(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) +const void *get_file_shm_readonly(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) { + int64_t shmid; const void *p_shm; if (p_trie_file_dict == NULL) @@ -262,12 +226,19 @@ const void *get_file_shm(const char *fil return NULL; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) // Not exist + if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) // Not exist { log_error("trie_dict_get(%s) not found\n", filename); return NULL; } + p_shm = shmat((int)shmid, NULL, SHM_RDONLY); + if (p_shm == (void *)-1) + { + log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno); + return NULL; + } + *p_data_len = ((struct shm_header_t *)p_shm)->data_len; *p_line_total = ((struct shm_header_t *)p_shm)->line_total; *pp_data = p_shm + sizeof(struct shm_header_t); @@ -275,3 +246,19 @@ const void *get_file_shm(const char *fil return p_shm; } + +int detach_file_shm(const void *p_shm) +{ + if (p_shm == NULL) + { + return -2; + } + + if (shmdt(p_shm) == -1) + { + log_error("shmdt() error (%d)\n", errno); + return -1; + } + + return 0; +}