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

Diff of /lbbs/src/trie_dict.c

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

Revision 1.19 by sysadm, Tue Nov 18 15:15:18 2025 UTC Revision 1.20 by sysadm, Wed Nov 19 15:50:13 2025 UTC
# Line 10  Line 10 
10  #include "config.h"  #include "config.h"
11  #endif  #endif
12    
13    #include "common.h"
14  #include "log.h"  #include "log.h"
15  #include "trie_dict.h"  #include "trie_dict.h"
16  #include <errno.h>  #include <errno.h>
17    #include <fcntl.h>
18  #include <stdio.h>  #include <stdio.h>
19  #include <stdlib.h>  #include <stdlib.h>
20  #include <string.h>  #include <string.h>
21  #include <time.h>  #include <time.h>
22  #include <unistd.h>  #include <unistd.h>
23  #include <sys/ipc.h>  #include <sys/mman.h>
24  #include <sys/shm.h>  #include <sys/stat.h>
25    
26  struct trie_node_pool_t  struct trie_node_pool_t
27  {  {
28          int shmid;          size_t shm_size;
29          int node_count_limit;          int node_count_limit;
30          int node_count;          int node_count;
31          TRIE_NODE *p_node_free_list;          TRIE_NODE *p_node_free_list;
32  };  };
33  typedef struct trie_node_pool_t TRIE_NODE_POOL;  typedef struct trie_node_pool_t TRIE_NODE_POOL;
34    
35    static char trie_node_shm_name[FILE_PATH_LEN];
36  static TRIE_NODE_POOL *p_trie_node_pool;  static TRIE_NODE_POOL *p_trie_node_pool;
37    
38  int trie_dict_init(const char *filename, int node_count_limit)  int trie_dict_init(const char *filename, int node_count_limit)
39  {  {
40          int shmid;          char filepath[FILE_PATH_LEN];
41          int proj_id;          int fd;
         key_t key;  
42          size_t size;          size_t size;
43          void *p_shm;          void *p_shm;
44          TRIE_NODE *p_trie_nodes;          TRIE_NODE *p_trie_nodes;
45          int i;          int i;
46    
47            if (filename == NULL)
48            {
49                    log_error("NULL pointer error\n");
50                    return -1;
51            }
52    
53          if (p_trie_node_pool != NULL)          if (p_trie_node_pool != NULL)
54          {          {
55                  log_error("trie_dict_pool already initialized\n");                  log_error("trie_dict_pool already initialized\n");
# Line 55  int trie_dict_init(const char *filename, Line 63  int trie_dict_init(const char *filename,
63          }          }
64    
65          // Allocate shared memory          // Allocate shared memory
66          proj_id = (int)(time(NULL) % getpid());          size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;
67          key = ftok(filename, proj_id);  
68          if (key == -1)          strncpy(filepath, filename, sizeof(filepath) - 1);
69            filepath[sizeof(filepath) - 1] = '\0';
70            snprintf(trie_node_shm_name, sizeof(trie_node_shm_name), "/TRIE_DICT_SHM_%s", basename(filepath));
71    
72            if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
73          {          {
74                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                  log_error("shm_unlink(%s) error (%d)\n", trie_node_shm_name, errno);
75                  return -2;                  return -2;
76          }          }
77    
78          size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;          if ((fd = shm_open(trie_node_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
         shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
         if (shmid == -1)  
79          {          {
80                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("shm_open(%s) error (%d)\n", trie_node_shm_name, errno);
81                  return -3;                  return -2;
82          }          }
83          p_shm = shmat(shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_shm == (void *)-1)  
84          {          {
85                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
86                  return -3;                  close(fd);
87                    return -2;
88            }
89    
90            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
91            if (p_shm == MAP_FAILED)
92            {
93                    log_error("mmap() error (%d)\n", errno);
94                    close(fd);
95                    return -2;
96            }
97    
98            if (close(fd) < 0)
99            {
100                    log_error("close(fd) error (%d)\n", errno);
101                    return -1;
102          }          }
103    
104          p_trie_node_pool = p_shm;          p_trie_node_pool = p_shm;
105          p_trie_node_pool->shmid = shmid;          p_trie_node_pool->shm_size = size;
106          p_trie_node_pool->node_count_limit = node_count_limit;          p_trie_node_pool->node_count_limit = node_count_limit;
107          p_trie_node_pool->node_count = 0;          p_trie_node_pool->node_count = 0;
108    
# Line 93  int trie_dict_init(const char *filename, Line 117  int trie_dict_init(const char *filename,
117          return 0;          return 0;
118  }  }
119    
120  void trie_dict_cleanup(void)  int trie_dict_cleanup(void)
121  {  {
         int shmid;  
   
122          if (p_trie_node_pool == NULL)          if (p_trie_node_pool == NULL)
123          {          {
124                  return;                  return -1;
125          }          }
126    
         shmid = p_trie_node_pool->shmid;  
   
127          detach_trie_dict_shm();          detach_trie_dict_shm();
128    
129          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
130          {          {
131                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shm_unlink(%s) error (%d)\n", trie_node_shm_name, errno);
132                    return -2;
133          }          }
134    
135            trie_node_shm_name[0] = '\0';
136    
137            return 0;
138  }  }
139    
140  int set_trie_dict_shm_readonly(void)  int get_trie_dict_shm_readonly(void)
141  {  {
142  #ifndef __CYGWIN__          int fd;
143          int shmid;          struct stat sb;
144            size_t size;
145          void *p_shm;          void *p_shm;
146    
147          if (p_trie_node_pool == NULL)          if ((fd = shm_open(trie_node_shm_name, O_RDONLY, 0600)) == -1)
148          {          {
149                  log_error("trie_dict_pool not initialized\n");                  log_error("shm_open(%s) error (%d)\n", trie_node_shm_name, errno);
150                  return -1;                  return -2;
151            }
152    
153            if (fstat(fd, &sb) < 0)
154            {
155                    log_error("fstat(fd) error (%d)\n", errno);
156                    close(fd);
157                    return -2;
158          }          }
159    
160          shmid = p_trie_node_pool->shmid;          size = (size_t)sb.st_size;
161    
162          // Remap shared memory in read-only mode          p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
163          p_shm = shmat(shmid, p_trie_node_pool, SHM_RDONLY | SHM_REMAP);          if (p_shm == MAP_FAILED)
         if (p_shm == (void *)-1)  
164          {          {
165                  log_error("shmat(trie_node_pool shmid=%d) error (%d)\n", shmid, errno);                  log_error("mmap() error (%d)\n", errno);
166                    close(fd);
167                    return -2;
168            }
169    
170            if (close(fd) < 0)
171            {
172                    log_error("close(fd) error (%d)\n", errno);
173                  return -1;                  return -1;
174          }          }
175    
176            if (p_trie_node_pool->shm_size != size)
177            {
178                    log_error("Shared memory size mismatch (%ld != %ld)\n", p_trie_node_pool->shm_size, size);
179                    munmap(p_shm, size);
180                    return -3;
181            }
182    
183          p_trie_node_pool = p_shm;          p_trie_node_pool = p_shm;
184  #endif  
185            return 0;
186    }
187    
188    int set_trie_dict_shm_readonly(void)
189    {
190            if (p_trie_node_pool != NULL && munmap(p_trie_node_pool, p_trie_node_pool->shm_size) < 0)
191            {
192                    log_error("munmap() error (%d)\n", errno);
193                    return -2;
194            }
195    
196            if (get_trie_dict_shm_readonly() < 0)
197            {
198                    log_error("get_trie_dict_shm_readonly() error\n");
199                    return -3;
200            }
201    
202          return 0;          return 0;
203  }  }
204    
205  int detach_trie_dict_shm(void)  int detach_trie_dict_shm(void)
206  {  {
207          if (p_trie_node_pool != NULL && shmdt(p_trie_node_pool) == -1)          if (p_trie_node_pool != NULL && munmap(p_trie_node_pool, p_trie_node_pool->shm_size) < 0)
208          {          {
209                  log_error("shmdt(trie_node_pool) error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
210                  return -1;                  return -1;
211          }          }
212    


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

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