r911: check before attempting adding
This commit is contained in:
parent
67c2d69218
commit
c280274331
59
bwashm.c
59
bwashm.c
|
|
@ -13,15 +13,15 @@ int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn)
|
|||
{
|
||||
const char *name;
|
||||
uint8_t *shm, *shm_idx;
|
||||
uint16_t *cnt, i;
|
||||
uint16_t *cnt;
|
||||
int shmid, to_init = 0, l;
|
||||
char path[PATH_MAX + 1], *p, *tmpfn = (char*)_tmpfn;
|
||||
char path[PATH_MAX + 1], *tmpfn = (char*)_tmpfn;
|
||||
|
||||
if (hint == 0 || hint[0] == 0) return -1;
|
||||
for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
|
||||
++name;
|
||||
|
||||
if ((shmid = shm_open("/bwactl", O_RDWR, 0444)) < 0) {
|
||||
if ((shmid = shm_open("/bwactl", O_RDWR, 0)) < 0) {
|
||||
shmid = shm_open("/bwactl", O_CREAT|O_RDWR|O_EXCL, 0644);
|
||||
to_init = 1;
|
||||
}
|
||||
|
|
@ -32,15 +32,6 @@ int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn)
|
|||
if (to_init) {
|
||||
memset(shm, 0, BWA_CTL_SIZE);
|
||||
cnt[1] = 4;
|
||||
} else {
|
||||
for (i = 0, p = (char*)shm + 4; i < cnt[0]; ++i) {
|
||||
if (strcmp(p + 8, name) == 0) break;
|
||||
p += 9 + strlen(p + 8);
|
||||
}
|
||||
if (i < cnt[0]) {
|
||||
fprintf(stderr, "[W::%s] index '%s' is already in shared memory\n", __func__, name);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (idx->mem == 0) bwa_idx2mem(idx);
|
||||
|
|
@ -106,7 +97,7 @@ bwaidx_t *bwa_idx_load_from_shm(const char *hint)
|
|||
if (hint == 0 || hint[0] == 0) return 0;
|
||||
for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
|
||||
++name;
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0444)) < 0) return 0;
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return 0;
|
||||
shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
|
||||
cnt = (uint16_t*)shm;
|
||||
if (cnt[0] == 0) return 0;
|
||||
|
|
@ -118,7 +109,7 @@ bwaidx_t *bwa_idx_load_from_shm(const char *hint)
|
|||
if (i == cnt[0]) return 0;
|
||||
|
||||
strcat(strcpy(path, "/bwaidx-"), name);
|
||||
if ((shmid = shm_open(path, O_RDONLY, 0444)) < 0) return 0;
|
||||
if ((shmid = shm_open(path, O_RDONLY, 0)) < 0) return 0;
|
||||
shm_idx = mmap(0, l_mem, PROT_READ, MAP_SHARED, shmid, 0);
|
||||
idx = calloc(1, sizeof(bwaidx_t));
|
||||
bwa_mem2idx(l_mem, shm_idx, idx);
|
||||
|
|
@ -126,12 +117,32 @@ bwaidx_t *bwa_idx_load_from_shm(const char *hint)
|
|||
return idx;
|
||||
}
|
||||
|
||||
int bwa_shm_test(const char *hint)
|
||||
{
|
||||
int shmid;
|
||||
uint16_t *cnt, i;
|
||||
char *p, *shm;
|
||||
const char *name;
|
||||
|
||||
if (hint == 0 || hint[0] == 0) return 0;
|
||||
for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
|
||||
++name;
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return 0;
|
||||
shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
|
||||
cnt = (uint16_t*)shm;
|
||||
for (i = 0, p = shm + 4; i < cnt[0]; ++i) {
|
||||
if (strcmp(p + 8, name) == 0) return 1;
|
||||
p += strlen(p) + 9;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bwa_shm_list(void)
|
||||
{
|
||||
int shmid;
|
||||
uint16_t *cnt, i;
|
||||
char *p, *shm;
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0444)) < 0) return -1;
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return -1;
|
||||
shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
|
||||
cnt = (uint16_t*)shm;
|
||||
for (i = 0, p = shm + 4; i < cnt[0]; ++i) {
|
||||
|
|
@ -150,7 +161,7 @@ int bwa_shm_destroy(void)
|
|||
char *p, *shm;
|
||||
char path[PATH_MAX + 1];
|
||||
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0444)) < 0) return -1;
|
||||
if ((shmid = shm_open("/bwactl", O_RDONLY, 0)) < 0) return -1;
|
||||
shm = mmap(0, BWA_CTL_SIZE, PROT_READ, MAP_SHARED, shmid, 0);
|
||||
cnt = (uint16_t*)shm;
|
||||
for (i = 0, p = shm + 4; i < cnt[0]; ++i) {
|
||||
|
|
@ -186,13 +197,15 @@ int main_shm(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
if (optind < argc) {
|
||||
bwaidx_t *idx;
|
||||
idx = bwa_idx_load_from_disk(argv[optind], BWA_IDX_ALL);
|
||||
if (bwa_shm_stage(idx, argv[optind], tmpfn) < 0) {
|
||||
fprintf(stderr, "[E::%s] failed to stage the index in shared memory\n", __func__);
|
||||
ret = 1;
|
||||
}
|
||||
bwa_idx_destroy(idx);
|
||||
if (bwa_shm_test(argv[optind]) == 0) {
|
||||
bwaidx_t *idx;
|
||||
idx = bwa_idx_load_from_disk(argv[optind], BWA_IDX_ALL);
|
||||
if (bwa_shm_stage(idx, argv[optind], tmpfn) < 0) {
|
||||
fprintf(stderr, "[E::%s] failed to stage the index in shared memory\n", __func__);
|
||||
ret = 1;
|
||||
}
|
||||
bwa_idx_destroy(idx);
|
||||
} else fprintf(stderr, "[M::%s] index '%s' is already in shared memory\n", __func__, argv[optind]);
|
||||
}
|
||||
if (to_list) bwa_shm_list();
|
||||
if (to_drop) bwa_shm_destroy();
|
||||
|
|
|
|||
Loading…
Reference in New Issue