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

Diff of /lbbs/src/section_list.c

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

Revision 1.4 by sysadm, Wed May 21 09:09:30 2025 UTC Revision 1.40 by sysadm, Mon Oct 13 02:23:27 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list.h"  
17  #include "log.h"  #include "log.h"
18    #include "section_list.h"
19  #include "trie_dict.h"  #include "trie_dict.h"
20    #include <errno.h>
21    #include <signal.h>
22  #include <stdio.h>  #include <stdio.h>
23    #include <stdlib.h>
24  #include <string.h>  #include <string.h>
 #include <signal.h>  
25  #include <unistd.h>  #include <unistd.h>
 #include <stdlib.h>  
 #include <errno.h>  
 #include <sys/shm.h>  
26  #include <sys/ipc.h>  #include <sys/ipc.h>
27    #include <sys/param.h>
28    #include <sys/sem.h>
29    #include <sys/shm.h>
30    
31    #ifdef _SEM_SEMUN_UNDEFINED
32    union semun
33    {
34            int val;                           /* Value for SETVAL */
35            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
36            unsigned short *array; /* Array for GETALL, SETALL */
37            struct seminfo *__buf; /* Buffer for IPC_INFO
38                                                              (Linux-specific) */
39    };
40    #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42    #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
43    #define SECTION_TRY_LOCK_TIMES 10
44    
45    #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
46    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
47    #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
48    
49  // ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT should be  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections
50  // no less than BBS_article_block_limit_per_section * BBS_max_section,  
51  // in order to allocate enough memory for blocks  #define SID_STR_LEN 5 // 32-bit + NULL
52  #define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  
53  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  struct article_block_t
54    {
55            ARTICLE articles[ARTICLE_PER_BLOCK];
56            int article_count;
57            struct article_block_t *p_next_block;
58    };
59    typedef struct article_block_t ARTICLE_BLOCK;
60    
61  struct article_block_shm_t  struct article_block_pool_t
62  {  {
63          int shmid;          int shmid;
64          void *p_shm;          struct
65            {
66                    int shmid;
67                    void *p_shm;
68            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
69            int shm_count;
70            ARTICLE_BLOCK *p_block_free_list;
71            ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
72            int block_count;
73  };  };
74  typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
75    
76  static ARTICLE_BLOCK_SHM *p_article_block_shm_pool;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
 static int article_block_shm_count;  
 static ARTICLE_BLOCK *p_article_block_free_list;  
77    
78  static SECTION_DATA *p_section_data_pool;  SECTION_LIST_POOL *p_section_list_pool = NULL;
 static int section_data_count;  
 static TRIE_NODE *p_trie_dict_section_data;  
79    
80  int section_data_pool_init(const char *filename, int article_block_count)  int article_block_init(const char *filename, int block_count)
81  {  {
82          int shmid;          int shmid;
83          int proj_id;          int proj_id;
# Line 55  int section_data_pool_init(const char *f Line 85  int section_data_pool_init(const char *f
85          size_t size;          size_t size;
86          void *p_shm;          void *p_shm;
87          int i;          int i;
88          int article_block_count_in_shm;          int block_count_in_shm;
89          ARTICLE_BLOCK *p_article_block_in_shm;          ARTICLE_BLOCK *p_block_in_shm;
90          ARTICLE_BLOCK **pp_article_block_next;          ARTICLE_BLOCK **pp_block_next;
91    
92          if (p_article_block_shm_pool != NULL ||          if (p_article_block_pool != NULL)
                 p_article_block_free_list != NULL ||  
                 p_section_data_pool != NULL ||  
                 p_trie_dict_section_data != NULL)  
93          {          {
94                  log_error("section_data_pool already initialized\n");                  log_error("article_block_pool already initialized\n");
95                  return -1;                  return -1;
96          }          }
97    
98          if (article_block_count > ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
99          {          {
100                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
101                  return -2;                  return -2;
102          }          }
103    
104          p_article_block_shm_pool = calloc((size_t)article_block_count / ARTICLE_BLOCK_PER_SHM + 1, sizeof(ARTICLE_BLOCK_SHM));          // Allocate shared memory
105          if (p_article_block_shm_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
106            key = ftok(filename, proj_id);
107            if (key == -1)
108          {          {
109                  log_error("calloc(%d ARTICLE_BLOCK_SHM) OOM\n", article_block_count / ARTICLE_BLOCK_PER_SHM + 1);                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
110                  return -2;                  return -3;
111          }          }
112    
113          p_section_data_pool = calloc(BBS_max_section, sizeof(SECTION_DATA));          size = sizeof(ARTICLE_BLOCK_POOL);
114          if (p_section_data_pool == NULL)          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
115            if (shmid == -1)
116          {          {
117                  log_error("calloc(%d SECTION_DATA) OOM\n", BBS_max_section);                  log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
118                  return -2;                  return -3;
119          }          }
120          section_data_count = 0;          p_shm = shmat(shmid, NULL, 0);
121            if (p_shm == (void *)-1)
         p_trie_dict_section_data = trie_dict_create();  
         if (p_trie_dict_section_data == NULL)  
122          {          {
123                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
124                  return -2;                  return -3;
125          }          }
126    
127          // Allocate shared memory          p_article_block_pool = p_shm;
128          article_block_shm_count = 0;          p_article_block_pool->shmid = shmid;
129          pp_article_block_next = &p_article_block_free_list;  
130            p_article_block_pool->shm_count = 0;
131            pp_block_next = &(p_article_block_pool->p_block_free_list);
132    
133          while (article_block_count > 0)          while (block_count > 0)
134          {          {
135                  article_block_count_in_shm =                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
136                          (article_block_count < ARTICLE_BLOCK_PER_SHM ? article_block_count : ARTICLE_BLOCK_PER_SHM);                  block_count -= block_count_in_shm;
                 article_block_count -= article_block_count_in_shm;  
137    
138                  proj_id = getpid() + article_block_shm_count;                  proj_id = p_article_block_pool->shm_count;
139                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
140                  if (key == -1)                  if (key == -1)
141                  {                  {
142                          log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);                          log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
143                          section_data_pool_cleanup();                          article_block_cleanup();
144                          return -3;                          return -3;
145                  }                  }
146    
147                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)article_block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
148                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
149                  if (shmid == -1)                  if (shmid == -1)
150                  {                  {
151                          log_error("shmget(shm_index = %d, size = %d) error (%d)\n", article_block_shm_count, size, errno);                          log_error("shmget(shm_index = %d, size = %d) error (%d)\n", p_article_block_pool->shm_count, size, errno);
152                          section_data_pool_cleanup();                          article_block_cleanup();
153                          return -3;                          return -3;
154                  }                  }
155                  p_shm = shmat(shmid, NULL, 0);                  p_shm = shmat(shmid, NULL, 0);
156                  if (p_shm == (void *)-1)                  if (p_shm == (void *)-1)
157                  {                  {
158                          log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                          log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
159                          section_data_pool_cleanup();                          article_block_cleanup();
160                          return -3;                          return -3;
161                  }                  }
162    
163                  (p_article_block_shm_pool + article_block_shm_count)->shmid = shmid;                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid;
164                  (p_article_block_shm_pool + article_block_shm_count)->p_shm = p_shm;                  (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm;
165                  article_block_shm_count++;                  p_article_block_pool->shm_count++;
166    
167                  p_article_block_in_shm = p_shm;                  p_block_in_shm = p_shm;
168                  *pp_article_block_next = p_article_block_in_shm;                  *pp_block_next = p_block_in_shm;
169    
170                  for (i = 0; i < article_block_count_in_shm; i++)                  for (i = 0; i < block_count_in_shm; i++)
171                  {                  {
172                          if (i < article_block_count_in_shm - 1)                          if (i < block_count_in_shm - 1)
173                          {                          {
174                                  (p_article_block_in_shm + i)->p_next_block = (p_article_block_in_shm + i + 1);                                  (p_block_in_shm + i)->p_next_block = (p_block_in_shm + i + 1);
175                          }                          }
176                          else                          else
177                          {                          {
178                                  (p_article_block_in_shm + i)->p_next_block = NULL;                                  (p_block_in_shm + i)->p_next_block = NULL;
179                                  pp_article_block_next = &((p_article_block_in_shm + i)->p_next_block);                                  pp_block_next = &((p_block_in_shm + i)->p_next_block);
180                          }                          }
181                  }                  }
182          }          }
183    
184            p_article_block_pool->block_count = 0;
185    
186          return 0;          return 0;
187  }  }
188    
189  void section_data_pool_cleanup(void)  void article_block_cleanup(void)
190  {  {
191          int i;          int shmid;
192    
193          if (p_trie_dict_section_data != NULL)          if (p_article_block_pool == NULL)
194          {          {
195                  trie_dict_destroy(p_trie_dict_section_data);                  return;
                 p_trie_dict_section_data = NULL;  
                 section_data_count = 0;  
196          }          }
197    
198          if (p_section_data_pool != NULL)          for (int i = 0; i < p_article_block_pool->shm_count; i++)
199          {          {
200                  free(p_section_data_pool);                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
201                  p_section_data_pool = NULL;                  {
202                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
203                    }
204    
205                    if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)
206                    {
207                            log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
208                    }
209          }          }
210    
211          if (p_article_block_free_list != NULL)          shmid = p_article_block_pool->shmid;
212    
213            if (shmdt(p_article_block_pool) == -1)
214          {          {
215                  p_article_block_free_list = NULL;                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
216          }          }
217    
218          if (p_article_block_shm_pool != NULL)          if (shmctl(shmid, IPC_RMID, NULL) == -1)
219          {          {
220                  for (i = 0; i < article_block_shm_count; i++)                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
221            }
222    
223            p_article_block_pool = NULL;
224    }
225    
226    int set_article_block_shm_readonly(void)
227    {
228            int shmid;
229            void *p_shm;
230            int i;
231    
232            if (p_article_block_pool == NULL)
233            {
234                    log_error("article_block_pool not initialized\n");
235                    return -1;
236            }
237    
238            for (i = 0; i < p_article_block_pool->shm_count; i++)
239            {
240                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
241    
242                    // Remap shared memory in read-only mode
243                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
244                    if (p_shm == (void *)-1)
245                  {                  {
246                          if (shmdt((p_article_block_shm_pool + i)->p_shm) == -1)                          log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
247                          {                          return -2;
248                                  log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_shm_pool + i)->shmid, errno);                  }
249                          }          }
250    
251                          if (shmctl((p_article_block_shm_pool + i)->shmid, IPC_RMID, NULL) == -1)          return 0;
252                          {  }
253                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_shm_pool + i)->shmid, errno);  
254                          }  int detach_article_block_shm(void)
255    {
256            int shmid;
257    
258            if (p_article_block_pool == NULL)
259            {
260                    return -1;
261            }
262    
263            for (int i = 0; i < p_article_block_pool->shm_count; i++)
264            {
265                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
266                    {
267                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
268                            return -2;
269                  }                  }
270            }
271    
272            shmid = p_article_block_pool->shmid;
273    
274                  p_article_block_shm_pool = NULL;          if (shmdt(p_article_block_pool) == -1)
275                  article_block_shm_count = 0;          {
276                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
277                    return -3;
278          }          }
279    
280            p_article_block_pool = NULL;
281    
282            return 0;
283  }  }
284    
285  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
286  {  {
287          ARTICLE_BLOCK *p_article_block = NULL;          ARTICLE_BLOCK *p_block = NULL;
288    
289          if (p_article_block_free_list != NULL)          if (p_article_block_pool->p_block_free_list != NULL)
290          {          {
291                  p_article_block = p_article_block_free_list;                  p_block = p_article_block_pool->p_block_free_list;
292                  p_article_block_free_list = p_article_block_free_list->p_next_block;                  p_article_block_pool->p_block_free_list = p_block->p_next_block;
293                    p_block->p_next_block = NULL;
294                    p_block->article_count = 0;
295          }          }
296    
297          return p_article_block;          return p_block;
298  }  }
299    
300  inline static void push_free_article_block(ARTICLE_BLOCK *p_article_block)  inline static void push_free_article_block(ARTICLE_BLOCK *p_block)
301  {  {
302          p_article_block->p_next_block = p_article_block_free_list;          p_block->p_next_block = p_article_block_pool->p_block_free_list;
303          p_article_block_free_list = p_article_block;          p_article_block_pool->p_block_free_list = p_block;
304  }  }
305    
306  SECTION_DATA *section_data_create(const char *sname, const char *stitle, const char *master_name)  int article_block_reset(void)
307  {  {
308          SECTION_DATA *p_section;          ARTICLE_BLOCK *p_block;
         int index;  
309    
310          if (p_section_data_pool == NULL || p_trie_dict_section_data == NULL)          if (p_article_block_pool == NULL)
311          {          {
312                  log_error("section_data not initialized\n");                  log_error("article_block_pool not initialized\n");
313                    return -1;
314            }
315    
316            while (p_article_block_pool->block_count > 0)
317            {
318                    p_article_block_pool->block_count--;
319                    p_block = p_article_block_pool->p_block[p_article_block_pool->block_count];
320                    push_free_article_block(p_block);
321            }
322    
323            return 0;
324    }
325    
326    ARTICLE *article_block_find_by_aid(int32_t aid)
327    {
328            ARTICLE_BLOCK *p_block;
329            int left;
330            int right;
331            int mid;
332    
333            if (p_article_block_pool == NULL)
334            {
335                    log_error("article_block_pool not initialized\n");
336                  return NULL;                  return NULL;
337          }          }
338    
339          if (section_data_count >= BBS_max_section)          if (p_article_block_pool->block_count == 0) // empty
340          {          {
                 log_error("section_data_count exceed limit %d\n", BBS_max_section);  
341                  return NULL;                  return NULL;
342          }          }
343    
344          index = section_data_count;          left = 0;
345          p_section = p_section_data_pool + index;          right = p_article_block_pool->block_count;
346    
347          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]
348          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          while (left < right - 1)
349            {
350                    // get block offset no less than mid value of left and right block offsets
351                    mid = (left + right) / 2 + (right - left) % 2;
352    
353          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));                  if (mid >= p_article_block_pool->block_count)
354          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';                  {
355                            log_error("block(mid = %d) is out of boundary\n", mid);
356                            return NULL;
357                    }
358    
359                    if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
360                    {
361                            right = mid;
362                    }
363                    else
364                    {
365                            left = mid;
366                    }
367            }
368    
369          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          p_block = p_article_block_pool->p_block[left];
         p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';  
370    
371          p_section->p_head_block = NULL;          left = 0;
372          p_section->p_tail_block = NULL;          right = p_block->article_count - 1;
         p_section->block_count = 0;  
         p_section->article_count = 0;  
         p_section->delete_count = 0;  
373    
374          if (trie_dict_set(p_trie_dict_section_data, sname, index) != 1)          // aid in the range [ aid of articles[left], aid of articles[right] ]
375            while (left < right)
376          {          {
377                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, index);                  mid = (left + right) / 2;
378                  return NULL;  
379                    if (aid <= p_block->articles[mid].aid)
380                    {
381                            right = mid;
382                    }
383                    else
384                    {
385                            left = mid + 1;
386                    }
387          }          }
388    
389          section_data_count++;          if (aid != p_block->articles[left].aid) // not found
390            {
391                    return NULL;
392            }
393    
394          return p_section;          return (p_block->articles + left); // found
395  }  }
396    
397  int section_data_free_block(SECTION_DATA *p_section)  ARTICLE *article_block_find_by_index(int index)
398  {  {
399          ARTICLE_BLOCK *p_block;          ARTICLE_BLOCK *p_block;
400    
401          if (p_section == NULL)          if (p_article_block_pool == NULL)
402            {
403                    log_error("article_block_pool not initialized\n");
404                    return NULL;
405            }
406    
407            if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)
408            {
409                    log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
410                    return NULL;
411            }
412    
413            p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];
414    
415            if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
416            {
417                    log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
418                    return NULL;
419            }
420    
421            return (p_block->articles + (index % ARTICLE_PER_BLOCK));
422    }
423    
424    extern int section_list_init(const char *filename)
425    {
426            int semid;
427            int shmid;
428            int proj_id;
429            key_t key;
430            size_t size;
431            void *p_shm;
432            union semun arg;
433            int i;
434    
435            if (p_section_list_pool != NULL)
436            {
437                    section_list_cleanup();
438            }
439    
440            proj_id = (int)(time(NULL) % getpid());
441            key = ftok(filename, proj_id);
442            if (key == -1)
443          {          {
444                  log_error("section_data_free_block() NULL pointer error\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
445                    return -3;
446            }
447    
448            // Allocate shared memory
449            size = sizeof(SECTION_LIST_POOL);
450            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
451            if (shmid == -1)
452            {
453                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
454                    return -3;
455            }
456            p_shm = shmat(shmid, NULL, 0);
457            if (p_shm == (void *)-1)
458            {
459                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
460                    return -3;
461            }
462    
463            p_section_list_pool = p_shm;
464            p_section_list_pool->shmid = shmid;
465            p_section_list_pool->section_count = 0;
466    
467            // Allocate semaphore as section locks
468            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
469            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
470            if (semid == -1)
471            {
472                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
473                    return -3;
474            }
475    
476            // Initialize sem value to 0
477            arg.val = 0;
478            for (i = 0; i < size; i++)
479            {
480                    if (semctl(semid, i, SETVAL, arg) == -1)
481                    {
482                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
483                            return -3;
484                    }
485            }
486    
487            p_section_list_pool->semid = semid;
488    
489            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
490            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
491            {
492                    log_error("trie_dict_create() OOM\n", BBS_max_section);
493                    return -2;
494            }
495    
496            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
497            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
498            {
499                    log_error("trie_dict_create() OOM\n", BBS_max_section);
500                    return -2;
501            }
502    
503            return 0;
504    }
505    
506    void section_list_cleanup(void)
507    {
508            int shmid;
509    
510            if (p_section_list_pool == NULL)
511            {
512                    return;
513            }
514    
515            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
516            {
517                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
518                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
519            }
520    
521            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
522            {
523                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
524                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
525            }
526    
527            shmid = p_section_list_pool->shmid;
528    
529            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
530            {
531                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
532            }
533    
534            if (shmdt(p_section_list_pool) == -1)
535            {
536                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
537            }
538    
539            if (shmctl(shmid, IPC_RMID, NULL) == -1)
540            {
541                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
542            }
543    
544            p_section_list_pool = NULL;
545    }
546    
547    int set_section_list_shm_readonly(void)
548    {
549            int shmid;
550            void *p_shm;
551    
552            if (p_section_list_pool == NULL)
553            {
554                    log_error("p_section_list_pool not initialized\n");
555                  return -1;                  return -1;
556          }          }
557    
558          if (p_section_data_pool == NULL)          shmid = p_section_list_pool->shmid;
559    
560            // Remap shared memory in read-only mode
561            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
562            if (p_shm == (void *)-1)
563          {          {
564                  log_error("section_data not initialized\n");                  log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
565                    return -3;
566            }
567    
568            p_section_list_pool = p_shm;
569    
570            return 0;
571    }
572    
573    int detach_section_list_shm(void)
574    {
575            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
576            {
577                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
578                  return -1;                  return -1;
579          }          }
580    
581          while (p_section->p_head_block != NULL)          p_section_list_pool = NULL;
582    
583            return 0;
584    }
585    
586    inline static void sid_to_str(int32_t sid, char *p_sid_str)
587    {
588            uint32_t u_sid;
589            int i;
590    
591            u_sid = (uint32_t)sid;
592            for (i = 0; i < SID_STR_LEN - 1; i++)
593          {          {
594                  p_block = p_section->p_head_block;                  p_sid_str[i] = (char)(u_sid % 255 + 1);
595                  p_section->p_head_block = p_block->p_next_block;                  u_sid /= 255;
596                  push_free_article_block(p_block);          }
597            p_sid_str[i] = '\0';
598    }
599    
600    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
601    {
602            SECTION_LIST *p_section;
603            char sid_str[SID_STR_LEN];
604    
605            if (p_section_list_pool == NULL)
606            {
607                    log_error("session_list_pool not initialized\n");
608                    return NULL;
609            }
610    
611            if (p_section_list_pool->section_count >= BBS_max_section)
612            {
613                    log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
614                    return NULL;
615            }
616    
617            sid_to_str(sid, sid_str);
618    
619            p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
620    
621            p_section->sid = sid;
622            p_section->ex_menu_tm = 0;
623    
624            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
625            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
626    
627            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
628            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
629    
630            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
631            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
632    
633            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
634            {
635                    log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
636                    return NULL;
637            }
638    
639            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
640            {
641                    log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
642                    return NULL;
643          }          }
644    
645          p_section->p_tail_block = NULL;          section_list_reset_articles(p_section);
646          p_section->block_count = 0;  
647            p_section_list_pool->section_count++;
648    
649            return p_section;
650    }
651    
652    void section_list_reset_articles(SECTION_LIST *p_section)
653    {
654          p_section->article_count = 0;          p_section->article_count = 0;
655          p_section->delete_count = 0;          p_section->topic_count = 0;
656            p_section->visible_article_count = 0;
657            p_section->visible_topic_count = 0;
658            p_section->p_article_head = NULL;
659            p_section->p_article_tail = NULL;
660    
661          return 0;          p_section->page_count = 0;
662            p_section->last_page_visible_article_count = 0;
663    
664            p_section->ontop_article_count = 0;
665    }
666    
667    SECTION_LIST *section_list_find_by_name(const char *sname, int64_t *p_index)
668    {
669            int64_t index;
670            int ret;
671    
672            if (p_section_list_pool == NULL)
673            {
674                    log_error("section_list not initialized\n");
675                    return NULL;
676            }
677    
678            ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
679            if (ret < 0)
680            {
681                    log_error("trie_dict_get(section, %s) error\n", sname);
682                    return NULL;
683            }
684            else if (ret == 0)
685            {
686                    return NULL;
687            }
688    
689            if (p_index != NULL)
690            {
691                    *p_index = index;
692            }
693    
694            return (p_section_list_pool->sections + index);
695  }  }
696    
697  SECTION_DATA *section_data_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_sid(int32_t sid, int64_t *p_index)
698  {  {
699          int64_t index;          int64_t index;
700            int ret;
701            char sid_str[SID_STR_LEN];
702    
703          if (p_section_data_pool == NULL || p_trie_dict_section_data == NULL)          if (p_section_list_pool == NULL)
704          {          {
705                  log_error("section_data not initialized\n");                  log_error("section_list not initialized\n");
706                  return NULL;                  return NULL;
707          }          }
708    
709          if (trie_dict_get(p_trie_dict_section_data, sname, &index) != 1)          sid_to_str(sid, sid_str);
710    
711            ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
712            if (ret < 0)
713            {
714                    log_error("trie_dict_get(section, %d) error\n", sid);
715                    return NULL;
716            }
717            else if (ret == 0)
718          {          {
                 log_error("trie_dict_get(section_data, %s) error\n", sname);  
719                  return NULL;                  return NULL;
720          }          }
721    
722          return (p_section_data_pool + index);          if (p_index != NULL)
723            {
724                    *p_index = index;
725            }
726    
727            return (p_section_list_pool->sections + index);
728  }  }
729    
730  int section_data_append_article(SECTION_DATA *p_section, const ARTICLE *p_article)  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)
731  {  {
732          ARTICLE_BLOCK *p_block;          ARTICLE_BLOCK *p_block;
733          int32_t last_aid = 0;          int32_t last_aid = 0;
734            ARTICLE *p_article;
735          ARTICLE *p_topic_head;          ARTICLE *p_topic_head;
736          ARTICLE *p_topic_tail;          ARTICLE *p_topic_tail;
737    
738          if (p_section == NULL || p_article == NULL)          if (p_section == NULL || p_article_src == NULL)
739          {          {
740                  log_error("section_data_append_article() NULL pointer error\n");                  log_error("section_list_append_article() NULL pointer error\n");
741                  return -1;                  return -1;
742          }          }
743    
744          if (p_section_data_pool == NULL)          if (p_article_block_pool == NULL)
745          {          {
746                  log_error("section_data not initialized\n");                  log_error("article_block_pool not initialized\n");
747                  return -1;                  return -1;
748          }          }
749    
750          if (p_section->p_tail_block == NULL || p_section->p_tail_block->article_count >= BBS_article_limit_per_block)          if (p_section->sid != p_article_src->sid)
751          {          {
752                  if (p_section->block_count >= BBS_article_block_limit_per_section)                  log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
753                  {                  return -2;
754                          log_error("section block count %d reach limit\n", p_section->block_count);          }
                         return -2;  
                 }  
755    
756            if (p_section->article_count >= BBS_article_limit_per_section)
757            {
758                    log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
759                    return -2;
760            }
761    
762            if (p_article_block_pool->block_count == 0 ||
763                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)
764            {
765                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
766                  {                  {
767                          log_error("pop_free_article_block() error\n");                          log_error("pop_free_article_block() error\n");
768                          return -2;                          return -2;
769                  }                  }
770    
771                  p_block->article_count = 0;                  if (p_article_block_pool->block_count > 0)
                 p_block->p_next_block = NULL;  
   
                 if (p_section->p_tail_block == NULL)  
                 {  
                         p_section->p_head_block = p_block;  
                         last_aid = 0;  
                 }  
                 else  
772                  {                  {
773                          p_section->p_tail_block->p_next_block = p_block;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;
                         last_aid = p_section->p_tail_block->articles[BBS_article_limit_per_block - 1].aid;  
774                  }                  }
775                  p_section->p_tail_block = p_block;  
776                  p_section->p_block[p_section->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
777                  p_section->block_count++;                  p_article_block_pool->block_count++;
778          }          }
779          else          else
780          {          {
781                  p_block = p_section->p_tail_block;                  p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
782                  last_aid = p_block->articles[p_block->article_count - 1].aid;                  last_aid = p_block->articles[p_block->article_count - 1].aid;
783          }          }
784    
785          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
786          if (p_article->aid <= last_aid)          if (p_article_src->aid <= last_aid)
787          {          {
788                  log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);
789                  return -3;                  return -3;
790          }          }
791    
792          if (p_block->article_count == 0)          p_article = (p_block->articles + p_block->article_count);
793            p_block->article_count++;
794            p_section->article_count++;
795    
796            // Copy article data
797            *p_article = *p_article_src;
798    
799            if (p_article->visible)
800          {          {
801                  p_section->block_head_aid[p_section->block_count - 1] = p_article->aid;                  p_section->visible_article_count++;
802          }          }
803    
804            // Link appended article as tail node of topic bi-directional list
805          if (p_article->tid != 0)          if (p_article->tid != 0)
806          {          {
807                  p_topic_head = section_data_find_article_by_aid(p_section, p_article->tid);                  p_topic_head = article_block_find_by_aid(p_article->tid);
808                  if (p_topic_head == NULL)                  if (p_topic_head == NULL)
809                  {                  {
810                          log_error("search head of topic (aid=%d) error\n", p_article->tid);                          log_error("search head of topic (aid=%d) error\n", p_article->tid);
811                          return -4;                          return -4;
812                  }                  }
813    
814                  p_topic_tail = section_data_find_article_by_aid(p_section, p_topic_head->prior_aid);                  p_topic_tail = p_topic_head->p_topic_prior;
815                  if (p_topic_tail == NULL)                  if (p_topic_tail == NULL)
816                  {                  {
817                          log_error("search tail of topic (aid=%d) error\n", p_topic_head->prior_aid);                          log_error("tail of topic (aid=%d) is NULL\n", p_article->tid);
818                          return -4;                          return -4;
819                  }                  }
820          }          }
821          else          else
822          {          {
823                  p_topic_head = &(p_block->articles[p_block->article_count]);                  p_section->topic_count++;
824                  p_topic_tail = p_topic_head;  
825                    if (p_article->visible)
826                    {
827                            p_section->visible_topic_count++;
828                    }
829    
830                    p_topic_head = p_article;
831                    p_topic_tail = p_article;
832          }          }
833    
834          // Copy article data          p_article->p_topic_prior = p_topic_tail;
835          p_block->articles[p_block->article_count] = *p_article;          p_article->p_topic_next = p_topic_head;
836            p_topic_head->p_topic_prior = p_article;
837            p_topic_tail->p_topic_next = p_article;
838    
839          // Link appended article as tail node of topic bi-directional list;          // Link appended article as tail node of article bi-directional list
840          p_block->articles[p_block->article_count].prior_aid = p_topic_tail->aid;          if (p_section->p_article_head == NULL)
841          p_block->articles[p_block->article_count].next_aid = p_topic_head->aid;          {
842          p_topic_head->prior_aid = p_article->aid;                  p_section->p_article_head = p_article;
843          p_topic_tail->next_aid = p_article->aid;                  p_section->p_article_tail = p_article;
844            }
845            p_article->p_prior = p_section->p_article_tail;
846            p_article->p_next = p_section->p_article_head;
847            p_section->p_article_head->p_prior = p_article;
848            p_section->p_article_tail->p_next = p_article;
849            p_section->p_article_tail = p_article;
850    
851          p_block->article_count++;          // Update page
852          p_section->article_count++;          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||
853                    p_section->article_count == 1)
854            {
855                    p_section->p_page_first_article[p_section->page_count] = p_article;
856                    p_section->page_count++;
857                    p_section->last_page_visible_article_count = 0;
858            }
859    
860            if (p_article->visible)
861            {
862                    p_section->last_page_visible_article_count++;
863            }
864    
865            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
866            {
867                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
868                                      p_section->sid, p_article->aid);
869                    return -5;
870            }
871    
872          return 0;          return 0;
873  }  }
874    
875  ARTICLE *section_data_find_article_by_aid(SECTION_DATA *p_section, int32_t aid)  int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible)
876    {
877            ARTICLE *p_article;
878            ARTICLE *p_reply;
879            int affected_count = 0;
880    
881            if (p_section == NULL)
882            {
883                    log_error("NULL pointer error\n");
884                    return -1;
885            }
886    
887            p_article = article_block_find_by_aid(aid);
888            if (p_article == NULL)
889            {
890                    return -1; // Not found
891            }
892    
893            if (p_section->sid != p_article->sid)
894            {
895                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
896                    return -2;
897            }
898    
899            if (p_article->visible == visible)
900            {
901                    return 0; // Already set
902            }
903    
904            if (visible == 0) // 1 -> 0
905            {
906                    p_section->visible_article_count--;
907    
908                    if (p_article->tid == 0)
909                    {
910                            p_section->visible_topic_count--;
911    
912                            // Set related visible replies to invisible
913                            for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next)
914                            {
915                                    if (p_reply->tid != aid)
916                                    {
917                                            log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);
918                                            continue;
919                                    }
920    
921                                    if (p_reply->visible == 1)
922                                    {
923                                            p_reply->visible = 0;
924                                            p_section->visible_article_count--;
925                                            affected_count++;
926                                    }
927                            }
928                    }
929            }
930            else // 0 -> 1
931            {
932                    p_section->visible_article_count++;
933    
934                    if (p_article->tid == 0)
935                    {
936                            p_section->visible_topic_count++;
937                    }
938            }
939    
940            p_article->visible = visible;
941            affected_count++;
942    
943            return affected_count;
944    }
945    
946    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
947    {
948            int i;
949    
950            if (p_section == NULL || p_article == NULL)
951            {
952                    log_error("NULL pointer error\n");
953                    return -1;
954            }
955    
956            if (p_section->sid != p_article->sid)
957            {
958                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
959                    return -2;
960            }
961    
962            if (p_article->ontop)
963            {
964                    for (i = 0; i < p_section->ontop_article_count; i++)
965                    {
966                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
967                            {
968                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
969                                    return 0;
970                            }
971                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
972                            {
973                                    break;
974                            }
975                    }
976    
977                    // Remove the oldest one if the array of ontop articles is full
978                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
979                    {
980                            if (i == 0) // p_article is the oldest one
981                            {
982                                    return 0;
983                            }
984                            memmove((void *)(p_section->p_ontop_articles),
985                                            (void *)(p_section->p_ontop_articles + 1),
986                                            sizeof(ARTICLE *) * (size_t)(i - 1));
987                            p_section->ontop_article_count--;
988                            i--;
989                    }
990                    else
991                    {
992                            memmove((void *)(p_section->p_ontop_articles + i + 1),
993                                            (void *)(p_section->p_ontop_articles + i),
994                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
995                    }
996    
997                    p_section->p_ontop_articles[i] = p_article;
998                    p_section->ontop_article_count++;
999    
1000                    // TODO: debug
1001            }
1002            else // ontop == 0
1003            {
1004                    for (i = 0; i < p_section->ontop_article_count; i++)
1005                    {
1006                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1007                            {
1008                                    break;
1009                            }
1010                    }
1011                    if (i == p_section->ontop_article_count) // not found
1012                    {
1013                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1014                            return 0;
1015                    }
1016    
1017                    memmove((void *)(p_section->p_ontop_articles + i),
1018                                    (void *)(p_section->p_ontop_articles + i + 1),
1019                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1020                    p_section->ontop_article_count--;
1021            }
1022    
1023            return 0;
1024    }
1025    
1026    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1027    {
1028            int page_count;
1029    
1030            if (p_section == NULL)
1031            {
1032                    log_error("NULL pointer error\n");
1033                    return -1;
1034            }
1035    
1036            page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1037                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1038                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1039    
1040            return page_count;
1041    }
1042    
1043    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1044    {
1045            if (p_section == NULL)
1046            {
1047                    log_error("NULL pointer error\n");
1048                    return -1;
1049            }
1050    
1051            if (page_id < p_section->page_count - 1)
1052            {
1053                    return BBS_article_limit_per_page;
1054            }
1055            else // if (page_id >= p_section->page_count - 1)
1056            {
1057                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1058                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1059            }
1060    }
1061    
1062    ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
1063  {  {
1064          ARTICLE *p_article;          ARTICLE *p_article;
         ARTICLE_BLOCK *p_block;  
1065          int left;          int left;
1066          int right;          int right;
1067          int mid;          int mid;
1068    
1069            *p_page = -1;
1070            *p_offset = -1;
1071            *pp_next = NULL;
1072    
1073          if (p_section == NULL)          if (p_section == NULL)
1074          {          {
1075                  log_error("section_data_find_article_by_aid() NULL pointer error\n");                  log_error("section_list_find_article_with_offset() NULL pointer error\n");
1076                  return NULL;                  return NULL;
1077          }          }
1078    
1079          if (p_section->block_count == 0) // empty section          if (p_section->article_count == 0) // empty
1080          {          {
1081                    *p_page = 0;
1082                    *p_offset = 0;
1083                  return NULL;                  return NULL;
1084          }          }
1085    
1086          left = 0;          left = 0;
1087          right = p_section->block_count;          right = p_section->page_count;
1088    
1089          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]          // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]
1090          while (left < right - 1)          while (left < right - 1)
1091          {          {
1092                  // get block offset no less than mid value of left and right block offsets                  // get page id no less than mid value of left page id and right page id
1093                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (right - left) % 2;
1094                    
1095                  if (mid >= BBS_article_block_limit_per_section)                  if (mid >= p_section->page_count)
1096                  {                  {
1097                          log_error("block_m(%d) is out of boundary\n", mid);                          log_error("page id (mid = %d) is out of boundary\n", mid);
1098                          return NULL;                          return NULL;
1099                  }                  }
1100    
1101                  if (aid < p_section->block_head_aid[mid])                  if (aid < p_section->p_page_first_article[mid]->aid)
1102                  {                  {
1103                          right = mid;                          right = mid;
1104                  }                  }
# Line 461  ARTICLE *section_data_find_article_by_ai Line 1108  ARTICLE *section_data_find_article_by_ai
1108                  }                  }
1109          }          }
1110    
1111          p_block = p_section->p_block[left];          *p_page = left;
1112    
1113            p_article = p_section->p_page_first_article[*p_page];
1114    
1115            // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
1116            right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
1117    
1118            // left will be the offset of article found or offset to insert
1119          left = 0;          left = 0;
         right = p_block->article_count - 1;  
1120    
1121          // aid in the range [ aid of articles[left], aid of articles[right] ]          while (aid > p_article->aid)
         while (left < right)  
1122          {          {
1123                  mid = (left + right) / 2;                  p_article = p_article->p_next;
1124                    left++;
1125    
1126                  if (aid <= p_block->articles[mid].aid)                  if (aid == p_article->aid)
1127                  {                  {
1128                          right = mid;                          *pp_next = p_article->p_next;
1129                            break;
1130                  }                  }
1131                  else  
1132                    // over last article in the page
1133                    if (p_article == p_section->p_article_head || p_article->aid >= right)
1134                  {                  {
1135                          left = mid + 1;                          *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
1136                            *p_offset = left;
1137                            return NULL; // not found
1138                  }                  }
1139          }          }
1140    
1141          p_article = &(p_block->articles[left]);          if (aid < p_article->aid)
1142            {
1143                    *pp_next = p_article;
1144                    p_article = NULL; // not found
1145            }
1146            else // aid == p_article->aid
1147            {
1148                    *pp_next = p_article->p_next;
1149            }
1150    
1151            *p_offset = left;
1152    
1153          return p_article;          return p_article;
1154  }  }
1155    
1156  ARTICLE *section_data_find_article_by_index(SECTION_DATA *p_section, int index)  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
1157  {  {
1158          ARTICLE *p_article;          ARTICLE *p_article;
1159          ARTICLE_BLOCK *p_block;          ARTICLE *p_next;
1160            int32_t page;
1161            int32_t offset;
1162            int visible_article_count;
1163            int page_head_set;
1164    
1165          if (p_section == NULL)          if (p_section == NULL)
1166          {          {
1167                  log_error("section_data_find_article_by_index() NULL pointer error\n");                  log_error("section_list_calculate_page() NULL pointer error\n");
1168                  return NULL;                  return -1;
1169          }          }
1170    
1171          if (index < 0 || index >= p_section->article_count)          if (p_section->article_count == 0) // empty
1172          {          {
1173                  log_error("section_data_find_article_by_index(%d) is out of boundary [0, %d)\n", index, p_section->article_count);                  p_section->page_count = 0;
1174                  return NULL;                  p_section->last_page_visible_article_count = 0;
1175    
1176                    return 0;
1177          }          }
1178    
1179          p_block = p_section->p_block[index / BBS_article_limit_per_block];          if (start_aid > 0)
1180          p_article = &(p_block->articles[index % BBS_article_limit_per_block]);          {
1181                    p_article = article_block_find_by_aid(start_aid);
1182                    if (p_article == NULL)
1183                    {
1184                            return -1; // Not found
1185                    }
1186    
1187          return p_article;                  if (p_section->sid != p_article->sid)
1188                    {
1189                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
1190                            return -2;
1191                    }
1192    
1193                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
1194                    if (p_article == NULL)
1195                    {
1196                            if (page < 0)
1197                            {
1198                                    return -1;
1199                            }
1200                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
1201                                              start_aid, p_section->sid);
1202                            return -2;
1203                    }
1204    
1205                    if (offset > 0)
1206                    {
1207                            p_article = p_section->p_page_first_article[page];
1208                    }
1209            }
1210            else
1211            {
1212                    p_article = p_section->p_article_head;
1213                    page = 0;
1214                    offset = 0;
1215            }
1216    
1217            visible_article_count = 0;
1218            page_head_set = 0;
1219    
1220            do
1221            {
1222                    if (!page_head_set && visible_article_count == 0)
1223                    {
1224                            p_section->p_page_first_article[page] = p_article;
1225                            page_head_set = 1;
1226                    }
1227    
1228                    if (p_article->visible)
1229                    {
1230                            visible_article_count++;
1231                    }
1232    
1233                    p_article = p_article->p_next;
1234    
1235                    // skip remaining invisible articles
1236                    while (p_article->visible == 0 && p_article != p_section->p_article_head)
1237                    {
1238                            p_article = p_article->p_next;
1239                    }
1240    
1241                    if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head)
1242                    {
1243                            page++;
1244                            visible_article_count = 0;
1245                            page_head_set = 0;
1246    
1247                            if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
1248                            {
1249                                    log_error("Count of page exceed limit in section %d\n", p_section->sid);
1250                                    break;
1251                            }
1252                    }
1253            } while (p_article != p_section->p_article_head);
1254    
1255            p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1256            p_section->last_page_visible_article_count = visible_article_count;
1257    
1258            return 0;
1259    }
1260    
1261    int32_t article_block_last_aid(void)
1262    {
1263            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1264            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1265    
1266            return last_aid;
1267    }
1268    
1269    int article_block_article_count(void)
1270    {
1271            int ret;
1272    
1273            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1274            {
1275                    return -1;
1276            }
1277    
1278            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1279                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1280    
1281            return ret;
1282  }  }
1283    
1284  int section_data_mark_del_article(SECTION_DATA *p_section, int32_t aid)  int article_count_of_topic(int32_t aid)
1285  {  {
1286          ARTICLE *p_article;          ARTICLE *p_article;
1287            int article_count;
1288    
1289            p_article = article_block_find_by_aid(aid);
1290            if (p_article == NULL)
1291            {
1292                    return 0; // Not found
1293            }
1294    
1295            article_count = 0;
1296    
1297            do
1298            {
1299                    if (p_article->tid != 0 && p_article->tid != aid)
1300                    {
1301                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1302                            break;
1303                    }
1304    
1305                    article_count++;
1306                    p_article = p_article->p_topic_next;
1307            } while (p_article->aid != aid);
1308    
1309            return article_count;
1310    }
1311    
1312    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
1313    {
1314            ARTICLE *p_article;
1315            ARTICLE *p_next;
1316            int32_t page;
1317            int32_t offset;
1318            int32_t move_article_count;
1319            int32_t dest_article_count_old;
1320            int32_t last_unaffected_aid_src;
1321            int32_t first_inserted_aid_dest;
1322            int move_counter;
1323    
1324            if (p_section_src == NULL || p_section_dest == NULL)
1325            {
1326                    log_error("section_list_move_topic() NULL pointer error\n");
1327                    return -1;
1328            }
1329    
1330            if (p_section_src->sid == p_section_dest->sid)
1331            {
1332                    log_error("section_list_move_topic() src and dest section are the same\n");
1333                    return -1;
1334            }
1335    
1336            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1337            {
1338                    log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1339                    return -2;
1340            }
1341    
1342            if (p_section_src->sid != p_article->sid)
1343            {
1344                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1345                                      p_section_src->sid, p_article->aid, p_article->sid);
1346                    return -2;
1347            }
1348    
1349            if (p_article->tid != 0)
1350            {
1351                    log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
1352                    return -2;
1353            }
1354    
1355            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1356    
1357            move_article_count = article_count_of_topic(aid);
1358            if (move_article_count <= 0)
1359            {
1360                    log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1361                    return -2;
1362            }
1363    
1364            if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1365            {
1366                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
1367                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
1368                    return -3;
1369            }
1370    
1371            dest_article_count_old = p_section_dest->article_count;
1372            move_counter = 0;
1373            first_inserted_aid_dest = p_article->aid;
1374    
1375            do
1376            {
1377                    if (p_section_src->sid != p_article->sid)
1378                    {
1379                            log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1380                                              p_section_src->sid, p_article->aid, p_article->sid);
1381                            p_article = p_article->p_topic_next;
1382                            continue;
1383                    }
1384    
1385                    // Remove from bi-directional article list of src section
1386                    if (p_section_src->p_article_head == p_article)
1387                    {
1388                            p_section_src->p_article_head = p_article->p_next;
1389                    }
1390                    if (p_section_src->p_article_tail == p_article)
1391                    {
1392                            p_section_src->p_article_tail = p_article->p_prior;
1393                    }
1394                    if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
1395                    {
1396                            p_section_src->p_article_head = NULL;
1397                            p_section_src->p_article_tail = NULL;
1398                    }
1399    
1400                    p_article->p_prior->p_next = p_article->p_next;
1401                    p_article->p_next->p_prior = p_article->p_prior;
1402    
1403                    // Update sid of article
1404                    p_article->sid = p_section_dest->sid;
1405    
1406                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1407                    {
1408                            log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1409                            p_article = p_article->p_topic_next;
1410                            continue;
1411                    }
1412    
1413                    // Insert into bi-directional article list of dest section
1414                    if (p_next == NULL) // empty section
1415                    {
1416                            p_section_dest->p_article_head = p_article;
1417                            p_section_dest->p_article_tail = p_article;
1418                            p_article->p_prior = p_article;
1419                            p_article->p_next = p_article;
1420                    }
1421                    else
1422                    {
1423                            if (p_section_dest->p_article_head == p_next)
1424                            {
1425                                    if (p_article->aid < p_next->aid)
1426                                    {
1427                                            p_section_dest->p_article_head = p_article;
1428                                    }
1429                                    else // p_article->aid > p_next->aid
1430                                    {
1431                                            p_section_dest->p_article_tail = p_article;
1432                                    }
1433                            }
1434    
1435                            p_article->p_prior = p_next->p_prior;
1436                            p_article->p_next = p_next;
1437                            p_next->p_prior->p_next = p_article;
1438                            p_next->p_prior = p_article;
1439                    }
1440    
1441                    // Update article / topic counter of src / desc section
1442                    p_section_src->article_count--;
1443                    p_section_dest->article_count++;
1444                    if (p_article->tid == 0)
1445                    {
1446                            p_section_src->topic_count--;
1447                            p_section_dest->topic_count++;
1448                    }
1449    
1450                    // Update visible article / topic counter of src / desc section
1451                    if (p_article->visible)
1452                    {
1453                            p_section_src->visible_article_count--;
1454                            p_section_dest->visible_article_count++;
1455                            if (p_article->tid == 0)
1456                            {
1457                                    p_section_src->visible_topic_count--;
1458                                    p_section_dest->visible_topic_count++;
1459                            }
1460                    }
1461    
1462                    // Update page for empty dest section
1463                    if (p_section_dest->article_count == 1)
1464                    {
1465                            p_section_dest->p_page_first_article[0] = p_article;
1466                            p_section_dest->page_count = 1;
1467                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1468                    }
1469    
1470                    p_article = p_article->p_topic_next;
1471    
1472                    move_counter++;
1473                    if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1474                    {
1475                            // Re-calculate pages of desc section
1476                            if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1477                            {
1478                                    log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1479                                                      p_section_dest->sid, first_inserted_aid_dest);
1480                            }
1481    
1482                            first_inserted_aid_dest = p_article->aid;
1483                    }
1484            } while (p_article->aid != aid);
1485    
1486            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1487            {
1488                    log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1489                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
1490            }
1491    
1492            // Re-calculate pages of src section
1493            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1494            {
1495                    log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1496                                      p_section_src->sid, last_unaffected_aid_src, aid);
1497            }
1498    
1499            if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1500            {
1501                    // Re-calculate pages of desc section
1502                    if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1503                    {
1504                            log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1505                                              p_section_dest->sid, first_inserted_aid_dest);
1506                    }
1507            }
1508    
1509            return move_article_count;
1510    }
1511    
1512    int get_section_index(SECTION_LIST *p_section)
1513    {
1514            int index;
1515    
1516            if (p_section_list_pool == NULL)
1517            {
1518                    log_error("get_section_index() error: uninitialized\n");
1519                    return -1;
1520            }
1521    
1522          if (p_section == NULL)          if (p_section == NULL)
1523          {          {
1524                  log_error("section_data_mark_del_article() NULL pointer error\n");                  index = BBS_max_section;
1525            }
1526            else
1527            {
1528                    index = (int)(p_section - p_section_list_pool->sections);
1529                    if (index < 0 || index >= BBS_max_section)
1530                    {
1531                            log_error("get_section_index(%d) error: index out of range\n", index);
1532                            return -2;
1533                    }
1534            }
1535    
1536            return index;
1537    }
1538    
1539    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1540    {
1541            int index;
1542            struct sembuf sops[4];
1543            struct timespec timeout;
1544            int ret;
1545    
1546            index = get_section_index(p_section);
1547            if (index < 0)
1548            {
1549                  return -2;                  return -2;
1550          }          }
1551    
1552          p_article = section_data_find_article_by_aid(p_section, aid);          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1553          if (p_article == NULL)          sops[0].sem_op = 0;                                                                // wait until unlocked
1554            sops[0].sem_flg = 0;
1555    
1556            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1557            sops[1].sem_op = 1;                                                        // lock
1558            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1559    
1560            // Read lock on any specific section will also acquire single read lock on "all section"
1561            // so that write lock on all section only need to acquire single write on on "all section"
1562            // rather than to acquire multiple write locks on all the available sections.
1563            if (index != BBS_max_section)
1564          {          {
1565                  return -1; // Not found                  sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1566                    sops[2].sem_op = 0;                                                // wait until unlocked
1567                    sops[2].sem_flg = 0;
1568    
1569                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1570                    sops[3].sem_op = 1;                                        // lock
1571                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1572            }
1573    
1574            timeout.tv_sec = wait_sec;
1575            timeout.tv_nsec = 0;
1576    
1577            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1578            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1579            {
1580                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1581            }
1582    
1583            return ret;
1584    }
1585    
1586    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1587    {
1588            int index;
1589            struct sembuf sops[3];
1590            struct timespec timeout;
1591            int ret;
1592    
1593            index = get_section_index(p_section);
1594            if (index < 0)
1595            {
1596                    return -2;
1597            }
1598    
1599            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1600            sops[0].sem_op = 0;                                                                // wait until unlocked
1601            sops[0].sem_flg = 0;
1602    
1603            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1604            sops[1].sem_op = 1;                                                                // lock
1605            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1606    
1607            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1608            sops[2].sem_op = 0;                                                        // wait until unlocked
1609            sops[2].sem_flg = 0;
1610    
1611            timeout.tv_sec = wait_sec;
1612            timeout.tv_nsec = 0;
1613    
1614            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1615            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1616            {
1617                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1618            }
1619    
1620            return ret;
1621    }
1622    
1623    int section_list_rd_unlock(SECTION_LIST *p_section)
1624    {
1625            int index;
1626            struct sembuf sops[2];
1627            int ret;
1628    
1629            index = get_section_index(p_section);
1630            if (index < 0)
1631            {
1632                    return -2;
1633          }          }
1634    
1635          if (p_article->visible == 0)          sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1636            sops[0].sem_op = -1;                                               // unlock
1637            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1638    
1639            if (index != BBS_max_section)
1640            {
1641                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1642                    sops[1].sem_op = -1;                                     // unlock
1643                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1644            }
1645    
1646            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1647            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1648            {
1649                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1650            }
1651    
1652            return ret;
1653    }
1654    
1655    int section_list_rw_unlock(SECTION_LIST *p_section)
1656    {
1657            int index;
1658            struct sembuf sops[1];
1659            int ret;
1660    
1661            index = get_section_index(p_section);
1662            if (index < 0)
1663            {
1664                    return -2;
1665            }
1666    
1667            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1668            sops[0].sem_op = -1;                                                       // unlock
1669            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1670    
1671            ret = semop(p_section_list_pool->semid, sops, 1);
1672            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1673            {
1674                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1675            }
1676    
1677            return ret;
1678    }
1679    
1680    int section_list_rd_lock(SECTION_LIST *p_section)
1681    {
1682            int timer = 0;
1683            int sid = (p_section == NULL ? 0 : p_section->sid);
1684            int ret = -1;
1685    
1686            while (!SYS_server_exit)
1687          {          {
1688                  return 0; // Already deleted                  ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1689                    if (ret == 0) // success
1690                    {
1691                            break;
1692                    }
1693                    else if (errno == EAGAIN || errno == EINTR) // retry
1694                    {
1695                            timer++;
1696                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1697                            {
1698                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer);
1699                            }
1700                    }
1701                    else // failed
1702                    {
1703                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1704                            break;
1705                    }
1706          }          }
1707    
1708          p_article->visible = 0;          return ret;
1709          p_section->delete_count++;  }
1710    
1711    int section_list_rw_lock(SECTION_LIST *p_section)
1712    {
1713            int timer = 0;
1714            int sid = (p_section == NULL ? 0 : p_section->sid);
1715            int ret = -1;
1716    
1717            while (!SYS_server_exit)
1718            {
1719                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1720                    if (ret == 0) // success
1721                    {
1722                            break;
1723                    }
1724                    else if (errno == EAGAIN || errno == EINTR) // retry
1725                    {
1726                            timer++;
1727                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1728                            {
1729                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer);
1730                            }
1731                    }
1732                    else // failed
1733                    {
1734                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1735                            break;
1736                    }
1737            }
1738    
1739          return 1;          return ret;
1740  }  }


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

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