| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "file_loader.h" |
#include "file_loader.h" |
|
#include "trie_dict.h" |
|
|
#include "str_process.h" |
|
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include <fcntl.h> |
#include "str_process.h" |
| 20 |
|
#include "trie_dict.h" |
| 21 |
#include <errno.h> |
#include <errno.h> |
| 22 |
#include <unistd.h> |
#include <fcntl.h> |
| 23 |
#include <stdlib.h> |
#include <stdlib.h> |
| 24 |
#include <string.h> |
#include <string.h> |
| 25 |
#include <time.h> |
#include <time.h> |
| 26 |
|
#include <unistd.h> |
| 27 |
|
#include <sys/ipc.h> |
| 28 |
#include <sys/mman.h> |
#include <sys/mman.h> |
|
#include <sys/stat.h> |
|
| 29 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 30 |
#include <sys/ipc.h> |
#include <sys/stat.h> |
| 31 |
|
|
| 32 |
#define MAX_SPLIT_FILE_LINES 65536 |
struct shm_header_t |
| 33 |
|
{ |
| 34 |
|
int shmid; |
| 35 |
|
size_t data_len; |
| 36 |
|
long line_total; |
| 37 |
|
}; |
| 38 |
|
|
| 39 |
static TRIE_NODE *p_trie_file_dict = NULL; |
static TRIE_NODE *p_trie_file_dict = NULL; |
| 40 |
|
|
| 56 |
return 0; |
return 0; |
| 57 |
} |
} |
| 58 |
|
|
| 59 |
static void trie_file_dict_cleanup_cb(const char *filename, int64_t shmid) |
static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) |
| 60 |
{ |
{ |
| 61 |
if (shmctl((int)shmid, IPC_RMID, NULL) == -1) |
int shmid = (int)value; |
| 62 |
|
|
| 63 |
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 64 |
{ |
{ |
| 65 |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); |
log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); |
| 66 |
} |
} |
| 67 |
} |
} |
| 68 |
|
|
| 75 |
|
|
| 76 |
trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); |
trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); |
| 77 |
trie_dict_destroy(p_trie_file_dict); |
trie_dict_destroy(p_trie_file_dict); |
| 78 |
|
|
| 79 |
|
p_trie_file_dict = NULL; |
| 80 |
} |
} |
| 81 |
|
|
| 82 |
int load_file_shm(const char *filename) |
int load_file(const char *filename) |
| 83 |
{ |
{ |
| 84 |
int fd; |
int fd; |
| 85 |
struct stat sb; |
struct stat sb; |
| 89 |
key_t key; |
key_t key; |
| 90 |
size_t size; |
size_t size; |
| 91 |
int shmid; |
int shmid; |
|
int64_t shmid_old; |
|
| 92 |
void *p_shm; |
void *p_shm; |
| 93 |
long line_total; |
long line_total; |
| 94 |
long line_offsets[MAX_SPLIT_FILE_LINES]; |
long line_offsets[MAX_SPLIT_FILE_LINES]; |
| 95 |
long *p_line_offsets; |
long *p_line_offsets; |
| 96 |
|
int64_t shmid_old; |
| 97 |
|
|
| 98 |
if ((fd = open(filename, O_RDONLY)) < 0) |
if ((fd = open(filename, O_RDONLY)) < 0) |
| 99 |
{ |
{ |
| 121 |
return -1; |
return -1; |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES); |
line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL); |
|
if (line_total >= MAX_SPLIT_FILE_LINES) |
|
|
{ |
|
|
log_error("split_data_lines() truncated over limit lines\n"); |
|
|
} |
|
| 125 |
|
|
| 126 |
// Allocate shared memory |
// Allocate shared memory |
| 127 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 132 |
return -2; |
return -2; |
| 133 |
} |
} |
| 134 |
|
|
| 135 |
size = 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); |
| 136 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 137 |
if (shmid == -1) |
if (shmid == -1) |
| 138 |
{ |
{ |
| 142 |
p_shm = shmat(shmid, NULL, 0); |
p_shm = shmat(shmid, NULL, 0); |
| 143 |
if (p_shm == (void *)-1) |
if (p_shm == (void *)-1) |
| 144 |
{ |
{ |
| 145 |
log_error("shmat() error (%d)\n", errno); |
log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); |
| 146 |
return -3; |
return -3; |
| 147 |
} |
} |
| 148 |
|
|
| 149 |
*((size_t *)p_shm) = data_len; |
((struct shm_header_t *)p_shm)->shmid = shmid; |
| 150 |
*((long *)(p_shm + sizeof(data_len))) = line_total; |
((struct shm_header_t *)p_shm)->data_len = data_len; |
| 151 |
memcpy(p_shm + sizeof(data_len) + sizeof(line_total), p_data, data_len); |
((struct shm_header_t *)p_shm)->line_total = line_total; |
| 152 |
|
memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len); |
| 153 |
|
|
| 154 |
if (munmap(p_data, data_len) < 0) |
if (munmap(p_data, data_len) < 0) |
| 155 |
{ |
{ |
| 157 |
return -2; |
return -2; |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
p_data = p_shm + sizeof(data_len) + sizeof(line_total); |
p_data = p_shm + sizeof(struct shm_header_t); |
| 161 |
p_line_offsets = p_data + data_len + 1; |
p_line_offsets = p_data + data_len + 1; |
| 162 |
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)); |
| 163 |
|
|
| 164 |
if (shmdt(p_shm) == -1) |
if (shmdt(p_shm) == -1) |
| 165 |
{ |
{ |
| 166 |
log_error("shmdt() error (%d)\n", errno); |
log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); |
| 167 |
return -3; |
return -3; |
| 168 |
} |
} |
| 169 |
|
|
| 191 |
return 0; |
return 0; |
| 192 |
} |
} |
| 193 |
|
|
| 194 |
int unload_file_shm(const char *filename) |
int unload_file(const char *filename) |
| 195 |
{ |
{ |
| 196 |
int64_t shmid = 0; |
int64_t shmid; |
| 197 |
|
|
| 198 |
if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&shmid) != 1) |
if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) |
| 199 |
{ |
{ |
| 200 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("trie_dict_get(%s) not found\n", filename); |
| 201 |
return -1; |
return -1; |
| 215 |
return 0; |
return 0; |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
const void *get_file_shm(const char *filename) |
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) |
| 219 |
{ |
{ |
| 220 |
int64_t shmid; |
int64_t shmid; |
| 221 |
const void *p_shm; |
const void *p_shm; |
| 235 |
p_shm = shmat((int)shmid, NULL, SHM_RDONLY); |
p_shm = shmat((int)shmid, NULL, SHM_RDONLY); |
| 236 |
if (p_shm == (void *)-1) |
if (p_shm == (void *)-1) |
| 237 |
{ |
{ |
| 238 |
log_error("shmat() error (%d)\n", errno); |
log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno); |
| 239 |
return NULL; |
return NULL; |
| 240 |
} |
} |
| 241 |
|
|
| 242 |
|
*p_data_len = ((struct shm_header_t *)p_shm)->data_len; |
| 243 |
|
*p_line_total = ((struct shm_header_t *)p_shm)->line_total; |
| 244 |
|
*pp_data = p_shm + sizeof(struct shm_header_t); |
| 245 |
|
*pp_line_offsets = *pp_data + *p_data_len + 1; |
| 246 |
|
|
| 247 |
return p_shm; |
return p_shm; |
| 248 |
} |
} |
| 249 |
|
|
| 250 |
|
int detach_file_shm(const void *p_shm) |
| 251 |
|
{ |
| 252 |
|
if (p_shm == NULL) |
| 253 |
|
{ |
| 254 |
|
return -2; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
if (shmdt(p_shm) == -1) |
| 258 |
|
{ |
| 259 |
|
log_error("shmdt() error (%d)\n", errno); |
| 260 |
|
return -1; |
| 261 |
|
} |
| 262 |
|
|
| 263 |
|
return 0; |
| 264 |
|
} |