| 31 |
|
|
| 32 |
#define MAX_SPLIT_FILE_LINES 65536 |
#define MAX_SPLIT_FILE_LINES 65536 |
| 33 |
|
|
| 34 |
|
struct shm_header_t |
| 35 |
|
{ |
| 36 |
|
int shmid; |
| 37 |
|
size_t data_len; |
| 38 |
|
long line_total; |
| 39 |
|
}; |
| 40 |
|
|
| 41 |
static TRIE_NODE *p_trie_file_dict = NULL; |
static TRIE_NODE *p_trie_file_dict = NULL; |
| 42 |
|
|
| 43 |
int file_loader_init() |
int file_loader_init() |
| 60 |
|
|
| 61 |
static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) |
static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) |
| 62 |
{ |
{ |
| 63 |
const void *p_shm = (const void *)value; |
int shmid = (int)value; |
|
int shmid = *((int *)p_shm); |
|
|
|
|
|
if (shmdt(p_shm) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); |
|
|
} |
|
| 64 |
|
|
| 65 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 66 |
{ |
{ |
| 76 |
} |
} |
| 77 |
|
|
| 78 |
trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); |
trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); |
|
|
|
| 79 |
trie_dict_destroy(p_trie_file_dict); |
trie_dict_destroy(p_trie_file_dict); |
| 80 |
|
|
| 81 |
p_trie_file_dict = NULL; |
p_trie_file_dict = NULL; |
| 82 |
} |
} |
| 83 |
|
|
| 84 |
int load_file_shm(const char *filename) |
int load_file(const char *filename) |
| 85 |
{ |
{ |
| 86 |
int fd; |
int fd; |
| 87 |
struct stat sb; |
struct stat sb; |
| 96 |
long line_offsets[MAX_SPLIT_FILE_LINES]; |
long line_offsets[MAX_SPLIT_FILE_LINES]; |
| 97 |
long *p_line_offsets; |
long *p_line_offsets; |
| 98 |
int64_t shmid_old; |
int64_t shmid_old; |
|
void *p_shm_old; |
|
| 99 |
|
|
| 100 |
if ((fd = open(filename, O_RDONLY)) < 0) |
if ((fd = open(filename, O_RDONLY)) < 0) |
| 101 |
{ |
{ |
| 138 |
return -2; |
return -2; |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
size = sizeof(shmid) + sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); |
size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); |
| 142 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 143 |
if (shmid == -1) |
if (shmid == -1) |
| 144 |
{ |
{ |
| 152 |
return -3; |
return -3; |
| 153 |
} |
} |
| 154 |
|
|
| 155 |
*((int *)p_shm) = shmid; |
((struct shm_header_t *)p_shm)->shmid = shmid; |
| 156 |
*((size_t *)(p_shm + sizeof(shmid))) = data_len; |
((struct shm_header_t *)p_shm)->data_len = data_len; |
| 157 |
*((long *)(p_shm + sizeof(shmid) + sizeof(data_len))) = line_total; |
((struct shm_header_t *)p_shm)->line_total = line_total; |
| 158 |
memcpy(p_shm + sizeof(shmid) + sizeof(data_len) + sizeof(line_total), p_data, data_len); |
memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len); |
| 159 |
|
|
| 160 |
if (munmap(p_data, data_len) < 0) |
if (munmap(p_data, data_len) < 0) |
| 161 |
{ |
{ |
| 163 |
return -2; |
return -2; |
| 164 |
} |
} |
| 165 |
|
|
| 166 |
p_data = p_shm + sizeof(int) + sizeof(data_len) + sizeof(line_total); |
p_data = p_shm + sizeof(struct shm_header_t); |
| 167 |
p_line_offsets = p_data + data_len + 1; |
p_line_offsets = p_data + data_len + 1; |
| 168 |
memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); |
memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); |
| 169 |
|
|
| 170 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1) |
if (shmdt(p_shm) == -1) |
| 171 |
{ |
{ |
| 172 |
shmid_old = *((int *)p_shm_old); |
log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); |
| 173 |
|
return -3; |
| 174 |
if (shmdt(p_shm_old) == -1) |
} |
|
{ |
|
|
log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno); |
|
|
return -3; |
|
|
} |
|
| 175 |
|
|
| 176 |
|
if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1) |
| 177 |
|
{ |
| 178 |
if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1) |
if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1) |
| 179 |
{ |
{ |
| 180 |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno); |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno); |
| 182 |
} |
} |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1) |
if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1) |
| 186 |
{ |
{ |
| 187 |
log_error("trie_dict_set(%s) error\n", filename); |
log_error("trie_dict_set(%s) error\n", filename); |
| 188 |
|
|
| 197 |
return 0; |
return 0; |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
int unload_file_shm(const char *filename) |
int unload_file(const char *filename) |
| 201 |
{ |
{ |
| 202 |
const void *p_shm; |
int64_t shmid; |
|
int shmid; |
|
| 203 |
|
|
| 204 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) |
if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) |
| 205 |
{ |
{ |
| 206 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("trie_dict_get(%s) not found\n", filename); |
| 207 |
return -1; |
return -1; |
| 208 |
} |
} |
| 209 |
|
|
|
shmid = *((int *)p_shm); |
|
|
|
|
|
if (shmdt(p_shm) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); |
|
|
} |
|
|
|
|
| 210 |
if (shmctl((int)shmid, IPC_RMID, NULL) == -1) |
if (shmctl((int)shmid, IPC_RMID, NULL) == -1) |
| 211 |
{ |
{ |
| 212 |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); |
| 221 |
return 0; |
return 0; |
| 222 |
} |
} |
| 223 |
|
|
| 224 |
const void *get_file_shm(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) |
const void *get_file_shm_readonly(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) |
| 225 |
{ |
{ |
| 226 |
|
int64_t shmid; |
| 227 |
const void *p_shm; |
const void *p_shm; |
| 228 |
|
|
| 229 |
if (p_trie_file_dict == NULL) |
if (p_trie_file_dict == NULL) |
| 232 |
return NULL; |
return NULL; |
| 233 |
} |
} |
| 234 |
|
|
| 235 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) // Not exist |
if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) // Not exist |
| 236 |
{ |
{ |
| 237 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("trie_dict_get(%s) not found\n", filename); |
| 238 |
return NULL; |
return NULL; |
| 239 |
} |
} |
| 240 |
|
|
| 241 |
*p_data_len = *((size_t *)(p_shm + sizeof(int))); |
p_shm = shmat((int)shmid, NULL, SHM_RDONLY); |
| 242 |
*p_line_total = *((long *)(p_shm + sizeof(int) + sizeof(size_t))); |
if (p_shm == (void *)-1) |
| 243 |
*pp_data = p_shm + sizeof(int) + sizeof(size_t) + sizeof(long); |
{ |
| 244 |
|
log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno); |
| 245 |
|
return NULL; |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
*p_data_len = ((struct shm_header_t *)p_shm)->data_len; |
| 249 |
|
*p_line_total = ((struct shm_header_t *)p_shm)->line_total; |
| 250 |
|
*pp_data = p_shm + sizeof(struct shm_header_t); |
| 251 |
*pp_line_offsets = *pp_data + *p_data_len + 1; |
*pp_line_offsets = *pp_data + *p_data_len + 1; |
| 252 |
|
|
| 253 |
return p_shm; |
return p_shm; |
| 254 |
} |
} |
| 255 |
|
|
| 256 |
|
int detach_file_shm(const void *p_shm) |
| 257 |
|
{ |
| 258 |
|
if (p_shm == NULL) |
| 259 |
|
{ |
| 260 |
|
return -2; |
| 261 |
|
} |
| 262 |
|
|
| 263 |
|
if (shmdt(p_shm) == -1) |
| 264 |
|
{ |
| 265 |
|
log_error("shmdt() error (%d)\n", errno); |
| 266 |
|
return -1; |
| 267 |
|
} |
| 268 |
|
|
| 269 |
|
return 0; |
| 270 |
|
} |