/[LeafOK_CVS]/lbbs/src/file_loader.c
ViewVC logotype

Contents of /lbbs/src/file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Fri May 16 14:09:31 2025 UTC (10 months ago) by sysadm
Branch: MAIN
Changes since 1.1: +1 -3 lines
Content type: text/x-csrc
Add data files load/reload

1 /***************************************************************************
2 file_loader.c - description
3 -------------------
4 Copyright : (C) 2004-2025 by Leaflet
5 Email : leaflet@leafok.com
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 3 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #include "file_loader.h"
18 #include "trie_dict.h"
19 #include "str_process.h"
20 #include "log.h"
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27
28 static FILE_MMAP *p_file_mmap_pool = NULL;
29 static int file_mmap_count = 0;
30 static int file_mmap_free_index = -1;
31 static TRIE_NODE *p_trie_file_dict = NULL;
32
33 int file_loader_init(int max_file_mmap_count)
34 {
35 if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT)
36 {
37 log_error("file_loader_init(%d) argument error\n", max_file_mmap_count);
38 return -1;
39 }
40
41 if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL)
42 {
43 log_error("File loader already initialized\n");
44 return -1;
45 }
46
47 p_file_mmap_pool = (FILE_MMAP *)calloc((size_t)max_file_mmap_count, sizeof(FILE_MMAP));
48 if (p_file_mmap_pool == NULL)
49 {
50 log_error("calloc(%d p_file_mmap_pool) error\n", max_file_mmap_count);
51 return -2;
52 }
53
54 file_mmap_count = max_file_mmap_count;
55 file_mmap_free_index = 0;
56
57 p_trie_file_dict = trie_dict_create();
58 if (p_trie_file_dict == NULL)
59 {
60 log_error("trie_dict_create() error\n");
61 return -2;
62 }
63
64 return 0;
65 }
66
67 void file_loader_cleanup(void)
68 {
69 if (p_trie_file_dict != NULL)
70 {
71 trie_dict_destroy(p_trie_file_dict);
72 p_trie_file_dict = NULL;
73 }
74
75 if (p_file_mmap_pool != NULL)
76 {
77 for (int i = 0; i < file_mmap_count; i++)
78 {
79 if (p_file_mmap_pool[i].p_data == NULL)
80 {
81 continue;
82 }
83
84 if (munmap(p_file_mmap_pool[i].p_data, p_file_mmap_pool[i].size) < 0)
85 {
86 log_error("munmap() error (%d)\n", errno);
87 }
88 }
89 free(p_file_mmap_pool);
90 p_file_mmap_pool = NULL;
91 }
92
93 file_mmap_count = 0;
94 file_mmap_free_index = -1;
95 }
96
97 int load_file_mmap(const char *filename)
98 {
99 int fd;
100 struct stat sb;
101 void *p_data;
102 size_t size;
103 int file_mmap_index = 0;
104
105 if ((fd = open(filename, O_RDONLY)) < 0)
106 {
107 log_error("open(%s) error (%d)\n", filename, errno);
108 return -1;
109 }
110
111 if (fstat(fd, &sb) < 0)
112 {
113 log_error("fstat(fd) error (%d)\n", errno);
114 return -1;
115 }
116
117 size = (size_t)sb.st_size;
118
119 p_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
120 if (p_data == MAP_FAILED)
121 {
122 log_error("mmap() error (%d)\n", errno);
123 return -2;
124 }
125
126 if (close(fd) < 0)
127 {
128 log_error("close(fd) error (%d)\n", errno);
129 return -1;
130 }
131
132 if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) == 0) // Not exist
133 {
134 if (file_mmap_free_index == -1)
135 {
136 log_error("file_mmap_pool is depleted\n");
137 return -3;
138 }
139
140 file_mmap_index = file_mmap_free_index;
141
142 if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1)
143 {
144 log_error("trie_dict_set(%s) error\n", filename);
145
146 if (munmap(p_data, size) < 0)
147 {
148 log_error("munmap() error (%d)\n", errno);
149 }
150
151 return -2;
152 }
153
154 do
155 {
156 file_mmap_free_index++;
157 if (file_mmap_free_index >= file_mmap_count)
158 {
159 file_mmap_free_index = 0;
160 }
161 if (file_mmap_free_index == file_mmap_index) // loop
162 {
163 file_mmap_free_index = -1;
164 break;
165 }
166 } while (p_file_mmap_pool[file_mmap_free_index].p_data != NULL);
167 }
168 else
169 {
170 // Unload existing data
171 if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)
172 {
173 log_error("munmap() error (%d)\n", errno);
174 }
175 }
176
177 p_file_mmap_pool[file_mmap_index].p_data = p_data;
178 p_file_mmap_pool[file_mmap_index].size = size;
179
180 p_file_mmap_pool[file_mmap_index].line_total =
181 split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES);
182
183 return 0;
184 }
185
186 int unload_file_mmap(const char *filename)
187 {
188 int file_mmap_index = 0;
189
190 if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1)
191 {
192 log_error("trie_dict_get(%s) not found\n", filename);
193 return -1;
194 }
195
196 if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)
197 {
198 log_error("munmap() error (%d)\n", errno);
199 }
200
201 p_file_mmap_pool[file_mmap_index].p_data = NULL;
202 p_file_mmap_pool[file_mmap_index].size = 0L;
203
204 if (file_mmap_free_index == -1)
205 {
206 file_mmap_free_index = file_mmap_index;
207 }
208
209 if (trie_dict_del(p_trie_file_dict, filename) != 1)
210 {
211 log_error("trie_dict_del(%s) error\n", filename);
212 return -2;
213 }
214
215 return 0;
216 }
217
218 const FILE_MMAP *get_file_mmap(const char *filename)
219 {
220 int file_mmap_index = file_mmap_free_index;
221
222 if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL)
223 {
224 log_error("File loader not initialized\n");
225 return NULL;
226 }
227
228 if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) // Not exist
229 {
230 log_error("trie_dict_get(%s) not found\n", filename);
231 return NULL;
232 }
233
234 return (&(p_file_mmap_pool[file_mmap_index]));
235 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1