Branch Coverage

stack.h
Criterion Covered Total %
branch 114 246 46.3


line true false branch
99 0 3 if (dl->tv_nsec >= 1000000000L) { dl->tv_sec++; dl->tv_nsec -= 1000000000L; }
107 3 2 if (rem->tv_nsec < 0) { rem->tv_sec--; rem->tv_nsec += 1000000000L; }
127 63 0 if (STK_SLOT_STATE(c) == STK_SLOT_EMPTY) {
129 63 0 if (__atomic_compare_exchange_n(ctl_word, &c, nc,
155 18039 0 if (STK_SLOT_STATE(c) == STK_SLOT_FILLED) {
157 18039 0 if (__atomic_compare_exchange_n(ctl_word, &c, nc,
178 4 63 if (t >= cap) return 0;
179 63 0 if (__atomic_compare_exchange_n(&hdr->top, &t, t + 1,
185 0 63 if (cp < sz) memset(stk_slot(h, t) + cp, 0, sz - cp);
188 0 63 if (__atomic_load_n(&hdr->waiters_pop, __ATOMIC_RELAXED) > 0) {
198 0 1 if (stk_try_push(h, val, vlen)) return 1;
199 0 1 if (timeout == 0) return 0;
205 1 0 if (has_dl) stk_make_deadline(timeout, &dl);
212 1 0 if (t >= cap) {
214 1 0 if (has_dl) {
215 0 1 if (!stk_remaining(&dl, &rem)) {
225 0 1 if (stk_try_push(h, val, vlen)) return 1;
226 1 0 if (has_dl && !stk_remaining(&dl, &rem)) {
1 0 if (has_dl && !stk_remaining(&dl, &rem)) {
241 8 18039 if (t == 0) return 0;
242 18039 0 if (__atomic_compare_exchange_n(&hdr->top, &t, t - 1,
248 0 18039 if (__atomic_load_n(&hdr->waiters_push, __ATOMIC_RELAXED) > 0) {
258 0 2 if (stk_try_pop(h, out)) return 1;
259 0 2 if (timeout == 0) return 0;
264 2 0 if (has_dl) stk_make_deadline(timeout, &dl);
271 2 0 if (t == 0) {
273 2 0 if (has_dl) {
274 0 2 if (!stk_remaining(&dl, &rem)) {
284 1 1 if (stk_try_pop(h, out)) return 1;
285 1 0 if (has_dl && !stk_remaining(&dl, &rem)) {
1 0 if (has_dl && !stk_remaining(&dl, &rem)) {
300 3 0 for (int tries = 0; tries < 64; tries++) {
302 0 3 if (t == 0) return 0;
304 0 3 if (STK_SLOT_STATE(c1) != STK_SLOT_FILLED) {
311 3 0 if (c1 == c2 && t == t2) return 1;
3 0 if (c1 == c2 && t == t2) return 1;
355 0 17 if (!h) { munmap(base, msize); return NULL; }
361 3 14 h->path = path ? strdup(path) : NULL;
370 1 2 if (hdr->magic != STK_MAGIC) return 0;
371 0 2 if (hdr->version != STK_VERSION) return 0;
372 0 2 if (hdr->variant_id != expected_variant) return 0;
373 2 0 if (hdr->elem_size == 0 || hdr->capacity == 0) return 0;
0 2 if (hdr->elem_size == 0 || hdr->capacity == 0) return 0;
374 0 2 if (hdr->capacity > 0x7FFFFFFFu) return 0;
375 0 2 if (hdr->total_size != file_size) return 0;
376 0 2 if (hdr->data_off != sizeof(StkHeader)) return 0;
377 0 2 if (hdr->ctl_off != stk_ctl_offset(hdr->elem_size, hdr->capacity)) return 0;
378 0 2 if (hdr->total_size != stk_total_size(hdr->elem_size, hdr->capacity)) return 0;
385 16 0 if (errbuf) errbuf[0] = '\0';
386 0 16 if (capacity == 0) { STK_ERR("capacity must be > 0"); return NULL; }
0 0 if (capacity == 0) { STK_ERR("capacity must be > 0"); return NULL; }
387 0 16 if (elem_size == 0) { STK_ERR("elem_size must be > 0"); return NULL; }
0 0 if (elem_size == 0) { STK_ERR("elem_size must be > 0"); return NULL; }
388 0 16 if (capacity > (UINT64_MAX - sizeof(StkHeader) - 16) / (elem_size + sizeof(uint64_t))) {
389 0 0 STK_ERR("capacity * elem_size overflow"); return NULL;
398 12 4 if (anonymous) {
402 0 12 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); return NULL; }
0 0 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); return NULL; }
405 0 4 if (fd < 0) { STK_ERR("open(%s): %s", path, strerror(errno)); return NULL; }
0 0 if (fd < 0) { STK_ERR("open(%s): %s", path, strerror(errno)); return NULL; }
406 0 4 if (flock(fd, LOCK_EX) < 0) { STK_ERR("flock: %s", strerror(errno)); close(fd); return NULL; }
0 0 if (flock(fd, LOCK_EX) < 0) { STK_ERR("flock: %s", strerror(errno)); close(fd); return NULL; }
409 0 4 if (fstat(fd, &st) < 0) {
410 0 0 STK_ERR("fstat: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL;
413 2 2 if (!is_new && (uint64_t)st.st_size < sizeof(StkHeader)) {
0 2 if (!is_new && (uint64_t)st.st_size < sizeof(StkHeader)) {
414 0 0 STK_ERR("%s: file too small (%lld)", path, (long long)st.st_size);
418 2 2 if (is_new) {
419 0 2 if (ftruncate(fd, (off_t)total) < 0) {
420 0 0 STK_ERR("ftruncate: %s", strerror(errno));
424 2 2 map_size = is_new ? (size_t)total : (size_t)st.st_size;
426 0 4 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
0 0 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
428 2 2 if (!is_new) {
429 1 1 if (!stk_validate_header((StkHeader *)base, (uint64_t)st.st_size, variant_id)) {
430 1 0 STK_ERR("invalid or incompatible stack file");
439 2 12 if (fd >= 0) { flock(fd, LOCK_UN); close(fd); }
446 1 0 if (errbuf) errbuf[0] = '\0';
447 0 1 if (capacity == 0) { STK_ERR("capacity must be > 0"); return NULL; }
0 0 if (capacity == 0) { STK_ERR("capacity must be > 0"); return NULL; }
448 0 1 if (elem_size == 0) { STK_ERR("elem_size must be > 0"); return NULL; }
0 0 if (elem_size == 0) { STK_ERR("elem_size must be > 0"); return NULL; }
449 0 1 if (capacity > (UINT64_MAX - sizeof(StkHeader) - 16) / (elem_size + sizeof(uint64_t))) {
450 0 0 STK_ERR("capacity * elem_size overflow"); return NULL;
454 1 0 int fd = memfd_create(name ? name : "stack", MFD_CLOEXEC | MFD_ALLOW_SEALING);
455 0 1 if (fd < 0) { STK_ERR("memfd_create: %s", strerror(errno)); return NULL; }
0 0 if (fd < 0) { STK_ERR("memfd_create: %s", strerror(errno)); return NULL; }
456 0 1 if (ftruncate(fd, (off_t)total) < 0) { STK_ERR("ftruncate: %s", strerror(errno)); close(fd); return NULL; }
0 0 if (ftruncate(fd, (off_t)total) < 0) { STK_ERR("ftruncate: %s", strerror(errno)); close(fd); return NULL; }
459 0 1 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); close(fd); return NULL; }
0 0 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); close(fd); return NULL; }
465 1 0 if (errbuf) errbuf[0] = '\0';
467 0 1 if (fstat(fd, &st) < 0) { STK_ERR("fstat: %s", strerror(errno)); return NULL; }
0 0 if (fstat(fd, &st) < 0) { STK_ERR("fstat: %s", strerror(errno)); return NULL; }
468 0 1 if ((uint64_t)st.st_size < sizeof(StkHeader)) { STK_ERR("too small"); return NULL; }
0 0 if ((uint64_t)st.st_size < sizeof(StkHeader)) { STK_ERR("too small"); return NULL; }
471 0 1 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); return NULL; }
0 0 if (base == MAP_FAILED) { STK_ERR("mmap: %s", strerror(errno)); return NULL; }
472 0 1 if (!stk_validate_header((StkHeader *)base, (uint64_t)st.st_size, variant_id)) {
473 0 0 STK_ERR("invalid stack"); munmap(base, ms); return NULL;
476 0 1 if (myfd < 0) { STK_ERR("fcntl: %s", strerror(errno)); munmap(base, ms); return NULL; }
0 0 if (myfd < 0) { STK_ERR("fcntl: %s", strerror(errno)); munmap(base, ms); return NULL; }
481 0 17 if (!h) return;
482 2 15 if (h->notify_fd >= 0) close(h->notify_fd);
483 2 15 if (h->backing_fd >= 0) close(h->backing_fd);
484 17 0 if (h->hdr) munmap(h->hdr, h->mmap_size);
494 0 3 if (__atomic_load_n(&h->hdr->waiters_push, __ATOMIC_RELAXED) > 0) {
498 0 3 if (__atomic_load_n(&h->hdr->waiters_pop, __ATOMIC_RELAXED) > 0) {
524 1 1 if (t == 0) return 0;
529 5 1 for (uint32_t i = 0; i < t; i++) {
536 5 0 if (st == STK_SLOT_FILLED) {
538 5 0 if (__atomic_compare_exchange_n(&h->ctl[i], &c, nc,
546 0 0 if ((++spins & 0x3F) == 0) {
547 0 0 if (!dl_set) { stk_make_deadline(2.0, &dl); dl_set = 1; }
549 0 0 if (!stk_remaining(&dl, &rem)) {
556 0 0 if (__atomic_compare_exchange_n(&h->ctl[i], &c, nc,
568 0 1 if (__atomic_load_n(&hdr->waiters_push, __ATOMIC_RELAXED) > 0) {
570 0 0 syscall(SYS_futex, &hdr->push_wake_seq, FUTEX_WAKE,
578 0 2 if (h->notify_fd >= 0) return h->notify_fd;
580 0 2 if (efd < 0) return -1;
585 0 3 if (h->notify_fd < 0) return 0;
590 0 2 if (h->notify_fd < 0) return -1;
592 0 2 if (read(h->notify_fd, &v, sizeof(v)) != sizeof(v)) return -1;