/[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.3 by sysadm, Sat May 17 05:54:42 2025 UTC Revision 1.7 by sysadm, Sun May 18 07:41:34 2025 UTC
# Line 51  int file_loader_init() Line 51  int file_loader_init()
51          return 0;          return 0;
52  }  }
53    
54  static void trie_file_dict_cleanup_cb(const char *filename, int64_t shmid)  static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)
55  {  {
56          log_std("Cleanup: %s %ld\n", filename, shmid);          const void *p_shm = (const void *)value;
57          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          int shmid = *((int *)p_shm);
58    
59            if (shmdt(p_shm) == -1)
60          {          {
61                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
62            }
63    
64            if (shmctl(shmid, IPC_RMID, NULL) == -1)
65            {
66                    log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
67          }          }
68  }  }
69    
# Line 68  void file_loader_cleanup(void) Line 75  void file_loader_cleanup(void)
75          }          }
76    
77          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
78    
79          trie_dict_destroy(p_trie_file_dict);          trie_dict_destroy(p_trie_file_dict);
80          p_trie_file_dict = NULL;          p_trie_file_dict = NULL;
81  }  }
# Line 82  int load_file_shm(const char *filename) Line 90  int load_file_shm(const char *filename)
90          key_t key;          key_t key;
91          size_t size;          size_t size;
92          int shmid;          int shmid;
         int64_t shmid_old;  
93          void *p_shm;          void *p_shm;
94          long line_total;          long line_total;
95          long line_offsets[MAX_SPLIT_FILE_LINES];          long line_offsets[MAX_SPLIT_FILE_LINES];
96          long *p_line_offsets;          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 129  int load_file_shm(const char *filename) Line 138  int load_file_shm(const char *filename)
138                  return -2;                  return -2;
139          }          }
140    
141          size = sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);          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);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
143          if (shmid == -1)          if (shmid == -1)
144          {          {
# Line 139  int load_file_shm(const char *filename) Line 148  int load_file_shm(const char *filename)
148          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
149          if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
150          {          {
151                  log_error("shmat() error (%d)\n", errno);                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
152                  return -3;                  return -3;
153          }          }
154    
155          *((size_t *)p_shm) = data_len;          *((int *)p_shm) = shmid;
156          *((long *)(p_shm + sizeof(data_len))) = line_total;          *((size_t *)(p_shm + sizeof(shmid))) = data_len;
157          memcpy(p_shm + sizeof(data_len) + sizeof(line_total), p_data, data_len);          *((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, data_len) < 0)          if (munmap(p_data, data_len) < 0)
161          {          {
# Line 153  int load_file_shm(const char *filename) Line 163  int load_file_shm(const char *filename)
163                  return -2;                  return -2;
164          }          }
165    
166          p_data = p_shm + sizeof(data_len) + sizeof(line_total);          p_data = p_shm + sizeof(int) + sizeof(data_len) + sizeof(line_total);
167          p_line_offsets = p_data + data_len + 1;          p_line_offsets = p_data + data_len + 1;
168          memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));          memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
169    
170          if (shmdt(p_shm) == -1)          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1)
171          {          {
172                  log_error("shmdt() error (%d)\n", errno);                  shmid_old = *((int *)p_shm_old);
173                  return -3;  
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    
         if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)  
         {  
180                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
181                  {                  {
182                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
# Line 172  int load_file_shm(const char *filename) Line 184  int load_file_shm(const char *filename)
184                  }                  }
185          }          }
186    
187          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1)
188          {          {
189                  log_error("trie_dict_set(%s) error\n", filename);                  log_error("trie_dict_set(%s) error\n", filename);
190    
# Line 189  int load_file_shm(const char *filename) Line 201  int load_file_shm(const char *filename)
201    
202  int unload_file_shm(const char *filename)  int unload_file_shm(const char *filename)
203  {  {
204          int64_t shmid = 0;          const void *p_shm;
205            int shmid;
206    
207          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&shmid) != 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            shmid = *((int *)p_shm);
214    
215            if (shmdt(p_shm) == -1)
216            {
217                    log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
218            }
219    
220          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
221          {          {
222                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
# Line 211  int unload_file_shm(const char *filename Line 231  int unload_file_shm(const char *filename
231          return 0;          return 0;
232  }  }
233    
234  const void *get_file_shm(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  {  {
         int64_t shmid;  
236          const void *p_shm;          const void *p_shm;
237    
238          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
# Line 222  const void *get_file_shm(const char *fil Line 241  const void *get_file_shm(const char *fil
241                  return NULL;                  return NULL;
242          }          }
243    
244          if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 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          p_shm = shmat((int)shmid, NULL, SHM_RDONLY);          *p_data_len = *((size_t *)(p_shm + sizeof(int)));
251          if (p_shm == (void *)-1)          *p_line_total = *((long *)(p_shm + sizeof(int) + sizeof(size_t)));
252          {          *pp_data = p_shm + sizeof(int) + sizeof(size_t) + sizeof(long);
253                  log_error("shmat() error (%d)\n", errno);          *pp_line_offsets = *pp_data + *p_data_len + 1;
                 return NULL;  
         }  
254    
255          return p_shm;          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