| 42 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 43 |
|
|
| 44 |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 45 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 46 |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
| 47 |
|
|
| 48 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
| 74 |
}; |
}; |
| 75 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 76 |
|
|
| 77 |
|
static int article_block_pool_shmid; |
| 78 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 79 |
|
|
|
static int section_list_pool_shmid; |
|
| 80 |
static int section_list_pool_semid; |
static int section_list_pool_semid; |
| 81 |
|
static int section_list_pool_shmid; |
| 82 |
static SECTION_LIST *p_section_list_pool = NULL; |
static SECTION_LIST *p_section_list_pool = NULL; |
| 83 |
|
|
| 84 |
static int section_list_count = 0; |
static int section_list_count = 0; |
| 85 |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
| 86 |
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
| 109 |
return -2; |
return -2; |
| 110 |
} |
} |
| 111 |
|
|
| 112 |
p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL)); |
// Allocate shared memory |
| 113 |
if (p_article_block_pool == NULL) |
proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm |
| 114 |
|
key = ftok(filename, proj_id); |
| 115 |
|
if (key == -1) |
| 116 |
{ |
{ |
| 117 |
log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n"); |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 118 |
return -2; |
return -3; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
// Allocate shared memory |
size = sizeof(ARTICLE_BLOCK_POOL); |
| 122 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 123 |
|
if (shmid == -1) |
| 124 |
|
{ |
| 125 |
|
log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno); |
| 126 |
|
return -3; |
| 127 |
|
} |
| 128 |
|
p_shm = shmat(shmid, NULL, 0); |
| 129 |
|
if (p_shm == (void *)-1) |
| 130 |
|
{ |
| 131 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 132 |
|
return -3; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
article_block_pool_shmid = shmid; |
| 136 |
|
p_article_block_pool = p_shm; |
| 137 |
|
|
| 138 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 139 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 140 |
|
|
| 143 |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
| 144 |
block_count -= block_count_in_shm; |
block_count -= block_count_in_shm; |
| 145 |
|
|
| 146 |
proj_id = getpid() + p_article_block_pool->shm_count; |
proj_id = p_article_block_pool->shm_count; |
| 147 |
key = ftok(filename, proj_id); |
key = ftok(filename, proj_id); |
| 148 |
if (key == -1) |
if (key == -1) |
| 149 |
{ |
{ |
| 196 |
|
|
| 197 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 198 |
{ |
{ |
| 199 |
if (p_article_block_pool != NULL) |
if (p_article_block_pool == NULL) |
| 200 |
|
{ |
| 201 |
|
return; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 205 |
{ |
{ |
| 206 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 207 |
{ |
{ |
| 208 |
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 209 |
{ |
} |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
|
} |
|
| 210 |
|
|
| 211 |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
| 212 |
{ |
{ |
| 213 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
} |
|
| 214 |
} |
} |
| 215 |
|
} |
| 216 |
|
|
| 217 |
free(p_article_block_pool); |
if (shmdt(p_article_block_pool) == -1) |
| 218 |
p_article_block_pool = NULL; |
{ |
| 219 |
|
log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
|
if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1) |
| 223 |
|
{ |
| 224 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno); |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
|
p_article_block_pool = NULL; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
| 377 |
section_list_pool_cleanup(); |
section_list_pool_cleanup(); |
| 378 |
} |
} |
| 379 |
|
|
|
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
|
|
if (p_section_list_pool == NULL) |
|
|
{ |
|
|
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
|
|
return -1; |
|
|
} |
|
|
|
|
| 380 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 381 |
key = ftok(filename, proj_id); |
key = ftok(filename, proj_id); |
| 382 |
if (key == -1) |
if (key == -1) |
| 406 |
|
|
| 407 |
section_list_pool_semid = semid; |
section_list_pool_semid = semid; |
| 408 |
|
|
| 409 |
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
size = sizeof(SECTION_LIST) * BBS_max_section; |
| 410 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 411 |
if (shmid == -1) |
if (shmid == -1) |
| 412 |
{ |
{ |