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

Diff of /lbbs/src/file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Fri May 16 14:09:31 2025 UTC Revision 1.15 by sysadm, Sat Jun 21 02:15:18 2025 UTC
# Line 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "file_loader.h"  #include "file_loader.h"
 #include "trie_dict.h"  
 #include "str_process.h"  
18  #include "log.h"  #include "log.h"
19  #include <fcntl.h>  #include "str_process.h"
20    #include "trie_dict.h"
21  #include <errno.h>  #include <errno.h>
22  #include <unistd.h>  #include <fcntl.h>
23  #include <stdlib.h>  #include <stdlib.h>
24    #include <string.h>
25    #include <time.h>
26    #include <unistd.h>
27    #include <sys/ipc.h>
28  #include <sys/mman.h>  #include <sys/mman.h>
29    #include <sys/shm.h>
30  #include <sys/stat.h>  #include <sys/stat.h>
31    
32  static FILE_MMAP *p_file_mmap_pool = NULL;  struct shm_header_t
33  static int file_mmap_count = 0;  {
34  static int file_mmap_free_index = -1;          int shmid;
35            size_t data_len;
36            long line_total;
37    };
38    
39  static TRIE_NODE *p_trie_file_dict = NULL;  static TRIE_NODE *p_trie_file_dict = NULL;
40    
41  int file_loader_init(int max_file_mmap_count)  int file_loader_init()
42  {  {
43          if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT)          if (p_trie_file_dict != NULL)
         {  
                 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)  
44          {          {
45                  log_error("File loader already initialized\n");                  log_error("File loader already initialized\n");
46                  return -1;                  return -1;
47          }          }
48    
         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;  
   
49          p_trie_file_dict = trie_dict_create();          p_trie_file_dict = trie_dict_create();
50          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
51          {          {
# Line 64  int file_loader_init(int max_file_mmap_c Line 56  int file_loader_init(int max_file_mmap_c
56          return 0;          return 0;
57  }  }
58    
59  void file_loader_cleanup(void)  static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)
60  {  {
61          if (p_trie_file_dict != NULL)          int shmid = (int)value;
62    
63            if (shmctl(shmid, IPC_RMID, NULL) == -1)
64          {          {
65                  trie_dict_destroy(p_trie_file_dict);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
                 p_trie_file_dict = NULL;  
66          }          }
67    }
68    
69          if (p_file_mmap_pool != NULL)  void file_loader_cleanup(void)
70    {
71            if (p_trie_file_dict == NULL)
72          {          {
73                  for (int i = 0; i < file_mmap_count; i++)                  return;
                 {  
                         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;  
74          }          }
75    
76          file_mmap_count = 0;          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
77          file_mmap_free_index = -1;          trie_dict_destroy(p_trie_file_dict);
78    
79            p_trie_file_dict = NULL;
80  }  }
81    
82  int load_file_mmap(const char *filename)  int load_file(const char *filename)
83  {  {
84          int fd;          int fd;
85          struct stat sb;          struct stat sb;
86          void *p_data;          void *p_data;
87            size_t data_len;
88            int proj_id;
89            key_t key;
90          size_t size;          size_t size;
91          int file_mmap_index = 0;          int shmid;
92            void *p_shm;
93            long line_total;
94            long line_offsets[MAX_SPLIT_FILE_LINES];
95            long *p_line_offsets;
96            int64_t shmid_old;
97    
98          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
99          {          {
# Line 114  int load_file_mmap(const char *filename) Line 107  int load_file_mmap(const char *filename)
107                  return -1;                  return -1;
108          }          }
109    
110          size = (size_t)sb.st_size;          data_len = (size_t)sb.st_size;
111            p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L);
         p_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);  
112          if (p_data == MAP_FAILED)          if (p_data == MAP_FAILED)
113          {          {
114                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)\n", errno);
# Line 129  int load_file_mmap(const char *filename) Line 121  int load_file_mmap(const char *filename)
121                  return -1;                  return -1;
122          }          }
123    
124          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);
125    
126            // Allocate shared memory
127            proj_id = (int)(time(NULL) % getpid());
128            key = ftok(filename, proj_id);
129            if (key == -1)
130          {          {
131                  if (file_mmap_free_index == -1)                  log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
132                  {                  return -2;
133                          log_error("file_mmap_pool is depleted\n");          }
                         return -3;  
                 }  
134    
135                  file_mmap_index = file_mmap_free_index;          size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
136            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
137            if (shmid == -1)
138            {
139                    log_error("shmget(size = %d) error (%d)\n", size, errno);
140                    return -3;
141            }
142            p_shm = shmat(shmid, NULL, 0);
143            if (p_shm == (void *)-1)
144            {
145                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
146                    return -3;
147            }
148    
149                  if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1)          ((struct shm_header_t *)p_shm)->shmid = shmid;
150                  {          ((struct shm_header_t *)p_shm)->data_len = data_len;
151                          log_error("trie_dict_set(%s) error\n", filename);          ((struct shm_header_t *)p_shm)->line_total = line_total;
152            memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len);
153    
154                          if (munmap(p_data, size) < 0)          if (munmap(p_data, data_len) < 0)
155                          {          {
156                                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
157                          }                  return -2;
158            }
159    
160                          return -2;          p_data = p_shm + sizeof(struct shm_header_t);
161                  }          p_line_offsets = p_data + data_len + 1;
162            memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
163    
164                  do          if (shmdt(p_shm) == -1)
165                  {          {
166                          file_mmap_free_index++;                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
167                          if (file_mmap_free_index >= file_mmap_count)                  return -3;
                         {  
                                 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);  
168          }          }
169          else  
170            if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)
171          {          {
172                  // Unload existing data                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
                 if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)  
173                  {                  {
174                          log_error("munmap() error (%d)\n", errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
175                            return -2;
176                  }                  }
177          }          }
178    
179          p_file_mmap_pool[file_mmap_index].p_data = p_data;          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)
180          p_file_mmap_pool[file_mmap_index].size = size;          {
181                    log_error("trie_dict_set(%s) error\n", filename);
182    
183                    if (shmctl(shmid, IPC_RMID, NULL) == -1)
184                    {
185                            log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
186                    }
187    
188          p_file_mmap_pool[file_mmap_index].line_total =                  return -4;
189                  split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES);          }
190    
191          return 0;          return 0;
192  }  }
193    
194  int unload_file_mmap(const char *filename)  int unload_file(const char *filename)
195  {  {
196          int file_mmap_index = 0;          int64_t shmid;
197    
198          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)
199          {          {
200                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
201                  return -1;                  return -1;
202          }          }
203    
204          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)
205          {          {
206                  log_error("munmap() error (%d)\n", errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)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)  
         {  
                 file_mmap_free_index = file_mmap_index;  
207          }          }
208    
209          if (trie_dict_del(p_trie_file_dict, filename) != 1)          if (trie_dict_del(p_trie_file_dict, filename) != 1)
# Line 215  int unload_file_mmap(const char *filenam Line 215  int unload_file_mmap(const char *filenam
215          return 0;          return 0;
216  }  }
217    
218  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)
219  {  {
220          int file_mmap_index = file_mmap_free_index;          int64_t shmid;
221            const void *p_shm;
222    
223          if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
224          {          {
225                  log_error("File loader not initialized\n");                  log_error("File loader not initialized\n");
226                  return NULL;                  return NULL;
227          }          }
228    
229          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
230          {          {
231                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
232                  return NULL;                  return NULL;
233          }          }
234    
235          return (&(p_file_mmap_pool[file_mmap_index]));          p_shm = shmat((int)shmid, NULL, SHM_RDONLY);
236            if (p_shm == (void *)-1)
237            {
238                    log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno);
239                    return NULL;
240            }
241    
242            *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
243            *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
244            *pp_data = p_shm + sizeof(struct shm_header_t);
245            *pp_line_offsets = *pp_data + *p_data_len + 1;
246    
247            return p_shm;
248    }
249    
250    int detach_file_shm(const void *p_shm)
251    {
252            if (p_shm == NULL)
253            {
254                    return -2;
255            }
256    
257            if (shmdt(p_shm) == -1)
258            {
259                    log_error("shmdt() error (%d)\n", errno);
260                    return -1;
261            }
262    
263            return 0;
264  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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