| line |
true |
false |
branch |
|
161
|
0 |
142 |
if (v < 2) return 2; |
|
162
|
0 |
142 |
if (v > 0x80000000U) return 0; |
|
179
|
80 |
34 |
if (needed <= sub->copy_buf_cap) return 1; |
|
180
|
4 |
30 |
uint32_t ns = sub->copy_buf_cap ? sub->copy_buf_cap : 64; |
|
181
|
11 |
34 |
while (ns < needed) { |
|
183
|
0 |
11 |
if (n2 <= ns) { ns = needed; break; } |
|
187
|
0 |
34 |
if (!nb) return 0; |
|
202
|
0 |
0 |
if (pid == 0) return 1; |
|
203
|
0 |
0 |
return !(kill((pid_t)pid, 0) == -1 && errno == ESRCH); |
|
|
0 |
0 |
return !(kill((pid_t)pid, 0) == -1 && errno == ESRCH); |
|
209
|
0 |
0 |
if (!__atomic_compare_exchange_n(&hdr->mutex, &observed, 0, |
|
213
|
0 |
0 |
if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0) |
|
221
|
190 |
0 |
if (__atomic_compare_exchange_n(&hdr->mutex, &expected, mypid, |
|
224
|
0 |
0 |
if (__builtin_expect(spin < PUBSUB_SPIN_LIMIT, 1)) { |
|
230
|
0 |
0 |
if (cur != 0) { |
|
233
|
0 |
0 |
if (rc == -1 && errno == ETIMEDOUT) { |
|
|
0 |
0 |
if (rc == -1 && errno == ETIMEDOUT) { |
|
236
|
0 |
0 |
if (val >= PUBSUB_MUTEX_WRITER_BIT) { |
|
238
|
0 |
0 |
if (!pubsub_pid_alive(pid)) |
|
252
|
0 |
190 |
if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0) |
|
263
|
97 |
672 |
if (__atomic_load_n(&hdr->sub_waiters, __ATOMIC_RELAXED) > 0) { |
|
275
|
7 |
4 |
if (remaining->tv_nsec < 0) { |
|
286
|
1 |
10 |
if (deadline->tv_nsec >= 1000000000L) { |
|
298
|
20 |
0 |
if (hdr->magic != PUBSUB_MAGIC || |
|
299
|
20 |
0 |
hdr->version != PUBSUB_VERSION || |
|
300
|
16 |
4 |
hdr->mode != mode || |
|
301
|
16 |
0 |
hdr->capacity == 0 || |
|
302
|
16 |
0 |
(hdr->capacity & (hdr->capacity - 1)) != 0 || |
|
303
|
16 |
0 |
hdr->total_size != file_size || |
|
304
|
0 |
16 |
hdr->slots_off != sizeof(PubSubHeader)) |
|
308
|
6 |
10 |
: (mode == PUBSUB_MODE_INT32) ? sizeof(PubSubInt32Slot) |
|
309
|
5 |
1 |
: (mode == PUBSUB_MODE_INT16) ? sizeof(PubSubInt16Slot) |
|
310
|
1 |
4 |
: sizeof(PubSubStrSlot); |
|
311
|
0 |
16 |
if (hdr->capacity > (hdr->total_size - hdr->slots_off) / slot_size) |
|
313
|
4 |
12 |
if (mode == PUBSUB_MODE_STR) { |
|
314
|
4 |
0 |
if (hdr->data_off == 0 || hdr->msg_size == 0 || hdr->arena_cap == 0) |
|
|
4 |
0 |
if (hdr->data_off == 0 || hdr->msg_size == 0 || hdr->arena_cap == 0) |
|
|
0 |
4 |
if (hdr->data_off == 0 || hdr->msg_size == 0 || hdr->arena_cap == 0) |
|
317
|
4 |
0 |
if (hdr->data_off < slots_end || |
|
318
|
0 |
4 |
hdr->data_off + hdr->arena_cap > hdr->total_size) |
|
334
|
0 |
142 |
if (!h) return NULL; |
|
338
|
50 |
92 |
h->data = (mode == PUBSUB_MODE_STR) ? (char *)base + hdr->data_off : NULL; |
|
344
|
47 |
95 |
h->path = path ? strdup(path) : NULL; |
|
375
|
75 |
67 |
: (mode == PUBSUB_MODE_INT32) ? sizeof(PubSubInt32Slot) |
|
376
|
62 |
13 |
: (mode == PUBSUB_MODE_INT16) ? sizeof(PubSubInt16Slot) |
|
377
|
12 |
50 |
: sizeof(PubSubStrSlot); |
|
380
|
50 |
92 |
if (mode == PUBSUB_MODE_STR) { |
|
391
|
0 |
50 |
if (arena_cap > UINT32_MAX) { |
|
410
|
138 |
0 |
if (errbuf) errbuf[0] = '\0'; |
|
413
|
0 |
138 |
if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; } |
|
|
0 |
0 |
if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; } |
|
414
|
0 |
138 |
if (mode > PUBSUB_MODE_INT16) { PUBSUB_ERR("unknown mode %u", mode); return NULL; } |
|
|
0 |
0 |
if (mode > PUBSUB_MODE_INT16) { PUBSUB_ERR("unknown mode %u", mode); return NULL; } |
|
416
|
49 |
89 |
if (mode == PUBSUB_MODE_STR && msg_size == 0) |
|
|
35 |
14 |
if (mode == PUBSUB_MODE_STR && msg_size == 0) |
|
420
|
0 |
138 |
if (!pubsub_calc_layout(cap, mode, msg_size, &slots_off, &data_off, &arena_cap, &total_size)) { |
|
421
|
0 |
0 |
PUBSUB_ERR("capacity * (msg_size+8) exceeds 4GB arena limit"); |
|
429
|
87 |
51 |
if (anonymous) { |
|
433
|
0 |
87 |
if (base == MAP_FAILED) { |
|
434
|
0 |
0 |
PUBSUB_ERR("mmap(anonymous): %s", strerror(errno)); |
|
441
|
0 |
51 |
if (fd < 0) { PUBSUB_ERR("open(%s): %s", path, strerror(errno)); return NULL; } |
|
|
0 |
0 |
if (fd < 0) { PUBSUB_ERR("open(%s): %s", path, strerror(errno)); return NULL; } |
|
443
|
0 |
51 |
if (flock(fd, LOCK_EX) < 0) { |
|
444
|
0 |
0 |
PUBSUB_ERR("flock(%s): %s", path, strerror(errno)); |
|
449
|
0 |
51 |
if (fstat(fd, &st) < 0) { |
|
450
|
0 |
0 |
PUBSUB_ERR("fstat(%s): %s", path, strerror(errno)); |
|
456
|
16 |
35 |
if (!is_new && (uint64_t)st.st_size < sizeof(PubSubHeader)) { |
|
|
0 |
16 |
if (!is_new && (uint64_t)st.st_size < sizeof(PubSubHeader)) { |
|
457
|
0 |
0 |
PUBSUB_ERR("%s: file too small (%lld)", path, (long long)st.st_size); |
|
461
|
35 |
16 |
if (is_new) { |
|
462
|
0 |
35 |
if (ftruncate(fd, (off_t)total_size) < 0) { |
|
463
|
0 |
0 |
PUBSUB_ERR("ftruncate(%s): %s", path, strerror(errno)); |
|
468
|
16 |
35 |
map_size = is_new ? (size_t)total_size : (size_t)st.st_size; |
|
470
|
0 |
51 |
if (base == MAP_FAILED) { |
|
471
|
0 |
0 |
PUBSUB_ERR("mmap(%s): %s", path, strerror(errno)); |
|
475
|
16 |
35 |
if (!is_new) { |
|
476
|
4 |
12 |
if (!pubsub_validate_header((PubSubHeader *)base, mode, (uint64_t)st.st_size)) { |
|
477
|
4 |
0 |
PUBSUB_ERR("%s: invalid or incompatible pubsub file", path); |
|
492
|
0 |
122 |
if (!h) { munmap(base, (size_t)total_size); return NULL; } |
|
499
|
4 |
0 |
if (errbuf) errbuf[0] = '\0'; |
|
502
|
0 |
4 |
if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; } |
|
|
0 |
0 |
if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; } |
|
503
|
0 |
4 |
if (mode > PUBSUB_MODE_INT16) { PUBSUB_ERR("unknown mode %u", mode); return NULL; } |
|
|
0 |
0 |
if (mode > PUBSUB_MODE_INT16) { PUBSUB_ERR("unknown mode %u", mode); return NULL; } |
|
505
|
1 |
3 |
if (mode == PUBSUB_MODE_STR && msg_size == 0) |
|
|
1 |
0 |
if (mode == PUBSUB_MODE_STR && msg_size == 0) |
|
509
|
0 |
4 |
if (!pubsub_calc_layout(cap, mode, msg_size, &slots_off, &data_off, &arena_cap, &total_size)) { |
|
510
|
0 |
0 |
PUBSUB_ERR("capacity * (msg_size+8) exceeds 4GB arena limit"); |
|
514
|
4 |
0 |
int fd = memfd_create(name ? name : "pubsub", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
|
515
|
0 |
4 |
if (fd < 0) { PUBSUB_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
|
|
0 |
0 |
if (fd < 0) { PUBSUB_ERR("memfd_create: %s", strerror(errno)); return NULL; } |
|
517
|
0 |
4 |
if (ftruncate(fd, (off_t)total_size) < 0) { |
|
518
|
0 |
0 |
PUBSUB_ERR("ftruncate(memfd): %s", strerror(errno)); |
|
524
|
0 |
4 |
if (base == MAP_FAILED) { |
|
525
|
0 |
0 |
PUBSUB_ERR("mmap(memfd): %s", strerror(errno)); |
|
533
|
0 |
4 |
if (!h) { munmap(base, (size_t)total_size); close(fd); return NULL; } |
|
539
|
4 |
0 |
if (errbuf) errbuf[0] = '\0'; |
|
542
|
0 |
4 |
if (fstat(fd, &st) < 0) { |
|
543
|
0 |
0 |
PUBSUB_ERR("fstat(fd=%d): %s", fd, strerror(errno)); |
|
547
|
0 |
4 |
if ((uint64_t)st.st_size < sizeof(PubSubHeader)) { |
|
548
|
0 |
0 |
PUBSUB_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size); |
|
554
|
0 |
4 |
if (base == MAP_FAILED) { |
|
555
|
0 |
0 |
PUBSUB_ERR("mmap(fd=%d): %s", fd, strerror(errno)); |
|
559
|
0 |
4 |
if (!pubsub_validate_header((PubSubHeader *)base, mode, (uint64_t)st.st_size)) { |
|
560
|
0 |
0 |
PUBSUB_ERR("fd %d: invalid or incompatible pubsub", fd); |
|
566
|
0 |
4 |
if (myfd < 0) { |
|
567
|
0 |
0 |
PUBSUB_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno)); |
|
573
|
0 |
4 |
if (!h) { munmap(base, map_size); close(myfd); return NULL; } |
|
579
|
0 |
142 |
if (!h) return; |
|
580
|
13 |
129 |
if (h->notify_fd >= 0) close(h->notify_fd); |
|
581
|
8 |
134 |
if (h->backing_fd >= 0) close(h->backing_fd); |
|
582
|
142 |
0 |
if (h->hdr) munmap(h->hdr, h->mmap_size); |
|
593
|
0 |
104 |
if (!sub) return NULL; |
|
605
|
64 |
40 |
if (from_oldest && wp > h->capacity) |
|
|
1 |
63 |
if (from_oldest && wp > h->capacity) |
|
607
|
63 |
40 |
else if (from_oldest) |
|
616
|
0 |
104 |
if (!sub) return; |
|
627
|
22 |
10 |
return (wp > sub->cursor) ? (wp - sub->cursor) : 0; |
|
744
|
5 |
6 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
6 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
6 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
6 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
6 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
6 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
4 |
2 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
2 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
2 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
30 |
1186 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
2 |
1184 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
1184 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
0 |
1184 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
1216 |
0 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
|
208 |
4 |
DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t) |
|
747
|
1 |
1 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
7 |
31 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
1 |
30 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
30 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
0 |
30 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
38 |
0 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
|
3 |
1 |
DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t) |
|
750
|
1 |
1 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
1 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
1 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
7 |
31 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
1 |
30 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
30 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
0 |
30 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
38 |
0 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
|
3 |
1 |
DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t) |
|
763
|
0 |
197 |
if (len > PUBSUB_STR_LEN_MASK) return -1; |
|
764
|
1 |
196 |
if (len > h->msg_size) return -1; |
|
777
|
3 |
193 |
if (alloc == 0) alloc = 8; |
|
778
|
0 |
196 |
if (alloc > h->arena_cap) return -1; |
|
780
|
1 |
195 |
if ((uint64_t)apos + alloc > h->arena_cap) |
|
786
|
3 |
193 |
slot->packed_len = len | (utf8 ? PUBSUB_STR_UTF8_FLAG : 0); |
|
799
|
2 |
184 |
if (len > h->msg_size) return -1; |
|
803
|
184 |
0 |
if (r == 1) pubsub_wake_subscribers(h->hdr); |
|
813
|
134 |
0 |
for (int attempt = 0; attempt < PUBSUB_POLL_RETRIES; attempt++) { |
|
817
|
18 |
116 |
if (cursor >= wp) return 0; |
|
819
|
2 |
114 |
if (wp - cursor > sub->capacity) { |
|
829
|
0 |
114 |
if (seq1 != cursor + 1) { |
|
830
|
0 |
0 |
if (seq1 > cursor + 1) { |
|
831
|
0 |
0 |
uint64_t new_cursor = wp > sub->capacity ? wp - sub->capacity : 0; |
|
832
|
0 |
0 |
if (new_cursor > cursor) |
|
846
|
0 |
114 |
if (len > sub->msg_size) continue; |
|
847
|
0 |
114 |
if ((uint64_t)aoff + len > sub->hdr->arena_cap) continue; |
|
849
|
0 |
114 |
if (!pubsub_ensure_copy_buf(sub, len + 1)) return 0; |
|
851
|
111 |
3 |
if (len > 0) |
|
856
|
0 |
114 |
if (seq2 != seq1) continue; |
|
871
|
52 |
3 |
if (r != 0) return r; |
|
872
|
0 |
3 |
if (timeout == 0.0) return 0; |
|
877
|
3 |
0 |
if (has_deadline) pubsub_make_deadline(timeout, &deadline); |
|
884
|
0 |
3 |
if (r != 0) { |
|
890
|
3 |
0 |
if (has_deadline) { |
|
891
|
0 |
3 |
if (!pubsub_remaining_time(&deadline, &remaining)) { |
|
902
|
2 |
1 |
if (r != 0) return r; |
|
903
|
1 |
0 |
if (rc == -1 && errno == ETIMEDOUT) return 0; |
|
|
1 |
0 |
if (rc == -1 && errno == ETIMEDOUT) return 0; |
|
913
|
2 |
6 |
if (hdr->mode == PUBSUB_MODE_STR) |
|
918
|
2 |
6 |
if (hdr->mode == PUBSUB_MODE_STR) |
|
923
|
4 |
4 |
if (hdr->mode == PUBSUB_MODE_INT) { |
|
925
|
256 |
4 |
for (uint32_t i = 0; i < cap; i++) |
|
927
|
1 |
3 |
} else if (hdr->mode == PUBSUB_MODE_INT32) { |
|
929
|
64 |
1 |
for (uint32_t i = 0; i < cap; i++) |
|
931
|
1 |
2 |
} else if (hdr->mode == PUBSUB_MODE_INT16) { |
|
933
|
64 |
1 |
for (uint32_t i = 0; i < cap; i++) |
|
937
|
80 |
2 |
for (uint32_t i = 0; i < cap; i++) |
|
941
|
2 |
6 |
if (hdr->mode == PUBSUB_MODE_STR) |
|
951
|
0 |
13 |
if (h->notify_fd >= 0) return h->notify_fd; |
|
957
|
1 |
0 |
if (h->notify_fd >= 0 && h->notify_fd != fd) |
|
|
0 |
1 |
if (h->notify_fd >= 0 && h->notify_fd != fd) |
|
963
|
8 |
0 |
if (h->notify_fd >= 0) { |
|
970
|
0 |
6 |
if (h->notify_fd < 0) return -1; |
|
972
|
0 |
6 |
if (read(h->notify_fd, &val, sizeof(val)) != sizeof(val)) return -1; |
|
977
|
4 |
0 |
if (sub->notify_fd >= 0) { |