Branch Coverage

pubsub.h
Criterion Covered Total %
branch 236 428 55.1


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)
257 97 672 if (__atomic_load_n(&hdr->sub_waiters, __ATOMIC_RELAXED) > 0) {
269 6 5 if (remaining->tv_nsec < 0) {
280 0 11 if (deadline->tv_nsec >= 1000000000L) {
292 20 0 if (hdr->magic != PUBSUB_MAGIC ||
293 20 0 hdr->version != PUBSUB_VERSION ||
294 16 4 hdr->mode != mode ||
295 16 0 hdr->capacity == 0 ||
296 16 0 (hdr->capacity & (hdr->capacity - 1)) != 0 ||
297 16 0 hdr->total_size != file_size ||
298 0 16 hdr->slots_off != sizeof(PubSubHeader))
302 6 10 : (mode == PUBSUB_MODE_INT32) ? sizeof(PubSubInt32Slot)
303 5 1 : (mode == PUBSUB_MODE_INT16) ? sizeof(PubSubInt16Slot)
304 1 4 : sizeof(PubSubStrSlot);
305 0 16 if (hdr->capacity > (hdr->total_size - hdr->slots_off) / slot_size)
307 4 12 if (mode == PUBSUB_MODE_STR) {
308 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)
311 4 0 if (hdr->data_off < slots_end ||
312 0 4 hdr->data_off + hdr->arena_cap > hdr->total_size)
328 0 142 if (!h) return NULL;
332 50 92 h->data = (mode == PUBSUB_MODE_STR) ? (char *)base + hdr->data_off : NULL;
338 47 95 h->path = path ? strdup(path) : NULL;
367 75 67 : (mode == PUBSUB_MODE_INT32) ? sizeof(PubSubInt32Slot)
368 62 13 : (mode == PUBSUB_MODE_INT16) ? sizeof(PubSubInt16Slot)
369 12 50 : sizeof(PubSubStrSlot);
372 50 92 if (mode == PUBSUB_MODE_STR) {
376 0 50 if (arena_cap > UINT32_MAX) arena_cap = UINT32_MAX;
391 138 0 if (errbuf) errbuf[0] = '\0';
394 0 138 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
0 0 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
395 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; }
397 49 89 if (mode == PUBSUB_MODE_STR && msg_size == 0)
35 14 if (mode == PUBSUB_MODE_STR && msg_size == 0)
407 87 51 if (anonymous) {
411 0 87 if (base == MAP_FAILED) {
412 0 0 PUBSUB_ERR("mmap(anonymous): %s", strerror(errno));
419 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; }
421 0 51 if (flock(fd, LOCK_EX) < 0) {
422 0 0 PUBSUB_ERR("flock(%s): %s", path, strerror(errno));
427 0 51 if (fstat(fd, &st) < 0) {
428 0 0 PUBSUB_ERR("fstat(%s): %s", path, strerror(errno));
434 16 35 if (!is_new && (uint64_t)st.st_size < sizeof(PubSubHeader)) {
0 16 if (!is_new && (uint64_t)st.st_size < sizeof(PubSubHeader)) {
435 0 0 PUBSUB_ERR("%s: file too small (%lld)", path, (long long)st.st_size);
439 35 16 if (is_new) {
440 0 35 if (ftruncate(fd, (off_t)total_size) < 0) {
441 0 0 PUBSUB_ERR("ftruncate(%s): %s", path, strerror(errno));
446 16 35 map_size = is_new ? (size_t)total_size : (size_t)st.st_size;
448 0 51 if (base == MAP_FAILED) {
449 0 0 PUBSUB_ERR("mmap(%s): %s", path, strerror(errno));
453 16 35 if (!is_new) {
454 4 12 if (!pubsub_validate_header((PubSubHeader *)base, mode, (uint64_t)st.st_size)) {
455 4 0 PUBSUB_ERR("%s: invalid or incompatible pubsub file", path);
470 0 122 if (!h) { munmap(base, (size_t)total_size); return NULL; }
477 4 0 if (errbuf) errbuf[0] = '\0';
480 0 4 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
0 0 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
481 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; }
483 1 3 if (mode == PUBSUB_MODE_STR && msg_size == 0)
1 0 if (mode == PUBSUB_MODE_STR && msg_size == 0)
489 4 0 int fd = memfd_create(name ? name : "pubsub", MFD_CLOEXEC | MFD_ALLOW_SEALING);
490 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; }
492 0 4 if (ftruncate(fd, (off_t)total_size) < 0) {
493 0 0 PUBSUB_ERR("ftruncate(memfd): %s", strerror(errno));
499 0 4 if (base == MAP_FAILED) {
500 0 0 PUBSUB_ERR("mmap(memfd): %s", strerror(errno));
508 0 4 if (!h) { munmap(base, (size_t)total_size); close(fd); return NULL; }
514 4 0 if (errbuf) errbuf[0] = '\0';
517 0 4 if (fstat(fd, &st) < 0) {
518 0 0 PUBSUB_ERR("fstat(fd=%d): %s", fd, strerror(errno));
522 0 4 if ((uint64_t)st.st_size < sizeof(PubSubHeader)) {
523 0 0 PUBSUB_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size);
529 0 4 if (base == MAP_FAILED) {
530 0 0 PUBSUB_ERR("mmap(fd=%d): %s", fd, strerror(errno));
534 0 4 if (!pubsub_validate_header((PubSubHeader *)base, mode, (uint64_t)st.st_size)) {
535 0 0 PUBSUB_ERR("fd %d: invalid or incompatible pubsub", fd);
541 0 4 if (myfd < 0) {
542 0 0 PUBSUB_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno));
548 0 4 if (!h) { munmap(base, map_size); close(myfd); return NULL; }
554 0 142 if (!h) return;
555 13 129 if (h->notify_fd >= 0) close(h->notify_fd);
556 8 134 if (h->backing_fd >= 0) close(h->backing_fd);
557 142 0 if (h->hdr) munmap(h->hdr, h->mmap_size);
568 0 104 if (!sub) return NULL;
580 64 40 if (from_oldest && wp > h->capacity)
1 63 if (from_oldest && wp > h->capacity)
582 63 40 else if (from_oldest)
591 0 104 if (!sub) return;
602 22 10 return (wp > sub->cursor) ? (wp - sub->cursor) : 0;
710 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)
713 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)
716 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)
729 0 197 if (len > PUBSUB_STR_LEN_MASK) return -1;
730 1 196 if (len > h->msg_size) return -1;
743 3 193 if (alloc == 0) alloc = 8;
744 0 196 if (alloc > h->arena_cap) return -1;
746 1 195 if ((uint64_t)apos + alloc > h->arena_cap)
752 3 193 slot->packed_len = len | (utf8 ? PUBSUB_STR_UTF8_FLAG : 0);
765 2 184 if (len > h->msg_size) return -1;
769 184 0 if (r == 1) pubsub_wake_subscribers(h->hdr);
779 134 0 for (int attempt = 0; attempt < PUBSUB_POLL_RETRIES; attempt++) {
783 18 116 if (cursor >= wp) return 0;
785 2 114 if (wp - cursor > sub->capacity) {
795 0 114 if (seq1 != cursor + 1) {
796 0 0 if (seq1 > cursor + 1) {
797 0 0 uint64_t new_cursor = wp > sub->capacity ? wp - sub->capacity : 0;
798 0 0 if (new_cursor > cursor)
812 0 114 if (len > sub->msg_size) continue;
813 0 114 if ((uint64_t)aoff + len > sub->hdr->arena_cap) continue;
815 0 114 if (!pubsub_ensure_copy_buf(sub, len + 1)) return 0;
817 111 3 if (len > 0)
822 0 114 if (seq2 != seq1) continue;
837 52 3 if (r != 0) return r;
838 0 3 if (timeout == 0.0) return 0;
843 3 0 if (has_deadline) pubsub_make_deadline(timeout, &deadline);
848 0 3 if (r != 0) return r;
852 3 0 if (has_deadline) {
853 0 3 if (!pubsub_remaining_time(&deadline, &remaining)) {
864 2 1 if (r != 0) return r;
865 1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
875 2 6 if (hdr->mode == PUBSUB_MODE_STR)
880 2 6 if (hdr->mode == PUBSUB_MODE_STR)
885 4 4 if (hdr->mode == PUBSUB_MODE_INT) {
887 256 4 for (uint32_t i = 0; i < cap; i++)
889 1 3 } else if (hdr->mode == PUBSUB_MODE_INT32) {
891 64 1 for (uint32_t i = 0; i < cap; i++)
893 1 2 } else if (hdr->mode == PUBSUB_MODE_INT16) {
895 64 1 for (uint32_t i = 0; i < cap; i++)
899 80 2 for (uint32_t i = 0; i < cap; i++)
903 2 6 if (hdr->mode == PUBSUB_MODE_STR)
913 0 13 if (h->notify_fd >= 0) return h->notify_fd;
919 1 0 if (h->notify_fd >= 0 && h->notify_fd != fd)
0 1 if (h->notify_fd >= 0 && h->notify_fd != fd)
925 8 0 if (h->notify_fd >= 0) {
932 0 6 if (h->notify_fd < 0) return -1;
934 0 6 if (read(h->notify_fd, &val, sizeof(val)) != sizeof(val)) return -1;
939 4 0 if (sub->notify_fd >= 0) {