/[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.49 by sysadm, Mon Nov 3 08:48:56 2025 UTC Revision 1.55 by sysadm, Wed Nov 5 04:19:21 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                             section_list.c  -  description  /*
3                                                           -------------------   * section_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - data models and basic operations of section and article
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "log.h"  #include "log.h"
10  #include "section_list.h"  #include "section_list.h"
# Line 40  union semun Line 32  union semun
32  };  };
33  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
34    
35  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second  enum _section_list_constant_t
36  #define SECTION_TRY_LOCK_TIMES 10  {
37            SECTION_TRY_LOCK_WAIT_TIME = 1, // second
38            SECTION_TRY_LOCK_TIMES = 10,
39    
40  #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate          ARTICLE_BLOCK_PER_SHM = 1000,           // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
41  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)          ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id)
42  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)          ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT),
43    
44  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections          CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
45    
46  #define SID_STR_LEN 5 // 32-bit + NULL          SID_STR_LEN = 5, // 32-bit + NULL
47    };
48    
49  struct article_block_t  struct article_block_t
50  {  {
51          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
52          int article_count;          int article_count;
53          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
54  };  };
# Line 216  void article_block_cleanup(void) Line 211  void article_block_cleanup(void)
211                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
212          }          }
213    
214          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
215          {          {
216                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
217          }          }
# Line 399  ARTICLE *article_block_find_by_index(int Line 394  ARTICLE *article_block_find_by_index(int
394                  return NULL;                  return NULL;
395          }          }
396    
397          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)
398          {          {
399                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
400                  return NULL;                  return NULL;
401          }          }
402    
403          p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];          p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block];
404    
405          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
406          {          {
407                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
408                  return NULL;                  return NULL;
409          }          }
410    
411          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
412  }  }
413    
414  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
# Line 531  void section_list_cleanup(void) Line 526  void section_list_cleanup(void)
526                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
527          }          }
528    
529          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
530          {          {
531                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
532          }          }
# Line 775  int section_list_append_article(SECTION_ Line 770  int section_list_append_article(SECTION_
770          }          }
771    
772          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
773                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block)
774          {          {
775                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
776                  {                  {
# Line 785  int section_list_append_article(SECTION_ Line 780  int section_list_append_article(SECTION_
780    
781                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
782                  {                  {
783                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid;
784                  }                  }
785    
786                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
# Line 864  int section_list_append_article(SECTION_ Line 859  int section_list_append_article(SECTION_
859          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
860    
861          // Update page          // Update page
862          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||          if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) ||
863                  p_section->article_count == 1)                  p_section->page_count == 0)
864          {          {
865                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
866                  p_section->page_count++;                  p_section->page_count++;
# Line 1063  int section_list_page_count_with_ontop(S Line 1058  int section_list_page_count_with_ontop(S
1058                  return -1;                  return -1;
1059          }          }
1060    
1061          page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +          page_count = p_section->page_count - 1 +
1062                                   (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +                                   (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1063                                   ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);                                   ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0);
1064    
1065            if (page_count < 0)
1066            {
1067                    page_count = 0;
1068            }
1069    
1070          return page_count;          return page_count;
1071  }  }
# Line 1279  int section_list_calculate_page(SECTION_ Line 1279  int section_list_calculate_page(SECTION_
1279          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1280    
1281          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1282          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1283                                                                                                              ? visible_article_count
1284                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1285    
1286          return 0;          return 0;
1287  }  }
# Line 1301  int article_block_article_count(void) Line 1303  int article_block_article_count(void)
1303                  return -1;                  return -1;
1304          }          }
1305    
1306          ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +          ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1307                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1308    
1309          return ret;          return ret;


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

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