Branch Coverage

pubsub.h
Criterion Covered Total %
branch 225 412 54.6


line true false branch
161 0 136 if (v < 2) return 2;
162 0 136 if (v > 0x80000000U) return 0;
179 79 32 if (needed <= sub->copy_buf_cap) return 1;
180 4 28 uint32_t ns = sub->copy_buf_cap ? sub->copy_buf_cap : 64;
181 11 32 while (ns < needed) {
183 0 11 if (n2 <= ns) { ns = needed; break; }
187 0 32 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 187 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 187 if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0)
257 61 700 if (__atomic_load_n(&hdr->sub_waiters, __ATOMIC_RELAXED) > 0) {
269 8 5 if (remaining->tv_nsec < 0) {
280 0 13 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))
300 4 12 if (mode == PUBSUB_MODE_STR) {
301 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)
303 0 4 if (hdr->data_off + hdr->arena_cap > hdr->total_size)
319 0 136 if (!h) return NULL;
323 48 88 h->data = (mode == PUBSUB_MODE_STR) ? (char *)base + hdr->data_off : NULL;
329 47 89 h->path = path ? strdup(path) : NULL;
358 71 65 : (mode == PUBSUB_MODE_INT32) ? sizeof(PubSubInt32Slot)
359 59 12 : (mode == PUBSUB_MODE_INT16) ? sizeof(PubSubInt16Slot)
360 11 48 : sizeof(PubSubStrSlot);
363 48 88 if (mode == PUBSUB_MODE_STR) {
367 0 48 if (arena_cap > UINT32_MAX) arena_cap = UINT32_MAX;
382 132 0 if (errbuf) errbuf[0] = '\0';
385 0 132 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
0 0 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
386 0 132 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; }
388 47 85 if (mode == PUBSUB_MODE_STR && msg_size == 0)
33 14 if (mode == PUBSUB_MODE_STR && msg_size == 0)
398 81 51 if (anonymous) {
402 0 81 if (base == MAP_FAILED) {
403 0 0 PUBSUB_ERR("mmap(anonymous): %s", strerror(errno));
408 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; }
410 0 51 if (flock(fd, LOCK_EX) < 0) {
411 0 0 PUBSUB_ERR("flock(%s): %s", path, strerror(errno));
416 0 51 if (fstat(fd, &st) < 0) {
417 0 0 PUBSUB_ERR("fstat(%s): %s", path, strerror(errno));
423 16 35 if (!is_new && (uint64_t)st.st_size < sizeof(PubSubHeader)) {
0 16 if (!is_new && (uint64_t)st.st_size < sizeof(PubSubHeader)) {
424 0 0 PUBSUB_ERR("%s: file too small (%lld)", path, (long long)st.st_size);
428 35 16 if (is_new) {
429 0 35 if (ftruncate(fd, (off_t)total_size) < 0) {
430 0 0 PUBSUB_ERR("ftruncate(%s): %s", path, strerror(errno));
435 16 35 map_size = is_new ? (size_t)total_size : (size_t)st.st_size;
437 0 51 if (base == MAP_FAILED) {
438 0 0 PUBSUB_ERR("mmap(%s): %s", path, strerror(errno));
442 16 35 if (!is_new) {
443 4 12 if (!pubsub_validate_header((PubSubHeader *)base, mode, (uint64_t)st.st_size)) {
444 4 0 PUBSUB_ERR("%s: invalid or incompatible pubsub file", path);
460 0 116 if (!h) { munmap(base, (size_t)total_size); return NULL; }
467 4 0 if (errbuf) errbuf[0] = '\0';
470 0 4 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
0 0 if (cap == 0) { PUBSUB_ERR("invalid capacity"); return NULL; }
471 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; }
473 1 3 if (mode == PUBSUB_MODE_STR && msg_size == 0)
1 0 if (mode == PUBSUB_MODE_STR && msg_size == 0)
479 4 0 int fd = memfd_create(name ? name : "pubsub", MFD_CLOEXEC);
480 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; }
482 0 4 if (ftruncate(fd, (off_t)total_size) < 0) {
483 0 0 PUBSUB_ERR("ftruncate(memfd): %s", strerror(errno));
488 0 4 if (base == MAP_FAILED) {
489 0 0 PUBSUB_ERR("mmap(memfd): %s", strerror(errno));
497 0 4 if (!h) { munmap(base, (size_t)total_size); close(fd); return NULL; }
503 4 0 if (errbuf) errbuf[0] = '\0';
506 0 4 if (fstat(fd, &st) < 0) {
507 0 0 PUBSUB_ERR("fstat(fd=%d): %s", fd, strerror(errno));
511 0 4 if ((uint64_t)st.st_size < sizeof(PubSubHeader)) {
512 0 0 PUBSUB_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size);
518 0 4 if (base == MAP_FAILED) {
519 0 0 PUBSUB_ERR("mmap(fd=%d): %s", fd, strerror(errno));
523 0 4 if (!pubsub_validate_header((PubSubHeader *)base, mode, (uint64_t)st.st_size)) {
524 0 0 PUBSUB_ERR("fd %d: invalid or incompatible pubsub", fd);
530 0 4 if (myfd < 0) {
531 0 0 PUBSUB_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno));
537 0 4 if (!h) { munmap(base, map_size); close(myfd); return NULL; }
543 0 136 if (!h) return;
544 13 123 if (h->notify_fd >= 0) close(h->notify_fd);
545 8 128 if (h->backing_fd >= 0) close(h->backing_fd);
546 136 0 if (h->hdr) munmap(h->hdr, h->mmap_size);
557 0 98 if (!sub) return NULL;
569 64 34 if (from_oldest && wp > h->capacity)
1 63 if (from_oldest && wp > h->capacity)
571 63 34 else if (from_oldest)
580 0 98 if (!sub) return;
591 18 8 return (wp > sub->cursor) ? (wp - sub->cursor) : 0;
699 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)
29 1183 DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t)
2 1181 DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t)
0 1181 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 1181 DEFINE_INT_PUBSUB(int, PubSubIntSlot, int64_t, uint64_t, int64_t)
1212 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)
702 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 30 DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t)
1 29 DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t)
0 29 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 29 DEFINE_INT_PUBSUB(int32, PubSubInt32Slot, int32_t, uint32_t, int32_t)
37 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)
705 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 30 DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t)
1 29 DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t)
0 29 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 29 DEFINE_INT_PUBSUB(int16, PubSubInt16Slot, int16_t, uint32_t, int32_t)
37 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)
718 1 193 if (len > h->msg_size) return -1;
731 3 190 if (alloc == 0) alloc = 8;
733 1 192 if ((uint64_t)apos + alloc > h->arena_cap)
739 2 191 slot->packed_len = len | (utf8 ? PUBSUB_STR_UTF8_FLAG : 0);
752 2 181 if (len > h->msg_size) return -1;
756 181 0 if (r == 1) pubsub_wake_subscribers(h->hdr);
766 134 0 for (int attempt = 0; attempt < PUBSUB_POLL_RETRIES; attempt++) {
770 21 113 if (cursor >= wp) return 0;
772 2 111 if (wp - cursor > sub->capacity) {
782 0 111 if (seq1 != cursor + 1) {
783 0 0 if (seq1 > cursor + 1) {
784 0 0 uint64_t new_cursor = wp > sub->capacity ? wp - sub->capacity : 0;
785 0 0 if (new_cursor > cursor)
799 0 111 if (len > sub->msg_size) continue;
800 0 111 if ((uint64_t)aoff + len > sub->hdr->arena_cap) continue;
802 0 111 if (!pubsub_ensure_copy_buf(sub, len + 1)) return 0;
804 108 3 if (len > 0)
809 0 111 if (seq2 != seq1) continue;
824 50 5 if (r != 0) return r;
825 0 5 if (timeout == 0.0) return 0;
830 5 0 if (has_deadline) pubsub_make_deadline(timeout, &deadline);
835 0 5 if (r != 0) return r;
839 5 0 if (has_deadline) {
840 0 5 if (!pubsub_remaining_time(&deadline, &remaining)) {
851 4 1 if (r != 0) return r;
852 1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
862 2 6 if (hdr->mode == PUBSUB_MODE_STR)
867 2 6 if (hdr->mode == PUBSUB_MODE_STR)
872 4 4 if (hdr->mode == PUBSUB_MODE_INT) {
874 256 4 for (uint32_t i = 0; i < cap; i++)
876 1 3 } else if (hdr->mode == PUBSUB_MODE_INT32) {
878 64 1 for (uint32_t i = 0; i < cap; i++)
880 1 2 } else if (hdr->mode == PUBSUB_MODE_INT16) {
882 64 1 for (uint32_t i = 0; i < cap; i++)
886 80 2 for (uint32_t i = 0; i < cap; i++)
890 2 6 if (hdr->mode == PUBSUB_MODE_STR)
900 0 13 if (h->notify_fd >= 0) return h->notify_fd;
906 1 0 if (h->notify_fd >= 0 && h->notify_fd != fd)
0 1 if (h->notify_fd >= 0 && h->notify_fd != fd)
912 8 0 if (h->notify_fd >= 0) {
919 6 0 if (h->notify_fd >= 0) {
926 4 0 if (sub->notify_fd >= 0) {