Branch Coverage

queue.h
Criterion Covered Total %
branch 306 556 55.0


line true false branch
148 1 95 if (v < 2) return 2;
149 0 95 if (v > 0x80000000U) return 0;
166 159 27 if (needed <= h->copy_buf_cap) return 1;
167 2 25 uint32_t ns = h->copy_buf_cap ? h->copy_buf_cap : 64;
168 0 15 while (ns < needed) { uint32_t n2 = ns * 2; if (n2 <= ns) { ns = needed; break; } ns = n2; }
15 27 while (ns < needed) { uint32_t n2 = ns * 2; if (n2 <= ns) { ns = needed; break; } ns = n2; }
170 0 27 if (!nb) return 0;
185 0 0 if (pid == 0) return 1;
186 0 0 return !(kill((pid_t)pid, 0) == -1 && errno == ESRCH);
0 0 return !(kill((pid_t)pid, 0) == -1 && errno == ESRCH);
192 0 0 if (!__atomic_compare_exchange_n(&hdr->mutex, &observed, 0,
196 0 0 if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0)
204 413 33 if (__atomic_compare_exchange_n(&hdr->mutex, &expected, mypid,
207 32 1 if (__builtin_expect(spin < QUEUE_SPIN_LIMIT, 1)) {
213 1 0 if (cur != 0) {
216 1 0 if (rc == -1 && errno == ETIMEDOUT) {
0 1 if (rc == -1 && errno == ETIMEDOUT) {
219 0 0 if (val >= QUEUE_MUTEX_WRITER_BIT) {
221 0 0 if (!queue_pid_alive(pid))
235 1 412 if (__atomic_load_n(&hdr->mutex_waiters, __ATOMIC_RELAXED) > 0)
241 0 398 if (__atomic_load_n(&hdr->pop_waiters, __ATOMIC_RELAXED) > 0) {
249 0 578 if (__atomic_load_n(&hdr->push_waiters, __ATOMIC_RELAXED) > 0) {
262 18 8 if (remaining->tv_nsec < 0) {
274 1 24 if (deadline->tv_nsec >= 1000000000L) {
289 84 0 if (errbuf) errbuf[0] = '\0';
292 0 84 if (cap == 0) { QUEUE_ERR("invalid capacity"); return NULL; }
0 0 if (cap == 0) { QUEUE_ERR("invalid capacity"); return NULL; }
293 0 84 if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return NULL; }
0 0 if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return NULL; }
297 47 37 : (mode == QUEUE_MODE_INT32) ? sizeof(QueueInt32Slot)
298 39 8 : (mode == QUEUE_MODE_INT16) ? sizeof(QueueInt16Slot)
299 3 36 : sizeof(QueueStrSlot);
303 36 48 if (mode == QUEUE_MODE_STR) {
307 7 29 if (arena_cap < 4096) arena_cap = 4096;
308 0 36 if (arena_cap > UINT32_MAX) arena_cap = UINT32_MAX;
318 18 66 if (anonymous) {
323 0 18 if (base == MAP_FAILED) {
324 0 0 QUEUE_ERR("mmap(anonymous): %s", strerror(errno));
330 0 66 if (fd < 0) { QUEUE_ERR("open(%s): %s", path, strerror(errno)); return NULL; }
0 0 if (fd < 0) { QUEUE_ERR("open(%s): %s", path, strerror(errno)); return NULL; }
332 0 66 if (flock(fd, LOCK_EX) < 0) {
333 0 0 QUEUE_ERR("flock(%s): %s", path, strerror(errno));
338 0 66 if (fstat(fd, &st) < 0) {
339 0 0 QUEUE_ERR("fstat(%s): %s", path, strerror(errno));
345 9 57 if (!is_new && (uint64_t)st.st_size < sizeof(QueueHeader)) {
0 9 if (!is_new && (uint64_t)st.st_size < sizeof(QueueHeader)) {
346 0 0 QUEUE_ERR("%s: file too small (%lld)", path, (long long)st.st_size);
350 57 9 if (is_new) {
351 0 57 if (ftruncate(fd, (off_t)total_size) < 0) {
352 0 0 QUEUE_ERR("ftruncate(%s): %s", path, strerror(errno));
357 9 57 map_size = is_new ? (size_t)total_size : (size_t)st.st_size;
359 0 66 if (base == MAP_FAILED) {
360 0 0 QUEUE_ERR("mmap(%s): %s", path, strerror(errno));
366 9 57 if (!is_new) {
369 9 0 hdr->version == QUEUE_VERSION &&
370 5 4 hdr->mode == mode &&
371 5 0 hdr->capacity > 0 &&
372 5 0 (hdr->capacity & (hdr->capacity - 1)) == 0 &&
373 9 0 hdr->total_size == (uint64_t)st.st_size &&
5 0 hdr->total_size == (uint64_t)st.st_size &&
374 5 0 hdr->slots_off == sizeof(QueueHeader));
375 3 6 if (mode == QUEUE_MODE_STR && valid)
2 1 if (mode == QUEUE_MODE_STR && valid)
376 2 0 valid = (hdr->arena_off > 0 &&
377 2 0 hdr->arena_off + hdr->arena_cap <= hdr->total_size);
378 4 5 if (!valid) {
379 4 0 QUEUE_ERR("%s: invalid or incompatible queue file", path);
411 33 42 if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot, base, slots_off, cap);
2042 33 if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot, base, slots_off, cap);
412 7 35 else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot, base, slots_off, cap);
1416 7 else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot, base, slots_off, cap);
413 2 33 else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot, base, slots_off, cap);
1040 2 else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot, base, slots_off, cap);
423 0 80 if (!h) { munmap(base, map_size); return NULL; }
427 35 45 h->arena = (mode == QUEUE_MODE_STR) ? (char *)base + hdr->arena_off : NULL;
432 62 18 h->path = path ? strdup(path) : NULL;
444 12 0 if (errbuf) errbuf[0] = '\0';
447 0 12 if (cap == 0) { QUEUE_ERR("invalid capacity"); return NULL; }
0 0 if (cap == 0) { QUEUE_ERR("invalid capacity"); return NULL; }
448 0 12 if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return NULL; }
0 0 if (mode > QUEUE_MODE_INT16) { QUEUE_ERR("unknown mode %u", mode); return NULL; }
452 6 6 : (mode == QUEUE_MODE_INT32) ? sizeof(QueueInt32Slot)
453 5 1 : (mode == QUEUE_MODE_INT16) ? sizeof(QueueInt16Slot)
454 1 4 : sizeof(QueueStrSlot);
457 4 8 if (mode == QUEUE_MODE_STR) {
461 0 4 if (arena_cap < 4096) arena_cap = 4096;
462 0 4 if (arena_cap > UINT32_MAX) arena_cap = UINT32_MAX;
468 12 0 int fd = memfd_create(name ? name : "queue", MFD_CLOEXEC);
469 0 12 if (fd < 0) { QUEUE_ERR("memfd_create: %s", strerror(errno)); return NULL; }
0 0 if (fd < 0) { QUEUE_ERR("memfd_create: %s", strerror(errno)); return NULL; }
471 0 12 if (ftruncate(fd, (off_t)total_size) < 0) {
472 0 0 QUEUE_ERR("ftruncate(memfd): %s", strerror(errno));
477 0 12 if (base == MAP_FAILED) {
478 0 0 QUEUE_ERR("mmap(memfd): %s", strerror(errno));
497 6 6 if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot, base, slots_off, cap);
200 6 if (mode == QUEUE_MODE_INT) INIT_SEQ(QueueIntSlot, base, slots_off, cap);
498 1 5 else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot, base, slots_off, cap);
32 1 else if (mode == QUEUE_MODE_INT32) INIT_SEQ(QueueInt32Slot, base, slots_off, cap);
499 1 4 else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot, base, slots_off, cap);
32 1 else if (mode == QUEUE_MODE_INT16) INIT_SEQ(QueueInt16Slot, base, slots_off, cap);
505 0 12 if (!h) { munmap(base, (size_t)total_size); close(fd); return NULL; }
509 4 8 h->arena = (mode == QUEUE_MODE_STR) ? (char *)base + arena_off : NULL;
523 2 0 if (errbuf) errbuf[0] = '\0';
526 0 2 if (fstat(fd, &st) < 0) {
527 0 0 QUEUE_ERR("fstat(fd=%d): %s", fd, strerror(errno));
531 0 2 if ((uint64_t)st.st_size < sizeof(QueueHeader)) {
532 0 0 QUEUE_ERR("fd %d: too small (%lld)", fd, (long long)st.st_size);
538 0 2 if (base == MAP_FAILED) {
539 0 0 QUEUE_ERR("mmap(fd=%d): %s", fd, strerror(errno));
545 2 0 hdr->version == QUEUE_VERSION &&
546 2 0 hdr->mode == mode &&
547 2 0 hdr->capacity > 0 &&
548 2 0 (hdr->capacity & (hdr->capacity - 1)) == 0 &&
549 2 0 hdr->total_size == (uint64_t)st.st_size &&
2 0 hdr->total_size == (uint64_t)st.st_size &&
550 2 0 hdr->slots_off == sizeof(QueueHeader));
551 1 1 if (mode == QUEUE_MODE_STR && valid)
1 0 if (mode == QUEUE_MODE_STR && valid)
552 1 0 valid = (hdr->arena_off > 0 &&
553 1 0 hdr->arena_off + hdr->arena_cap <= hdr->total_size);
554 0 2 if (!valid) {
555 0 0 QUEUE_ERR("fd %d: invalid or incompatible queue", fd);
562 0 2 if (myfd < 0) {
563 0 0 QUEUE_ERR("fcntl(F_DUPFD_CLOEXEC): %s", strerror(errno));
569 0 2 if (!h) { munmap(base, map_size); close(myfd); return NULL; }
573 1 1 h->arena = (mode == QUEUE_MODE_STR) ? (char *)base + hdr->arena_off : NULL;
586 0 94 if (!h) return;
587 10 84 if (h->notify_fd >= 0) close(h->notify_fd);
588 14 80 if (h->backing_fd >= 0) close(h->backing_fd);
589 94 0 if (h->hdr) munmap(h->hdr, h->mmap_size);
741 44 5 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
6 3 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
100 7 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
0 7 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
7 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
0 7 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
7 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
0 7 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
5 2 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
2 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
2 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
10 3 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
1 2 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
2 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
0 2 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
2 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
0 2 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
1 1 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
1 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
1 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
256 38 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
256 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
38 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
155 10 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
155 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
10 0 DEFINE_INT_QUEUE(int, QueueIntSlot, int64_t, uint64_t, int64_t)
744 16 1 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
1 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
99 2 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 2 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
2 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 2 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
2 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 2 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
2 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
138 7 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
138 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
7 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
37 1 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
37 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
1 0 DEFINE_INT_QUEUE(int32, QueueInt32Slot, int32_t, uint32_t, int32_t)
747 0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
12 1 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
12 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
1 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
12 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
12 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
0 0 DEFINE_INT_QUEUE(int16, QueueInt16Slot, int16_t, uint32_t, int32_t)
758 0 187 if (len > QUEUE_STR_LEN_MASK) return -2;
760 11 176 if (hdr->tail - hdr->head >= h->capacity) {
766 1 175 if (alloc == 0) alloc = 8;
771 2 174 if ((uint64_t)pos + alloc > h->arena_cap) {
776 1 175 if ((uint64_t)hdr->arena_used + skip > h->arena_cap) {
777 0 1 if (hdr->tail == hdr->head) {
794 4 171 slot->packed_len = len | (utf8 ? QUEUE_STR_UTF8_FLAG : 0);
810 170 11 if (r == 1) queue_wake_consumers(h->hdr);
819 30 161 if (hdr->tail == hdr->head) {
830 0 161 if (!queue_ensure_copy_buf(h, len + 1))
832 160 1 if (len > 0)
839 48 113 if (hdr->arena_used == 0)
852 137 23 if (r == 1) queue_wake_producers(h->hdr);
859 5 3 if (r != 0) return r; /* 1 = success, -2 = too long */
860 0 3 if (timeout == 0) return 0;
865 3 0 if (has_deadline) queue_make_deadline(timeout, &deadline);
870 0 3 if (r != 0) return r;
874 3 0 if (has_deadline) {
875 0 3 if (!queue_remaining_time(&deadline, &remaining)) {
885 1 2 if (r != 0) return r;
886 2 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
2 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
893 50 7 if (r != 0) return r;
894 0 7 if (timeout == 0) return 0;
899 7 0 if (has_deadline) queue_make_deadline(timeout, &deadline);
904 0 8 if (r != 0) return r;
908 8 0 if (has_deadline) {
909 0 8 if (!queue_remaining_time(&deadline, &remaining)) {
919 5 3 if (r != 0) return r;
920 2 1 if (rc == -1 && errno == ETIMEDOUT) return 0;
2 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
948 1 6 if (hdr->tail == hdr->head) {
956 0 6 if (!queue_ensure_copy_buf(h, len + 1)) {
960 6 0 if (len > 0)
975 0 21 if (len > QUEUE_STR_LEN_MASK) {
981 6 15 if (size >= h->capacity) {
988 0 15 if (alloc == 0) alloc = 8;
993 0 15 if ((uint64_t)pos + alloc > h->arena_cap) {
998 0 15 if ((uint64_t)hdr->arena_used + skip > h->arena_cap) {
999 0 0 if (hdr->tail == hdr->head) {
1018 1 14 slot->packed_len = len | (utf8 ? QUEUE_STR_UTF8_FLAG : 0);
1034 1 2 if (r != 0) return r;
1035 0 2 if (timeout == 0) return 0;
1040 2 0 if (has_deadline) queue_make_deadline(timeout, &deadline);
1045 0 2 if (r != 0) return r;
1049 2 0 if (has_deadline) {
1050 0 2 if (!queue_remaining_time(&deadline, &remaining)) {
1060 1 1 if (r != 0) return r;
1061 1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
1071 7 19 if (hdr->tail == hdr->head) {
1084 0 19 if (!queue_ensure_copy_buf(h, len + 1)) {
1089 19 0 if (len > 0)
1101 0 19 if (slot_alloc == 0) slot_alloc = 8;
1102 16 3 if (slot->arena_off + slot_alloc == hdr->arena_wpos)
1105 7 12 if (hdr->arena_used == 0)
1117 1 2 if (r != 0) return r;
1118 0 2 if (timeout == 0) return 0;
1123 2 0 if (has_deadline) queue_make_deadline(timeout, &deadline);
1128 0 2 if (r != 0) return r;
1132 2 0 if (has_deadline) {
1133 0 2 if (!queue_remaining_time(&deadline, &remaining)) {
1143 1 1 if (r != 0) return r;
1144 1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
1 0 if (rc == -1 && errno == ETIMEDOUT) return 0;
1158 1 10 if (h->notify_fd >= 0) return h->notify_fd;
1164 1 0 if (h->notify_fd >= 0 && h->notify_fd != fd)
1 0 if (h->notify_fd >= 0 && h->notify_fd != fd)
1171 7 1 if (h->notify_fd >= 0) {
1179 10 0 if (h->notify_fd >= 0) {