utils/src/utils.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * SPDX-License-Identifier: MIT | ||
| 3 | * | ||
| 4 | * Copyright (c) 2025 Manuel Hernández Méndez | ||
| 5 | * | ||
| 6 | * Authors: | ||
| 7 | * Manuel Hernández Méndez <maherme.dev@gmail.com> | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include "utils.h" | ||
| 11 | #include <stdio.h> | ||
| 12 | #include <stdlib.h> | ||
| 13 | #include <time.h> | ||
| 14 | |||
| 15 | void * | ||
| 16 | 2 | utilsCalloc(size_t nelem, size_t elsize) | |
| 17 | { | ||
| 18 | 2 | void *ret = calloc(nelem, elsize); | |
| 19 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 1 time.
|
2 | if (!ret) |
| 20 | { | ||
| 21 | 1 | perror("calloc"); | |
| 22 | 1 | exit(EXIT_FAILURE); | |
| 23 | } | ||
| 24 | 1 | return ret; | |
| 25 | } | ||
| 26 | |||
| 27 | bool | ||
| 28 | 5 | utilsCheckTimeout(struct timespec time, long long timeout_ns) | |
| 29 | { | ||
| 30 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 4 times.
|
5 | if (timeout_ns < 0) |
| 31 | { | ||
| 32 | 1 | return false; | |
| 33 | } | ||
| 34 | |||
| 35 | 4 | struct timespec current_time; | |
| 36 | 4 | clock_gettime(CLOCK_MONOTONIC, ¤t_time); | |
| 37 | 4 | long long delta_time = (current_time.tv_sec - time.tv_sec) * NS_PER_S + (current_time.tv_nsec - time.tv_nsec); | |
| 38 |
2/2✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 2 times.
|
4 | if (delta_time >= timeout_ns) |
| 39 | { | ||
| 40 | 2 | return true; | |
| 41 | } | ||
| 42 | |||
| 43 | 2 | return false; | |
| 44 | } | ||
| 45 |