--- lbbs/src/file_loader.c 2025/05/16 14:09:31 1.2 +++ lbbs/src/file_loader.c 2025/05/23 14:11:38 1.10 @@ -22,38 +22,32 @@ #include #include #include +#include +#include #include #include +#include +#include -static FILE_MMAP *p_file_mmap_pool = NULL; -static int file_mmap_count = 0; -static int file_mmap_free_index = -1; -static TRIE_NODE *p_trie_file_dict = NULL; +#define MAX_SPLIT_FILE_LINES 65536 -int file_loader_init(int max_file_mmap_count) +struct shm_header_t { - if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT) - { - log_error("file_loader_init(%d) argument error\n", max_file_mmap_count); - return -1; - } + int shmid; + size_t data_len; + long line_total; +}; - if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL) +static TRIE_NODE *p_trie_file_dict = NULL; + +int file_loader_init() +{ + if (p_trie_file_dict != NULL) { log_error("File loader already initialized\n"); return -1; } - p_file_mmap_pool = (FILE_MMAP *)calloc((size_t)max_file_mmap_count, sizeof(FILE_MMAP)); - if (p_file_mmap_pool == NULL) - { - log_error("calloc(%d p_file_mmap_pool) error\n", max_file_mmap_count); - return -2; - } - - file_mmap_count = max_file_mmap_count; - file_mmap_free_index = 0; - p_trie_file_dict = trie_dict_create(); if (p_trie_file_dict == NULL) { @@ -64,43 +58,51 @@ int file_loader_init(int max_file_mmap_c return 0; } -void file_loader_cleanup(void) +static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) { - if (p_trie_file_dict != NULL) + const void *p_shm = (const void *)value; + int shmid = *((int *)p_shm); + + if (shmdt(p_shm) == -1) { - trie_dict_destroy(p_trie_file_dict); - p_trie_file_dict = NULL; + log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); } - if (p_file_mmap_pool != NULL) + if (shmctl(shmid, IPC_RMID, NULL) == -1) { - for (int i = 0; i < file_mmap_count; i++) - { - if (p_file_mmap_pool[i].p_data == NULL) - { - continue; - } - - if (munmap(p_file_mmap_pool[i].p_data, p_file_mmap_pool[i].size) < 0) - { - log_error("munmap() error (%d)\n", errno); - } - } - free(p_file_mmap_pool); - p_file_mmap_pool = NULL; + log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); + } +} + +void file_loader_cleanup(void) +{ + if (p_trie_file_dict == NULL) + { + return; } - file_mmap_count = 0; - file_mmap_free_index = -1; + 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_mmap(const char *filename) +int load_file_shm(const char *filename) { int fd; struct stat sb; void *p_data; + size_t data_len; + int proj_id; + key_t key; size_t size; - int file_mmap_index = 0; + int shmid; + void *p_shm; + long line_total; + 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) { @@ -114,9 +116,8 @@ int load_file_mmap(const char *filename) return -1; } - size = (size_t)sb.st_size; - - p_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L); + data_len = (size_t)sb.st_size; + p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L); if (p_data == MAP_FAILED) { log_error("mmap() error (%d)\n", errno); @@ -129,81 +130,111 @@ int load_file_mmap(const char *filename) return -1; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) == 0) // Not exist + line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES); + if (line_total >= MAX_SPLIT_FILE_LINES) { - if (file_mmap_free_index == -1) - { - log_error("file_mmap_pool is depleted\n"); - return -3; - } + log_error("split_data_lines() truncated over limit lines\n"); + } - file_mmap_index = file_mmap_free_index; + // Allocate shared memory + proj_id = (int)(time(NULL) % getpid()); + key = ftok(filename, proj_id); + if (key == -1) + { + log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); + return -2; + } - if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1) - { - log_error("trie_dict_set(%s) error\n", filename); + size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); + shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); + if (shmid == -1) + { + log_error("shmget(size = %d) error (%d)\n", size, errno); + return -3; + } + p_shm = shmat(shmid, NULL, 0); + if (p_shm == (void *)-1) + { + log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); + return -3; + } - if (munmap(p_data, size) < 0) - { - log_error("munmap() error (%d)\n", errno); - } + ((struct shm_header_t *)p_shm)->shmid = shmid; + ((struct shm_header_t *)p_shm)->data_len = data_len; + ((struct shm_header_t *)p_shm)->line_total = line_total; + memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len); - return -2; - } + if (munmap(p_data, data_len) < 0) + { + log_error("munmap() error (%d)\n", errno); + return -2; + } - do - { - file_mmap_free_index++; - if (file_mmap_free_index >= file_mmap_count) - { - file_mmap_free_index = 0; - } - if (file_mmap_free_index == file_mmap_index) // loop - { - file_mmap_free_index = -1; - break; - } - } while (p_file_mmap_pool[file_mmap_free_index].p_data != NULL); + p_data = p_shm + sizeof(struct shm_header_t); + p_line_offsets = p_data + data_len + 1; + memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); + + // Remap shared memory in read-only mode + p_shm = shmat(shmid, p_shm, SHM_RDONLY | SHM_REMAP); + if (p_shm == (void *)-1) + { + log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); + return -3; } - else + + if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1) { - // Unload existing data - if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) + 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("munmap() error (%d)\n", errno); + log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno); + return -2; } } - p_file_mmap_pool[file_mmap_index].p_data = p_data; - p_file_mmap_pool[file_mmap_index].size = size; + if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1) + { + log_error("trie_dict_set(%s) error\n", filename); + + if (shmctl(shmid, IPC_RMID, NULL) == -1) + { + log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); + } - p_file_mmap_pool[file_mmap_index].line_total = - split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES); + return -4; + } return 0; } -int unload_file_mmap(const char *filename) +int unload_file_shm(const char *filename) { - int file_mmap_index = 0; + const void *p_shm; + int shmid; - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) + if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) { log_error("trie_dict_get(%s) not found\n", filename); return -1; } - if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) + shmid = *((int *)p_shm); + + if (shmdt(p_shm) == -1) { - log_error("munmap() error (%d)\n", errno); + log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); } - p_file_mmap_pool[file_mmap_index].p_data = NULL; - p_file_mmap_pool[file_mmap_index].size = 0L; - - if (file_mmap_free_index == -1) + if (shmctl((int)shmid, IPC_RMID, NULL) == -1) { - file_mmap_free_index = file_mmap_index; + log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); } if (trie_dict_del(p_trie_file_dict, filename) != 1) @@ -215,21 +246,26 @@ int unload_file_mmap(const char *filenam return 0; } -const FILE_MMAP *get_file_mmap(const char *filename) +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) { - int file_mmap_index = file_mmap_free_index; + const void *p_shm; - if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL) + if (p_trie_file_dict == NULL) { log_error("File loader not initialized\n"); return NULL; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) // Not exist + if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) // Not exist { log_error("trie_dict_get(%s) not found\n", filename); return NULL; } - return (&(p_file_mmap_pool[file_mmap_index])); + *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); + *pp_line_offsets = *pp_data + *p_data_len + 1; + + return p_shm; }