/[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.4 by sysadm, Sat May 17 05:59:14 2025 UTC
# Line 22  Line 22 
22  #include <errno.h>  #include <errno.h>
23  #include <unistd.h>  #include <unistd.h>
24  #include <stdlib.h>  #include <stdlib.h>
25    #include <string.h>
26    #include <time.h>
27  #include <sys/mman.h>  #include <sys/mman.h>
28  #include <sys/stat.h>  #include <sys/stat.h>
29    #include <sys/shm.h>
30    #include <sys/ipc.h>
31    
32    #define MAX_SPLIT_FILE_LINES 65536
33    
 static FILE_MMAP *p_file_mmap_pool = NULL;  
 static int file_mmap_count = 0;  
 static int file_mmap_free_index = -1;  
34  static TRIE_NODE *p_trie_file_dict = NULL;  static TRIE_NODE *p_trie_file_dict = NULL;
35    
36  int file_loader_init(int max_file_mmap_count)  int file_loader_init()
37  {  {
38          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)  
39          {          {
40                  log_error("File loader already initialized\n");                  log_error("File loader already initialized\n");
41                  return -1;                  return -1;
42          }          }
43    
         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;  
   
44          p_trie_file_dict = trie_dict_create();          p_trie_file_dict = trie_dict_create();
45          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
46          {          {
# Line 64  int file_loader_init(int max_file_mmap_c Line 51  int file_loader_init(int max_file_mmap_c
51          return 0;          return 0;
52  }  }
53    
54  void file_loader_cleanup(void)  static void trie_file_dict_cleanup_cb(const char *filename, int64_t shmid)
55  {  {
56          if (p_trie_file_dict != NULL)          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
57          {          {
58                  trie_dict_destroy(p_trie_file_dict);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
                 p_trie_file_dict = NULL;  
59          }          }
60    }
61    
62          if (p_file_mmap_pool != NULL)  void file_loader_cleanup(void)
63    {
64            if (p_trie_file_dict == NULL)
65          {          {
66                  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;  
67          }          }
68    
69          file_mmap_count = 0;          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
70          file_mmap_free_index = -1;          trie_dict_destroy(p_trie_file_dict);
71            p_trie_file_dict = NULL;
72  }  }
73    
74  int load_file_mmap(const char *filename)  int load_file_shm(const char *filename)
75  {  {
76          int fd;          int fd;
77          struct stat sb;          struct stat sb;
78          void *p_data;          void *p_data;
79            size_t data_len;
80            int proj_id;
81            key_t key;
82          size_t size;          size_t size;
83          int file_mmap_index = 0;          int shmid;
84            int64_t shmid_old;
85            void *p_shm;
86            long line_total;
87            long line_offsets[MAX_SPLIT_FILE_LINES];
88            long *p_line_offsets;
89    
90          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
91          {          {
# Line 114  int load_file_mmap(const char *filename) Line 99  int load_file_mmap(const char *filename)
99                  return -1;                  return -1;
100          }          }
101    
102          size = (size_t)sb.st_size;          data_len = (size_t)sb.st_size;
103            p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L);
         p_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);  
104          if (p_data == MAP_FAILED)          if (p_data == MAP_FAILED)
105          {          {
106                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)\n", errno);
# Line 129  int load_file_mmap(const char *filename) Line 113  int load_file_mmap(const char *filename)
113                  return -1;                  return -1;
114          }          }
115    
116          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);
117            if (line_total >= MAX_SPLIT_FILE_LINES)
118          {          {
119                  if (file_mmap_free_index == -1)                  log_error("split_data_lines() truncated over limit lines\n");
120                  {          }
                         log_error("file_mmap_pool is depleted\n");  
                         return -3;  
                 }  
121    
122                  file_mmap_index = file_mmap_free_index;          // Allocate shared memory
123            proj_id = (int)(time(NULL) % getpid());
124            key = ftok(filename, proj_id);
125            if (key == -1)
126            {
127                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
128                    return -2;
129            }
130    
131                  if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1)          size = sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
132                  {          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
133                          log_error("trie_dict_set(%s) error\n", filename);          if (shmid == -1)
134            {
135                    log_error("shmget(size = %d) error (%d)\n", size, errno);
136                    return -3;
137            }
138            p_shm = shmat(shmid, NULL, 0);
139            if (p_shm == (void *)-1)
140            {
141                    log_error("shmat() error (%d)\n", errno);
142                    return -3;
143            }
144    
145                          if (munmap(p_data, size) < 0)          *((size_t *)p_shm) = data_len;
146                          {          *((long *)(p_shm + sizeof(data_len))) = line_total;
147                                  log_error("munmap() error (%d)\n", errno);          memcpy(p_shm + sizeof(data_len) + sizeof(line_total), p_data, data_len);
                         }  
148    
149                          return -2;          if (munmap(p_data, data_len) < 0)
150                  }          {
151                    log_error("munmap() error (%d)\n", errno);
152                    return -2;
153            }
154    
155                  do          p_data = p_shm + sizeof(data_len) + sizeof(line_total);
156                  {          p_line_offsets = p_data + data_len + 1;
157                          file_mmap_free_index++;          memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
158                          if (file_mmap_free_index >= file_mmap_count)  
159                          {          if (shmdt(p_shm) == -1)
160                                  file_mmap_free_index = 0;          {
161                          }                  log_error("shmdt() error (%d)\n", errno);
162                          if (file_mmap_free_index == file_mmap_index) // loop                  return -3;
                         {  
                                 file_mmap_free_index = -1;  
                                 break;  
                         }  
                 } while (p_file_mmap_pool[file_mmap_free_index].p_data != NULL);  
163          }          }
164          else  
165            if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)
166          {          {
167                  // 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)  
168                  {                  {
169                          log_error("munmap() error (%d)\n", errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
170                            return -2;
171                  }                  }
172          }          }
173    
174          p_file_mmap_pool[file_mmap_index].p_data = p_data;          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)
175          p_file_mmap_pool[file_mmap_index].size = size;          {
176                    log_error("trie_dict_set(%s) error\n", filename);
177    
178                    if (shmctl(shmid, IPC_RMID, NULL) == -1)
179                    {
180                            log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
181                    }
182    
183          p_file_mmap_pool[file_mmap_index].line_total =                  return -4;
184                  split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES);          }
185    
186          return 0;          return 0;
187  }  }
188    
189  int unload_file_mmap(const char *filename)  int unload_file_shm(const char *filename)
190  {  {
191          int file_mmap_index = 0;          int64_t shmid = 0;
192    
193          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 *)&shmid) != 1)
194          {          {
195                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
196                  return -1;                  return -1;
197          }          }
198    
199          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)  
200          {          {
201                  file_mmap_free_index = file_mmap_index;                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
202          }          }
203    
204          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 210  int unload_file_mmap(const char *filenam
210          return 0;          return 0;
211  }  }
212    
213  const FILE_MMAP *get_file_mmap(const char *filename)  const void *get_file_shm(const char *filename)
214  {  {
215          int file_mmap_index = file_mmap_free_index;          int64_t shmid;
216            const void *p_shm;
217    
218          if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
219          {          {
220                  log_error("File loader not initialized\n");                  log_error("File loader not initialized\n");
221                  return NULL;                  return NULL;
222          }          }
223    
224          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
225          {          {
226                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
227                  return NULL;                  return NULL;
228          }          }
229    
230          return (&(p_file_mmap_pool[file_mmap_index]));          p_shm = shmat((int)shmid, NULL, SHM_RDONLY);
231            if (p_shm == (void *)-1)
232            {
233                    log_error("shmat() error (%d)\n", errno);
234                    return NULL;
235            }
236    
237            return p_shm;
238  }  }


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

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