/[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.8 by sysadm, Sun May 18 08:17:25 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  static FILE_MMAP *p_file_mmap_pool = NULL;  #define MAX_SPLIT_FILE_LINES 65536
 static int file_mmap_count = 0;  
 static int file_mmap_free_index = -1;  
 static TRIE_NODE *p_trie_file_dict = NULL;  
33    
34  int file_loader_init(int max_file_mmap_count)  struct shm_header_t
35  {  {
36          if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT)          int shmid;
37          {          size_t data_len;
38                  log_error("file_loader_init(%d) argument error\n", max_file_mmap_count);          long line_total;
39                  return -1;  };
         }  
40    
41          if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL)  static TRIE_NODE *p_trie_file_dict = NULL;
42    
43    int file_loader_init()
44    {
45            if (p_trie_file_dict != NULL)
46          {          {
47                  log_error("File loader already initialized\n");                  log_error("File loader already initialized\n");
48                  return -1;                  return -1;
49          }          }
50    
         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;  
   
51          p_trie_file_dict = trie_dict_create();          p_trie_file_dict = trie_dict_create();
52          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
53          {          {
# Line 64  int file_loader_init(int max_file_mmap_c Line 58  int file_loader_init(int max_file_mmap_c
58          return 0;          return 0;
59  }  }
60    
61  void file_loader_cleanup(void)  static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)
62  {  {
63          if (p_trie_file_dict != NULL)          const void *p_shm = (const void *)value;
64            int shmid = *((int *)p_shm);
65    
66            if (shmdt(p_shm) == -1)
67          {          {
68                  trie_dict_destroy(p_trie_file_dict);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
                 p_trie_file_dict = NULL;  
69          }          }
70    
71          if (p_file_mmap_pool != NULL)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
72          {          {
73                  for (int i = 0; i < file_mmap_count; i++)                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
                 {  
                         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    
77          file_mmap_count = 0;  void file_loader_cleanup(void)
78          file_mmap_free_index = -1;  {
79            if (p_trie_file_dict == NULL)
80            {
81                    return;
82            }
83    
84            trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
85    
86            trie_dict_destroy(p_trie_file_dict);
87            p_trie_file_dict = NULL;
88  }  }
89    
90  int load_file_mmap(const char *filename)  int load_file_shm(const char *filename)
91  {  {
92          int fd;          int fd;
93          struct stat sb;          struct stat sb;
94          void *p_data;          void *p_data;
95            size_t data_len;
96            int proj_id;
97            key_t key;
98          size_t size;          size_t size;
99          int file_mmap_index = 0;          int shmid;
100            void *p_shm;
101            long line_total;
102            long line_offsets[MAX_SPLIT_FILE_LINES];
103            long *p_line_offsets;
104            int64_t shmid_old;
105            void *p_shm_old;
106    
107          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
108          {          {
# Line 114  int load_file_mmap(const char *filename) Line 116  int load_file_mmap(const char *filename)
116                  return -1;                  return -1;
117          }          }
118    
119          size = (size_t)sb.st_size;          data_len = (size_t)sb.st_size;
120            p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L);
         p_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);  
121          if (p_data == MAP_FAILED)          if (p_data == MAP_FAILED)
122          {          {
123                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)\n", errno);
# Line 129  int load_file_mmap(const char *filename) Line 130  int load_file_mmap(const char *filename)
130                  return -1;                  return -1;
131          }          }
132    
133          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);
134            if (line_total >= MAX_SPLIT_FILE_LINES)
135          {          {
136                  if (file_mmap_free_index == -1)                  log_error("split_data_lines() truncated over limit lines\n");
137                  {          }
                         log_error("file_mmap_pool is depleted\n");  
                         return -3;  
                 }  
138    
139                  file_mmap_index = file_mmap_free_index;          // Allocate shared memory
140            proj_id = (int)(time(NULL) % getpid());
141            key = ftok(filename, proj_id);
142            if (key == -1)
143            {
144                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
145                    return -2;
146            }
147    
148                  if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1)          size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
149                  {          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
150                          log_error("trie_dict_set(%s) error\n", filename);          if (shmid == -1)
151            {
152                    log_error("shmget(size = %d) error (%d)\n", size, errno);
153                    return -3;
154            }
155            p_shm = shmat(shmid, NULL, 0);
156            if (p_shm == (void *)-1)
157            {
158                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
159                    return -3;
160            }
161    
162                          if (munmap(p_data, size) < 0)          ((struct shm_header_t *)p_shm)->shmid = shmid;
163                          {          ((struct shm_header_t *)p_shm)->data_len = data_len;
164                                  log_error("munmap() error (%d)\n", errno);          ((struct shm_header_t *)p_shm)->line_total = line_total;
165                          }          memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len);
166    
167                          return -2;          if (munmap(p_data, data_len) < 0)
168            {
169                    log_error("munmap() error (%d)\n", errno);
170                    return -2;
171            }
172    
173            p_data = p_shm + sizeof(struct shm_header_t);
174            p_line_offsets = p_data + data_len + 1;
175            memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
176    
177            if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1)
178            {
179                    shmid_old = *((int *)p_shm_old);
180    
181                    if (shmdt(p_shm_old) == -1)
182                    {
183                            log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno);
184                            return -3;
185                  }                  }
186    
187                  do                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
188                  {                  {
189                          file_mmap_free_index++;                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
190                          if (file_mmap_free_index >= file_mmap_count)                          return -2;
191                          {                  }
                                 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);  
192          }          }
193          else  
194            if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1)
195          {          {
196                  // Unload existing data                  log_error("trie_dict_set(%s) error\n", filename);
197                  if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)  
198                    if (shmctl(shmid, IPC_RMID, NULL) == -1)
199                  {                  {
200                          log_error("munmap() error (%d)\n", errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
201                  }                  }
         }  
202    
203          p_file_mmap_pool[file_mmap_index].p_data = p_data;                  return -4;
204          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);  
205    
206          return 0;          return 0;
207  }  }
208    
209  int unload_file_mmap(const char *filename)  int unload_file_shm(const char *filename)
210  {  {
211          int file_mmap_index = 0;          const void *p_shm;
212            int shmid;
213    
214          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)
215          {          {
216                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
217                  return -1;                  return -1;
218          }          }
219    
220          if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0)          shmid = *((int *)p_shm);
221    
222            if (shmdt(p_shm) == -1)
223          {          {
224                  log_error("munmap() error (%d)\n", errno);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
225          }          }
226    
227          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)  
228          {          {
229                  file_mmap_free_index = file_mmap_index;                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
230          }          }
231    
232          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 238  int unload_file_mmap(const char *filenam
238          return 0;          return 0;
239  }  }
240    
241  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)
242  {  {
243          int file_mmap_index = file_mmap_free_index;          const void *p_shm;
244    
245          if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
246          {          {
247                  log_error("File loader not initialized\n");                  log_error("File loader not initialized\n");
248                  return NULL;                  return NULL;
249          }          }
250    
251          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
252          {          {
253                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
254                  return NULL;                  return NULL;
255          }          }
256    
257          return (&(p_file_mmap_pool[file_mmap_index]));          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
258            *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
259            *pp_data = p_shm + sizeof(struct shm_header_t);
260            *pp_line_offsets = *pp_data + *p_data_len + 1;
261    
262            return p_shm;
263  }  }


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

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