--- lbbs/src/file_loader.c 2025/05/16 12:22:35 1.1 +++ lbbs/src/file_loader.c 2025/11/04 14:30:44 1.19 @@ -1,61 +1,43 @@ -/*************************************************************************** - file_loader.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * file_loader + * - shared memory based file loader + * + * Copyright (C) 2004-2025 by Leaflet + */ #include "file_loader.h" -#include "trie_dict.h" -#include "str_process.h" #include "log.h" -#include +#include "str_process.h" +#include "trie_dict.h" #include -#include +#include #include +#include +#include +#include +#include #include +#include #include -#define FILE_MMAP_COUNT_LIMIT 256 +struct shm_header_t +{ + int shmid; + size_t data_len; + long line_total; +}; -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; -int file_loader_init(int max_file_mmap_count) +int file_loader_init() { - if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT) - { - log_error("file_loader_init(%d) argument error\n", max_file_mmap_count); - return -1; - } - - if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL) + 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) { @@ -66,43 +48,44 @@ 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) + int shmid = (int)value; + + if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) { - trie_dict_destroy(p_trie_file_dict); - p_trie_file_dict = NULL; + log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); } +} - if (p_file_mmap_pool != NULL) +void file_loader_cleanup(void) +{ + if (p_trie_file_dict == NULL) { - 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; + 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(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; if ((fd = open(filename, O_RDONLY)) < 0) { @@ -116,9 +99,8 @@ int load_file_mmap(const char *filename) return -1; } - size = (size_t)sb.st_size; - - p_data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, 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); @@ -131,81 +113,89 @@ 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, 1, NULL); + + // Allocate shared memory + proj_id = (int)(time(NULL) % getpid()); + key = ftok(filename, proj_id); + if (key == -1) { - if (file_mmap_free_index == -1) - { - log_error("file_mmap_pool is depleted\n"); - return -3; - } + log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); + return -2; + } - file_mmap_index = file_mmap_free_index; + 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 (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1) - { - log_error("trie_dict_set(%s) error\n", filename); + ((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((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len); - if (munmap(p_data, size) < 0) - { - log_error("munmap() error (%d)\n", errno); - } + if (munmap(p_data, data_len) < 0) + { + log_error("munmap() error (%d)\n", errno); + return -2; + } - return -2; - } + p_data = (char *)p_shm + sizeof(struct shm_header_t); + p_line_offsets = (long *)((char *)p_data + data_len + 1); + memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); - 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); + if (shmdt(p_shm) == -1) + { + log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); + return -3; } - else + + if (trie_dict_get(p_trie_file_dict, filename, &shmid_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) + 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)shmid) != 1) + { + log_error("trie_dict_set(%s) error\n", filename); + + if (shmid != 0 && 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(const char *filename) { - int file_mmap_index = 0; + int64_t 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, &shmid) != 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) + if (shmctl((int)shmid, IPC_RMID, NULL) == -1) { - log_error("munmap() error (%d)\n", 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) - { - 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) @@ -217,21 +207,50 @@ int unload_file_mmap(const char *filenam return 0; } -const FILE_MMAP *get_file_mmap(const char *filename) +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) { - int file_mmap_index = file_mmap_free_index; + int64_t shmid; + 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, &shmid) != 1) // Not exist { log_error("trie_dict_get(%s) not found\n", filename); return NULL; } - return (&(p_file_mmap_pool[file_mmap_index])); + 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 = (char *)p_shm + sizeof(struct shm_header_t); + *pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1); + + 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; }