diff options
Diffstat (limited to 'src/util/util.h')
| -rw-r--r-- | src/util/util.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/util/util.h b/src/util/util.h new file mode 100644 index 0000000..6ef3c86 --- /dev/null +++ b/src/util/util.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* See LICENSE file for copyright and license details. */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * MAX(A, B) | ||
| 5 | * Macro to compute the maximum of two values. | ||
| 6 | * It returns the greater of the two values, A or B. | ||
| 7 | * Usage: MAX(3, 5) -> 5 | ||
| 8 | */ | ||
| 9 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) | ||
| 10 | |||
| 11 | /* | ||
| 12 | * MIN(A, B) | ||
| 13 | * Macro to compute the minimum of two values. | ||
| 14 | * It returns the lesser of the two values, A or B. | ||
| 15 | * Usage: MIN(3, 5) -> 3 | ||
| 16 | */ | ||
| 17 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) | ||
| 18 | |||
| 19 | /* | ||
| 20 | * BETWEEN(X, A, B) | ||
| 21 | * Macro to check if a value X is between two values A and B (inclusive). | ||
| 22 | * It returns true (1) if X is between A and B, and false (0) otherwise. | ||
| 23 | * Usage: BETWEEN(4, 1, 5) -> 1 (true), BETWEEN(6, 1, 5) -> 0 (false) | ||
| 24 | */ | ||
| 25 | #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) | ||
| 26 | |||
| 27 | /* | ||
| 28 | * die() | ||
| 29 | * Function to print an error message and terminate the program. | ||
| 30 | * Takes a formatted string (`fmt`) and additional arguments (as in `printf`). | ||
| 31 | * This function uses variable arguments and will print the formatted error | ||
| 32 | * message to `stderr` and then exit the program with a non-zero status | ||
| 33 | * (typically 1). | ||
| 34 | * | ||
| 35 | * Usage: | ||
| 36 | * die("Error occurred in function %s", __func__); | ||
| 37 | */ | ||
| 38 | void die(const char *fmt, ...); | ||
| 39 | |||
| 40 | /* | ||
| 41 | * ecalloc() | ||
| 42 | * Function to allocate memory for an array of elements, initializing the memory | ||
| 43 | * to zero. This function works like `calloc()`, but it adds error handling. If | ||
| 44 | * memory allocation fails, it will call the `die()` function to print an error | ||
| 45 | * message and terminate the program. | ||
| 46 | * | ||
| 47 | * Parameters: | ||
| 48 | * - `nmemb`: The number of elements to allocate memory for. | ||
| 49 | * - `size`: The size of each element in bytes. | ||
| 50 | * | ||
| 51 | * Returns: | ||
| 52 | * - A pointer to the allocated memory (of type `void *`). | ||
| 53 | * - If the allocation fails, the program will terminate via `die()`. | ||
| 54 | * | ||
| 55 | * Usage: | ||
| 56 | * int *arr = ecalloc(10, sizeof(int)); // Allocate memory for an array of 10 | ||
| 57 | * integers. | ||
| 58 | */ | ||
| 59 | void *ecalloc(size_t nmemb, size_t size); | ||
