/[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.7 by sysadm, Sun May 18 07:41:34 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 value)
55  {  {
56          if (p_trie_file_dict != NULL)          const void *p_shm = (const void *)value;
57            int shmid = *((int *)p_shm);
58    
59            if (shmdt(p_shm) == -1)
60          {          {
61                  trie_dict_destroy(p_trie_file_dict);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
                 p_trie_file_dict = NULL;  
62          }          }
63    
64          if (p_file_mmap_pool != NULL)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
65          {          {
66                  for (int i = 0; i < file_mmap_count; i++)                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
67                  {          }
68                          if (p_file_mmap_pool[i].p_data == NULL)  }
69                          {  
70                                  continue;  void file_loader_cleanup(void)
71                          }  {
72            if (p_trie_file_dict == NULL)
73                          if (munmap(p_file_mmap_pool[i].p_data, p_file_mmap_pool[i].size) < 0)          {
74                          {                  return;
                                 log_error("munmap() error (%d)\n", errno);  
                         }  
                 }  
                 free(p_file_mmap_pool);  
                 p_file_mmap_pool = NULL;  
75          }          }
76    
77          file_mmap_count = 0;          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
78          file_mmap_free_index = -1;  
79            trie_dict_destroy(p_trie_file_dict);
80            p_trie_file_dict = NULL;
81  }  }
82    
83  int load_file_mmap(const char *filename)  int load_file_shm(const char *filename)
84  {  {
85          int fd;          int fd;
86          struct stat sb;          struct stat sb;
87          void *p_data;          void *p_data;
88            size_t data_len;
89            int proj_id;
90            key_t key;
91          size_t size;          size_t size;
92          int file_mmap_index = 0;          int shmid;
93            void *p_shm;
94            long line_total;
95            long line_offsets[MAX_SPLIT_FILE_LINES];
96            long *p_line_offsets;
97            int64_t shmid_old;
98            void *p_shm_old;
99    
100          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
101          {          {
# Line 114  int load_file_mmap(const char *filename) Line 109  int load_file_mmap(const char *filename)
109                  return -1;                  return -1;
110          }          }
111    
112          size = (size_t)sb.st_size;          data_len = (size_t)sb.st_size;
113            p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L);
         p_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);  
114          if (p_data == MAP_FAILED)          if (p_data == MAP_FAILED)
115          {          {
116                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)\n", errno);
# Line 129  int load_file_mmap(const char *filename) Line 123  int load_file_mmap(const char *filename)
123                  return -1;                  return -1;
124          }          }
125    
126          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);
127            if (line_total >= MAX_SPLIT_FILE_LINES)
128          {          {
129                  if (file_mmap_free_index == -1)                  log_error("split_data_lines() truncated over limit lines\n");
130                  {          }
131                          log_error("file_mmap_pool is depleted\n");  
132                          return -3;          // Allocate shared memory
133                  }          proj_id = (int)(time(NULL) % getpid());
134            key = ftok(filename, proj_id);
135            if (key == -1)
136            {
137                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
138                    return -2;
139            }
140    
141                  file_mmap_index = file_mmap_free_index;          size = sizeof(shmid) + sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
142            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
143            if (shmid == -1)
144            {
145                    log_error("shmget(size = %d) error (%d)\n", size, errno);
146                    return -3;
147            }
148            p_shm = shmat(shmid, NULL, 0);
149            if (p_shm == (void *)-1)
150            {
151                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
152                    return -3;
153            }
154    
155                  if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1)          *((int *)p_shm) = shmid;
156                  {          *((size_t *)(p_shm + sizeof(shmid))) = data_len;
157                          log_error("trie_dict_set(%s) error\n", filename);          *((long *)(p_shm + sizeof(shmid) + sizeof(data_len))) = line_total;
158            memcpy(p_shm + sizeof(shmid) + sizeof(data_len) + sizeof(line_total), p_data, data_len);
159    
160                          if (munmap(p_data, size) < 0)          if (munmap(p_data, data_len) < 0)
161                          {          {
162                                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
163                          }                  return -2;
164            }
165    
166                          return -2;          p_data = p_shm + sizeof(int) + sizeof(data_len) + sizeof(line_total);
167            p_line_offsets = p_data + data_len + 1;
168            memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
169    
170            if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1)
171            {
172                    shmid_old = *((int *)p_shm_old);
173    
174                    if (shmdt(p_shm_old) == -1)
175                    {
176                            log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno);
177                            return -3;
178                  }                  }
179    
180                  do                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
181                  {                  {
182                          file_mmap_free_index++;                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
183                          if (file_mmap_free_index >= file_mmap_count)                          return -2;
184                          {                  }
                                 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);  
185          }          }
186          else  
187            if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1)
188          {          {
189                  // Unload existing data                  log_error("trie_dict_set(%s) error\n", filename);
190                  if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)  
191                    if (shmctl(shmid, IPC_RMID, NULL) == -1)
192                  {                  {
193                          log_error("munmap() error (%d)\n", errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
194                  }                  }
         }  
195    
196          p_file_mmap_pool[file_mmap_index].p_data = p_data;                  return -4;
197          p_file_mmap_pool[file_mmap_index].size = size;          }
   
         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);  
198    
199          return 0;          return 0;
200  }  }
201    
202  int unload_file_mmap(const char *filename)  int unload_file_shm(const char *filename)
203  {  {
204          int file_mmap_index = 0;          const void *p_shm;
205            int shmid;
206    
207          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)
208          {          {
209                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
210                  return -1;                  return -1;
211          }          }
212    
213          if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)          shmid = *((int *)p_shm);
214    
215            if (shmdt(p_shm) == -1)
216          {          {
217                  log_error("munmap() error (%d)\n", errno);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
218          }          }
219    
220          p_file_mmap_pool[file_mmap_index].p_data = NULL;          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
         p_file_mmap_pool[file_mmap_index].size = 0L;  
   
         if (file_mmap_free_index == -1)  
221          {          {
222                  file_mmap_free_index = file_mmap_index;                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
223          }          }
224    
225          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 231  int unload_file_mmap(const char *filenam
231          return 0;          return 0;
232  }  }
233    
234  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)
235  {  {
236          int file_mmap_index = file_mmap_free_index;          const void *p_shm;
237    
238          if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
239          {          {
240                  log_error("File loader not initialized\n");                  log_error("File loader not initialized\n");
241                  return NULL;                  return NULL;
242          }          }
243    
244          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
245          {          {
246                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
247                  return NULL;                  return NULL;
248          }          }
249    
250          return (&(p_file_mmap_pool[file_mmap_index]));          *p_data_len = *((size_t *)(p_shm + sizeof(int)));
251            *p_line_total = *((long *)(p_shm + sizeof(int) + sizeof(size_t)));
252            *pp_data = p_shm + sizeof(int) + sizeof(size_t) + sizeof(long);
253            *pp_line_offsets = *pp_data + *p_data_len + 1;
254    
255            return p_shm;
256  }  }


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

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