| 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" |
| 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 |
}; |
}; |
| 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 |
} |
} |
| 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) |
| 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 |
} |
} |
| 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 |
{ |
{ |
| 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; |
| 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++; |
| 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 |
} |
} |
| 1084 |
} |
} |
| 1085 |
else // if (page_id >= p_section->page_count - 1) |
else // if (page_id >= p_section->page_count - 1) |
| 1086 |
{ |
{ |
| 1087 |
return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count - |
return MIN(MAX(0, |
| 1088 |
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))); |
(p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1089 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))), |
| 1090 |
|
BBS_article_limit_per_page); |
| 1091 |
} |
} |
| 1092 |
} |
} |
| 1093 |
|
|
| 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 |
} |
} |
| 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; |